fishpi-sdk 1.1.0

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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use std::sync::Arc;

use serde_json::{Value, json};
use tokio::sync::Mutex;
use url::Url;

use crate::client::core::FishPi;
use crate::domain::chatroom::{
    BarragerCost, BarragerMsg, ChatContentType, ChatReactionMsg, ChatRoomEvent, ChatRoomEventKind,
    ChatRoomMessageMode, ChatRoomMsg, CustomMsg, MusicMsg, MuteItem, OnlineInfo,
};
use crate::domain::redpacket::RedPacketStatusMsg;
use crate::ids::MessageId;
use crate::utils::build_http_path;
use crate::utils::error::Error;
use crate::wire::chatroom::{
    WireBarragerMsg, WireChatReactionMsg, WireChatRoomMsg, WireChatRoomNode, WireMuteItem,
    WireOnlineInfo, payload_type,
};
use crate::wire::redpacket::WireRedPacketStatusMsg;
use crate::ws::{ParsedMessageHandler, RetryPolicy, WsConnection, WsLogHook, build_ws_url};

type ChatRoomHandler = ParsedMessageHandler<ChatRoomEventKind, ChatRoomEvent>;

fn deser<T: serde::de::DeserializeOwned>(v: &Value) -> Result<T, Error> {
    serde_json::from_value(v.clone()).map_err(|e| Error::decode(e.to_string()))
}

fn parse_chatroom_message(json: &Value) -> Result<(ChatRoomEventKind, ChatRoomEvent), Error> {
    let type_str = json["type"]
        .as_str()
        .ok_or_else(|| Error::decode("Missing type in message"))?;

    match type_str {
        "online" => {
            let users = json["users"]
                .as_array()
                .map(|arr| {
                    arr.iter()
                        .filter_map(|u| deser::<WireOnlineInfo>(u).ok())
                        .map(OnlineInfo::from)
                        .collect()
                })
                .unwrap_or_default();
            let discussing = json["discussing"]
                .as_str()
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty());
            let online_chat_cnt = json["onlineChatCnt"]
                .as_u64()
                .or_else(|| json["onlineCnt"].as_u64())
                .map(|n| n as usize);
            Ok((
                ChatRoomEventKind::Online,
                ChatRoomEvent::Online {
                    users,
                    discussing,
                    online_chat_cnt,
                },
            ))
        }
        "discussChanged" => {
            let d = json["newDiscuss"].as_str().unwrap_or_default().to_string();
            Ok((
                ChatRoomEventKind::DiscussChanged,
                ChatRoomEvent::DiscussChanged(d),
            ))
        }
        "revoke" => {
            let id = json["oId"].as_str().unwrap_or_default().to_string();
            Ok((ChatRoomEventKind::Revoke, ChatRoomEvent::Revoke(id)))
        }
        "msg" => {
            let wire: WireChatRoomMsg = deser(json)?;
            let kind = payload_type(&wire.content);
            let msg = ChatRoomMsg::from(wire);
            match kind.as_deref() {
                Some(t) if t.eq_ignore_ascii_case("music") => Ok((
                    ChatRoomEventKind::Music,
                    ChatRoomEvent::Music(MusicMsg::from_base(msg)),
                )),
                Some(t) if t.eq_ignore_ascii_case("weather") => {
                    Ok((ChatRoomEventKind::Weather, ChatRoomEvent::Weather(msg)))
                }
                _ => Ok((ChatRoomEventKind::Msg, ChatRoomEvent::Msg(msg))),
            }
        }
        "redPacket" => {
            let wire: WireChatRoomMsg = deser(json)?;
            Ok((
                ChatRoomEventKind::RedPacket,
                ChatRoomEvent::RedPacket(ChatRoomMsg::from(wire)),
            ))
        }
        "redPacketStatus" => {
            let wire: WireRedPacketStatusMsg = deser(json)?;
            Ok((
                ChatRoomEventKind::RedPacketStatus,
                ChatRoomEvent::RedPacketStatus(RedPacketStatusMsg::from(wire)),
            ))
        }
        "barrager" => {
            let wire: WireBarragerMsg = deser(json)?;
            Ok((
                ChatRoomEventKind::Barrager,
                ChatRoomEvent::Barrager(BarragerMsg::from(wire)),
            ))
        }
        "customMessage" => {
            let message = json["message"].as_str().unwrap_or_default().to_string();
            Ok((
                ChatRoomEventKind::Custom,
                ChatRoomEvent::Custom(CustomMsg { message }),
            ))
        }
        "chatreaction" => {
            let wire: WireChatReactionMsg = deser(json)?;
            Ok((
                ChatRoomEventKind::ChatReaction,
                ChatRoomEvent::ChatReaction(ChatReactionMsg::from(wire)),
            ))
        }
        other => Err(Error::decode(format!("Unknown message type: {}", other))),
    }
}

