feat: favorite relays (#250)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { COMMENT_EVENT_KIND, PICTURE_EVENT_KIND } from '@/constants'
|
||||
import { ExtendedKind } from '@/constants'
|
||||
import client from '@/services/client.service'
|
||||
import { TDraftEvent, TMailboxRelay, TRelaySet } from '@/types'
|
||||
import dayjs from 'dayjs'
|
||||
@@ -150,7 +150,7 @@ export async function createPictureNoteDraftEvent(
|
||||
}
|
||||
|
||||
return {
|
||||
kind: PICTURE_EVENT_KIND,
|
||||
kind: ExtendedKind.PICTURE,
|
||||
content,
|
||||
tags,
|
||||
created_at: dayjs().unix()
|
||||
@@ -210,7 +210,7 @@ export async function createCommentDraftEvent(
|
||||
}
|
||||
|
||||
return {
|
||||
kind: COMMENT_EVENT_KIND,
|
||||
kind: ExtendedKind.COMMENT,
|
||||
content,
|
||||
tags,
|
||||
created_at: dayjs().unix()
|
||||
@@ -255,6 +255,25 @@ export function createProfileDraftEvent(content: string, tags: string[][] = []):
|
||||
}
|
||||
}
|
||||
|
||||
export function createFavoriteRelaysDraftEvent(
|
||||
favoriteRelays: string[],
|
||||
relaySetEvents: Event[]
|
||||
): TDraftEvent {
|
||||
const tags: string[][] = []
|
||||
favoriteRelays.forEach((url) => {
|
||||
tags.push(['relay', url])
|
||||
})
|
||||
relaySetEvents.forEach((event) => {
|
||||
tags.push(['a', getEventCoordinate(event)])
|
||||
})
|
||||
return {
|
||||
kind: ExtendedKind.FAVORITE_RELAYS,
|
||||
content: '',
|
||||
tags,
|
||||
created_at: dayjs().unix()
|
||||
}
|
||||
}
|
||||
|
||||
function generateImetaTags(imageUrls: string[], pictureInfos: { url: string; tags: string[][] }[]) {
|
||||
return imageUrls.map((imageUrl) => {
|
||||
const pictureInfo = pictureInfos.find((info) => info.url === imageUrl)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { BIG_RELAY_URLS, COMMENT_EVENT_KIND, PICTURE_EVENT_KIND } from '@/constants'
|
||||
import { BIG_RELAY_URLS, ExtendedKind } from '@/constants'
|
||||
import client from '@/services/client.service'
|
||||
import { TImageInfo, TRelayList } from '@/types'
|
||||
import { TImageInfo, TRelayList, TRelaySet } from '@/types'
|
||||
import { LRUCache } from 'lru-cache'
|
||||
import { Event, kinds, nip19 } from 'nostr-tools'
|
||||
import { getAmountFromInvoice, getLightningAddressFromProfile } from './lightning'
|
||||
@@ -47,11 +47,11 @@ export function isReplyNoteEvent(event: Event) {
|
||||
}
|
||||
|
||||
export function isCommentEvent(event: Event) {
|
||||
return event.kind === COMMENT_EVENT_KIND
|
||||
return event.kind === ExtendedKind.COMMENT
|
||||
}
|
||||
|
||||
export function isPictureEvent(event: Event) {
|
||||
return event.kind === PICTURE_EVENT_KIND
|
||||
return event.kind === ExtendedKind.PICTURE
|
||||
}
|
||||
|
||||
export function isProtectedEvent(event: Event) {
|
||||
@@ -59,7 +59,7 @@ export function isProtectedEvent(event: Event) {
|
||||
}
|
||||
|
||||
export function isSupportedKind(kind: number) {
|
||||
return [kinds.ShortTextNote, PICTURE_EVENT_KIND].includes(kind)
|
||||
return [kinds.ShortTextNote, ExtendedKind.PICTURE].includes(kind)
|
||||
}
|
||||
|
||||
export function getParentEventTag(event?: Event) {
|
||||
@@ -195,6 +195,22 @@ export function getProfileFromProfileEvent(event: Event) {
|
||||
}
|
||||
}
|
||||
|
||||
export function getRelaySetFromRelaySetEvent(event: Event): TRelaySet {
|
||||
const id = getReplaceableEventIdentifier(event)
|
||||
const relayUrls = event.tags
|
||||
.filter(tagNameEquals('relay'))
|
||||
.map((tag) => tag[1])
|
||||
.filter((url) => url && isWebsocketUrl(url))
|
||||
.map((url) => normalizeUrl(url))
|
||||
|
||||
let name = event.tags.find(tagNameEquals('title'))?.[1]
|
||||
if (!name) {
|
||||
name = id
|
||||
}
|
||||
|
||||
return { id, name, relayUrls }
|
||||
}
|
||||
|
||||
export async function extractMentions(content: string, parentEvent?: Event) {
|
||||
const parentEventPubkey = parentEvent ? parentEvent.pubkey : undefined
|
||||
const pubkeys: string[] = []
|
||||
@@ -485,3 +501,7 @@ export function extractEmbeddedEventIds(event: Event) {
|
||||
export function getLatestEvent(events: Event[]) {
|
||||
return events.sort((a, b) => b.created_at - a.created_at)[0]
|
||||
}
|
||||
|
||||
export function getReplaceableEventIdentifier(event: Event) {
|
||||
return event.tags.find(tagNameEquals('d'))?.[1] ?? ''
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ export const toOthersRelaySettings = (pubkey: string) => {
|
||||
const npub = nip19.npubEncode(pubkey)
|
||||
return `/users/${npub}/relays`
|
||||
}
|
||||
export const toRelaySettings = (tag?: 'mailbox' | 'relay-sets') => {
|
||||
export const toRelaySettings = (tag?: 'mailbox' | 'favorite-relays') => {
|
||||
return '/relay-settings' + (tag ? '#' + tag : '')
|
||||
}
|
||||
export const toSettings = () => '/settings'
|
||||
|
||||
Reference in New Issue
Block a user