Skip to content

Commit

Permalink
Merge branch 'feature/offline-sync'
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarbugli committed Jun 17, 2020
2 parents bb20f8e + f518475 commit eb4793f
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 39 deletions.
12 changes: 10 additions & 2 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export class StreamChat {
/**
* disconnect - closes the WS connection
*/
disconnect() {
disconnect(timeout) {
this.logger('info', 'client:disconnect() - Disconnecting the client', {
tags: ['connection', 'client'],
});
Expand Down Expand Up @@ -315,7 +315,7 @@ export class StreamChat {

// close the WS connection
if (this.wsConnection) {
return this.wsConnection.disconnect();
return this.wsConnection.disconnect(timeout);
}

return Promise.resolve();
Expand Down Expand Up @@ -1443,4 +1443,12 @@ export class StreamChat {
deleteRole(name) {
return this.delete(`${this.baseURL}/custom_role/${name}`);
}

/** sync - returns all events that happened for a list of channels since last sync
* @param {array} channel_cids list of channel CIDs
* @param {string} last_sync_at last time the user was online and in sync. RFC3339 ie. "2020-05-06T15:05:01.207Z"
*/
sync(channel_cids, last_sync_at) {
return this.post(`${this.baseURL}/sync`, { channel_cids, last_sync_at });
}
}
4 changes: 2 additions & 2 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class StableWSConnection {
* disconnect - Disconnect the connection and doesn't recover...
*
*/
disconnect() {
disconnect(timeout) {
this.logger(
'info',
`connection:disconnect() - Closing the websocket connection for wsID ${this.wsID}`,
Expand Down Expand Up @@ -196,7 +196,7 @@ export class StableWSConnection {
ws.onclose = onclose;
// In case we don't receive close frame websocket server in time,
// lets not wait for more than 1 seconds.
setTimeout(onclose, 1000);
setTimeout(onclose, timeout != null ? timeout : 1000);
});

this.logger(
Expand Down
2 changes: 1 addition & 1 deletion test/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ describe('Connection and reconnect behaviour', function() {
await conn.connect();
expect(conn.isConnecting).to.equal(false);
const wsID = conn.wsID;
conn.disconnect();
conn.disconnect(5000);
expect(conn.isHealthy).to.equal(false);
expect(conn.wsID).to.equal(wsID + 1);
expect(conn.ws).to.equal(undefined);
Expand Down
2 changes: 1 addition & 1 deletion test/moderation.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ describe('channel muteStatus', function() {
const c = await getTestClientForUser(userID);
expect(c.health.me.channel_mutes.length).to.be.equal(1);
expect(c.health.me.channel_mutes[0].channel.cid).to.be.equal(channel.cid);
await c.disconnect();
await c.disconnect(5000);
});

it('remove mute update internal mute state', async function() {
Expand Down
2 changes: 1 addition & 1 deletion test/multitenant.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ describe('User teams field', function() {
before(async function() {
const client = getTestClient(false);
await client.setUser({ id: userId }, token);
await client.disconnect();
await client.disconnect(5000);
});

after(async () => {
Expand Down
10 changes: 5 additions & 5 deletions test/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe('Mark all read server-side', function() {
);
expect(response.me.total_unread_count).to.eq(5);
expect(response.me.unread_count).to.eq(5);
await thierryClient.disconnect();
await thierryClient.disconnect(5000);
});

it('thierry checks unread counts via query channel', async function() {
Expand Down Expand Up @@ -265,7 +265,7 @@ describe('Mark all read server-side', function() {
expect(response.me.total_unread_count).to.eq(0);
expect(response.me.unread_count).to.eq(0);
expect(response.me.unread_channels).to.eq(0);
await thierryClient.disconnect();
await thierryClient.disconnect(5000);
});

it('thierry checks unread counts via query channel', async function() {
Expand Down Expand Up @@ -345,7 +345,7 @@ describe('Mark all read', function() {
expect(response.me.total_unread_count).to.eq(5);
expect(response.me.unread_count).to.eq(5);
expect(response.me.unread_channels).to.eq(5);
await thierryClient.disconnect();
await thierryClient.disconnect(5000);
});

it('thierry checks unread counts via query channel', async function() {
Expand All @@ -368,7 +368,7 @@ describe('Mark all read', function() {
const thierryClient = getTestClient(false);
await thierryClient.setUser({ id: thierryID }, createUserToken(thierryID));
await thierryClient.markAllRead();
await thierryClient.disconnect();
await thierryClient.disconnect(5000);
});

it('thierry connects and receives unread_count=0', async function() {
Expand All @@ -379,7 +379,7 @@ describe('Mark all read', function() {
);
expect(response.me.total_unread_count).to.eq(0);
expect(response.me.unread_count).to.eq(0);
await thierryClient.disconnect();
await thierryClient.disconnect(5000);
});

it('thierry checks unread counts via query channel', async function() {
Expand Down
16 changes: 8 additions & 8 deletions test/presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Presence', function() {
it('login as a different user', async function() {
const testClientP = await getTestClientForUser('jones');

testClientP.disconnect();
testClientP.disconnect(5000);
testClientP.setUser(
{
id: 'jimmy',
Expand All @@ -93,6 +93,7 @@ describe('Presence', function() {
const b = user1Client.channel('messaging', id, {
members: ['doug', 'claire', 'user1', 'james'],
});
await b.create();
const results = [];
const eventPromise = new Promise(resolve => {
b.on('all', e => {
Expand Down Expand Up @@ -127,6 +128,7 @@ describe('Presence', function() {
const b = user1Client.channel('messaging', id, {
members: ['doug', 'claire', 'user1', 'james'],
});
await b.create();
const results = [];
const eventPromise = new Promise(resolve => {
b.on('all', e => {
Expand Down Expand Up @@ -154,7 +156,7 @@ describe('Presence', function() {
// james start watching it
await channel.watch();
// Watching client goes offline
await james.disconnect();
await james.disconnect(5000);
await eventPromise;
});
});
Expand Down Expand Up @@ -194,7 +196,7 @@ describe('Presence', function() {
it('should be offline after disconnect', async function() {
const userID = `timmy-${uuidv4()}`;
const testClientP = await getTestClientForUser(userID, 'mystatus');
await testClientP.disconnect();
await testClientP.disconnect(5000);
const response = await user1Client.queryUsers({ id: { $in: [userID] } });
const timmy = response.users[0];
expect(timmy.id).to.equal(userID);
Expand Down Expand Up @@ -278,7 +280,6 @@ describe('Presence', function() {
});
// same as above, but with the query channels endpoint
user1Client.on('user.presence.changed', event => {
console.log(event.type);
if (event.user.id === paulID) {
expect(event.user.status).to.equal('rallying fremen');
expect(event.user.online).to.equal(true);
Expand All @@ -298,7 +299,6 @@ describe('Presence', function() {
// start out as offline
expect(r[0].state.members[paulID].user.online).to.equal(false);
expect(user1Client.state.users[paulID].online).to.equal(false);
console.log('waiting for connect..... event');
await getTestClientForUser(paulID, 'rallying fremen');
}
runAndLogPromise(runTest);
Expand Down Expand Up @@ -442,7 +442,7 @@ describe('Watchers count', function() {
const resp = await newClientChannel.watch();
expect(resp.watcher_count).to.eq(2);

await newClient.disconnect();
await newClient.disconnect(5000);
});

it('decrease watcher count', async function() {
Expand Down Expand Up @@ -500,7 +500,7 @@ describe('Count Anonymous users', function() {
if (i % 2 === 0) {
await clients[i].channel.stopWatching();
} else {
await clients[i].client.disconnect();
await clients[i].client.disconnect(5000);
}
const resp = await channel.query({ state: true });
if (i !== nClients - 1) {
Expand Down Expand Up @@ -548,7 +548,7 @@ describe('Count Guest users using state', function() {
if (i % 2 === 0) {
await clients[i].channel.stopWatching();
} else {
clients[i].client.disconnect();
clients[i].client.disconnect(5000);
}
const resp = await channel.query({ state: true });
if (i !== nClients - 1) {
Expand Down
12 changes: 6 additions & 6 deletions test/serverside.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,12 @@ describe('App configs', function() {

it('Using a tampered token fails because of auth enabled', async function() {
await expectHTTPErrorCode(401, client2.setUser(user, userToken));
client2.disconnect();
client2.disconnect(5000);
});

it('Using dev token fails because of auth enabled', async function() {
await expectHTTPErrorCode(401, client2.setUser(user, client2.devToken(user.id)));
client2.disconnect();
client2.disconnect(5000);
});

it('Disable auth checks', async function() {
Expand All @@ -671,12 +671,12 @@ describe('App configs', function() {

it('Using a tampered token does not fail because auth is disabled', async function() {
await client2.setUser(user, userToken);
client2.disconnect();
client2.disconnect(5000);
});

it('Using dev token does not fail because auth is disabled', async function() {
await client2.setUser(user, client2.devToken(user.id));
client2.disconnect();
client2.disconnect(5000);
});

it('Disable permission checks', async function() {
Expand All @@ -689,7 +689,7 @@ describe('App configs', function() {
it('A user can do super stuff because permission checks are off', async function() {
await client2.setUser(user, userToken);
await client2.channel('messaging', 'secret-place').watch();
client2.disconnect();
client2.disconnect(5000);
});

it('Re-enable permission checks', async function() {
Expand All @@ -716,7 +716,7 @@ describe('App configs', function() {

it('Using a tampered token fails because auth is back on', async function() {
await expectHTTPErrorCode(401, client2.setUser(user, userToken));
client2.disconnect();
client2.disconnect(5000);
});

describe('Push notifications', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/silent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ chai.use(chaiAsPromised);

async function testUnreadCount(userId, expectedCount) {
const client = await getTestClientForUser(userId);
await client.disconnect();
await client.disconnect(5000);
expect(client.health.me.unread_count).to.eql(expectedCount);
}

Expand Down
Loading

0 comments on commit eb4793f

Please sign in to comment.