Skip to main content

gitpoulpi_hosting/conversation/
caps.rs

1//! What a provider lets the conversation do.
2
3use serde::{Deserialize, Serialize};
4
5use super::model::{ItemKind, MarkupFlavor};
6
7/// What a control may be offered for, per provider and item kind. Every
8/// `false` is a real "Unsupported" and hides the matching control.
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct ConversationCaps {
11    pub read: bool,
12    pub comment: bool,
13    pub reply_in_thread: bool,
14    pub start_thread: bool,
15    pub edit_own: bool,
16    pub delete_own: bool,
17    pub resolve: bool,
18    pub reactions: bool,
19    pub events: bool,
20    pub internal_notes: bool,
21    pub read_flavor: MarkupFlavor,
22    pub write_flavor: MarkupFlavor,
23    pub max_body: Option<usize>,
24}
25
26impl ConversationCaps {
27    /// Nothing: the answer for a provider or kind without a conversation.
28    pub const NONE: Self = Self {
29        read: false,
30        comment: false,
31        reply_in_thread: false,
32        start_thread: false,
33        edit_own: false,
34        delete_own: false,
35        resolve: false,
36        reactions: false,
37        events: false,
38        internal_notes: false,
39        read_flavor: MarkupFlavor::PlainText,
40        write_flavor: MarkupFlavor::PlainText,
41        max_body: None,
42    };
43
44    /// GitHub: issues have a flat comment list, pull requests also threads.
45    pub fn github(kind: ItemKind) -> Self {
46        Self {
47            read: true,
48            comment: true,
49            reply_in_thread: kind == ItemKind::PullRequest,
50            start_thread: false,
51            edit_own: true,
52            delete_own: true,
53            resolve: kind == ItemKind::PullRequest,
54            reactions: true,
55            events: true,
56            internal_notes: false,
57            read_flavor: MarkupFlavor::Gfm,
58            write_flavor: MarkupFlavor::Gfm,
59            max_body: Some(65_536),
60        }
61    }
62
63    /// Gitea and Forgejo: a request has code threads (resolution only on
64    /// Gitea), an issue a flat list; no reactions are read, no internal notes.
65    pub fn gitea(kind: ItemKind, can_resolve: bool) -> Self {
66        let pr = kind == ItemKind::PullRequest;
67        Self {
68            read: true,
69            comment: true,
70            reply_in_thread: pr,
71            start_thread: false,
72            edit_own: true,
73            delete_own: true,
74            resolve: pr && can_resolve,
75            reactions: false,
76            events: true,
77            internal_notes: false,
78            read_flavor: MarkupFlavor::GiteaMd,
79            write_flavor: MarkupFlavor::GiteaMd,
80            max_body: None,
81        }
82    }
83
84    /// Azure DevOps. A pull request has threads (a reply is a comment of the
85    /// thread; the thread status is shown but not changed, so `resolve` is
86    /// off). A work item has a flat
87    /// comment list with no threads and no event history here. Azure DevOps
88    /// Server (7.0) takes work item comments as HTML, so it is written as
89    /// HTML and shown as the Markdown converted from it.
90    pub fn azure_devops(kind: ItemKind, server: bool) -> Self {
91        let pr = kind == ItemKind::PullRequest;
92        Self {
93            read: true,
94            comment: true,
95            reply_in_thread: pr,
96            start_thread: false,
97            edit_own: true,
98            delete_own: true,
99            resolve: false,
100            reactions: false,
101            events: pr,
102            internal_notes: false,
103            read_flavor: MarkupFlavor::AzureMd,
104            write_flavor: if pr || !server {
105                MarkupFlavor::AzureMd
106            } else {
107                MarkupFlavor::Html
108            },
109            max_body: Some(150_000),
110        }
111    }
112
113    /// Bitbucket Cloud and Data Center: pull requests only (Cloud retired
114    /// its issue tracker; Data Center has none). A resolved thread is shown
115    /// but not changed (`resolve` off); no reactions, no internal notes.
116    pub fn bitbucket(kind: ItemKind) -> Self {
117        if kind == ItemKind::Issue {
118            return Self::NONE;
119        }
120        Self {
121            read: true,
122            comment: true,
123            reply_in_thread: true,
124            start_thread: false,
125            edit_own: true,
126            delete_own: true,
127            resolve: false,
128            reactions: false,
129            events: true,
130            internal_notes: false,
131            read_flavor: MarkupFlavor::BitbucketMd,
132            write_flavor: MarkupFlavor::BitbucketMd,
133            max_body: None,
134        }
135    }
136
137    /// Gerrit: changes only. Messages and comments cannot be edited or
138    /// deleted by their author (only an administrator can delete a change
139    /// message); a reply is posted at once into the comment's thread.
140    pub fn gerrit(kind: ItemKind) -> Self {
141        if kind == ItemKind::Issue {
142            return Self::NONE;
143        }
144        Self {
145            read: true,
146            comment: true,
147            reply_in_thread: true,
148            start_thread: false,
149            edit_own: false,
150            delete_own: false,
151            resolve: true,
152            reactions: false,
153            events: true,
154            internal_notes: false,
155            read_flavor: MarkupFlavor::PlainText,
156            write_flavor: MarkupFlavor::PlainText,
157            max_body: None,
158        }
159    }
160
161    /// GitLab: issues and merge requests both have discussions.
162    pub fn gitlab(kind: ItemKind) -> Self {
163        Self {
164            read: true,
165            comment: true,
166            reply_in_thread: true,
167            start_thread: false,
168            edit_own: true,
169            delete_own: true,
170            resolve: kind == ItemKind::PullRequest,
171            reactions: false,
172            events: true,
173            internal_notes: true,
174            read_flavor: MarkupFlavor::GitlabMd,
175            write_flavor: MarkupFlavor::GitlabMd,
176            max_body: Some(1_000_000),
177        }
178    }
179
180    /// An issue tracker: its items are issues only, so a pull request has
181    /// no conversation here.
182    fn tracker(kind: ItemKind, caps: Self) -> Self {
183        if kind == ItemKind::Issue {
184            caps
185        } else {
186            Self::NONE
187        }
188    }
189
190    /// Jira (Cloud and Data Center): a flat comment list, edit and delete
191    /// of one's own, the changelog as events. Comments are read as wiki
192    /// markup (shown as Markdown); `write_flavor` is what goes on the wire:
193    /// the user types Markdown and it is converted to wiki. Internal
194    /// comments need a project role the profile does not know, so they
195    /// cannot be posted.
196    pub fn jira(kind: ItemKind) -> Self {
197        Self::tracker(
198            kind,
199            Self {
200                read: true,
201                comment: true,
202                edit_own: true,
203                delete_own: true,
204                events: true,
205                read_flavor: MarkupFlavor::Gfm,
206                write_flavor: MarkupFlavor::JiraWiki,
207                max_body: Some(32_767),
208                ..Self::NONE
209            },
210        )
211    }
212
213    /// Linear: comments reply to a parent, the issue history is the events.
214    pub fn linear(kind: ItemKind) -> Self {
215        Self::tracker(
216            kind,
217            Self {
218                read: true,
219                comment: true,
220                reply_in_thread: true,
221                edit_own: true,
222                delete_own: true,
223                events: true,
224                read_flavor: MarkupFlavor::Gfm,
225                write_flavor: MarkupFlavor::Gfm,
226                ..Self::NONE
227            },
228        )
229    }
230
231    /// YouTrack: a flat comment list. The activity stream is not read, so
232    /// there are no events.
233    pub fn youtrack(kind: ItemKind) -> Self {
234        Self::tracker(
235            kind,
236            Self {
237                read: true,
238                comment: true,
239                edit_own: true,
240                delete_own: true,
241                read_flavor: MarkupFlavor::YoutrackMd,
242                write_flavor: MarkupFlavor::YoutrackMd,
243                ..Self::NONE
244            },
245        )
246    }
247
248    /// Redmine: journals are the comments and the events; private notes
249    /// exist; a comment can be edited (Redmine 5+) but never deleted.
250    /// `textile`: the server formats text as Textile, so what the user
251    /// types as Markdown is converted for it.
252    pub fn redmine(kind: ItemKind, textile: bool) -> Self {
253        Self::tracker(
254            kind,
255            Self {
256                read: true,
257                comment: true,
258                edit_own: true,
259                events: true,
260                internal_notes: true,
261                read_flavor: MarkupFlavor::Gfm,
262                write_flavor: if textile {
263                    MarkupFlavor::Textile
264                } else {
265                    MarkupFlavor::Gfm
266                },
267                ..Self::NONE
268            },
269        )
270    }
271
272    /// Trello: the comments of a card, the moves between lists as events.
273    pub fn trello(kind: ItemKind) -> Self {
274        Self::tracker(
275            kind,
276            Self {
277                read: true,
278                comment: true,
279                edit_own: true,
280                delete_own: true,
281                events: true,
282                read_flavor: MarkupFlavor::TrelloMd,
283                write_flavor: MarkupFlavor::TrelloMd,
284                max_body: Some(16_384),
285                ..Self::NONE
286            },
287        )
288    }
289
290    /// A folder of Markdown notes: comments live in the note file itself.
291    /// No network, no events, and only the local author's comments can be
292    /// edited or deleted.
293    pub fn local_markdown(kind: ItemKind) -> Self {
294        Self::tracker(
295            kind,
296            Self {
297                read: true,
298                comment: true,
299                edit_own: true,
300                delete_own: true,
301                read_flavor: MarkupFlavor::ObsidianMd,
302                write_flavor: MarkupFlavor::ObsidianMd,
303                ..Self::NONE
304            },
305        )
306    }
307}