wechat-profile 1.4.0

Readers and checked writers for supported official WeChat profiles
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
//! Whole-conversation local read projection at a caller's frozen timestamp.
use crate::{
    projection::{check_known_table, identity, ownership},
    writer::{exact_columns, find_database, open_write},
    *,
};
use rusqlite::{params, OptionalExtension};
use serde::Serialize;

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ThreadReadProjection {
    pub session_present: bool,
    pub removed_unread_rows: usize,
    pub unread_count_before: i64,
    pub unread_count_after: i64,
    pub last_clear_unread_timestamp: i64,
    pub whole_thread: bool,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ThreadExitReadOutcome {
    Applied,
    MissingSession,
    HiddenSession,
    ExcludedPeer,
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ThreadExitReadProjection {
    pub outcome: ThreadExitReadOutcome,
    pub projection: Option<ThreadReadProjection>,
}

// The ordinary session-exit path excludes these built-in service holders and
// endpoints. Notification aggregate peers have a separate handler branch.
const EXIT_READ_EXCLUDED_PEERS: &[&str] = &[
    "brandsessionholder",
    "brandservicesessionholder",
    "qmessage",
    "medianote",
    "gh_22b87fa7cb3c",
    "qqsync",
    "lbsapp",
    "shakeapp",
    "floatbottle",
    "masssendapp",
    "gh_43f2581f6fd6",
    "appbrandcustomerservicemsg",
    "service_officialaccounts",
    "notifymessage",
    "notification_messages",
];

/// Clear this session through a frozen server timestamp, preserving later rows.
/// Unlike an exact-ID projection, this reconciles an older counter/list mismatch
/// when the entire known session is before the boundary. It performs no RPC.
pub fn mark_read_thread(
    location: &ProfileLocation,
    keys: &KeyFile,
    thread: &str,
    through_time: i64,
    options: &WriteOptions,
) -> Result<ThreadReadProjection> {
    project_thread_read(location, keys, thread, through_time, options, false)?
        .projection
        .ok_or(ProfileError::InvalidMetadata)
}

/// Project only the unread-clear component of a recognized session-exit
/// control. Missing, hidden and excluded sessions remain unchanged. The caller
/// must retain the full control as incomplete and journal every evaluated result.
pub fn mark_read_thread_exit(
    location: &ProfileLocation,
    keys: &KeyFile,
    thread: &str,
    through_time: i64,
    options: &WriteOptions,
) -> Result<ThreadExitReadProjection> {
    project_thread_read(location, keys, thread, through_time, options, true)
}

fn project_thread_read(
    location: &ProfileLocation,
    keys: &KeyFile,
    thread: &str,
    through_time: i64,
    options: &WriteOptions,
    visible_ordinary_only: bool,
) -> Result<ThreadExitReadProjection> {
    ownership(location, keys, options)?;
    identity(thread)?;
    if !u32::try_from(through_time).is_ok_and(|time| time != 0) {
        return Err(ProfileError::InvalidQuery);
    }
    if visible_ordinary_only && EXIT_READ_EXCLUDED_PEERS.contains(&thread) {
        return Ok(ThreadExitReadProjection {
            outcome: ThreadExitReadOutcome::ExcludedPeer,
            projection: None,
        });
    }
    let relative = find_database(location, "session.db")?;
    let mut database = open_write(location, &relative, keys, options.allow_plaintext)?;
    check_known_table(&database, "SessionTable")?;
    check_known_table(&database, "SessionUnreadListTable_1")?;
    exact_columns(&database, &relative, "Name2Id", &["user_name"])?;
    let tx = database.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
    let session = tx
        .query_row(
            "SELECT COALESCE(unread_count,0),COALESCE(last_timestamp,0),COALESCE(last_clear_unread_timestamp,0),COALESCE(is_hidden,0) FROM SessionTable WHERE username=?1",
            [thread],
            |row| Ok((row.get::<_, i64>(0)?, row.get::<_, i64>(1)?, row.get::<_, i64>(2)?, row.get::<_, i64>(3)?)),
        )
        .optional()?;
    let Some((before, last_time, old_clear, is_hidden)) = session else {
        // A status control can refer to a session absent from local history.
        // Do not manufacture a conversation merely to record an empty read.
        return Ok(ThreadExitReadProjection {
            outcome: ThreadExitReadOutcome::MissingSession,
            projection: Some(ThreadReadProjection {
                session_present: false,
                removed_unread_rows: 0,
                unread_count_before: 0,
                unread_count_after: 0,
                last_clear_unread_timestamp: 0,
                whole_thread: true,
            }),
        });
    };
    if visible_ordinary_only && is_hidden != 0 {
        return Ok(ThreadExitReadProjection {
            outcome: ThreadExitReadOutcome::HiddenSession,
            projection: None,
        });
    }
    if before < 0 || old_clear < 0 {
        return Err(ProfileError::WritesUnsupported);
    }
    let name_id = tx
        .query_row(
            "SELECT rowid FROM Name2Id WHERE user_name=?1",
            [thread],
            |row| row.get::<_, i64>(0),
        )
        .optional()?;
    let (removed, remaining, first) = if let Some(name_id) = name_id {
        let removed = tx.execute(
            "DELETE FROM SessionUnreadListTable_1 WHERE username_id=?1 AND create_time<=?2",
            params![name_id, through_time],
        )?;
        let remaining: i64 = tx.query_row(
            "SELECT COUNT(*) FROM SessionUnreadListTable_1 WHERE username_id=?1",
            [name_id],
            |row| row.get(0),
        )?;
        let first = tx.query_row(
            "SELECT server_id FROM SessionUnreadListTable_1 WHERE username_id=?1 ORDER BY create_time,local_id,server_id LIMIT 1",
            [name_id],
            |row| row.get::<_, i64>(0),
        ).optional()?.unwrap_or(0);
        (removed, remaining, first)
    } else {
        (0, 0, 0)
    };
    // Unknown counter contributions can belong to newer history. Preserve them
    // when the session summary is newer than this read boundary.
    let after = if last_time > through_time {
        before.saturating_sub(removed as i64).max(remaining)
    } else {
        remaining
    };
    let clear = old_clear.max(through_time);
    tx.execute(
        "UPDATE SessionTable SET unread_count=?1,unread_first_msg_srv_id=?2,last_clear_unread_timestamp=?3,unread_first_pat_msg_local_id=CASE WHEN ?1=0 THEN 0 ELSE unread_first_pat_msg_local_id END,unread_first_pat_msg_sort_seq=CASE WHEN ?1=0 THEN 0 ELSE unread_first_pat_msg_sort_seq END WHERE username=?4",
        params![after, first, clear, thread],
    )?;
    tx.commit()?;
    Ok(ThreadExitReadProjection {
        outcome: ThreadExitReadOutcome::Applied,
        projection: Some(ThreadReadProjection {
            session_present: true,
            removed_unread_rows: removed,
            unread_count_before: before,
            unread_count_after: after,
            last_clear_unread_timestamp: clear,
            whole_thread: true,
        }),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use rusqlite::Connection;
    use std::{collections::BTreeMap, fs};

    fn fixture(reordered: bool) -> (tempfile::TempDir, ProfileLocation, KeyFile, WriteOptions) {
        let directory = tempfile::tempdir().unwrap();
        let path = directory
            .path()
            .join("account/db_storage/session/session.db");
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        let sql = include_str!("layout/session.db.sql");
        let sql = if reordered {
            sql.replace(
                "server_id INTEGER, local_id INTEGER, create_time INTEGER",
                "server_id INTEGER, create_time INTEGER, local_id INTEGER",
            )
        } else {
            sql.into()
        };
        let database = Connection::open(path).unwrap();
        database.execute_batch(&sql).unwrap();
        database.execute_batch("INSERT INTO Name2Id(rowid,user_name)VALUES(1,'peer'),(2,'other'); INSERT INTO SessionTable(username,unread_count,unread_first_msg_srv_id,unread_first_pat_msg_local_id,unread_first_pat_msg_sort_seq,last_timestamp,last_clear_unread_timestamp,draft,status)VALUES('peer',9,90,12,13,100,20,'keep draft',7),('other',1,500,0,0,100,0,'other',3); INSERT INTO SessionUnreadListTable_1(username_id,server_id,local_id,create_time)VALUES(1,90,1,90),(1,80,2,100),(2,500,1,100);").unwrap();
        drop(database);
        let location = ProfileLocation::new(directory.path().join("account")).unwrap();
        let keys = KeyFile {
            account_binding: "account".into(),
            databases: BTreeMap::new(),
            image_dat: None,
        };
        let options = WriteOptions {
            exclusive_ownership_confirmed: true,
            allow_plaintext: true,
            durable_intent_id: "synthetic-thread-read".into(),
        };
        (directory, location, keys, options)
    }

    #[test]
    fn whole_thread_clears_legacy_counter_and_advances_watermark_in_both_column_orders() {
        for reordered in [false, true] {
            let (_directory, location, keys, options) = fixture(reordered);
            let result = mark_read_thread(&location, &keys, "peer", 100, &options).unwrap();
            assert_eq!(
                (
                    result.removed_unread_rows,
                    result.unread_count_before,
                    result.unread_count_after
                ),
                (2, 9, 0)
            );
            assert_eq!(result.last_clear_unread_timestamp, 100);
            let database = Connection::open(location.storage.join("session/session.db")).unwrap();
            let row = database.query_row("SELECT unread_count,unread_first_msg_srv_id,last_clear_unread_timestamp,unread_first_pat_msg_local_id,unread_first_pat_msg_sort_seq,draft,status FROM SessionTable WHERE username='peer'", [], |row| Ok((row.get::<_,i64>(0)?,row.get::<_,i64>(1)?,row.get::<_,i64>(2)?,row.get::<_,i64>(3)?,row.get::<_,i64>(4)?,row.get::<_,String>(5)?,row.get::<_,i64>(6)?))).unwrap();
            assert_eq!(row, (0, 0, 100, 0, 0, "keep draft".into(), 7));
            assert_eq!(
                database
                    .query_row(
                        "SELECT unread_count FROM SessionTable WHERE username='other'",
                        [],
                        |row| row.get::<_, i64>(0)
                    )
                    .unwrap(),
                1
            );
            assert_eq!(
                database
                    .query_row(
                        "SELECT COUNT(*) FROM SessionUnreadListTable_1 WHERE username_id=2",
                        [],
                        |row| row.get::<_, i64>(0)
                    )
                    .unwrap(),
                1
            );
            drop(database);
            let again = mark_read_thread(&location, &keys, "peer", 90, &options).unwrap();
            assert_eq!(
                (
                    again.removed_unread_rows,
                    again.unread_count_after,
                    again.last_clear_unread_timestamp
                ),
                (0, 0, 100)
            );
        }
    }

    #[test]
    fn later_arrivals_and_unknown_newer_counter_contributions_survive() {
        let (_directory, location, keys, options) = fixture(false);
        let database = Connection::open(location.storage.join("session/session.db")).unwrap();
        database.execute_batch("UPDATE SessionTable SET last_timestamp=110 WHERE username='peer'; INSERT INTO SessionUnreadListTable_1(username_id,server_id,local_id,create_time)VALUES(1,3,3,110);").unwrap();
        drop(database);
        let result = mark_read_thread(&location, &keys, "peer", 100, &options).unwrap();
        assert_eq!(
            (result.removed_unread_rows, result.unread_count_after),
            (2, 7)
        );
        let database = Connection::open(location.storage.join("session/session.db")).unwrap();
        assert_eq!(
            database
                .query_row(
                    "SELECT unread_first_msg_srv_id FROM SessionTable WHERE username='peer'",
                    [],
                    |row| row.get::<_, i64>(0)
                )
                .unwrap(),
            3
        );
        drop(database);
        let result = mark_read_thread(&location, &keys, "peer", 110, &options).unwrap();
        assert_eq!(result.unread_count_after, 0);
        assert_eq!(result.last_clear_unread_timestamp, 110);
    }

    #[test]
    fn unknown_session_is_empty_and_unsafe_writes_are_rejected() {
        let (_directory, location, keys, mut options) = fixture(false);
        assert!(
            !mark_read_thread(&location, &keys, "absent", 100, &options)
                .unwrap()
                .session_present
        );
        assert!(mark_read_thread(&location, &keys, "peer", 0, &options).is_err());
        assert!(mark_read_thread(&location, &keys, "peer\n", 100, &options).is_err());
        options.exclusive_ownership_confirmed = false;
        assert!(mark_read_thread(&location, &keys, "peer", 100, &options).is_err());
        options.exclusive_ownership_confirmed = true;
        let mut other_keys = keys.clone();
        other_keys.account_binding = "other".into();
        assert!(mark_read_thread(&location, &other_keys, "peer", 100, &options).is_err());
        let database = Connection::open(location.storage.join("session/session.db")).unwrap();
        database
            .execute_batch(
                "CREATE TRIGGER unexpected AFTER UPDATE ON SessionTable BEGIN SELECT 1; END;",
            )
            .unwrap();
        drop(database);
        assert!(mark_read_thread(&location, &keys, "peer", 100, &options).is_err());
    }

    #[test]
    fn exit_read_skips_missing_hidden_and_every_excluded_builtin() {
        let (_directory, location, keys, options) = fixture(false);
        assert_eq!(
            mark_read_thread_exit(&location, &keys, "absent", 100, &options)
                .unwrap()
                .outcome,
            ThreadExitReadOutcome::MissingSession
        );
        let database = Connection::open(location.storage.join("session/session.db")).unwrap();
        database
            .execute(
                "UPDATE SessionTable SET is_hidden=1 WHERE username='peer'",
                [],
            )
            .unwrap();
        assert_eq!(
            mark_read_thread_exit(&location, &keys, "peer", 100, &options)
                .unwrap()
                .outcome,
            ThreadExitReadOutcome::HiddenSession
        );
        assert_eq!(
            database
                .query_row(
                    "SELECT unread_count FROM SessionTable WHERE username='peer'",
                    [],
                    |row| row.get::<_, i64>(0)
                )
                .unwrap(),
            9
        );
        for peer in EXIT_READ_EXCLUDED_PEERS {
            database.execute("INSERT INTO SessionTable(username,unread_count,is_hidden,last_timestamp,last_clear_unread_timestamp)VALUES(?1,4,0,100,20)", [peer]).unwrap();
            assert_eq!(
                mark_read_thread_exit(&location, &keys, peer, 100, &options)
                    .unwrap()
                    .outcome,
                ThreadExitReadOutcome::ExcludedPeer
            );
            assert_eq!(
                database
                    .query_row(
                        "SELECT unread_count FROM SessionTable WHERE username=?1",
                        [peer],
                        |row| row.get::<_, i64>(0)
                    )
                    .unwrap(),
                4
            );
        }
        database
            .execute(
                "UPDATE SessionTable SET is_hidden=0,last_timestamp=110 WHERE username='peer'",
                [],
            )
            .unwrap();
        database.execute_batch("INSERT INTO SessionUnreadListTable_1(username_id,server_id,local_id,create_time)VALUES(1,10,3,110)").unwrap();
        drop(database);
        let applied = mark_read_thread_exit(&location, &keys, "peer", 100, &options).unwrap();
        assert_eq!(applied.outcome, ThreadExitReadOutcome::Applied);
        let projection = applied.projection.unwrap();
        assert_eq!(
            (
                projection.removed_unread_rows,
                projection.unread_count_after,
                projection.last_clear_unread_timestamp
            ),
            (2, 7, 100)
        );
    }
}