Backend: Prep for MMM parser processing

This commit is contained in:
Natty 2023-10-26 21:37:15 +02:00
parent 42e68fffcd
commit fc86f0e29c
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
5 changed files with 81 additions and 30 deletions

View File

@ -29,6 +29,11 @@ impl From<&str> for Id {
} }
} }
#[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[ts(export)]
#[repr(transparent)]
pub struct MmXml(String);
#[derive(Copy, Clone, Debug, Deserialize, Serialize, TS)] #[derive(Copy, Clone, Debug, Deserialize, Serialize, TS)]
#[ts(export)] #[ts(export)]
pub enum NotificationType { pub enum NotificationType {

View File

@ -1,6 +1,6 @@
use crate::types::emoji::EmojiContext; use crate::types::emoji::EmojiContext;
use crate::types::user::PackUserBase; use crate::types::user::PackUserBase;
use crate::types::Id; use crate::types::{Id, MmXml};
use crate::{Packed, Required}; use crate::{Packed, Required};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -52,9 +52,11 @@ pack!(PackPollBase, Required<Id> as id & Required<PollBase> as poll);
pub struct NoteBase { pub struct NoteBase {
pub created_at: DateTime<Utc>, pub created_at: DateTime<Utc>,
pub cw: Option<String>, pub cw: Option<String>,
pub cw_mm: Option<MmXml>,
pub uri: Option<String>, pub uri: Option<String>,
pub url: Option<String>, pub url: Option<String>,
pub text: String, pub text: String,
pub text_mm: MmXml,
pub visibility: NoteVisibility, pub visibility: NoteVisibility,
pub user: Box<PackUserBase>, pub user: Box<PackUserBase>,
pub parent_note_id: Option<String>, pub parent_note_id: Option<String>,
@ -63,10 +65,10 @@ pub struct NoteBase {
pub renote_count: u64, pub renote_count: u64,
pub hashtags: Vec<String>, pub hashtags: Vec<String>,
pub reactions: Vec<PackReactionBase>, pub reactions: Vec<PackReactionBase>,
pub emojis: EmojiContext,
pub local_only: bool, pub local_only: bool,
pub has_poll: bool, pub has_poll: bool,
pub file_ids: Vec<String>, pub file_ids: Vec<String>,
pub emojis: EmojiContext,
} }
pack!(PackNoteBase, Required<Id> as id & Required<NoteBase> as note); pack!(PackNoteBase, Required<Id> as id & Required<NoteBase> as note);

View File

@ -1,5 +1,5 @@
use crate::types::emoji::EmojiContext; use crate::types::emoji::EmojiContext;
use crate::types::{Id, NotificationSettings}; use crate::types::{Id, MmXml, NotificationSettings};
use crate::{Packed, Required}; use crate::{Packed, Required};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -29,6 +29,7 @@ pub enum SpeechTransform {
pub struct ProfileField { pub struct ProfileField {
name: String, name: String,
value: String, value: String,
value_mm: Option<MmXml>,
verified_at: Option<DateTime<Utc>>, verified_at: Option<DateTime<Utc>>,
} }
@ -38,6 +39,7 @@ pub struct UserBase {
pub acct: String, pub acct: String,
pub username: String, pub username: String,
pub display_name: String, pub display_name: String,
pub display_name_mm: MmXml,
pub host: Option<String>, pub host: Option<String>,
pub speech_transform: SpeechTransform, pub speech_transform: SpeechTransform,
pub created_at: DateTime<Utc>, pub created_at: DateTime<Utc>,
@ -61,6 +63,7 @@ pub struct UserProfileExt {
pub is_suspended: bool, pub is_suspended: bool,
pub description: Option<String>, pub description: Option<String>,
pub description_mm: Option<MmXml>,
pub location: Option<String>, pub location: Option<String>,
pub birthday: Option<DateTime<Utc>>, pub birthday: Option<DateTime<Utc>>,
pub fields: Vec<ProfileField>, pub fields: Vec<ProfileField>,
@ -79,6 +82,7 @@ pub struct UserProfileExt {
pub banner_blurhash: Option<String>, pub banner_blurhash: Option<String>,
pub has_public_reactions: bool, pub has_public_reactions: bool,
pub emojis: EmojiContext,
} }
#[derive(Clone, Debug, Deserialize, Serialize, TS)] #[derive(Clone, Debug, Deserialize, Serialize, TS)]

View File

@ -6,6 +6,7 @@ use magnetar_sdk::types::{
emoji::EmojiContext, emoji::EmojiContext,
note::{NoteAttachmentExt, NoteBase, NoteVisibility, PackReactionBase, ReactionBase}, note::{NoteAttachmentExt, NoteBase, NoteVisibility, PackReactionBase, ReactionBase},
user::UserBase, user::UserBase,
MmXml,
}; };
use magnetar_sdk::{Packed, Required}; use magnetar_sdk::{Packed, Required};
@ -27,9 +28,11 @@ impl PackType<&ck::note_reaction::Model> for ReactionBase {
pub struct NoteBaseSource<'a> { pub struct NoteBaseSource<'a> {
pub note: &'a ck::note::Model, pub note: &'a ck::note::Model,
pub cw_mm: Option<&'a MmXml>,
pub text_mm: &'a MmXml,
pub reactions: &'a Vec<PackReactionBase>, pub reactions: &'a Vec<PackReactionBase>,
pub emojis: &'a EmojiContext,
pub user: &'a UserBase, pub user: &'a UserBase,
pub emoji_context: &'a EmojiContext,
} }
impl PackType<NoteBaseSource<'_>> for NoteBase { impl PackType<NoteBaseSource<'_>> for NoteBase {
@ -37,18 +40,22 @@ impl PackType<NoteBaseSource<'_>> for NoteBase {
_context: &PackingContext, _context: &PackingContext,
NoteBaseSource { NoteBaseSource {
note, note,
text_mm,
cw_mm,
reactions, reactions,
emojis,
user, user,
emoji_context,
}: NoteBaseSource<'_>, }: NoteBaseSource<'_>,
) -> Self { ) -> Self {
use ck::sea_orm_active_enums::NoteVisibilityEnum as NVE; use ck::sea_orm_active_enums::NoteVisibilityEnum as NVE;
NoteBase { NoteBase {
created_at: note.created_at.into(), created_at: note.created_at.into(),
cw: note.cw.clone(), cw: note.cw.clone(),
cw_mm: cw_mm.cloned(),
uri: note.uri.clone(), uri: note.uri.clone(),
url: note.url.clone(), url: note.url.clone(),
text: note.text.clone().unwrap_or_default(), text: note.text.clone().unwrap_or_default(),
text_mm: text_mm.clone(),
visibility: match note.visibility { visibility: match note.visibility {
NVE::Followers => NoteVisibility::Followers, NVE::Followers => NoteVisibility::Followers,
NVE::Hidden => NoteVisibility::Direct, NVE::Hidden => NoteVisibility::Direct,
@ -66,10 +73,10 @@ impl PackType<NoteBaseSource<'_>> for NoteBase {
renote_count: note.renote_count as u64, renote_count: note.renote_count as u64,
hashtags: note.tags.clone(), hashtags: note.tags.clone(),
reactions: reactions.clone(), reactions: reactions.clone(),
emojis: emojis.clone(),
local_only: note.local_only, local_only: note.local_only,
has_poll: note.has_poll, has_poll: note.has_poll,
file_ids: note.file_ids.clone(), file_ids: note.file_ids.clone(),
emojis: emoji_context.clone(),
} }
} }
} }

View File

@ -3,9 +3,10 @@ use magnetar_calckey_model::ck::sea_orm_active_enums::UserProfileFfvisibilityEnu
use magnetar_sdk::types::emoji::{EmojiContext, PackEmojiBase}; use magnetar_sdk::types::emoji::{EmojiContext, PackEmojiBase};
use magnetar_sdk::types::note::PackNoteFull; use magnetar_sdk::types::note::PackNoteFull;
use magnetar_sdk::types::user::{ use magnetar_sdk::types::user::{
AvatarDecoration, PackSecurityKeyBase, SecurityKeyBase, SpeechTransform, UserBase, AvatarDecoration, PackSecurityKeyBase, ProfileField, SecurityKeyBase, SpeechTransform,
UserDetailExt, UserProfileExt, UserProfilePinsEx, UserRelationExt, UserSecretsExt, UserBase, UserDetailExt, UserProfileExt, UserProfilePinsEx, UserRelationExt, UserSecretsExt,
}; };
use magnetar_sdk::types::MmXml;
use crate::model::{PackType, PackingContext}; use crate::model::{PackType, PackingContext};
@ -15,14 +16,23 @@ impl PackType<&[PackEmojiBase]> for EmojiContext {
} }
} }
type UserBaseSource<'a> = ( struct UserBaseSource<'a> {
&'a ck::user::Model, user: &'a ck::user::Model,
&'a Option<ck::drive_file::Model>, username_mm: &'a MmXml,
&'a EmojiContext, avatar: &'a Option<ck::drive_file::Model>,
); emoji_context: &'a EmojiContext,
}
impl PackType<UserBaseSource<'_>> for UserBase { impl PackType<UserBaseSource<'_>> for UserBase {
fn extract(_context: &PackingContext, (user, avatar, emoji_context): UserBaseSource) -> Self { fn extract(
_context: &PackingContext,
UserBaseSource {
user,
username_mm,
avatar,
emoji_context,
}: UserBaseSource,
) -> Self {
UserBase { UserBase {
acct: user acct: user
.host .host
@ -31,6 +41,7 @@ impl PackType<UserBaseSource<'_>> for UserBase {
.unwrap_or_else(|| format!("@{}", user.username)), .unwrap_or_else(|| format!("@{}", user.username)),
username: user.username.clone(), username: user.username.clone(),
display_name: user.name.clone().unwrap_or_else(|| user.username.clone()), display_name: user.name.clone().unwrap_or_else(|| user.username.clone()),
display_name_mm: username_mm.clone(),
host: user.host.clone(), host: user.host.clone(),
speech_transform: if user.is_cat && user.speak_as_cat { speech_transform: if user.is_cat && user.speak_as_cat {
SpeechTransform::Cat SpeechTransform::Cat
@ -54,14 +65,27 @@ impl PackType<UserBaseSource<'_>> for UserBase {
} }
} }
type UserProfileExtSource<'a> = ( struct UserProfileExtSource<'a> {
&'a ck::user::Model, user: &'a ck::user::Model,
&'a ck::user_profile::Model, profile: &'a ck::user_profile::Model,
Option<&'a UserRelationExt>, profile_fields: &'a Vec<ProfileField>,
); description_mm: Option<&'a MmXml>,
relation: Option<&'a UserRelationExt>,
emoji_context: &'a EmojiContext,
}
impl PackType<UserProfileExtSource<'_>> for UserProfileExt { impl PackType<UserProfileExtSource<'_>> for UserProfileExt {
fn extract(context: &PackingContext, (user, profile, relation): UserProfileExtSource) -> Self { fn extract(
context: &PackingContext,
UserProfileExtSource {
user,
profile,
profile_fields,
description_mm,
relation,
emoji_context,
}: UserProfileExtSource,
) -> Self {
let follow_visibility = match profile.ff_visibility { let follow_visibility = match profile.ff_visibility {
UserProfileFfvisibilityEnum::Public => true, UserProfileFfvisibilityEnum::Public => true,
UserProfileFfvisibilityEnum::Followers => relation.is_some_and(|r| r.follows_you), UserProfileFfvisibilityEnum::Followers => relation.is_some_and(|r| r.follows_you),
@ -73,12 +97,13 @@ impl PackType<UserProfileExtSource<'_>> for UserProfileExt {
is_silenced: user.is_silenced, is_silenced: user.is_silenced,
is_suspended: user.is_suspended, is_suspended: user.is_suspended,
description: profile.description.clone(), description: profile.description.clone(),
description_mm: description_mm.cloned(),
location: profile.location.clone(), location: profile.location.clone(),
birthday: profile birthday: profile
.birthday .birthday
.clone() .clone()
.and_then(|b| b.parse().map_or_else(|_| None, Some)), .and_then(|b| b.parse().map_or_else(|_| None, Some)),
fields: serde_json::from_value(profile.fields.clone()).unwrap_or_else(|_| Vec::new()), fields: profile_fields.clone(),
follower_count: follow_visibility.then_some(user.followers_count as u64), follower_count: follow_visibility.then_some(user.followers_count as u64),
following_count: follow_visibility.then_some(user.following_count as u64), following_count: follow_visibility.then_some(user.following_count as u64),
note_count: Some(user.notes_count as u64), note_count: Some(user.notes_count as u64),
@ -89,6 +114,7 @@ impl PackType<UserProfileExtSource<'_>> for UserProfileExt {
banner_color: None, banner_color: None,
banner_blurhash: None, banner_blurhash: None,
has_public_reactions: profile.public_reactions, has_public_reactions: profile.public_reactions,
emojis: emoji_context.clone(),
} }
} }
} }
@ -103,19 +129,26 @@ impl PackType<&ck::user::Model> for UserDetailExt {
} }
} }
type UserRelationExtSource<'a> = ( struct UserRelationExtSource<'a> {
Option<&'a ck::following::Model>, follow_out: Option<&'a ck::following::Model>,
Option<&'a ck::following::Model>, follow_in: Option<&'a ck::following::Model>,
Option<&'a ck::blocking::Model>, block_out: Option<&'a ck::blocking::Model>,
Option<&'a ck::blocking::Model>, block_in: Option<&'a ck::blocking::Model>,
Option<&'a ck::muting::Model>, mute: Option<&'a ck::muting::Model>,
Option<&'a ck::renote_muting::Model>, renote_mute: Option<&'a ck::renote_muting::Model>,
); }
impl PackType<UserRelationExtSource<'_>> for UserRelationExt { impl PackType<UserRelationExtSource<'_>> for UserRelationExt {
fn extract( fn extract(
context: &PackingContext, context: &PackingContext,
(follow_out, follow_in, block_out, block_in, mute, renote_mute): UserRelationExtSource, UserRelationExtSource {
follow_out,
follow_in,
block_out,
block_in,
mute,
renote_mute,
}: UserRelationExtSource,
) -> Self { ) -> Self {
let self_user = context.self_user(); let self_user = context.self_user();