rosu_v2/request/
comments.rs

1use crate::{
2    model::comments::{CommentBundle, CommentSort},
3    request::{serialize::maybe_comment_sort, Query, Request},
4    routing::Route,
5    Osu,
6};
7
8use serde::Serialize;
9
10/// Get a list of comments and their replies up to two levels deep in form of a
11/// [`CommentBundle`].
12#[must_use = "requests must be configured and executed"]
13#[derive(Serialize)]
14pub struct GetComments<'a> {
15    #[serde(skip)]
16    osu: &'a Osu,
17    commentable_type: Option<String>,
18    commentable_id: Option<u32>,
19    parent_id: Option<u32>,
20    #[serde(serialize_with = "maybe_comment_sort")]
21    sort: Option<CommentSort>,
22    #[serde(rename = "cursor_string")]
23    cursor: Option<&'a str>,
24}
25
26impl<'a> GetComments<'a> {
27    pub(crate) const fn new(osu: &'a Osu) -> Self {
28        Self {
29            osu,
30            commentable_type: None,
31            commentable_id: None,
32            parent_id: None,
33            sort: None,
34            cursor: None,
35        }
36    }
37
38    /// Sort the result by date, newest first
39    #[inline]
40    pub const fn sort_new(mut self) -> Self {
41        self.sort = Some(CommentSort::New);
42
43        self
44    }
45
46    /// Sort the result by vote count
47    #[inline]
48    pub const fn sort_top(mut self) -> Self {
49        self.sort = Some(CommentSort::Top);
50
51        self
52    }
53
54    /// Sort the result by date, oldest first
55    #[inline]
56    pub const fn sort_old(mut self) -> Self {
57        self.sort = Some(CommentSort::Old);
58
59        self
60    }
61
62    /// Limit to comments which are reply to the specified id. Specify 0 to get top level comments
63    #[inline]
64    pub const fn parent(mut self, parent_id: u32) -> Self {
65        self.parent_id = Some(parent_id);
66
67        self
68    }
69
70    /// The id of the resource to get comments for
71    #[inline]
72    pub const fn commentable_id(mut self, commentable_id: u32) -> Self {
73        self.commentable_id = Some(commentable_id);
74
75        self
76    }
77
78    /// The type of resource to get comments for
79    #[inline]
80    pub fn commentable_type(mut self, commentable_type: impl Into<String>) -> Self {
81        self.commentable_type = Some(commentable_type.into());
82
83        self
84    }
85
86    #[inline]
87    pub(crate) const fn cursor(mut self, cursor: &'a str) -> Self {
88        self.cursor = Some(cursor);
89
90        self
91    }
92}
93
94into_future! {
95    |self: GetComments<'_>| -> CommentBundle {
96        Request::with_query(Route::GetComments, Query::encode(&self))
97    }
98}