From 475c33e57e4bd7abe902b372996100fe01029a49 Mon Sep 17 00:00:00 2001 From: Natty Date: Mon, 15 Jan 2024 01:46:08 +0100 Subject: [PATCH] Notification fetching base --- Cargo.lock | 1 + ext_calckey_model/src/lib.rs | 10 ++ ext_calckey_model/src/note_model/mod.rs | 3 +- ext_calckey_model/src/notification_model.rs | 162 ++++++++++++++++++ ext_calckey_model/src/user_model.rs | 1 + .../frontend/magnetar-common/src/packed.ts | 11 ++ .../frontend/magnetar-common/src/types.ts | 5 + .../src/types/NotificationAppExt.ts | 3 + .../src/types/NotificationBase.ts | 3 + .../src/types/NotificationNoteExt.ts | 4 + .../src/types/NotificationReactionExt.ts | 4 + .../src/types/NotificationType.ts | 2 +- .../src/types/NotificationUserExt.ts | 4 + .../src/types/PackNotification.ts | 13 ++ .../src/types/packed/PackNotificationApp.ts | 5 + .../types/packed/PackNotificationFollow.ts | 5 + .../PackNotificationFollowRequestAccepted.ts | 5 + .../PackNotificationFollowRequestReceived.ts | 5 + .../types/packed/PackNotificationMention.ts | 5 + .../types/packed/PackNotificationPollEnd.ts | 5 + .../src/types/packed/PackNotificationQuote.ts | 5 + .../types/packed/PackNotificationReaction.ts | 6 + .../types/packed/PackNotificationRenote.ts | 5 + .../src/types/packed/PackNotificationReply.ts | 5 + magnetar_sdk/Cargo.toml | 2 + magnetar_sdk/src/types/mod.rs | 18 +- magnetar_sdk/src/types/notification.rs | 75 ++++++++ 27 files changed, 354 insertions(+), 18 deletions(-) create mode 100644 ext_calckey_model/src/notification_model.rs create mode 100644 fe_calckey/frontend/magnetar-common/src/types/NotificationAppExt.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/NotificationBase.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/NotificationNoteExt.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/NotificationReactionExt.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/NotificationUserExt.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/PackNotification.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationApp.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollow.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollowRequestAccepted.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollowRequestReceived.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationMention.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationPollEnd.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationQuote.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationReaction.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationRenote.ts create mode 100644 fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationReply.ts create mode 100644 magnetar_sdk/src/types/notification.rs diff --git a/Cargo.lock b/Cargo.lock index 5909d92..bf41059 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1617,6 +1617,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", + "strum", "ts-rs", "unicode-segmentation", ] diff --git a/ext_calckey_model/src/lib.rs b/ext_calckey_model/src/lib.rs index 8293802..c861b36 100644 --- a/ext_calckey_model/src/lib.rs +++ b/ext_calckey_model/src/lib.rs @@ -1,6 +1,7 @@ pub mod emoji; pub mod model_ext; pub mod note_model; +pub mod notification_model; pub mod poll; pub mod user_model; @@ -10,6 +11,7 @@ pub use sea_orm; use user_model::UserResolver; use crate::note_model::NoteResolver; +use crate::notification_model::NotificationResolver; use chrono::Utc; use ext_calckey_model_migration::{Migrator, MigratorTrait}; use futures_util::StreamExt; @@ -296,6 +298,14 @@ impl CalckeyModel { Ok(meta) } + pub fn get_notification_resolver(&self) -> NotificationResolver { + NotificationResolver::new( + self.clone(), + self.get_user_resolver(), + self.get_note_resolver(), + ) + } + pub fn get_note_resolver(&self) -> NoteResolver { NoteResolver::new(self.clone(), self.get_user_resolver()) } diff --git a/ext_calckey_model/src/note_model/mod.rs b/ext_calckey_model/src/note_model/mod.rs index 98a8cc8..1810985 100644 --- a/ext_calckey_model/src/note_model/mod.rs +++ b/ext_calckey_model/src/note_model/mod.rs @@ -25,6 +25,7 @@ const USER: &str = "user."; const REPLY: &str = "reply."; const RENOTE: &str = "renote."; +#[derive(Clone)] pub struct NoteResolver { db: CalckeyModel, user_resolver: UserResolver, @@ -207,7 +208,7 @@ impl NoteResolver { */ - fn attach_note( + pub fn attach_note( &self, q: &mut SelectStatement, note_tbl: &MagIden, diff --git a/ext_calckey_model/src/notification_model.rs b/ext_calckey_model/src/notification_model.rs new file mode 100644 index 0000000..5941822 --- /dev/null +++ b/ext_calckey_model/src/notification_model.rs @@ -0,0 +1,162 @@ +use crate::model_ext::{ + AliasColumnExt, AliasSourceExt, AliasSuffixExt, CursorPaginationExt, EntityPrefixExt, MagIden, + ModelPagination, SelectColumnsExt, +}; +use crate::note_model::data::NoteData; +use crate::note_model::{NoteResolveOptions, NoteResolver}; +use crate::user_model::{UserData, UserResolveOptions, UserResolver}; +use crate::{CalckeyDbError, CalckeyModel}; +use chrono::{DateTime, Utc}; +use ck::{access_token, notification, user}; +use ext_calckey_model_migration::{JoinType, SelectStatement}; +use magnetar_sdk::types::SpanFilter; +use sea_orm::Iden; +use sea_orm::{DbErr, EntityTrait, FromQueryResult, QueryFilter, QueryResult, QuerySelect}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct NotificationData { + pub notification: notification::Model, + pub access_token: Option, + pub notifier: Option, + pub notification_note: Option, +} + +impl FromQueryResult for NotificationData { + fn from_query_result(res: &QueryResult, prefix: &str) -> Result { + let prefix = if prefix.is_empty() { + notification::Entity.base_prefix() + } else { + MagIden::alias(prefix) + }; + + Ok(NotificationData { + notification: notification::Model::from_query_result(res, &prefix.to_string())?, + access_token: access_token::Model::from_query_result_optional( + res, + &prefix.join_str_as_str(ACCESS_TOKEN), + )?, + notification_note: NoteData::from_query_result_optional( + res, + &prefix.join_str_as_str(NOTIFICATION_NOTE), + )?, + notifier: UserData::from_query_result_optional(res, &prefix.join_str_as_str(NOTIFIER))?, + }) + } +} + +impl ModelPagination for NotificationData { + fn id(&self) -> &str { + &self.notification.id + } + + fn time(&self) -> DateTime { + self.notification.created_at.into() + } +} + +const NOTIFICATION: &str = "notification."; +const NOTIFIER: &str = "notifier."; +const NOTIFICATION_NOTE: &str = "note."; +const ACCESS_TOKEN: &str = "access_token."; + +pub struct NotificationResolveOptions {} + +#[derive(Clone)] +pub struct NotificationResolver { + db: CalckeyModel, + note_resolver: NoteResolver, + user_resolver: UserResolver, +} + +impl NotificationResolver { + pub(crate) fn new( + db: CalckeyModel, + user_resolver: UserResolver, + note_resolver: NoteResolver, + ) -> Self { + Self { + db, + note_resolver, + user_resolver, + } + } + + pub fn resolve( + &self, + q: &mut SelectStatement, + notification_tbl: &MagIden, + note_options: &NoteResolveOptions, + user_options: &UserResolveOptions, + note_resolver: &NoteResolver, + user_resolver: &UserResolver, + ) { + let notifier_tbl = notification_tbl.join_str(NOTIFIER); + q.add_aliased_columns::(¬ifier_tbl); + q.join_columns( + JoinType::LeftJoin, + notification::Relation::User2.with_from_alias(notification_tbl), + ¬ifier_tbl, + ); + user_resolver.resolve(q, ¬ifier_tbl, &user_options); + + let token_tbl = notification_tbl.join_str(ACCESS_TOKEN); + q.add_aliased_columns::(&token_tbl); + q.join_columns( + JoinType::LeftJoin, + notification::Relation::AccessToken.with_from_alias(notification_tbl), + &token_tbl, + ); + + let note_tbl = notification_tbl.join_str(NOTIFICATION_NOTE); + q.join_columns( + JoinType::LeftJoin, + notification::Relation::Note.with_from_alias(notification_tbl), + ¬e_tbl, + ); + note_resolver.attach_note(q, ¬e_tbl, 1, 1, note_options, &self.user_resolver); + } + + pub async fn get( + &self, + note_options: &NoteResolveOptions, + user_options: &UserResolveOptions, + user_id: &str, + pagination: &SpanFilter, + prev: &mut Option, + next: &mut Option, + limit: u64, + ) -> Result, CalckeyDbError> { + let notification_tbl = notification::Entity.base_prefix(); + + let mut select = notification::Entity::find(); + + let query = QuerySelect::query(&mut select); + self.resolve( + query, + ¬ification_tbl, + note_options, + user_options, + &self.note_resolver, + &self.user_resolver, + ); + + let notifications = select + .filter( + notification_tbl + .col(notification::Column::NotifieeId) + .eq(user_id), + ) + .get_paginated_model::( + &self.db.0, + (notification::Column::CreatedAt, notification::Column::Id), + pagination, + prev, + next, + limit, + ) + .await?; + + Ok(notifications) + } +} diff --git a/ext_calckey_model/src/user_model.rs b/ext_calckey_model/src/user_model.rs index a980bf0..92b2e33 100644 --- a/ext_calckey_model/src/user_model.rs +++ b/ext_calckey_model/src/user_model.rs @@ -115,6 +115,7 @@ const PROFILE: &str = "profile."; const AVATAR: &str = "avatar."; const BANNER: &str = "banner."; +#[derive(Clone)] pub struct UserResolver { db: CalckeyModel, } diff --git a/fe_calckey/frontend/magnetar-common/src/packed.ts b/fe_calckey/frontend/magnetar-common/src/packed.ts index f8e2b94..43ac9aa 100644 --- a/fe_calckey/frontend/magnetar-common/src/packed.ts +++ b/fe_calckey/frontend/magnetar-common/src/packed.ts @@ -14,3 +14,14 @@ export { PackUserBase } from "./types/packed/PackUserBase"; export { PackUserMaybeAll } from "./types/packed/PackUserMaybeAll"; export { PackUserSelf } from "./types/packed/PackUserSelf"; export { PackUserSelfMaybeAll } from "./types/packed/PackUserSelfMaybeAll"; +export { PackNotificationFollow } from "./types/packed/PackNotificationFollow"; +export { PackNotificationFollowRequestReceived } from "./types/packed/PackNotificationFollowRequestReceived"; +export { PackNotificationFollowRequestAccepted } from "./types/packed/PackNotificationFollowRequestAccepted"; +export { PackNotificationMention } from "./types/packed/PackNotificationMention"; +export { PackNotificationReply } from "./types/packed/PackNotificationReply"; +export { PackNotificationRenote } from "./types/packed/PackNotificationRenote"; +export { PackNotificationReaction } from "./types/packed/PackNotificationReaction"; +export { PackNotificationQuote } from "./types/packed/PackNotificationQuote"; +export { PackNotificationPollEnd } from "./types/packed/PackNotificationPollEnd"; +export { PackNotificationApp } from "./types/packed/PackNotificationApp"; +export { PackNotification } from "./types/PackNotification"; diff --git a/fe_calckey/frontend/magnetar-common/src/types.ts b/fe_calckey/frontend/magnetar-common/src/types.ts index df69bec..dc07fdc 100644 --- a/fe_calckey/frontend/magnetar-common/src/types.ts +++ b/fe_calckey/frontend/magnetar-common/src/types.ts @@ -49,3 +49,8 @@ export { NoFilter } from "./types/NoFilter"; export { TimelineType } from "./types/TimelineType"; export { Empty } from "./types/Empty"; export { PaginationShape } from "./types/PaginationShape"; +export { NotificationBase } from "./types/NotificationBase"; +export { NotificationAppExt } from "./types/NotificationAppExt"; +export { NotificationNoteExt } from "./types/NotificationNoteExt"; +export { NotificationReactionExt } from "./types/NotificationReactionExt"; +export { NotificationUserExt } from "./types/NotificationUserExt"; diff --git a/fe_calckey/frontend/magnetar-common/src/types/NotificationAppExt.ts b/fe_calckey/frontend/magnetar-common/src/types/NotificationAppExt.ts new file mode 100644 index 0000000..cc181c3 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/NotificationAppExt.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export interface NotificationAppExt { body: string, header: string, icon: string, } \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/NotificationBase.ts b/fe_calckey/frontend/magnetar-common/src/types/NotificationBase.ts new file mode 100644 index 0000000..0e7f8ca --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/NotificationBase.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export interface NotificationBase { id: string, created_at: string, is_read: boolean, } \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/NotificationNoteExt.ts b/fe_calckey/frontend/magnetar-common/src/types/NotificationNoteExt.ts new file mode 100644 index 0000000..78aab79 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/NotificationNoteExt.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { PackNoteMaybeFull } from "./packed/PackNoteMaybeFull"; + +export interface NotificationNoteExt { note: PackNoteMaybeFull, } \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/NotificationReactionExt.ts b/fe_calckey/frontend/magnetar-common/src/types/NotificationReactionExt.ts new file mode 100644 index 0000000..7a9c841 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/NotificationReactionExt.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Reaction } from "./Reaction"; + +export interface NotificationReactionExt { reaction: Reaction, } \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/NotificationType.ts b/fe_calckey/frontend/magnetar-common/src/types/NotificationType.ts index 8a53267..b5f089c 100644 --- a/fe_calckey/frontend/magnetar-common/src/types/NotificationType.ts +++ b/fe_calckey/frontend/magnetar-common/src/types/NotificationType.ts @@ -1,3 +1,3 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -export type NotificationType = "Follow" | "Mention" | "Reply" | "Renote" | "Quote" | "Reaction" | "PollVote" | "PollEnded" | "FollowRequest" | "FollowRequestAccepted" | "App"; \ No newline at end of file +export type NotificationType = "Follow" | "FollowRequestReceived" | "FollowRequestAccepted" | "Mention" | "Reply" | "Renote" | "Reaction" | "Quote" | "PollEnd" | "App"; \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/NotificationUserExt.ts b/fe_calckey/frontend/magnetar-common/src/types/NotificationUserExt.ts new file mode 100644 index 0000000..113b7e2 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/NotificationUserExt.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { PackUserBase } from "./packed/PackUserBase"; + +export interface NotificationUserExt { user: PackUserBase, } \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/PackNotification.ts b/fe_calckey/frontend/magnetar-common/src/types/PackNotification.ts new file mode 100644 index 0000000..e467217 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/PackNotification.ts @@ -0,0 +1,13 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { PackNotificationApp } from "./packed/PackNotificationApp"; +import type { PackNotificationFollow } from "./packed/PackNotificationFollow"; +import type { PackNotificationFollowRequestAccepted } from "./packed/PackNotificationFollowRequestAccepted"; +import type { PackNotificationFollowRequestReceived } from "./packed/PackNotificationFollowRequestReceived"; +import type { PackNotificationMention } from "./packed/PackNotificationMention"; +import type { PackNotificationPollEnd } from "./packed/PackNotificationPollEnd"; +import type { PackNotificationQuote } from "./packed/PackNotificationQuote"; +import type { PackNotificationReaction } from "./packed/PackNotificationReaction"; +import type { PackNotificationRenote } from "./packed/PackNotificationRenote"; +import type { PackNotificationReply } from "./packed/PackNotificationReply"; + +export type PackNotification = { "type": "Follow" } & PackNotificationFollow | { "type": "FollowRequestReceived" } & PackNotificationFollowRequestReceived | { "type": "FollowRequestAccepted" } & PackNotificationFollowRequestAccepted | { "type": "Mention" } & PackNotificationMention | { "type": "Reply" } & PackNotificationReply | { "type": "Renote" } & PackNotificationRenote | { "type": "Reaction" } & PackNotificationReaction | { "type": "Quote" } & PackNotificationQuote | { "type": "PollEnd" } & PackNotificationPollEnd | { "type": "App" } & PackNotificationApp; \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationApp.ts b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationApp.ts new file mode 100644 index 0000000..ce3c5c5 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationApp.ts @@ -0,0 +1,5 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Id } from "../Id"; +import type { NotificationAppExt } from "../NotificationAppExt"; + +export type PackNotificationApp = Id & NotificationAppExt; \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollow.ts b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollow.ts new file mode 100644 index 0000000..52248d1 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollow.ts @@ -0,0 +1,5 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Id } from "../Id"; +import type { NotificationUserExt } from "../NotificationUserExt"; + +export type PackNotificationFollow = Id & NotificationUserExt; \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollowRequestAccepted.ts b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollowRequestAccepted.ts new file mode 100644 index 0000000..2ea79c9 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollowRequestAccepted.ts @@ -0,0 +1,5 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Id } from "../Id"; +import type { NotificationUserExt } from "../NotificationUserExt"; + +export type PackNotificationFollowRequestAccepted = Id & NotificationUserExt; \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollowRequestReceived.ts b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollowRequestReceived.ts new file mode 100644 index 0000000..f198da6 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationFollowRequestReceived.ts @@ -0,0 +1,5 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Id } from "../Id"; +import type { NotificationUserExt } from "../NotificationUserExt"; + +export type PackNotificationFollowRequestReceived = Id & NotificationUserExt; \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationMention.ts b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationMention.ts new file mode 100644 index 0000000..417b2f4 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationMention.ts @@ -0,0 +1,5 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Id } from "../Id"; +import type { NotificationNoteExt } from "../NotificationNoteExt"; + +export type PackNotificationMention = Id & NotificationNoteExt; \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationPollEnd.ts b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationPollEnd.ts new file mode 100644 index 0000000..7c3fee7 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationPollEnd.ts @@ -0,0 +1,5 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Id } from "../Id"; +import type { NotificationNoteExt } from "../NotificationNoteExt"; + +export type PackNotificationPollEnd = Id & NotificationNoteExt; \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationQuote.ts b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationQuote.ts new file mode 100644 index 0000000..9713cf6 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationQuote.ts @@ -0,0 +1,5 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Id } from "../Id"; +import type { NotificationNoteExt } from "../NotificationNoteExt"; + +export type PackNotificationQuote = Id & NotificationNoteExt; \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationReaction.ts b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationReaction.ts new file mode 100644 index 0000000..631f242 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationReaction.ts @@ -0,0 +1,6 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Id } from "../Id"; +import type { NotificationReactionExt } from "../NotificationReactionExt"; +import type { NotificationUserExt } from "../NotificationUserExt"; + +export type PackNotificationReaction = Id & NotificationReactionExt & NotificationUserExt; \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationRenote.ts b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationRenote.ts new file mode 100644 index 0000000..1f3c78d --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationRenote.ts @@ -0,0 +1,5 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Id } from "../Id"; +import type { NotificationNoteExt } from "../NotificationNoteExt"; + +export type PackNotificationRenote = Id & NotificationNoteExt; \ No newline at end of file diff --git a/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationReply.ts b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationReply.ts new file mode 100644 index 0000000..4e44445 --- /dev/null +++ b/fe_calckey/frontend/magnetar-common/src/types/packed/PackNotificationReply.ts @@ -0,0 +1,5 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Id } from "../Id"; +import type { NotificationNoteExt } from "../NotificationNoteExt"; + +export type PackNotificationReply = Id & NotificationNoteExt; \ No newline at end of file diff --git a/magnetar_sdk/Cargo.toml b/magnetar_sdk/Cargo.toml index 5a27d25..0f0306e 100644 --- a/magnetar_sdk/Cargo.toml +++ b/magnetar_sdk/Cargo.toml @@ -15,6 +15,8 @@ serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } serde_urlencoded = { workspace = true } +strum = { workspace = true, features = ["derive"] } + ts-rs = { workspace = true, features = ["chrono", "chrono-impl"] } unicode-segmentation = { workspace = true } diff --git a/magnetar_sdk/src/types/mod.rs b/magnetar_sdk/src/types/mod.rs index ad46425..6f374cf 100644 --- a/magnetar_sdk/src/types/mod.rs +++ b/magnetar_sdk/src/types/mod.rs @@ -2,9 +2,11 @@ pub mod drive; pub mod emoji; pub mod instance; pub mod note; +pub mod notification; pub mod timeline; pub mod user; +use crate::types::notification::NotificationType; use crate::util_types::U64Range; use chrono::{DateTime, Utc}; use serde::de::Error; @@ -213,22 +215,6 @@ impl> From for Id { #[repr(transparent)] pub struct MmXml(pub String); -#[derive(Copy, Clone, Debug, Deserialize, Serialize, TS)] -#[ts(export)] -pub enum NotificationType { - Follow, - Mention, - Reply, - Renote, - Quote, - Reaction, - PollVote, - PollEnded, - FollowRequest, - FollowRequestAccepted, - App, -} - #[derive(Clone, Debug, Deserialize, Serialize, TS)] #[ts(export)] pub struct NotificationSettings { diff --git a/magnetar_sdk/src/types/notification.rs b/magnetar_sdk/src/types/notification.rs new file mode 100644 index 0000000..6ee7fec --- /dev/null +++ b/magnetar_sdk/src/types/notification.rs @@ -0,0 +1,75 @@ +use crate::types::note::{ + PackNoteMaybeFull, Reaction, ReactionShortcode, ReactionUnicode, ReactionUnknown, +}; +use crate::types::user::PackUserBase; +use crate::types::Id; +use crate::{Packed, Required}; +use chrono::{DateTime, Utc}; +use magnetar_sdk_macros::pack; +use serde::{Deserialize, Serialize}; +use strum::EnumDiscriminants; +use ts_rs::TS; + +#[derive(Clone, Debug, Deserialize, Serialize, TS)] +#[ts(export)] +pub struct NotificationBase { + pub id: String, + pub created_at: DateTime, + pub is_read: bool, +} + +#[derive(Clone, Debug, Deserialize, Serialize, TS)] +#[ts(export)] +pub struct NotificationNoteExt { + pub note: PackNoteMaybeFull, +} + +#[derive(Clone, Debug, Deserialize, Serialize, TS)] +#[ts(export)] +pub struct NotificationUserExt { + pub user: PackUserBase, +} + +#[derive(Clone, Debug, Deserialize, Serialize, TS)] +#[ts(export)] +pub struct NotificationReactionExt { + pub reaction: Reaction, +} + +#[derive(Clone, Debug, Deserialize, Serialize, TS)] +#[ts(export)] +pub struct NotificationAppExt { + pub body: String, + pub header: String, + pub icon: String, +} + +pack!(PackNotificationFollow, Required as id & Required as follower); +pack!(PackNotificationFollowRequestReceived, Required as id & Required as follower); +pack!(PackNotificationFollowRequestAccepted, Required as id & Required as follower); +pack!(PackNotificationMention, Required as id & Required as note); +pack!(PackNotificationReply, Required as id & Required as note); +pack!(PackNotificationRenote, Required as id & Required as note); +pack!(PackNotificationReaction, Required as id & Required as reaction & Required as user); +pack!(PackNotificationQuote, Required as id & Required as note); +pack!(PackNotificationPollEnd, Required as id & Required as note); +pack!(PackNotificationApp, Required as id & Required as custom); + +#[derive(Clone, Debug, Deserialize, Serialize, TS, EnumDiscriminants)] +#[strum_discriminants(name(NotificationType))] +#[strum_discriminants(ts(export))] +#[strum_discriminants(derive(Deserialize, Serialize, TS))] +#[ts(export)] +#[serde(tag = "type")] +pub enum PackNotification { + Follow(PackNotificationFollow), + FollowRequestReceived(PackNotificationFollowRequestReceived), + FollowRequestAccepted(PackNotificationFollowRequestAccepted), + Mention(PackNotificationMention), + Reply(PackNotificationReply), + Renote(PackNotificationRenote), + Reaction(PackNotificationReaction), + Quote(PackNotificationQuote), + PollEnd(PackNotificationPollEnd), + App(PackNotificationApp), +}