magnetar/magnetar_sdk/src/types/note.rs

134 lines
3.5 KiB
Rust

use crate::types::emoji::EmojiContext;
use crate::types::user::PackUserBase;
use crate::types::{Id, MmXml};
use crate::{Packed, Required};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use ts_rs::TS;
use crate::types::drive::PackDriveFileBase;
use magnetar_sdk_macros::pack;
#[derive(Copy, Clone, Default, Debug, Deserialize, Serialize, TS)]
#[ts(export)]
pub struct NoteListFilter {
show_renotes: Option<bool>,
show_replies: Option<bool>,
show_files_only: Option<bool>,
uncwed_sensitive: Option<bool>,
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, TS)]
#[ts(export)]
pub enum NoteVisibility {
Public,
Home,
Followers,
Direct,
}
#[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[ts(export)]
pub struct PollChoice {
pub title: String,
pub votes_count: u64,
pub voted: bool,
}
#[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[ts(export)]
pub struct PollBase {
pub expires_at: DateTime<Utc>,
pub expired: bool,
pub multiple_choice: bool,
pub options: Vec<PollChoice>,
}
pack!(PackPollBase, Required<Id> as id & Required<PollBase> as poll);
#[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[ts(export)]
pub struct NoteBase {
pub created_at: DateTime<Utc>,
pub cw: Option<String>,
pub cw_mm: Option<MmXml>,
pub uri: Option<String>,
pub url: Option<String>,
pub text: String,
pub text_mm: Option<MmXml>,
pub visibility: NoteVisibility,
pub user: Box<PackUserBase>,
pub parent_note_id: Option<String>,
pub renoted_note_id: Option<String>,
pub reply_count: u64,
pub renote_count: u64,
pub hashtags: Vec<String>,
pub reactions: Vec<PackReactionBase>,
pub local_only: bool,
pub has_poll: bool,
pub file_ids: Vec<String>,
pub emojis: EmojiContext,
}
pack!(PackNoteBase, Required<Id> as id & Required<NoteBase> as note);
#[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[ts(export)]
pub struct NoteAttachmentExt {
pub poll: Option<PackPollBase>,
pub attachments: Vec<PackDriveFileBase>,
}
pack!(
PackNoteWithMaybeAttachments,
Required<Id> as id & Required<NoteBase> as note & Option<NoteAttachmentExt> as attachment
);
#[derive(Clone, Debug, Deserialize, Serialize, TS)]
pub struct NoteDetailExt {
pub parent_note: Option<Box<PackNoteWithMaybeAttachments>>,
pub renoted_note: Option<Box<PackNoteWithMaybeAttachments>>,
}
pack!(
PackNoteMaybeFull,
Required<Id> as id & Required<NoteBase> as note & Option<NoteAttachmentExt> as attachment & Option<NoteDetailExt> as detail
);
#[derive(Clone, Debug, Deserialize, Serialize, TS)]
pub enum Reaction {
Unicode(String),
Shortcode(String),
}
impl Reaction {
pub fn guess_from(s: &str, default_emote: &str) -> Option<Self> {
let code = s.trim();
match code.chars().next() {
Some(':') => Some(Reaction::Shortcode(code.to_string())),
Some(_) => Some(Reaction::Unicode(code.to_string())),
None => Self::guess_from(default_emote, "👍"),
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize, TS)]
pub struct ReactionBase {
pub created_at: DateTime<Utc>,
pub user_id: String,
pub reaction: Reaction,
}
pack!(PackReactionBase, Required<Id> as id & Required<ReactionBase> as reaction);
#[derive(Clone, Debug, Deserialize, Serialize, TS)]
pub struct ReactionUserExt {
pub user: Box<PackUserBase>,
}
pack!(
PackReactionWithUser,
Required<Id> as id & Required<ReactionBase> as reaction & Required<ReactionUserExt> as user
);