rostra-client-db 0.1.2

Embedded database and projections for a Rostra client
Documentation
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
use std::cmp;
use std::sync::Arc;

use rostra_core::event::{EventExt as _, EventKind, VerifiedEventContent, content_kind};
use rostra_core::id::{RostraId, ToShort as _};
use rostra_core::{ExternalEventId, Timestamp};
use rostra_util_error::{BoxedError, FmtCompact as _};
use snafu::{Location, OptionExt as _, ResultExt as _, Snafu};
use tracing::debug;

use crate::event::{EventSingletonRecord, SocialVoteProjection, SocialVoteValue};
use crate::event_order::EventOrder;
use crate::{
    Database, DbError, IdSocialProfileRecord, IrohNodeRecord, LOG_TARGET, OverflowSnafu,
    SocialPostReceiptAlreadyIndexedSnafu, SocialPostReceiptMismatchSnafu,
    SocialPostsReactionsRecord, SocialPostsRepliesRecord, WriteTransactionCtx,
    events_singletons_new, ids_followees, shoutbox_posts_by_received_at, social_posts,
    social_posts_by_received_at, social_posts_by_time, social_posts_reactions,
    social_posts_received_at_keys, social_posts_replaced_by, social_posts_replaces,
    social_posts_replies, social_posts_self_mention,
};

#[derive(Debug, Snafu)]
pub enum ProcessEventError {
    #[snafu(transparent)]
    Db { source: DbError },
    Invalid {
        #[snafu(implicit)]
        location: Location,
        source: BoxedError,
    },
}
pub type ProcessEventResult<T> = std::result::Result<T, ProcessEventError>;

#[derive(Debug, Clone, Copy)]
enum SocialPostProjectionPlan {
    Skip,
    Apply {
        replaced_event_id: Option<rostra_core::ShortEventId>,
    },
}

impl Database {
    fn social_post_replaced_event_id(
        event_content: &VerifiedEventContent,
        content: &content_kind::SocialPost,
    ) -> Option<rostra_core::ShortEventId> {
        event_content
            .event
            .is_delete_parent_aux_content_set()
            .then(|| event_content.event.parent_aux())
            .flatten()
            .filter(|_| {
                content
                    .djot_content
                    .as_deref()
                    .is_some_and(|text| !text.trim().is_empty())
            })
    }

    fn social_post_projection_plan(
        event_content: &VerifiedEventContent,
        content: &content_kind::SocialPost,
    ) -> SocialPostProjectionPlan {
        let replaced_event_id = Self::social_post_replaced_event_id(event_content, content);
        if event_content.event.is_delete_parent_aux_content_set() && replaced_event_id.is_none() {
            SocialPostProjectionPlan::Skip
        } else {
            SocialPostProjectionPlan::Apply { replaced_event_id }
        }
    }

    fn insert_social_post_replacement_tx(
        event_content: &VerifiedEventContent,
        old_event_id: rostra_core::ShortEventId,
        tx: &WriteTransactionCtx,
    ) -> ProcessEventResult<()> {
        let author = event_content.author();
        let event_id = event_content.event_id().to_short();

        tx.open_table(&social_posts_replaced_by::TABLE)
            .map_err(DbError::from)?
            .insert(&(author, old_event_id, event_id), &())
            .map_err(DbError::from)?;
        tx.open_table(&social_posts_replaces::TABLE)
            .map_err(DbError::from)?
            .insert(&(author, event_id, old_event_id), &())
            .map_err(DbError::from)?;

        Ok(())
    }

    fn insert_social_post_receipt_tx(
        tx: &WriteTransactionCtx,
        event_id: rostra_core::ShortEventId,
        received_at: Timestamp,
    ) -> ProcessEventResult<()> {
        let mut receipt_keys = tx
            .open_table(&social_posts_received_at_keys::TABLE)
            .map_err(DbError::from)?;
        if receipt_keys
            .get(&event_id)
            .map_err(DbError::from)?
            .is_some()
        {
            return SocialPostReceiptAlreadyIndexedSnafu { event_id }
                .fail()
                .map_err(ProcessEventError::from);
        }

        let mut receipts = tx
            .open_table(&social_posts_by_received_at::TABLE)
            .map_err(DbError::from)?;
        let reception_order =
            Self::insert_reception_ordered_tx(tx, received_at, &event_id, &mut receipts)
                .map_err(ProcessEventError::from)?;
        receipt_keys
            .insert(&event_id, &(received_at, reception_order))
            .map_err(DbError::from)?;
        Ok(())
    }

