-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |