Elffuss BraidHub

utopia-ia/elffuss-braid public

356f55034aff · 9.9 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! The inbox (section 21): what the hub tells an account about the threads
5//! it takes part in or watches, and about its own requests to the hub;
6//! marking notifications read or done; and following or muting one thread.
7//! The inbox is all there is: the hub sends no e-mail.
8//!
9//! A notification names a repository, a thread by its kind and number, who
10//! did what, and why the account was told. A thread's title is read when
11//! the inbox is listed, and only from a repository the caller may read
12//! then. Times are RFC 3339, in UTC; logins are lowercase.
13
14use serde::{Deserialize, Serialize};
15
16use crate::cards::RepoRef;
17
18/// The account was mentioned (`@login`) in the thread.
19pub const MENTION: &str = "mention";
20
21/// The account was asked to review the change request.
22pub const REVIEW_REQUESTED: &str = "review_requested";
23
24/// The thread is assigned to the account.
25pub const ASSIGNED: &str = "assigned";
26
27/// The account reviewed the change request.
28pub const REVIEW: &str = "review";
29
30/// The account opened the thread.
31pub const AUTHOR: &str = "author";
32
33/// The account commented on the thread.
34pub const COMMENT: &str = "comment";
35
36/// The thread was closed or opened again.
37pub const STATE: &str = "state";
38
39/// The change request landed.
40pub const LANDED: &str = "landed";
41
42/// The account watches all of the repository, or follows the thread.
43pub const SUBSCRIBED: &str = "subscribed";
44
45/// A notice of the hub about the account's own requests, such as a fork
46/// made or a grant to renew, or about the default branch of a repository
47/// it administers or landed on: [`verify_red`], [`verify_fixed`] and
48/// [`verify_stuck`] (section 25), whose `url` is the repository's
49/// verification page. The reason of each notice is this one, whatever its
50/// code.
51///
52/// [`verify_red`]: crate::verify::NOTICE_RED
53/// [`verify_fixed`]: crate::verify::NOTICE_FIXED
54/// [`verify_stuck`]: crate::verify::NOTICE_STUCK
55pub const SYSTEM: &str = "system";
56
57/// Why an account is told about a thread, strongest first: a notification
58/// gives the strongest of the reasons it has, until it is read.
59pub const REASONS: [&str; 9] = [
60 MENTION,
61 REVIEW_REQUESTED,
62 ASSIGNED,
63 REVIEW,
64 AUTHOR,
65 COMMENT,
66 STATE,
67 LANDED,
68 SUBSCRIBED,
69];
70
71/// A notification about an issue.
72pub const ISSUE: &str = "issue";
73
74/// A notification about a change request.
75pub const REQUEST: &str = "request";
76
77/// What a thread subscription says when the account chose to follow the
78/// thread.
79pub const MANUAL: &str = "manual";
80
81/// What a thread subscription says when the account muted the thread.
82pub const MUTED: &str = "muted";
83
84/// What a thread subscription says when the account takes part in the
85/// thread.
86pub const PARTICIPATING: &str = "participating";
87
88/// What a thread subscription says when the account watches all of the
89/// repository.
90pub const WATCHING: &str = "watching";
91
92/// What a thread subscription says when the account ignores the
93/// repository, which silences every thread of it.
94pub const IGNORING: &str = "ignoring";
95
96/// The thread a notification is about.
97#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
98pub struct NotificationThread {
99 /// `issue` or `request`.
100 pub kind: String,
101 /// Its number in its repository.
102 pub number: u64,
103 /// Its title, as it is now.
104 pub title: String,
105}
106
107/// A notification.
108#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
109pub struct Notification {
110 /// Its id: what marking it read or done names.
111 pub id: u64,
112 /// The repository it is about, if any.
113 #[serde(default)]
114 pub repo: Option<RepoRef>,
115 /// The thread it is about; `None` for a notice of the hub.
116 #[serde(default)]
117 pub thread: Option<NotificationThread>,
118 /// Why the account was told: one of [`REASONS`], or [`SYSTEM`].
119 pub reason: String,
120 /// Who did what it reports last; `None` for the hub itself.
121 #[serde(default)]
122 pub actor: Option<String>,
123 /// What happened last: in a thread, `opened`, `commented`, `closed`,
124 /// `reopened`, `assigned`, `mentioned`, `review_requested`, `reviewed`
125 /// or `landed`; in a notice of the hub, what it says, such as
126 /// `fork_done`, or `verify_red`, `verify_fixed` and `verify_stuck` for
127 /// a repository's default branch.
128 #[serde(default)]
129 pub code: Option<String>,
130 /// When it last changed.
131 pub updated: String,
132 /// Whether it waits to be read.
133 pub unread: bool,
134 /// The page of the hub it leads to.
135 pub url: String,
136}
137
138/// `GET /v1/notifications`: a page of the inbox, newest first.
139#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
140pub struct Notifications {
141 /// The notifications.
142 pub notifications: Vec<Notification>,
143 /// How many of the caller's notifications wait to be read, of the
144 /// repository asked for, if one was.
145 pub unread: u64,
146 /// The cursor of the next page, or `None` on the last one.
147 #[serde(default)]
148 pub next: Option<String>,
149}
150
151/// `POST /v1/notifications/read`: which notifications to mark read. Either
152/// `ids`, or any of `all`, `before` and `repo`, which mark every
153/// notification that matches them.
154#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
155pub struct MarkRead {
156 /// These notifications, by id.
157 #[serde(default, skip_serializing_if = "Vec::is_empty")]
158 pub ids: Vec<u64>,
159 /// Every notification.
160 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
161 pub all: bool,
162 /// Those that last changed at or before this time: what a client
163 /// showed, and nothing that came since.
164 #[serde(default, skip_serializing_if = "Option::is_none")]
165 pub before: Option<String>,
166 /// Those of this repository, `owner/name`.
167 #[serde(default, skip_serializing_if = "Option::is_none")]
168 pub repo: Option<String>,
169}
170
171/// The answer of `POST /v1/notifications/read`.
172#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
173pub struct Marked {
174 /// How many notifications it marked read that were unread.
175 pub marked: u64,
176}
177
178/// `GET …/threads/{n}/subscription`, the answer of `PUT`, and its body,
179/// where `reason` is left out: whether the caller is told about a thread.
180#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
181pub struct ThreadSubscription {
182 /// Whether the thread notifies the caller.
183 pub subscribed: bool,
184 /// Why: [`MANUAL`], [`MUTED`], [`PARTICIPATING`], [`WATCHING`] or
185 /// [`IGNORING`]; absent when nothing ties the caller to the thread.
186 #[serde(default, skip_serializing_if = "Option::is_none")]
187 pub reason: Option<String>,
188}
189
190#[cfg(test)]
191mod tests {
192 use super::*;
193
194 #[test]
195 fn notifications_on_the_wire() {
196 let page = Notifications {
197 notifications: vec![
198 Notification {
199 id: 7,
200 repo: Some(RepoRef {
201 owner: "ada".into(),
202 name: "demo".into(),
203 }),
204 thread: Some(NotificationThread {
205 kind: ISSUE.into(),
206 number: 4,
207 title: "A title".into(),
208 }),
209 reason: MENTION.into(),
210 actor: Some("bob".into()),
211 code: Some("commented".into()),
212 updated: "2026-10-18T12:00:00Z".into(),
213 unread: true,
214 url: "https://hub.example/ada/demo/issues/4".into(),
215 },
216 Notification {
217 id: 8,
218 reason: SYSTEM.into(),
219 code: Some("fork_failed".into()),
220 updated: "2026-10-18T12:00:00Z".into(),
221 url: "https://hub.example/notifications".into(),
222 ..Notification::default()
223 },
224 ],
225 unread: 1,
226 next: Some("n1.7".into()),
227 };
228 let text = serde_json::to_string(&page).unwrap();
229 assert_eq!(serde_json::from_str::<Notifications>(&text).unwrap(), page);
230 // A notice of the hub names no thread and no actor.
231 let notice: serde_json::Value = serde_json::from_str(&text).unwrap();
232 assert!(notice["notifications"][1]["thread"].is_null());
233 assert!(notice["notifications"][1]["actor"].is_null());
234 assert_eq!(REASONS[0], MENTION);
235 assert!(!REASONS.contains(&SYSTEM));
236 // A notice of a red branch names its repository, and leads to the
237 // page of its verification.
238 let red: Notification = serde_json::from_str(
239 r#"{"id":9,"repo":{"owner":"ada","name":"demo"},"thread":null,"reason":"system","actor":null,"code":"verify_red","updated":"2026-10-22T12:03:00Z","unread":true,"url":"https://hub.example/ada/demo/verify"}"#,
240 )
241 .unwrap();
242 assert_eq!(red.reason, SYSTEM);
243 assert_eq!(red.code.as_deref(), Some(crate::verify::NOTICE_RED));
244 assert!(red.thread.is_none() && red.url.ends_with("/verify"));
245 }
246
247 #[test]
248 fn marking_and_subscriptions_on_the_wire() {
249 let ids = MarkRead {
250 ids: vec![1, 2],
251 ..MarkRead::default()
252 };
253 assert_eq!(serde_json::to_string(&ids).unwrap(), r#"{"ids":[1,2]}"#);
254 let all: MarkRead = serde_json::from_str(r#"{"all":true}"#).unwrap();
255 assert!(all.all && all.ids.is_empty() && all.repo.is_none());
256 assert_eq!(serde_json::to_string(&all).unwrap(), r#"{"all":true}"#);
257 let body: ThreadSubscription = serde_json::from_str(r#"{"subscribed":false}"#).unwrap();
258 assert_eq!(body.reason, None);
259 let muted = ThreadSubscription {
260 subscribed: false,
261 reason: Some(MUTED.into()),
262 };
263 assert_eq!(
264 serde_json::to_string(&muted).unwrap(),
265 r#"{"subscribed":false,"reason":"muted"}"#
266 );
267 }
268}