@@ -26,7 +26,9 @@ use lnurl::lnurl::LnUrl;
26
26
use nostr:: key:: SecretKey ;
27
27
use nostr:: nips:: nip04:: { decrypt, encrypt} ;
28
28
use nostr:: nips:: nip47:: * ;
29
- use nostr:: { Event , EventBuilder , EventId , Filter , JsonUtil , Keys , Kind , Metadata , Tag , Timestamp } ;
29
+ use nostr:: {
30
+ Event , EventBuilder , EventId , Filter , JsonUtil , Keys , Kind , Metadata , Tag , TagKind , Timestamp ,
31
+ } ;
30
32
use nostr_sdk:: { Client , NostrSigner , RelayPoolNotification } ;
31
33
use serde:: { Deserialize , Serialize } ;
32
34
use std:: collections:: { HashMap , HashSet } ;
@@ -1195,6 +1197,60 @@ impl<S: MutinyStorage> NostrManager<S> {
1195
1197
Ok ( event_id)
1196
1198
}
1197
1199
1200
+ /// Creates a recommendation event for a federation
1201
+ pub async fn recommend_federation (
1202
+ & self ,
1203
+ invite_code : & InviteCode ,
1204
+ ) -> Result < EventId , MutinyError > {
1205
+ let kind = Kind :: from ( 18173 ) ;
1206
+ // first pull down our current recommendation event, we need to do this
1207
+ // to make sure we don't overwrite any other recommendations
1208
+ let filter = Filter :: new ( ) . kind ( kind) . author ( self . public_key ) . limit ( 1 ) ;
1209
+ // fetch events
1210
+ let events = self
1211
+ . client
1212
+ . get_events_of ( vec ! [ filter] , Some ( Duration :: from_secs ( 5 ) ) )
1213
+ . await ?;
1214
+
1215
+ let current = events
1216
+ . into_iter ( )
1217
+ . find_map ( |e| if e. kind == kind { Some ( e) } else { None } ) ;
1218
+
1219
+ // tag the federation invite code
1220
+ let invite_code_tag = Tag :: Generic (
1221
+ TagKind :: U ,
1222
+ vec ! [ invite_code. to_string( ) , "fedimint" . to_string( ) ] ,
1223
+ ) ;
1224
+
1225
+ // todo tag the federation announcement event, to do so we need to have the pubkey of the federation
1226
+
1227
+ // create the new recommendation event by either creating a new one or modifying the current one
1228
+ let builder = match current {
1229
+ Some ( e) => {
1230
+ let mut tags = e. tags . clone ( ) ;
1231
+
1232
+ let mut modified = false ;
1233
+
1234
+ if !tags. iter ( ) . any ( |t| t == & invite_code_tag) {
1235
+ tags. push ( invite_code_tag) ;
1236
+ modified = true ;
1237
+ }
1238
+
1239
+ // if we already recommended this federation, return the event id
1240
+ // no need to send another recommendation
1241
+ if !modified {
1242
+ return Ok ( e. id ) ;
1243
+ }
1244
+
1245
+ EventBuilder :: new ( kind, e. content . clone ( ) , tags)
1246
+ }
1247
+ None => EventBuilder :: new ( kind, "" , [ invite_code_tag] ) ,
1248
+ } ;
1249
+
1250
+ // send the event
1251
+ Ok ( self . client . send_event_builder ( builder) . await ?)
1252
+ }
1253
+
1198
1254
/// Queries our relays for federation announcements
1199
1255
pub async fn discover_federations ( & self ) -> Result < Vec < NostrDiscoveredFedimint > , MutinyError > {
1200
1256
// get contacts by npub
0 commit comments