feat: support customizing mentioned users
This commit is contained in:
parent
c1d469b1f4
commit
3c23a7f9f8
5 changed files with 200 additions and 76 deletions
|
|
@ -7,7 +7,7 @@ import {
|
|||
extractCommentMentions,
|
||||
extractHashtags,
|
||||
extractImagesFromContent,
|
||||
extractMentions,
|
||||
extractRelatedEventIds,
|
||||
getEventCoordinate,
|
||||
isProtectedEvent,
|
||||
isReplaceable
|
||||
|
|
@ -54,21 +54,29 @@ export function createRepostDraftEvent(event: Event): TDraftEvent {
|
|||
export async function createShortTextNoteDraftEvent(
|
||||
content: string,
|
||||
pictureInfos: { url: string; tags: string[][] }[],
|
||||
mentions: string[],
|
||||
options: {
|
||||
parentEvent?: Event
|
||||
addClientTag?: boolean
|
||||
protectedEvent?: boolean
|
||||
} = {}
|
||||
): Promise<TDraftEvent> {
|
||||
const { pubkeys, otherRelatedEventIds, quoteEventIds, rootEventId, parentEventId } =
|
||||
await extractMentions(content, options.parentEvent)
|
||||
const { otherRelatedEventIds, quoteEventIds, rootEventId, parentEventId } =
|
||||
await extractRelatedEventIds(content, options.parentEvent)
|
||||
const hashtags = extractHashtags(content)
|
||||
|
||||
const tags = pubkeys
|
||||
.map((pubkey) => ['p', pubkey])
|
||||
.concat(quoteEventIds.map((eventId) => ['q', eventId, client.getEventHint(eventId)]))
|
||||
.concat(hashtags.map((hashtag) => ['t', hashtag]))
|
||||
const tags = hashtags.map((hashtag) => ['t', hashtag])
|
||||
|
||||
// imeta tags
|
||||
const { images } = extractImagesFromContent(content)
|
||||
if (images && images.length) {
|
||||
tags.push(...generateImetaTags(images, pictureInfos))
|
||||
}
|
||||
|
||||
// q tags
|
||||
tags.push(...quoteEventIds.map((eventId) => ['q', eventId, client.getEventHint(eventId)]))
|
||||
|
||||
// e tags
|
||||
if (rootEventId) {
|
||||
tags.push(['e', rootEventId, client.getEventHint(rootEventId), 'root'])
|
||||
}
|
||||
|
|
@ -79,10 +87,8 @@ export async function createShortTextNoteDraftEvent(
|
|||
tags.push(['e', parentEventId, client.getEventHint(parentEventId), 'reply'])
|
||||
}
|
||||
|
||||
const { images } = extractImagesFromContent(content)
|
||||
if (images && images.length) {
|
||||
tags.push(...generateImetaTags(images, pictureInfos))
|
||||
}
|
||||
// p tags
|
||||
tags.push(...mentions.map((pubkey) => ['p', pubkey]))
|
||||
|
||||
if (options.addClientTag) {
|
||||
tags.push(['client', 'jumble'])
|
||||
|
|
@ -117,12 +123,13 @@ export function createRelaySetDraftEvent(relaySet: TRelaySet): TDraftEvent {
|
|||
export async function createPictureNoteDraftEvent(
|
||||
content: string,
|
||||
pictureInfos: { url: string; tags: string[][] }[],
|
||||
mentions: string[],
|
||||
options: {
|
||||
addClientTag?: boolean
|
||||
protectedEvent?: boolean
|
||||
} = {}
|
||||
): Promise<TDraftEvent> {
|
||||
const { pubkeys, quoteEventIds } = await extractMentions(content)
|
||||
const { quoteEventIds } = await extractRelatedEventIds(content)
|
||||
const hashtags = extractHashtags(content)
|
||||
if (!pictureInfos.length) {
|
||||
throw new Error('No images found in content')
|
||||
|
|
@ -130,9 +137,9 @@ export async function createPictureNoteDraftEvent(
|
|||
|
||||
const tags = pictureInfos
|
||||
.map((info) => ['imeta', ...info.tags.map(([n, v]) => `${n} ${v}`)])
|
||||
.concat(pubkeys.map((pubkey) => ['p', pubkey]))
|
||||
.concat(quoteEventIds.map((eventId) => ['q', eventId, client.getEventHint(eventId)]))
|
||||
.concat(hashtags.map((hashtag) => ['t', hashtag]))
|
||||
.concat(quoteEventIds.map((eventId) => ['q', eventId, client.getEventHint(eventId)]))
|
||||
.concat(mentions.map((pubkey) => ['p', pubkey]))
|
||||
|
||||
if (options.addClientTag) {
|
||||
tags.push(['client', 'jumble'])
|
||||
|
|
@ -154,13 +161,13 @@ export async function createCommentDraftEvent(
|
|||
content: string,
|
||||
parentEvent: Event,
|
||||
pictureInfos: { url: string; tags: string[][] }[],
|
||||
mentions: string[],
|
||||
options: {
|
||||
addClientTag?: boolean
|
||||
protectedEvent?: boolean
|
||||
} = {}
|
||||
): Promise<TDraftEvent> {
|
||||
const {
|
||||
pubkeys,
|
||||
quoteEventIds,
|
||||
rootEventId,
|
||||
rootEventKind,
|
||||
|
|
@ -171,23 +178,29 @@ export async function createCommentDraftEvent(
|
|||
} = await extractCommentMentions(content, parentEvent)
|
||||
const hashtags = extractHashtags(content)
|
||||
|
||||
const tags = [
|
||||
['E', rootEventId, client.getEventHint(rootEventId), rootEventPubkey],
|
||||
['K', rootEventKind.toString()],
|
||||
['P', rootEventPubkey],
|
||||
['e', parentEventId, client.getEventHint(parentEventId), parentEventPubkey],
|
||||
['k', parentEventKind.toString()],
|
||||
['p', parentEventPubkey]
|
||||
]
|
||||
.concat(pubkeys.map((pubkey) => ['p', pubkey]))
|
||||
const tags = hashtags
|
||||
.map((hashtag) => ['t', hashtag])
|
||||
.concat(quoteEventIds.map((eventId) => ['q', eventId, client.getEventHint(eventId)]))
|
||||
.concat(hashtags.map((hashtag) => ['t', hashtag]))
|
||||
|
||||
const { images } = extractImagesFromContent(content)
|
||||
if (images && images.length) {
|
||||
tags.push(...generateImetaTags(images, pictureInfos))
|
||||
}
|
||||
|
||||
tags.push(
|
||||
...mentions.filter((pubkey) => pubkey !== parentEventPubkey).map((pubkey) => ['p', pubkey])
|
||||
)
|
||||
tags.push(
|
||||
...[
|
||||
['E', rootEventId, client.getEventHint(rootEventId), rootEventPubkey],
|
||||
['K', rootEventKind.toString()],
|
||||
['P', rootEventPubkey],
|
||||
['e', parentEventId, client.getEventHint(parentEventId), parentEventPubkey],
|
||||
['k', parentEventKind.toString()],
|
||||
['p', parentEventPubkey]
|
||||
]
|
||||
)
|
||||
|
||||
if (options.addClientTag) {
|
||||
tags.push(['client', 'jumble'])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -171,11 +171,9 @@ export function getProfileFromProfileEvent(event: Event) {
|
|||
}
|
||||
|
||||
export async function extractMentions(content: string, parentEvent?: Event) {
|
||||
let parentEventPubkey: string | undefined
|
||||
const pubkeySet = new Set<string>()
|
||||
const relatedEventIdSet = new Set<string>()
|
||||
const quoteEventIdSet = new Set<string>()
|
||||
let rootEventId: string | undefined
|
||||
let parentEventId: string | undefined
|
||||
const relatedPubkeySet = new Set<string>()
|
||||
const matches = content.match(
|
||||
/nostr:(npub1[a-z0-9]{58}|nprofile1[a-z0-9]+|note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g
|
||||
)
|
||||
|
|
@ -192,7 +190,6 @@ export async function extractMentions(content: string, parentEvent?: Event) {
|
|||
const event = await client.fetchEvent(id)
|
||||
if (event) {
|
||||
pubkeySet.add(event.pubkey)
|
||||
quoteEventIdSet.add(event.id)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
|
|
@ -200,13 +197,52 @@ export async function extractMentions(content: string, parentEvent?: Event) {
|
|||
}
|
||||
}
|
||||
|
||||
if (parentEvent) {
|
||||
parentEventPubkey = parentEvent.pubkey
|
||||
parentEvent.tags.forEach(([tagName, tagValue]) => {
|
||||
if (['p', 'P'].includes(tagName) && !!tagValue) {
|
||||
relatedPubkeySet.add(tagValue)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (parentEventPubkey) {
|
||||
pubkeySet.delete(parentEventPubkey)
|
||||
relatedPubkeySet.delete(parentEventPubkey)
|
||||
}
|
||||
|
||||
return {
|
||||
pubkeys: Array.from(pubkeySet),
|
||||
relatedPubkeys: Array.from(relatedPubkeySet).filter((p) => !pubkeySet.has(p)),
|
||||
parentEventPubkey
|
||||
}
|
||||
}
|
||||
|
||||
export async function extractRelatedEventIds(content: string, parentEvent?: Event) {
|
||||
const relatedEventIdSet = new Set<string>()
|
||||
const quoteEventIdSet = new Set<string>()
|
||||
let rootEventId: string | undefined
|
||||
let parentEventId: string | undefined
|
||||
const matches = content.match(/nostr:(note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g)
|
||||
|
||||
for (const m of matches || []) {
|
||||
try {
|
||||
const id = m.split(':')[1]
|
||||
const { type, data } = nip19.decode(id)
|
||||
if (type === 'nevent') {
|
||||
quoteEventIdSet.add(data.id)
|
||||
} else if (type === 'note') {
|
||||
quoteEventIdSet.add(data)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
if (parentEvent) {
|
||||
relatedEventIdSet.add(parentEvent.id)
|
||||
pubkeySet.add(parentEvent.pubkey)
|
||||
parentEvent.tags.forEach((tag) => {
|
||||
if (tagNameEquals('p')(tag)) {
|
||||
pubkeySet.add(tag[1])
|
||||
} else if (isRootETag(tag)) {
|
||||
if (isRootETag(tag)) {
|
||||
rootEventId = tag[1]
|
||||
} else if (tagNameEquals('e')(tag)) {
|
||||
relatedEventIdSet.add(tag[1])
|
||||
|
|
@ -223,7 +259,6 @@ export async function extractMentions(content: string, parentEvent?: Event) {
|
|||
if (parentEventId) relatedEventIdSet.delete(parentEventId)
|
||||
|
||||
return {
|
||||
pubkeys: Array.from(pubkeySet),
|
||||
otherRelatedEventIds: Array.from(relatedEventIdSet),
|
||||
quoteEventIds: Array.from(quoteEventIdSet),
|
||||
rootEventId,
|
||||
|
|
@ -232,7 +267,6 @@ export async function extractMentions(content: string, parentEvent?: Event) {
|
|||
}
|
||||
|
||||
export async function extractCommentMentions(content: string, parentEvent: Event) {
|
||||
const pubkeySet = new Set<string>()
|
||||
const quoteEventIdSet = new Set<string>()
|
||||
const rootEventId = parentEvent.tags.find(tagNameEquals('E'))?.[1] ?? parentEvent.id
|
||||
const rootEventKind = parentEvent.tags.find(tagNameEquals('K'))?.[1] ?? parentEvent.kind
|
||||
|
|
@ -241,34 +275,23 @@ export async function extractCommentMentions(content: string, parentEvent: Event
|
|||
const parentEventKind = parentEvent.kind
|
||||
const parentEventPubkey = parentEvent.pubkey
|
||||
|
||||
const matches = content.match(
|
||||
/nostr:(npub1[a-z0-9]{58}|nprofile1[a-z0-9]+|note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g
|
||||
)
|
||||
const matches = content.match(/nostr:(note1[a-z0-9]{58}|nevent1[a-z0-9]+)/g)
|
||||
|
||||
for (const m of matches || []) {
|
||||
try {
|
||||
const id = m.split(':')[1]
|
||||
const { type, data } = nip19.decode(id)
|
||||
if (type === 'nprofile') {
|
||||
pubkeySet.add(data.pubkey)
|
||||
} else if (type === 'npub') {
|
||||
pubkeySet.add(data)
|
||||
} else if (['nevent', 'note', 'naddr'].includes(type)) {
|
||||
const event = await client.fetchEvent(id)
|
||||
if (event) {
|
||||
pubkeySet.add(event.pubkey)
|
||||
quoteEventIdSet.add(event.id)
|
||||
}
|
||||
if (type === 'nevent') {
|
||||
quoteEventIdSet.add(data.id)
|
||||
} else if (type === 'note') {
|
||||
quoteEventIdSet.add(data)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
pubkeySet.add(parentEvent.pubkey)
|
||||
|
||||
return {
|
||||
pubkeys: Array.from(pubkeySet),
|
||||
quoteEventIds: Array.from(quoteEventIdSet),
|
||||
rootEventId,
|
||||
rootEventKind,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue