This repository has no description
1(*---------------------------------------------------------------------------
2 Copyright (c) 2025 Anil Madhavapeddy. All rights reserved.
3 SPDX-License-Identifier: ISC
4 ---------------------------------------------------------------------------*)
5
6module Rights = struct
7 type t = {
8 may_read_items : bool;
9 may_add_items : bool;
10 may_remove_items : bool;
11 may_set_seen : bool;
12 may_set_keywords : bool;
13 may_create_child : bool;
14 may_rename : bool;
15 may_delete : bool;
16 may_submit : bool;
17 }
18
19 let may_read_items t = t.may_read_items
20 let may_add_items t = t.may_add_items
21 let may_remove_items t = t.may_remove_items
22 let may_set_seen t = t.may_set_seen
23 let may_set_keywords t = t.may_set_keywords
24 let may_create_child t = t.may_create_child
25 let may_rename t = t.may_rename
26 let may_delete t = t.may_delete
27 let may_submit t = t.may_submit
28
29 let make may_read_items may_add_items may_remove_items may_set_seen
30 may_set_keywords may_create_child may_rename may_delete may_submit =
31 { may_read_items; may_add_items; may_remove_items; may_set_seen;
32 may_set_keywords; may_create_child; may_rename; may_delete; may_submit }
33
34 let jsont =
35 let kind = "MailboxRights" in
36 Jsont.Object.map ~kind make
37 |> Jsont.Object.mem "mayReadItems" Jsont.bool ~enc:may_read_items
38 |> Jsont.Object.mem "mayAddItems" Jsont.bool ~enc:may_add_items
39 |> Jsont.Object.mem "mayRemoveItems" Jsont.bool ~enc:may_remove_items
40 |> Jsont.Object.mem "maySetSeen" Jsont.bool ~enc:may_set_seen
41 |> Jsont.Object.mem "maySetKeywords" Jsont.bool ~enc:may_set_keywords
42 |> Jsont.Object.mem "mayCreateChild" Jsont.bool ~enc:may_create_child
43 |> Jsont.Object.mem "mayRename" Jsont.bool ~enc:may_rename
44 |> Jsont.Object.mem "mayDelete" Jsont.bool ~enc:may_delete
45 |> Jsont.Object.mem "maySubmit" Jsont.bool ~enc:may_submit
46 |> Jsont.Object.finish
47end
48
49type role = [
50 | `All
51 | `Archive
52 | `Drafts
53 | `Flagged
54 | `Important
55 | `Inbox
56 | `Junk
57 | `Sent
58 | `Subscribed
59 | `Trash
60 | `Snoozed
61 | `Scheduled
62 | `Memos
63 | `Other of string
64]
65
66let role_to_string = function
67 | `All -> "all"
68 | `Archive -> "archive"
69 | `Drafts -> "drafts"
70 | `Flagged -> "flagged"
71 | `Important -> "important"
72 | `Inbox -> "inbox"
73 | `Junk -> "junk"
74 | `Sent -> "sent"
75 | `Subscribed -> "subscribed"
76 | `Trash -> "trash"
77 | `Snoozed -> "snoozed"
78 | `Scheduled -> "scheduled"
79 | `Memos -> "memos"
80 | `Other s -> s
81
82let role_of_string = function
83 | "all" -> `All
84 | "archive" -> `Archive
85 | "drafts" -> `Drafts
86 | "flagged" -> `Flagged
87 | "important" -> `Important
88 | "inbox" -> `Inbox
89 | "junk" -> `Junk
90 | "sent" -> `Sent
91 | "subscribed" -> `Subscribed
92 | "trash" -> `Trash
93 | "snoozed" -> `Snoozed
94 | "scheduled" -> `Scheduled
95 | "memos" -> `Memos
96 | s -> `Other s
97
98let role_jsont =
99 Jsont.map ~kind:"MailboxRole"
100 ~dec:(fun s -> role_of_string s)
101 ~enc:role_to_string
102 Jsont.string
103
104type t = {
105 id : Proto_id.t;
106 name : string;
107 parent_id : Proto_id.t option;
108 role : role option;
109 sort_order : int64;
110 total_emails : int64;
111 unread_emails : int64;
112 total_threads : int64;
113 unread_threads : int64;
114 my_rights : Rights.t;
115 is_subscribed : bool;
116}
117
118let id t = t.id
119let name t = t.name
120let parent_id t = t.parent_id
121let role t = t.role
122let sort_order t = t.sort_order
123let total_emails t = t.total_emails
124let unread_emails t = t.unread_emails
125let total_threads t = t.total_threads
126let unread_threads t = t.unread_threads
127let my_rights t = t.my_rights
128let is_subscribed t = t.is_subscribed
129
130let make id name parent_id role sort_order total_emails unread_emails
131 total_threads unread_threads my_rights is_subscribed =
132 { id; name; parent_id; role; sort_order; total_emails; unread_emails;
133 total_threads; unread_threads; my_rights; is_subscribed }
134
135let jsont =
136 let kind = "Mailbox" in
137 (* parentId and role can be null - RFC 8621 Section 2 *)
138 let nullable_id = Jsont.(option Proto_id.jsont) in
139 let nullable_role = Jsont.(option role_jsont) in
140 Jsont.Object.map ~kind make
141 |> Jsont.Object.mem "id" Proto_id.jsont ~enc:id
142 |> Jsont.Object.mem "name" Jsont.string ~enc:name
143 |> Jsont.Object.mem "parentId" nullable_id
144 ~dec_absent:None ~enc_omit:Option.is_none ~enc:parent_id
145 |> Jsont.Object.mem "role" nullable_role
146 ~dec_absent:None ~enc_omit:Option.is_none ~enc:role
147 |> Jsont.Object.mem "sortOrder" Proto_int53.Unsigned.jsont ~dec_absent:0L ~enc:sort_order
148 |> Jsont.Object.mem "totalEmails" Proto_int53.Unsigned.jsont ~enc:total_emails
149 |> Jsont.Object.mem "unreadEmails" Proto_int53.Unsigned.jsont ~enc:unread_emails
150 |> Jsont.Object.mem "totalThreads" Proto_int53.Unsigned.jsont ~enc:total_threads
151 |> Jsont.Object.mem "unreadThreads" Proto_int53.Unsigned.jsont ~enc:unread_threads
152 |> Jsont.Object.mem "myRights" Rights.jsont ~enc:my_rights
153 |> Jsont.Object.mem "isSubscribed" Jsont.bool ~enc:is_subscribed
154 |> Jsont.Object.finish
155
156module Filter_condition = struct
157 type t = {
158 parent_id : Proto_id.t option option;
159 name : string option;
160 role : role option option;
161 has_any_role : bool option;
162 is_subscribed : bool option;
163 }
164
165 let make parent_id name role has_any_role is_subscribed =
166 { parent_id; name; role; has_any_role; is_subscribed }
167
168 let jsont =
169 let kind = "MailboxFilterCondition" in
170 (* parentId and role can be absent, null, or have a value - RFC 8621 Section 2.1 *)
171 (* Use opt_mem with Jsont.option to get option option type:
172 - None = field absent (don't filter)
173 - Some None = field present with null (filter for no parent/role)
174 - Some (Some x) = field present with value (filter for specific value) *)
175 let nullable_id = Jsont.(option Proto_id.jsont) in
176 let nullable_role = Jsont.(option role_jsont) in
177 Jsont.Object.map ~kind make
178 |> Jsont.Object.opt_mem "parentId" nullable_id ~enc:(fun f -> f.parent_id)
179 |> Jsont.Object.opt_mem "name" Jsont.string ~enc:(fun f -> f.name)
180 |> Jsont.Object.opt_mem "role" nullable_role ~enc:(fun f -> f.role)
181 |> Jsont.Object.opt_mem "hasAnyRole" Jsont.bool ~enc:(fun f -> f.has_any_role)
182 |> Jsont.Object.opt_mem "isSubscribed" Jsont.bool ~enc:(fun f -> f.is_subscribed)
183 |> Jsont.Object.finish
184end