-
Notifications
You must be signed in to change notification settings - Fork 26
/
idunn_poi.js
159 lines (148 loc) · 4.91 KB
/
idunn_poi.js
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
import Poi from './poi';
import Ajax from '../../libs/ajax';
import nconf from '@qwant/nconf-getter';
import Error from '../../adapters/error';
import { sources } from 'config/constants.yml';
import Telemetry from '../../libs/telemetry';
import QueryContext from 'src/adapters/query_context';
const serviceConfig = nconf.get().services;
const LNG_INDEX = 0;
const LAT_INDEX = 1;
export default class IdunnPoi extends Poi {
constructor(rawPoi) {
let alternativeName = '';
if (rawPoi.address) {
if (rawPoi.address.label) {
alternativeName = rawPoi.address.label;
} else if (rawPoi.address.street && rawPoi.address.street.label) {
alternativeName = rawPoi.address.street.label;
}
}
const latLng = {
lat: rawPoi.geometry.coordinates[LAT_INDEX],
lng: rawPoi.geometry.coordinates[LNG_INDEX],
};
super(rawPoi.id, rawPoi.name, alternativeName, rawPoi.type, latLng, rawPoi.class_name,
rawPoi.subclass_name);
this.blocks = rawPoi.blocks;
this.localName = rawPoi.local_name;
this.address = IdunnPoi.getAddress(rawPoi);
this.bbox = rawPoi.geometry.bbox;
this.meta = rawPoi.meta || {};
if (this.isFromOSM()) {
const [_osmKey, itemKind, itemId] = rawPoi.id.split(':');
this.viewUrl = `https://www.openstreetmap.org/${itemKind}/${itemId}`;
this.editUrl = `https://www.openstreetmap.org/edit?editor=id&${itemKind}=${itemId}`;
}
this.blocksByType = {};
if (this.blocks) {
this.blocksByType = Object.assign({}, ...this.blocks.map(b => ({ [b.type]: b })));
const imagesBlock = this.blocksByType.images;
if (imagesBlock && imagesBlock.images.length > 0) {
this.topImageUrl = imagesBlock.images[0].url;
}
}
}
getInputValue() {
switch (this.type) {
case 'address':
case 'street':
return this.alternativeName;
default:
return this.name;
}
}
/* ?bbox={bbox}&category=<category-name>&size={size}&verbosity=long/ */
static async poiCategoryLoad(bbox, size, category, query) {
const url = `${serviceConfig.idunn.url}/v1/places`;
const requestParams = { bbox, size };
if (category) {
requestParams['category'] = category;
}
if (query) {
requestParams['q'] = query;
}
try {
const response = await Ajax.getLang(url, requestParams);
response.places = response.places.map(rawPoi => new IdunnPoi(rawPoi));
return response;
} catch (err) {
if (err === 400 || err === 404) {
return {};
} else {
const s_requestParams = JSON.stringify(requestParams);
Error.sendOnce(
'idunn_poi', 'poiApiLoad',
`unknown error getting idunn poi reaching ${url} with options ${s_requestParams}`,
err
);
return {};
}
}
}
static async poiApiLoad(obj, options = {}) {
let rawPoi = null;
const url = `${serviceConfig.idunn.url}/v1/places/${obj.id}`;
let requestParams = {};
if (options.simple) {
requestParams = { verbosity: 'short' };
}
try {
const headers = QueryContext.toHeaders(obj.queryContext);
rawPoi = await Ajax.getLang(url, requestParams, {}, headers);
return new IdunnPoi(rawPoi);
} catch (err) {
if (err === 404) {
return;
} else if (err === 0 && obj.queryContext !== undefined) {
// When the OPTIONS request is rejected, the error is 0 and not 405
console.warn('Headers aren\'t allowed, sending query without them...');
obj.queryContext = undefined;
return this.poiApiLoad(obj);
}
const s_requestParams = JSON.stringify(requestParams);
Error.sendOnce(
'idunn_poi', 'poiApiLoad',
`unknown error getting idunn poi reaching ${url} with options ${s_requestParams}`,
err
);
}
}
static getAddress(rawPoi) {
switch (rawPoi.type) {
case 'admin':
return { label: rawPoi.address.admin.label };
case 'address':
case 'street': {
const postcode = (rawPoi.address.postcode || '').split(';', 1)[0];
const city = rawPoi.address.admins.find(a => a.class_name === 'city') || {};
const country = rawPoi.address.admins.find(a => a.class_name === 'country') || {};
const label = [postcode, city.name, country.name]
.filter(x => x).join(', ');
return { label };
}
default:
return rawPoi.address;
}
}
logGradesClick(template) {
const grades = this.blocksByType.grades;
if (grades && grades.url) {
Telemetry.add('reviews', 'poi', this.meta.source,
Telemetry.buildInteractionData({
id: this.id,
source: this.meta.source,
template,
zone: template === 'multiple' ? 'list' : 'detail',
element: 'reviews',
})
);
}
}
isFromOSM() {
return this.meta && this.meta.source === sources.osm;
}
isFromPagesjaunes() {
return this.meta && this.meta.source === sources.pagesjaunes;
}
}