Skip to content

Commit 50d8933

Browse files
committed
refactor: update ICustomSoundsModel.setName method to return the updated sound object
1 parent 7f1966c commit 50d8933

4 files changed

Lines changed: 19 additions & 27 deletions

File tree

apps/meteor/app/custom-sounds/server/methods/insertOrUpdateSound.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,12 @@ Meteor.methods<ServerMethods>({
8787
}
8888

8989
if (soundData.name !== soundData.previousName) {
90-
await CustomSounds.setName(soundData._id, soundData.name);
91-
void api.broadcast('notify.updateCustomSound', {
92-
soundData: {
93-
_id: soundData._id,
94-
name: soundData.name,
95-
extension: soundData.extension,
96-
_updatedAt: undefined!, // FIXME,
97-
},
98-
});
90+
const updatedSound = await CustomSounds.setName(soundData._id, soundData.name);
91+
if (updatedSound) {
92+
void api.broadcast('notify.updateCustomSound', {
93+
soundData: updatedSound,
94+
});
95+
}
9996
}
10097

10198
return soundData._id;

apps/meteor/app/custom-sounds/server/methods/uploadCustomSound.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { api } from '@rocket.chat/core-services';
22
import type { RequiredField } from '@rocket.chat/core-typings';
33
import type { ServerMethods } from '@rocket.chat/ddp-client';
4+
import { CustomSounds } from '@rocket.chat/models';
45
import { Meteor } from 'meteor/meteor';
56

67
import type { ICustomSoundData } from './insertOrUpdateSound';
@@ -28,19 +29,13 @@ Meteor.methods<ServerMethods>({
2829

2930
return new Promise((resolve) => {
3031
const ws = RocketChatFileCustomSoundsInstance.createWriteStream(`${soundData._id}.${soundData.extension}`, contentType);
31-
ws.on('end', () => {
32-
setTimeout(
33-
() =>
34-
api.broadcast('notify.updateCustomSound', {
35-
soundData: {
36-
_id: soundData._id,
37-
name: soundData.name,
38-
extension: soundData.extension,
39-
_updatedAt: undefined!, // FIXME
40-
},
41-
}),
42-
500,
43-
);
32+
ws.on('end', async () => {
33+
setTimeout(async () => {
34+
const sound = await CustomSounds.findOneById(soundData._id);
35+
if (sound) {
36+
void api.broadcast('notify.updateCustomSound', { soundData: sound });
37+
}
38+
}, 500);
4439
resolve();
4540
});
4641

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { ICustomSound } from '@rocket.chat/core-typings';
2-
import type { FindCursor, FindOptions, InsertOneResult, UpdateResult, WithId } from 'mongodb';
2+
import type { FindCursor, FindOptions, InsertOneResult, WithId } from 'mongodb';
33

44
import type { IBaseModel } from './IBaseModel';
55

66
export interface ICustomSoundsModel extends IBaseModel<ICustomSound> {
77
findByName(name: string, options?: FindOptions<ICustomSound>): FindCursor<ICustomSound>;
88
findByNameExceptId(name: string, except: string, options?: FindOptions<ICustomSound>): FindCursor<ICustomSound>;
9-
setName(_id: string, name: string): Promise<UpdateResult>;
9+
setName(_id: string, name: string): Promise<ICustomSound | null>;
1010
create(data: Omit<ICustomSound, '_id' | '_updatedAt'>): Promise<InsertOneResult<WithId<ICustomSound>>>;
1111
}

packages/models/src/models/CustomSounds.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ICustomSound, RocketChatRecordDeleted } from '@rocket.chat/core-typings';
22
import type { ICustomSoundsModel } from '@rocket.chat/model-typings';
3-
import type { Collection, FindCursor, Db, FindOptions, IndexDescription, InsertOneResult, UpdateResult, WithId } from 'mongodb';
3+
import type { Collection, FindCursor, Db, FindOptions, IndexDescription, InsertOneResult, WithId } from 'mongodb';
44

55
import { BaseRaw } from './BaseRaw';
66

@@ -32,14 +32,14 @@ export class CustomSoundsRaw extends BaseRaw<ICustomSound> implements ICustomSou
3232
}
3333

3434
// update
35-
setName(_id: string, name: string): Promise<UpdateResult> {
35+
setName(_id: string, name: string): Promise<ICustomSound | null> {
3636
const update = {
3737
$set: {
3838
name,
3939
},
4040
};
4141

42-
return this.updateOne({ _id }, update);
42+
return this.findOneAndUpdate({ _id }, update, { returnDocument: 'after' });
4343
}
4444

4545
// INSERT

0 commit comments

Comments
 (0)