    fn remove_social_post_receipt_tx(
        tx: &WriteTransactionCtx,
        event_id: rostra_core::ShortEventId,
    ) -> ProcessEventResult<()> {
        let mut receipt_keys = tx
            .open_table(&social_posts_received_at_keys::TABLE)
            .map_err(DbError::from)?;
        let Some(receipt_key) = receipt_keys
            .get(&event_id)
            .map_err(DbError::from)?
            .map(|entry| entry.value())
        else {
            // A schedule can establish deletion before this post's ordinary
            // projection is materialized. In that case there is no receipt to
            // remove. Version-25 total replay eliminates the old inconsistent
            // forward-only representation before normal access.
            return Ok(());
        };

        let mut receipts = tx
            .open_table(&social_posts_by_received_at::TABLE)
            .map_err(DbError::from)?;
        let actual_event_id = receipts
            .get(&receipt_key)
            .map_err(DbError::from)?
            .map(|entry| entry.value());
        if actual_event_id != Some(event_id) {
            return SocialPostReceiptMismatchSnafu {
                event_id,
                actual_event_id,
            }
            .fail()
            .map_err(ProcessEventError::from);
        }
        receipts.remove(&receipt_key).map_err(DbError::from)?;
        receipt_keys.remove(&event_id).map_err(DbError::from)?;
        Ok(())
    }

    /// Derives only immutable edit lineage from an already-Deleted social post.
    pub(crate) fn process_deleted_social_post_replacement_tx(
        event_content: &VerifiedEventContent,
        tx: &WriteTransactionCtx,
    ) -> ProcessEventResult<bool> {
        if event_content.kind() != EventKind::SOCIAL_POST
            || Self::MAX_CONTENT_LEN <= event_content.content_len()
        {
            return Ok(false);
        }
        let content = event_content
            .deserialize_cbor::<content_kind::SocialPost>()
            .boxed()
            .context(InvalidSnafu)?;
        let Some(old_event_id) = Self::social_post_replaced_event_id(event_content, &content)
        else {
            return Ok(false);
        };

        Self::insert_social_post_replacement_tx(event_content, old_event_id, tx)?;
        Ok(true)
    }

    /// Compute the effective received-at timestamp for notification ordering.
    ///
    /// For posts whose author timestamp predates both database creation and the
    /// current follow epoch, use the event's own timestamp instead of `now`.
    /// This pushes old synced posts to the bottom of the notification timeline
    /// instead of showing them all as "just received".
    pub(crate) fn effective_received_at(
        &self,
        author: RostraId,
        event_ts: Timestamp,
        now: Timestamp,
        ids_followees_table: &impl ids_followees::ReadableTable,
    ) -> Timestamp {
        // Own posts always appear as fresh
        if author == self.self_id {
            return now;
        }

        // Post is newer than (or same age as) DB — not a historical sync
        if self.db_init_time <= event_ts {
            return now;
        }

        // Look up the start of the current follow epoch.
        let record = ids_followees_table
            .get(&(self.self_id, author))
            .ok()
            .flatten()
            .map(|g| g.value());

        let Some(record) = record else {
            // Not following this author — use default
            return now;
        };

        // Post predates the current follow epoch — it's historical.
        if event_ts < record.first_ts {
            return event_ts;
        }

        now
    }

