kind.mx raw
1 // Package kind provides event kind types and a kind database.
2 package kind
3
4 import (
5 "smesh.lol/pkg/nostr/ints"
6 "smesh.lol/pkg/lol/chk"
7 )
8
9 type Integer interface {
10 ~int | ~int8 | ~int16 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint64
11 }
12
13 type K struct {
14 K uint16
15 }
16
17 func New[V Integer](k V) *K { return &K{uint16(k)} }
18
19 func (k *K) ToInt() int {
20 if k == nil {
21 return 0
22 }
23 return int(k.K)
24 }
25
26 func (k *K) ToU16() uint16 {
27 if k == nil {
28 return 0
29 }
30 return k.K
31 }
32
33 func (k *K) ToI32() int32 {
34 if k == nil {
35 return 0
36 }
37 return int32(k.K)
38 }
39
40 func (k *K) ToU64() uint64 {
41 if k == nil {
42 return 0
43 }
44 return uint64(k.K)
45 }
46
47 func (k *K) Name() []byte { return []byte(GetString(k.K)) }
48
49 func (k *K) Equal(k2 uint16) bool {
50 if k == nil {
51 return false
52 }
53 return k.K == k2
54 }
55
56 var Privileged = []*K{
57 EncryptedDirectMessage, GiftWrap, GiftWrapWithKind4, JWTBinding,
58 ApplicationSpecificData, Seal, DirectMessage, FileMessage,
59 MLSKeyPackage, KeyPackageRelaysList,
60 ChannelCreation, ChannelMetadata, ChannelMessage, ChannelHideMessage, ChannelMuteUser,
61 }
62
63 func IsChannelKind(k uint16) bool {
64 return k >= ChannelCreation.K && k <= ChannelMuteUser.K
65 }
66
67 func IsDiscoverableChannelKind(k uint16) bool {
68 return k == ChannelCreation.K || k == ChannelMetadata.K
69 }
70
71 func IsPrivileged(k uint16) bool {
72 for i := range Privileged {
73 if k == Privileged[i].K {
74 return true
75 }
76 }
77 return false
78 }
79
80 func (k *K) Marshal(dst []byte) []byte {
81 return ints.New(k.ToU64()).Marshal(dst)
82 }
83
84 func (k *K) Unmarshal(b []byte) (r []byte, err error) {
85 n := ints.New(0)
86 if r, err = n.Unmarshal(b); chk.T(err) {
87 return
88 }
89 k.K = n.Uint16()
90 return
91 }
92
93 // GetString returns a human-readable name for a kind number.
94 // No mutex needed — single cooperative thread.
95 func GetString(t uint16) string {
96 return Map[t]
97 }
98
99 func IsEphemeral(k uint16) bool {
100 return k >= EphemeralStart.K && k < EphemeralEnd.K
101 }
102
103 func IsReplaceable(k uint16) bool {
104 return k == ProfileMetadata.K || k == FollowList.K ||
105 (k >= ReplaceableStart.K && k < ReplaceableEnd.K)
106 }
107
108 func IsParameterizedReplaceable(k uint16) bool {
109 return k >= ParameterizedReplaceableStart.K &&
110 k < ParameterizedReplaceableEnd.K
111 }
112
113 var Directory = []*K{
114 ProfileMetadata, FollowList, EventDeletion, Reporting,
115 RelayListMetadata, MuteList, DMRelaysList,
116 }
117
118 func IsDirectoryEvent(k uint16) bool {
119 for i := range Directory {
120 if k == Directory[i].K {
121 return true
122 }
123 }
124 return false
125 }
126
127 var (
128 ProfileMetadata = &K{0}
129 SetMetadata = &K{0}
130 TextNote = &K{1}
131 RecommendRelay = &K{2}
132 FollowList = &K{3}
133 Follows = &K{3}
134 EncryptedDirectMessage = &K{4}
135 EventDeletion = &K{5}
136 Deletion = &K{5}
137 Repost = &K{6}
138 Reaction = &K{7}
139 BadgeAward = &K{8}
140 ChatMessage = &K{9}
141 GroupChatThreadedReply = &K{10}
142 Thread = &K{11}
143 GroupThreadReply = &K{12}
144 Seal = &K{13}
145 DirectMessage = &K{14}
146 FileMessage = &K{15}
147 GenericRepost = &K{16}
148 ReactionToWebsite = &K{17}
149 Picture = &K{20}
150 VideoEventHorizontal = &K{34235}
151 VideoEventVertical = &K{34236}
152 MLSKeyPackage = &K{443}
153 MLSWelcome = &K{444}
154 MLSGroupEvent = &K{445}
155 ChannelCreation = &K{40}
156 ChannelMetadata = &K{41}
157 ChannelMessage = &K{42}
158 ChannelHideMessage = &K{43}
159 ChannelMuteUser = &K{44}
160 Bid = &K{1021}
161 BidConfirmation = &K{1022}
162 OpenTimestamps = &K{1040}
163 GiftWrap = &K{1059}
164 GiftWrapWithKind4 = &K{1060}
165 FileMetadata = &K{1063}
166 LiveChatMessage = &K{1311}
167 BitcoinBlock = &K{1517}
168 LiveStream = &K{1808}
169 ProblemTracker = &K{1971}
170 MemoryHole = &K{1984}
171 Reporting = &K{1984}
172 Label = &K{1985}
173 CommunityPostApproval = &K{4550}
174 JobRequestStart = &K{5000}
175 JobRequestEnd = &K{5999}
176 JobResultStart = &K{6000}
177 JobResultEnd = &K{6999}
178 JobFeedback = &K{7000}
179 ZapGoal = &K{9041}
180 ZapRequest = &K{9734}
181 Zap = &K{9735}
182 Highlights = &K{9802}
183 ReplaceableStart = &K{10000}
184 MuteList = &K{10000}
185 BlockList = &K{10000}
186 PinList = &K{10001}
187 RelayListMetadata = &K{10002}
188 BookmarkList = &K{10003}
189 CommunitiesList = &K{10004}
190 PublicChatsList = &K{10005}
191 BlockedRelaysList = &K{10006}
192 SearchRelaysList = &K{10007}
193 RelayGroupConfig = &K{10008}
194 InterestsList = &K{10015}
195 UserEmojiList = &K{10030}
196 DMRelaysList = &K{10050}
197 KeyPackageRelaysList = &K{10051}
198 FileStorageServerList = &K{10096}
199 JWTBinding = &K{13004}
200 NWCWalletServiceInfo = &K{13194}
201 WalletServiceInfo = &K{13194}
202 ReplaceableEnd = &K{19999}
203 EphemeralStart = &K{20000}
204 LightningPubRPC = &K{21000}
205 ClientAuthentication = &K{22242}
206 NWCWalletRequest = &K{23194}
207 WalletRequest = &K{23194}
208 NWCWalletResponse = &K{23195}
209 WalletResponse = &K{23195}
210 NWCNotification = &K{23196}
211 WalletNotificationNip4 = &K{23196}
212 WalletNotification = &K{23197}
213 NostrConnect = &K{24133}
214 HTTPAuth = &K{27235}
215 EphemeralEnd = &K{29999}
216 ParameterizedReplaceableStart = &K{30000}
217 CategorizedPeopleList = &K{30000}
218 FollowSets = &K{30000}
219 CategorizedBookmarksList = &K{30001}
220 GenericLists = &K{30001}
221 RelaySets = &K{30002}
222 BookmarkSets = &K{30003}
223 CurationSets = &K{30004}
224 ProfileBadges = &K{30008}
225 BadgeDefinition = &K{30009}
226 InterestSets = &K{30015}
227 StallDefinition = &K{30017}
228 ProductDefinition = &K{30018}
229 MarketplaceUIUX = &K{30019}
230 ProductSoldAsAuction = &K{30020}
231 Article = &K{30023}
232 LongFormContent = &K{30023}
233 DraftLongFormContent = &K{30024}
234 EmojiSets = &K{30030}
235 ApplicationSpecificData = &K{30078}
236 LiveEvent = &K{30311}
237 UserStatuses = &K{30315}
238 ClassifiedListing = &K{30402}
239 DraftClassifiedListing = &K{30403}
240 DateBasedCalendarEvent = &K{31922}
241 TimeBasedCalendarEvent = &K{31923}
242 Calendar = &K{31924}
243 CalendarEventRSVP = &K{31925}
244 HandlerRecommendation = &K{31989}
245 HandlerInformation = &K{31990}
246 WaveLakeTrack = &K{32123}
247 CommunityDefinition = &K{34550}
248 ACLEvent = &K{39998}
249 PolicyConfig = &K{12345}
250 ParameterizedReplaceableEnd = &K{40000}
251 )
252
253 // Map is populated from the embedded kinds.json during init().
254 var Map = map[uint16]string{}
255