aboutsummaryrefslogtreecommitdiffstats
path: root/lib/locations.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-10-30 16:35:08 -0700
committerAdam Soltys <[email protected]>2024-10-30 16:35:08 -0700
commit72e2ff9b1d1f69677af1971a1fa88636cf8a1d63 (patch)
treebb793e952c8a9a67236a6447ed67a366680093ce /lib/locations.ts
parentd09712ef8f8ea8218ce229b233fc0013e6c06f7a (diff)
downloadcoinos-server-72e2ff9b1d1f69677af1971a1fa88636cf8a1d63.tar.gz
coinos-server-72e2ff9b1d1f69677af1971a1fa88636cf8a1d63.zip
dedup locations
Diffstat (limited to 'lib/locations.ts')
-rw-r--r--lib/locations.ts52
1 files changed, 31 insertions, 21 deletions
diff --git a/lib/locations.ts b/lib/locations.ts
index 11981c19..74baf7e6 100644
--- a/lib/locations.ts
+++ b/lib/locations.ts
@@ -1,11 +1,24 @@
+import { g, s } from "$lib/db";
import { err } from "$lib/logging";
+import { fields, pick } from "$lib/utils";
import got from "got";
-import { g, s } from "$lib/db";
-import { fields, pick, uniq } from "$lib/utils";
-export let getLocations = async () => {
+const dedup = (array) =>
+ Object.values(
+ array.reduce((acc, obj) => {
+ if (
+ !acc[obj.id] ||
+ new Date(obj.updated_at) > new Date(acc[obj.id].updated_at)
+ ) {
+ acc[obj.id] = obj;
+ }
+ return acc;
+ }, {}),
+ );
+
+export const getLocations = async () => {
try {
- let previous = await g("locations");
+ const previous = await g("locations");
let since = await g("locations:since");
if (!since) since = "2022-09-19T00:00:00Z";
if (Date.now() - new Date(since).getTime() < 60000) return;
@@ -16,35 +29,32 @@ export let getLocations = async () => {
locations = locations.filter(
(l) =>
- l["osm_json"].tags &&
- l["osm_json"].tags["payment:coinos"] === "yes" &&
- l["osm_json"].tags.name &&
- !l["deleted_at"],
+ l.osm_json.tags &&
+ l.osm_json.tags["payment:coinos"] === "yes" &&
+ l.osm_json.tags.name &&
+ !l.deleted_at,
);
locations.map((l) => {
- let { bounds, lat, lon } = l["osm_json"];
+ const { bounds, lat, lon } = l.osm_json;
- l["osm_json"].lat = lat || (bounds.minlat + bounds.maxlat) / 2;
- l["osm_json"].lon = lon || (bounds.minlon + bounds.maxlon) / 2;
+ l.osm_json.lat = lat || (bounds.minlat + bounds.maxlat) / 2;
+ l.osm_json.lon = lon || (bounds.minlon + bounds.maxlon) / 2;
});
- for await (let l of locations) {
- let username = l.tags["payment:coinos"];
+ for await (const l of locations) {
+ const username = l.tags["payment:coinos"];
if (username) {
- let uid = await g(`user:${username}`);
- let user = await g(`user:${uid}`);
- if (user) l["osm_json"].tags.user = pick(user, fields);
+ const uid = await g(`user:${username}`);
+ const user = await g(`user:${uid}`);
+ if (user) l.osm_json.tags.user = pick(user, fields);
}
}
locations.push(...previous);
- await s(
- "locations",
- uniq(locations, (l) => l.id),
- );
- await s("locations:since", new Date().toISOString().split(".")[0] + "Z");
+ await s("locations", dedup(locations));
+ await s("locations:since", `${new Date().toISOString().split(".")[0]}Z`);
} catch (e) {
err("problem fetching locations", e);
}