Skip to content

Commit

Permalink
define methods for API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnouv committed Oct 17, 2023
1 parent 4d54a1a commit a288c85
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 75 deletions.
88 changes: 13 additions & 75 deletions apps/meteor/app/api/server/v1/moderation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IModerationReport, IUser, IUserEmail } from '@rocket.chat/core-typings';
import type { IModerationReport, IUser } from '@rocket.chat/core-typings';
import { ModerationReports, Users } from '@rocket.chat/models';
import {
isReportHistoryProps,
Expand All @@ -12,6 +12,8 @@ import {
import { escapeRegExp } from '@rocket.chat/string-helpers';

import { deleteReportedMessages } from '../../../../server/lib/moderation/deleteReportedMessages';
import { getUserReports } from '../../../../server/lib/moderation/getUserReports';
import { getUserReportsByUid } from '../../../../server/lib/moderation/getUserReportsByUid';
import { API } from '../api';
import { getPaginationItems } from '../helpers/getPaginationItems';

Expand Down Expand Up @@ -72,41 +74,12 @@ API.v1.addRoute(
},
{
async get() {
const { latest: _latest, oldest: _oldest, selector = '' } = this.queryParams;

const { count = 20, offset = 0 } = await getPaginationItems(this.queryParams);

const { sort } = await this.parseJsonQuery();

const latest = _latest ? new Date(_latest) : new Date();

const oldest = _oldest ? new Date(_oldest) : new Date(0);

const escapedSelector = escapeRegExp(selector);

const reports = await ModerationReports.findUserReports(latest, oldest, escapedSelector, {
offset,
count,
sort,
}).toArray();

if (reports.length === 0) {
return API.v1.success({
reports,
count: 0,
offset,
total: 0,
});
try {
const result = await getUserReports(this.queryParams, this.parseJsonQuery.bind(this));
return API.v1.success(result);
} catch (error) {
return API.v1.failure('Error while fetching user reports');
}

const total = await ModerationReports.countUserReportsInRange(latest, oldest, escapedSelector);

return API.v1.success({
reports,
count: reports.length,
offset,
total,
});
},
},
);
Expand Down Expand Up @@ -170,47 +143,12 @@ API.v1.addRoute(
},
{
async get() {
const { userId, selector = '' } = this.queryParams;
const { sort } = await this.parseJsonQuery();
const { count = 50, offset = 0 } = await getPaginationItems(this.queryParams);

const user = await Users.findOneById<IUser>(userId, {
projection: {
_id: 1,
username: 1,
name: 1,
avatarETag: 1,
active: 1,
roles: 1,
emails: 1,
createdAt: 1,
},
});

const escapedSelector = escapeRegExp(selector);
const { cursor, totalCount } = ModerationReports.findUserReportsByReportedUserId(userId, escapedSelector, {
offset,
count,
sort,
});

const [reports, total] = await Promise.all([cursor.toArray(), totalCount]);

const emailSet = new Map<IUserEmail['address'], IUserEmail>();

reports.flatMap((report) => report.reportedUser?.emails ?? []).forEach((email) => emailSet.set(email.address, email));

if (user) {
user.emails = Array.from(emailSet.values());
try {
const result = await getUserReportsByUid(this.queryParams, this.parseJsonQuery.bind(this));
return API.v1.success(result);
} catch (error) {
return API.v1.failure('Error while fetching user reports');
}

return API.v1.success({
user,
reports,
count: reports.length,
total,
offset,
});
},
},
);
Expand Down
5 changes: 5 additions & 0 deletions apps/meteor/server/lib/moderation/definitions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Method, PathPattern } from '@rocket.chat/rest-typings';

import type { ActionThis, Options } from '../../../../app/api/server/definition';

export type ParseJsonQuery = ActionThis<Method, PathPattern, Options>['parseJsonQuery'];
48 changes: 48 additions & 0 deletions apps/meteor/server/lib/moderation/getUserReports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ModerationReports } from '@rocket.chat/models';
import type { ReportHistoryPropsGET } from '@rocket.chat/rest-typings';
import { escapeRegExp } from '@rocket.chat/string-helpers';

import { getPaginationItems } from '../../../app/api/server/helpers/getPaginationItems';
import type { ParseJsonQuery } from './definitions';

export const getUserReports = async (queryParams: ReportHistoryPropsGET, parseJsonQuery: ParseJsonQuery) => {
try {
const { latest: _latest, oldest: _oldest, selector = '' } = queryParams;

const { count = 20, offset = 0 } = await getPaginationItems(queryParams);

const { sort } = await parseJsonQuery();

const latest = _latest ? new Date(_latest) : new Date();

const oldest = _oldest ? new Date(_oldest) : new Date(0);

const escapedSelector = escapeRegExp(selector);

const reports = await ModerationReports.findUserReports(latest, oldest, escapedSelector, {
offset,
count,
sort,
}).toArray();

if (reports.length === 0) {
return {
reports,
count: 0,
offset,
total: 0,
};
}

const total = await ModerationReports.countUserReportsInRange(latest, oldest, escapedSelector);

return {
reports,
count: reports.length,
offset,
total,
};
} catch (error) {
throw new Error('Error while fetching user reports');
}
};
55 changes: 55 additions & 0 deletions apps/meteor/server/lib/moderation/getUserReportsByUid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { IUser, IUserEmail } from '@rocket.chat/core-typings';
import { ModerationReports, Users } from '@rocket.chat/models';
import type { GetUserReportsParamsGET } from '@rocket.chat/rest-typings';
import { escapeRegExp } from '@rocket.chat/string-helpers';

import { getPaginationItems } from '../../../app/api/server/helpers/getPaginationItems';
import type { ParseJsonQuery } from './definitions';

export const getUserReportsByUid = async (queryParams: GetUserReportsParamsGET, parseJsonQuery: ParseJsonQuery) => {
try {
const { userId, selector = '' } = queryParams;
const { sort } = await parseJsonQuery();
const { count = 50, offset = 0 } = await getPaginationItems(queryParams);

const user = await Users.findOneById<IUser>(userId, {
projection: {
_id: 1,
username: 1,
name: 1,
avatarETag: 1,
active: 1,
roles: 1,
emails: 1,
createdAt: 1,
},
});

const escapedSelector = escapeRegExp(selector);
const { cursor, totalCount } = ModerationReports.findUserReportsByReportedUserId(userId, escapedSelector, {
offset,
count,
sort,
});

const [reports, total] = await Promise.all([cursor.toArray(), totalCount]);

const emailSet = new Map<IUserEmail['address'], IUserEmail>();

reports.flatMap((report) => report.reportedUser?.emails ?? []).forEach((email) => emailSet.set(email.address, email));

if (user) {
user.emails = Array.from(emailSet.values());
}

return {
user,
reports,
count: reports.length,
total,
offset,
};
} catch (error) {
throw new Error('Error while fetching users reports');
}
};

0 comments on commit a288c85

Please sign in to comment.