forked from misskey-dev/misskey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request misskey-dev#16 from nadesuki/nade_1.3.0
Nadesskey 1.3.0
- Loading branch information
Showing
34 changed files
with
498 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions
31
packages/backend/migration/1682932943132-note-notification.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import Redis from 'ioredis'; | ||
import type { NoteNotification } from '@/models/entities/NoteNotification.js'; | ||
import type { User } from '@/models/entities/User.js'; | ||
import type { Note } from '@/models/entities/Note.js'; | ||
import { IdService } from '@/core/IdService.js'; | ||
import { GlobalEventService } from '@/core/GlobalEventService.js'; | ||
import { NotificationService } from '@/core/NotificationService.js'; | ||
import { PushNotificationService } from '@/core/PushNotificationService.js'; | ||
import { DI } from '@/di-symbols.js'; | ||
import type { NoteNotificationsRepository } from '@/models/index.js'; | ||
import { UtilityService } from '@/core/UtilityService.js'; | ||
import { bindThis } from '@/decorators.js'; | ||
import { StreamMessages } from '@/server/api/stream/types.js'; | ||
import type { OnApplicationShutdown } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class NoteNotificationService implements OnApplicationShutdown { | ||
private targetUsersFetched: boolean; | ||
private targetUsers: NoteNotification[]; | ||
|
||
constructor( | ||
@Inject(DI.redisForSub) | ||
private redisForSub: Redis.Redis, | ||
|
||
@Inject(DI.noteNotificationsRepository) | ||
private noteNotificationsRepository: NoteNotificationsRepository, | ||
|
||
private notificationService: NotificationService, | ||
) { | ||
this.targetUsersFetched = false; | ||
this.targetUsers = []; | ||
|
||
this.redisForSub.on('message', this.onRedisMessage); | ||
} | ||
|
||
@bindThis | ||
public onApplicationShutdown(signal?: string | undefined) { | ||
this.redisForSub.off('message', this.onRedisMessage); | ||
} | ||
|
||
@bindThis | ||
private async onRedisMessage(_: string, data: string): Promise<void> { | ||
const obj = JSON.parse(data); | ||
|
||
if (obj.channel === 'internal') { | ||
const { type, body } = obj.message as StreamMessages['internal']['payload']; | ||
switch (type) { | ||
case 'noteNotificationCreated': | ||
this.targetUsers.push({ | ||
...body, | ||
createdAt: new Date(body.createdAt), | ||
}); | ||
break; | ||
case 'noteNotificationDeleted': | ||
this.targetUsers = this.targetUsers.filter(a => a.id !== body.id); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
|
||
@bindThis | ||
public async sendNotificationToSubscriber(note: Note, noteUser: { id: User['id']; username: string; host: string | null; }): Promise<void> { | ||
if (!['public', 'home'].includes(note.visibility)) return; | ||
|
||
const targetUsers = await this.getTargetUsers(); | ||
const matchedTargets = targetUsers.filter(x => x.targetUserId === noteUser.id); | ||
|
||
matchedTargets.forEach(x => { | ||
this.notificationService.createNotification(x.userId, 'note', { | ||
notifierId: noteUser.id, | ||
noteId: note.id, | ||
}); | ||
}); | ||
} | ||
|
||
@bindThis | ||
public async getTargetUsers() { | ||
if (!this.targetUsersFetched) { | ||
this.targetUsers = await this.noteNotificationsRepository.find(); | ||
this.targetUsersFetched = true; | ||
} | ||
|
||
return this.targetUsers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.