/// 聊天室长连接:接收消息、注册监听、缓存在线/话题、断开重连。
pub struct ChatRoomConnection {
    connection: WsConnection,
    handler: ChatRoomHandler,
    url: String,
    node_name: Option<String>,
    discuss: Arc<Mutex<String>>,
    onlines: Arc<Mutex<Vec<OnlineInfo>>>,
}

impl ChatRoomConnection {
    /// 监听在线用户更新(附带话题与在线数),并刷新缓存。
    pub async fn on_online<F>(&self, listener: F)
    where
        F: Fn(Vec<OnlineInfo>, Option<String>, Option<usize>) + Send + Sync + 'static,
    {
        let onlines = Arc::clone(&self.onlines);
        let discuss = Arc::clone(&self.discuss);
        self.add(ChatRoomEventKind::Online, move |e| {
            if let ChatRoomEvent::Online {
                users,
                discussing,
                online_chat_cnt,
            } = e
            {
                if !users.is_empty()
                    && let Ok(mut g) = onlines.try_lock()
                {
                    *g = users.clone();
                }
                if let Some(topic) = discussing.as_ref()
                    && let Ok(mut g) = discuss.try_lock()
                {
                    *g = topic.clone();
                }
                listener(users, discussing, online_chat_cnt);
            }
        })
        .await;
    }

    /// 监听话题变更,并刷新缓存。
    pub async fn on_discuss<F>(&self, listener: F)
    where
        F: Fn(String) + Send + Sync + 'static,
    {
        let discuss = Arc::clone(&self.discuss);
        self.add(ChatRoomEventKind::DiscussChanged, move |e| {
            if let ChatRoomEvent::DiscussChanged(topic) = e {
                if let Ok(mut g) = discuss.try_lock() {
                    *g = topic.clone();
                }
                listener(topic);
            }
        })
        .await;
    }

    /// 监听消息撤回。
    pub async fn on_revoke<F>(&self, listener: F)
    where
        F: Fn(String) + Send + Sync + 'static,
    {
        self.add(ChatRoomEventKind::Revoke, move |e| {
            if let ChatRoomEvent::Revoke(id) = e {
                listener(id);
            }
        })
        .await;
    }

    /// 监听普通消息。
    pub async fn on_msg<F>(&self, listener: F)
    where
        F: Fn(ChatRoomMsg) + Send + Sync + 'static,
    {
        self.add(ChatRoomEventKind::Msg, move |e| {
            if let ChatRoomEvent::Msg(m) = e {
                listener(m);
            }
        })
        .await;
    }

    /// 监听弹幕。
    pub async fn on_barrager<F>(&self, listener: F)
    where
        F: Fn(BarragerMsg) + Send + Sync + 'static,
    {
        self.add(ChatRoomEventKind::Barrager, move |e| {
            if let ChatRoomEvent::Barrager(b) = e {
                listener(b);
            }
        })
        .await;
    }

    /// 监听红包消息。
    pub async fn on_redpacket<F>(&self, listener: F)
    where
        F: Fn(ChatRoomMsg) + Send + Sync + 'static,
    {
        self.add(ChatRoomEventKind::RedPacket, move |e| {
            if let ChatRoomEvent::RedPacket(m) = e {
                listener(m);
            }
        })
        .await;
    }

    /// 监听红包状态。
    pub async fn on_redpacket_status<F>(&self, listener: F)
    where
        F: Fn(RedPacketStatusMsg) + Send + Sync + 'static,
    {
        self.add(ChatRoomEventKind::RedPacketStatus, move |e| {
            if let ChatRoomEvent::RedPacketStatus(s) = e {
                listener(s);
            }
        })
        .await;
    }

    /// 监听音乐消息。
    pub async fn on_music<F>(&self, listener: F)
    where
        F: Fn(MusicMsg) + Send + Sync + 'static,
    {
        self.add(ChatRoomEventKind::Music, move |e| {
            if let ChatRoomEvent::Music(m) = e {
                listener(m);
            }
        })
        .await;
    }

    /// 监听天气消息。
    pub async fn on_weather<F>(&self, listener: F)
    where
        F: Fn(ChatRoomMsg) + Send + Sync + 'static,
    {
        self.add(ChatRoomEventKind::Weather, move |e| {
            if let ChatRoomEvent::Weather(m) = e {
                listener(m);
            }
        })
        .await;
    }

