Skip to main content

gitpoulpi_hosting/conversation/
model.rs

1//! The provider-neutral conversation shapes.
2
3use serde::{Deserialize, Serialize};
4
5use crate::review::model::{DiffSide, ReviewState};
6
7/// Whether an item is an issue or a pull / merge request.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9#[serde(rename_all = "snake_case")]
10pub enum ItemKind {
11    Issue,
12    PullRequest,
13}
14
15/// Which item. `number` is GitHub's number or GitLab's `iid`; `id` carries a
16/// provider's own key (Jira key, Trello card id, Linear uuid) and is empty
17/// where the number is enough.
18#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
19pub struct ItemKey {
20    pub kind: ItemKind,
21    pub number: u64,
22    #[serde(default)]
23    pub id: String,
24}
25
26impl ItemKey {
27    pub fn issue(number: u64) -> Self {
28        Self {
29            kind: ItemKind::Issue,
30            number,
31            id: String::new(),
32        }
33    }
34
35    pub fn pull_request(number: u64) -> Self {
36        Self {
37            kind: ItemKind::PullRequest,
38            number,
39            id: String::new(),
40        }
41    }
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
45#[serde(rename_all = "snake_case")]
46pub enum ActorKind {
47    User,
48    Bot,
49    System,
50    #[default]
51    Unknown,
52}
53
54/// Who wrote a comment or caused an event.
55#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
56pub struct Actor {
57    pub login: String,
58    /// The name to show: the full name when the host gives one, else the login.
59    pub display: String,
60    pub kind: ActorKind,
61    pub web_url: Option<String>,
62}
63
64impl Actor {
65    pub fn new(login: impl Into<String>, display: impl Into<String>, kind: ActorKind) -> Self {
66        Self {
67            login: login.into(),
68            display: display.into(),
69            kind,
70            web_url: None,
71        }
72    }
73
74    /// The actor of a deleted account or an anonymous author.
75    pub fn unknown() -> Self {
76        Self::new("ghost", "ghost", ActorKind::Unknown)
77    }
78}
79
80/// The dialect a text is written in.
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
82#[serde(rename_all = "snake_case")]
83pub enum MarkupFlavor {
84    #[default]
85    Gfm,
86    GitlabMd,
87    GiteaMd,
88    CommonMark,
89    AzureMd,
90    BitbucketMd,
91    Html,
92    JiraWiki,
93    Textile,
94    YoutrackMd,
95    TrelloMd,
96    PlainText,
97    ObsidianMd,
98}
99
100#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
101pub struct Markup {
102    pub text: String,
103    pub flavor: MarkupFlavor,
104}
105
106impl Markup {
107    pub fn new(text: impl Into<String>, flavor: MarkupFlavor) -> Self {
108        Self {
109            text: text.into(),
110            flavor,
111        }
112    }
113}
114
115/// One reaction kind and how many people gave it (`+1`, `heart`).
116#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
117pub struct Reaction {
118    pub name: String,
119    pub count: u32,
120}
121
122#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
123pub struct Comment {
124    pub id: String,
125    pub author: Actor,
126    pub body: Markup,
127    pub created_at: String,
128    pub updated_at: Option<String>,
129    /// Edited after it was posted.
130    pub edited: bool,
131    pub reactions: Vec<Reaction>,
132    /// The comment this answers (a thread's first comment).
133    pub in_reply_to: Option<String>,
134    /// The host's discussion id, what `reply_in_discussion` takes.
135    pub discussion_id: Option<String>,
136    /// Only visible to project members (GitLab internal notes).
137    pub internal: bool,
138    /// What the host says about the token's rights; `None` = it does not say.
139    pub can_edit: Option<bool>,
140    pub can_delete: Option<bool>,
141    pub web_url: Option<String>,
142}
143
144#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
145#[serde(tag = "kind", rename_all = "snake_case")]
146pub enum EventKind {
147    Opened,
148    Closed,
149    Reopened,
150    Merged,
151    Labeled {
152        added: Vec<String>,
153        removed: Vec<String>,
154    },
155    Assigned {
156        added: Vec<String>,
157        removed: Vec<String>,
158    },
159    Renamed {
160        from: String,
161        to: String,
162    },
163    CommitsPushed {
164        count: u32,
165        head: Option<String>,
166    },
167    CrossReferenced {
168        from: String,
169        url: Option<String>,
170    },
171    StatusChanged {
172        from: String,
173        to: String,
174    },
175    ReviewRequested {
176        who: String,
177    },
178    /// Anything else, with the host's own wording.
179    Other {
180        text: String,
181    },
182}
183
184#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
185pub struct Event {
186    pub id: String,
187    pub actor: Option<Actor>,
188    pub at: String,
189    pub kind: EventKind,
190}
191
192/// A submitted review: the verdict and its summary.
193#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
194pub struct ReviewEntry {
195    pub id: String,
196    pub author: Actor,
197    pub state: ReviewState,
198    pub body: Markup,
199    pub submitted_at: String,
200}
201
202/// A conversation on code: `ReviewThread` with actors, markup and the hunk.
203#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
204pub struct CodeThread {
205    pub id: String,
206    pub path: String,
207    pub line: Option<u32>,
208    pub original_line: Option<u32>,
209    pub side: DiffSide,
210    pub start_line: Option<u32>,
211    pub resolved: bool,
212    pub outdated: bool,
213    pub resolvable: bool,
214    /// The diff excerpt the thread is anchored on, when the host gives it.
215    pub diff_hunk: Option<String>,
216    /// Oldest first.
217    pub comments: Vec<Comment>,
218}
219
220#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
221#[serde(tag = "entry", rename_all = "snake_case")]
222pub enum TimelineEntry {
223    Comment(Comment),
224    Review(ReviewEntry),
225    Thread(CodeThread),
226    Event(Event),
227}
228
229impl TimelineEntry {
230    /// When it happened (a thread: when it was opened).
231    pub fn at(&self) -> &str {
232        match self {
233            Self::Comment(c) => &c.created_at,
234            Self::Review(r) => &r.submitted_at,
235            Self::Thread(t) => t.comments.first().map_or("", |c| c.created_at.as_str()),
236            Self::Event(e) => &e.at,
237        }
238    }
239}
240
241#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
242pub struct ItemHeader {
243    pub key: ItemKey,
244    /// What a human types to reference it (`#12`, `!7`).
245    pub reference: String,
246    pub title: String,
247    /// `open`, `closed`, `merged`...
248    pub state_label: String,
249    pub open: bool,
250    pub author: Actor,
251    pub body: Markup,
252    pub created_at: String,
253    pub web_url: String,
254    pub labels: Vec<String>,
255}
256
257/// Opaque: where the window before the one just read starts.
258#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
259pub struct PageCursor(pub String);
260
261#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
262pub struct TimelinePage {
263    pub header: ItemHeader,
264    /// Oldest to newest.
265    pub entries: Vec<TimelineEntry>,
266    /// The cursor of the window before this one; `None` at the start.
267    pub older: Option<PageCursor>,
268    /// About how many entries the conversation holds, when known.
269    pub total_hint: Option<u32>,
270}
271
272#[derive(Debug, Clone, Copy, PartialEq, Eq)]
273pub struct TimelineOpts {
274    /// Include system events (labels, state changes, pushes...).
275    pub events: bool,
276}
277
278impl Default for TimelineOpts {
279    fn default() -> Self {
280        Self { events: true }
281    }
282}