Skip to content

Commit a144f18

Browse files
committed
wip: add message fetcher
1 parent 531fd27 commit a144f18

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { Client } from "../../client.ts";
2+
import type { Chat } from "./mod.ts";
3+
import type * as line from '@evex/linejs-types'
4+
import { TalkMessage } from '../message/mod.ts'
5+
6+
export interface MessageFetcher {
7+
fetch: (limit: number) => Promise<TalkMessage[]>
8+
}
9+
export const createMessageFetcher = async (client: Client, chat: Chat) => {
10+
const boxes = await client.base.talk.getMessageBoxes({
11+
messageBoxListRequest: {},
12+
});
13+
const box = boxes.messageBoxes.find((box) => box.id === chat.mid);
14+
if (!box) {
15+
throw new Error("Message box not found.");
16+
}
17+
18+
let lastMessageId: line.MessageBoxV2MessageId = box.lastDeliveredMessageId
19+
return {
20+
async fetch(limit: number) {
21+
const messages = await client.base.talk.getPreviousMessagesV2WithRequest({
22+
request: {
23+
messageBoxId: box.id,
24+
endMessageId: lastMessageId,
25+
messagesCount: limit,
26+
},
27+
});
28+
const lastMessage = messages.at(-1)!
29+
lastMessageId = {
30+
deliveredTime: lastMessage.deliveredTime,
31+
messageId: parseInt(lastMessage.id)
32+
}
33+
34+
return await Promise.all(
35+
messages.map((message) => TalkMessage.fromRawTalk(message, client)),
36+
);
37+
}
38+
}
39+
};

packages/linejs/client/features/chat/mod.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Client } from "../../mod.ts";
22
import type * as line from "@evex/linejs-types";
33
import { TalkMessage } from "../message/talk.ts";
4+
import { createMessageFetcher, type MessageFetcher } from './fetcher.ts'
45

56
interface ChatInit {
67
client: Client;
@@ -147,6 +148,10 @@ export class Chat {
147148
);
148149
}
149150

151+
messageFetcher(): Promise<MessageFetcher> {
152+
return createMessageFetcher(this.#client, this)
153+
}
154+
150155
/**
151156
* Create a Chat instance from raw message.
152157
* @param raw Raw message

0 commit comments

Comments
 (0)