fishpi-sdk 0.1.7

A Rust SDK for interacting with the FishPi community API
Documentation
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! # 用户 API 模块
//!
//! 这个模块提供了与用户相关的 API 操作,包括获取用户信息、查询表情、活跃度、签到、转账、关注、修改头像和用户信息等功能。
//! 主要结构体是 `User`,用于管理用户的 API 请求。
//!
//! ## 主要组件
//!
//! - [`User`] - 用户客户端结构体,负责所有用户相关的 API 调用。
//!
//! ## 方法列表
//!
//! - [`User::new`] - 创建新的用户客户端实例。
//! - [`User::get_token`] - 获取当前 API token。
//! - [`User::set_token`] - 重新设置请求 token。
//! - [`User::is_logined`] - 检查用户是否已登录(API key 是否为空)。
//! - [`User::info`] - 返回登录账户信息。
//! - [`User::emotions`] - 查询登录用户常用表情。
//! - [`User::liveness`] - 查询登录用户当前活跃度。
//! - [`User::is_checkin`] - 检查用户是否已经签到。
//! - [`User::is_collected_liveness`] - 检查用户是否领取昨日活跃奖励。
//! - [`User::reward_liveness`] - 领取昨日活跃度奖励。
//! - [`User::transfer`] - 转账。
//! - [`User::follow`] - 关注用户。
//! - [`User::unfollow`] - 取消关注用户。
//! - [`User::update_avatar`] - 修改用户头像。
//! - [`User::update_user_info`] - 修改用户信息。
//! - [`User::get_user`] - 获取其他用户信息。
//! - [`User::report`] - 举报。
//! - [`User::upload`] - 上传文件。
//! - [`User::get_points`] - 获取用户积分。
//!
//! ## 示例
//!
//! ```rust,no_run
//! use fishpi_sdk::{FishPi, model::misc::LoginData};
//! use fishpi_sdk::model::user::UpdateUserInfoParams;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // 登录获取用户客户端
//!     let login_data = LoginData::new("your_name_or_email", "your_password", None);
//!     let user = FishPi::login(&login_data).await?;
//!
//!     // 获取用户信息
//!     let info = user.info().await?;
//!     println!("User name: {}", info.name());
//!
//!     // 查询表情
//!     let emotions = user.emotions().await?;
//!     println!("Emotions: {:?}", emotions);
//!
//!     // 转账
//!     user.transfer("target_user", 100, "Gift").await?;
//!
//!     // 修改用户信息
//!     let params = UpdateUserInfoParams {
//!         nickName: Some("New Name".to_string()),
//!         userUrl: Some("https://example.com".to_string()),
//!         userIntro: Some("New intro".to_string()),
//!         userTag: Some("tag".to_string()),
//!         mbti: None,
//!     };
//!     user.update_user_info(params).await?;
//!
//!     Ok(())
//! }
//! ```
use crate::api::article::Article;
use crate::api::breezemoon::BreezeMoon;
use crate::api::chat::Chat;
use crate::api::chatroom::ChatRoom;
use crate::api::comment::Comment;
use crate::api::notice::Notice;
use crate::api::redpacket::Redpacket;
use crate::model::misc::{Report, UploadResult};
use crate::model::user::{UpdateUserInfoParams, UserInfo, UserPoint};
use crate::utils::error::Error;
use crate::utils::{ResponseResult, build_http_path, get, post, upload_files};
use serde_json::{Value, json};

pub struct User {
    api_key: String,
    pub chatroom: ChatRoom,
    pub chat: Chat,
    pub breezemoon: BreezeMoon,
    pub article: Article,
    pub notice: Notice,
    pub redpacket: Redpacket,
    pub comment: Comment,
}

impl User {
    pub fn new(api_key: String) -> Self {
        Self {
            api_key: api_key.clone(),
            chatroom: ChatRoom::new(api_key.clone()),
            chat: Chat::new(api_key.clone()),
            breezemoon: BreezeMoon::new(api_key.clone()),
            article: Article::new(api_key.clone()),
            notice: Notice::new(api_key.clone()),
            redpacket: Redpacket::new(api_key.clone()),
            comment: Comment::new(api_key.clone()),
        }
    }

