Skip to content

Commit

Permalink
invite: fallback mode for failing connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Sep 17, 2019
1 parent 8e515bd commit 963f7fa
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 8 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"homepage": "./",
"dependencies": {
"@peerlinks/protocol": "^6.0.2",
"@peerlinks/protocol": "^6.0.3",
"@peerlinks/sqlite-storage": "^3.0.0",
"@peerlinks/swarm": "^3.0.0",
"binary-search": "^1.3.6",
Expand All @@ -44,7 +44,8 @@
"react:build": "react-scripts build",
"react:test": "react-scripts test",
"react:eject": "react-scripts eject",
"electron:dev": "concurrently \"BROWSER=none npm run react:start\" \"wait-on http://localhost:3000/ && electron .\"",
"electron:dev": "concurrently \"BROWSER=none npm run react:start\" \"wait-on http://localhost:3000/ && npm run electron:dev-standalone\"",
"electron:dev-standalone": "electron .",
"electron:build": "npm run react:build && electron-builder -mlw",
"electron:publish": "npm run electron:build -- -p always"
},
Expand Down
4 changes: 3 additions & 1 deletion src/electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const UPDATE_FREQUENCY = 4 * 3600 * 1000;

const USER_DATA_DIR = app.getPath('userData');
const DB_FILE = path.join(
USER_DATA_DIR, isDev ? 'db-dev.sqlite' : 'db-v2.sqlite');
USER_DATA_DIR,
isDev ? (process.env.PEERLINKS_DB || 'db-dev.sqlite') :
'db-v2.sqlite');

// Create `userData` folder if it doesn't exist
if (!fs.existsSync(USER_DATA_DIR)) {
Expand Down
28 changes: 28 additions & 0 deletions src/electron/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,40 @@ export default class Network {
const { encryptedInvite, peerId } =
identity.issueInvite(channel, request, inviteeName);

return {
encryptedInvite: {
box: bs58.encode(encryptedInvite.box),
requestId: bs58.encode(encryptedInvite.requestId),
},
peerId: peerId.toString('hex'),
};
});

handle('sendInvite', async ({ peerId, encryptedInvite }) => {
peerId = Buffer.from(peerId, 'hex');
encryptedInvite = {
box: bs58.decode(encryptedInvite.box),
requestId: bs58.decode(encryptedInvite.requestId),
};

return await this.swarm.sendInvite({
peerId,
encryptedInvite,
}, INVITE_TIMEOUT);
});

handle('acceptInvite', async ({ requestId, box }) => {
const encryptedInvite = {
requestId: bs58.decode(requestId),
box: bs58.decode(box),
};

return await this.swarm.sendInvite({
peerId: this.peerLinks.id,
encryptedInvite,
}, INVITE_TIMEOUT);
});

handle('renameIdentityPair', async (params) => {
const { channelId, identityKey, newName } = params;

Expand Down
45 changes: 44 additions & 1 deletion src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,29 @@ export function loadMessages(options) {
// NOTE: Command
export function invite(params) {
const run = async (dispatch) => {
return await network.invite(params);
const { encryptedInvite, peerId } = await network.invite(params);

const post = (text) => {
dispatch(appendInternalMessage({
channelId: params.channelId,
text,
}));
};

post('(Sending invite...)');

const delay = setTimeout(() => {
post('It took unusually long to send an invite...');
post(
'As a fallback - consider asking your peer to run following command ' +
'in any channel:');
post(`\`/accept-invite ${encryptedInvite.requestId} ` +
`${encryptedInvite.box}\``);
}, 5000);

const isSuccess = await network.sendInvite({ encryptedInvite, peerId });
clearTimeout(delay);
return isSuccess;
};

return (dispatch) => {
Expand All @@ -627,6 +649,27 @@ export function invite(params) {
};
}


// NOTE: Command
export function acceptInvite(params) {
const run = async (dispatch) => {
return await network.acceptInvite(params);
};

return (dispatch) => {
run(dispatch).then((success) => {
if (!success) {
throw new Error('Was not waiting for an invite...');
}
}).catch((e) => {
dispatch(addNotification({
kind: 'error',
content: `Failed to accept invite: ` + e.message,
}));
});
};
}

// NOTE: Command
export function displayHelp({ channelId }) {
return appendInternalMessage({
Expand Down
6 changes: 5 additions & 1 deletion src/redux/commands.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
invite, displayHelp, getFeedURL, renameIdentityPair,
invite, acceptInvite, displayHelp, getFeedURL, renameIdentityPair,
} from './actions';

export default new Map([
Expand All @@ -11,6 +11,10 @@ export default new Map([
'invite',
{ args: [ 'inviteeName', 'request' ], action: invite },
],
[
'accept-invite',
{ args: [ 'requestId', 'box' ], action: acceptInvite },
],
[
'get-feed-url',
{ args: [ ], action: getFeedURL },
Expand Down
9 changes: 9 additions & 0 deletions src/redux/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ export default class Network {
});
}

async acceptInvite({ requestId, box }) {
return await this.request('network:acceptInvite', { requestId, box });
}

async sendInvite({ encryptedInvite, peerId }) {
return await this.request('network:sendInvite',
{ encryptedInvite, peerId });
}

async renameIdentityPair({ channelId, identityKey, newName }) {
return await this.request('network:renameIdentityPair',
{ channelId, identityKey, newName });
Expand Down

0 comments on commit 963f7fa

Please sign in to comment.