2021-03-20 11:05:33 +09:00
|
|
|
import { EntityRepository, In, Repository } from 'typeorm';
|
2021-03-20 13:54:59 +09:00
|
|
|
import { Users, Notes, UserGroupInvitations, AccessTokens, NoteReactions, Emojis } from '..';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { Notification } from '../entities/notification';
|
2019-04-23 22:35:26 +09:00
|
|
|
import { awaitAll } from '../../prelude/await-all';
|
2019-06-27 18:04:09 +09:00
|
|
|
import { SchemaType } from '../../misc/schema';
|
2021-03-20 11:05:33 +09:00
|
|
|
import { Note } from '../entities/note';
|
|
|
|
import { NoteReaction } from '../entities/note-reaction';
|
|
|
|
import { User } from '../entities/user';
|
2021-03-20 13:54:59 +09:00
|
|
|
import { decodeReaction } from '../../misc/reaction-lib';
|
|
|
|
import { Emoji } from '../entities/emoji';
|
2019-04-23 22:35:26 +09:00
|
|
|
|
|
|
|
export type PackedNotification = SchemaType<typeof packedNotificationSchema>;
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
@EntityRepository(Notification)
|
|
|
|
export class NotificationRepository extends Repository<Notification> {
|
|
|
|
public async pack(
|
|
|
|
src: Notification['id'] | Notification,
|
2021-03-20 11:05:33 +09:00
|
|
|
options: {
|
|
|
|
_hintForEachNotes_: {
|
2021-03-20 13:54:59 +09:00
|
|
|
emojis: Emoji[] | null;
|
2021-03-20 11:05:33 +09:00
|
|
|
myReactions: Map<Note['id'], NoteReaction | null>;
|
|
|
|
};
|
|
|
|
}
|
2019-04-23 22:35:26 +09:00
|
|
|
): Promise<PackedNotification> {
|
2021-02-13 15:33:38 +09:00
|
|
|
const notification = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
|
|
|
const token = notification.appAccessTokenId ? await AccessTokens.findOneOrFail(notification.appAccessTokenId) : null;
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2019-04-23 22:35:26 +09:00
|
|
|
return await awaitAll({
|
2019-04-07 21:50:36 +09:00
|
|
|
id: notification.id,
|
2019-04-23 22:35:26 +09:00
|
|
|
createdAt: notification.createdAt.toISOString(),
|
2019-04-07 21:50:36 +09:00
|
|
|
type: notification.type,
|
2020-05-26 14:33:55 +09:00
|
|
|
isRead: notification.isRead,
|
2019-04-07 21:50:36 +09:00
|
|
|
userId: notification.notifierId,
|
2020-03-28 18:07:41 +09:00
|
|
|
user: notification.notifierId ? Users.pack(notification.notifier || notification.notifierId) : null,
|
2019-04-07 21:50:36 +09:00
|
|
|
...(notification.type === 'mention' ? {
|
2021-03-20 11:05:33 +09:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, {
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_
|
|
|
|
}),
|
2019-04-07 21:50:36 +09:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'reply' ? {
|
2021-03-20 11:05:33 +09:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, {
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_
|
|
|
|
}),
|
2019-04-07 21:50:36 +09:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'renote' ? {
|
2021-03-20 11:05:33 +09:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, {
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_
|
|
|
|
}),
|
2019-04-07 21:50:36 +09:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'quote' ? {
|
2021-03-20 11:05:33 +09:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, {
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_
|
|
|
|
}),
|
2019-04-07 21:50:36 +09:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'reaction' ? {
|
2021-03-20 11:05:33 +09:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, {
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_
|
|
|
|
}),
|
2019-04-07 21:50:36 +09:00
|
|
|
reaction: notification.reaction
|
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'pollVote' ? {
|
2021-03-20 11:05:33 +09:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, {
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_
|
|
|
|
}),
|
2019-04-07 21:50:36 +09:00
|
|
|
choice: notification.choice
|
2020-02-13 02:17:54 +09:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'groupInvited' ? {
|
|
|
|
invitation: UserGroupInvitations.pack(notification.userGroupInvitationId!),
|
|
|
|
} : {}),
|
2020-03-28 18:07:41 +09:00
|
|
|
...(notification.type === 'app' ? {
|
|
|
|
body: notification.customBody,
|
2020-03-29 10:49:43 +09:00
|
|
|
header: notification.customHeader || token?.name,
|
|
|
|
icon: notification.customIcon || token?.iconUrl,
|
2020-03-28 18:07:41 +09:00
|
|
|
} : {}),
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
|
|
|
}
|
2019-04-25 13:27:07 +09:00
|
|
|
|
2021-03-20 11:05:33 +09:00
|
|
|
public async packMany(
|
2021-03-19 18:22:14 +09:00
|
|
|
notifications: Notification[],
|
2021-03-20 11:05:33 +09:00
|
|
|
meId: User['id']
|
2019-04-25 13:27:07 +09:00
|
|
|
) {
|
2021-03-20 11:05:33 +09:00
|
|
|
if (notifications.length === 0) return [];
|
|
|
|
|
|
|
|
const notes = notifications.filter(x => x.note != null).map(x => x.note!);
|
|
|
|
const noteIds = notes.map(n => n.id);
|
|
|
|
const myReactionsMap = new Map<Note['id'], NoteReaction | null>();
|
|
|
|
const renoteIds = notes.filter(n => n.renoteId != null).map(n => n.renoteId!);
|
|
|
|
const targets = [...noteIds, ...renoteIds];
|
|
|
|
const myReactions = await NoteReactions.find({
|
|
|
|
userId: meId,
|
|
|
|
noteId: In(targets),
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const target of targets) {
|
|
|
|
myReactionsMap.set(target, myReactions.find(reaction => reaction.noteId === target) || null);
|
|
|
|
}
|
|
|
|
|
2021-03-20 13:54:59 +09:00
|
|
|
// TODO: ここら辺の処理をaggregateEmojisみたいな関数に切り出したい
|
|
|
|
let emojisWhere: any[] = [];
|
|
|
|
for (const note of notes) {
|
|
|
|
if (typeof note !== 'object') continue;
|
|
|
|
emojisWhere.push({
|
|
|
|
name: In(note.emojis),
|
|
|
|
host: note.userHost
|
|
|
|
});
|
|
|
|
if (note.renote) {
|
|
|
|
emojisWhere.push({
|
|
|
|
name: In(note.renote.emojis),
|
|
|
|
host: note.renote.userHost
|
|
|
|
});
|
|
|
|
if (note.renote.user) {
|
|
|
|
emojisWhere.push({
|
|
|
|
name: In(note.renote.user.emojis),
|
|
|
|
host: note.renote.userHost
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const customReactions = Object.keys(note.reactions).map(x => decodeReaction(x)).filter(x => x.name);
|
|
|
|
emojisWhere = emojisWhere.concat(customReactions.map(x => ({
|
|
|
|
name: x.name,
|
|
|
|
host: x.host
|
|
|
|
})));
|
|
|
|
if (note.user) {
|
|
|
|
emojisWhere.push({
|
|
|
|
name: In(note.user.emojis),
|
|
|
|
host: note.userHost
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const emojis = emojisWhere.length > 0 ? await Emojis.find({
|
|
|
|
where: emojisWhere,
|
|
|
|
select: ['name', 'host', 'url']
|
|
|
|
}) : null;
|
|
|
|
|
2021-03-20 11:05:33 +09:00
|
|
|
return await Promise.all(notifications.map(x => this.pack(x, {
|
|
|
|
_hintForEachNotes_: {
|
2021-03-20 13:54:59 +09:00
|
|
|
myReactions: myReactionsMap,
|
|
|
|
emojis: emojis,
|
2021-03-20 11:05:33 +09:00
|
|
|
}
|
|
|
|
})));
|
2019-04-25 13:27:07 +09:00
|
|
|
}
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
2019-04-23 22:35:26 +09:00
|
|
|
|
|
|
|
export const packedNotificationSchema = {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this notification.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the notification was created.'
|
|
|
|
},
|
|
|
|
type: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2020-01-30 04:37:25 +09:00
|
|
|
enum: ['follow', 'followRequestAccepted', 'receiveFollowRequest', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollVote'],
|
2019-04-23 22:35:26 +09:00
|
|
|
description: 'The type of the notification.'
|
|
|
|
},
|
|
|
|
userId: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
user: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'User',
|
2019-06-27 18:04:09 +09:00
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|