From 78a963fe334caae564424c6458a8565da957c8be Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 21 Feb 2021 12:26:49 +0900 Subject: Messagingの入力中インジケータを実装 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/api/stream/channels/messaging.ts | 47 +++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'src/server/api/stream/channels/messaging.ts') diff --git a/src/server/api/stream/channels/messaging.ts b/src/server/api/stream/channels/messaging.ts index 8456871e6a..7279da3ece 100644 --- a/src/server/api/stream/channels/messaging.ts +++ b/src/server/api/stream/channels/messaging.ts @@ -12,6 +12,9 @@ export default class extends Channel { private otherpartyId: string | null; private otherparty?: User; private groupId: string | null; + private subCh: string; + private typers: Record = {}; + private emitTypersIntervalId: ReturnType; @autobind public async init(params: any) { @@ -31,14 +34,28 @@ export default class extends Channel { } } - const subCh = this.otherpartyId + this.emitTypersIntervalId = setInterval(this.emitTypers, 5000); + + this.subCh = this.otherpartyId ? `messagingStream:${this.user!.id}-${this.otherpartyId}` : `messagingStream:${this.groupId}`; // Subscribe messaging stream - this.subscriber.on(subCh, data => { + this.subscriber.on(this.subCh, this.onEvent); + } + + @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(); + } + } else { this.send(data); - }); + } } @autobind @@ -60,4 +77,28 @@ export default class extends Channel { break; } } + + @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() { + this.subscriber.off(this.subCh, this.onEvent); + + clearInterval(this.emitTypersIntervalId); + } } -- cgit v1.2.3-freya From 3fa1d2bfc0aff0c94fd299f79e5e072161697bf3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 21 Feb 2021 13:38:03 +0900 Subject: fix for lint --- src/client/scripts/paging.ts | 2 -- src/server/api/stream/channels/messaging.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) (limited to 'src/server/api/stream/channels/messaging.ts') diff --git a/src/client/scripts/paging.ts b/src/client/scripts/paging.ts index a8f122412c..6e3da94124 100644 --- a/src/client/scripts/paging.ts +++ b/src/client/scripts/paging.ts @@ -192,8 +192,6 @@ export default (opts) => ({ this.items = this.items.slice(-opts.displayLimit); this.more = true; } - } else { - } this.items.push(item); // TODO diff --git a/src/server/api/stream/channels/messaging.ts b/src/server/api/stream/channels/messaging.ts index 7279da3ece..4c41dc820b 100644 --- a/src/server/api/stream/channels/messaging.ts +++ b/src/server/api/stream/channels/messaging.ts @@ -98,7 +98,7 @@ export default class extends Channel { @autobind public dispose() { this.subscriber.off(this.subCh, this.onEvent); - + clearInterval(this.emitTypersIntervalId); } } -- cgit v1.2.3-freya