Compare commits
4 Commits
c068c759b2
...
4316b69a43
| Author | SHA1 | Date | |
|---|---|---|---|
| 4316b69a43 | |||
| b2d040dc3c | |||
| 3bf8ce6e23 | |||
| c63e495516 |
@@ -141,7 +141,7 @@ function mergeMainProfile(profile, mainProfile) {
|
|||||||
return mergedProfile;
|
return mergedProfile;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function interpolateProfiles(actorIdsOrNames, context) {
|
export async function interpolateProfiles(actorIdsOrNames, context, options = {}) {
|
||||||
const profiles = await fetchProfiles(actorIdsOrNames, context);
|
const profiles = await fetchProfiles(actorIdsOrNames, context);
|
||||||
|
|
||||||
const profilesByActorId = profiles.reduce((acc, profile) => ({
|
const profilesByActorId = profiles.reduce((acc, profile) => ({
|
||||||
@@ -303,5 +303,7 @@ export async function interpolateProfiles(actorIdsOrNames, context) {
|
|||||||
.then(transaction.commit)
|
.then(transaction.commit)
|
||||||
.catch(transaction.rollback);
|
.catch(transaction.rollback);
|
||||||
|
|
||||||
await context.knex.schema.refreshMaterializedView('actors_meta');
|
if (options.refreshView) {
|
||||||
|
await context.knex.schema.refreshMaterializedView('actors_meta');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
72
geo.mjs
Normal file
72
geo.mjs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
export async function resolvePlace(query, context) {
|
||||||
|
if (!query) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cacheKey = `place-${context.slugify(query)}`;
|
||||||
|
const cachedPlace = await context.redis.hGetAll(cacheKey);
|
||||||
|
|
||||||
|
if (context.argv.placeCache !== false && await context.redis.exists(cacheKey)) {
|
||||||
|
await context.redis.expire(cacheKey, 3600 * 24 * 30);
|
||||||
|
|
||||||
|
context.logger.debug(`Using cached place '${cacheKey}' for query '${query}': ${JSON.stringify(cachedPlace)}`);
|
||||||
|
|
||||||
|
return cachedPlace;
|
||||||
|
}
|
||||||
|
|
||||||
|
// query is a nationality, lookup would get weird results (British resolves to British, Northern Ireland)
|
||||||
|
const country = await context.knex('countries')
|
||||||
|
.where('nationality', 'ilike', `%${query}%`)
|
||||||
|
.orWhere('alpha3', 'ilike', `%${query}%`)
|
||||||
|
.orWhere('alpha2', 'ilike', `%${query}%`)
|
||||||
|
.orderBy('priority', 'desc')
|
||||||
|
.first();
|
||||||
|
|
||||||
|
if (country) {
|
||||||
|
return {
|
||||||
|
country: country.alpha2,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// https://operations.osmfoundation.org/policies/nominatim/
|
||||||
|
const res = await context.unprint.get(`https://nominatim.openstreetmap.org/search?q=${encodeURI(query)}&format=json&accept-language=en&addressdetails=1`, {
|
||||||
|
headers: {
|
||||||
|
'User-Agent': context.config.location.userAgent,
|
||||||
|
},
|
||||||
|
interval: 1000,
|
||||||
|
concurrency: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
const [item] = res.body;
|
||||||
|
|
||||||
|
if (item && item.address) {
|
||||||
|
const rawPlace = item.address;
|
||||||
|
const place = {};
|
||||||
|
|
||||||
|
if (item.class === 'place' || item.class === 'boundary') {
|
||||||
|
const location = rawPlace[item.type] || rawPlace.city || rawPlace.place || rawPlace.town;
|
||||||
|
|
||||||
|
if (location) {
|
||||||
|
place.place = location;
|
||||||
|
place.city = rawPlace.city || location;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rawPlace.state) place.state = rawPlace.state;
|
||||||
|
if (rawPlace.country_code) place.country = rawPlace.country_code.toUpperCase();
|
||||||
|
if (rawPlace.continent) place.continent = rawPlace.continent;
|
||||||
|
|
||||||
|
context.logger.debug(`Resolved place '${query}' to ${JSON.stringify(place)}`);
|
||||||
|
|
||||||
|
await context.redis.hSet(cacheKey, place);
|
||||||
|
await context.redis.expire(cacheKey, 3600 * 24 * 30);
|
||||||
|
|
||||||
|
return place;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
context.logger.error(`Failed to resolve place '${query}': ${error.message}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "traxxx-utils",
|
"name": "traxxx-utils",
|
||||||
"version": "1.2.5",
|
"version": "1.2.7",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "traxxx-utils",
|
"name": "traxxx-utils",
|
||||||
"version": "1.2.5",
|
"version": "1.2.7",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/cli": "^7.25.7",
|
"@babel/cli": "^7.25.7",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "traxxx-common",
|
"name": "traxxx-common",
|
||||||
"version": "1.2.5",
|
"version": "1.2.7",
|
||||||
"description": "Common utilities for traxxx core and web.",
|
"description": "Common utilities for traxxx core and web.",
|
||||||
"main": "src/app.js",
|
"main": "src/app.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user