{"action":"created","comment":{"url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/comments/49858032","id":49858032,"diff_hunk":"@@ -2060,6 +2066,145 @@ MatrixClient.prototype.paginateEventContext = function(eventContext, opts) {\n     return promise;\n };\n \n+/**\n+ * Get an EventTimeline for the given event\n+ *\n+ * <p>If the room object already has the given event in its store, the\n+ * corresponding timeline will be returned. Otherwise, we make a /context\n+ * request, and use it to construct an EventTimeline.\n+ *\n+ * @param {Room} room  The room to look for the event in\n+ * @param {string} eventId  The ID of the event to look for\n+ *\n+ * @return {module:client.Promise} Resolves:\n+ *    {@link module:models/event-timeline~EventTimeline} including the given\n+ *    event\n+ */\n+MatrixClient.prototype.getEventTimeline = function(room, eventId) {\n+    // don't allow any timeline support unless it's been enabled.\n+    if (!this.timelineSupport) {\n+        throw Error(\"timeline support is disabled. Set the 'timelineSupport'\" +\n+                    \" parameter to true when creating MatrixClient to enable\" +\n+                    \" it.\");\n+    }\n+\n+    if (room.getTimelineForEvent(eventId)) {\n+        return q(room.getTimelineForEvent(eventId));\n+    }\n+\n+    var path = utils.encodeUri(\n+        \"/rooms/$roomId/context/$eventId\", {\n+            $roomId: room.roomId,\n+            $eventId: eventId,\n+        }\n+    );\n+\n+    // TODO: we should implement a backoff (as per scrollback()) to deal more\n+    // nicely with HTTP errors.\n+    var self = this;\n+    var promise =\n+        self._http.authedRequest(undefined, \"GET\", path\n+    ).then(function(res) {\n+        if (!res.event) {\n+            throw Error(\"'event' not in '/context' result - homeserver too old?\");\n+        }\n+\n+        // by the time the request completes, the event might have ended up in\n+        // the timeline.\n+        if (room.getTimelineForEvent(eventId)) {\n+            return room.getTimelineForEvent(eventId);\n+        }\n+\n+        // we start with the last event, since that's the point at which we\n+        // have known state.\n+        var events = res.events_before\n+            .concat([res.event])\n+            .concat(res.events_after);\n+        events.reverse();\n+        var matrixEvents = utils.map(events, self.getEventMapper());\n+\n+        var timeline = room.getTimelineForEvent(matrixEvents[0].getId());\n+        if (!timeline) {\n+            timeline = room.createTimeline();\n+            timeline.initialiseState(utils.map(res.state,\n+                                               self.getEventMapper()));\n+            timeline.getState(false).paginationToken = res.end;\n+        }\n+        room.addEventsToTimeline(matrixEvents, true, timeline, res.start);\n+\n+        // there is no guarantee that the event ended up in \"timeline\" (we\n+        // might have switched to a neighbouring timeline) - so check the\n+        // room's index again. On the other hand, there's no guarantee the\n+        // event ended up anywhere, if it was later redacted, so we just\n+        // return the timeline we first thought of.\n+        return room.getTimelineForEvent(eventId) || timeline;\n+    });\n+    return promise;\n+};\n+\n+\n+/**\n+ * Take an EventTimeline, and back/forward-fill results.\n+ *\n+ * @param {module:models/event-timeline~EventTimeline} eventTimeline timeline\n+ *    object to be updated\n+ * @param {Object =} opts\n+ * @param {boolean}  [opts.backwards = false]  true to fill backwards,\n+ *    false to go forwards\n+ * @param {number}   [opts.limit = 30]         number of events to request\n+ *\n+ * @return {module:client.Promise} Resolves: false if there are no events and\n+ *    we reached the end of the timeline; else true. Rejects: Error with an error response\n+ */\n+MatrixClient.prototype.paginateEventTimeline = function(eventTimeline, opts) {\n+    // TODO: we should implement a backoff (as per scrollback()) to deal more\n+    // nicely with HTTP errors.\n+    opts = opts || {};\n+    var backwards = opts.backwards || false;\n+\n+    var token = eventTimeline.getPaginationToken(backwards);\n+    if (!token) {\n+        // no more results.\n+        return q.reject(new Error(\"No paginate token\"));\n+    }\n+\n+    var dir = backwards ? 'b' : 'f';\n+    var pendingRequest = eventTimeline._paginationRequests[dir];\n+\n+    if (pendingRequest) {\n+        // already a request in progress - return the existing promise\n+        return pendingRequest;\n+    }\n+\n+    var path = utils.encodeUri(\n+        \"/rooms/$roomId/messages\", {$roomId: eventTimeline.getRoomId()}","path":"lib/client.js","position":135,"original_position":135,"commit_id":"e68ab7d54a0bd5436e87f71ae7f663227ed35a68","original_commit_id":"e68ab7d54a0bd5436e87f71ae7f663227ed35a68","user":{"login":"Kegsay","id":7190048,"avatar_url":"https://avatars.githubusercontent.com/u/7190048?v=3","gravatar_id":"","url":"https://api.github.com/users/Kegsay","html_url":"https://github.com/Kegsay","followers_url":"https://api.github.com/users/Kegsay/followers","following_url":"https://api.github.com/users/Kegsay/following{/other_user}","gists_url":"https://api.github.com/users/Kegsay/gists{/gist_id}","starred_url":"https://api.github.com/users/Kegsay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kegsay/subscriptions","organizations_url":"https://api.github.com/users/Kegsay/orgs","repos_url":"https://api.github.com/users/Kegsay/repos","events_url":"https://api.github.com/users/Kegsay/events{/privacy}","received_events_url":"https://api.github.com/users/Kegsay/received_events","type":"User","site_admin":false},"body":"`/messages` allows you to go forwards?","created_at":"2016-01-15T14:27:19Z","updated_at":"2016-01-15T14:27:19Z","html_url":"https://github.com/matrix-org/matrix-js-sdk/pull/63#discussion_r49858032","pull_request_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/63","_links":{"self":{"href":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/comments/49858032"},"html":{"href":"https://github.com/matrix-org/matrix-js-sdk/pull/63#discussion_r49858032"},"pull_request":{"href":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/63"}}},"pull_request":{"url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/63","id":56167895,"html_url":"https://github.com/matrix-org/matrix-js-sdk/pull/63","diff_url":"https://github.com/matrix-org/matrix-js-sdk/pull/63.diff","patch_url":"https://github.com/matrix-org/matrix-js-sdk/pull/63.patch","issue_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/issues/63","number":63,"state":"open","locked":false,"title":"Support for non-contiguous event timelines","user":{"login":"richvdh","id":1389908,"avatar_url":"https://avatars.githubusercontent.com/u/1389908?v=3","gravatar_id":"","url":"https://api.github.com/users/richvdh","html_url":"https://github.com/richvdh","followers_url":"https://api.github.com/users/richvdh/followers","following_url":"https://api.github.com/users/richvdh/following{/other_user}","gists_url":"https://api.github.com/users/richvdh/gists{/gist_id}","starred_url":"https://api.github.com/users/richvdh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/richvdh/subscriptions","organizations_url":"https://api.github.com/users/richvdh/orgs","repos_url":"https://api.github.com/users/richvdh/repos","events_url":"https://api.github.com/users/richvdh/events{/privacy}","received_events_url":"https://api.github.com/users/richvdh/received_events","type":"User","site_admin":false},"body":"This provides optional support for fetching old events via the /context API,\nand paginating backwards and forwards from them, eventually merging into the\nlive timeline.\n\nTo support it, events are now stored in an EventTimeline, rather than directly\nin an array in the Room; the old names are maintained as references for\ncompatibility.\n\nThe feature has to be enabled explicitly, otherwise it would be impossible for\nexisting clients to back-paginate to the old events after a gappy /sync.\n\nStill TODO here:\n\n* An object which provides a window into the timelines to make them possible to\n  use. This will be a separate PR.\n\n* Rewrite the 'EventContext' used by the searchRoomEvents API in terms of an\n  EventTimeline - it is essentially a subset.","created_at":"2016-01-15T13:19:25Z","updated_at":"2016-01-15T14:27:19Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5a211738d5c9370967562bbd10461a1def8db7cb","assignee":{"login":"Kegsay","id":7190048,"avatar_url":"https://avatars.githubusercontent.com/u/7190048?v=3","gravatar_id":"","url":"https://api.github.com/users/Kegsay","html_url":"https://github.com/Kegsay","followers_url":"https://api.github.com/users/Kegsay/followers","following_url":"https://api.github.com/users/Kegsay/following{/other_user}","gists_url":"https://api.github.com/users/Kegsay/gists{/gist_id}","starred_url":"https://api.github.com/users/Kegsay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kegsay/subscriptions","organizations_url":"https://api.github.com/users/Kegsay/orgs","repos_url":"https://api.github.com/users/Kegsay/repos","events_url":"https://api.github.com/users/Kegsay/events{/privacy}","received_events_url":"https://api.github.com/users/Kegsay/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/63/commits","review_comments_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/63/comments","review_comment_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/comments{/number}","comments_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/issues/63/comments","statuses_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/statuses/e68ab7d54a0bd5436e87f71ae7f663227ed35a68","head":{"label":"matrix-org:rav/context","ref":"rav/context","sha":"e68ab7d54a0bd5436e87f71ae7f663227ed35a68","user":{"login":"matrix-org","id":8418310,"avatar_url":"https://avatars.githubusercontent.com/u/8418310?v=3","gravatar_id":"","url":"https://api.github.com/users/matrix-org","html_url":"https://github.com/matrix-org","followers_url":"https://api.github.com/users/matrix-org/followers","following_url":"https://api.github.com/users/matrix-org/following{/other_user}","gists_url":"https://api.github.com/users/matrix-org/gists{/gist_id}","starred_url":"https://api.github.com/users/matrix-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matrix-org/subscriptions","organizations_url":"https://api.github.com/users/matrix-org/orgs","repos_url":"https://api.github.com/users/matrix-org/repos","events_url":"https://api.github.com/users/matrix-org/events{/privacy}","received_events_url":"https://api.github.com/users/matrix-org/received_events","type":"Organization","site_admin":false},"repo":{"id":31670053,"name":"matrix-js-sdk","full_name":"matrix-org/matrix-js-sdk","owner":{"login":"matrix-org","id":8418310,"avatar_url":"https://avatars.githubusercontent.com/u/8418310?v=3","gravatar_id":"","url":"https://api.github.com/users/matrix-org","html_url":"https://github.com/matrix-org","followers_url":"https://api.github.com/users/matrix-org/followers","following_url":"https://api.github.com/users/matrix-org/following{/other_user}","gists_url":"https://api.github.com/users/matrix-org/gists{/gist_id}","starred_url":"https://api.github.com/users/matrix-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matrix-org/subscriptions","organizations_url":"https://api.github.com/users/matrix-org/orgs","repos_url":"https://api.github.com/users/matrix-org/repos","events_url":"https://api.github.com/users/matrix-org/events{/privacy}","received_events_url":"https://api.github.com/users/matrix-org/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/matrix-org/matrix-js-sdk","description":"Matrix Client-Server SDK for JavaScript","fork":false,"url":"https://api.github.com/repos/matrix-org/matrix-js-sdk","forks_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/forks","keys_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/keys{/key_id}","collaborators_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/teams","hooks_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/hooks","issue_events_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/issues/events{/number}","events_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/events","assignees_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/assignees{/user}","branches_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/branches{/branch}","tags_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/tags","blobs_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/git/refs{/sha}","trees_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/git/trees{/sha}","statuses_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/statuses/{sha}","languages_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/languages","stargazers_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/stargazers","contributors_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/contributors","subscribers_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/subscribers","subscription_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/subscription","commits_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/commits{/sha}","git_commits_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/git/commits{/sha}","comments_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/comments{/number}","issue_comment_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/issues/comments{/number}","contents_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/contents/{+path}","compare_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/compare/{base}...{head}","merges_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/merges","archive_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/downloads","issues_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/issues{/number}","pulls_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls{/number}","milestones_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/milestones{/number}","notifications_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/labels{/name}","releases_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/releases{/id}","created_at":"2015-03-04T17:28:51Z","updated_at":"2016-01-12T09:38:23Z","pushed_at":"2016-01-15T13:27:06Z","git_url":"git://github.com/matrix-org/matrix-js-sdk.git","ssh_url":"git@github.com:matrix-org/matrix-js-sdk.git","clone_url":"https://github.com/matrix-org/matrix-js-sdk.git","svn_url":"https://github.com/matrix-org/matrix-js-sdk","homepage":"","size":2415,"stargazers_count":20,"watchers_count":20,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":7,"mirror_url":null,"open_issues_count":5,"forks":7,"open_issues":5,"watchers":20,"default_branch":"master"}},"base":{"label":"matrix-org:develop","ref":"develop","sha":"a8d24798e6c6c5ec535106c27f7466cc4ef20d3c","user":{"login":"matrix-org","id":8418310,"avatar_url":"https://avatars.githubusercontent.com/u/8418310?v=3","gravatar_id":"","url":"https://api.github.com/users/matrix-org","html_url":"https://github.com/matrix-org","followers_url":"https://api.github.com/users/matrix-org/followers","following_url":"https://api.github.com/users/matrix-org/following{/other_user}","gists_url":"https://api.github.com/users/matrix-org/gists{/gist_id}","starred_url":"https://api.github.com/users/matrix-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matrix-org/subscriptions","organizations_url":"https://api.github.com/users/matrix-org/orgs","repos_url":"https://api.github.com/users/matrix-org/repos","events_url":"https://api.github.com/users/matrix-org/events{/privacy}","received_events_url":"https://api.github.com/users/matrix-org/received_events","type":"Organization","site_admin":false},"repo":{"id":31670053,"name":"matrix-js-sdk","full_name":"matrix-org/matrix-js-sdk","owner":{"login":"matrix-org","id":8418310,"avatar_url":"https://avatars.githubusercontent.com/u/8418310?v=3","gravatar_id":"","url":"https://api.github.com/users/matrix-org","html_url":"https://github.com/matrix-org","followers_url":"https://api.github.com/users/matrix-org/followers","following_url":"https://api.github.com/users/matrix-org/following{/other_user}","gists_url":"https://api.github.com/users/matrix-org/gists{/gist_id}","starred_url":"https://api.github.com/users/matrix-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matrix-org/subscriptions","organizations_url":"https://api.github.com/users/matrix-org/orgs","repos_url":"https://api.github.com/users/matrix-org/repos","events_url":"https://api.github.com/users/matrix-org/events{/privacy}","received_events_url":"https://api.github.com/users/matrix-org/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/matrix-org/matrix-js-sdk","description":"Matrix Client-Server SDK for JavaScript","fork":false,"url":"https://api.github.com/repos/matrix-org/matrix-js-sdk","forks_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/forks","keys_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/keys{/key_id}","collaborators_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/teams","hooks_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/hooks","issue_events_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/issues/events{/number}","events_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/events","assignees_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/assignees{/user}","branches_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/branches{/branch}","tags_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/tags","blobs_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/git/refs{/sha}","trees_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/git/trees{/sha}","statuses_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/statuses/{sha}","languages_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/languages","stargazers_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/stargazers","contributors_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/contributors","subscribers_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/subscribers","subscription_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/subscription","commits_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/commits{/sha}","git_commits_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/git/commits{/sha}","comments_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/comments{/number}","issue_comment_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/issues/comments{/number}","contents_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/contents/{+path}","compare_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/compare/{base}...{head}","merges_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/merges","archive_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/downloads","issues_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/issues{/number}","pulls_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls{/number}","milestones_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/milestones{/number}","notifications_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/labels{/name}","releases_url":"https://api.github.com/repos/matrix-org/matrix-js-sdk/releases{/id}","created_at":"2015-03-04T17:28:51Z","updated_at":"2016-01-12T09:38:23Z","pushed_at":"2016-01-15T13:27:06Z","git_url":"git://github.com/matrix-org/matrix-js-sdk.git","ssh_url":"git@github.com:matrix-org/matrix-js-sdk.git","clone_url":"https://github.com/matrix-org/matrix-js-sdk.git","svn_url":"https://github.com/matrix-org/matrix-js-sdk","homepage":"","size":2415,"stargazers_count":20,"watchers_count":20,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":7,"mirror_url":null,"open_issues_count":5,"forks":7,"open_issues":5,"watchers":20,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/63"},"html":{"href":"https://github.com/matrix-org/matrix-js-sdk/pull/63"},"issue":{"href":"https://api.github.com/repos/matrix-org/matrix-js-sdk/issues/63"},"comments":{"href":"https://api.github.com/repos/matrix-org/matrix-js-sdk/issues/63/comments"},"review_comments":{"href":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/63/comments"},"review_comment":{"href":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/matrix-org/matrix-js-sdk/pulls/63/commits"},"statuses":{"href":"https://api.github.com/repos/matrix-org/matrix-js-sdk/statuses/e68ab7d54a0bd5436e87f71ae7f663227ed35a68"}}}}