Skip to content

Commit

Permalink
Chore: Custom Sounds Endpoints (#25633)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Jun 1, 2022
1 parent db257d3 commit d24789a
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 102 deletions.
20 changes: 0 additions & 20 deletions apps/meteor/app/api/server/lib/custom-sounds.js

This file was deleted.

20 changes: 0 additions & 20 deletions apps/meteor/app/api/server/lib/custom-user-status.js

This file was deleted.

26 changes: 0 additions & 26 deletions apps/meteor/app/api/server/v1/custom-sounds.js

This file was deleted.

29 changes: 29 additions & 0 deletions apps/meteor/app/api/server/v1/custom-sounds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CustomSounds } from '../../../models/server/raw';
import { API } from '../api';

API.v1.addRoute(
'custom-sounds.list',
{ authRequired: true },
{
async get() {
const { offset, count } = this.getPaginationItems();
const { sort, query } = this.parseJsonQuery();
const cursor = await CustomSounds.find(query, {
sort: sort || { name: 1 },
skip: offset,
limit: count,
});

const total = await cursor.count();

const sounds = await cursor.toArray();

return API.v1.success({
sounds,
count: sounds.length,
offset,
total,
});
},
},
);
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';

import { CustomUserStatus } from '../../../models';
import { CustomUserStatus } from '../../../models/server/raw';
import { API } from '../api';
import { findCustomUserStatus } from '../lib/custom-user-status';

API.v1.addRoute(
'custom-user-status.list',
{ authRequired: true },
{
get() {
async get() {
const { offset, count } = this.getPaginationItems();
const { sort, query } = this.parseJsonQuery();

return API.v1.success(
Promise.await(
findCustomUserStatus({
query,
pagination: {
offset,
count,
sort,
},
}),
),
);
const cursor = await CustomUserStatus.find(query, {
sort: sort || { name: 1 },
skip: offset,
limit: count,
});

const total = await cursor.count();

const statuses = await cursor.toArray();

return API.v1.success({
statuses,
count: statuses.length,
offset,
total,
});
},
},
);
Expand All @@ -33,7 +36,7 @@ API.v1.addRoute(
'custom-user-status.create',
{ authRequired: true },
{
post() {
async post() {
check(this.bodyParams, {
name: String,
statusType: Match.Maybe(String),
Expand All @@ -44,12 +47,15 @@ API.v1.addRoute(
statusType: this.bodyParams.statusType,
};

Meteor.runAsUser(this.userId, () => {
Meteor.call('insertOrUpdateUserStatus', userStatusData);
});
Meteor.call('insertOrUpdateUserStatus', userStatusData);

const customUserStatus = await CustomUserStatus.findOneByName(userStatusData.name);
if (!customUserStatus) {
throw new Meteor.Error('error-creating-custom-user-status', 'Error creating custom user status');
}

return API.v1.success({
customUserStatus: CustomUserStatus.findOneByName(userStatusData.name),
customUserStatus,
});
},
},
Expand All @@ -65,7 +71,7 @@ API.v1.addRoute(
return API.v1.failure('The "customUserStatusId" params is required!');
}

Meteor.runAsUser(this.userId, () => Meteor.call('deleteCustomUserStatus', customUserStatusId));
Meteor.call('deleteCustomUserStatus', customUserStatusId);

return API.v1.success();
},
Expand All @@ -76,7 +82,7 @@ API.v1.addRoute(
'custom-user-status.update',
{ authRequired: true },
{
post() {
async post() {
check(this.bodyParams, {
_id: String,
name: String,
Expand All @@ -89,19 +95,23 @@ API.v1.addRoute(
statusType: this.bodyParams.statusType,
};

const customUserStatus = CustomUserStatus.findOneById(userStatusData._id);
const customUserStatusToUpdate = await CustomUserStatus.findOneById(userStatusData._id);

// Ensure the message exists
if (!customUserStatus) {
if (!customUserStatusToUpdate) {
return API.v1.failure(`No custom user status found with the id of "${userStatusData._id}".`);
}

Meteor.runAsUser(this.userId, () => {
Meteor.call('insertOrUpdateUserStatus', userStatusData);
});
Meteor.call('insertOrUpdateUserStatus', userStatusData);

const customUserStatus = await CustomUserStatus.findOneById(userStatusData._id);

if (!customUserStatus) {
throw new Meteor.Error('error-updating-custom-user-status', 'Error updating custom user status');
}

return API.v1.success({
customUserStatus: CustomUserStatus.findOneById(userStatusData._id),
customUserStatus,
});
},
},
Expand Down
7 changes: 5 additions & 2 deletions apps/meteor/app/models/server/raw/CustomUserStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ export class CustomUserStatusRaw extends BaseRaw<T> {
}

// find one by name
async findOneByName(name: string, options: WithoutProjection<FindOneOptions<T>>): Promise<T | null> {
return this.findOne({ name }, options);

async findOneByName(name: string, options?: undefined): Promise<T | null>;

async findOneByName(name: string, options?: WithoutProjection<FindOneOptions<T>>): Promise<T | null> {
return options ? this.findOne({ name }, options) : this.findOne({ name });
}

// find
Expand Down
7 changes: 2 additions & 5 deletions packages/rest-typings/src/v1/customSounds.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { ICustomSound } from '../../../core-typings/dist';
import type { PaginatedRequest } from '../helpers/PaginatedRequest';
import type { PaginatedResult } from '../helpers/PaginatedResult';

export type CustomSoundEndpoint = {
'custom-sounds.list': {
GET: (params: PaginatedRequest<{ query: string }>) => PaginatedResult<{
sounds: {
_id: string;
name: string;
extension: string;
}[];
sounds: ICustomSound[];
}>;
};
};
15 changes: 14 additions & 1 deletion packages/rest-typings/src/v1/customUserStatus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IUserStatus } from '@rocket.chat/core-typings';
import type { ICustomUserStatus, IUserStatus } from '@rocket.chat/core-typings';

import type { PaginatedRequest } from '../helpers/PaginatedRequest';
import type { PaginatedResult } from '../helpers/PaginatedResult';
Expand All @@ -9,4 +9,17 @@ export type CustomUserStatusEndpoints = {
statuses: IUserStatus[];
}>;
};
'custom-user-status.create': {
POST: (params: { name: string; statusType?: string }) => {
customUserStatus: ICustomUserStatus;
};
};
'custom-user-status.delete': {
POST: (params: { customUserStatusId: string }) => void;
};
'custom-user-status.update': {
POST: (params: { id: string; name?: string; statusType?: string }) => {
customUserStatus: ICustomUserStatus;
};
};
};

0 comments on commit d24789a

Please sign in to comment.