Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pubsub: use relay hints #382

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions src/js/Helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ const emojiRegex =
/([\u{1f300}-\u{1f5ff}\u{1f900}-\u{1f9ff}\u{1f600}-\u{1f64f}\u{1f680}-\u{1f6ff}\u{2600}-\u{26ff}\u{2700}-\u{27bf}\u{1f1e6}-\u{1f1ff}\u{1f191}-\u{1f251}\u{1f004}\u{1f0cf}\u{1f170}-\u{1f171}\u{1f17e}-\u{1f17f}\u{1f18e}\u{3030}\u{2b50}\u{2b55}\u{2934}-\u{2935}\u{2b05}-\u{2b07}\u{2b1b}-\u{2b1c}\u{3297}\u{3299}\u{303d}\u{00a9}\u{00ae}\u{2122}\u{23f3}\u{24c2}\u{23e9}-\u{23ef}\u{25b6}\u{23f8}-\u{23fa}]+)/gu;
const pubKeyRegex =
/(?:^|\s|nostr:|(?:https?:\/\/[\w./]+)|iris\.to\/|snort\.social\/p\/|damus\.io\/)+((?:@)?npub[a-zA-Z0-9]{59,60})(?![\w/])/gi;
const nprofileRegex =
/(?:^|\s|nostr:|(?:https?:\/\/[\w./]+)|iris\.to\/|snort\.social\/p\/|damus\.io\/)+((?:@)?nprofile[a-zA-Z0-9]{59,})(?![\w/])/gi;
const noteRegex =
/(?:^|\s|nostr:|(?:https?:\/\/[\w./]+)|iris\.to\/|snort\.social\/e\/|damus\.io\/)+((?:@)?note[a-zA-Z0-9]{59,60})(?![\w/])/gi;
// No max length of 60.
const neventRegex =
/(?:^|\s|nostr:|(?:https?:\/\/[\w./]+)|iris\.to\/|snort\.social\/e\/|damus\.io\/)+((?:@)?nevent[a-zA-Z0-9]{59,})(?![\w/])/gi;
const nip19Regex = /\bnostr:(n(?:event|profile)1\w+)\b/g;

const hashtagRegex = /(#\w+)/g;
Expand Down Expand Up @@ -877,4 +882,6 @@ export default {
pubKeyRegex,
noteRegex,
hashtagRegex,
nprofileRegex,
neventRegex,
};
2 changes: 1 addition & 1 deletion src/js/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Main extends Component<Props, ReactState> {

// if id begins with "note", it's a post. otherwise it's a profile.
const NoteOrProfile = (params: { id?: string; path: string }) => {
if (params.id.startsWith('note')) {
if (params.id.match(/^(note|nevent)/)) {
return <Note id={params.id} />;
}
return <Profile id={params.id} tab="posts" path={params.path} />;
Expand Down
5 changes: 4 additions & 1 deletion src/js/components/MessageForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export default class MessageForm extends Component {
if (taggedItems) {
event.tags = event.tags || [];
for (const tag of taggedItems) {
const hexTag = Key.toNostrHexAddress(tag.match(/npub[a-zA-Z0-9]{59,60}/)[0]);
const link = tag.match(/(?:npub|nprofile)[a-zA-Z0-9]{59,}/)[0];
const hexTag = Key.toNostrHexAddress(link);
if (!hexTag) {
continue;
}
Expand All @@ -55,7 +56,9 @@ export default class MessageForm extends Component {
}

handleTagged(Helpers.pubKeyRegex, 'p');
handleTagged(Helpers.nprofileRegex, 'p');
handleTagged(Helpers.noteRegex, 'e');
handleTagged(Helpers.neventRegex, 'e');

const hashtags = [...msg.text.matchAll(Helpers.hashtagRegex)].map((m) => m[0].slice(1));
if (hashtags.length) {
Expand Down
32 changes: 32 additions & 0 deletions src/js/nostr/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,26 @@ const Events = {
if (!id) {
return;
}
const hints = Events._getHintedRelays(event);
Object.entries(hints).forEach(([id, relays]) => {
this.eventsMetaDb.upsert(id, { relays });
});
this.eventsMetaDb.upsert(id, { relays: new Set([url]) });
},
_getHintedRelays(event: Event) {
const tagsWanted = ['e', 'p'];
const ids = event.tags.reduce<{ [id: string]: Set<string> }>((acc, [tag, id, relay]) => {
if (!tagsWanted.includes(tag) || !relay?.startsWith('wss://')) {
return acc;
}
if (!acc[id]) {
acc[id] = new Set();
}
acc[id].add(relay.trim());
return acc;
}, {});
return ids;
},
handleNextFutureEvent() {
if (this.futureEventIds.size === 0) {
return;
Expand Down Expand Up @@ -645,6 +663,7 @@ const Events = {

console.log('publishing event', event);
this.handle(event as Event);
this.handleEventMetadata({ event: event as Event, url: Relays.enabledRelays()[0] });

// also publish at most 10 events referred to in tags
const referredEvents = event.tags
Expand All @@ -665,6 +684,7 @@ const Events = {
if (!event.tags) {
event.tags = [];
}
Events.addRelayHints(event as Event);
event.content = event.content || '';
event.created_at = event.created_at || Math.floor(Date.now() / 1000);
event.pubkey = Key.getPubKey();
Expand Down Expand Up @@ -762,6 +782,18 @@ const Events = {
unsub2();
};
},
addRelayHints(event: { tags: string[][] }) {
event.tags.forEach((tag) => {
// Only for 'e' and 'p', and if already exists then out.
if (!['e', 'p'].includes(tag[0]) || tag[2]) {
return;
}
const hints = this.eventsMetaDb.getRelays(tag[1]);
if (hints.length > 0) {
tag[2] = hints[0];
}
});
},
};

export default Events;
8 changes: 7 additions & 1 deletion src/js/nostr/PubSub.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { throttle } from 'lodash';
import { throttle, uniq } from 'lodash';
import { RelayPool } from 'nostr-relaypool';

import { Event, Filter, matchFilter } from '../lib/nostr-tools';
Expand Down Expand Up @@ -204,6 +204,8 @@ const PubSub = {
relays = Array.from(Relays.searchRelays.keys());
} else if (mergeSubscriptions || filter.authors?.length !== 1) {
relays = Relays.enabledRelays();
const hints = this._getHintedRelays(filter);
relays = uniq([...relays, ...hints]);
}
if (dev.indexed03 !== false && filter.kinds?.every((k) => k === 0 || k === 3)) {
relays = ['wss://us.rbr.bio', 'wss://eu.rbr.bio'];
Expand Down Expand Up @@ -234,6 +236,10 @@ const PubSub = {
},
);
},
_getHintedRelays(filter: Filter) {
const idsWanted = [...(filter.ids || []), ...(filter.authors || [])];
return idsWanted.flatMap((id) => Events.eventsMetaDb.getRelays(id));
},
};

export default PubSub;
Expand Down