This commit is contained in:
syuilo 2020-03-28 18:07:41 +09:00
parent 9ea1ed8559
commit 614a1d74dd
21 changed files with 229 additions and 91 deletions

View file

@ -4,6 +4,7 @@ import { id } from '../id';
import { Note } from './note';
import { FollowRequest } from './follow-request';
import { UserGroupInvitation } from './user-group-invitation';
import { AccessToken } from './access-token';
@Entity()
export class Notification {
@ -35,11 +36,13 @@ export class Notification {
/**
* (initiator)
*/
@Index()
@Column({
...id(),
nullable: true,
comment: 'The ID of sender user of the Notification.'
})
public notifierId: User['id'];
public notifierId: User['id'] | null;
@ManyToOne(type => User, {
onDelete: 'CASCADE'
@ -59,16 +62,19 @@ export class Notification {
* receiveFollowRequest -
* followRequestAccepted -
* groupInvited -
* app -
*/
@Index()
@Column('enum', {
enum: ['follow', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollVote', 'receiveFollowRequest', 'followRequestAccepted', 'groupInvited'],
enum: ['follow', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollVote', 'receiveFollowRequest', 'followRequestAccepted', 'groupInvited', 'app'],
comment: 'The type of the Notification.'
})
public type: 'follow' | 'mention' | 'reply' | 'renote' | 'quote' | 'reaction' | 'pollVote' | 'receiveFollowRequest' | 'followRequestAccepted' | 'groupInvited';
public type: 'follow' | 'mention' | 'reply' | 'renote' | 'quote' | 'reaction' | 'pollVote' | 'receiveFollowRequest' | 'followRequestAccepted' | 'groupInvited' | 'app';
/**
*
*/
@Index()
@Column('boolean', {
default: false,
comment: 'Whether the Notification is read.'
@ -114,10 +120,52 @@ export class Notification {
@Column('varchar', {
length: 128, nullable: true
})
public reaction: string;
public reaction: string | null;
@Column('integer', {
nullable: true
})
public choice: number;
public choice: number | null;
/**
* body
*/
@Column('varchar', {
length: 2048, nullable: true
})
public customBody: string | null;
/**
* header
* ()
*/
@Column('varchar', {
length: 256, nullable: true
})
public customHeader: string | null;
/**
* icon(URL)
* ()
*/
@Column('varchar', {
length: 1024, nullable: true
})
public customIcon: string | null;
/**
* ()
*/
@Index()
@Column({
...id(),
nullable: true
})
public appAccessTokenId: AccessToken['id'] | null;
@ManyToOne(type => AccessToken, {
onDelete: 'CASCADE'
})
@JoinColumn()
public appAccessToken: AccessToken | null;
}

View file

@ -1,5 +1,5 @@
import { EntityRepository, Repository } from 'typeorm';
import { Users, Notes, UserGroupInvitations } from '..';
import { Users, Notes, UserGroupInvitations, AccessTokens } from '..';
import { Notification } from '../entities/notification';
import { ensure } from '../../prelude/ensure';
import { awaitAll } from '../../prelude/await-all';
@ -13,13 +13,14 @@ export class NotificationRepository extends Repository<Notification> {
src: Notification['id'] | Notification,
): Promise<PackedNotification> {
const notification = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const token = notification.appAccessTokenId ? await AccessTokens.findOne(notification.appAccessTokenId).then(ensure) : null;
return await awaitAll({
id: notification.id,
createdAt: notification.createdAt.toISOString(),
type: notification.type,
userId: notification.notifierId,
user: Users.pack(notification.notifier || notification.notifierId),
user: notification.notifierId ? Users.pack(notification.notifier || notification.notifierId) : null,
...(notification.type === 'mention' ? {
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId),
} : {}),
@ -43,6 +44,11 @@ export class NotificationRepository extends Repository<Notification> {
...(notification.type === 'groupInvited' ? {
invitation: UserGroupInvitations.pack(notification.userGroupInvitationId!),
} : {}),
...(notification.type === 'app' ? {
body: notification.customBody,
header: notification.customHeader || token!.name,
icon: notification.customIcon || token!.iconUrl,
} : {}),
});
}