helix-im 0.1.28

基于 Helix Core 的确定性 MessageV3 IM 业务模块
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
//! ChannelUpdate 写后投影组装。
//!
//! 这里不直接 I/O:只生成 `StorageOp` 与从 driver 回包 bytes 组装 MessageV3 事件数据。

use crate::error::ImError;
use crate::state::ChannelId;
use crate::sync_session::PostFields;
use helix_core::effect::{Row, SqlValue, StorageOp};
use serde_json::Value;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PendingChannelUpdate {
    pub channel_id: ChannelId,
    pub event_seq: u64,
    pub msg_id: String,
    pub fields: PostFields,
    pub update: crate::channel_write::PostChannelUpdate,
    pub source: String,
    pub causation_id: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChannelUpdateProjection {
    pub unread_count: i64,
    pub mention_count: i64,
    pub urgent_count: i64,
    pub mention_list: Vec<String>,
    pub urgent_post_list: Vec<String>,
    pub unread_post_id: Option<String>,
    pub last_root_post_at: i64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemberChannelUpdate {
    pub user_id: String,
    pub effect_id: Option<String>,
    /// The current viewer's notification mode from the notify-only WS member patch.
    pub notify: Option<String>,
    pub channel_is_top: Option<bool>,
    pub projection_revision: Option<u64>,
    pub unread_count: Option<i64>,
    pub unread_post_id: Option<String>,
    pub last_post: Option<Value>,
    pub last_post_at: Option<i64>,
    pub last_root_post_at: Option<i64>,
    pub mention_count: Option<i64>,
    pub mention_count_root: Option<i64>,
    pub mention_list: Option<Vec<String>>,
    pub mention_user: Option<String>,
    pub urgent_count: Option<i64>,
    pub urgent_post_list: Option<Vec<String>>,
    pub urgent_mention_user: Option<String>,
    pub msg_count: Option<i64>,
    pub msg_count_root: Option<i64>,
    pub msg_count_private: Option<i64>,
    pub last_read_seq: Option<i64>,
}

/// Require the monotonic identity shared by online patches and offline sync projections.
pub(crate) fn require_member_projection_identity(
    projection: &MemberChannelUpdate,
    context: &str,
) -> Result<(u64, String), ImError> {
    let revision = projection
        .projection_revision
        .filter(|value| *value > 0)
        .ok_or_else(|| ImError::Parse(format!("{context} missing revision")))?;
    let effect_id = projection
        .effect_id
        .as_deref()
        .filter(|value| !value.trim().is_empty())
        .ok_or_else(|| ImError::Parse(format!("{context} missing effectId")))?
        .to_string();
    Ok((revision, effect_id))
}

impl PendingChannelUpdate {
    /// 记录一次消息写入对频道绝对态的派生更新。
    pub fn new(
        channel_id: ChannelId,
        event_seq: u64,
        msg_id: &str,
        fields: &PostFields,
        update: &crate::channel_write::PostChannelUpdate,
        source: &str,
    ) -> Self {
        Self {
            channel_id,
            event_seq,
            msg_id: msg_id.to_string(),
            fields: fields.clone(),
            update: update.clone(),
            source: source.to_string(),
            causation_id: None,
        }
    }

    /// 绑定用于串联同步批次的因果标识。
    pub fn with_causation_id(mut self, causation_id: Option<String>) -> Self {
        self.causation_id = causation_id.filter(|value| !value.is_empty());
        self
    }

    /// 生成未读计数写入与权威频道行读回操作。
    pub fn storage_ops(&self) -> Vec<StorageOp> {
        vec![
            crate::acl::to_effect::bump_channel_unread_op(&self.update),
            crate::acl::to_effect::get_channel_row_op(self.channel_id),
        ]
    }

    /// 从已持久化频道行构造 `im:channel:update` 的绝对态数据。
    pub fn event_data_from_channel_reply(&self, reply: &[u8]) -> Option<Value> {
        let mut projection = projection_from_channel_reply(reply)?;
        projection.urgent_count = projection.urgent_count.max(0);
        Some(crate::acl::to_effect::post_channel_update_data(
            self.channel_id,
            self.event_seq,
            self.msg_id.as_str(),
            &self.fields,
            &self.update,
            &projection,
            self.source.as_str(),
        ))
    }
}

/// 将 viewer-local `update_channel` 权威态转换为带确定性更新时间的成员投影。
pub fn member_channel_from_update_channel(
    data: &Value,
    channel_id: ChannelId,
    auth_user_id: &str,
    now_ms: u64,
) -> Option<(Row, MemberChannelUpdate)> {
    let channel = data.get("channel").unwrap_or(data);
    let user_id = pick_text(
        channel,
        data,
        &["userId", "user_id", "memberUserId", "member_user_id"],
    )
    .or_else(|| (!auth_user_id.is_empty()).then(|| auth_user_id.to_string()))?;

    let mut row: Row = vec![
        (
            "channel_id".to_string(),
            SqlValue::Text(channel_id.as_str().to_string()),
        ),
        ("user_id".to_string(), SqlValue::Text(user_id.clone())),
    ];

    let mut projection = MemberChannelUpdate {
        user_id,
        effect_id: pick_text(channel, data, &["effectId", "effect_id"]),
        notify: None,
        channel_is_top: None,
        projection_revision: projection_revision(channel, data),
        unread_count: None,
        unread_post_id: None,
        last_post: None,
        last_post_at: None,
        last_root_post_at: None,
        mention_count: None,
        mention_count_root: None,
        mention_list: None,
        mention_user: None,
        urgent_count: None,
        urgent_post_list: None,
        urgent_mention_user: None,
        msg_count: None,
        msg_count_root: None,
        msg_count_private: None,
        last_read_seq: None,
    };

    if let Some(revision) = projection.projection_revision {
        row.push((
            "projection_revision".to_string(),
            SqlValue::Integer(revision.min(i64::MAX as u64) as i64),
        ));
    }
    if let Some(effect_id) = projection.effect_id.as_ref() {
        row.push(("effect_id".to_string(), SqlValue::Text(effect_id.clone())));
    }

    // `update_channel` carries this member-scoped value as a short scalar; keep it
    // separate from channel-wide fields so the caller can persist it on the composite key.
    if let Some(v) = pick_notify(channel, data) {
        row.push(("notify".to_string(), SqlValue::Text(v.clone())));
        projection.notify = Some(v);
    }

    if let Some(v) = pick_bool(channel, data, &["channelIsTop", "channel_is_top", "top"]) {
        row.push(("channel_is_top".to_string(), SqlValue::Integer(v as i64)));
        projection.channel_is_top = Some(v);
    }

    if let Some(v) = pick_int(channel, data, &["unreadCount", "unread_count"]) {
        row.push(("unread_count".to_string(), SqlValue::Integer(v)));
        projection.unread_count = Some(v);
    }
    // 与 increment 的 Go wire 对齐;空串是清除锚点,不能被通用 pick_text 当作缺失。
    if let Some(v) = pick_value(
        channel,
        data,
        &["unReadPostId", "unreadPostId", "unread_post_id"],
    )
    .and_then(Value::as_str)
    {
        row.push(("unread_post_id".to_string(), SqlValue::Text(v.to_owned())));
        projection.unread_post_id = Some(v.to_owned());
    }
    if let Some(v) = pick_value(channel, data, &["lastPost", "last_post"]) {
        let v = crate::message_summary::prepare_post(v);
        row.push((
            "last_post".to_string(),
            SqlValue::Text(value_storage_text(&v)),
        ));
        projection.last_post = Some(v);
    }
    if let Some(v) = pick_int(channel, data, &["lastPostAt", "last_post_at"]) {
        row.push(("last_post_at".to_string(), SqlValue::Integer(v)));
        projection.last_post_at = Some(v);
    }
    if let Some(v) = pick_int(channel, data, &["lastRootPostAt", "last_root_post_at"]) {
        row.push(("last_root_post_at".to_string(), SqlValue::Integer(v)));
        projection.last_root_post_at = Some(v);
    }
    if let Some(v) = pick_int(channel, data, &["mentionCount", "mention_count"]) {
        row.push(("mention_count".to_string(), SqlValue::Integer(v)));
        projection.mention_count = Some(v);
    }
    if let Some(v) = pick_int(channel, data, &["mentionCountRoot", "mention_count_root"]) {
        row.push(("mention_count_root".to_string(), SqlValue::Integer(v)));
        projection.mention_count_root = Some(v);
    }
    if let Some(v) = pick_value(channel, data, &["mentionList", "mention_list"]) {
        row.push((
            "mention_list".to_string(),
            SqlValue::Text(value_storage_text(v)),
        ));
        projection.mention_list = Some(json_list_value(v));
    }
    if let Some(v) = pick_text(channel, data, &["mentionUser", "mention_user"]) {
        row.push(("mention_user".to_string(), SqlValue::Text(v.clone())));
        projection.mention_user = Some(v);
    }
    if let Some(v) = pick_int(
        channel,
        data,
        &[
            "urgentCount",
            "urgent_count",
            "urgentMentionCount",
            "urgent_mention_count",
        ],
    ) {
        let v = v.max(0);
        row.push(("urgent_mention_count".to_string(), SqlValue::Integer(v)));
        projection.urgent_count = Some(v);
    }
    if let Some(v) = pick_value(channel, data, &["urgentPostList", "urgent_post_list"]) {
        row.push((
            "urgent_post_list".to_string(),
            SqlValue::Text(value_storage_text(v)),
        ));
        projection.urgent_post_list = Some(json_list_value(v));
    }
    if let Some(v) = pick_text(
        channel,
        data,
        &[
            "urgentMentionUser",
            "urgent_mention_user",
            "urgentCurrentName",
            "urgent_current_name",
        ],
    ) {
        row.push(("urgent_mention_user".to_string(), SqlValue::Text(v.clone())));
        projection.urgent_mention_user = Some(v);
    }
    if let Some(v) = pick_int(channel, data, &["msgCount", "msg_count"]) {
        row.push(("msg_count".to_string(), SqlValue::Integer(v)));
        projection.msg_count = Some(v);
    }
    if let Some(v) = pick_int(channel, data, &["msgCountRoot", "msg_count_root"]) {
        row.push(("msg_count_root".to_string(), SqlValue::Integer(v)));
        projection.msg_count_root = Some(v);
    }
    if let Some(v) = pick_int(channel, data, &["msgCountPrivate", "msg_count_private"]) {
        row.push(("msg_count_private".to_string(), SqlValue::Integer(v)));
        projection.msg_count_private = Some(v);
    }
    if let Some(v) = pick_int(
        channel,
        data,
        &[
            "lastReadSeq",
            "last_read_seq",
            "readLastSeq",
            "read_last_seq",
        ],
    ) {
        row.push(("last_read_seq".to_string(), SqlValue::Integer(v)));
        projection.last_read_seq = Some(v);
    }

    if row.len() <= 2 {
        return None;
    }
    row.push((
        "updated_at".to_string(),
        SqlValue::Integer(now_ms.min(i64::MAX as u64) as i64),
    ));
    Some((row, projection))
}

/// Decode the authoritative row returned by the final `ScopedGet` in a canonical
/// projection transaction. Emission must use this row, never the inbound frame.
pub fn member_channel_from_reply(
    reply: &[u8],
    channel_id: ChannelId,
    auth_user_id: &str,
    now_ms: u64,
) -> Option<MemberChannelUpdate> {
    let rows: Vec<Value> = serde_json::from_slice(reply).ok()?;
    let row = rows.first()?;
    member_channel_from_update_channel(row, channel_id, auth_user_id, now_ms)
        .map(|(_, projection)| projection)
}

/// A canonical retry may omit no absolute field it originally supplied. Compare
/// the supplied subset with the durable readback to detect same-revision conflicts.
pub fn member_projection_matches(
    expected: &MemberChannelUpdate,
    actual: &MemberChannelUpdate,
) -> bool {
    macro_rules! matches_optional {
        ($field:ident) => {
            expected.$field.is_none() || expected.$field == actual.$field
        };
    }
    expected.user_id == actual.user_id
        && matches_optional!(effect_id)
        && matches_optional!(notify)
        && matches_optional!(channel_is_top)
        && matches_optional!(projection_revision)
        && matches_optional!(unread_count)
        && matches_optional!(unread_post_id)
        && matches_optional!(last_post)
        && matches_optional!(last_post_at)
        && matches_optional!(last_root_post_at)
        && matches_optional!(mention_count)
        && matches_optional!(mention_count_root)
        && matches_optional!(mention_list)
        && matches_optional!(mention_user)
        && matches_optional!(urgent_count)
        && matches_optional!(urgent_post_list)
        && matches_optional!(urgent_mention_user)
        && matches_optional!(msg_count)
        && matches_optional!(msg_count_root)
        && matches_optional!(msg_count_private)
        && matches_optional!(last_read_seq)
}

mod decode;
pub use decode::projection_from_channel_reply;

fn pick_value<'a>(primary: &'a Value, fallback: &'a Value, keys: &[&str]) -> Option<&'a Value> {
    keys.iter()
        .find_map(|key| primary.get(*key).or_else(|| fallback.get(*key)))
}

fn pick_int(primary: &Value, fallback: &Value, keys: &[&str]) -> Option<i64> {
    pick_value(primary, fallback, keys).and_then(|v| {
        v.as_i64()
            .or_else(|| v.as_u64().and_then(|n| i64::try_from(n).ok()))
            .or_else(|| v.as_str().and_then(|s| s.parse::<i64>().ok()))
    })
}

fn pick_text(primary: &Value, fallback: &Value, keys: &[&str]) -> Option<String> {
    pick_value(primary, fallback, keys).and_then(|v| match v {
        Value::String(s) if !s.is_empty() => Some(s.clone()),
        Value::Number(_) | Value::Bool(_) => Some(v.to_string()),
        _ => None,
    })
}

/// Read only the three server-defined notification modes; malformed values are ignored here
/// and rejected by the WS handler before any write effect is produced.
fn pick_notify(primary: &Value, fallback: &Value) -> Option<String> {
    pick_value(primary, fallback, &["notify"])
        .and_then(Value::as_str)
        .filter(|value| matches!(*value, "NORMAL" | "STRONG" | "IGNORE"))
        .map(str::to_string)
}

/// 只读取后端绝对态的布尔值;字符串等宽松形态交给上层 intent gate 拒绝。
fn pick_bool(primary: &Value, fallback: &Value, keys: &[&str]) -> Option<bool> {
    pick_value(primary, fallback, keys).and_then(Value::as_bool)
}

fn projection_revision(primary: &Value, fallback: &Value) -> Option<u64> {
    const KEYS: &[&str] = &["projectionRevision", "projection_revision"];
    [primary, fallback]
        .into_iter()
        .flat_map(|value| KEYS.iter().filter_map(|key| value.get(*key)))
        .filter_map(|value| {
            value
                .as_u64()
                .or_else(|| value.as_i64().and_then(|number| u64::try_from(number).ok()))
                .or_else(|| value.as_str().and_then(|number| number.parse::<u64>().ok()))
        })
        .max()
}

fn value_storage_text(value: &Value) -> String {
    match value {
        Value::String(s) => s.clone(),
        Value::Null => String::new(),
        other => other.to_string(),
    }
}

fn json_list_value(value: &Value) -> Vec<String> {
    match value {
        Value::Array(items) => items
            .iter()
            .filter_map(|item| item.as_str().map(str::to_string))
            .collect(),
        Value::Object(map) => map.keys().cloned().collect(),
        Value::String(s) => {
            if s.is_empty() {
                Vec::new()
            } else {
                serde_json::from_str::<Value>(s)
                    .ok()
                    .map(|v| json_list_value(&v))
                    .unwrap_or_else(|| vec![s.clone()])
            }
        }
        _ => Vec::new(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Construct the smallest post update needed to exercise channel read-back projection.
    fn pending_update() -> PendingChannelUpdate {
        let channel_id = crate::state::test_channel_id(91);
        let update = crate::channel_write::post_updates(
            channel_id,
            &serde_json::json!({"id":"post-91", "type":"NOTICE"}),
            "viewer-91",
            1,
        );
        PendingChannelUpdate::new(
            channel_id,
            1,
            "post-91",
            &PostFields::default(),
            &update,
            "test",
        )
    }

    /// A negative member urgent count is clamped before channel_member persistence and projection.
    #[test]
    fn clamps_negative_member_urgent_count_before_persist() {
        let channel_id = crate::state::test_channel_id(90);
        let data = serde_json::json!({
            "channel": {
                "userId": "viewer-90",
                "urgentCount": -4,
                "urgentPostList": ["post-90"]
            }
        });

        let (row, projection) = member_channel_from_update_channel(&data, channel_id, "", 90)
            .expect("member update should contain a user and urgent projection");
        assert!(row.iter().any(|(column, value)| {
            column == "urgent_mention_count" && matches!(value, SqlValue::Integer(0))
        }));
        assert_eq!(projection.urgent_count, Some(0));
        assert_eq!(
            projection.urgent_post_list,
            Some(vec!["post-90".to_string()])
        );
    }

    /// A notify-only member patch produces a composite-key row without inventing badge fields.
    #[test]
    fn parses_notify_only_member_patch() {
        let channel_id = crate::state::test_channel_id(92);
        let (row, projection) = member_channel_from_update_channel(
            &serde_json::json!({"id": channel_id.as_str(), "userId": "viewer-92", "notify": "IGNORE"}),
            channel_id,
            "viewer-92",
            92,
        )
        .expect("notify-only member patch should be persisted");
        assert!(row.iter().any(|(column, value)| {
            column == "notify" && matches!(value, SqlValue::Text(mode) if mode == "IGNORE")
        }));
        assert_eq!(projection.notify.as_deref(), Some("IGNORE"));
        assert_eq!(projection.unread_count, None);
        assert_eq!(projection.channel_is_top, None);
    }

    #[test]
    fn member_projection_revision_never_falls_back_to_event_or_time_fields() {
        let channel_id = crate::state::test_channel_id(93);
        let (_, legacy) = member_channel_from_update_channel(
            &serde_json::json!({
                "channelId": channel_id.as_str(),
                "userId": "viewer-93",
                "unreadCount": 1,
                "eventSeq": 77,
                "updateAt": 88
            }),
            channel_id,
            "viewer-93",
            93,
        )
        .expect("absolute unread is still a valid legacy patch");
        assert_eq!(legacy.projection_revision, None);

        let (_, canonical) = member_channel_from_update_channel(
            &serde_json::json!({
                "channelId": channel_id.as_str(),
                "userId": "viewer-93",
                "unreadCount": 1,
                "projectionRevision": 9,
                "effectId": "effect-9",
                "eventSeq": 77
            }),
            channel_id,
            "viewer-93",
            93,
        )
        .expect("canonical projection should parse");
        assert_eq!(canonical.projection_revision, Some(9));
        assert_eq!(canonical.effect_id.as_deref(), Some("effect-9"));
    }

    #[test]
    fn canonical_projection_detects_same_revision_payload_conflict() {
        let channel_id = crate::state::test_channel_id(94);
        let parse = |unread_count| {
            member_channel_from_update_channel(
                &serde_json::json!({
                    "channelId": channel_id.as_str(),
                    "userId": "viewer-94",
                    "projectionRevision": 9,
                    "effectId": "effect-9",
                    "unreadCount": unread_count
                }),
                channel_id,
                "viewer-94",
                94,
            )
            .unwrap()
            .1
        };

        assert!(member_projection_matches(&parse(1), &parse(1)));
        assert!(!member_projection_matches(&parse(2), &parse(1)));
    }

    /// A negative persisted urgent count is clamped before the channel update event is emitted.
    #[test]
    fn clamps_negative_readback_urgent_count_in_channel_event() {
        let pending = pending_update();
        let reply = br#"[{"unread_count":1,"mention_count":2,"urgent_count":-5,"urgent_post_list":"[\"post-91\"]","last_root_post_at":10}]"#;

        let data = pending
            .event_data_from_channel_reply(reply)
            .expect("valid channel readback should produce event data");
        assert_eq!(data["dialogPatch"]["urgentCount"], 0);
        assert_eq!(
            data["dialogPatch"]["urgentPostList"],
            serde_json::json!(["post-91"])
        );
    }
}