From 9e9d378bf1301d4c3fcde08543be1014c71a87cd Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Apr 2020 22:17:17 +0900 Subject: [PATCH] feat(streaming): Add emoji added event --- src/client/init.ts | 5 +++++ src/server/api/endpoints/admin/emoji/add.ts | 5 +++++ src/server/api/stream/index.ts | 9 +++++++++ src/services/stream.ts | 5 +++++ 4 files changed, 24 insertions(+) diff --git a/src/client/init.ts b/src/client/init.ts index 73e02145d..c226f29b3 100644 --- a/src/client/init.ts +++ b/src/client/init.ts @@ -237,6 +237,11 @@ os.init(async () => { // マウント app.$mount('#app'); + os.stream.on('emojiAdded', data => { + // TODO + //store.commit('instance/set', ); + }); + if (store.getters.isSignedIn) { const main = os.stream.useSharedConnection('main'); diff --git a/src/server/api/endpoints/admin/emoji/add.ts b/src/server/api/endpoints/admin/emoji/add.ts index f56303b32..77e29cfb2 100644 --- a/src/server/api/endpoints/admin/emoji/add.ts +++ b/src/server/api/endpoints/admin/emoji/add.ts @@ -7,6 +7,7 @@ import { insertModerationLog } from '../../../../../services/insert-moderation-l import { ApiError } from '../../../error'; import { ID } from '../../../../../misc/cafy-id'; import rndstr from 'rndstr'; +import { publishBroadcastStream } from '../../../../../services/stream'; export const meta = { desc: { @@ -53,6 +54,10 @@ export default define(meta, async (ps, me) => { await getConnection().queryResultCache!.remove(['meta_emojis']); + publishBroadcastStream('emojiAdded', { + emoji: await Emojis.pack(emoji.id) + }); + insertModerationLog(me, 'addEmoji', { emojiId: emoji.id }); diff --git a/src/server/api/stream/index.ts b/src/server/api/stream/index.ts index 2781d14bd..b7cefcf5a 100644 --- a/src/server/api/stream/index.ts +++ b/src/server/api/stream/index.ts @@ -39,6 +39,10 @@ export default class Connection { this.wsConnection.on('message', this.onWsConnectionMessage); + this.subscriber.on('broadcast', async ({ type, body }) => { + this.onBroadcastMessage(type, body); + }); + if (this.user) { this.updateFollowing(); this.followingClock = setInterval(this.updateFollowing, 5000); @@ -72,6 +76,11 @@ export default class Connection { } } + @autobind + private onBroadcastMessage(type: string, body: any) { + this.sendMessageToWs(type, body); + } + /** * APIリクエスト要求時 */ diff --git a/src/services/stream.ts b/src/services/stream.ts index 269aed56b..ec43c6ff2 100644 --- a/src/services/stream.ts +++ b/src/services/stream.ts @@ -19,6 +19,10 @@ class Publisher { })); } + public publishBroadcastStream = (type: string, value?: any): void => { + this.publish('broadcast', type, typeof value === 'undefined' ? null : value); + } + public publishMainStream = (userId: User['id'], type: string, value?: any): void => { this.publish(`mainStream:${userId}`, type, typeof value === 'undefined' ? null : value); } @@ -75,6 +79,7 @@ const publisher = new Publisher(); export default publisher; +export const publishBroadcastStream = publisher.publishBroadcastStream; export const publishMainStream = publisher.publishMainStream; export const publishDriveStream = publisher.publishDriveStream; export const publishNoteStream = publisher.publishNoteStream;