1 | ;
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.SearchResult = SearchResult;
|
9 |
|
10 | var utils = _interopRequireWildcard(require("../utils"));
|
11 |
|
12 | var _eventContext = require("./event-context");
|
13 |
|
14 | /*
|
15 | Copyright 2015, 2016 OpenMarket Ltd
|
16 | Copyright 2019 The Matrix.org Foundation C.I.C.
|
17 |
|
18 | Licensed under the Apache License, Version 2.0 (the "License");
|
19 | you may not use this file except in compliance with the License.
|
20 | You may obtain a copy of the License at
|
21 |
|
22 | http://www.apache.org/licenses/LICENSE-2.0
|
23 |
|
24 | Unless required by applicable law or agreed to in writing, software
|
25 | distributed under the License is distributed on an "AS IS" BASIS,
|
26 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
27 | See the License for the specific language governing permissions and
|
28 | limitations 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 | */
|
44 | function 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 |
|
57 | SearchResult.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 |