Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2025 B310 Digital GmbH
Copyright 2025-2026 B310 Digital GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ services:
depends_on:
- amb-relay

# Crawl peertube and fill your local amb relay
# peertube-to-amb:
# build:
# context: https://github.com/edufeed-org/nostr-activitypub-amb-bridge
# dockerfile: Dockerfile
# # volumes:
# # - ../nostr-activitypub-amb-bridge:/home/appuser/app
# tty: true
# stdin_open: true

imgproxy:
image: ghcr.io/imgproxy/imgproxy:latest
# network_mode: host does not work on Windows/macOS, use port mapping instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,33 @@ function sanitizeKeywords(keywords: string): string {
.trim();
}

function deduplicateEvents(events: readonly Event[]): Event[] {
return Array.from(
/**
* Extracts the "d" tag value (resource URL) from a Nostr event,
* falling back to the Nostr event ID when no "d" tag exists.
*/
const getResourceKey = (event: Event): string =>
event.tags.find((tag) => tag[0] === 'd' && tag.length >= 2)?.[1] ?? event.id;

/**
* Deduplicates events by resource identity (the "d" tag / resource URL).
*
* The same resource can appear with different Nostr event IDs when published
* by multiple relays or republished. For each unique resource URL, the newest
* event (highest `created_at`) is kept.
*/
const deduplicateEvents = (events: readonly Event[]): Event[] =>
Array.from(
events
.reduce(
(seen, event) => (seen.has(event.id) ? seen : seen.set(event.id, event)),
new Map<string, Event>(),
)
.reduce((seen, event) => {
const key = getResourceKey(event);
const existing = seen.get(key);
if (!existing || event.created_at > existing.created_at) {
seen.set(key, event);
}
return seen;
}, new Map<string, Event>())
.values(),
);
}

function collectResults(
settled: readonly PromiseSettledResult<Event[]>[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ describe('NostrAmbRelayAdapter multi-relay', () => {
expect(result.items).toHaveLength(2);
});

it('should deduplicate events with the same id from multiple relays', async () => {
it('should deduplicate events with the same event id from multiple relays', async () => {
const sharedEvent = makeEvent('shared-event');

(Relay.connect as jest.Mock)
Expand All @@ -348,6 +348,52 @@ describe('NostrAmbRelayAdapter multi-relay', () => {
expect(result.items).toHaveLength(1);
});

it('should deduplicate events with different event ids but same resource URL', async () => {
const resourceUrl = 'https://example.com/same-resource';
const event1 = {
...makeEvent('event-id-1'),
created_at: 1000,
tags: [['d', resourceUrl]],
};
const event2 = {
...makeEvent('event-id-2'),
created_at: 2000,
tags: [['d', resourceUrl]],
};

(Relay.connect as jest.Mock)
.mockResolvedValueOnce(makeMockRelay([event1]))
.mockResolvedValueOnce(makeMockRelay([event2]));

const adapter = createAdapter([RELAY_URL, RELAY_URL_2]);
const result = await adapter.search(baseQuery);

expect(result.total).toBe(1);
});

it('should keep the newest event when deduplicating by resource URL', async () => {
const resourceUrl = 'https://example.com/same-resource';
const olderEvent = {
...makeEvent('old-event'),
created_at: 1000,
tags: [['d', resourceUrl]],
};
const newerEvent = {
...makeEvent('new-event'),
created_at: 2000,
tags: [['d', resourceUrl]],
};

(Relay.connect as jest.Mock)
.mockResolvedValueOnce(makeMockRelay([olderEvent]))
.mockResolvedValueOnce(makeMockRelay([newerEvent]));

const adapter = createAdapter([RELAY_URL, RELAY_URL_2]);
const result = await adapter.search(baseQuery);

expect(result.items[0].amb.id).toBe(resourceUrl);
});

it('should return results from successful relay when one relay fails', async () => {
const event = makeEvent('event-1');

Expand Down
2 changes: 1 addition & 1 deletion packages/oer-finder-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@edufeed-org/oer-finder-plugin",
"version": "0.2.4",
"version": "0.2.5",
"description": "Web Components plugin for OER Proxy",
"author": "B310 Digital GmbH",
"license": "MIT",
Expand Down