UNPKG

node-groupme

Version:

The only GroupMe API library that isn't a million years old.

72 lines 2.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const __1 = require(".."); class ChatMessageManager extends __1.MessageManager { constructor(client, channel) { super(client, channel, __1.ChatMessage); } async fetch(options) { if (typeof options === 'string') { return await this.fetchId(options); } else if (Array.isArray(options)) { return await this.fetchIds(options); } else if (typeof options === 'object') { return await this.fetchIndex(options); } else { return await this.fetchAll(); } } async fetchId(id) { const res = await this.client.rest.api('GET', `direct_messages/${id}`, { query: { other_user_id: this.channel.recipient.id }, }); const message = this._upsert(new __1.ChatMessage(this.client, this.channel, res.message)); return message; } async fetchIds(ids) { const messages = await Promise.all(ids.map(this.fetchId)); const batch = new __1.Collection(messages.map(m => [m.id, m])); return batch; } async fetchIndex(options) { const apiParams = { other_user_id: this.channel.recipient.id }; if (options.before_id !== undefined) apiParams.before_id = options.before_id; if (options.after_id !== undefined) apiParams.after_id = options.after_id; if (options.since_id !== undefined) apiParams.since_id = options.since_id; if (options.limit !== undefined) apiParams.limit = options.limit; const batch = new __1.Collection(); const res = await this.client.rest.api('GET', `direct_messages`, { query: apiParams }, { allowNull: true }); if (!res) return batch; for (const msgData of res.direct_messages) { const message = this._upsert(new __1.ChatMessage(this.client, this.channel, msgData)); batch.set(message.id, message); } batch.sort(); return batch; } async fetchAll() { let batch = new __1.Collection(); let lastMessageID = (await this.fetchIndex({ limit: 1 })).last()?.id; if (!lastMessageID) return batch; do { batch = await this.fetchIndex({ limit: 100, before_id: lastMessageID, }); lastMessageID = batch.last()?.id; } while (batch.size); this.cache.sort(); return this.cache; } } exports.default = ChatMessageManager; //# sourceMappingURL=ChatMessageManager.js.map