Fix bug with replayable errors, fix bug with set representation of swarmNodes not being handled properly

pull/147/head
Beaudan 7 years ago
parent 8b5106433f
commit dde63a552f

@ -847,6 +847,8 @@
e.name === 'SendMessageNetworkError' || e.name === 'SendMessageNetworkError' ||
e.name === 'SignedPreKeyRotationError' || e.name === 'SignedPreKeyRotationError' ||
e.name === 'OutgoingIdentityKeyError' || e.name === 'OutgoingIdentityKeyError' ||
e.name === 'DNSResolutionError' ||
e.name === 'EmptySwarmError' ||
e.name === 'PoWError' e.name === 'PoWError'
); );
}, },

@ -663,18 +663,14 @@ async function removeAllSessions(id) {
function setifyProperty(data, propertyName) { function setifyProperty(data, propertyName) {
if (!data) return data; if (!data) return data;
const returnData = data; const returnData = data;
if (returnData[propertyName]) { if (returnData[propertyName] && Array.isArray(returnData[propertyName])) {
returnData[propertyName] = new Set(returnData[propertyName]); returnData[propertyName] = new Set(returnData[propertyName]);
} }
return returnData; return returnData;
} }
async function getSwarmNodesByPubkey(pubkey) { async function getSwarmNodesByPubkey(pubkey) {
let swarmNodes = await channels.getSwarmNodesByPubkey(pubkey); return channels.getSwarmNodesByPubkey(pubkey);
if (Array.isArray(swarmNodes)) {
swarmNodes = new Set(swarmNodes);
}
return swarmNodes;
} }
async function getConversationCount() { async function getConversationCount() {
@ -682,11 +678,7 @@ async function getConversationCount() {
} }
async function saveConversation(data) { async function saveConversation(data) {
const storeData = data; await channels.saveConversation(data);
if (storeData.swarmNodes) {
storeData.swarmNodes = Array.from(storeData.swarmNodes);
}
await channels.saveConversation(storeData);
} }
async function saveConversations(data) { async function saveConversations(data) {
@ -694,8 +686,7 @@ async function saveConversations(data) {
} }
async function getConversationById(id, { Conversation }) { async function getConversationById(id, { Conversation }) {
const rawData = await channels.getConversationById(id); const data = await channels.getConversationById(id);
const data = setifyProperty(rawData, 'swarmNodes');
return new Conversation(data); return new Conversation(data);
} }
@ -704,8 +695,10 @@ async function updateConversation(id, data, { Conversation }) {
if (!existing) { if (!existing) {
throw new Error(`Conversation ${id} does not exist!`); throw new Error(`Conversation ${id} does not exist!`);
} }
const setData = setifyProperty(data, 'swarmNodes');
const setExisting = setifyProperty(existing.attributes, 'swarmNodes');
const merged = merge({}, existing.attributes, data); const merged = merge({}, setExisting, setData);
if (merged.swarmNodes instanceof Set) { if (merged.swarmNodes instanceof Set) {
merged.swarmNodes = Array.from(merged.swarmNodes); merged.swarmNodes = Array.from(merged.swarmNodes);
} }
@ -729,9 +722,7 @@ async function _removeConversations(ids) {
} }
async function getAllConversations({ ConversationCollection }) { async function getAllConversations({ ConversationCollection }) {
const conversations = (await channels.getAllConversations()).map(c => const conversations = await channels.getAllConversations();
setifyProperty(c, 'swarmNodes')
);
const collection = new ConversationCollection(); const collection = new ConversationCollection();
collection.add(conversations); collection.add(conversations);
@ -744,9 +735,7 @@ async function getAllConversationIds() {
} }
async function getAllPrivateConversations({ ConversationCollection }) { async function getAllPrivateConversations({ ConversationCollection }) {
const conversations = (await channels.getAllPrivateConversations()).map(c => const conversations = await channels.getAllPrivateConversations();
setifyProperty(c, 'swarmNodes')
);
const collection = new ConversationCollection(); const collection = new ConversationCollection();
collection.add(conversations); collection.add(conversations);
@ -754,9 +743,7 @@ async function getAllPrivateConversations({ ConversationCollection }) {
} }
async function getAllGroupsInvolvingId(id, { ConversationCollection }) { async function getAllGroupsInvolvingId(id, { ConversationCollection }) {
const conversations = (await channels.getAllGroupsInvolvingId(id)).map(c => const conversations = await channels.getAllGroupsInvolvingId(id);
setifyProperty(c, 'swarmNodes')
);
const collection = new ConversationCollection(); const collection = new ConversationCollection();
collection.add(conversations); collection.add(conversations);
@ -764,9 +751,7 @@ async function getAllGroupsInvolvingId(id, { ConversationCollection }) {
} }
async function searchConversations(query, { ConversationCollection }) { async function searchConversations(query, { ConversationCollection }) {
const conversations = (await channels.searchConversations(query)).map(c => const conversations = await channels.searchConversations(query);
setifyProperty(c, 'swarmNodes')
);
const collection = new ConversationCollection(); const collection = new ConversationCollection();
collection.add(conversations); collection.add(conversations);

@ -106,21 +106,11 @@ class LokiMessageAPI {
} }
try { try {
swarmNodes = await window.LokiSnodeAPI.getSwarmNodesByPubkey(pubKey); swarmNodes = await window.LokiSnodeAPI.getSwarmNodesByPubkey(pubKey);
// Filter out the nodes we have already got responses from swarmNodes = swarmNodes.filter(node => !(node in completedNodes));
swarmNodes = Object.keys(swarmNodes)
.filter(node => !(node in completedNodes))
.reduce(
// eslint-disable-next-line no-loop-func
(obj, node) => ({
...obj,
[node]: swarmNodes[node],
}),
{}
);
} catch (e) { } catch (e) {
throw new window.textsecure.EmptySwarmError(pubKey, e); throw new window.textsecure.EmptySwarmError(pubKey, e);
} }
if (!swarmNodes || swarmNodes.size === 0) { if (!swarmNodes || swarmNodes.length === 0) {
if (completedNodes.length !== 0) { if (completedNodes.length !== 0) {
// TODO: Decide how to handle some completed requests but not enough // TODO: Decide how to handle some completed requests but not enough
return; return;
@ -134,7 +124,7 @@ class LokiMessageAPI {
const remainingRequests = const remainingRequests =
MINIMUM_SUCCESSFUL_REQUESTS - completedNodes.length; MINIMUM_SUCCESSFUL_REQUESTS - completedNodes.length;
await Promise.all( await Promise.all(
Array.from(swarmNodes) swarmNodes
.splice(0, remainingRequests) .splice(0, remainingRequests)
.map(nodeUrl => doRequest(nodeUrl)) .map(nodeUrl => doRequest(nodeUrl))
); );

@ -108,7 +108,7 @@ class LokiSnodeAPI {
async getSwarmNodesByPubkey(pubKey) { async getSwarmNodesByPubkey(pubKey) {
const swarmNodes = await window.Signal.Data.getSwarmNodesByPubkey(pubKey); const swarmNodes = await window.Signal.Data.getSwarmNodesByPubkey(pubKey);
// TODO: Check if swarm list is below a threshold rather than empty // TODO: Check if swarm list is below a threshold rather than empty
if (swarmNodes && swarmNodes.size !== 0) { if (swarmNodes && swarmNodes.length !== 0) {
return swarmNodes; return swarmNodes;
} }
return this.replenishSwarm(pubKey); return this.replenishSwarm(pubKey);
@ -120,10 +120,10 @@ class LokiSnodeAPI {
this.swarmsPendingReplenish[pubKey] = new Promise(async resolve => { this.swarmsPendingReplenish[pubKey] = new Promise(async resolve => {
let newSwarmNodes; let newSwarmNodes;
try { try {
newSwarmNodes = new Set(await this.getSwarmNodes(pubKey)); newSwarmNodes = await this.getSwarmNodes(pubKey);
} catch (e) { } catch (e) {
// TODO: Handle these errors sensibly // TODO: Handle these errors sensibly
newSwarmNodes = new Set([]); newSwarmNodes = [];
} }
conversation.set({ swarmNodes: newSwarmNodes }); conversation.set({ swarmNodes: newSwarmNodes });
await window.Signal.Data.updateConversation( await window.Signal.Data.updateConversation(

Loading…
Cancel
Save