Skip to content

Commit

Permalink
chore: handle rocket.cat creation and deletion (#31170)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Jan 23, 2024
1 parent db25519 commit 30183fd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
7 changes: 7 additions & 0 deletions apps/meteor/app/lib/server/functions/deleteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ import { relinquishRoomOwnerships } from './relinquishRoomOwnerships';
import { updateGroupDMsName } from './updateGroupDMsName';

export async function deleteUser(userId: string, confirmRelinquish = false, deletedBy?: IUser['_id']): Promise<void> {
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 },
});
Expand Down
53 changes: 30 additions & 23 deletions apps/meteor/server/startup/initialData.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,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', { projection: { _id: 1 } }))) {
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) {
Expand Down

0 comments on commit 30183fd

Please sign in to comment.