    pub fn get_token(&self) -> &str {
        &self.api_key
    }

    /// 重新设置请求token
    pub fn set_token(&mut self, token: String) {
        self.api_key = token.clone();
        self.chatroom.set_api_key(token.clone());
        self.chat = Chat::new(token.clone());
        self.breezemoon = BreezeMoon::new(token.clone());
        self.article = Article::new(token.clone());
        self.notice = Notice::new(token.clone());
        self.redpacket = Redpacket::new(token.clone());
        self.comment = Comment::new(token);
    }

    pub fn is_logined(&self) -> bool {
        !self.api_key.is_empty()
    }

    /// 返回登录账户信息,需要先登录或设置有效api_key
    pub async fn info(&self) -> Result<UserInfo, Error> {
        let mut resp = get(&build_http_path(
            "api/user",
            &[("apiKey", self.api_key.clone())],
        ))
        .await?;

        if resp["code"] != 0 {
            return Err(Error::Api(
                resp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        let data_value = if let Some(data_str) = resp["data"].as_str() {
            serde_json::from_str(data_str).map_err(|e| Error::Api(e.to_string()))?
        } else {
            resp["data"].take()
        };

        UserInfo::from_value(&data_value)
    }

    /// 查询登录用户常用表情
    pub async fn emotions(&self) -> Result<Vec<String>, Error> {
        let mut resp = get(&build_http_path(
            "users/emotions",
            &[("apiKey", self.api_key.clone())],
        ))
        .await?;

        if resp["code"] != 0 {
            return Err(Error::Api(
                resp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        let data: Vec<Value> = serde_json::from_value(resp["data"].take())
            .map_err(|e| Error::Parse(format!("Failed to parse emotions: {}", e)))?;
        let emotions: Vec<String> = data
            .into_iter()
            .filter_map(|v| {
                v.as_object()
                    .and_then(|obj| obj.values().next())
                    .and_then(|val| val.as_str())
                    .map(|s| s.to_string())
            })
            .collect();
        Ok(emotions)
    }

    /// 查询登录用户当前活跃度,请求频率请至少 10 分钟一次
    pub async fn liveness(&self) -> Result<u32, Error> {
        let resp = get(&build_http_path(
            "user/liveness",
            &[("apiKey", self.api_key.clone())],
        ))
        .await?;

        let liveness_raw = resp
            .get("liveness")
            .and_then(|v| {
                v.as_f64()
                    .or_else(|| v.as_u64().map(|n| n as f64))
                    .or_else(|| v.as_i64().map(|n| n as f64))
                    .or_else(|| v.as_str().and_then(|s| s.parse::<f64>().ok()))
            })
            .ok_or_else(|| Error::Api("Missing or invalid liveness".to_string()))?;

        Ok(liveness_raw.max(0.0).round() as u32)
    }

    /// 检查用户是否已经签到
    pub async fn is_checkin(&self) -> Result<bool, Error> {
        let resp = get(&build_http_path(
            "user/isCheckin",
            &[("apiKey", self.api_key.clone())],
        ))
        .await?;

        let is_checkin: bool = resp["isCheckin"].as_bool().unwrap_or(false);
        Ok(is_checkin)
    }

    /// 检查用户是否领取昨日活跃奖励
    pub async fn is_collected_liveness(&self) -> Result<bool, Error> {
        let resp = get(&build_http_path(
            "api/activity/is-collected-liveness",
            &[("apiKey", self.api_key.clone())],
        ))
        .await?;

        let is_rewarded: bool = resp["isLivenessRewarded"].as_bool().unwrap_or(false);
        Ok(is_rewarded)
    }

    /// 领取昨日活跃度奖励
    pub async fn reward_liveness(&self) -> Result<u32, Error> {
        let resp = get(&build_http_path(
            "activity/yesterday-liveness-reward-api",
            &[("apiKey", self.api_key.clone())],
        ))
        .await?;

        let success: u32 = resp["sum"].as_u64().unwrap_or(0) as u32;
        Ok(success)
    }

    /// 转账
    pub async fn transfer(&self, username: &str, amount: u32, memo: &str) -> Result<bool, Error> {
        let data = json!({
            "username": username,
            "amount": amount,
            "memo": memo,
            "apiKey": self.api_key,
        });

        let resp = post("point/transfer", Some(data)).await?;

        if resp["code"] != 0 {
            return Err(Error::Api(
                resp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        Ok(true)
    }

    /// 关注用户
    pub async fn follow(&self, following_id: &str) -> Result<bool, Error> {
        let data = json!({
            "followingId": following_id,
            "apiKey": self.api_key,
        });

        let resp = post("follow/user", Some(data)).await?;

        if resp["code"] != 0 {
            return Err(Error::Api(
                resp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        Ok(true)
    }

    /// 取消关注用户
    pub async fn unfollow(&self, following_id: &str) -> Result<bool, Error> {
        let data = json!({
            "followingId": following_id,
            "apiKey": self.api_key,
        });

        let resp = post("unfollow/user", Some(data)).await?;

        if resp["code"] != 0 {
            return Err(Error::Api(
                resp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        Ok(true)
    }

    /// 修改用户头像
    pub async fn update_avatar(&self, avatar_url: &str) -> Result<bool, Error> {
        let data = json!({
            "userAvatarURL": avatar_url,
            "apiKey": self.api_key
        });

        let resp = post("api/settings/avatar", Some(data)).await?;

        if resp["code"] != 0 {
            return Err(Error::Api(
                resp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        Ok(true)
    }

    /// 修改用户信息
    ///
    /// #### 参数
    /// * `params` 用户信息参数 [UpdateUserInfoParams]
    pub async fn update_user_info(&self, params: UpdateUserInfoParams) -> Result<bool, Error> {
        let data = json!({
            "userNickname": params.nickName,
            "userURL": params.userUrl,
            "userIntro": params.userIntro,
            "userTag": params.userTag,
            "apiKey": self.api_key,
        });

        let resp = post("api/settings/profiles", Some(data)).await?;

        if resp["code"] != 0 {
            return Err(Error::Api(
                resp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        Ok(true)
    }

    /// 获取用户信息
    ///
    /// - `username` 用户名
    ///
    /// 返回用户信息
    pub async fn get_user(&self, username: &str) -> Result<UserInfo, Error> {
        let url = build_http_path(
            &format!("user/{}", username),
            &[("apiKey", self.api_key.clone())],
        );

        let rsp = get(&url).await?;

        if rsp.get("code").and_then(|c| c.as_i64()).unwrap_or(0) != 0 {
            return Err(Error::Api(
                rsp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        UserInfo::from_value(&rsp)
    }

    /// 举报
    ///
    /// - `data` 举报数据 [Report]
    ///
    /// 返回举报结果
    pub async fn report(&self, data: &Report) -> Result<ResponseResult, Error> {
        let url = "report".to_string();

        let mut data_json = serde_json::to_value(data)
            .map_err(|e| Error::Parse(format!("Failed to serialize Report: {}", e)))?;
        data_json["apiKey"] = Value::String(self.api_key.clone());

        let rsp = post(&url, Some(data_json)).await?;

        ResponseResult::from_value(&rsp)
    }

    /// 上传文件
    ///
    /// - `files` 文件路径列表
    ///
    /// 返回上传结果
    pub async fn upload(&self, files: Vec<String>) -> Result<UploadResult, Error> {
        // 检查文件是否存在
        for file in &files {
            if !std::path::Path::new(file).exists() {
                return Err(Error::Api(format!("File not exist: {}", file)));
            }
        }

        let url = "upload".to_string();
        let rsp = upload_files(&url, files, &self.api_key).await?;

        if rsp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
            return Err(Error::Api(
                rsp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        UploadResult::from_value(&rsp["data"])
    }

    /// 获取用户积分
    ///
    /// - `username` 用户名
    ///   返回用户积分信息 [UserPoint]
    pub async fn get_points(&self, username: &str) -> Result<UserPoint, Error> {
        let resp = get(&format!("user/{}/point", username)).await?;

        if resp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
            return Err(Error::Api(
                resp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        UserPoint::from_value(&resp)
    }
}