From 51af3697f0b0b60adddb71a50128595e818a2a49 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Wed, 6 Dec 2023 15:22:34 -0300 Subject: [PATCH] handle rocket.cat creation and deletion --- .../app/lib/server/functions/deleteUser.ts | 7 +++ apps/meteor/server/startup/initialData.js | 53 +++++++++++-------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/apps/meteor/app/lib/server/functions/deleteUser.ts b/apps/meteor/app/lib/server/functions/deleteUser.ts index 7a01dab2fcd1..24ef854d48f3 100644 --- a/apps/meteor/app/lib/server/functions/deleteUser.ts +++ b/apps/meteor/app/lib/server/functions/deleteUser.ts @@ -25,6 +25,13 @@ import { relinquishRoomOwnerships } from './relinquishRoomOwnerships'; import { updateGroupDMsName } from './updateGroupDMsName'; export async function deleteUser(userId: string, confirmRelinquish = false, deletedBy?: IUser['_id']): Promise { + if (userId === 'rocket.cat') { + throw new Meteor.Error('error-action-not-allowed', 'Deleting the rocket.cat user is not allowed', { + method: 'deleteUser', + action: 'Delete_user', + }); + } + const user = await Users.findOneById(userId, { projection: { username: 1, avatarOrigin: 1, roles: 1, federated: 1 }, }); diff --git a/apps/meteor/server/startup/initialData.js b/apps/meteor/server/startup/initialData.js index 61fbdf9f3328..e8aa8fc984d8 100644 --- a/apps/meteor/server/startup/initialData.js +++ b/apps/meteor/server/startup/initialData.js @@ -24,34 +24,41 @@ Meteor.startup(async () => { Settings.updateValueById('Initial_Channel_Created', true); } - if (!(await Users.findOneById('rocket.cat'))) { - await Users.create({ - _id: 'rocket.cat', - name: 'Rocket.Cat', - username: 'rocket.cat', - status: 'online', - statusDefault: 'online', - utcOffset: 0, - active: true, - type: 'bot', - }); + try { + if (!(await Users.findOneById('rocket.cat'))) { + await Users.create({ + _id: 'rocket.cat', + name: 'Rocket.Cat', + username: 'rocket.cat', + status: 'online', + statusDefault: 'online', + utcOffset: 0, + active: true, + type: 'bot', + }); - await addUserRolesAsync('rocket.cat', ['bot']); + await addUserRolesAsync('rocket.cat', ['bot']); - const buffer = Buffer.from(await Assets.getBinaryAsync('avatars/rocketcat.png')); + const buffer = Buffer.from(await Assets.getBinaryAsync('avatars/rocketcat.png')); - const rs = RocketChatFile.bufferToStream(buffer, 'utf8'); - const fileStore = FileUpload.getStore('Avatars'); - await fileStore.deleteByName('rocket.cat'); + const rs = RocketChatFile.bufferToStream(buffer, 'utf8'); + const fileStore = FileUpload.getStore('Avatars'); + await fileStore.deleteByName('rocket.cat'); - const file = { - userId: 'rocket.cat', - type: 'image/png', - size: buffer.length, - }; + const file = { + userId: 'rocket.cat', + type: 'image/png', + size: buffer.length, + }; - const upload = await fileStore.insert(file, rs); - await Users.setAvatarData('rocket.cat', 'local', upload.etag); + const upload = await fileStore.insert(file, rs); + await Users.setAvatarData('rocket.cat', 'local', upload.etag); + } + } catch (error) { + console.log( + 'Error creating default `rocket.cat` user, if you created a user with this username please remove it and restart the server', + ); + throw error; } if (process.env.ADMIN_PASS) {