This commit is contained in:
syuilo
2025-03-17 18:59:22 +09:00
parent 0c81f8a28a
commit 86f2ababd1
4 changed files with 93 additions and 19 deletions

View File

@@ -283,7 +283,7 @@ export class ChatService {
}
@bindThis
public async userHistory(meId: MiUser['id'], limit: number) {
public async userHistory(meId: MiUser['id'], limit: number): Promise<MiChatMessage[]> {
const history: MiChatMessage[] = [];
const mutingQuery = this.mutingsRepository.createQueryBuilder('muting')
@@ -324,7 +324,7 @@ export class ChatService {
}
@bindThis
public async roomHistory(meId: MiUser['id'], limit: number) {
public async roomHistory(meId: MiUser['id'], limit: number): Promise<MiChatMessage[]> {
/*
const rooms = await this.userRoomJoiningsRepository.findBy({
userId: meId,
@@ -359,4 +359,24 @@ export class ChatService {
return history;
*/
}
@bindThis
public async getUserReadStateMap(userId: MiUser['id'], otherIds: MiUser['id'][]) {
const readStateMap: Record<MiUser['id'], boolean> = {};
const redisPipeline = this.redisClient.pipeline();
for (const otherId of otherIds) {
redisPipeline.get(`newChatMessageExists:${userId}:${otherId}`);
}
const markers = await redisPipeline.exec();
for (let i = 0; i < otherIds.length; i++) {
const marker = markers[i][1];
readStateMap[otherIds[i]] = marker == null;
}
return readStateMap;
}
}

View File

@@ -46,6 +46,10 @@ export const packedChatMessageSchema = {
optional: true, nullable: true,
ref: 'DriveFile',
},
isRead: {
type: 'boolean',
optional: true, nullable: false,
},
},
} as const;

View File

@@ -48,7 +48,20 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
super(meta, paramDef, async (ps, me) => {
const history = ps.room ? await this.chatService.roomHistory(me.id, ps.limit) : await this.chatService.userHistory(me.id, ps.limit);
return await this.chatMessageEntityService.packMany(history, me);
const packedMessages = await this.chatMessageEntityService.packMany(history, me);
if (ps.room) {
} else {
const otherIds = history.map(m => m.fromUserId === me.id ? m.toUserId! : m.fromUserId!);
const readStateMap = await this.chatService.getUserReadStateMap(me.id, otherIds);
for (const message of packedMessages) {
const otherId = message.fromUserId === me.id ? message.toUserId! : message.fromUserId!;
message.isRead = readStateMap[otherId] ?? false;
}
}
return packedMessages;
});
}
}