    /// 监听进出场消息。
    pub async fn on_custom<F>(&self, listener: F)
    where
        F: Fn(CustomMsg) + Send + Sync + 'static,
    {
        self.add(ChatRoomEventKind::Custom, move |e| {
            if let ChatRoomEvent::Custom(c) = e {
                listener(c);
            }
        })
        .await;
    }

    /// 监听表态消息。
    pub async fn on_chat_reaction<F>(&self, listener: F)
    where
        F: Fn(ChatReactionMsg) + Send + Sync + 'static,
    {
        self.add(ChatRoomEventKind::ChatReaction, move |e| {
            if let ChatRoomEvent::ChatReaction(r) = e {
                listener(r);
            }
        })
        .await;
    }

    /// 监听全部事件。
    pub async fn on_all<F>(&self, listener: F)
    where
        F: Fn(ChatRoomEvent) + Send + Sync + 'static,
    {
        self.add(ChatRoomEventKind::All, listener).await;
    }

    async fn add<F>(&self, kind: ChatRoomEventKind, listener: F)
    where
        F: Fn(ChatRoomEvent) + Send + Sync + 'static,
    {
        self.handler
            .get_emitter()
            .add_listener(kind, listener)
            .await;
    }

    /// 移除某类监听。
    pub async fn off(&self, kind: ChatRoomEventKind) {
        self.handler.get_emitter().remove_listener(Some(kind)).await;
    }

    /// 当前缓存话题。
    pub async fn discuss(&self) -> String {
        self.discuss.lock().await.clone()
    }

    /// 当前缓存在线人数。
    pub async fn online_count(&self) -> usize {
        self.onlines.lock().await.len()
    }

    /// 所连聊天室节点的中文名(无则 None)。
    pub fn node_name(&self) -> Option<&str> {
        self.node_name.as_deref()
    }

    /// 重连。
    pub async fn reconnect(&mut self) -> Result<(), Error> {
        self.connection
            .reconnect(&self.url, self.handler.clone())
            .await
            .map_err(Error::from)
    }

    /// 设置重连策略。
    pub fn set_retry_policy(&mut self, policy: RetryPolicy) {
        self.connection.set_retry_policy(policy);
    }

    /// 设置日志钩子。
    pub fn set_log_hook<F>(&mut self, hook: F)
    where
        F: Fn(&str) + Send + Sync + 'static,
    {
        let hook = Arc::new(hook) as WsLogHook;
        self.connection.set_log_hook_arc(hook.clone());
        self.handler.set_log_hook_arc(hook);
    }

    /// 断开连接。
    pub fn disconnect(&mut self) {
        self.connection.disconnect();
    }
}

/// 聊天室域访问器。
pub struct ChatRoomApi(pub(crate) FishPi);

impl FishPi {
    /// 聊天室域。
    pub fn chatroom(&self) -> ChatRoomApi {
        ChatRoomApi(self.clone())
    }
}

impl ChatRoomApi {
    /// 解析聊天室节点,返回 (WS URL, 节点中文名)。
    async fn chatroom_ws_url(&self) -> Result<(String, Option<String>), Error> {
        let fallback = || {
            build_ws_url(
                self.0.domain(),
                "chat-room-channel",
                &[("apiKey", self.0.api_key().to_string())],
            )
            .map(|url| (url, None))
            .map_err(Error::from)
        };

        let raw = match self.0.get_raw(&self.0.with_key("chat-room/node/get")).await {
            Ok(v) => v,
            Err(_) => return fallback(),
        };
        let node: WireChatRoomNode = match serde_json::from_value(raw) {
            Ok(n) => n,
            Err(_) => return fallback(),
        };
        let node_name = Some(node.msg.trim().to_string()).filter(|s| !s.is_empty());
        match Url::parse(&node.data) {
            Ok(mut parsed) => {
                if parsed.path().is_empty() {
                    parsed.set_path("/");
                }
                Ok((parsed.to_string(), node_name))
            }
            Err(_) => fallback(),
        }
    }

    /// 建立聊天室长连接。
    pub async fn connect(&self) -> Result<ChatRoomConnection, Error> {
        let (url, node_name) = self.chatroom_ws_url().await?;
        let handler = ChatRoomHandler::new(
            parse_chatroom_message,
            Some(ChatRoomEventKind::All),
            "chatroom",
        );
        let mut connection = WsConnection::new();
        connection
            .connect(false, &url, handler.clone())
            .await
            .map_err(Error::from)?;
        Ok(ChatRoomConnection {
            connection,
            handler,
            url,
            node_name,
            discuss: Arc::new(Mutex::new(String::new())),
            onlines: Arc::new(Mutex::new(Vec::new())),
        })
    }

