UNPKG

node-groupme

Version:

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

70 lines 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const __1 = require(".."); class GroupMessageManager extends __1.MessageManager { constructor(client, channel) { super(client, channel, __1.GroupMessage); } 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', `groups/${this.channel.id}/messages/${id}`); const message = this._upsert(new __1.GroupMessage(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 = {}; 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', `groups/${this.channel.id}/messages`, { query: apiParams }, { allowNull: true }); if (!res) return batch; for (const msgData of res.messages) { const message = this._upsert(new __1.GroupMessage(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.first()?.id; } while (batch.size); this.cache.sort(); return this.cache; } } exports.default = GroupMessageManager; //# sourceMappingURL=GroupMessageManager.js.map