Skip to main content

rostra_client_db/
news.rs

1use std::collections::BTreeSet;
2
3use rostra_core::event::{EventAuxKey, EventExt as _, EventKind, content_kind};
4use rostra_core::id::RostraId;
5use rostra_core::{ExternalEventId, Timestamp};
6use serde::{Deserialize, Serialize};
7use tracing::warn;
8
9use crate::event::{EventSingletonRecord, SocialVoteProjection};
10use crate::event_order::EventOrder;
11use crate::social::SocialPostRecord;
12use crate::{
13    Database, DbResult, InvalidVoteSingletonProjectionSnafu, LOG_TARGET,
14    MissingVoteSingletonProjectionSnafu, SocialNewsRankRecord, SocialVoteScore,
15    SocialVoteSumRecord, content_store, events, events_content_state, events_singletons_new,
16    social_news_rank_by_post_id, social_news_rank_by_score, social_news_rank_by_time, social_posts,
17    social_vote_sums,
18};
19
20pub(crate) const NEWS_MAX_AGE_SECS: u64 = 4 * 365 * 24 * 60 * 60;
21
22#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
23pub struct NewsRankPaginationCursor {
24    pub score: SocialVoteScore,
25    pub post_id: ExternalEventId,
26}
27
28#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
29pub struct NewsTimePaginationCursor {
30    pub ts: Timestamp,
31    pub post_id: ExternalEventId,
32}
33
34#[derive(Clone, Debug)]
35pub struct NewsPostRecord<C> {
36    pub post: SocialPostRecord<C>,
37    pub post_id: ExternalEventId,
38    pub creation_ts: Timestamp,
39    pub score: SocialVoteScore,
40    pub vote_sum: i64,
41}
42
43impl Database {
44    pub(crate) fn notify_news_score_update_on_commit(
45        &self,
46        tx: &crate::WriteTransactionCtx,
47        post_id: ExternalEventId,
48    ) {
49        let mut sender = self.news_score_updates_tx.clone();
50        tx.on_commit(move || {
51            sender.send(post_id);
52        });
53    }
54
55    pub(crate) fn cap_creation_timestamp(ts: Timestamp, now: Timestamp) -> Timestamp {
56        ts.min(now)
57    }
58
59    pub(crate) fn social_vote_aux_key(reply_to: ExternalEventId) -> EventAuxKey {
60        EventAuxKey::from_bytes(reply_to.event_id().to_bytes())
61    }
62
63    fn validate_social_vote_projection(
64        record: &EventSingletonRecord,
65        aux_key: EventAuxKey,
66    ) -> DbResult<SocialVoteProjection> {
67        let Some(vote) = record.social_vote else {
68            return MissingVoteSingletonProjectionSnafu {
69                event_id: record.event_id,
70            }
71            .fail();
72        };
73        if Self::social_vote_aux_key(vote.target) != aux_key {
74            return InvalidVoteSingletonProjectionSnafu {
75                event_id: record.event_id,
76            }
77            .fail();
78        }
79        Ok(vote)
80    }
81
82    pub(crate) fn calculate_news_score(
83        creation_ts: Timestamp,
84        vote_sum: i64,
85        now: Timestamp,
86    ) -> SocialVoteScore {
87        let hours_passed = now.secs_since(creation_ts) / 3600;
88        let numerator = 10i128 + i128::from(vote_sum).saturating_mul(10_000);
89        let denominator = i128::from(hours_passed) + 1;
90        let score = numerator / denominator;
91        score.clamp(i128::from(i64::MIN), i128::from(i64::MAX)) as i64
92    }
93
94    /// Apply the aggregate half of a validated social-vote winner update.
95    ///
96    /// The caller must validate the singleton header/payload relationship and
97    /// persist this exact projection as the paired winner later in the same
98    /// transaction. Transaction rollback keeps both halves atomic on failure.
99    pub(crate) fn process_social_vote_tx(
100        &self,
101        vote: SocialVoteProjection,
102        author: RostraId,
103        event_order: EventOrder,
104        tx: &crate::WriteTransactionCtx,
105    ) -> DbResult<()> {
106        let singleton_key = (
107            author,
108            EventKind::SOCIAL_VOTE,
109            Self::social_vote_aux_key(vote.target),
110        );
111        let singletons_table = tx.open_table(&events_singletons_new::TABLE)?;
112        let existing_singleton = singletons_table.get(&singleton_key)?.map(|g| g.value());
113        if existing_singleton.as_ref().is_some_and(|existing| {
114            event_order <= EventOrder::new(existing.ts, existing.inner.event_id)
115        }) {
116            return Ok(());
117        }
118
119        let previous_vote = if let Some(existing) = existing_singleton {
120            Some(Self::validate_social_vote_projection(
121                &existing.inner,
122                singleton_key.2,
123            )?)
124        } else {
125            None
126        };
127
128        let adjustments = match previous_vote {
129            Some(previous) if previous.target == vote.target => [
130                Some((vote.target, vote.value.score() - previous.value.score())),
131                None,
132            ],
133            Some(previous) => [
134                Some((previous.target, -previous.value.score())),
135                Some((vote.target, vote.value.score())),
136            ],
137            None => [Some((vote.target, vote.value.score())), None],
138        };
139        let mut changed_sum = false;
140        for (target, delta) in adjustments.into_iter().flatten() {
141            if delta == 0 {
142                continue;
143            }
144            changed_sum = true;
145            let mut vote_sums_table = tx.open_table(&social_vote_sums::TABLE)?;
146            let mut record =
147                vote_sums_table
148                    .get(&target)?
149                    .map(|g| g.value())
150                    .unwrap_or(SocialVoteSumRecord {
151                        last_vote_time: Timestamp::ZERO,
152                        current_sum: 0,
153                    });
154            record.last_vote_time = record.last_vote_time.max(event_order.timestamp());
155            record.current_sum = record.current_sum.saturating_add(delta);
156            vote_sums_table.insert(&target, &record)?;
157        }
158
159        match previous_vote {
160            Some(previous) if previous.target != vote.target => {
161                self.notify_news_score_update_on_commit(tx, previous.target);
162                self.notify_news_score_update_on_commit(tx, vote.target);
163            }
164            _ if changed_sum => self.notify_news_score_update_on_commit(tx, vote.target),
165            _ => {}
166        }
167        Ok(())
168    }
169
170    pub(crate) fn upsert_social_news_rank_tx(
171        &self,
172        post_id: ExternalEventId,
173        creation_ts: Timestamp,
174        score: SocialVoteScore,
175        tx: &crate::WriteTransactionCtx,
176    ) -> DbResult<()> {
177        let mut rank_by_post_id_table = tx.open_table(&social_news_rank_by_post_id::TABLE)?;
178        let mut rank_by_score_table = tx.open_table(&social_news_rank_by_score::TABLE)?;
179        let mut rank_by_time_table = tx.open_table(&social_news_rank_by_time::TABLE)?;
180
181        if let Some(existing) = rank_by_post_id_table.get(&post_id)?.map(|g| g.value()) {
182            rank_by_score_table.remove(&(existing.score, post_id))?;
183            rank_by_time_table.remove(&(existing.creation_ts, post_id))?;
184        }
185
186        let record = SocialNewsRankRecord { creation_ts, score };
187        rank_by_post_id_table.insert(&post_id, &record)?;
188        rank_by_score_table.insert(&(score, post_id), &())?;
189        rank_by_time_table.insert(&(creation_ts, post_id), &())?;
190        Ok(())
191    }
192
193    pub(crate) fn remove_social_news_rank_tx(
194        post_id: ExternalEventId,
195        tx: &crate::WriteTransactionCtx,
196    ) -> DbResult<()> {
197        let mut rank_by_post_id_table = tx.open_table(&social_news_rank_by_post_id::TABLE)?;
198        let Some(existing) = rank_by_post_id_table.remove(&post_id)?.map(|g| g.value()) else {
199            return Ok(());
200        };
201
202        let mut rank_by_score_table = tx.open_table(&social_news_rank_by_score::TABLE)?;
203        let mut rank_by_time_table = tx.open_table(&social_news_rank_by_time::TABLE)?;
204        rank_by_score_table.remove(&(existing.score, post_id))?;
205        rank_by_time_table.remove(&(existing.creation_ts, post_id))?;
206        Ok(())
207    }
208
209    pub async fn recalculate_news_post_score(&self, post_id: ExternalEventId) -> bool {
210        let now = Timestamp::now();
211        self.write_with(|tx| Self::recalculate_news_post_score_tx(post_id, now, tx))
212            .await
213            .expect("Storage error")
214    }
215
216    pub(crate) fn recalculate_news_post_score_tx(
217        post_id: ExternalEventId,
218        now: Timestamp,
219        tx: &crate::WriteTransactionCtx,
220    ) -> DbResult<bool> {
221        let mut rank_by_post_id_table = tx.open_table(&social_news_rank_by_post_id::TABLE)?;
222        let Some(existing) = rank_by_post_id_table.get(&post_id)?.map(|g| g.value()) else {
223            return Ok(false);
224        };
225
226        if NEWS_MAX_AGE_SECS < now.secs_since(existing.creation_ts) {
227            rank_by_post_id_table.remove(&post_id)?;
228            let mut rank_by_score_table = tx.open_table(&social_news_rank_by_score::TABLE)?;
229            let mut rank_by_time_table = tx.open_table(&social_news_rank_by_time::TABLE)?;
230            rank_by_score_table.remove(&(existing.score, post_id))?;
231            rank_by_time_table.remove(&(existing.creation_ts, post_id))?;
232            return Ok(false);
233        }
234
235        let vote_sum = tx
236            .open_table(&social_vote_sums::TABLE)?
237            .get(&post_id)?
238            .map(|g| g.value().current_sum)
239            .unwrap_or(0);
240        let score = Self::calculate_news_score(existing.creation_ts, vote_sum, now);
241        if score == existing.score {
242            return Ok(true);
243        }
244
245        let mut rank_by_score_table = tx.open_table(&social_news_rank_by_score::TABLE)?;
246        let mut rank_by_time_table = tx.open_table(&social_news_rank_by_time::TABLE)?;
247        rank_by_score_table.remove(&(existing.score, post_id))?;
248        rank_by_time_table.remove(&(existing.creation_ts, post_id))?;
249
250        let updated = SocialNewsRankRecord {
251            creation_ts: existing.creation_ts,
252            score,
253        };
254        rank_by_post_id_table.insert(&post_id, &updated)?;
255        rank_by_score_table.insert(&(score, post_id), &())?;
256        rank_by_time_table.insert(&(existing.creation_ts, post_id), &())?;
257        Ok(true)
258    }
259
260    pub async fn get_random_news_post_ids(&self, limit: usize) -> Vec<ExternalEventId> {
261        self.read_with(|tx| {
262            let table = tx.open_table(&social_news_rank_by_post_id::TABLE)?;
263            let mut post_ids = BTreeSet::new();
264            let max_attempts = limit.saturating_mul(4).max(limit);
265
266            for _ in 0..max_attempts {
267                let Some(post_id) = Self::get_random_table_key(&table)? else {
268                    break;
269                };
270                post_ids.insert(post_id);
271                if limit <= post_ids.len() {
272                    break;
273                }
274            }
275
276            Ok(post_ids.into_iter().collect())
277        })
278        .await
279        .expect("Storage error")
280    }
281
282    pub async fn get_social_vote_sum(&self, post_id: ExternalEventId) -> i64 {
283        self.read_with(|tx| {
284            Ok(tx
285                .open_table(&social_vote_sums::TABLE)?
286                .get(&post_id)?
287                .map(|g| g.value().current_sum)
288                .unwrap_or(0))
289        })
290        .await
291        .expect("Storage error")
292    }
293
294    /// Return a voter's current projected vote for one full post identity.
295    ///
296    /// The outer `None` means this post is not the winner for its shortened
297    /// singleton key. The inner `None` is a retained neutral vote. This query
298    /// reads the inline current projection and does not require retained source
299    /// payload bytes.
300    ///
301    /// # Panics
302    ///
303    /// Panics on storage failure or a vote singleton with a missing or invalid
304    /// inline projection.
305    pub async fn get_social_vote(
306        &self,
307        voter: RostraId,
308        post_id: ExternalEventId,
309    ) -> Option<Option<bool>> {
310        self.read_with(|tx| {
311            let singleton = tx
312                .open_table(&events_singletons_new::TABLE)?
313                .get(&(
314                    voter,
315                    EventKind::SOCIAL_VOTE,
316                    Self::social_vote_aux_key(post_id),
317                ))?
318                .map(|g| g.value());
319            let Some(singleton) = singleton else {
320                return Ok(None);
321            };
322            let vote = Self::validate_social_vote_projection(
323                &singleton.inner,
324                Self::social_vote_aux_key(post_id),
325            )?;
326            if vote.target != post_id {
327                return Ok(None);
328            }
329            Ok(Some(vote.value.as_upvote()))
330        })
331        .await
332        .expect("Storage error")
333    }
334
335    /// Paginate news posts by rank score, newest cursor positions moving from
336    /// highest score toward lower scores.
337    pub async fn paginate_news_posts_by_rank_rev(
338        &self,
339        cursor: Option<NewsRankPaginationCursor>,
340        limit: usize,
341    ) -> (
342        Vec<NewsPostRecord<content_kind::SocialPost>>,
343        Option<NewsRankPaginationCursor>,
344    ) {
345        self.read_with(|tx| {
346            let rank_by_score_table = tx.open_table(&social_news_rank_by_score::TABLE)?;
347            let events_table = tx.open_table(&events::TABLE)?;
348            let social_posts_table = tx.open_table(&social_posts::TABLE)?;
349            let events_content_state_table = tx.open_table(&events_content_state::TABLE)?;
350            let content_store_table = tx.open_table(&content_store::TABLE)?;
351            let vote_sums_table = tx.open_table(&social_vote_sums::TABLE)?;
352            let rank_by_post_id_table = tx.open_table(&social_news_rank_by_post_id::TABLE)?;
353
354            let (ret, cursor) = Self::paginate_table_rev(
355                &rank_by_score_table,
356                cursor.map(|c| (c.score, c.post_id)),
357                limit,
358                move |(_score, post_id), _| {
359                    Self::news_post_record_for_id_tx(
360                        post_id,
361                        &events_table,
362                        &social_posts_table,
363                        &events_content_state_table,
364                        &content_store_table,
365                        &vote_sums_table,
366                        &rank_by_post_id_table,
367                    )
368                },
369            )?;
370
371            Ok((
372                ret,
373                cursor.map(|(score, post_id)| NewsRankPaginationCursor { score, post_id }),
374            ))
375        })
376        .await
377        .expect("Storage error")
378    }
379
380    /// Paginate news posts by capped creation timestamp, newest first.
381    pub async fn paginate_news_posts_by_time_rev(
382        &self,
383        cursor: Option<NewsTimePaginationCursor>,
384        limit: usize,
385    ) -> (
386        Vec<NewsPostRecord<content_kind::SocialPost>>,
387        Option<NewsTimePaginationCursor>,
388    ) {
389        self.read_with(|tx| {
390            let rank_by_time_table = tx.open_table(&social_news_rank_by_time::TABLE)?;
391            let events_table = tx.open_table(&events::TABLE)?;
392            let social_posts_table = tx.open_table(&social_posts::TABLE)?;
393            let events_content_state_table = tx.open_table(&events_content_state::TABLE)?;
394            let content_store_table = tx.open_table(&content_store::TABLE)?;
395            let vote_sums_table = tx.open_table(&social_vote_sums::TABLE)?;
396            let rank_by_post_id_table = tx.open_table(&social_news_rank_by_post_id::TABLE)?;
397
398            let (ret, cursor) = Self::paginate_table_rev(
399                &rank_by_time_table,
400                cursor.map(|c| (c.ts, c.post_id)),
401                limit,
402                move |(_ts, post_id), _| {
403                    Self::news_post_record_for_id_tx(
404                        post_id,
405                        &events_table,
406                        &social_posts_table,
407                        &events_content_state_table,
408                        &content_store_table,
409                        &vote_sums_table,
410                        &rank_by_post_id_table,
411                    )
412                },
413            )?;
414
415            Ok((
416                ret,
417                cursor.map(|(ts, post_id)| NewsTimePaginationCursor { ts, post_id }),
418            ))
419        })
420        .await
421        .expect("Storage error")
422    }
423
424    fn news_post_record_for_id_tx(
425        post_id: ExternalEventId,
426        events_table: &impl events::ReadableTable,
427        social_posts_table: &impl social_posts::ReadableTable,
428        events_content_state_table: &impl events_content_state::ReadableTable,
429        content_store_table: &impl content_store::ReadableTable,
430        vote_sums_table: &impl social_vote_sums::ReadableTable,
431        rank_by_post_id_table: &impl social_news_rank_by_post_id::ReadableTable,
432    ) -> DbResult<Option<NewsPostRecord<content_kind::SocialPost>>> {
433        let Some(rank) = rank_by_post_id_table.get(&post_id)?.map(|g| g.value()) else {
434            return Ok(None);
435        };
436
437        let Some((social_post, event, social_post_record)) = Self::get_social_post_record_tx(
438            events_table,
439            social_posts_table,
440            events_content_state_table,
441            content_store_table,
442            post_id.event_id(),
443        )?
444        else {
445            warn!(target: LOG_TARGET, %post_id, "Missing social post for news rank record");
446            return Ok(None);
447        };
448
449        if !social_post.news {
450            return Ok(None);
451        }
452
453        let vote_sum = vote_sums_table
454            .get(&post_id)?
455            .map(|g| g.value().current_sum)
456            .unwrap_or(0);
457
458        Ok(Some(NewsPostRecord {
459            post: SocialPostRecord {
460                ts: event.timestamp(),
461                event_id: post_id.event_id(),
462                author: event.author(),
463                reply_to: social_post.reply_to,
464                content: social_post,
465                reply_count: social_post_record.reply_count,
466            },
467            post_id,
468            creation_ts: rank.creation_ts,
469            score: rank.score,
470            vote_sum,
471        }))
472    }
473}