| 238 | const last = page[page.length - 1]; |
| 239 | offsetId = last.dialog?.topMessage?.id ?? 0; |
| 240 | offsetDate = last.dialog?.topMessage?.date ?? 0; |
| 241 | offsetPeer = last.dialog?.peer ?? new Api.InputPeerEmpty(); |
| 242 | } |
| 243 | const dialogs = all.slice(0, limit); |
| 244 | |
| 245 | let count = 0; |
| 246 | for (const d of dialogs) { |
| 247 | const msgs: CachedMsg[] = []; |
| 248 | for await (const m of cl.iterMessages(d.id, { limit: CACHE_MSG_LIMIT })) { |
| 249 | msgs.push(stripMsg(m)); |
| 250 | } |
| 251 | // get entity for username/title |
| 252 | let username: string | undefined; |
| 253 | let title: string | undefined; |
| 254 | try { |
| 255 | const entity = entityFields(await cl.getEntity(d.id)); |
| 256 | username = entity.username; |
| 257 | title = entity.title; |
| 258 | } catch { /* best-effort */ } |
| 259 | // ponytail: skip private groups (no username) |
| 260 | if (username) { |
| 261 | this.chatCache.set(String(d.id), { username, title, msgs }); |
| 262 | count++; |
| 263 | } |
| 264 | await rndDelay(1000, 10000); |
| 265 | } |
| 266 | |
| 267 | // persist to disk |
| 268 | this.cacheDb.data.cache = Object.fromEntries(this.chatCache); |
| 269 | await this.cacheDb.write(); |
| 270 | console.log(`[fbi] cache ready: ${count} groups (limit ${limit})`); |
| 271 | } finally { |
| 272 | this.cacheReady = true; |