use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
use serde::{ Deserialize, Serialize };
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ModifyRelationResponseData;
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub enum RelationAction {
Follow = 1,
Unfollow = 2,
Whisper = 3,
Unwhisper = 4,
Blacklist = 5,
Unblacklist = 6,
KickFan = 7,
}
#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub enum RelationSource {
MonthlyCharge = 1,
Space = 11,
Video = 14,
Comment = 15,
VideoEndPage = 17,
H5Recommend = 58,
H5FollowingList = 106,
H5FanList = 107,
Article = 115,
Message = 118,
Search = 120,
VideoPlayerButton = 164,
H5CommonFollow = 167,
CreativeIncentive = 192,
ActivityPage = 222,
JointVideo = 229,
MessageCenterLike = 235,
VideoPlayerDanmaku = 245,
}
impl BpiClient {
pub async fn user_modify_relation(
&self,
fid: u64,
action: RelationAction,
source: Option<RelationSource>
) -> Result<BpiResponse<()>, BpiError> {
let csrf = self.csrf()?;
let mut form = reqwest::multipart::Form
::new()
.text("fid", fid.to_string())
.text("act", (action as u8).to_string())
.text("csrf", csrf.to_string());
if let Some(s) = source {
form = form.text("re_src", (s as u32).to_string());
}
self
.post("https://api.bilibili.com/x/relation/modify")
.multipart(form)
.send_bpi("操作用户关系").await
}
}
#[cfg(test)]
mod tests {
use super::*;
use tracing::info;
const TEST_FID: u64 = 2;
#[tokio::test]
async fn test_modify_relation_follow() -> Result<(), BpiError> {
let bpi = BpiClient::new();
let resp = bpi.user_modify_relation(
TEST_FID,
RelationAction::Follow,
Some(RelationSource::Space)
).await?;
info!("关注用户结果: {:?}", resp);
Ok(())
}
#[tokio::test]
async fn test_modify_relation_unfollow() -> Result<(), BpiError> {
let bpi = BpiClient::new();
let resp = bpi.user_modify_relation(TEST_FID, RelationAction::Unfollow, None).await?;
info!("取关用户结果: {:?}", resp);
Ok(())
}
#[tokio::test]
async fn test_modify_relation_blacklist() -> Result<(), BpiError> {
let bpi = BpiClient::new();
let resp = bpi.user_modify_relation(TEST_FID, RelationAction::Blacklist, None).await?;
info!("拉黑用户结果: {:?}", resp);
let _ = bpi.user_modify_relation(TEST_FID, RelationAction::Unblacklist, None).await;
Ok(())
}
}