mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
* Typecheck across packages that are built with esbuild * Boilerplate new Errors package * No need, tsup checks with --dts * Switch to tsup * Setup dev script * Add readme * More boilerplaty things * Finish createError function * Install @directus/random * Downgrade node types * Add utility function to check if an error is a DirectusError * Use new is-error check * Install errors package * Add failed validation common error * Export common errors * Move joi convertion to utils * Export failed validation * Use new failed validation error in validate-batch * Enhance typing output of createError * Remove outdir (handled by tsup now) * Replace Exception with Error * Replace exception in test * Remove exceptions from app * Remove exceptions from app * Remove failed validation exception from users service * Remove old failed validation exception from shared * Remove exceptions package in favor of errors * Uninstall exceptions * Replace baseexception check * Migrate content too large error * Critical detail * Replace ForbiddenException * WIP remove exceptions * Add ForbiddenError to errors * HitRateLimitError * Move validation related error/helper to new validation package * Add index * Add docs * Install random * Convert TokenExpired * Convert user-suspended * Convert invalid-credentials * Move UnsupportedMediaType * Replace wrong imports for forbidden * Convert invalid-ip * Move invalid provider * Move InvalidOtp * Convert InvalidToken * Move MethodNotAllowed * Convert range not satisfiable * Move unexpect response * Move UnprocessableContent * Move IllegalAssetTransformation * Move RouteNotFound * Finalize not found * Various db errors * Move value too long * Move not null * Move record-not-unique * Move value out of range * Finish db errors * Service unavailable * GQL errors * Update packages/validation/src/errors/failed-validation.ts Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com> * Update packages/validation/src/errors/failed-validation.ts Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com> * InvalidQuery * Add test for invalid query message constructor * Invalid Payload * Finalize exceptions move * Improve type of isDirectusError * Various fixes * Fix build in api * Update websocket exceptions use * Allow optional reason for invalid config * Update errors usage in utils * Remove unused package from errors * Update lockfile * Update api/src/auth/drivers/ldap.ts Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com> * Update packages/validation/src/utils/joi-to-error-extensions.ts Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com> * Put error codes in shared enum * Replace instanceof checks in api * Fix tests I think * Tweak override names * Fix linter warnings * Set snapshots * Start fixing BB tests * Fix blackbox tests * Add changeset * Update changeset * Update extension docs to use new createError abstraction * 🙄 * Fix graphql validation error name * 🥳 * use ErrorCode.Forbidden * fix blackbox auth login test * Add license files * Rename preMutationException to preMutationError * Remove unused ms dep & sort package.json * Remove periods from error messages for consistency Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com> * Add optional code check * Use updated error code checker * Rename InvalidConfigError to InvalidProviderConfigError --------- Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com> Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch> Co-authored-by: ian <licitdev@gmail.com>
114 lines
4.0 KiB
TypeScript
114 lines
4.0 KiB
TypeScript
import { Action } from '@directus/constants';
|
|
import { isDirectusError } from '@directus/errors';
|
|
import type { Accountability } from '@directus/types';
|
|
import { uniq } from 'lodash-es';
|
|
import validateUUID from 'uuid-validate';
|
|
import env from '../env.js';
|
|
import { ErrorCode } from '../errors/index.js';
|
|
import logger from '../logger.js';
|
|
import type { AbstractServiceOptions, Item, MutationOptions, PrimaryKey } from '../types/index.js';
|
|
import { getPermissions } from '../utils/get-permissions.js';
|
|
import { Url } from '../utils/url.js';
|
|
import { userName } from '../utils/user-name.js';
|
|
import { AuthorizationService } from './authorization.js';
|
|
import { ItemsService } from './items.js';
|
|
import { NotificationsService } from './notifications.js';
|
|
import { UsersService } from './users.js';
|
|
|
|
export class ActivityService extends ItemsService {
|
|
notificationsService: NotificationsService;
|
|
usersService: UsersService;
|
|
|
|
constructor(options: AbstractServiceOptions) {
|
|
super('directus_activity', options);
|
|
this.notificationsService = new NotificationsService({ schema: this.schema });
|
|
this.usersService = new UsersService({ schema: this.schema });
|
|
}
|
|
|
|
override async createOne(data: Partial<Item>, opts?: MutationOptions): Promise<PrimaryKey> {
|
|
if (data['action'] === Action.COMMENT && typeof data['comment'] === 'string') {
|
|
const usersRegExp = new RegExp(/@[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/gi);
|
|
|
|
const mentions = uniq(data['comment'].match(usersRegExp) ?? []);
|
|
|
|
const sender = await this.usersService.readOne(this.accountability!.user!, {
|
|
fields: ['id', 'first_name', 'last_name', 'email'],
|
|
});
|
|
|
|
for (const mention of mentions) {
|
|
const userID = mention.substring(1);
|
|
|
|
const user = await this.usersService.readOne(userID, {
|
|
fields: ['id', 'first_name', 'last_name', 'email', 'role.id', 'role.admin_access', 'role.app_access'],
|
|
});
|
|
|
|
const accountability: Accountability = {
|
|
user: userID,
|
|
role: user['role']?.id ?? null,
|
|
admin: user['role']?.admin_access ?? null,
|
|
app: user['role']?.app_access ?? null,
|
|
};
|
|
|
|
accountability.permissions = await getPermissions(accountability, this.schema);
|
|
|
|
const authorizationService = new AuthorizationService({ schema: this.schema, accountability });
|
|
const usersService = new UsersService({ schema: this.schema, accountability });
|
|
|
|
try {
|
|
await authorizationService.checkAccess('read', data['collection'], data['item']);
|
|
|
|
const templateData = await usersService.readByQuery({
|
|
fields: ['id', 'first_name', 'last_name', 'email'],
|
|
filter: { id: { _in: mentions.map((mention) => mention.substring(1)) } },
|
|
});
|
|
|
|
const userPreviews = templateData.reduce((acc, user) => {
|
|
acc[user['id']] = `<em>${userName(user)}</em>`;
|
|
return acc;
|
|
}, {} as Record<string, string>);
|
|
|
|
let comment = data['comment'];
|
|
|
|
for (const mention of mentions) {
|
|
const uuid = mention.substring(1);
|
|
// We only match on UUIDs in the first place. This is just an extra sanity check
|
|
if (validateUUID(uuid) === false) continue;
|
|
comment = comment.replace(new RegExp(mention, 'gm'), userPreviews[uuid] ?? '@Unknown User');
|
|
}
|
|
|
|
comment = `> ${comment.replace(/\n+/gm, '\n> ')}`;
|
|
|
|
const message = `
|
|
Hello ${userName(user)},
|
|
|
|
${userName(sender)} has mentioned you in a comment:
|
|
|
|
${comment}
|
|
|
|
<a href="${new Url(env['PUBLIC_URL'])
|
|
.addPath('admin', 'content', data['collection'], data['item'])
|
|
.toString()}">Click here to view.</a>
|
|
`;
|
|
|
|
await this.notificationsService.createOne({
|
|
recipient: userID,
|
|
sender: sender['id'],
|
|
subject: `You were mentioned in ${data['collection']}`,
|
|
message,
|
|
collection: data['collection'],
|
|
item: data['item'],
|
|
});
|
|
} catch (err: any) {
|
|
if (isDirectusError(err, ErrorCode.Forbidden)) {
|
|
logger.warn(`User ${userID} doesn't have proper permissions to receive notification for this item.`);
|
|
} else {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return super.createOne(data, opts);
|
|
}
|
|
}
|