FoundKey/packages/backend/src/remote/activitypub/kernel/flag/index.ts
Johann150 1516ddfc9b
refactor: remove CacheableUser & co
The CacheableUser, CacheableLocalUser and CacheableRemoteUser are
identical types to User, ILocalUser and IRemoteUser so it seems
nonsensical to have different types for them.
2023-05-18 13:25:57 +02:00

33 lines
1.1 KiB
TypeScript

import { In } from 'typeorm';
import config from '@/config/index.js';
import { genId } from '@/misc/gen-id.js';
import { IRemoteUser } from '@/models/entities/user.js';
import { AbuseUserReports, Users } from '@/models/index.js';
import { IFlag, getApIds } from '@/remote/activitypub/type.js';
export default async (actor: IRemoteUser, activity: IFlag): Promise<string> => {
// The object is `(User|Note) | (User|Note)[]`, but since the database schema
// cannot be made to handle every possible case, the target user is the first user
// and everything else is stored by URL.
const uris = getApIds(activity.object);
const userIds = uris.filter(uri => uri.startsWith(config.url + '/users/')).map(uri => uri.split('/').pop()!);
const users = await Users.findBy({
id: In(userIds),
});
if (users.length < 1) return 'skip';
await AbuseUserReports.insert({
id: genId(),
createdAt: new Date(),
targetUserId: users[0].id,
targetUserHost: users[0].host,
reporterId: actor.id,
reporterHost: actor.host,
comment: activity.content,
urls: uris.filter(uri => !uri.startsWith(config.url + '/users/')),
});
return 'ok';
};