1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-09-01 21:21:58 +03:00

Merge pull request #60 from matrix-org/kegan/guest-access

Add constructs for guest access
This commit is contained in:
Kegsay
2016-01-11 15:19:14 +00:00
3 changed files with 202 additions and 23 deletions

View File

@@ -59,6 +59,7 @@ function SyncApi(client, opts) {
opts.pollTimeout = opts.pollTimeout || (30 * 1000);
opts.pendingEventOrdering = opts.pendingEventOrdering || "chronological";
this.opts = opts;
this._peekRoomId = null;
}
/**
@@ -154,6 +155,100 @@ SyncApi.prototype.syncLeftRooms = function() {
});
};
/**
* Peek into a room. This will result in the room in question being synced so it
* is accessible via getRooms(). Live updates for the room will be provided.
* @param {string} roomId The room ID to peek into.
* @return {Promise} A promise which resolves once the room has been added to the
* store.
*/
SyncApi.prototype.peek = function(roomId) {
var self = this;
var client = this.client;
this._peekRoomId = roomId;
return this.client.roomInitialSync(roomId, 20).then(function(response) {
// make sure things are init'd
response.messages = response.messages || {};
response.messages.chunk = response.messages.chunk || [];
response.state = response.state || [];
var peekRoom = self.createRoom(roomId);
// FIXME: Mostly duplicated from _processRoomEvents but not entirely
// because "state" in this API is at the BEGINNING of the chunk
var oldStateEvents = utils.map(
utils.deepCopy(response.state), client.getEventMapper()
);
var stateEvents = utils.map(
response.state, client.getEventMapper()
);
var messages = utils.map(
response.messages.chunk, client.getEventMapper()
);
if (response.messages.start) {
peekRoom.oldState.paginationToken = response.messages.start;
}
// set the state of the room to as it was after the timeline executes
peekRoom.oldState.setStateEvents(oldStateEvents);
peekRoom.currentState.setStateEvents(stateEvents);
self._resolveInvites(peekRoom);
peekRoom.recalculate(self.client.credentials.userId);
// roll backwards to diverge old state:
peekRoom.addEventsToTimeline(messages.reverse(), true);
client.store.storeRoom(peekRoom);
client.emit("Room", peekRoom);
self._peekPoll(roomId);
return peekRoom;
});
};
/**
* Stop polling for updates in the peeked room. NOPs if there is no room being
* peeked.
*/
SyncApi.prototype.stopPeeking = function() {
this._peekRoomId = null;
};
/**
* Do a peek room poll.
* @param {string} roomId
* @param {string} token from= token
*/
SyncApi.prototype._peekPoll = function(roomId, token) {
if (this._peekRoomId !== roomId) {
console.log("Stopped peeking in room %s", roomId);
return;
}
var self = this;
// FIXME: gut wrenching; hard-coded timeout values
this.client._http.authedRequestWithPrefix(undefined, "GET", "/events", {
room_id: roomId,
timeout: 30 * 1000,
from: token
}, undefined, httpApi.PREFIX_V1, 50 * 1000).done(function(res) {
// strip out events which aren't for the given room_id (e.g presence)
var events = res.chunk.filter(function(e) {
return e.room_id === roomId;
}).map(self.client.getEventMapper());
var room = self.client.getRoom(roomId);
room.addEvents(events);
self._peekPoll(roomId, res.end);
}, function(err) {
console.error("[%s] Peek poll failed: %s", roomId, err);
setTimeout(function() {
self._peekPoll(roomId, token);
}, 30 * 1000);
});
};
/**
* Main entry point
*/
@@ -205,8 +300,8 @@ SyncApi.prototype.sync = function() {
}
if (client.isGuest()) {
// no push rules for guests
getFilter();
// no push rules for guests, no access to POST filter for guests.
self._sync({});
}
else {
getPushRules();
@@ -225,8 +320,13 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
var self = this;
attempt = attempt || 1;
var filterId = syncOptions.filterId;
if (client.isGuest() && !filterId) {
filterId = this._getGuestFilter();
}
var qps = {
filter: syncOptions.filterId,
filter: filterId,
timeout: this.opts.pollTimeout,
since: client.store.getSyncToken() || undefined // do not send 'null'
};
@@ -238,10 +338,6 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
qps.timeout = 1;
}
if (client._guestRooms && client._isGuest) {
qps.room_id = client._guestRooms;
}
client._http.authedRequestWithPrefix(
undefined, "GET", "/sync", qps, undefined, httpApi.PREFIX_V2_ALPHA,
this.opts.pollTimeout + BUFFER_PERIOD_MS // normal timeout= plus buffer time
@@ -562,7 +658,25 @@ SyncApi.prototype._processRoomEvents = function(room, stateEventList,
// execute the timeline events, this will begin to diverge the current state
// if the timeline has any state events in it.
room.addEventsToTimeline(timelineEventList);
};
/**
* @return {string}
*/
SyncApi.prototype._getGuestFilter = function() {
var guestRooms = this.client._guestRooms; // FIXME: horrible gut-wrenching
if (!guestRooms) {
return "{}";
}
// we just need to specify the filter inline if we're a guest because guests
// can't create filters.
return JSON.stringify({
room: {
timeline: {
limit: 20
}
}
});
};
function retryTimeMsForAttempt(attempt) {