1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use s2rs_derive::Forwarder;
use super::utils::ResponseUtils;
use super::{Api, utils::RequestBuilderUtils};
use crate::cursor::Cursor;
use crate::json;

// region: Message
pub struct Message {
    pub id: u64,
    pub created_at: String,
    pub actor_name: String,
    pub actor_id: u64,
    pub event: MessageEvent,
    pub event_type: String,
}

#[derive(Forwarder, Debug, Clone)]
pub enum MessageParseError {
    #[forward] Expected(json::ExpectedError),
    #[forward] Event(MessageEventParseError)
}

impl json::Parsable for Message {
    type Error = MessageParseError;
    fn parse(data: &json::Parser) -> Result<Self, Self::Error> {
        Ok(Self {
            id: data.i("id").u64()?,
            created_at: data.i("datetime_created").string()?,
            actor_name: data.i("actor_username").string()?,
            actor_id: data.i("actor_id").u64()?,
            event: data.parse()?,
            event_type: data.i("type").string()?,
        })
    }
}
// endregion: Message

// region: MessageCommentLocation
pub enum MessageCommentLocation {
    Profile,
    Project,
    Studio
}

impl MessageCommentLocation {
    pub fn from_u8(value: u8) -> Option<Self> {
        Some(match value {
            0 => Self::Project,
            1 => Self::Profile,
            2 => Self::Studio,
            _ => None?
        })
    }
}
// endregion: MessageCommentLocation

// region: MessageEvent
pub enum MessageEvent {
    FollowUser {
        to_id: u64,
        to_name: String,
    },
    LoveProject {
        id: u64,
        title: String,
    },
    FavoriteProject {
        id: u64,
        title: String,
    },
    RemixProject {
        id: u64,
        title: String,
        parent_id: u64,
        parent_title: String,
    },
    AddComment {
        location: MessageCommentLocation,
        location_type: u8,
        location_id: u64,
        location_title: String,
        id: u64,
        fragment: String,
        to_name: Option<String>,
    },
    InviteCurator {
        id: u64,
        title: String,
    },
    PromoteStudio {
        id: u64,
        title: String,
    },
    StudioActivity {
        id: u64,
        title: String,
    },
    ForumPost {
        id: u64,
        title: String,
    },
    Welcome
}

#[derive(Forwarder, Debug, Clone)]
pub enum MessageEventParseError {
    #[forward] Expected(json::ExpectedError),
    #[forward] CommentLocation(u8),
    InvalidType(String)
}

impl json::Parsable for MessageEvent {
    type Error = MessageEventParseError;
    fn parse(data: &json::Parser) -> Result<Self, Self::Error> {
        Ok(match data.i("type").string()?.as_str() {
            "followuser" => Self::FollowUser {
                to_id: data.i("followed_user_id").u64()?,
                to_name: data.i("followed_username").string()?,
            },
            "loveproject" => Self::LoveProject {
                id: data.i("project_id").u64()?,
                title: data.i("title").string()?
            },
            "favoriteproject" => Self::FavoriteProject {
                id: data.i("project_id").u64()?,
                title: data.i("project_title").string()?,
            },
            "remixproject" => Self::RemixProject {
                id: data.i("project_id").u64()?,
                title: data.i("title").string()?,
                parent_id: data.i("parent_id").u64()?,
                parent_title: data.i("parent_title").string()?
            },
            "addcomment" => {
                let location_type = data.i("comment_type").u8()?;
                Self::AddComment {
                    location: MessageCommentLocation::from_u8(location_type).ok_or(MessageEventParseError::CommentLocation(location_type))?,
                    location_type,
                    location_id: data.i("comment_obj_id").u64()?,
                    location_title: data.i("comment_obj_title").string()?,
                    id: data.i("comment_id").u64()?,
                    fragment: data.i("comment_fragment").string()?,
                    to_name: data.try_i("commentee_username")?,
                }
            },
            "curatorinvite" => Self::InviteCurator {
                id: data.i("gallery_id").u64()?,
                title: data.i("title").string()?,
            },
            "becomeownerstudio" => Self::PromoteStudio {
                id: data.i("gallery_id").u64()?,
                title: data.i("title").string()?,
            },
            "studioactivity" => Self::StudioActivity {
                id: data.i("gallery_id").u64()?,
                title: data.i("title").string()?,
            },
            "forumpost" => Self::ForumPost {
                id: data.i("topic_id").u64()?,
                title: data.i("topic_title").string()?,
            },
            t => Err(MessageEventParseError::InvalidType(t.to_owned()))?
        })
    }
}
// endregion: MessageEvent

#[derive(Forwarder, Debug)]
pub enum GetUserMessagesError {
    #[forward] This(super::Error),
    #[forward] Parsing(MessageParseError)
}

impl Api {
    pub async fn user_messages(&self, name: &str, cursor: impl Into<Cursor>) -> Result<Vec<Message>, GetUserMessagesError> {
        let response = self.get(&format!["users/{name}/messages/"]).cursor(cursor).send_success().await?;
        response.json_parser_vec().await
    }
}