Add place names to Activity timeline
Create a new place name resolver service with the logic previously used for My Contributions only
This commit is contained in:
8 files changed
+574
-387
No files matched your search
@@ -38,8 +38,12 @@ export default class ActivityZapItem extends Component {
|
||||
return this.item?.photo?.thumbUrl || this.item?.photo?.url;
|
||||
}
|
||||
|
||||
get placeIdentifier() {
|
||||
return this.item?.placeIdentifier;
|
||||
get placeName() {
|
||||
return this.item?.placeName;
|
||||
}
|
||||
|
||||
get placeNameLoading() {
|
||||
return this.item?.placeNameLoading;
|
||||
}
|
||||
|
||||
<template>
|
||||
@@ -79,8 +83,12 @@ export default class ActivityZapItem extends Component {
|
||||
{{/if}}
|
||||
</div>
|
||||
<span class="zap-context-text">
|
||||
{{#if this.placeIdentifier}}
|
||||
{{this.placeIdentifier}}
|
||||
{{#if this.placeName}}
|
||||
{{this.placeName}}
|
||||
{{else if this.placeNameLoading}}
|
||||
<span class="contribution-name-loading">Loading…</span>
|
||||
{{else}}
|
||||
<span class="contribution-name-loading">Unnamed place</span>
|
||||
{{/if}}
|
||||
·
|
||||
{{formatRelativeDate @item.createdAt}}
|
||||
|
||||
@@ -16,11 +16,13 @@ import { parseZapReceipt, enrichWithPhoto } from '../utils/activity';
|
||||
* user's own kind 360 photos.
|
||||
* 3. Resolve sender profiles asynchronously via a per-sender `ProfileModel`
|
||||
* subscription, updating the tracked entry fields when they load.
|
||||
* 4. Update `@tracked items` so the UI renders progressively.
|
||||
* 4. Resolve place names via the shared `placeNameResolver` service.
|
||||
* 5. Update `@tracked items` so the UI renders progressively.
|
||||
*/
|
||||
export default class ActivityService extends Service {
|
||||
@service nostrData;
|
||||
@service nostrAuth;
|
||||
@service placeNameResolver;
|
||||
|
||||
@tracked items = [];
|
||||
|
||||
@@ -76,6 +78,7 @@ export default class ActivityService extends Service {
|
||||
this._cleanupProfileSubs();
|
||||
this.items = [];
|
||||
this._userPubkey = null;
|
||||
this.placeNameResolver.reset();
|
||||
}
|
||||
|
||||
willDestroy() {
|
||||
@@ -110,7 +113,27 @@ export default class ActivityService extends Service {
|
||||
pubkey,
|
||||
});
|
||||
|
||||
// 2. Bookmark lookup is synchronous — resolve those immediately so the
|
||||
// first render shows bookmarked place names without a "Loading…" flicker.
|
||||
for (const entry of entries) {
|
||||
if (entry.osmId) {
|
||||
const bookmarkName = this.placeNameResolver.resolveBookmark(
|
||||
entry.osmId
|
||||
);
|
||||
if (bookmarkName) {
|
||||
entry.placeName = bookmarkName;
|
||||
entry.placeNameLoading = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.items = entries;
|
||||
|
||||
// 3. Async-resolve remaining entries from the IndexedDB name cache and OSM
|
||||
// cache, then fall through to the network batch for the rest.
|
||||
void this.placeNameResolver.resolveInBackground(entries).then(() => {
|
||||
this.items = [...this.items];
|
||||
});
|
||||
}
|
||||
|
||||
_resolveSender(entry) {
|
||||
@@ -119,7 +142,7 @@ export default class ActivityService extends Service {
|
||||
|
||||
// Set up a ProfileModel subscription for this sender so the entry's
|
||||
// tracked fields update when the profile arrives from cache or network.
|
||||
// The event loader (configured in nostr-data) auto-fetches missing kind 0
|
||||
// The event loader (configured in nostrData) auto-fetches missing kind 0
|
||||
// events from the IDB cache and relays.
|
||||
if (!this._profileSubs.has(pubkey)) {
|
||||
const sub = this.nostrData.store
|
||||
|
||||
@@ -2,8 +2,6 @@ import Service, { service } from '@ember/service';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import { groupPhotoContributions } from '../utils/contributions';
|
||||
|
||||
const NAME_CACHE_STORE = 'contributions-name-cache';
|
||||
|
||||
/**
|
||||
* Orchestrates loading the user's own Nostr contributions, grouping them into
|
||||
* timeline entries, and resolving place names from bookmarks or the OSM API.
|
||||
@@ -25,77 +23,11 @@ const NAME_CACHE_STORE = 'contributions-name-cache';
|
||||
export default class ContributionsService extends Service {
|
||||
@service nostrData;
|
||||
@service nostrAuth;
|
||||
@service storage;
|
||||
@service osm;
|
||||
@service localForage;
|
||||
@service placeNameResolver;
|
||||
|
||||
@tracked items = [];
|
||||
|
||||
_sub = null;
|
||||
_pendingBatchPromise = null;
|
||||
_lastBatchSignature = '';
|
||||
_unresolvable = new Set();
|
||||
|
||||
/**
|
||||
* Async name resolution. Checks, in order:
|
||||
* 1. Bookmarks (sync, instant).
|
||||
* 2. The persistent IndexedDB name cache (per-entry key).
|
||||
* 3. The OSM service's IndexedDB cache (from place detail visits).
|
||||
*
|
||||
* @param {object} entry A contribution entry with `osmType`, `osmId`, and `placeIdentifier`.
|
||||
* @returns {Promise<string|null>}
|
||||
*/
|
||||
async _resolveCachedName(entry) {
|
||||
// 1. Try bookmarks (instant)
|
||||
const bookmark = this.storage.findPlaceById(entry.osmId);
|
||||
if (bookmark?.title) return bookmark.title;
|
||||
|
||||
// 2. Try the persistent name cache (IndexedDB, per-entry, survives sessions)
|
||||
const cachedName = await this.localForage.get(
|
||||
NAME_CACHE_STORE,
|
||||
entry.placeIdentifier
|
||||
);
|
||||
if (cachedName) return cachedName;
|
||||
|
||||
// 3. Try OSM IndexedDB cache (from place detail visits)
|
||||
const cached = await this.osm.getCachedOsmObject(
|
||||
entry.osmType,
|
||||
entry.osmId
|
||||
);
|
||||
return cached?.title || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously resolves names for still-loading entries from the caches.
|
||||
* Falls through to `_maybeBatchFetchNames` for anything that remains
|
||||
* unresolved. Fire-and-forget from `_updateItems` so the list renders
|
||||
* immediately with bookmark-resolved names.
|
||||
*
|
||||
* @param {Array} entries
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async _resolveFromCache(entries) {
|
||||
const pending = entries.filter((e) => e.placeNameLoading);
|
||||
if (pending.length === 0) {
|
||||
this._maybeBatchFetchNames(this.items);
|
||||
return;
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
pending.map(async (entry) => {
|
||||
const name = await this._resolveCachedName(entry);
|
||||
if (name) {
|
||||
entry.placeName = name;
|
||||
entry.placeNameLoading = false;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// Re-render with whatever resolved, then kick off the network batch for
|
||||
// entries that are still loading.
|
||||
this.items = [...this.items];
|
||||
this._maybeBatchFetchNames(this.items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the user's contributions and subscribes to live updates.
|
||||
@@ -131,9 +63,7 @@ export default class ContributionsService extends Service {
|
||||
this._sub = null;
|
||||
}
|
||||
this.items = [];
|
||||
this._lastBatchSignature = '';
|
||||
this._pendingBatchPromise = null;
|
||||
this._unresolvable.clear();
|
||||
this.placeNameResolver.reset();
|
||||
}
|
||||
|
||||
willDestroy() {
|
||||
@@ -148,9 +78,9 @@ export default class ContributionsService extends Service {
|
||||
// 2. Bookmark lookup is synchronous — resolve those immediately so the
|
||||
// first render shows bookmarked place names without a "Loading…" flicker.
|
||||
for (const entry of entries) {
|
||||
const bookmark = this.storage.findPlaceById(entry.osmId);
|
||||
if (bookmark?.title) {
|
||||
entry.placeName = bookmark.title;
|
||||
const bookmarkName = this.placeNameResolver.resolveBookmark(entry.osmId);
|
||||
if (bookmarkName) {
|
||||
entry.placeName = bookmarkName;
|
||||
entry.placeNameLoading = false;
|
||||
}
|
||||
}
|
||||
@@ -159,121 +89,8 @@ export default class ContributionsService extends Service {
|
||||
|
||||
// 3. Async-resolve remaining entries from the IndexedDB name cache and OSM
|
||||
// cache, then fall through to the network batch for the rest.
|
||||
void this._resolveFromCache(entries);
|
||||
}
|
||||
|
||||
_isUnresolvable(entry) {
|
||||
return this._unresolvable.has(entry.placeIdentifier);
|
||||
}
|
||||
|
||||
_maybeBatchFetchNames(entries) {
|
||||
const unresolved = entries.filter(
|
||||
(e) => e.placeNameLoading && !this._isUnresolvable(e)
|
||||
);
|
||||
|
||||
// Apply fallbacks for items already marked unresolvable in this session
|
||||
for (const entry of entries) {
|
||||
if (entry.placeNameLoading && this._isUnresolvable(entry)) {
|
||||
entry.placeName = this._fallbackName(entry);
|
||||
entry.placeNameLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (unresolved.length === 0) {
|
||||
if (this.items.some((i) => !i.placeNameLoading)) {
|
||||
this.items = [...this.items];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Build a stable signature so we only fire one batch request per unique set
|
||||
const signature = unresolved
|
||||
.map((e) => e.placeIdentifier)
|
||||
.sort()
|
||||
.join('|');
|
||||
if (signature === this._lastBatchSignature && this._pendingBatchPromise) {
|
||||
return;
|
||||
}
|
||||
this._lastBatchSignature = signature;
|
||||
|
||||
this._pendingBatchPromise = this._batchResolveNames(unresolved)
|
||||
.then((nameMap) => {
|
||||
// Merge resolved names back into the current `items`.
|
||||
for (const item of this.items) {
|
||||
if (!item.placeNameLoading) continue;
|
||||
if (nameMap.has(item.placeIdentifier)) {
|
||||
const name = nameMap.get(item.placeIdentifier);
|
||||
item.placeName = name;
|
||||
item.placeNameLoading = false;
|
||||
} else {
|
||||
// Could not be resolved (deleted object, network error for this item).
|
||||
// Mark as unresolvable for this session and use a fallback.
|
||||
this._unresolvable.add(item.placeIdentifier);
|
||||
item.placeName = this._fallbackName(item);
|
||||
item.placeNameLoading = false;
|
||||
}
|
||||
}
|
||||
// Trigger a re-render
|
||||
this.items = [...this.items];
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error('[contributions] Batch name resolution failed', e);
|
||||
// On a total failure, mark all as unresolvable and apply fallbacks
|
||||
for (const item of this.items) {
|
||||
if (item.placeNameLoading) {
|
||||
this._unresolvable.add(item.placeIdentifier);
|
||||
item.placeName = this._fallbackName(item);
|
||||
item.placeNameLoading = false;
|
||||
}
|
||||
}
|
||||
this.items = [...this.items];
|
||||
})
|
||||
.finally(() => {
|
||||
this._pendingBatchPromise = null;
|
||||
});
|
||||
}
|
||||
|
||||
_fallbackName(item) {
|
||||
return `OSM ${item.osmType} ${item.osmId}`;
|
||||
}
|
||||
|
||||
async _batchResolveNames(entries) {
|
||||
const nameMap = new Map();
|
||||
const toFetch = [];
|
||||
|
||||
// Re-check cache in case it was populated between the trigger and now
|
||||
for (const entry of entries) {
|
||||
const cached = await this._resolveCachedName(entry);
|
||||
if (cached) {
|
||||
nameMap.set(entry.placeIdentifier, cached);
|
||||
} else {
|
||||
toFetch.push({ osmType: entry.osmType, osmId: entry.osmId });
|
||||
}
|
||||
}
|
||||
|
||||
if (toFetch.length === 0) return nameMap;
|
||||
|
||||
const places = await this.osm.fetchOsmObjectsBatch(toFetch);
|
||||
|
||||
// Persist each newly-resolved name to the IndexedDB name cache as its own
|
||||
// entry (per-placeIdentifier key) so we don't re-fetch next session.
|
||||
const writePromises = [];
|
||||
for (const entry of entries) {
|
||||
if (nameMap.has(entry.placeIdentifier)) continue;
|
||||
const cacheKey = `${entry.osmType}:${entry.osmId}`;
|
||||
const place = places.get(cacheKey);
|
||||
if (place?.title) {
|
||||
nameMap.set(entry.placeIdentifier, place.title);
|
||||
writePromises.push(
|
||||
this.localForage.set(
|
||||
NAME_CACHE_STORE,
|
||||
entry.placeIdentifier,
|
||||
place.title
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
await Promise.all(writePromises);
|
||||
return nameMap;
|
||||
void this.placeNameResolver.resolveInBackground(entries).then(() => {
|
||||
this.items = [...this.items];
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
import Service, { service } from '@ember/service';
|
||||
|
||||
const NAME_CACHE_STORE = 'place-name-cache';
|
||||
|
||||
export default class PlaceNameResolverService extends Service {
|
||||
@service storage;
|
||||
@service osm;
|
||||
@service localForage;
|
||||
|
||||
_pendingBatchPromise = null;
|
||||
_lastBatchSignature = '';
|
||||
_unresolvable = new Set();
|
||||
|
||||
/**
|
||||
* Synchronous bookmark lookup. Returns the bookmark title if found, null otherwise.
|
||||
*
|
||||
* @param {string} osmId The OSM entity ID (e.g. "123456")
|
||||
* @returns {string|null}
|
||||
*/
|
||||
resolveBookmark(osmId) {
|
||||
const bookmark = this.storage.findPlaceById(osmId);
|
||||
return bookmark?.title || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronous name resolution from caches (localForage → OSM IDB cache).
|
||||
* Returns the resolved name or null if not in cache.
|
||||
*
|
||||
* @param {object} entry Entry with placeIdentifier, osmType, osmId
|
||||
* @returns {Promise<string|null>}
|
||||
*/
|
||||
async _resolveCachedName(entry) {
|
||||
// 1. Try the persistent name cache (IndexedDB, per-entry, survives sessions)
|
||||
const cachedName = await this.localForage.get(
|
||||
NAME_CACHE_STORE,
|
||||
entry.placeIdentifier
|
||||
);
|
||||
if (cachedName) return cachedName;
|
||||
|
||||
// 2. Try OSM IndexedDB cache (from place detail visits)
|
||||
const cached = await this.osm.getCachedOsmObject(
|
||||
entry.osmType,
|
||||
entry.osmId
|
||||
);
|
||||
return cached?.title || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves names for entries in the background.
|
||||
* Fire-and-forget: resolves from caches, then falls through to batch OSM fetch
|
||||
* for anything still unresolved. Mutates entry.placeName and entry.placeNameLoading.
|
||||
* The caller is responsible for triggering a re-render (e.g. `this.items = [...this.items]`).
|
||||
*
|
||||
* @param {Array} entries Entries with placeIdentifier, osmType, osmId, placeName, placeNameLoading
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async resolveInBackground(entries) {
|
||||
const pending = entries.filter((e) => e.placeNameLoading);
|
||||
if (pending.length === 0) {
|
||||
this._maybeBatchFetchNames(entries);
|
||||
return;
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
pending.map(async (entry) => {
|
||||
const name = await this._resolveCachedName(entry);
|
||||
if (name) {
|
||||
entry.placeName = name;
|
||||
entry.placeNameLoading = false;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// Trigger re-render via caller, then kick off the network batch for
|
||||
// entries that are still loading.
|
||||
this._maybeBatchFetchNames(entries);
|
||||
}
|
||||
|
||||
_isUnresolvable(entry) {
|
||||
return this._unresolvable.has(entry.placeIdentifier);
|
||||
}
|
||||
|
||||
_maybeBatchFetchNames(entries) {
|
||||
const unresolved = entries.filter(
|
||||
(e) => e.placeNameLoading && !this._isUnresolvable(e)
|
||||
);
|
||||
|
||||
// Apply fallbacks for items already marked unresolvable in this session
|
||||
for (const entry of entries) {
|
||||
if (entry.placeNameLoading && this._isUnresolvable(entry)) {
|
||||
entry.placeName = this._fallbackName(entry);
|
||||
entry.placeNameLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (unresolved.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Build a stable signature so we only fire one batch request per unique set
|
||||
const signature = unresolved
|
||||
.map((e) => e.placeIdentifier)
|
||||
.sort()
|
||||
.join('|');
|
||||
if (signature === this._lastBatchSignature && this._pendingBatchPromise) {
|
||||
return;
|
||||
}
|
||||
this._lastBatchSignature = signature;
|
||||
|
||||
this._pendingBatchPromise = this._batchResolveNames(unresolved)
|
||||
.then((nameMap) => {
|
||||
// Merge resolved names back into the entries
|
||||
for (const entry of entries) {
|
||||
if (!entry.placeNameLoading) continue;
|
||||
if (nameMap.has(entry.placeIdentifier)) {
|
||||
const name = nameMap.get(entry.placeIdentifier);
|
||||
entry.placeName = name;
|
||||
entry.placeNameLoading = false;
|
||||
} else {
|
||||
// Could not be resolved (deleted object, network error for this item).
|
||||
// Mark as unresolvable for this session and use a fallback.
|
||||
this._unresolvable.add(entry.placeIdentifier);
|
||||
entry.placeName = this._fallbackName(entry);
|
||||
entry.placeNameLoading = false;
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error('[place-name-resolver] Batch name resolution failed', e);
|
||||
// On a total failure, mark all as unresolvable and apply fallbacks
|
||||
for (const entry of entries) {
|
||||
if (entry.placeNameLoading) {
|
||||
this._unresolvable.add(entry.placeIdentifier);
|
||||
entry.placeName = this._fallbackName(entry);
|
||||
entry.placeNameLoading = false;
|
||||
}
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
this._pendingBatchPromise = null;
|
||||
});
|
||||
}
|
||||
|
||||
_fallbackName(entry) {
|
||||
return `OSM ${entry.osmType} ${entry.osmId}`;
|
||||
}
|
||||
|
||||
async _batchResolveNames(entries) {
|
||||
const nameMap = new Map();
|
||||
const toFetch = [];
|
||||
|
||||
// Re-check cache in case it was populated between the trigger and now
|
||||
for (const entry of entries) {
|
||||
const cached = await this._resolveCachedName(entry);
|
||||
if (cached) {
|
||||
nameMap.set(entry.placeIdentifier, cached);
|
||||
} else {
|
||||
toFetch.push({ osmType: entry.osmType, osmId: entry.osmId });
|
||||
}
|
||||
}
|
||||
|
||||
if (toFetch.length === 0) return nameMap;
|
||||
|
||||
const places = await this.osm.fetchOsmObjectsBatch(toFetch);
|
||||
|
||||
// Persist each newly-resolved name to the IndexedDB name cache as its own
|
||||
// entry (per-placeIdentifier key) so we don't re-fetch next session.
|
||||
const writePromises = [];
|
||||
for (const entry of entries) {
|
||||
if (nameMap.has(entry.placeIdentifier)) continue;
|
||||
const cacheKey = `${entry.osmType}:${entry.osmId}`;
|
||||
const place = places.get(cacheKey);
|
||||
if (place?.title) {
|
||||
nameMap.set(entry.placeIdentifier, place.title);
|
||||
writePromises.push(
|
||||
this.localForage.set(
|
||||
NAME_CACHE_STORE,
|
||||
entry.placeIdentifier,
|
||||
place.title
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
await Promise.all(writePromises);
|
||||
return nameMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets per-session state. Called when the owning service stops.
|
||||
*/
|
||||
reset() {
|
||||
this._unresolvable.clear();
|
||||
this._lastBatchSignature = '';
|
||||
this._pendingBatchPromise = null;
|
||||
}
|
||||
}
|
||||
+15
-2
@@ -19,14 +19,17 @@ import { parsePhotoFromEvent } from './contributions';
|
||||
/**
|
||||
* A single activity timeline entry.
|
||||
*
|
||||
* `senderName`, `senderAvatar`, and `senderProfileLoading` are tracked so the
|
||||
* row re-renders when the sender's profile is resolved asynchronously.
|
||||
* `senderName`, `senderAvatar`, `senderProfileLoading`, `placeName`,
|
||||
* and `placeNameLoading` are tracked so the row re-renders when the sender's
|
||||
* profile or place name is resolved asynchronously.
|
||||
*/
|
||||
export class ActivityEntry {
|
||||
type = 'zap';
|
||||
photoEventId;
|
||||
photo;
|
||||
placeIdentifier;
|
||||
osmType;
|
||||
osmId;
|
||||
senderPubkey;
|
||||
amountSats;
|
||||
message;
|
||||
@@ -34,6 +37,8 @@ export class ActivityEntry {
|
||||
@tracked senderName = null;
|
||||
@tracked senderAvatar = null;
|
||||
@tracked senderProfileLoading = true;
|
||||
@tracked placeName = null;
|
||||
@tracked placeNameLoading = true;
|
||||
|
||||
constructor({
|
||||
photoEventId,
|
||||
@@ -125,5 +130,13 @@ export function enrichWithPhoto(entry, store, userPubkey) {
|
||||
|
||||
entry.photo = photo;
|
||||
entry.placeIdentifier = photo.placeIdentifier || null;
|
||||
|
||||
// Parse osmType and osmId from placeIdentifier (e.g. "osm:node:123" → "node", "123")
|
||||
if (entry.placeIdentifier) {
|
||||
const [, osmType, osmId] = entry.placeIdentifier.split(':');
|
||||
entry.osmType = osmType;
|
||||
entry.osmId = osmId;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -145,4 +145,85 @@ module('Integration | Component | activity-zap-item', function (hooks) {
|
||||
|
||||
assert.strictEqual(selected, this.item);
|
||||
});
|
||||
|
||||
test('it shows resolved place name when placeName is set', async function (assert) {
|
||||
this.item = new ActivityEntry({
|
||||
photoEventId: 'photo-1',
|
||||
photo: null,
|
||||
placeIdentifier: 'osm:node:12345',
|
||||
senderPubkey: 'b'.repeat(64),
|
||||
amountSats: 21,
|
||||
message: null,
|
||||
createdAt: 1000,
|
||||
});
|
||||
this.item.senderName = 'Alice';
|
||||
this.item.senderProfileLoading = false;
|
||||
this.item.placeName = 'Café Central';
|
||||
this.item.placeNameLoading = false;
|
||||
|
||||
await render(
|
||||
<template>
|
||||
<ActivityZapItem @item={{this.item}} @onSelect={{this.noop}} />
|
||||
</template>
|
||||
);
|
||||
|
||||
assert
|
||||
.dom('.zap-context-text')
|
||||
.includesText('Café Central', 'Resolved place name is displayed');
|
||||
assert
|
||||
.dom('.contribution-name-loading')
|
||||
.doesNotExist('No loading/fallback text shown');
|
||||
});
|
||||
|
||||
test('it shows "Loading…" when placeNameLoading is true', async function (assert) {
|
||||
this.item = new ActivityEntry({
|
||||
photoEventId: 'photo-1',
|
||||
photo: null,
|
||||
placeIdentifier: 'osm:node:12345',
|
||||
senderPubkey: 'b'.repeat(64),
|
||||
amountSats: 21,
|
||||
message: null,
|
||||
createdAt: 1000,
|
||||
});
|
||||
this.item.senderName = 'Alice';
|
||||
this.item.senderProfileLoading = false;
|
||||
this.item.placeName = null;
|
||||
this.item.placeNameLoading = true;
|
||||
|
||||
await render(
|
||||
<template>
|
||||
<ActivityZapItem @item={{this.item}} @onSelect={{this.noop}} />
|
||||
</template>
|
||||
);
|
||||
|
||||
assert
|
||||
.dom('.zap-context-text .contribution-name-loading')
|
||||
.hasText('Loading…', 'Loading state is displayed');
|
||||
});
|
||||
|
||||
test('it shows "Unnamed place" when not loading and no placeName', async function (assert) {
|
||||
this.item = new ActivityEntry({
|
||||
photoEventId: 'photo-1',
|
||||
photo: null,
|
||||
placeIdentifier: 'osm:node:12345',
|
||||
senderPubkey: 'b'.repeat(64),
|
||||
amountSats: 21,
|
||||
message: null,
|
||||
createdAt: 1000,
|
||||
});
|
||||
this.item.senderName = 'Alice';
|
||||
this.item.senderProfileLoading = false;
|
||||
this.item.placeName = null;
|
||||
this.item.placeNameLoading = false;
|
||||
|
||||
await render(
|
||||
<template>
|
||||
<ActivityZapItem @item={{this.item}} @onSelect={{this.noop}} />
|
||||
</template>
|
||||
);
|
||||
|
||||
assert
|
||||
.dom('.zap-context-text .contribution-name-loading')
|
||||
.hasText('Unnamed place', 'Fallback name is displayed');
|
||||
});
|
||||
});
|
||||
@@ -51,12 +51,6 @@ class MockOsmService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
const NAME_CACHE_STORE = 'contributions-name-cache';
|
||||
|
||||
function flushPromises() {
|
||||
return new Promise((resolve) => setTimeout(resolve, 50));
|
||||
}
|
||||
|
||||
module('Unit | Service | contributions', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
@@ -66,212 +60,57 @@ module('Unit | Service | contributions', function (hooks) {
|
||||
this.owner.register('service:osm', MockOsmService);
|
||||
});
|
||||
|
||||
test('_updateItems resolves names from bookmarks immediately', function (assert) {
|
||||
test('_updateItems groups events into entries and resolves bookmark names', function (assert) {
|
||||
const events = [makePhotoEvent({ id: 'e1', created_at: 1000 })];
|
||||
|
||||
const service = this.owner.lookup('service:contributions');
|
||||
service.storage.savedPlaces = [{ id: '123', title: 'Bookmarked Café' }];
|
||||
service.storage.findPlaceById = (id) =>
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
storage.savedPlaces = [{ id: '123', title: 'Bookmarked Café' }];
|
||||
storage.findPlaceById = (id) =>
|
||||
id === '123' ? { id: '123', title: 'Bookmarked Café' } : null;
|
||||
|
||||
service._updateItems(events);
|
||||
|
||||
assert.strictEqual(service.items.length, 1);
|
||||
assert.false(service.items[0].placeNameLoading, 'Not loading');
|
||||
assert.strictEqual(
|
||||
service.items[0].placeName,
|
||||
'Bookmarked Café',
|
||||
'Resolved from bookmark'
|
||||
);
|
||||
});
|
||||
|
||||
test('_updateItems resolves names from the persistent name cache', async function (assert) {
|
||||
const events = [
|
||||
makePhotoEvent({
|
||||
id: 'e1',
|
||||
created_at: 1000,
|
||||
placeIdentifier: 'osm:node:999',
|
||||
}),
|
||||
];
|
||||
|
||||
const service = this.owner.lookup('service:contributions');
|
||||
await service.localForage.set(
|
||||
NAME_CACHE_STORE,
|
||||
'osm:node:999',
|
||||
'Cached Park'
|
||||
);
|
||||
|
||||
service._updateItems(events);
|
||||
await flushPromises();
|
||||
|
||||
assert.strictEqual(service.items[0].placeName, 'Cached Park');
|
||||
assert.false(service.items[0].placeNameLoading);
|
||||
assert.strictEqual(service.items[0].placeName, 'Bookmarked Café');
|
||||
});
|
||||
|
||||
test('resolved names are persisted to the IndexedDB name cache', async function (assert) {
|
||||
const events = [
|
||||
makePhotoEvent({
|
||||
id: 'e1',
|
||||
created_at: 1000,
|
||||
placeIdentifier: 'osm:node:42',
|
||||
}),
|
||||
];
|
||||
|
||||
test('stop clears items and resets resolver state', async function (assert) {
|
||||
const service = this.owner.lookup('service:contributions');
|
||||
service.osm.fetchOsmObjectsBatch = async () => {
|
||||
const map = new Map();
|
||||
map.set('node:42', { title: 'Test Place' });
|
||||
return map;
|
||||
};
|
||||
|
||||
const events = [makePhotoEvent({ id: 'e1', created_at: 1000 })];
|
||||
service._updateItems(events);
|
||||
await flushPromises();
|
||||
|
||||
assert.strictEqual(service.items.length, 1);
|
||||
|
||||
service.stop();
|
||||
|
||||
assert.strictEqual(service.items.length, 0, 'items cleared');
|
||||
const resolver = this.owner.lookup('service:place-name-resolver');
|
||||
assert.strictEqual(
|
||||
await service.localForage.get(NAME_CACHE_STORE, 'osm:node:42'),
|
||||
'Test Place',
|
||||
'Name is persisted to the IndexedDB name cache'
|
||||
resolver._lastBatchSignature,
|
||||
'',
|
||||
'resolver state reset'
|
||||
);
|
||||
});
|
||||
|
||||
test('names stored in a previous session are resolved from the IndexedDB cache', async function (assert) {
|
||||
const events = [
|
||||
makePhotoEvent({
|
||||
id: 'e1',
|
||||
created_at: 1000,
|
||||
placeIdentifier: 'osm:node:77',
|
||||
}),
|
||||
];
|
||||
|
||||
test('multiple _updateItems calls re-group events correctly', function (assert) {
|
||||
const service = this.owner.lookup('service:contributions');
|
||||
await service.localForage.set(
|
||||
NAME_CACHE_STORE,
|
||||
'osm:node:77',
|
||||
'Persisted Place'
|
||||
);
|
||||
const events1 = [makePhotoEvent({ id: 'e1', created_at: 1000 })];
|
||||
const events2 = [makePhotoEvent({ id: 'e2', created_at: 2000 })];
|
||||
|
||||
service._updateItems(events);
|
||||
await flushPromises();
|
||||
|
||||
assert.strictEqual(service.items[0].placeName, 'Persisted Place');
|
||||
assert.false(service.items[0].placeNameLoading);
|
||||
});
|
||||
|
||||
test('batch fetch failure applies a fallback name so items do not stay loading', async function (assert) {
|
||||
const events = [
|
||||
makePhotoEvent({
|
||||
id: 'e1',
|
||||
created_at: 1000,
|
||||
placeIdentifier: 'osm:node:555',
|
||||
}),
|
||||
];
|
||||
|
||||
const service = this.owner.lookup('service:contributions');
|
||||
// OSM batch returns empty (simulating all-failed fetch)
|
||||
service.osm.fetchOsmObjectsBatch = async () => new Map();
|
||||
|
||||
service._updateItems(events);
|
||||
|
||||
// Wait for the background batch to complete
|
||||
await flushPromises();
|
||||
|
||||
assert.false(
|
||||
service.items[0].placeNameLoading,
|
||||
'Item is no longer loading after batch failure'
|
||||
);
|
||||
service._updateItems(events1);
|
||||
assert.strictEqual(
|
||||
service.items[0].placeName,
|
||||
'OSM node 555',
|
||||
'Fallback name is applied'
|
||||
service.items.length,
|
||||
1,
|
||||
'first update creates one entry'
|
||||
);
|
||||
});
|
||||
|
||||
test('fallback names are NOT persisted to the name cache (so they can be re-fetched next session)', async function (assert) {
|
||||
const events = [
|
||||
makePhotoEvent({
|
||||
id: 'e1',
|
||||
created_at: 1000,
|
||||
placeIdentifier: 'osm:node:666',
|
||||
}),
|
||||
];
|
||||
|
||||
const service = this.owner.lookup('service:contributions');
|
||||
service.osm.fetchOsmObjectsBatch = async () => new Map();
|
||||
|
||||
service._updateItems(events);
|
||||
await flushPromises();
|
||||
|
||||
// Fallback should be shown but NOT cached
|
||||
assert.strictEqual(service.items[0].placeName, 'OSM node 666');
|
||||
assert.notOk(
|
||||
await service.localForage.get(NAME_CACHE_STORE, 'osm:node:666'),
|
||||
'No fallback name persisted in the IndexedDB name cache'
|
||||
);
|
||||
assert.true(
|
||||
service._unresolvable.has('osm:node:666'),
|
||||
'Item is marked unresolvable for this session'
|
||||
);
|
||||
});
|
||||
|
||||
test('unresolvable items are not re-fetched within the same session', async function (assert) {
|
||||
const events = [
|
||||
makePhotoEvent({
|
||||
id: 'e1',
|
||||
created_at: 1000,
|
||||
placeIdentifier: 'osm:node:777',
|
||||
}),
|
||||
];
|
||||
|
||||
let fetchCount = 0;
|
||||
const service = this.owner.lookup('service:contributions');
|
||||
service.osm.fetchOsmObjectsBatch = async () => {
|
||||
fetchCount++;
|
||||
return new Map();
|
||||
};
|
||||
|
||||
// First update triggers a fetch
|
||||
service._updateItems(events);
|
||||
await flushPromises();
|
||||
|
||||
assert.strictEqual(fetchCount, 1, 'First update triggers a fetch');
|
||||
assert.strictEqual(service.items[0].placeName, 'OSM node 777');
|
||||
|
||||
// Second update should NOT trigger another fetch (already unresolvable)
|
||||
service._updateItems(events);
|
||||
await flushPromises();
|
||||
|
||||
assert.strictEqual(fetchCount, 1, 'Second update does not re-fetch');
|
||||
service._updateItems(events2);
|
||||
assert.strictEqual(
|
||||
service.items[0].placeName,
|
||||
'OSM node 777',
|
||||
'Fallback is shown immediately'
|
||||
);
|
||||
});
|
||||
|
||||
test('successful batch resolution stores names in the name cache', async function (assert) {
|
||||
const events = [
|
||||
makePhotoEvent({
|
||||
id: 'e1',
|
||||
created_at: 1000,
|
||||
placeIdentifier: 'osm:node:111',
|
||||
}),
|
||||
];
|
||||
|
||||
const service = this.owner.lookup('service:contributions');
|
||||
service.osm.fetchOsmObjectsBatch = async () => {
|
||||
const map = new Map();
|
||||
map.set('node:111', { title: 'Resolved Café' });
|
||||
return map;
|
||||
};
|
||||
|
||||
service._updateItems(events);
|
||||
await flushPromises();
|
||||
|
||||
assert.strictEqual(service.items[0].placeName, 'Resolved Café');
|
||||
assert.strictEqual(
|
||||
await service.localForage.get(NAME_CACHE_STORE, 'osm:node:111'),
|
||||
'Resolved Café',
|
||||
'Name is stored in the name cache'
|
||||
service.items.length,
|
||||
1,
|
||||
'second update replaces with new group'
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,210 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'marco/tests/helpers';
|
||||
import Service from '@ember/service';
|
||||
|
||||
function makeEntry(opts = {}) {
|
||||
const placeIdentifier = opts.placeIdentifier || 'osm:node:123';
|
||||
const [, osmType, osmId] = placeIdentifier.split(':');
|
||||
return {
|
||||
placeIdentifier,
|
||||
osmType: opts.osmType || osmType,
|
||||
osmId: opts.osmId || osmId,
|
||||
placeName: opts.placeName || null,
|
||||
placeNameLoading: opts.placeNameLoading ?? true,
|
||||
};
|
||||
}
|
||||
|
||||
class MockStorageService extends Service {
|
||||
savedPlaces = [];
|
||||
findPlaceById() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class MockOsmService extends Service {
|
||||
async getCachedOsmObject() {
|
||||
return null;
|
||||
}
|
||||
|
||||
async fetchOsmObjectsBatch() {
|
||||
return new Map();
|
||||
}
|
||||
}
|
||||
|
||||
function flushPromises() {
|
||||
return new Promise((resolve) => setTimeout(resolve, 50));
|
||||
}
|
||||
|
||||
module('Unit | Service | place-name-resolver', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
hooks.beforeEach(function () {
|
||||
this.owner.register('service:storage', MockStorageService);
|
||||
this.owner.register('service:osm', MockOsmService);
|
||||
});
|
||||
|
||||
test('resolveBookmark returns bookmark title when found', function (assert) {
|
||||
const resolver = this.owner.lookup('service:place-name-resolver');
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
storage.savedPlaces = [{ id: '123', title: 'Bookmarked Café' }];
|
||||
storage.findPlaceById = (id) =>
|
||||
id === '123' ? { id: '123', title: 'Bookmarked Café' } : null;
|
||||
|
||||
const name = resolver.resolveBookmark('123');
|
||||
|
||||
assert.strictEqual(name, 'Bookmarked Café');
|
||||
});
|
||||
|
||||
test('resolveBookmark returns null when no bookmark', function (assert) {
|
||||
const resolver = this.owner.lookup('service:place-name-resolver');
|
||||
|
||||
const name = resolver.resolveBookmark('999');
|
||||
|
||||
assert.notOk(name);
|
||||
});
|
||||
|
||||
test('resolveInBackground resolves names from the persistent name cache', async function (assert) {
|
||||
const resolver = this.owner.lookup('service:place-name-resolver');
|
||||
const localForage = this.owner.lookup('service:localForage');
|
||||
const entry = makeEntry({ placeIdentifier: 'osm:node:999' });
|
||||
await localForage.set('place-name-cache', 'osm:node:999', 'Cached Park');
|
||||
|
||||
await resolver.resolveInBackground([entry]);
|
||||
|
||||
assert.strictEqual(entry.placeName, 'Cached Park');
|
||||
assert.false(entry.placeNameLoading);
|
||||
});
|
||||
|
||||
test('resolved names are persisted to the IndexedDB name cache', async function (assert) {
|
||||
const resolver = this.owner.lookup('service:place-name-resolver');
|
||||
const localForage = this.owner.lookup('service:localForage');
|
||||
const entry = makeEntry({ placeIdentifier: 'osm:node:42' });
|
||||
resolver.osm.fetchOsmObjectsBatch = async () => {
|
||||
const map = new Map();
|
||||
map.set('node:42', { title: 'Test Place' });
|
||||
return map;
|
||||
};
|
||||
|
||||
await resolver.resolveInBackground([entry]);
|
||||
await flushPromises();
|
||||
|
||||
assert.strictEqual(
|
||||
await localForage.get('place-name-cache', 'osm:node:42'),
|
||||
'Test Place',
|
||||
'Name is persisted to the IndexedDB name cache'
|
||||
);
|
||||
});
|
||||
|
||||
test('names stored in a previous session are resolved from the IndexedDB cache', async function (assert) {
|
||||
const resolver = this.owner.lookup('service:place-name-resolver');
|
||||
const localForage = this.owner.lookup('service:localForage');
|
||||
const entry = makeEntry({ placeIdentifier: 'osm:node:77' });
|
||||
await localForage.set('place-name-cache', 'osm:node:77', 'Persisted Place');
|
||||
|
||||
await resolver.resolveInBackground([entry]);
|
||||
|
||||
assert.strictEqual(entry.placeName, 'Persisted Place');
|
||||
assert.false(entry.placeNameLoading);
|
||||
});
|
||||
|
||||
test('batch fetch failure applies a fallback name so items do not stay loading', async function (assert) {
|
||||
const resolver = this.owner.lookup('service:place-name-resolver');
|
||||
const entry = makeEntry({ placeIdentifier: 'osm:node:555' });
|
||||
resolver.osm.fetchOsmObjectsBatch = async () => new Map();
|
||||
|
||||
await resolver.resolveInBackground([entry]);
|
||||
await flushPromises();
|
||||
|
||||
assert.false(
|
||||
entry.placeNameLoading,
|
||||
'Item is no longer loading after batch failure'
|
||||
);
|
||||
assert.strictEqual(
|
||||
entry.placeName,
|
||||
'OSM node 555',
|
||||
'Fallback name is applied'
|
||||
);
|
||||
});
|
||||
|
||||
test('fallback names are NOT persisted to the name cache (so they can be re-fetched next session)', async function (assert) {
|
||||
const resolver = this.owner.lookup('service:place-name-resolver');
|
||||
const localForage = this.owner.lookup('service:localForage');
|
||||
const entry = makeEntry({ placeIdentifier: 'osm:node:666' });
|
||||
resolver.osm.fetchOsmObjectsBatch = async () => new Map();
|
||||
|
||||
await resolver.resolveInBackground([entry]);
|
||||
await flushPromises();
|
||||
|
||||
assert.strictEqual(entry.placeName, 'OSM node 666');
|
||||
assert.notOk(
|
||||
await localForage.get('place-name-cache', 'osm:node:666'),
|
||||
'No fallback name persisted in the IndexedDB name cache'
|
||||
);
|
||||
assert.true(
|
||||
resolver._unresolvable.has('osm:node:666'),
|
||||
'Item is marked unresolvable for this session'
|
||||
);
|
||||
});
|
||||
|
||||
test('unresolvable items are not re-fetched within the same session', async function (assert) {
|
||||
const resolver = this.owner.lookup('service:place-name-resolver');
|
||||
const entry = makeEntry({ placeIdentifier: 'osm:node:777' });
|
||||
let fetchCount = 0;
|
||||
resolver.osm.fetchOsmObjectsBatch = async () => {
|
||||
fetchCount++;
|
||||
return new Map();
|
||||
};
|
||||
|
||||
// First call triggers a fetch
|
||||
await resolver.resolveInBackground([entry]);
|
||||
await flushPromises();
|
||||
|
||||
assert.strictEqual(fetchCount, 1, 'First call triggers a fetch');
|
||||
assert.strictEqual(entry.placeName, 'OSM node 777');
|
||||
|
||||
// Second call should NOT trigger another fetch (already unresolvable)
|
||||
await resolver.resolveInBackground([entry]);
|
||||
await flushPromises();
|
||||
|
||||
assert.strictEqual(fetchCount, 1, 'Second call does not re-fetch');
|
||||
assert.strictEqual(
|
||||
entry.placeName,
|
||||
'OSM node 777',
|
||||
'Fallback is shown immediately'
|
||||
);
|
||||
});
|
||||
|
||||
test('successful batch resolution stores names in the name cache', async function (assert) {
|
||||
const resolver = this.owner.lookup('service:place-name-resolver');
|
||||
const localForage = this.owner.lookup('service:localForage');
|
||||
const entry = makeEntry({ placeIdentifier: 'osm:node:111' });
|
||||
resolver.osm.fetchOsmObjectsBatch = async () => {
|
||||
const map = new Map();
|
||||
map.set('node:111', { title: 'Resolved Café' });
|
||||
return map;
|
||||
};
|
||||
|
||||
await resolver.resolveInBackground([entry]);
|
||||
await flushPromises();
|
||||
|
||||
assert.strictEqual(entry.placeName, 'Resolved Café');
|
||||
assert.strictEqual(
|
||||
await localForage.get('place-name-cache', 'osm:node:111'),
|
||||
'Resolved Café',
|
||||
'Name is stored in the name cache'
|
||||
);
|
||||
});
|
||||
|
||||
test('reset clears per-session state', function (assert) {
|
||||
const resolver = this.owner.lookup('service:place-name-resolver');
|
||||
resolver._unresolvable.add('test-id');
|
||||
resolver._lastBatchSignature = 'sig';
|
||||
resolver._pendingBatchPromise = Promise.resolve();
|
||||
|
||||
resolver.reset();
|
||||
|
||||
assert.strictEqual(resolver._unresolvable.size, 0);
|
||||
assert.strictEqual(resolver._lastBatchSignature, '');
|
||||
assert.strictEqual(resolver._pendingBatchPromise, null);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user