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

@ -3,46 +3,26 @@ import pushSw from './push-notification';
import { Notifications, Mutings } from '../models';
import { genId } from '../misc/gen-id';
import { User } from '../models/entities/user';
import { Note } from '../models/entities/note';
import { Notification } from '../models/entities/notification';
import { FollowRequest } from '../models/entities/follow-request';
import { UserGroupInvitation } from '../models/entities/user-group-invitation';
export async function createNotification(
notifieeId: User['id'],
notifierId: User['id'],
type: Notification['type'],
content?: {
noteId?: Note['id'];
reaction?: string;
choice?: number;
followRequestId?: FollowRequest['id'];
userGroupInvitationId?: UserGroupInvitation['id'];
}
data: Partial<Notification>
) {
if (notifieeId === notifierId) {
if (data.notifierId && (notifieeId === data.notifierId)) {
return null;
}
const data = {
// Create notification
const notification = await Notifications.save({
id: genId(),
createdAt: new Date(),
notifieeId: notifieeId,
notifierId: notifierId,
type: type,
isRead: false,
} as Partial<Notification>;
if (content) {
if (content.noteId) data.noteId = content.noteId;
if (content.reaction) data.reaction = content.reaction;
if (content.choice) data.choice = content.choice;
if (content.followRequestId) data.followRequestId = content.followRequestId;
if (content.userGroupInvitationId) data.userGroupInvitationId = content.userGroupInvitationId;
}
// Create notification
const notification = await Notifications.save(data);
...data
} as Partial<Notification>);
const packed = await Notifications.pack(notification);
@ -58,7 +38,7 @@ export async function createNotification(
const mutings = await Mutings.find({
muterId: notifieeId
});
if (mutings.map(m => m.muteeId).includes(notifierId)) {
if (data.notifierId && mutings.map(m => m.muteeId).includes(data.notifierId)) {
return;
}
//#endregion

View file

@ -57,7 +57,9 @@ export async function insertFollowingDoc(followee: User, follower: User) {
});
// 通知を作成
createNotification(follower.id, followee.id, 'followRequestAccepted');
createNotification(follower.id, 'followRequestAccepted', {
notifierId: followee.id,
});
}
if (alreadyFollowed) return;
@ -95,7 +97,9 @@ export async function insertFollowingDoc(followee: User, follower: User) {
Users.pack(follower, followee).then(packed => publishMainStream(followee.id, 'followed', packed)),
// 通知を作成
createNotification(followee.id, follower.id, 'follow');
createNotification(followee.id, 'follow', {
notifierId: follower.id
});
}
}

View file

@ -50,7 +50,8 @@ export default async function(follower: User, followee: User, requestId?: string
}).then(packed => publishMainStream(followee.id, 'meUpdated', packed));
// 通知を作成
createNotification(followee.id, follower.id, 'receiveFollowRequest', {
createNotification(followee.id, 'receiveFollowRequest', {
notifierId: follower.id,
followRequestId: followRequest.id
});
}

View file

@ -78,7 +78,8 @@ class NotificationManager {
// 通知される側のユーザーが通知する側のユーザーをミュートしていない限りは通知する
if (!mentioneesMutedUserIds.includes(this.notifier.id)) {
createNotification(x.target, this.notifier.id, x.reason, {
createNotification(x.target, x.reason, {
notifierId: this.notifier.id,
noteId: this.note.id
});
}

View file

@ -48,7 +48,8 @@ export default async function(user: User, note: Note, choice: number) {
});
// Notify
createNotification(note.userId, user.id, 'pollVote', {
createNotification(note.userId, 'pollVote', {
notifierId: user.id,
noteId: note.id,
choice: choice
});
@ -60,7 +61,8 @@ export default async function(user: User, note: Note, choice: number) {
})
.then(watchers => {
for (const watcher of watchers) {
createNotification(watcher.userId, user.id, 'pollVote', {
createNotification(watcher.userId, 'pollVote', {
notifierId: user.id,
noteId: note.id,
choice: choice
});

View file

@ -66,7 +66,8 @@ export default async (user: User, note: Note, reaction?: string) => {
// リアクションされたユーザーがローカルユーザーなら通知を作成
if (note.userHost === null) {
createNotification(note.userId, user.id, 'reaction', {
createNotification(note.userId, 'reaction', {
notifierId: user.id,
noteId: note.id,
reaction: reaction
});
@ -78,7 +79,8 @@ export default async (user: User, note: Note, reaction?: string) => {
userId: Not(user.id)
}).then(watchers => {
for (const watcher of watchers) {
createNotification(watcher.userId, user.id, 'reaction', {
createNotification(watcher.userId, 'reaction', {
notifierId: user.id,
noteId: note.id,
reaction: reaction
});