Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
feat(renderer/api/nt): support getting previous messages (#29)
Browse files Browse the repository at this point in the history
* feat(renderer/api/nt): support getting previous messages

* feat(examples/auto-reply): add example for `getPreviousMessages`

* fix(renderer/api/nt): don't throw error when no message available
  • Loading branch information
FlysoftBeta committed Jun 18, 2023
1 parent dc34f4b commit 55f1bc4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/自动回复/qqntim.json
Expand Up @@ -2,5 +2,5 @@
"id": "example-auto-reply",
"name": "示例插件:自动回复",
"author": "Flysoft",
"injections": [{ "type": "renderer", "script": "renderer.js" }]
"injections": [{ "type": "renderer", "script": "renderer.js", "page": "main" }]
}
24 changes: 24 additions & 0 deletions examples/自动回复/renderer.js
@@ -1,10 +1,34 @@
module.exports = (qqntim) => {
qqntim.nt.getFriendsList().then((list) => {
console.log("[Example-AutoReply] 好友列表", list);
list.forEach((friend) =>
qqntim.nt
.getPreviousMessages({ chatType: "friend", uid: friend.uid }, 20)
.then((messages) =>
console.log(
"[Example-AutoReply] 好友 " +
friend.nickName +
" 的最近 20 条消息",
messages
)
)
);
});

qqntim.nt.getGroupsList().then((list) => {
console.log("[Example-AutoReply] 群组列表", list);
list.forEach((group) =>
qqntim.nt
.getPreviousMessages({ chatType: "group", uid: group.uid }, 20)
.then((messages) =>
console.log(
"[Example-AutoReply] 群组 " + group.name + " 的最近 20 条消息",
messages
)
)
);
});

qqntim.nt.on("new-messages", (messages) => {
console.log("[Example-AutoReply] 收到新消息", messages);
messages.forEach((message) => {
Expand Down
25 changes: 25 additions & 0 deletions src/renderer/api/nt/index.ts
Expand Up @@ -239,6 +239,7 @@ class NT extends (EventEmitter as new () => TypedEmitter<NTEvents>) {
this.once("friends-list-updated", (list) => resolve(list));
});
}

async getGroupsList(forced: boolean) {
ntCall("ns-ntApi-2", "nodeIKernelGroupService/getGroupList", [
{ forceFetch: forced },
Expand All @@ -248,6 +249,30 @@ class NT extends (EventEmitter as new () => TypedEmitter<NTEvents>) {
this.once("groups-list-updated", (list) => resolve(list));
});
}

async getPreviousMessages(peer: Peer, count: number = 20, startMsgId = "0") {
try {
const msgs = await ntCall(
"ns-ntApi-2",
"nodeIKernelMsgService/getMsgsIncludeSelf",
[
{
peer: destructPeer(peer),
msgId: startMsgId,
cnt: count,
queryOrder: true,
},
undefined,
]
);
const messages = (msgs.msgList as any[]).map((msg) =>
constructMessage(msg, this.pendingMediaDownloads)
);
return messages;
} catch {
return [];
}
}
}

export const nt = new NT();

0 comments on commit 55f1bc4

Please sign in to comment.