From 60259ab0bb8b1382b5f9ab3518c8027e60657fd9 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Sun, 9 Feb 2020 23:22:09 -0800 Subject: [PATCH 1/5] handle clearing profileName correctly, support multidevice profiles better, use getLokiProfile() --- js/modules/loki_app_dot_net_api.js | 53 +++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index fab433da6..c8c712eb1 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -109,17 +109,27 @@ class LokiAppDotNetServerAPI { JSON.stringify(objToSign) ); */ + let pName = profileName; + + // You cannot use null to clear the profile name + // the name key has to be set to know what value we want changed + if (pName === undefined || pName === null) { + pName = ''; + } const res = await this.serverRequest('users/me', { method: 'PATCH', objBody: { - name: profileName, + name: pName, }, }); // no big deal if it fails... if (res.err || !res.response || !res.response.data) { if (res.err) { - log.error(`setProfileName Error ${res.err}`); + log.error( + `setProfileName Error ${res.err} ${res.statusCode}`, + this.baseServerUrl + ); } return []; } @@ -204,10 +214,12 @@ class LokiAppDotNetServerAPI { tokenRes.response.data.user ) { // get our profile name - // FIXME: should this be window.storage.get('primaryDevicePubKey')? - const ourNumber = textsecure.storage.user.getNumber(); + // this should be primaryDevicePubKey + // because the rest of the profile system uses that... + const ourNumber = window.storage.get('primaryDevicePubKey'); const profileConvo = ConversationController.get(ourNumber); - const profileName = profileConvo.getProfileName(); + const profile = profileConvo.getLokiProfile(); + const profileName = profile && profile.displayName; // if doesn't match, write it to the network if (tokenRes.response.data.user.name !== profileName) { // update our profile name if it got out of sync @@ -468,7 +480,10 @@ class LokiAppDotNetServerAPI { try { response = JSON.parse(txtResponse); } catch (e) { - log.warn(`_sendToProxy Could not parse outer JSON [${txtResponse}]`); + log.warn( + `_sendToProxy Could not parse outer JSON [${txtResponse}]`, + endpoint + ); } if (response.meta && response.meta.code === 200) { @@ -487,13 +502,17 @@ class LokiAppDotNetServerAPI { try { response = options.textResponse ? respStr : JSON.parse(respStr); } catch (e) { - log.warn(`_sendToProxy Could not parse inner JSON [${respStr}]`, endpoint); + log.warn( + `_sendToProxy Could not parse inner JSON [${respStr}]`, + endpoint + ); } } else { log.warn( 'file server secure_rpc gave an non-200 response: ', response, - ` txtResponse[${txtResponse}]` + ` txtResponse[${txtResponse}]`, + endpoint ); } return { result, txtResponse, response }; @@ -1000,15 +1019,17 @@ class LokiPublicChannelAPI { const res = await this.serverRequest( `loki/v1/channels/${this.channelId}/moderators` ); - // FIXME: should this be window.storage.get('primaryDevicePubKey')? - const ourNumber = textsecure.storage.user.getNumber(); + const ourNumberDevice = textsecure.storage.user.getNumber(); + const ourNumberProfile = window.storage.get('primaryDevicePubKey'); // Get the list of moderators if no errors occurred const moderators = !res.err && res.response && res.response.moderators; // if we encountered problems then we'll keep the old mod status if (moderators) { - this.modStatus = moderators.includes(ourNumber); + this.modStatus = + moderators.includes(ourNumberProfile) || + moderators.includes(ourNumberDevice); } await this.conversation.setModerators(moderators || []); @@ -1411,8 +1432,8 @@ class LokiPublicChannelAPI { let pendingMessages = []; // get our profile name - // FIXME: should this be window.storage.get('primaryDevicePubKey')? - const ourNumber = textsecure.storage.user.getNumber(); + const ourNumberProfile = window.storage.get('primaryDevicePubKey'); + const ourNumberDevice = textsecure.storage.user.getNumber(); let lastProfileName = false; // the signature forces this to be async @@ -1485,7 +1506,7 @@ class LokiPublicChannelAPI { const from = adnMessage.user.name || 'Anonymous'; // profileName // if us - if (pubKey === ourNumber) { + if (pubKey === ourNumberProfile || pubKey === ourNumberDevice) { // update the last name we saw from ourself lastProfileName = from; } @@ -1635,7 +1656,7 @@ class LokiPublicChannelAPI { const slaveKey = messageData.source; // prevent our own device sent messages from coming back in - if (slaveKey === ourNumber) { + if (slaveKey === ourNumberDevice) { // we originally sent these return; } @@ -1666,7 +1687,7 @@ class LokiPublicChannelAPI { // if we received one of our own messages if (lastProfileName !== false) { // get current profileName - const profileConvo = ConversationController.get(ourNumber); + const profileConvo = ConversationController.get(ourNumberProfile); const profileName = profileConvo.getProfileName(); // check to see if it out of sync if (profileName !== lastProfileName) { From 6faa15d5b45521cb55f69f6b74b520e9932a45fe Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Mon, 10 Feb 2020 14:23:21 -0800 Subject: [PATCH 2/5] Update js/modules/loki_app_dot_net_api.js Co-Authored-By: Mikunj Varsani --- js/modules/loki_app_dot_net_api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index c8c712eb1..5fc778512 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -109,7 +109,7 @@ class LokiAppDotNetServerAPI { JSON.stringify(objToSign) ); */ - let pName = profileName; + let pName = profileName || ''; // You cannot use null to clear the profile name // the name key has to be set to know what value we want changed From 14f64e3769365b991fc689b2ce5a219577ae1c2d Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Mon, 10 Feb 2020 14:24:15 -0800 Subject: [PATCH 3/5] Remove unneeded branch --- js/modules/loki_app_dot_net_api.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 5fc778512..4e40b068a 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -109,13 +109,10 @@ class LokiAppDotNetServerAPI { JSON.stringify(objToSign) ); */ - let pName = profileName || ''; // You cannot use null to clear the profile name // the name key has to be set to know what value we want changed - if (pName === undefined || pName === null) { - pName = ''; - } + let pName = profileName || ''; const res = await this.serverRequest('users/me', { method: 'PATCH', From c521497f90d116ea50da2a4efa6641efa14ddff0 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Mon, 10 Feb 2020 15:10:58 -0800 Subject: [PATCH 4/5] lint --- js/modules/loki_app_dot_net_api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 4e40b068a..f73b74668 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -112,7 +112,7 @@ class LokiAppDotNetServerAPI { // You cannot use null to clear the profile name // the name key has to be set to know what value we want changed - let pName = profileName || ''; + const pName = profileName || ''; const res = await this.serverRequest('users/me', { method: 'PATCH', From 5feee5dd5f48f1954b8eba51577b960ffd971bdf Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Mon, 10 Feb 2020 17:08:54 -0800 Subject: [PATCH 5/5] handle profile properly if primaryDevicePubKey is not set --- js/modules/loki_app_dot_net_api.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index c8c712eb1..e00958e22 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -216,7 +216,9 @@ class LokiAppDotNetServerAPI { // get our profile name // this should be primaryDevicePubKey // because the rest of the profile system uses that... - const ourNumber = window.storage.get('primaryDevicePubKey'); + const ourNumber = + window.storage.get('primaryDevicePubKey') || + textsecure.storage.user.getNumber(); const profileConvo = ConversationController.get(ourNumber); const profile = profileConvo.getLokiProfile(); const profileName = profile && profile.displayName; @@ -1028,7 +1030,7 @@ class LokiPublicChannelAPI { // if we encountered problems then we'll keep the old mod status if (moderators) { this.modStatus = - moderators.includes(ourNumberProfile) || + (ourNumberProfile && moderators.includes(ourNumberProfile)) || moderators.includes(ourNumberDevice); } @@ -1432,8 +1434,10 @@ class LokiPublicChannelAPI { let pendingMessages = []; // get our profile name - const ourNumberProfile = window.storage.get('primaryDevicePubKey'); const ourNumberDevice = textsecure.storage.user.getNumber(); + // if no primaryDevicePubKey fall back to ourNumberDevice + const ourNumberProfile = + window.storage.get('primaryDevicePubKey') || ourNumberDevice; let lastProfileName = false; // the signature forces this to be async