Skip to main content

imessage_db/imessage/
types.rs

1/// Query parameter types for the iMessage database.
2use std::collections::HashMap;
3
4use serde::Deserialize;
5use serde_json::Value;
6
7/// A single WHERE clause item.
8/// The `statement` is a SQL fragment with named parameters (`:name` or `:...name` for arrays).
9/// The `args` map binds parameter names to values.
10#[derive(Debug, Clone, Deserialize)]
11pub struct WhereClause {
12    pub statement: String,
13    #[serde(default)]
14    pub args: HashMap<String, Value>,
15}
16
17/// Parameters for querying messages.
18#[derive(Debug, Clone)]
19pub struct MessageQueryParams {
20    pub chat_guid: Option<String>,
21    pub offset: i64,
22    pub limit: i64,
23    pub after: Option<i64>,
24    pub before: Option<i64>,
25    pub with_chats: bool,
26    pub with_chat_participants: bool,
27    pub with_attachments: bool,
28    pub sort: SortOrder,
29    pub order_by: String,
30    pub where_clauses: Vec<WhereClause>,
31}
32
33impl Default for MessageQueryParams {
34    fn default() -> Self {
35        Self {
36            chat_guid: None,
37            offset: 0,
38            limit: 100,
39            after: None,
40            before: None,
41            with_chats: false,
42            with_chat_participants: false,
43            with_attachments: true,
44            sort: SortOrder::Desc,
45            order_by: "message.date".to_string(),
46            where_clauses: vec![],
47        }
48    }
49}
50
51/// Parameters for querying updated messages.
52#[derive(Debug, Clone)]
53pub struct UpdatedMessageQueryParams {
54    pub chat_guid: Option<String>,
55    pub offset: i64,
56    pub limit: i64,
57    pub after: Option<i64>,
58    pub before: Option<i64>,
59    pub with_chats: bool,
60    pub with_attachments: bool,
61    pub include_created: bool,
62    pub sort: SortOrder,
63}
64
65impl Default for UpdatedMessageQueryParams {
66    fn default() -> Self {
67        Self {
68            chat_guid: None,
69            offset: 0,
70            limit: 100,
71            after: None,
72            before: None,
73            with_chats: false,
74            with_attachments: true,
75            include_created: false,
76            sort: SortOrder::Desc,
77        }
78    }
79}
80
81/// Parameters for querying chats.
82#[derive(Debug, Clone)]
83pub struct ChatQueryParams {
84    pub chat_guid: Option<String>,
85    pub glob_guid: bool,
86    pub with_participants: bool,
87    pub with_last_message: bool,
88    pub with_archived: bool,
89    pub offset: i64,
90    pub limit: Option<i64>,
91    pub order_by: String,
92}
93
94impl Default for ChatQueryParams {
95    fn default() -> Self {
96        Self {
97            chat_guid: None,
98            glob_guid: false,
99            with_participants: true,
100            with_last_message: false,
101            with_archived: true,
102            offset: 0,
103            limit: None,
104            order_by: "chat.ROWID".to_string(),
105        }
106    }
107}
108
109/// Parameters for querying handles.
110#[derive(Debug, Clone)]
111pub struct HandleQueryParams {
112    pub address: Option<String>,
113    pub offset: i64,
114    pub limit: i64,
115}
116
117impl Default for HandleQueryParams {
118    fn default() -> Self {
119        Self {
120            address: None,
121            offset: 0,
122            limit: 1000,
123        }
124    }
125}
126
127/// Parameters for counting messages.
128#[derive(Debug, Clone, Default)]
129pub struct MessageCountParams {
130    pub after: Option<i64>,
131    pub before: Option<i64>,
132    pub is_from_me: bool,
133    pub chat_guid: Option<String>,
134    pub updated: bool,
135    pub min_row_id: Option<i64>,
136    pub max_row_id: Option<i64>,
137    pub where_clauses: Vec<WhereClause>,
138}
139
140/// Sort direction.
141#[derive(Debug, Clone, Copy, PartialEq, Eq)]
142pub enum SortOrder {
143    Asc,
144    Desc,
145}
146
147impl SortOrder {
148    pub fn as_sql(&self) -> &'static str {
149        match self {
150            SortOrder::Asc => "ASC",
151            SortOrder::Desc => "DESC",
152        }
153    }
154}
155
156impl std::fmt::Display for SortOrder {
157    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
158        f.write_str(self.as_sql())
159    }
160}