    /// After an event content was inserted process special kinds of event
    /// content, like follows/unfollows.
    ///
    /// The `now` parameter should be `Timestamp::now()` for normal operation,
    /// but can be set to a specific value for testing or migration.
    pub(crate) fn process_event_content_inserted_tx(
        &self,
        event_content: &VerifiedEventContent,
        now: Timestamp,
        tx: &WriteTransactionCtx,
    ) -> ProcessEventResult<()> {
        let author = event_content.event.event.author;
        let event_order = EventOrder::new(
            event_content.timestamp(),
            event_content.event_id().to_short(),
        );
        let mut singleton_record = Some(EventSingletonRecord {
            event_id: event_order.event_id(),
            social_vote: None,
        });
        #[allow(clippy::single_match)]
        match event_content.event.event.kind {
            EventKind::FOLLOW | EventKind::UNFOLLOW => {
                let mut ids_followees_t = tx
                    .open_table(&crate::ids_followees::TABLE)
                    .map_err(DbError::from)?;
                let mut ids_followers_t = tx
                    .open_table(&crate::ids_followers::TABLE)
                    .map_err(DbError::from)?;
                let mut ids_follow_events_t = tx
                    .open_table(&crate::ids_follow_events::TABLE)
                    .map_err(DbError::from)?;
                let mut id_unfollowed_t = tx
                    .open_table(&crate::ids_unfollowed::TABLE)
                    .map_err(DbError::from)?;

                // Both FOLLOW and UNFOLLOW use the same content type.
                // The actual follow/unfollow distinction is in the content's is_unfollow()
                // method.
                let content = event_content
                    .deserialize_cbor::<content_kind::Follow>()
                    .boxed()
                    .context(InvalidSnafu)?;
                let (followee, updated) = (
                    content.followee,
                    if content.is_unfollow() {
                        Database::insert_unfollow_tx(
                            author,
                            event_order,
                            content.followee,
                            &mut ids_followees_t,
                            &mut ids_followers_t,
                            &mut ids_follow_events_t,
                            &mut id_unfollowed_t,
                        )?
                    } else {
                        Database::insert_follow_tx(
                            author,
                            event_order,
                            content,
                            &mut ids_followees_t,
                            &mut ids_followers_t,
                            &mut ids_follow_events_t,
                            &mut id_unfollowed_t,
                        )?
                    },
                );

                if updated && tx.commit_hooks_enabled() {
                    if author == self.self_id {
                        // Self's followees changed - update both followees and WoT
                        let followees_sender = self.self_followees_updated.clone();
                        let wot_sender = self.self_wot_updated.clone();
                        let self_followees =
                            Database::read_followees_tx(self.self_id, &ids_followees_t)?;
                        let self_wot = Database::compute_wot_tx(
                            self.self_id,
                            &self_followees,
                            &ids_followees_t,
                        )?;
                        let self_followees = Arc::new(self_followees);
                        let self_wot = Arc::new(self_wot);
                        tx.on_commit(move || {
                            followees_sender.send_replace(self_followees);
                            wot_sender.send_replace(self_wot);
                        });
                    } else if ids_followees_t
                        .get(&(self.self_id, author))
                        .map_err(DbError::from)?
                        .is_some()
                    {
                        // One of self's followees changed their followees - update WoT
                        let wot_sender = self.self_wot_updated.clone();
                        let self_followees =
                            Database::read_followees_tx(self.self_id, &ids_followees_t)?;
                        let self_wot = Database::compute_wot_tx(
                            self.self_id,
                            &self_followees,
                            &ids_followees_t,
                        )?;
                        let self_wot = Arc::new(self_wot);
                        tx.on_commit(move || {
                            wot_sender.send_replace(self_wot);
                        });
                    }

                    if followee == self.self_id {
                        let followers_sender = self.self_followers_updated.clone();
                        let self_followers =
                            Arc::new(Database::read_followers_tx(self.self_id, &ids_followers_t)?);

                        tx.on_commit(move || {
                            followers_sender.send_replace(self_followers);
                        });
                    }
                }
            }
            _ => match event_content.event.event.kind {
                EventKind::NODE_ANNOUNCEMENT => {
                    let content = event_content
                        .deserialize_cbor::<content_kind::NodeAnnouncement>()
                        .boxed()
                        .context(InvalidSnafu)?;
                    let mut ids_nodes_tbl = tx
                        .open_table(&crate::ids_nodes::TABLE)
                        .map_err(DbError::from)?;

                    let addr = match content {
                        content_kind::NodeAnnouncement::Iroh { addr } => addr,
                    };
                    let key = (event_content.author(), addr);
                    let mut existing = ids_nodes_tbl
                        .get(&key)
                        .map_err(DbError::from)?
                        .map(|g| g.value())
                        .unwrap_or_else(|| IrohNodeRecord {
                            announcement_ts: event_content.timestamp(),
                            stats: Default::default(),
                        });

                    existing.announcement_ts =
                        cmp::max(existing.announcement_ts, event_content.timestamp());

                    ids_nodes_tbl
                        .insert(&key, &existing)
                        .map_err(DbError::from)?;

                    Database::trim_iroh_nodes_to_limit_tx(
                        event_content.author(),
                        &mut ids_nodes_tbl,
                    )?;
                }
                EventKind::SOCIAL_PROFILE_UPDATE => {
                    let content = event_content
                        .deserialize_cbor::<content_kind::SocialProfileUpdate>()
                        .boxed()
                        .context(InvalidSnafu)?;
                    Database::insert_latest_value_tx(
                        event_order.timestamp(),
                        &author,
                        IdSocialProfileRecord {
                            event_id: event_order.event_id(),
                            display_name: content.display_name,
                            bio: content.bio,
                            avatar: content.avatar,
                        },
                        &mut tx
                            .open_table(&crate::social_profiles::TABLE)
                            .map_err(DbError::from)?,
                    )?;
                }
                EventKind::SOCIAL_POST => {
                    let content = event_content
                        .deserialize_cbor::<content_kind::SocialPost>().inspect_err(|err| {
                            debug!(target: LOG_TARGET, err = %err.fmt_compact(), "Ignoring malformed SocialComment payload");
                        }).boxed().context(InvalidSnafu)?;

                    let replaced_event_id =
                        match Self::social_post_projection_plan(event_content, &content) {
                            SocialPostProjectionPlan::Skip => return Ok(()),
                            SocialPostProjectionPlan::Apply { replaced_event_id } => {
                                replaced_event_id
                            }
                        };
                    let event_id = event_content.event_id().to_short();

                    let mut social_post_by_time_tbl = tx
                        .open_table(&social_posts_by_time::TABLE)
                        .map_err(DbError::from)?;
                    social_post_by_time_tbl
                        .insert(&(event_content.timestamp(), event_id), &())
                        .map_err(DbError::from)?;

                    // Also insert into received_at index for notification ordering.
                    // Use effective_received_at to push old synced posts to the
                    // bottom of notifications.
                    let ids_followees_tbl = tx
                        .open_table(&ids_followees::TABLE)
                        .map_err(DbError::from)?;
                    let received_at = self.effective_received_at(
                        author,
                        event_content.timestamp(),
                        now,
                        &ids_followees_tbl,
                    );
                    drop(ids_followees_tbl);
                    Self::insert_social_post_receipt_tx(tx, event_id, received_at)?;

                    if let Some(old_event_id) = replaced_event_id {
                        Self::insert_social_post_replacement_tx(event_content, old_event_id, tx)?;
                    }

                    if tx.commit_hooks_enabled() {
                        tx.on_commit({
                            let event_content = event_content.clone();
                            let content = content.clone();
                            let new_posts_tx = self.new_posts_tx.clone();
                            move || {
                                let _ = new_posts_tx.send((event_content.to_owned(), content));
                            }
                        });
                    }

                    if content.news {
                        let post_id =
                            ExternalEventId::new(author, event_content.event_id().to_short());
                        self.upsert_social_news_rank_tx(
                            post_id,
                            Self::cap_creation_timestamp(event_content.timestamp(), now),
                            0,
                            tx,
                        )?;
                        self.notify_news_score_update_on_commit(tx, post_id);
                    }

                    // Check for @mentions of self in the post content
                    if author != self.self_id {
                        if let Some(ref djot_content) = content.djot_content {
                            if rostra_djot::mention::contains_mention(djot_content, self.self_id) {
                                let mut self_mention_tbl = tx
                                    .open_table(&social_posts_self_mention::TABLE)
                                    .map_err(DbError::from)?;
                                self_mention_tbl
                                    .insert(&event_content.event_id().to_short(), &())
                                    .map_err(DbError::from)?;
                            }
                        }
                    }

                    if let Some(reply_to) = content.reply_to {
                        let mut social_post_tbl =
                            tx.open_table(&social_posts::TABLE).map_err(DbError::from)?;
                        let mut social_post_replies_tbl = tx
                            .open_table(&social_posts_replies::TABLE)
                            .map_err(DbError::from)?;

                        let mut social_post_reactions_tbl = tx
                            .open_table(&social_posts_reactions::TABLE)
                            .map_err(DbError::from)?;

                        let mut reply_to_social_post_record = social_post_tbl
                            .get(&reply_to.event_id())
                            .map_err(DbError::from)?
                            .map(|g| g.value())
                            .unwrap_or_default();

                        if content.djot_content.is_some() {
                            reply_to_social_post_record.reply_count = reply_to_social_post_record
                                .reply_count
                                .checked_add(1)
                                .context(OverflowSnafu)?;

                            social_post_replies_tbl
                                .insert(
                                    &(
                                        reply_to.event_id(),
                                        event_content.event.event.timestamp.into(),
                                        event_id,
                                    ),
                                    &SocialPostsRepliesRecord,
                                )
                                .map_err(DbError::from)?;
                        }

                        if content.reaction.is_some() {
                            reply_to_social_post_record.reaction_count =
                                reply_to_social_post_record
                                    .reaction_count
                                    .checked_add(1)
                                    .context(OverflowSnafu)?;

                            social_post_reactions_tbl
                                .insert(
                                    &(
                                        reply_to.event_id(),
                                        event_content.event.event.timestamp.into(),
                                        event_id,
                                    ),
                                    &SocialPostsReactionsRecord,
                                )
                                .map_err(DbError::from)?;
                        }
                        social_post_tbl
                            .insert(&reply_to.event_id(), &reply_to_social_post_record)
                            .map_err(DbError::from)?;
                    }

                    Database::append_social_post_materialization_tx(event_id, tx)
                        .map_err(ProcessEventError::from)?;
                }
                EventKind::SOCIAL_VOTE => {
                    let content = event_content
                        .deserialize_cbor::<content_kind::SocialVote>()
                        .boxed()
                        .context(InvalidSnafu)?;
                    let vote = content
                        .reply_to
                        .filter(|reply_to| {
                            event_content.event.is_singleton()
                                && event_content.aux_key() == Self::social_vote_aux_key(*reply_to)
                        })
                        .map(|target| SocialVoteProjection {
                            target,
                            value: SocialVoteValue::from(content.upvote),
                        });
                    singleton_record = vote.map(|vote| EventSingletonRecord {
                        event_id: event_order.event_id(),
                        social_vote: Some(vote),
                    });
                    if let Some(vote) = vote {
                        self.process_social_vote_tx(vote, author, event_order, tx)?;
                    }
                }
                EventKind::SHOUTBOX => {
                    let content = event_content
                        .deserialize_cbor::<content_kind::Shoutbox>()
                        .boxed()
                        .context(InvalidSnafu)?;

                    // Insert into shoutbox_posts_by_received_at
                    // Use effective_received_at to push old synced posts to the
                    // bottom of notifications.
                    let ids_followees_tbl = tx
                        .open_table(&ids_followees::TABLE)
                        .map_err(DbError::from)?;
                    let received_at = self.effective_received_at(
                        author,
                        event_content.timestamp(),
                        now,
                        &ids_followees_tbl,
                    );
                    drop(ids_followees_tbl);
                    let mut shoutbox_by_received_at_tbl = tx
                        .open_table(&shoutbox_posts_by_received_at::TABLE)
                        .map_err(DbError::from)?;
                    Self::insert_reception_ordered_tx(
                        tx,
                        received_at,
                        &event_content.event_id().to_short(),
                        &mut shoutbox_by_received_at_tbl,
                    )
                    .map_err(ProcessEventError::from)?;

                    // Broadcast to subscribers
                    if tx.commit_hooks_enabled() {
                        tx.on_commit({
                            let event_content = event_content.clone();
                            let content = content.clone();
                            let new_shoutbox_tx = self.new_shoutbox_tx.clone();
                            move || {
                                let _ = new_shoutbox_tx.send((event_content.to_owned(), content));
                            }
                        });
                    }
                }
                _ => {}
            },
        };

        if event_content.event.is_singleton() {
            let Some(singleton_record) = singleton_record else {
                return Ok(());
            };
            let mut events_singletons_tbl = tx
                .open_table(&events_singletons_new::TABLE)
                .map_err(DbError::from)?;

            Self::insert_latest_value_tx(
                event_order.timestamp(),
                &(
                    event_content.author(),
                    event_content.kind(),
                    event_content.aux_key(),
                ),
                singleton_record,
                &mut events_singletons_tbl,
            )?;
        }

        Ok(())
    }

