2023-01-13 05:40:33 +01:00
|
|
|
import {
|
|
|
|
readUserMessagingMessage,
|
|
|
|
readGroupMessagingMessage,
|
|
|
|
deliverReadActivity,
|
|
|
|
} from "../../common/read-messaging-message.js";
|
|
|
|
import Channel from "../channel.js";
|
|
|
|
import { UserGroupJoinings, Users, MessagingMessages } from "@/models/index.js";
|
|
|
|
import type { User, ILocalUser, IRemoteUser } from "@/models/entities/user.js";
|
|
|
|
import type { UserGroup } from "@/models/entities/user-group.js";
|
|
|
|
import type { StreamMessages } from "../types.js";
|
2018-10-07 04:06:17 +02:00
|
|
|
|
|
|
|
export default class extends Channel {
|
2023-01-13 05:40:33 +01:00
|
|
|
public readonly chName = "messaging";
|
2018-10-11 16:07:20 +02:00
|
|
|
public static shouldShare = false;
|
2018-11-10 18:22:34 +01:00
|
|
|
public static requireCredential = true;
|
2018-10-11 16:01:57 +02:00
|
|
|
|
2019-05-18 13:36:33 +02:00
|
|
|
private otherpartyId: string | null;
|
2021-06-30 16:33:50 +02:00
|
|
|
private otherparty: User | null;
|
2019-05-18 13:36:33 +02:00
|
|
|
private groupId: string | null;
|
2023-01-13 05:40:33 +01:00
|
|
|
private subCh:
|
|
|
|
| `messagingStream:${User["id"]}-${User["id"]}`
|
|
|
|
| `messagingStream:${UserGroup["id"]}`;
|
|
|
|
private typers: Record<User["id"], Date> = {};
|
2021-02-21 04:26:49 +01:00
|
|
|
private emitTypersIntervalId: ReturnType<typeof setInterval>;
|
2018-10-07 04:06:17 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
constructor(id: string, connection: Channel["connection"]) {
|
2022-02-27 03:07:39 +01:00
|
|
|
super(id, connection);
|
|
|
|
this.onEvent = this.onEvent.bind(this);
|
|
|
|
this.onMessage = this.onMessage.bind(this);
|
|
|
|
this.emitTypers = this.emitTypers.bind(this);
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:06:17 +02:00
|
|
|
public async init(params: any) {
|
2021-06-30 16:33:50 +02:00
|
|
|
this.otherpartyId = params.otherparty;
|
2023-01-13 05:40:33 +01:00
|
|
|
this.otherparty = this.otherpartyId
|
|
|
|
? await Users.findOneByOrFail({ id: this.otherpartyId })
|
|
|
|
: null;
|
2021-06-30 16:33:50 +02:00
|
|
|
this.groupId = params.group;
|
2019-05-18 13:36:33 +02:00
|
|
|
|
|
|
|
// Check joining
|
|
|
|
if (this.groupId) {
|
2022-03-26 07:34:00 +01:00
|
|
|
const joining = await UserGroupJoinings.findOneBy({
|
2019-05-18 13:36:33 +02:00
|
|
|
userId: this.user!.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
userGroupId: this.groupId,
|
2019-05-18 13:36:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (joining == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 04:26:49 +01:00
|
|
|
this.emitTypersIntervalId = setInterval(this.emitTypers, 5000);
|
|
|
|
|
|
|
|
this.subCh = this.otherpartyId
|
2019-05-18 13:36:33 +02:00
|
|
|
? `messagingStream:${this.user!.id}-${this.otherpartyId}`
|
|
|
|
: `messagingStream:${this.groupId}`;
|
2018-10-07 04:06:17 +02:00
|
|
|
|
|
|
|
// Subscribe messaging stream
|
2021-02-21 04:26:49 +01:00
|
|
|
this.subscriber.on(this.subCh, this.onEvent);
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
private onEvent(
|
|
|
|
data:
|
|
|
|
| StreamMessages["messaging"]["payload"]
|
|
|
|
| StreamMessages["groupMessaging"]["payload"],
|
|
|
|
) {
|
|
|
|
if (data.type === "typing") {
|
2021-02-21 04:26:49 +01:00
|
|
|
const id = data.body;
|
|
|
|
const begin = this.typers[id] == null;
|
|
|
|
this.typers[id] = new Date();
|
|
|
|
if (begin) {
|
|
|
|
this.emitTypers();
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-07 04:06:17 +02:00
|
|
|
this.send(data);
|
2021-02-21 04:26:49 +01:00
|
|
|
}
|
2018-10-07 04:06:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public onMessage(type: string, body: any) {
|
|
|
|
switch (type) {
|
2023-01-13 05:40:33 +01:00
|
|
|
case "read":
|
2019-05-18 13:36:33 +02:00
|
|
|
if (this.otherpartyId) {
|
|
|
|
readUserMessagingMessage(this.user!.id, this.otherpartyId, [body.id]);
|
2019-12-14 19:37:19 +01:00
|
|
|
|
|
|
|
// リモートユーザーからのメッセージだったら既読配信
|
2023-01-13 05:40:33 +01:00
|
|
|
if (
|
|
|
|
Users.isLocalUser(this.user!) &&
|
|
|
|
Users.isRemoteUser(this.otherparty!)
|
|
|
|
) {
|
|
|
|
MessagingMessages.findOneBy({ id: body.id }).then((message) => {
|
|
|
|
if (message)
|
|
|
|
deliverReadActivity(
|
|
|
|
this.user as ILocalUser,
|
|
|
|
this.otherparty as IRemoteUser,
|
|
|
|
message,
|
|
|
|
);
|
2019-12-14 19:37:19 +01:00
|
|
|
});
|
|
|
|
}
|
2019-05-18 13:36:33 +02:00
|
|
|
} else if (this.groupId) {
|
|
|
|
readGroupMessagingMessage(this.user!.id, this.groupId, [body.id]);
|
|
|
|
}
|
2018-10-07 04:06:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-21 04:26:49 +01:00
|
|
|
|
|
|
|
private async emitTypers() {
|
|
|
|
const now = new Date();
|
|
|
|
|
|
|
|
// Remove not typing users
|
|
|
|
for (const [userId, date] of Object.entries(this.typers)) {
|
2023-01-13 05:40:33 +01:00
|
|
|
if (now.getTime() - date.getTime() > 5000) this.typers[userId] = undefined;
|
2021-02-21 04:26:49 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const users = await Users.packMany(Object.keys(this.typers), null, {
|
|
|
|
detail: false,
|
|
|
|
});
|
2021-02-21 04:26:49 +01:00
|
|
|
|
|
|
|
this.send({
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "typers",
|
2021-02-21 04:26:49 +01:00
|
|
|
body: users,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public dispose() {
|
|
|
|
this.subscriber.off(this.subCh, this.onEvent);
|
2021-02-21 05:38:03 +01:00
|
|
|
|
2021-02-21 04:26:49 +01:00
|
|
|
clearInterval(this.emitTypersIntervalId);
|
|
|
|
}
|
2018-10-07 04:06:17 +02:00
|
|
|
}
|