magnetar/src/model/data/note.rs

84 lines
2.8 KiB
Rust

use magnetar_calckey_model::ck;
use magnetar_sdk::types::note::Reaction;
use magnetar_sdk::types::user::PackUserBase;
use magnetar_sdk::types::{
drive::PackDriveFileBase,
emoji::EmojiContext,
note::{NoteAttachmentExt, NoteBase, NoteVisibility, PackReactionBase, ReactionBase},
user::UserBase,
};
use magnetar_sdk::{Packed, Required};
use crate::model::{PackType, PackingContext};
impl PackType<&ck::note_reaction::Model> for ReactionBase {
fn extract(context: &PackingContext, reaction: &ck::note_reaction::Model) -> Self {
ReactionBase {
created_at: reaction.created_at.into(),
user_id: reaction.user_id.clone(),
reaction: Reaction::guess_from(
&reaction.reaction,
&context.instance_info.default_reaction,
)
.unwrap_or_else(|| /* Shouldn't happen */ Reaction::Unicode("👍".to_string())),
}
}
}
pub struct NoteBaseSource<'a> {
pub note: &'a ck::note::Model,
pub reactions: &'a Vec<PackReactionBase>,
pub emojis: &'a EmojiContext,
pub user: &'a UserBase,
}
impl PackType<NoteBaseSource<'_>> for NoteBase {
fn extract(
_context: &PackingContext,
NoteBaseSource {
note,
reactions,
emojis,
user,
}: NoteBaseSource<'_>,
) -> Self {
use ck::sea_orm_active_enums::NoteVisibilityEnum as NVE;
NoteBase {
created_at: note.created_at.into(),
cw: note.cw.clone(),
uri: note.uri.clone(),
url: note.url.clone(),
text: note.text.clone().unwrap_or_default(),
visibility: match note.visibility {
NVE::Followers => NoteVisibility::Followers,
NVE::Hidden => NoteVisibility::Direct,
NVE::Public => NoteVisibility::Public,
NVE::Home => NoteVisibility::Home,
NVE::Specified => NoteVisibility::Direct,
},
user: Box::new(PackUserBase::pack_from((
Required(note.user_id.as_str().into()),
Required(user.clone()),
))),
parent_note_id: note.reply_id.clone(),
renoted_note_id: note.renote_id.clone(),
reply_count: note.replies_count as u64,
renote_count: note.renote_count as u64,
hashtags: note.tags.clone(),
reactions: reactions.clone(),
emojis: emojis.clone(),
local_only: note.local_only,
has_poll: note.has_poll,
file_ids: note.file_ids.clone(),
}
}
}
impl PackType<&Vec<PackDriveFileBase>> for NoteAttachmentExt {
fn extract(_context: &PackingContext, data: &Vec<PackDriveFileBase>) -> Self {
NoteAttachmentExt {
attachments: data.clone(),
}
}
}