diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2021-02-20 20:20:05 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2021-02-20 20:20:05 +0900 |
| commit | 25d37302a8bfda954c7ede1e9d355db587c82228 (patch) | |
| tree | 8d2eff01cc738a84776976d36cabc8a143b40798 /src/server/api/stream/channels | |
| parent | Improve usability (diff) | |
| download | sharkey-25d37302a8bfda954c7ede1e9d355db587c82228.tar.gz sharkey-25d37302a8bfda954c7ede1e9d355db587c82228.tar.bz2 sharkey-25d37302a8bfda954c7ede1e9d355db587c82228.zip | |
チャンネルで入力中ユーザーを表示するように、Chat UIでタイムラインでは投稿フォームを上に表示するように
Diffstat (limited to 'src/server/api/stream/channels')
| -rw-r--r-- | src/server/api/stream/channels/channel.ts | 39 | ||||
| -rw-r--r-- | src/server/api/stream/channels/games/reversi-game.ts | 2 |
2 files changed, 39 insertions, 2 deletions
diff --git a/src/server/api/stream/channels/channel.ts b/src/server/api/stream/channels/channel.ts index c24b3db937..aa570d1ef4 100644 --- a/src/server/api/stream/channels/channel.ts +++ b/src/server/api/stream/channels/channel.ts @@ -1,14 +1,17 @@ import autobind from 'autobind-decorator'; import Channel from '../channel'; -import { Notes } from '../../../../models'; +import { Notes, Users } from '../../../../models'; import { isMutedUserRelated } from '../../../../misc/is-muted-user-related'; import { PackedNote } from '../../../../models/repositories/note'; +import { User } from '../../../../models/entities/user'; export default class extends Channel { public readonly chName = 'channel'; public static shouldShare = false; public static requireCredential = false; private channelId: string; + private typers: Record<User['id'], Date> = {}; + private emitTypersIntervalId: ReturnType<typeof setInterval>; @autobind public async init(params: any) { @@ -16,6 +19,8 @@ export default class extends Channel { // Subscribe stream this.subscriber.on('notesStream', this.onNote); + this.subscriber.on(`channelStream:${this.channelId}`, this.onEvent); + this.emitTypersIntervalId = setInterval(this.emitTypers, 5000); } @autobind @@ -42,8 +47,40 @@ export default class extends Channel { } @autobind + private onEvent(data: any) { + if (data.type === 'typing') { + const id = data.body; + const begin = this.typers[id] == null; + this.typers[id] = new Date(); + if (begin) { + this.emitTypers(); + } + } + } + + @autobind + private async emitTypers() { + const now = new Date(); + + // Remove not typing users + for (const [userId, date] of Object.entries(this.typers)) { + if (now.getTime() - date.getTime() > 5000) delete this.typers[userId]; + } + + const users = await Users.packMany(Object.keys(this.typers), null, { detail: false }); + + this.send({ + type: 'typers', + body: users, + }); + } + + @autobind public dispose() { // Unsubscribe events this.subscriber.off('notesStream', this.onNote); + this.subscriber.off(`channelStream:${this.channelId}`, this.onEvent); + + clearInterval(this.emitTypersIntervalId); } } diff --git a/src/server/api/stream/channels/games/reversi-game.ts b/src/server/api/stream/channels/games/reversi-game.ts index ea62ab1e88..e1c2116ac6 100644 --- a/src/server/api/stream/channels/games/reversi-game.ts +++ b/src/server/api/stream/channels/games/reversi-game.ts @@ -15,7 +15,7 @@ export default class extends Channel { private gameId: ReversiGame['id'] | null = null; private watchers: Record<User['id'], Date> = {}; - private emitWatchersIntervalId: any; + private emitWatchersIntervalId: ReturnType<typeof setInterval>; @autobind public async init(params: any) { |