damus.io

damus.io website
git clone git://jb55.com/damus.io
Log | Files | Refs | README | LICENSE

PurpleUtils.ts (2172B)


      1 // MARK: - Types
      2 
      3 import { NostrEvent, Relay } from "nostr-tools"
      4 
      5 interface AccountInfo {
      6 pubkey: string,
      7 created_at: number, // Unix timestamp in seconds
      8 expiry: number | null, // Unix timestamp in seconds
      9 subscriber_number: number,
     10 active: boolean,
     11 testflight_url: string | null,
     12 }
     13 
     14 interface Profile {
     15 pubkey: string
     16 name: string
     17 picture: string
     18 about: string
     19 }
     20 
     21 // MARK: - Helper functions
     22 
     23 const getPurpleAccountInfo = async (pubkey: string): Promise<AccountInfo | null> => {
     24 const response = await fetch(process.env.NEXT_PUBLIC_PURPLE_API_BASE_URL + "/accounts/" + pubkey, {
     25     method: 'GET',
     26     headers: {
     27     'Content-Type': 'application/json'
     28     },
     29 })
     30 if (response.status === 404) {
     31     return null
     32 }
     33 const data: AccountInfo = await response.json()
     34 return data
     35 }
     36 
     37 const getProfile = async (pubkey: string): Promise<Profile | null> => {
     38 const profile_event: NostrEvent | null = await getProfileEvent(pubkey)
     39 if (!profile_event) {
     40     return null
     41 }
     42 try {
     43     const profile_data = JSON.parse(profile_event.content)
     44     const profile = {
     45     pubkey: profile_event.pubkey,
     46     name: profile_data.name,
     47     picture: profile_data.picture,
     48     about: profile_data.about,
     49     }
     50     return profile
     51 }
     52 catch (e) {
     53     return null
     54 }
     55 }
     56 
     57 const getProfileEvent = async (pubkey: string): Promise<NostrEvent | null> => {
     58 const relay = await Relay.connect(process.env.NEXT_PUBLIC_NOSTR_RELAY || 'wss://relay.damus.io')
     59 
     60 const events: Array<NostrEvent> = []
     61 return new Promise((resolve, reject) => {
     62 
     63     // let's query for an event that exist 
     64     const sub = relay.subscribe([
     65     {
     66         authors: [pubkey],
     67         kinds: [0],
     68     },
     69     ], {
     70     onevent(event: NostrEvent) {
     71         events.push(event)
     72     },
     73     oneose() {
     74         // Get the most recent event, based on `created_at` field
     75         if (events.length === 0) {
     76         // No events found
     77         resolve(null)
     78         }
     79         else {
     80         const most_recent_event = events.sort((a, b) => a.created_at - b.created_at)[0]
     81         resolve(most_recent_event)
     82         }
     83         relay.close()
     84     }
     85     })
     86 });
     87 }
     88 
     89 export { getPurpleAccountInfo, getProfile, getProfileEvent }
     90 export type { AccountInfo, Profile }
     91