use serde::de::Error;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Notice {
GroupFileUpload(GroupFileUploadEvent),
GroupAdminChange(GroupAdminChangeEvent),
GroupMemberDecrease(GroupMemberDecreaseEvent),
GroupMemberIncrease(GroupMemberIncreaseEvent),
GroupBan(GroupBanEvent),
FriendAdd(FriendAddEvent),
GroupMessageRecall(GroupMessageRecallEvent),
FriendMessageRecall(FriendMessageRecallEvent),
GroupPoke(GroupPokeEvent),
GroupLuckyKing(GroupLuckyKingEvent),
GroupMemberHonorChange(GroupMemberHonorChangeEvent),
FriendInputStatusChange(FriendInputStatusChangeEvent),
GroupEssenceMessageChange(GroupEssenceMessageChangeEvent),
GroupCardChange(GroupCardChangeEvent),
}
impl<'de> Deserialize<'de> for Notice {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = serde_json::Value::deserialize(deserializer)?;
match value["notice_type"].as_str() {
Some("group_upload") => serde_json::from_value(value)
.map(Notice::GroupFileUpload)
.map_err(D::Error::custom),
Some("group_admin") => serde_json::from_value(value)
.map(Notice::GroupAdminChange)
.map_err(D::Error::custom),
Some("group_decrease") => serde_json::from_value(value)
.map(Notice::GroupMemberDecrease)
.map_err(D::Error::custom),
Some("group_increase") => serde_json::from_value(value)
.map(Notice::GroupMemberIncrease)
.map_err(D::Error::custom),
Some("group_ban") => serde_json::from_value(value)
.map(Notice::GroupBan)
.map_err(D::Error::custom),
Some("friend_add") => serde_json::from_value(value)
.map(Notice::FriendAdd)
.map_err(D::Error::custom),
Some("group_recall") => serde_json::from_value(value)
.map(Notice::GroupMessageRecall)
.map_err(D::Error::custom),
Some("friend_recall") => serde_json::from_value(value)
.map(Notice::FriendMessageRecall)
.map_err(D::Error::custom),
Some("essence") => serde_json::from_value(value)
.map(Notice::GroupEssenceMessageChange)
.map_err(D::Error::custom),
Some("group_card") => serde_json::from_value(value)
.map(Notice::GroupCardChange)
.map_err(D::Error::custom),
Some("notify") => match value["sub_type"].as_str() {
Some("poke") => serde_json::from_value(value)
.map(Notice::GroupPoke)
.map_err(D::Error::custom),
Some("lucky_king") => serde_json::from_value(value)
.map(Notice::GroupLuckyKing)
.map_err(D::Error::custom),
Some("honor") => serde_json::from_value(value)
.map(Notice::GroupMemberHonorChange)
.map_err(D::Error::custom),
Some("input_status") => serde_json::from_value(value)
.map(Notice::FriendInputStatusChange)
.map_err(D::Error::custom),
_ => Err(D::Error::custom("Invalid notify sub_type")),
},
_ => Err(D::Error::custom("Invalid notice_type")),
}
}
fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error>
where
D: serde::Deserializer<'de>,
{
let value = serde_json::Value::deserialize(deserializer)?;
*place = match value["notice_type"].as_str() {
Some("group_upload") => serde_json::from_value(value)
.map(Notice::GroupFileUpload)
.map_err(D::Error::custom)?,
Some("group_admin") => serde_json::from_value(value)
.map(Notice::GroupAdminChange)
.map_err(D::Error::custom)?,
Some("group_decrease") => serde_json::from_value(value)
.map(Notice::GroupMemberDecrease)
.map_err(D::Error::custom)?,
Some("group_increase") => serde_json::from_value(value)
.map(Notice::GroupMemberIncrease)
.map_err(D::Error::custom)?,
Some("group_ban") => serde_json::from_value(value)
.map(Notice::GroupBan)
.map_err(D::Error::custom)?,
Some("friend_add") => serde_json::from_value(value)
.map(Notice::FriendAdd)
.map_err(D::Error::custom)?,
Some("group_recall") => serde_json::from_value(value)
.map(Notice::GroupMessageRecall)
.map_err(D::Error::custom)?,
Some("friend_recall") => serde_json::from_value(value)
.map(Notice::FriendMessageRecall)
.map_err(D::Error::custom)?,
Some("essence") => serde_json::from_value(value)
.map(Notice::GroupEssenceMessageChange)
.map_err(D::Error::custom)?,
Some("group_card") => serde_json::from_value(value)
.map(Notice::GroupCardChange)
.map_err(D::Error::custom)?,
Some("notify") => match value["sub_type"].as_str() {
Some("poke") => serde_json::from_value(value)
.map(Notice::GroupPoke)
.map_err(D::Error::custom)?,
Some("lucky_king") => serde_json::from_value(value)
.map(Notice::GroupLuckyKing)
.map_err(D::Error::custom)?,
Some("honor") => serde_json::from_value(value)
.map(Notice::GroupMemberHonorChange)
.map_err(D::Error::custom)?,
Some("input_status") => serde_json::from_value(value)
.map(Notice::FriendInputStatusChange)
.map_err(D::Error::custom)?,
_ => return Err(D::Error::custom("Invalid notify sub_type")),
},
_ => return Err(D::Error::custom("Invalid notice_type")),
};
Ok(())
}
}
impl Serialize for Notice {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
match self {
Notice::GroupFileUpload(m) => m.serialize(serializer),
Notice::GroupAdminChange(m) => m.serialize(serializer),
Notice::GroupMemberDecrease(m) => m.serialize(serializer),
Notice::GroupMemberIncrease(m) => m.serialize(serializer),
Notice::GroupBan(m) => m.serialize(serializer),
Notice::FriendAdd(m) => m.serialize(serializer),
Notice::GroupMessageRecall(m) => m.serialize(serializer),
Notice::FriendMessageRecall(m) => m.serialize(serializer),
Notice::GroupPoke(m) => m.serialize(serializer),
Notice::GroupLuckyKing(m) => m.serialize(serializer),
Notice::GroupMemberHonorChange(m) => m.serialize(serializer),
Notice::FriendInputStatusChange(m) => m.serialize(serializer),
Notice::GroupEssenceMessageChange(m) => m.serialize(serializer),
Notice::GroupCardChange(m) => m.serialize(serializer),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupFileUploadEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub group_id: i64, pub user_id: i64, pub file: GroupFile, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupFile {
pub id: String, pub name: String, pub size: i64, pub busid: i64, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupAdminChangeEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub sub_type: String, pub group_id: i64, pub user_id: i64, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupMemberDecreaseEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub sub_type: String, pub group_id: i64, pub operator_id: i64, pub user_id: i64, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupMemberIncreaseEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub sub_type: String, pub group_id: i64, pub operator_id: i64, pub user_id: i64, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupBanEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub sub_type: String, pub group_id: i64, pub operator_id: i64, pub user_id: i64, pub duration: u64, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct FriendAddEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub user_id: i64, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupMessageRecallEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub group_id: i64, pub user_id: i64, pub operator_id: i64, pub message_id: i64, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct FriendMessageRecallEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub user_id: i64, pub message_id: i64, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupPokeEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub sub_type: String, pub group_id: i64, pub user_id: i64, pub target_id: i64, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupLuckyKingEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub sub_type: String, pub group_id: i64, pub user_id: i64, pub target_id: i64, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupMemberHonorChangeEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub sub_type: String, pub group_id: i64, pub honor_type: String, pub user_id: i64, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct FriendInputStatusChangeEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub sub_type: String, pub status_text: String, pub event_type: u8, pub user_id: i64, }
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InputStatus {
Start,
Typing,
Cleared,
}
impl FriendInputStatusChangeEvent {
pub fn to_status(&self) -> InputStatus {
if !self.status_text.is_empty() {
if self.event_type == 1 {
InputStatus::Start
} else {
InputStatus::Typing
}
} else {
InputStatus::Cleared
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum EssenseMessageChangeType {
#[serde(rename = "add")]
Add,
#[serde(rename = "delete")]
Delete,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupEssenceMessageChangeEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub group_id: i64, pub user_id: i64, pub notice_type: String, pub message_id: i64, pub sender_id: i64, pub sub_type: EssenseMessageChangeType, }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupCardChangeEvent {
pub time: i64, pub self_id: i64, pub post_type: String, pub notice_type: String, pub group_id: i64, pub user_id: i64, pub card_new: String, pub card_old: String, }