File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
packages/linejs/client/features/chat Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 1
1
import type { Client } from "../../mod.ts" ;
2
2
import type * as line from "@evex/linejs-types" ;
3
3
import { TalkMessage } from "../message/talk.ts" ;
4
+ import { createMessageFetcher , type MessageFetcher } from './fetcher.ts'
4
5
5
6
interface ChatInit {
6
7
client : Client ;
@@ -147,6 +148,10 @@ export class Chat {
147
148
) ;
148
149
}
149
150
151
+ messageFetcher ( ) : Promise < MessageFetcher > {
152
+ return createMessageFetcher ( this . #client, this )
153
+ }
154
+
150
155
/**
151
156
* Create a Chat instance from raw message.
152
157
* @param raw Raw message
You can’t perform that action at this time.
0 commit comments