1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-18 05:42:00 +03:00

Add storeEvents impl.

This commit is contained in:
Kegan Dougal
2015-07-02 11:03:50 +01:00
parent 023a3cf2bd
commit ea738e31ba
4 changed files with 99 additions and 11 deletions

View File

@@ -830,27 +830,38 @@ MatrixClient.prototype.roomState = function(roomId, callback) {
*/
MatrixClient.prototype.scrollback = function(room, limit, callback) {
if (utils.isFunction(limit)) { callback = limit; limit = undefined; }
limit = limit || 30;
if (room.oldState.paginationToken === null) {
return q(room); // already at the start.
}
// attempt to grab more events from the store first
var numAdded = this.store.scrollback(room, limit).length;
if (numAdded === limit) {
// store contained everything we needed.
return q(room);
}
// reduce the required number of events appropriately
limit = limit - numAdded;
var path = utils.encodeUri(
"/rooms/$roomId/messages", {$roomId: room.roomId}
);
limit = limit || 30;
var params = {
from: room.oldState.paginationToken,
limit: limit,
dir: 'b'
};
var defer = q.defer();
var self = this;
this._http.authedRequest(callback, "GET", path, params).done(function(res) {
room.addEventsToTimeline(
utils.map(res.chunk, _PojoToMatrixEventMapper), true
);
var matrixEvents = utils.map(res.chunk, _PojoToMatrixEventMapper);
room.addEventsToTimeline(matrixEvents, true);
room.oldState.paginationToken = res.end;
if (res.chunk.length < limit) {
room.oldState.paginationToken = null;
}
self.store.storeEvents(room, matrixEvents, res.end, true);
_resolve(callback, defer, room);
}, function(err) {
_reject(callback, defer, err);