    pub(crate) fn process_event_content_reverted_tx(
        &self,
        event_content: &VerifiedEventContent,
        tx: &WriteTransactionCtx,
    ) -> ProcessEventResult<()> {
        #[allow(clippy::single_match)]
        match event_content.event.event.kind {
            EventKind::SOCIAL_POST => {
                let content = event_content
                    .deserialize_cbor::<content_kind::SocialPost>()
                    .boxed()
                    .context(InvalidSnafu)?;
                match Self::social_post_projection_plan(event_content, &content) {
                    SocialPostProjectionPlan::Skip => return Ok(()),
                    SocialPostProjectionPlan::Apply { .. } => {}
                }
                let mut social_post_by_time_tbl = tx
                    .open_table(&social_posts_by_time::TABLE)
                    .map_err(DbError::from)?;

                social_post_by_time_tbl
                    .remove(&(
                        event_content.timestamp(),
                        event_content.event_id().to_short(),
                    ))
                    .map_err(DbError::from)?;
                Self::remove_social_post_receipt_tx(tx, event_content.event_id().to_short())?;

                if content.news {
                    Self::remove_social_news_rank_tx(
                        ExternalEventId::new(
                            event_content.author(),
                            event_content.event_id().to_short(),
                        ),
                        tx,
                    )?;
                }

                // Remove from self-mention table if present
                let mut self_mention_tbl = tx
                    .open_table(&social_posts_self_mention::TABLE)
                    .map_err(DbError::from)?;
                self_mention_tbl
                    .remove(&event_content.event_id().to_short())
                    .map_err(DbError::from)?;

                if let Some(reply_to) = content.reply_to {
                    let mut social_posts_tbl =
                        tx.open_table(&social_posts::TABLE).map_err(DbError::from)?;
                    let mut social_replies_tbl = tx
                        .open_table(&social_posts_replies::TABLE)
                        .map_err(DbError::from)?;
                    let mut social_reactions_tbl = tx
                        .open_table(&social_posts_reactions::TABLE)
                        .map_err(DbError::from)?;

                    let mut social_post_record = social_posts_tbl
                        .get(&reply_to.event_id())
                        .map_err(DbError::from)?
                        .map(|g| g.value())
                        .unwrap_or_default();

                    if content.djot_content.is_some() {
                        social_replies_tbl
                            .remove(&(
                                reply_to.event_id(),
                                event_content.timestamp(),
                                event_content.event_id().to_short(),
                            ))
                            .map_err(DbError::from)?;

                        social_post_record.reply_count = social_post_record
                            .reply_count
                            .checked_sub(1)
                            .context(OverflowSnafu)?;
                    }

                    if content.reaction.is_some() {
                        social_reactions_tbl
                            .remove(&(
                                reply_to.event_id(),
                                event_content.timestamp(),
                                event_content.event_id().to_short(),
                            ))
                            .map_err(DbError::from)?;

                        social_post_record.reaction_count = social_post_record
                            .reaction_count
                            .checked_sub(1)
                            .context(OverflowSnafu)?;
                    }
                    social_posts_tbl
                        .insert(&reply_to.event_id(), &social_post_record)
                        .map_err(DbError::from)?;
                }
            }
            _ => {}
        }

        Ok(())
    }
}