nip-25: use proper reacted to e and p tags
ci/woodpecker/pr/woodpecker Pipeline was successful Details
ci/woodpecker/push/woodpecker Pipeline was successful Details

the actual event that is being reacted must be the last event tag,
was wrongly using the first one and displayed wrong star if tags
had more than 1 event tag.

upvoting now also includes e and p tags from the event that is
upvoted.

see also https://github.com/nostr-protocol/nips/blob/master/25.md
pull/58/head
OFF0 2 years ago
parent dc2def3361
commit 7edf1151a6
Signed by: offbyn
GPG Key ID: 94A2F643C51F37FA

@ -221,7 +221,7 @@ document.body.addEventListener('click', (e) => {
return; return;
} }
if (button && button.name === 'star') { if (button && button.name === 'star') {
upvote(id, relay) upvote(id, pubkey);
return; return;
} }
if (button && button.name === 'back') { if (button && button.name === 'back') {
@ -272,7 +272,7 @@ function handleTextNote(evt, relay) {
} }
} }
const replyList = []; const replyList = []; // could use textNoteList with isReply field
const reactionMap = {}; const reactionMap = {};
const getReactionList = (id) => { const getReactionList = (id) => {
@ -280,23 +280,13 @@ const getReactionList = (id) => {
}; };
function handleReaction(evt, relay) { function handleReaction(evt, relay) {
if (!evt.content.length) { // last id is the note that is being reacted to https://github.com/nostr-protocol/nips/blob/master/25.md
// console.log('reaction with no content', evt) const lastEventTag = evt.tags.filter(hasEventTag).at(-1);
if (!lastEventTag || !evt.content.length) {
// ignore reactions with no content
return; return;
} }
const eventTags = evt.tags.filter(hasEventTag); const [, eventId] = lastEventTag;
let replies = eventTags.filter(([tag, eventId, relayUrl, marker]) => marker === 'reply');
if (replies.length === 0) {
// deprecated https://github.com/nostr-protocol/nips/blob/master/10.md#positional-e-tags-deprecated
replies = eventTags.filter((tags) => tags[3] === undefined);
}
if (replies.length !== 1) {
console.log('call me', evt);
return;
}
const [tag, eventId/*, relayUrl, marker*/] = replies[0];
if (reactionMap[eventId]) { if (reactionMap[eventId]) {
if (reactionMap[eventId].find(reaction => reaction.id === evt.id)) { if (reactionMap[eventId].find(reaction => reaction.id === evt.id)) {
// already received this reaction from a different relay // already received this reaction from a different relay
@ -758,13 +748,20 @@ function hideNewMessage(hide) {
newMessageDiv.hidden = hide; newMessageDiv.hidden = hide;
} }
async function upvote(eventId, relay) { async function upvote(eventId, eventPubkey) {
const privatekey = localStorage.getItem('private_key'); const privatekey = localStorage.getItem('private_key');
const note = replyList.find(r => r.id === eventId) || textNoteList.find(n => n.id === (eventId));
const tags = [
...note.tags
.filter(tag => ['e', 'p'].includes(tag[0])) // take e and p tags from event
.map(([a, b]) => [a, b]), // drop optional (nip-10) relay and marker fields
['e', eventId], ['p', eventPubkey], // last e and p tag is the id and pubkey of the note being reacted to (nip-25)
];
const newReaction = { const newReaction = {
kind: 7, kind: 7,
pubkey, // TODO: lib could check that this is the pubkey of the key to sign with pubkey, // TODO: lib could check that this is the pubkey of the key to sign with
content: '+', content: '+',
tags: [['e', eventId, relay, 'reply']], tags,
created_at: Math.floor(Date.now() * 0.001), created_at: Math.floor(Date.now() * 0.001),
}; };
const sig = await signEvent(newReaction, privatekey).catch(console.error); const sig = await signEvent(newReaction, privatekey).catch(console.error);

Loading…
Cancel
Save