From 88b62210e77c24350ca6bb67fe74425c1257636b Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Tue, 10 Dec 2019 00:19:20 -0800 Subject: [PATCH] array/meta guard, getModerators(), addModerators()/removeModerators() --- js/modules/loki_app_dot_net_api.js | 68 +++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 2328c7cb9..747986c82 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -329,7 +329,7 @@ class LokiAppDotNetServerAPI { // if it's a response style with a meta if (result.status !== 200) { - if (!forceFreshToken && response.meta.code === 401) { + if (!forceFreshToken && (!response.meta || response.meta.code === 401)) { // copy options because lint complains if we modify this directly const updatedOptions = options; // force it this time @@ -371,6 +371,60 @@ class LokiAppDotNetServerAPI { return res.response.data.annotations || []; } + async getModerators(channelId) { + if (!channelId) { + log.warn('No channelId provided to getModerators!'); + return []; + } + const res = await this.serverRequest( + `loki/v1/channels/${channelId}/moderators` + ); + + return (!res.err && res.response && res.response.moderators) || []; + } + + async addModerators(pubKeysParam) { + let pubKeys = pubKeysParam; + if (!Array.isArray(pubKeys)) { + pubKeys = [pubKeys]; + } + pubKeys = pubKeys.map(key => `@${key}`); + const users = await this.getUsers(pubKeys); + const validUsers = users.filter(user => !!user.id); + const results = await Promise.all( + validUsers.map(async user => { + log.info(`POSTing loki/v1/moderators/${user.id}`); + const res = await this.serverRequest(`loki/v1/moderators/${user.id}`, { + method: 'POST', + }); + return !!(!res.err && res.response && res.response.data); + }) + ); + const anyFailures = results.some(test => !test); + return anyFailures ? results : true; // return failures or total success + } + + async removeModerators(pubKeysParam) { + let pubKeys = pubKeysParam; + if (!Array.isArray(pubKeys)) { + pubKeys = [pubKeys]; + } + pubKeys = pubKeys.map(key => `@${key}`); + const users = await this.getUsers(pubKeys); + const validUsers = users.filter(user => !!user.id); + + const results = await Promise.all( + validUsers.map(async user => { + const res = await this.serverRequest(`loki/v1/moderators/${user.id}`, { + method: 'DELETE', + }); + return !!(!res.err && res.response && res.response.data); + }) + ); + const anyFailures = results.some(test => !test); + return anyFailures ? results : true; // return failures or total success + } + async getSubscribers(channelId, wantObjects) { if (!channelId) { log.warn('No channelId provided to getSubscribers!'); @@ -645,6 +699,10 @@ class LokiPublicChannelAPI { return this.serverAPI.getSubscribers(this.channelId, true); } + getModerators() { + return this.serverAPI.getModerators(this.channelId); + } + // get moderation actions async pollForModerators() { try { @@ -1287,6 +1345,14 @@ class LokiPublicChannelAPI { // look up primary device once const primaryPubKey = slavePrimaryMap[slaveKey]; + if (!Array.isArray(slaveMessages[slaveKey])) { + log.warn( + `messages for ${slaveKey} is not an array`, + slaveMessages[slaveKey] + ); + return; + } + // send out remaining messages for this merged identity slaveMessages[slaveKey].forEach(messageDataP => { const messageData = messageDataP; // for linter