c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1 | use std::collections::{HashSet, VecDeque}; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 2 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 3 | use ruma::events::room::message::sanitize::remove_plain_reply_fallback; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 4 | use serde_json::Value; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 5 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 6 | use super::{ |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 7 | MessageIndex, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 8 | index::{Doc, provisional_key}, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 9 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 10 | use crate::normalise::poll_question; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 11 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 12 | const REMEMBERED_REDACTIONS: usize = 10_000; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 13 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 14 | #[derive(Clone, Debug, Eq, PartialEq)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 15 | pub(crate) struct Edit { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 16 | pub(crate) id: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 17 | pub(crate) target: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 18 | pub(crate) sender: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 19 | pub(crate) ts: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 20 | pub(crate) text: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 21 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 22 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 23 | #[derive(Clone, Debug, Eq, PartialEq)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 24 | pub(crate) enum Observed { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 25 | Message { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 26 | key: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 27 | sender: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 28 | ts: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 29 | text: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 30 | bundled: Option<Edit>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 31 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 32 | Edit(Edit), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 33 | Redaction { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 34 | target: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 35 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 36 | Redacted { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 37 | key: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 38 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 39 | Encrypted { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 40 | key: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 41 | session_id: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 42 | ts: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 43 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 44 | Other, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 45 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 46 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 47 | #[derive(Debug, Eq, PartialEq)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 48 | pub(crate) enum Effect { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 49 | Indexed(u64), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 50 | Removed, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 51 | /// written, but nothing searchable changed |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 52 | Held, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 53 | /// an edit was redacted out from under its original, which has to be read again |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 54 | Refetch(String), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 55 | Nothing, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 56 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 57 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 58 | /// redactions seen lately, so a stale copy of what they removed can't come back |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 59 | #[derive(Default)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 60 | pub(crate) struct Redactions { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 61 | order: VecDeque<String>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 62 | seen: HashSet<String>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 63 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 64 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 65 | impl Redactions { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 66 | fn insert(&mut self, key: String) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 67 | if self.seen.insert(key.clone()) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 68 | self.order.push_back(key); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 69 | if self.order.len() > REMEMBERED_REDACTIONS |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 70 | && let Some(oldest) = self.order.pop_front() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 71 | { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 72 | self.seen.remove(&oldest); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 73 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 74 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 75 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 76 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 77 | fn contains(&self, key: &str) -> bool { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 78 | self.seen.contains(key) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 79 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 80 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 81 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 82 | fn text_of(event_type: &str, content: &Value) -> Option<String> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 83 | let text = match event_type { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 84 | "m.room.message" => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 85 | let msgtype = content.get("msgtype")?.as_str()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 86 | if msgtype == "m.key.verification.request" { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 87 | return None; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 88 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 89 | let mut body = content.get("body")?.as_str()?.to_owned(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 90 | if content.pointer("/m.relates_to/m.in_reply_to").is_some() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 91 | body = remove_plain_reply_fallback(&body).to_owned(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 92 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 93 | match content.get("filename").and_then(Value::as_str) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 94 | Some(filename) if filename != body => format!("{body}\n{filename}"), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 95 | _ => body, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 96 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 97 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 98 | "m.sticker" => content.get("body")?.as_str()?.to_owned(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 99 | "m.poll.start" | "org.matrix.msc3381.poll.start" => poll_question(content, event_type)?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 100 | _ => return None, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 101 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 102 | (!text.trim().is_empty()).then_some(text) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 103 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 104 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 105 | fn edit_of(value: &Value, event_type: &str) -> Option<Edit> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 106 | let content = value.get("content")?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 107 | if content.pointer("/m.relates_to/rel_type")?.as_str()? != "m.replace" { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 108 | return None; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 109 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 110 | Some(Edit { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 111 | id: value.get("event_id")?.as_str()?.to_owned(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 112 | target: content |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 113 | .pointer("/m.relates_to/event_id")? |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 114 | .as_str()? |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 115 | .to_owned(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 116 | sender: value.get("sender")?.as_str()?.to_owned(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 117 | ts: value.get("origin_server_ts")?.as_u64()?, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 118 | text: text_of(event_type, content.get("m.new_content")?).unwrap_or_default(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 119 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 120 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 121 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 122 | pub(crate) fn observe(value: &Value) -> Observed { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 123 | observe_inner(value).unwrap_or(Observed::Other) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 124 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 125 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 126 | fn observe_inner(value: &Value) -> Option<Observed> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 127 | let event_type = value.get("type")?.as_str()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 128 | let key = value.get("event_id")?.as_str()?.to_owned(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 129 | if event_type == "m.room.redaction" { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 130 | let target = value |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 131 | .get("redacts") |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 132 | .or_else(|| value.pointer("/content/redacts"))? |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 133 | .as_str()? |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 134 | .to_owned(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 135 | return Some(Observed::Redaction { target }); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 136 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 137 | if value.pointer("/unsigned/redacted_because").is_some() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 138 | return Some(Observed::Redacted { key }); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 139 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 140 | let ts = value.get("origin_server_ts")?.as_u64()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 141 | if event_type == "m.room.encrypted" { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 142 | let session_id = value.pointer("/content/session_id")?.as_str()?.to_owned(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 143 | return Some(Observed::Encrypted { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 144 | key, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 145 | session_id, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 146 | ts, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 147 | }); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 148 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 149 | if value |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 150 | .pointer("/content/m.relates_to/rel_type") |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 151 | .and_then(Value::as_str) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 152 | == Some("m.replace") |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 153 | { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 154 | return edit_of(value, event_type).map(Observed::Edit); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 155 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 156 | let content = value.get("content")?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 157 | let text = text_of(event_type, content)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 158 | let sender = value.get("sender")?.as_str()?.to_owned(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 159 | let bundled = value |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 160 | .pointer("/unsigned/m.relations/m.replace") |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 161 | .and_then(|edit| edit_of(edit, event_type)) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 162 | .filter(|edit| edit.target == key); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 163 | Some(Observed::Message { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 164 | key, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 165 | sender, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 166 | ts, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 167 | text, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 168 | bundled, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 169 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 170 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 171 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 172 | fn newer(ts: u64, id: &str, than: &Doc) -> bool { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 173 | (ts, id) > (than.content_ts, than.content.as_str()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 174 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 175 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 176 | /// what `observed` does to `room`'s part of the index. edits land on their original under |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 177 | /// its key, the latest by its own sender wins, and one that arrives first waits under |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 178 | /// `provisional_key` until the original turns up |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 179 | pub(crate) fn apply( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 180 | index: &mut dyn MessageIndex, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 181 | redactions: &mut Redactions, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 182 | room: &str, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 183 | observed: Observed, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 184 | ) -> Effect { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 185 | match observed { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 186 | Observed::Message { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 187 | key, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 188 | sender, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 189 | ts, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 190 | text, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 191 | bundled, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 192 | } => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 193 | if redactions.contains(&key) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 194 | return Effect::Nothing; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 195 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 196 | let mut doc = Doc { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 197 | key: key.clone(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 198 | room: room.to_owned(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 199 | sender: sender.clone(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 200 | ts, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 201 | content: key.clone(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 202 | content_ts: ts, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 203 | text, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 204 | provisional: false, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 205 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 206 | if let Some(edit) = |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 207 | bundled.filter(|edit| edit.sender == sender && !redactions.contains(&edit.id)) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 208 | { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 209 | doc.content = edit.id; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 210 | doc.content_ts = edit.ts; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 211 | doc.text = edit.text; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 212 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 213 | if let Some(waiting) = index.get(&provisional_key(&key, &sender)) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 214 | && newer(waiting.content_ts, &waiting.content, &doc) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 215 | { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 216 | doc.content = waiting.content; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 217 | doc.content_ts = waiting.content_ts; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 218 | doc.text = waiting.text; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 219 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 220 | let held = index.remove_provisional(&key); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 221 | if let Some(existing) = index.get(&key) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 222 | && !newer(doc.content_ts, &doc.content, &existing) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 223 | { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 224 | return if held { Effect::Held } else { Effect::Nothing }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 225 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 226 | index.put(doc); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 227 | Effect::Indexed(ts) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 228 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 229 | Observed::Edit(edit) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 230 | if redactions.contains(&edit.target) || redactions.contains(&edit.id) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 231 | return Effect::Nothing; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 232 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 233 | if index |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 234 | .by_content(&edit.target) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 235 | .is_some_and(|doc| doc.key != edit.target) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 236 | { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 237 | return Effect::Nothing; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 238 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 239 | if let Some(existing) = index.get(&edit.target) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 240 | if existing.sender != edit.sender || !newer(edit.ts, &edit.id, &existing) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 241 | return Effect::Nothing; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 242 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 243 | let ts = existing.ts; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 244 | index.put(Doc { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 245 | content: edit.id, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 246 | content_ts: edit.ts, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 247 | text: edit.text, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 248 | ..existing |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 249 | }); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 250 | return Effect::Indexed(ts); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 251 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 252 | let key = provisional_key(&edit.target, &edit.sender); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 253 | if let Some(waiting) = index.get(&key) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 254 | && !newer(edit.ts, &edit.id, &waiting) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 255 | { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 256 | return Effect::Nothing; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 257 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 258 | index.put(Doc { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 259 | key, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 260 | room: room.to_owned(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 261 | sender: edit.sender, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 262 | ts: edit.ts, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 263 | content: edit.id, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 264 | content_ts: edit.ts, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 265 | text: edit.text, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 266 | provisional: true, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 267 | }); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 268 | Effect::Held |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 269 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 270 | Observed::Redaction { target } | Observed::Redacted { key: target } => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 271 | redactions.insert(target.clone()); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 272 | let removed = index.remove(&target); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 273 | if index.remove_provisional(&target) || removed { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 274 | return Effect::Removed; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 275 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 276 | match index.by_content(&target) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 277 | Some(doc) if doc.key != target => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 278 | index.remove(&doc.key); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 279 | if doc.provisional { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 280 | Effect::Removed |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 281 | } else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 282 | Effect::Refetch(doc.key) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 283 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 284 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 285 | _ => Effect::Nothing, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 286 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 287 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 288 | Observed::Encrypted { .. } | Observed::Other => Effect::Nothing, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 289 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 290 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 291 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 292 | #[cfg(test)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 293 | #[path = "feed_tests.rs"] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 294 | mod tests; |