mirror of
https://github.com/xCyanGrizzly/DragonsStash.git
synced 2026-07-22 23:12:02 +00:00
Compare commits
2 Commits
04effed825
...
e8daabd28d
| Author | SHA1 | Date | |
|---|---|---|---|
| e8daabd28d | |||
| 106700b13f |
@@ -308,14 +308,15 @@ async function scanDestinationChannel(
|
||||
}>(client, {
|
||||
_: "searchChatMessages",
|
||||
chat_id: Number(chatId),
|
||||
// No topic context for a flat destination scan. TDLib 1.8.64+ replaced
|
||||
// `message_thread_id` / `saved_messages_topic_id` with a single
|
||||
// optional `topic_id`; for a flat scan we just omit it.
|
||||
query: "",
|
||||
from_message_id: currentFromId,
|
||||
offset: 0,
|
||||
limit: 100,
|
||||
filter: { _: "searchMessagesFilterDocument" },
|
||||
sender_id: null,
|
||||
message_thread_id: 0,
|
||||
saved_messages_topic_id: 0,
|
||||
});
|
||||
|
||||
if (!result.messages || result.messages.length === 0) break;
|
||||
|
||||
@@ -39,7 +39,15 @@ interface TdMessage {
|
||||
id: number;
|
||||
date: number;
|
||||
media_album_id?: string;
|
||||
// TDLib 1.8.50 exposed `reply_to_message_id` directly on the message.
|
||||
// 1.8.64+ replaced it with a tagged-union `reply_to: MessageReplyTo`.
|
||||
// Read both for resilience across versions.
|
||||
reply_to_message_id?: number;
|
||||
reply_to?: {
|
||||
_: string;
|
||||
chat_id?: number;
|
||||
message_id?: number;
|
||||
};
|
||||
content: {
|
||||
_: string;
|
||||
document?: {
|
||||
@@ -66,6 +74,24 @@ interface TdMessage {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick the right "the message I'm replying to" ID across TDLib versions.
|
||||
* - 1.8.50 and earlier expose it directly as `reply_to_message_id`.
|
||||
* - 1.8.64+ expose `reply_to: MessageReplyTo` (tagged union); a reply to
|
||||
* a regular message has `_: "messageReplyToMessage"` with `message_id`.
|
||||
* - Story replies (`_: "messageReplyToStory"`) intentionally return null
|
||||
* here — they aren't useful for our reply-chain grouping.
|
||||
*/
|
||||
function extractReplyToMessageId(msg: TdMessage): bigint | undefined {
|
||||
if (msg.reply_to_message_id) {
|
||||
return BigInt(msg.reply_to_message_id);
|
||||
}
|
||||
if (msg.reply_to && msg.reply_to._ === "messageReplyToMessage" && msg.reply_to.message_id) {
|
||||
return BigInt(msg.reply_to.message_id);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
interface TdFile {
|
||||
id: number;
|
||||
size: number;
|
||||
@@ -202,12 +228,14 @@ export async function getChannelMessages(
|
||||
const result = await invokeWithTimeout<{ messages: TdMessage[]; total_count?: number }>(client, {
|
||||
_: "searchChatMessages",
|
||||
chat_id: Number(chatId),
|
||||
// No topic_id for a flat (non-forum) channel scan. TDLib 1.8.64+
|
||||
// dropped the top-level `message_thread_id: 0` we used to pass; the
|
||||
// type-narrow now is "omit the field entirely if not in a topic".
|
||||
query: "",
|
||||
from_message_id: fromMessageId,
|
||||
offset: 0,
|
||||
limit: Math.min(limit, 100),
|
||||
filter,
|
||||
message_thread_id: 0,
|
||||
});
|
||||
|
||||
if (!result.messages || result.messages.length === 0) break;
|
||||
@@ -233,7 +261,7 @@ export async function getChannelMessages(
|
||||
fileSize: BigInt(doc.document.size),
|
||||
date: new Date(msg.date * 1000),
|
||||
mediaAlbumId: msg.media_album_id && msg.media_album_id !== "0" ? msg.media_album_id : undefined,
|
||||
replyToMessageId: msg.reply_to_message_id ? BigInt(msg.reply_to_message_id) : undefined,
|
||||
replyToMessageId: extractReplyToMessageId(msg),
|
||||
caption: msg.content?.caption?.text || undefined,
|
||||
remoteUniqueId: doc.document.remote?.unique_id || undefined,
|
||||
});
|
||||
|
||||
@@ -64,7 +64,11 @@ export async function getForumTopicList(
|
||||
const topics: ForumTopic[] = [];
|
||||
let offsetDate = 0;
|
||||
let offsetMessageId = 0;
|
||||
let offsetMessageThreadId = 0;
|
||||
// TDLib 1.8.64+ renamed `offset_message_thread_id` → `offset_forum_topic_id`
|
||||
// in the getForumTopics request, and `next_offset_message_thread_id` →
|
||||
// `next_offset_forum_topic_id` in the response. Individual topic infos
|
||||
// also moved from `info.message_thread_id` → `info.forum_topic_id`.
|
||||
let offsetForumTopicId = 0;
|
||||
let pageCount = 0;
|
||||
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
@@ -80,12 +84,16 @@ export async function getForumTopicList(
|
||||
|
||||
const prevOffsetDate = offsetDate;
|
||||
const prevOffsetMessageId = offsetMessageId;
|
||||
const prevOffsetMessageThreadId = offsetMessageThreadId;
|
||||
const prevOffsetForumTopicId = offsetForumTopicId;
|
||||
|
||||
const result = await invokeWithTimeout<{
|
||||
topics?: {
|
||||
info?: {
|
||||
// Both names — 1.8.50 used the first, 1.8.64+ uses the second.
|
||||
// Read both so a future TDLib downgrade or transition build is
|
||||
// still handled.
|
||||
message_thread_id?: number;
|
||||
forum_topic_id?: number;
|
||||
name?: string;
|
||||
is_general?: boolean;
|
||||
};
|
||||
@@ -93,45 +101,49 @@ export async function getForumTopicList(
|
||||
next_offset_date?: number;
|
||||
next_offset_message_id?: number;
|
||||
next_offset_message_thread_id?: number;
|
||||
next_offset_forum_topic_id?: number;
|
||||
}>(client, {
|
||||
_: "getForumTopics",
|
||||
chat_id: Number(chatId),
|
||||
query: "",
|
||||
offset_date: offsetDate,
|
||||
offset_message_id: offsetMessageId,
|
||||
offset_message_thread_id: offsetMessageThreadId,
|
||||
offset_forum_topic_id: offsetForumTopicId,
|
||||
limit: 100,
|
||||
});
|
||||
|
||||
if (!result.topics || result.topics.length === 0) break;
|
||||
|
||||
for (const t of result.topics) {
|
||||
if (!t.info?.message_thread_id) continue;
|
||||
const topicId = t.info?.forum_topic_id ?? t.info?.message_thread_id;
|
||||
if (!topicId) continue;
|
||||
|
||||
topics.push({
|
||||
topicId: BigInt(t.info.message_thread_id),
|
||||
name: t.info.is_general ? "General" : (t.info.name ?? "Unnamed"),
|
||||
topicId: BigInt(topicId),
|
||||
name: t.info?.is_general ? "General" : (t.info?.name ?? "Unnamed"),
|
||||
});
|
||||
}
|
||||
|
||||
// Check if there are more pages
|
||||
const nextForumTopicId =
|
||||
result.next_offset_forum_topic_id ?? result.next_offset_message_thread_id;
|
||||
if (
|
||||
!result.next_offset_date &&
|
||||
!result.next_offset_message_id &&
|
||||
!result.next_offset_message_thread_id
|
||||
!nextForumTopicId
|
||||
) {
|
||||
break;
|
||||
}
|
||||
|
||||
offsetDate = result.next_offset_date ?? 0;
|
||||
offsetMessageId = result.next_offset_message_id ?? 0;
|
||||
offsetMessageThreadId = result.next_offset_message_thread_id ?? 0;
|
||||
offsetForumTopicId = nextForumTopicId ?? 0;
|
||||
|
||||
// Stuck detection: if offsets didn't advance, break
|
||||
if (
|
||||
offsetDate === prevOffsetDate &&
|
||||
offsetMessageId === prevOffsetMessageId &&
|
||||
offsetMessageThreadId === prevOffsetMessageThreadId
|
||||
offsetForumTopicId === prevOffsetForumTopicId
|
||||
) {
|
||||
log.warn(
|
||||
{ chatId: chatId.toString(), topicCount: topics.length },
|
||||
@@ -227,14 +239,20 @@ export async function getTopicMessages(
|
||||
}>(client, {
|
||||
_: "searchChatMessages",
|
||||
chat_id: Number(chatId),
|
||||
// TDLib 1.8.64+ replaced the top-level `message_thread_id` and
|
||||
// `saved_messages_topic_id` parameters with a single tagged-union
|
||||
// `topic_id: MessageTopic$Input`. For a forum topic, use the
|
||||
// messageTopicForum variant carrying the forum_topic_id.
|
||||
topic_id: {
|
||||
_: "messageTopicForum",
|
||||
forum_topic_id: Number(topicId),
|
||||
},
|
||||
query: "",
|
||||
message_thread_id: Number(topicId),
|
||||
from_message_id: currentFromId,
|
||||
offset: 0,
|
||||
limit: Math.min(limit, 100),
|
||||
filter: null,
|
||||
sender_id: null,
|
||||
saved_messages_topic_id: 0,
|
||||
});
|
||||
|
||||
if (!result.messages || result.messages.length === 0) break;
|
||||
|
||||
Reference in New Issue
Block a user