/
PlaceFilterInfo.ts
214 lines (197 loc) · 8.24 KB
/
PlaceFilterInfo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2020 Vircadia Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict'
import { Request } from 'express';
import { PlaceEntity } from '@Entities/PlaceEntity';
import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter';
import { Maturity } from '@Entities/Sets/Maturity';
import { Visibility } from '@Entities/Sets/Visibility';
import { VKeyedCollection } from '@Tools/vTypes';
import { Logger } from '@Tools/Logging';
// Process a request that wants to filter Account collection with parameters:
// &maturity=unrated,mature
// &tag=tag1,tag2,tag3
// &status=online,active
// online = Place/Domain are heartbeating
// active = one or more avatars are present
// &order=ascending,decending,num_users,name
// &search=regex
export class PlaceFilterInfo extends CriteriaFilter {
private _maturity: string[];
private _tags: string[];
private _ascending: number = 1;
private _orderByName: boolean = false;
private _orderByUsers: boolean = false;
private _search: string;
// Set to 'true' if the pagination was passed in the criteria query parameters
private _doingQuery: boolean = false;
public constructor() {
super();
return;
}
// Passed the request, get the filter parameters from the query.
// Here we pre-process the parameters to make the DB query construction quicker.
public parametersFromRequest(pRequest: Request) : void {
try {
// Comma separated list of attribute criteria.
if (typeof(pRequest.query.maturity) === 'string') {
this._maturity = pRequest.query.maturity.split(',');
// Check to make sure all the pieces are legal maturity names
let allLegal = true;
for (const mat of this._maturity) {
if ( ! Maturity.KnownMaturity(mat)) {
allLegal = false;
break;
};
};
if (allLegal) {
if (this._maturity.includes(Maturity.UNRATED)) {
// Adding a 'null' to the set causes places with no rating to be included
this._maturity.push(null);
};
}
else {
Logger.cdebug('query-detail', `PlaceFilterInfo.parametersFromRequest: passed undefined maturity level ${JSON.stringify(this._maturity)}`);
this._maturity = undefined;
};
Logger.cdebug('query-detail', `PlaceFilterInfo.parametersFromRequest: maturity=${JSON.stringify(this._maturity)}`);
};
// Commas separated list of place tags
if (typeof(pRequest.query.tag) === 'string') {
this._tags = pRequest.query.tag.split(',');
Logger.cdebug('query-detail', `PlaceFilterInfo.parametersFromRequest: tags=${JSON.stringify(this._tags)}`);
};
if (typeof(pRequest.query.status) === 'string') {
const statuses = pRequest.query.status.split(',');
for (const stat of statuses) {
switch (stat) {
case 'online':
break;
case 'active':
break;
default:
break;
};
};
// Logger.cdebug('query-detail', `PlaceFilterInfo.parametersFromRequest: status=${JSON.stringify(this._active)}`);
};
if (typeof(pRequest.query.order) === 'string') {
const orderings = pRequest.query.order.split(',');
for (const order of orderings) {
switch (order) {
case 'ascending':
this._ascending = 1;
break;
case 'decending':
this._ascending = -1;
break;
case 'num_users':
this._orderByUsers = true;
break;
case 'name':
this._orderByName = true;
break;
default:
break;
};
};
Logger.cdebug('query-detail', `PlaceFilterInfo.parametersFromRequest: order: ${this._ascending}, users: ${this._orderByUsers}, name: ${this._orderByName}`);
};
if (typeof(pRequest.query.search) === 'string') {
const searcher = pRequest.query.search;
this._search = searcher;
Logger.cdebug('query-detail', `PlaceFilterInfo.parametersFromRequest: search: ${this._search}`);
};
}
catch (e) {
Logger.error('PlaceFilterInfo: parameters from request: exception: ' + e);
};
};
// Add any parameters to the response
public addResponseFields(pRequest: Request) {
return;
};
// Passed (what should be) an AccountEntity, test if the filters
// think it's passable.
// Return 'true' of this account fits the search criteria.
public criteriaTest(pThingy: any): boolean {
let ret = false;
if (this._doingQuery) {
ret = true;
}
else {
if (this._maturity) {
if (pThingy.hasOwnProperty('maturity')) {
if (this._maturity.includes((pThingy as PlaceEntity).maturity)) {
ret = true;
};
}
else {
if (this._maturity.includes(Maturity.UNRATED)) {
ret = true;
};
};
};
if (!ret && this._tags && pThingy.hasOwnProperty('tags')) {
for (const aTag of this._tags) {
if ((pThingy as PlaceEntity).tags.includes(aTag)) {
ret = true;
break;
};
};
};
if (!ret && this._search && pThingy.hasOwnProperty('name')) {
const matches = (pThingy as PlaceEntity).name.match(this._search);
ret = matches && (matches.length === 1);
};
};
return ret;
};
// Return the MongoDB query parameters for the search criteria
public criteriaParameters(): any {
this._doingQuery = true;
const criteria:VKeyedCollection = {};
if (this._maturity) {
/* tslint:disable-next-line */
criteria['maturity'] = { '$in': this._maturity }
};
if (this._tags) {
/* tslint:disable-next-line */
criteria['tags'] = { '$in': this._tags }
};
if (this._search) {
/* tslint:disable-next-line */
criteria['name'] = { '$regex': this._search, '$options': 'i' }
};
/* tslint:disable-next-line */
criteria['$or'] = [ { 'visibility': { '$exists': false }},
{ 'visibility': Visibility.OPEN },
];
return criteria;
};
public sortCriteriaParameters(): any {
if (this._orderByUsers || this._orderByName) {
const sorting: any = {};
if (this._orderByUsers) {
sorting.attendance = this._ascending;
}
if (this._orderByName) {
sorting.name = this._ascending;
}
Logger.cdebug('query-detail', `PlaceFilterInfo.sortCriteriaParameters: ${JSON.stringify(sorting)}`);
return sorting;
}
return null;
};
};