UNPKG

2.04 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.SearchResult = SearchResult;
9
10var utils = _interopRequireWildcard(require("../utils"));
11
12var _eventContext = require("./event-context");
13
14/*
15Copyright 2015, 2016 OpenMarket Ltd
16Copyright 2019 The Matrix.org Foundation C.I.C.
17
18Licensed under the Apache License, Version 2.0 (the "License");
19you may not use this file except in compliance with the License.
20You may obtain a copy of the License at
21
22 http://www.apache.org/licenses/LICENSE-2.0
23
24Unless required by applicable law or agreed to in writing, software
25distributed under the License is distributed on an "AS IS" BASIS,
26WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27See the License for the specific language governing permissions and
28limitations under the License.
29*/
30
31/**
32 * @module models/search-result
33 */
34
35/**
36 * Construct a new SearchResult
37 *
38 * @param {number} rank where this SearchResult ranks in the results
39 * @param {event-context.EventContext} eventContext the matching event and its
40 * context
41 *
42 * @constructor
43 */
44function SearchResult(rank, eventContext) {
45 this.rank = rank;
46 this.context = eventContext;
47}
48/**
49 * Create a SearchResponse from the response to /search
50 * @static
51 * @param {Object} jsonObj
52 * @param {function} eventMapper
53 * @return {SearchResult}
54 */
55
56
57SearchResult.fromJson = function (jsonObj, eventMapper) {
58 const jsonContext = jsonObj.context || {};
59 const events_before = jsonContext.events_before || [];
60 const events_after = jsonContext.events_after || [];
61 const context = new _eventContext.EventContext(eventMapper(jsonObj.result));
62 context.setPaginateToken(jsonContext.start, true);
63 context.addEvents(utils.map(events_before, eventMapper), true);
64 context.addEvents(utils.map(events_after, eventMapper), false);
65 context.setPaginateToken(jsonContext.end, false);
66 return new SearchResult(jsonObj.rank, context);
67};
\No newline at end of file