-
Notifications
You must be signed in to change notification settings - Fork 74
/
process.rs
874 lines (797 loc) · 32.8 KB
/
process.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
use crate::comms::ToOverlordMessage;
use crate::error::Error;
use crate::filter::EventFilterAction;
use crate::globals::GLOBALS;
use crate::people::{PersonList, PersonListMetadata};
use crate::person_relay::PersonRelay;
use crate::relationship::{RelationshipByAddr, RelationshipById};
use async_recursion::async_recursion;
use heed::RwTxn;
use nostr_types::{
Event, EventAddr, EventKind, EventReference, Id, Metadata, NostrBech32, PublicKey, RelayList,
RelayUrl, RelayUsage, SimpleRelayList, Tag, Unixtime,
};
use std::sync::atomic::Ordering;
/// This is mainly used internally to gossip-lib, but you can use it to stuff events
/// into gossip from other sources. This processes a new event, saving the results into
/// the database and also populating the GLOBALS maps.
#[async_recursion]
pub async fn process_new_event(
event: &Event,
seen_on: Option<RelayUrl>,
subscription: Option<String>,
verify: bool,
process_even_if_duplicate: bool,
) -> Result<(), Error> {
let now = Unixtime::now()?;
// Bump count
GLOBALS.events_processed.fetch_add(1, Ordering::SeqCst);
// Detect if duplicate. We still need to process some things even if a duplicate
let duplicate = GLOBALS.storage.has_event(event.id)?;
// Verify the event,
// Don't verify if it is a duplicate:
// NOTE: relays could send forged events with valid IDs of other events, but if
// they do that in an event that is a duplicate of one we already have, this
// duplicate will only affect seen-on information, it will not be saved.
if !duplicate && verify {
let mut maxtime = now;
maxtime.0 += GLOBALS.storage.read_setting_future_allowance_secs() as i64;
if let Err(e) = event.verify(Some(maxtime)) {
tracing::error!("{}: VERIFY ERROR: {}", e, serde_json::to_string(&event)?);
return Ok(());
}
}
if let Some(url) = &seen_on {
// Save seen-on-relay information
GLOBALS
.storage
.add_event_seen_on_relay(event.id, url, now, None)?;
// Create the person if missing in the database
GLOBALS
.storage
.write_person_if_missing(&event.pubkey, None)?;
// Update person-relay information (seen them on this relay)
let mut pr = match GLOBALS.storage.read_person_relay(event.pubkey, url)? {
Some(pr) => pr,
None => PersonRelay::new(event.pubkey, url.clone()),
};
pr.last_fetched = Some(now.0 as u64);
GLOBALS.storage.write_person_relay(&pr, None)?;
}
// Spam filter (displayable and author is not followed)
if (event.kind.is_feed_displayable() || event.kind == EventKind::GiftWrap)
&& !GLOBALS
.people
.is_person_in_list(&event.pubkey, PersonList::Followed)
{
let filter_result = {
if event.kind == EventKind::GiftWrap {
if let Ok(rumor) = GLOBALS.identity.unwrap_giftwrap(event) {
let author = GLOBALS.storage.read_person(&rumor.pubkey)?;
Some(crate::filter::filter_rumor(rumor, author, event.id))
} else {
None
}
} else {
let author = GLOBALS.storage.read_person(&event.pubkey)?;
Some(crate::filter::filter_event(event.clone(), author))
}
};
match filter_result {
None => {}
Some(EventFilterAction::Allow) => {}
Some(EventFilterAction::Deny) => {
tracing::info!(
"SPAM FILTER: Filtered out event {}",
event.id.as_hex_string()
);
return Ok(());
}
Some(EventFilterAction::MuteAuthor) => {
let public = true;
GLOBALS.people.mute(&event.pubkey, true, public)?;
return Ok(());
}
}
}
// Invalidate the note itself (due to seen_on probably changing)
GLOBALS.ui_notes_to_invalidate.write().push(event.id);
// Determine if we already had this event
if duplicate && !process_even_if_duplicate {
tracing::trace!(
"{}: Old Event: {} {:?} @{}",
seen_on.as_ref().map(|r| r.as_str()).unwrap_or("_"),
subscription.as_ref().unwrap_or(&"_".to_string()),
event.kind,
event.created_at
);
return Ok(()); // No more processing needed for existing event.
}
// Ignore if the event is already deleted (by id)
for (_id, relbyid) in GLOBALS.storage.find_relationships_by_id(event.id)? {
if let RelationshipById::Deletion { by, reason: _ } = relbyid {
if event.delete_author_allowed(by) {
tracing::trace!(
"{}: Deleted Event: {} {:?} @{}",
seen_on.as_ref().map(|r| r.as_str()).unwrap_or("_"),
subscription.as_ref().unwrap_or(&"_".to_string()),
event.kind,
event.created_at
);
return Ok(());
}
}
}
// Ignore if the event is already deleted (by address)
if let Some(parameter) = event.parameter() {
let ea = EventAddr {
d: parameter.to_owned(),
relays: vec![],
kind: event.kind,
author: event.pubkey,
};
for (_id, relbyaddr) in GLOBALS.storage.find_relationships_by_addr(&ea)? {
if let RelationshipByAddr::Deletion { by, reason: _ } = relbyaddr {
if by == event.pubkey {
tracing::trace!(
"{}: Deleted Event: {} {:?} @{}",
seen_on.as_ref().map(|r| r.as_str()).unwrap_or("_"),
subscription.as_ref().unwrap_or(&"_".to_string()),
event.kind,
event.created_at
);
return Ok(());
}
}
}
}
// Save event
// Bail if the event is an already-replaced replaceable event
if event.kind.is_replaceable() {
if !GLOBALS.storage.replace_event(event, None)? {
tracing::trace!(
"{}: Old Event: {} {:?} @{}",
seen_on.as_ref().map(|r| r.as_str()).unwrap_or("_"),
subscription.as_ref().unwrap_or(&"_".to_string()),
event.kind,
event.created_at
);
return Ok(()); // This did not replace anything.
}
} else {
// This will ignore if it is already there
GLOBALS.storage.write_event(event, None)?;
}
// Log
tracing::debug!(
"{}: New Event: {} {:?} @{}",
seen_on.as_ref().map(|r| r.as_str()).unwrap_or("_"),
subscription.as_ref().unwrap_or(&"_".to_string()),
event.kind,
event.created_at
);
// If we were searching for this event, add it to the search results
let is_a_search_result: bool = GLOBALS.events_being_searched_for.read().contains(&event.id);
if is_a_search_result {
GLOBALS
.events_being_searched_for
.write()
.retain(|id| *id != event.id);
GLOBALS.note_search_results.write().push(event.clone());
}
// FIXME do same for event addr
// If it is a GiftWrap, from here on out operate on the Rumor with the giftwrap's id
let mut event: &Event = event; // take ownership of this reference
let mut rumor_event: Event;
if event.kind == EventKind::GiftWrap {
let rumor = GLOBALS.identity.unwrap_giftwrap(event)?;
rumor_event = rumor.into_event_with_bad_signature();
rumor_event.id = event.id; // Lie so it's handled with the giftwrap's id
event = &rumor_event;
}
if seen_on.is_some() {
for tag in event.tags.iter() {
if let Ok((_, Some(uurl), _optmarker)) = tag.parse_event() {
if let Ok(url) = RelayUrl::try_from_unchecked_url(&uurl) {
GLOBALS.storage.write_relay_if_missing(&url, None)?;
}
}
if let Ok((pubkey, maybeurl, _)) = tag.parse_pubkey() {
// Add person if missing
GLOBALS.people.create_all_if_missing(&[pubkey])?;
if let Some(uncheckedurl) = maybeurl {
if let Ok(url) = RelayUrl::try_from_unchecked_url(&uncheckedurl) {
GLOBALS.storage.write_relay_if_missing(&url, None)?;
// upsert person_relay.last_suggested_bytag
let mut pr = match GLOBALS.storage.read_person_relay(pubkey, &url)? {
Some(pr) => pr,
None => PersonRelay::new(pubkey, url.clone()),
};
pr.last_suggested_bytag = Some(now.0 as u64);
GLOBALS.storage.write_person_relay(&pr, None)?;
}
}
}
}
}
// Save event relationships (whether from a relay or not)
let invalid_ids = process_relationships_of_event(event, None)?;
// Invalidate UI events indicated by those relationships
GLOBALS.ui_notes_to_invalidate.write().extend(&invalid_ids);
// Let seeker know about this event id (in case it was sought)
GLOBALS.seeker.found_or_cancel(event.id);
// If metadata, update person
if event.kind == EventKind::Metadata {
let metadata: Metadata = serde_json::from_str(&event.content)?;
GLOBALS
.people
.update_metadata(&event.pubkey, metadata, event.created_at)
.await?;
}
if event.kind == EventKind::ContactList {
if let Some(pubkey) = GLOBALS.identity.public_key() {
if event.pubkey == pubkey {
// Updates stamps and counts, does NOT change membership
let (_personlist, _metadata) =
update_or_allocate_person_list_from_event(event, pubkey)?;
} else {
process_somebody_elses_contact_list(event).await?;
}
} else {
process_somebody_elses_contact_list(event).await?;
}
} else if event.kind == EventKind::MuteList || event.kind == EventKind::FollowSets {
// Only our own
if let Some(pubkey) = GLOBALS.identity.public_key() {
if event.pubkey == pubkey {
// Updates stamps and counts, does NOT change membership
let (_personlist, _metadata) =
update_or_allocate_person_list_from_event(event, pubkey)?;
}
}
} else if event.kind == EventKind::RelayList {
GLOBALS.storage.process_relay_list(event)?;
// Let the seeker know we now have relays for this author, in case the seeker
// wants to update it's state
// (we might not, but by this point we have tried)
GLOBALS.seeker.found_author_relays(event.pubkey);
// the following also refreshes scores before it picks relays
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::RefreshScoresAndPickRelays);
} else if event.kind == EventKind::Repost {
// If it has a json encoded inner event
if let Ok(inner_event) = serde_json::from_str::<Event>(&event.content) {
// process the inner event
process_new_event(&inner_event, None, None, verify, false).await?;
// Seek additional info for this event by id and author
GLOBALS
.seeker
.seek_id_and_author(inner_event.id, inner_event.pubkey)?;
} else {
// If the content is a repost, seek the event it reposts
for eref in event.mentions().iter() {
match eref {
EventReference::Id(id, optrelay, _marker) => {
if let Some(rurl) = optrelay {
GLOBALS
.seeker
.seek_id_and_relays(*id, vec![rurl.to_owned()]);
} else {
// Even if the event tags the author, we have no way to coorelate
// the nevent with that tag.
GLOBALS.seeker.seek_id(*id);
}
}
EventReference::Addr(ea) => {
if !ea.relays.is_empty() {
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::FetchEventAddr(ea.clone()));
}
}
}
}
}
} else if event.kind == EventKind::NostrConnect {
crate::nip46::handle_command(event, seen_on.clone())?
}
if event.kind.is_feed_displayable() {
// Process the content for references to things we might want
for bech32 in NostrBech32::find_all_in_string(&event.content) {
match bech32 {
NostrBech32::Id(id) => {
if GLOBALS.storage.read_event(id)?.is_none() {
if let Some(relay_url) = seen_on.as_ref() {
let _ = GLOBALS.to_overlord.send(ToOverlordMessage::FetchEvent(
id,
vec![relay_url.to_owned()],
));
}
}
}
NostrBech32::EventPointer(ep) => {
if GLOBALS.storage.read_event(ep.id)?.is_none() {
let relay_urls: Vec<RelayUrl> = ep
.relays
.iter()
.filter_map(|unchecked| {
RelayUrl::try_from_unchecked_url(unchecked).ok()
})
.collect();
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::FetchEvent(ep.id, relay_urls));
}
}
NostrBech32::EventAddr(mut ea) => {
if let Ok(None) = GLOBALS
.storage
.get_replaceable_event(ea.kind, ea.author, &ea.d)
{
// Add the seen_on relay
if let Some(seen_on_url) = seen_on.as_ref() {
let seen_on_unchecked_url = seen_on_url.to_unchecked_url();
if !ea.relays.contains(&seen_on_unchecked_url) {
ea.relays.push(seen_on_unchecked_url);
}
}
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::FetchEventAddr(ea));
}
}
NostrBech32::Profile(prof) => {
// Record existence of such a person
GLOBALS.people.create_if_missing(prof.pubkey);
// Make sure we have their relays
for relay in prof.relays {
if let Ok(rurl) = RelayUrl::try_from_unchecked_url(&relay) {
if let Some(_pr) =
GLOBALS.storage.read_person_relay(prof.pubkey, &rurl)?
{
// FIXME: we need a new field in PersonRelay for this case.
// If the event was signed by the profile person, we should trust it.
// If it wasn't, we can instead bump last_suggested_bytag.
} else {
let mut pr = PersonRelay::new(prof.pubkey, rurl);
pr.read = true;
pr.write = true;
GLOBALS.storage.write_person_relay(&pr, None)?;
}
}
}
}
NostrBech32::Pubkey(pubkey) => {
// Record existence of such a person
GLOBALS.people.create_if_missing(pubkey);
}
NostrBech32::Relay(relay) => {
if let Ok(rurl) = RelayUrl::try_from_unchecked_url(&relay) {
// make sure we have the relay
GLOBALS.storage.write_relay_if_missing(&rurl, None)?;
}
}
}
// TBD: If the content contains an nprofile, make sure the pubkey is associated
// with those relays
}
}
// TBD (have to parse runes language for this)
//if event.kind == EventKind::RelayList {
// process_somebody_elses_relay_list(event.pubkey.clone(), &event.contents).await?;
//}
// FIXME: Handle EventKind::RecommendedRelay
Ok(())
}
async fn process_somebody_elses_contact_list(event: &Event) -> Result<(), Error> {
// We don't keep their contacts or show to the user yet.
// We only process the contents for (non-standard) relay list information.
// Try to parse the contents as a SimpleRelayList (ignore if it is not)
if let Ok(srl) = serde_json::from_str::<SimpleRelayList>(&event.content) {
// Update that we received the relay list (and optionally bump forward the date
// if this relay list happens to be newer)
let newer = GLOBALS
.people
.update_relay_list_stamps(event.pubkey, event.created_at.0)
.await?;
if !newer {
return Ok(());
}
let mut relay_list: RelayList = Default::default();
for (url, simple_relay_usage) in srl.0.iter() {
if let Ok(relay_url) = RelayUrl::try_from_unchecked_url(url) {
if simple_relay_usage.read && simple_relay_usage.write {
relay_list.0.insert(relay_url, RelayUsage::Both);
} else if simple_relay_usage.read {
relay_list.0.insert(relay_url, RelayUsage::Inbox);
} else if simple_relay_usage.write {
relay_list.0.insert(relay_url, RelayUsage::Outbox);
}
}
}
GLOBALS
.storage
.set_relay_list(event.pubkey, relay_list, None)?;
// the following also refreshes scores before it picks relays
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::RefreshScoresAndPickRelays);
}
Ok(())
}
/// Process relationships of an event.
/// This returns IDs that should be UI invalidated (must be redrawn)
pub(crate) fn process_relationships_of_event<'a>(
event: &Event,
rw_txn: Option<&mut RwTxn<'a>>,
) -> Result<Vec<Id>, Error> {
let mut invalidate: Vec<Id> = Vec::new();
let mut f = |txn: &mut RwTxn<'a>| -> Result<(), Error> {
// replies to
match event.replies_to() {
Some(EventReference::Id(id, _, _)) => {
GLOBALS.storage.write_relationship_by_id(
id,
event.id,
RelationshipById::Reply,
Some(txn),
)?;
}
Some(EventReference::Addr(ea)) => {
GLOBALS.storage.write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::Reply,
Some(txn),
)?;
}
None => (),
}
// timestamps
if event.kind == EventKind::Timestamp {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.storage.write_relationship_by_id(
id,
event.id,
RelationshipById::Timestamp,
Some(txn),
)?;
}
}
}
// deletes
if let Some((vec, reason)) = event.deletes() {
for er in vec.iter() {
match er {
EventReference::Id(id, _, _) => {
// If we have the event,
// Actually delete at this point in some cases
if let Some(deleted_event) = GLOBALS.storage.read_event(*id)? {
if !deleted_event.delete_author_allowed(event.pubkey) {
// No further processing if not a valid delete
continue;
}
invalidate.push(deleted_event.id);
if !deleted_event.kind.is_feed_displayable() {
// Otherwise actually delete (PITA to do otherwise)
GLOBALS.storage.delete_event(deleted_event.id, Some(txn))?;
}
}
// Store the delete (we either don't have the target to verify,
// or we just verified above. In the former case, it is okay because
// we verify on usage)
GLOBALS.storage.write_relationship_by_id(
*id,
event.id,
RelationshipById::Deletion {
by: event.pubkey,
reason: reason.clone(),
},
Some(txn),
)?;
}
EventReference::Addr(ea) => {
// If we have the event,
// Actually delete at this point in some cases
if let Some(deleted_event) = GLOBALS
.storage
.get_replaceable_event(ea.kind, ea.author, &ea.d)?
{
if !deleted_event.delete_author_allowed(event.pubkey) {
// No further processing if not a valid delete
continue;
}
invalidate.push(deleted_event.id);
if !deleted_event.kind.is_feed_displayable() {
// Otherwise actually delete (PITA to do otherwise)
GLOBALS.storage.delete_event(deleted_event.id, Some(txn))?;
}
}
// Store the delete (we either don't have the target to verify,
// or we just verified above. In the former case, it is okay because
// we verify on usage)
GLOBALS.storage.write_relationship_by_addr(
ea.clone(),
event.id,
RelationshipByAddr::Deletion {
by: event.pubkey,
reason: reason.clone(),
},
Some(txn),
)?;
}
}
}
}
// reacts to
if let Some((reacted_to_id, reaction, _maybe_url)) = event.reacts_to() {
// NOTE: reactions may precede the event they react to. So we cannot validate here.
GLOBALS.storage.write_relationship_by_id(
reacted_to_id, // event reacted to
event.id, // the reaction event id
RelationshipById::Reaction {
by: event.pubkey,
reaction,
},
Some(txn),
)?;
invalidate.push(reacted_to_id);
}
// labels
if event.kind == EventKind::Label {
// Get the label from the "l" tag
let mut label = "";
let mut namespace = "";
for t in &event.tags {
if t.tagname() == "l" {
if t.value() != "" {
label = t.value();
if t.get_index(2) != "" {
namespace = t.get_index(2);
}
}
}
}
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.storage.write_relationship_by_id(
id,
event.id,
RelationshipById::Labels {
label: label.to_owned(),
namespace: namespace.to_owned(),
},
Some(txn),
)?;
}
}
}
// ListMutesThread
if event.kind == EventKind::MuteList {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.storage.write_relationship_by_id(
id,
event.id,
RelationshipById::ListMutesThread,
Some(txn),
)?;
}
}
}
// ListPins
if event.kind == EventKind::PinList {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.storage.write_relationship_by_id(
id,
event.id,
RelationshipById::ListPins,
Some(txn),
)?;
}
}
}
// ListBookmarks
if event.kind == EventKind::BookmarkList {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.storage.write_relationship_by_id(
id,
event.id,
RelationshipById::ListBookmarks,
Some(txn),
)?;
}
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.storage.write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::ListBookmarks,
Some(txn),
)?;
}
}
}
// BookmarkSets
if event.kind == EventKind::BookmarkSets {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.storage.write_relationship_by_id(
id,
event.id,
RelationshipById::ListBookmarks,
Some(txn),
)?;
}
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.storage.write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::ListBookmarks,
Some(txn),
)?;
}
}
}
// CurationSets
if event.kind == EventKind::CurationSets {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.storage.write_relationship_by_id(
id,
event.id,
RelationshipById::Curation,
Some(txn),
)?;
}
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.storage.write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::Curation,
Some(txn),
)?;
}
}
}
if event.kind == EventKind::LiveChatMessage {
for tag in &event.tags {
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.storage.write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::LiveChatMessage,
Some(txn),
)?;
}
}
}
if event.kind == EventKind::BadgeAward {
for tag in &event.tags {
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.storage.write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::BadgeAward,
Some(txn),
)?;
}
}
}
if event.kind == EventKind::HandlerRecommendation {
for tag in &event.tags {
if let Ok((ea, _marker)) = tag.parse_address() {
GLOBALS.storage.write_relationship_by_addr(
ea,
event.id,
RelationshipByAddr::HandlerRecommendation,
Some(txn),
)?;
}
}
}
if event.kind == EventKind::Reporting {
for tag in &event.tags {
if let Ok((id, Some(rurl), _)) = tag.parse_event() {
let report = &rurl.0;
GLOBALS.storage.write_relationship_by_id(
id,
event.id,
RelationshipById::Reports(report.to_owned()),
Some(txn),
)?;
}
}
}
// zaps
match event.zaps() {
Ok(Some(zapdata)) => {
GLOBALS.storage.write_relationship_by_id(
zapdata.id,
event.id,
RelationshipById::ZapReceipt {
by: event.pubkey,
amount: zapdata.amount,
},
Some(txn),
)?;
invalidate.push(zapdata.id);
}
Err(e) => tracing::error!("Invalid zap receipt: {}", e),
_ => {}
}
// JobResult
if event.kind.is_job_result() {
for tag in &event.tags {
if let Ok((id, _, _)) = tag.parse_event() {
GLOBALS.storage.write_relationship_by_id(
id,
event.id,
RelationshipById::JobResult,
Some(txn),
)?;
}
}
}
Ok(())
};
match rw_txn {
Some(txn) => f(txn)?,
None => {
let mut txn = GLOBALS.storage.get_write_txn()?;
f(&mut txn)?;
txn.commit()?;
}
};
Ok(invalidate)
}
// This updates the event data and maybe the title, but it does NOT update the list
// (that happens only when the user overwrites/merges)
fn update_or_allocate_person_list_from_event(
event: &Event,
pubkey: PublicKey,
) -> Result<(PersonList, PersonListMetadata), Error> {
// Determine PersonList and fetch Metadata
let (list, mut metadata, new) = crate::people::fetch_current_personlist_matching_event(event)?;
// Update metadata
{
metadata.event_created_at = event.created_at;
metadata.event_public_len = event.tags.iter().filter(|t| t.tagname() == "p").count();
if event.kind == EventKind::ContactList {
metadata.event_private_len = None;
} else if GLOBALS.identity.is_unlocked() {
let mut private_len: Option<usize> = None;
if let Ok(bytes) = GLOBALS.identity.decrypt_nip04(&pubkey, &event.content) {
if let Ok(vectags) = serde_json::from_slice::<Vec<Tag>>(&bytes) {
private_len = Some(vectags.iter().filter(|t| t.tagname() == "p").count());
}
}
metadata.event_private_len = private_len;
}
if let Some(title) = event.title() {
metadata.title = title.to_owned();
}
// If title is empty, use the d-tag
if metadata.title.is_empty() && !metadata.dtag.is_empty() {
metadata.title = metadata.dtag.clone();
}
}
// Save metadata
GLOBALS
.storage
.set_person_list_metadata(list, &metadata, None)?;
if new {
// Ask the overlord to populate the list from the event, since it is
// locally new
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::UpdatePersonList {
person_list: list,
merge: false,
});
}
Ok((list, metadata))
}