UNPKG

node-groupme

Version:

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

147 lines 5.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const url_1 = require("url"); const __1 = require(".."); class GroupManager extends __1.BaseManager { constructor(client) { super(client, __1.Group); this.former = new __1.FormerGroupManager(client); } async create(options) { const body = { name: options.name }; if (options.type !== undefined) body.type = options.type; if (options.description !== undefined) body.description = options.description; if (options.image_url !== undefined) body.image_url = options.description; if (options.share !== undefined) body.share = options.share; if (options.join_question !== undefined) { body.show_join_question = true; body.join_question = { text: options.join_question, type: 'join_reason/questions/text' }; } if (options.requires_approval !== undefined) body.requires_approval = options.requires_approval; if (options.office_mode !== undefined) body.office_mode = options.office_mode; const res = await this.client.rest.api('POST', 'groups', { body }); const group = this._upsert(new __1.Group(this.client, res)); if (res.members) { res.members.forEach(data => { const user = this.client.users._add({ id: data.user_id, avatar_url: data.image_url, name: data.name, }); group.members._upsert(new __1.Member(this.client, group, user, data)); }); } return group; } async join(inviteLinkOrGroupID, shareToken) { if (shareToken !== undefined) { return await this.joinWithToken(inviteLinkOrGroupID, shareToken); } else { const inviteURL = new url_1.URL(inviteLinkOrGroupID); if (!inviteURL.hostname.endsWith('groupme.com')) throw new Error(`Invalid invite link\n-- URL: ${inviteLinkOrGroupID}`); const matches = inviteURL.pathname.match(/.+\/(\d+)\/([A-Za-z0-9]+)$/); if (matches === null) throw new Error(`Invalid invite link\n-- URL: ${inviteLinkOrGroupID}`); return await this.joinWithToken(matches[1], matches[2]); } } async joinWithToken(groupID, shareToken) { const res = await this.client.rest.api('POST', `groups/${groupID}/join/${shareToken}`); const group = this._upsert(new __1.Group(this.client, res.group)); if (res.group.members) { res.group.members.forEach(data => { const user = this.client.users._add({ id: data.user_id, avatar_url: data.image_url, name: data.name, }); group.members._upsert(new __1.Member(this.client, group, user, data)); }); } return group; } async fetch(options) { if (typeof options === 'string') { return await this.fetchId(options); } else if (options instanceof Array) { 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/${id}`); const group = this._upsert(new __1.Group(this.client, res)); if (res.members) { res.members.forEach(data => { const user = this.client.users._add({ id: data.user_id, avatar_url: data.image_url, name: data.name, }); group.members._upsert(new __1.Member(this.client, group, user, data)); }); } return group; } async fetchIds(ids) { const batch = new __1.Collection(); await Promise.all(ids.map(async (id) => { const group = await this.fetchId(id); batch.set(group.id, group); })); return batch; } async fetchIndex(options) { const apiParams = {}; if (options.page !== undefined) apiParams.page = options.page; if (options.per_page !== undefined) apiParams.per_page = options.per_page; if (options.omit_members === true) apiParams.omit = 'memberships'; const batch = new __1.Collection(); const groupsIndexResponse = await this.client.rest.api('GET', 'groups', { query: apiParams }); groupsIndexResponse.forEach(g => { /** The Group object to store data in. */ const group = this._upsert(new __1.Group(this.client, g)); if (g.members) { g.members.forEach(data => { const user = this.client.users._add({ id: data.user_id, avatar_url: data.image_url, name: data.name, }); group.members._upsert(new __1.Member(this.client, group, user, data)); }); } batch.set(group.id, group); }); return batch; } async fetchAll() { let batch, i = 1; do { batch = await this.fetchIndex({ page: i++, omit_members: false, }); } while (batch.size); return this.client.groups.cache; } } exports.default = GroupManager; //# sourceMappingURL=GroupManager.js.map