    /// 发送聊天室消息。
    pub async fn send(&self, content: impl Into<String>) -> Result<(), Error> {
        self.0
            .post_raw(
                "chat-room/send",
                &json!({ "content": content.into(), "client": self.0.client_tag() }),
            )
            .await?;
        Ok(())
    }

    /// 设置聊天室话题。
    pub async fn set_discuss(&self, discuss: impl Into<String>) -> Result<(), Error> {
        self.send(format!("[setdiscuss]{}[/setdiscuss]", discuss.into()))
            .await
    }

    /// 查询聊天室历史消息。
    pub async fn history(
        &self,
        page: u32,
        content_type: ChatContentType,
    ) -> Result<Vec<ChatRoomMsg>, Error> {
        let url = build_http_path(
            "chat-room/more",
            &[
                ("page", page.to_string()),
                ("type", content_type.as_str().to_string()),
                ("apiKey", self.0.api_key().to_string()),
            ],
        );
        let rsp = self.0.get_raw(&url).await?;
        Ok(collect_msgs(&rsp))
    }

    /// 获取指定消息附近的消息。
    pub async fn msg_around(
        &self,
        msg_id: impl Into<MessageId>,
        mode: ChatRoomMessageMode,
        size: u32,
        content_type: ChatContentType,
    ) -> Result<Vec<ChatRoomMsg>, Error> {
        let url = build_http_path(
            "chat-room/getMessage",
            &[
                ("oId", msg_id.into().into_string()),
                ("mode", mode.as_str().to_string()),
                ("size", size.to_string()),
                ("type", content_type.as_str().to_string()),
                ("apiKey", self.0.api_key().to_string()),
            ],
        );
        let rsp = self.0.get_raw(&url).await?;
        Ok(collect_msgs(&rsp))
    }

    /// 撤回聊天室消息,返回服务端提示。
    pub async fn revoke(&self, msg_id: impl Into<MessageId>) -> Result<String, Error> {
        let rsp = self
            .0
            .delete_raw(&format!("chat-room/revoke/{}", msg_id.into()), &json!({}))
            .await?;
        Ok(rsp["msg"].as_str().unwrap_or_default().to_string())
    }

    /// 发送弹幕,返回服务端提示。
    pub async fn barrager(
        &self,
        content: impl Into<String>,
        color: Option<&str>,
    ) -> Result<String, Error> {
        let color = color.unwrap_or("#ffffff");
        let tag = format!(
            "[barrager]{{\"color\":\"{}\",\"content\":\"{}\"}}[/barrager]",
            color,
            content.into()
        );
        let rsp = self
            .0
            .post_raw("chat-room/send", &json!({ "content": tag }))
            .await?;
        Ok(rsp["msg"].as_str().unwrap_or("弹幕发送成功").to_string())
    }

    /// 获取弹幕花费。
    pub async fn barrage_cost(&self) -> Result<BarragerCost, Error> {
        let rsp = self
            .0
            .get_raw(&self.0.with_key("chat-room/barrager/get"))
            .await?;
        let text = rsp["data"].as_str().unwrap_or("5积分").trim();
        let cost = text.trim_end_matches("积分").parse::<u32>().unwrap_or(0);
        Ok(BarragerCost {
            cost,
            unit: "积分".to_string(),
        })
    }

    /// 获取禁言成员列表(思过崖)。
    pub async fn mutes(&self) -> Result<Vec<MuteItem>, Error> {
        let rsp = self.0.get_raw("chat-room/si-guo-list").await?;
        Ok(rsp["data"]
            .as_array()
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| serde_json::from_value::<WireMuteItem>(v.clone()).ok())
                    .map(MuteItem::from)
                    .collect()
            })
            .unwrap_or_default())
    }

    /// 获取消息原文(去掉 HTML 注释尾巴)。
    pub async fn raw_message(&self, msg_id: impl Into<MessageId>) -> Result<String, Error> {
        let text = self
            .0
            .get_text(&format!("cr/raw/{}", msg_id.into()))
            .await?;
        Ok(text.split("<!--").next().unwrap_or("").trim().to_string())
    }
}

fn collect_msgs(rsp: &Value) -> Vec<ChatRoomMsg> {
    rsp["data"]
        .as_array()
        .map(|arr| {
            arr.iter()
                .filter_map(|v| serde_json::from_value::<WireChatRoomMsg>(v.clone()).ok())
                .map(ChatRoomMsg::from)
                .collect()
        })
        .unwrap_or_default()
}