diff --git a/ext_calckey_model/entity_ck/src/entities/sea_orm_active_enums.rs b/ext_calckey_model/entity_ck/src/entities/sea_orm_active_enums.rs index a40f2eb..f3dcf9b 100644 --- a/ext_calckey_model/entity_ck/src/entities/sea_orm_active_enums.rs +++ b/ext_calckey_model/entity_ck/src/entities/sea_orm_active_enums.rs @@ -98,14 +98,10 @@ pub enum NotificationTypeEnum { Follow, #[sea_orm(string_value = "followRequestAccepted")] FollowRequestAccepted, - #[sea_orm(string_value = "groupInvited")] - GroupInvited, #[sea_orm(string_value = "mention")] Mention, #[sea_orm(string_value = "pollEnded")] PollEnded, - #[sea_orm(string_value = "pollVote")] - PollVote, #[sea_orm(string_value = "quote")] Quote, #[sea_orm(string_value = "reaction")] diff --git a/ext_calckey_model/migration/src/lib.rs b/ext_calckey_model/migration/src/lib.rs index 4f31641..138350a 100644 --- a/ext_calckey_model/migration/src/lib.rs +++ b/ext_calckey_model/migration/src/lib.rs @@ -9,6 +9,7 @@ mod m20240107_220523_generated_is_quote; mod m20240107_224446_generated_is_renote; mod m20240112_215106_remove_pages; mod m20240112_234759_remove_gallery; +mod m20240115_212109_remove_poll_vote_notification; pub struct Migrator; @@ -25,6 +26,7 @@ impl MigratorTrait for Migrator { Box::new(m20240107_224446_generated_is_renote::Migration), Box::new(m20240112_215106_remove_pages::Migration), Box::new(m20240112_234759_remove_gallery::Migration), + Box::new(m20240115_212109_remove_poll_vote_notification::Migration), ] } } diff --git a/ext_calckey_model/migration/src/m20240115_212109_remove_poll_vote_notification.rs b/ext_calckey_model/migration/src/m20240115_212109_remove_poll_vote_notification.rs new file mode 100644 index 0000000..b569309 --- /dev/null +++ b/ext_calckey_model/migration/src/m20240115_212109_remove_poll_vote_notification.rs @@ -0,0 +1,45 @@ +use sea_orm_migration::prelude::*; +use sea_orm_migration::sea_orm::TransactionTrait; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + let db = manager.get_connection(); + + db.execute_unprepared( + r#" + DELETE FROM "notification" WHERE "type" = 'pollVote' OR "type" = 'groupInvited'; + + ALTER TYPE "notification_type_enum" RENAME TO "notification_type_enum_old"; + CREATE TYPE "notification_type_enum" AS ENUM('follow', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollEnded', 'receiveFollowRequest', 'followRequestAccepted', 'app'); + ALTER TABLE "notification" ALTER COLUMN "type" TYPE "notification_type_enum" USING "type"::text::notification_type_enum; + DROP TYPE "notification_type_enum_old"; + "#, + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + let db = manager.get_connection(); + let txn = db.begin().await?; + + db.execute_unprepared( + r#" + ALTER TYPE "notification_type_enum" RENAME TO "notification_type_enum_old"; + CREATE TYPE "notification_type_enum" AS ENUM('follow', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollVote', 'pollEnded', 'receiveFollowRequest', 'followRequestAccepted', 'groupInvited', 'app'); + ALTER TABLE "notification" ALTER COLUMN "type" TYPE "notification_type_enum" USING "type"::text::notification_type_enum; + DROP TYPE "notification_type_enum_old"; + "#, + ) + .await?; + + txn.commit().await?; + + Ok(()) + } +} diff --git a/ext_calckey_model/src/model_ext.rs b/ext_calckey_model/src/model_ext.rs index 6f8739b..4db43dc 100644 --- a/ext_calckey_model/src/model_ext.rs +++ b/ext_calckey_model/src/model_ext.rs @@ -5,8 +5,8 @@ use ext_calckey_model_migration::{ use magnetar_sdk::types::SpanFilter; use sea_orm::{ ColumnTrait, Condition, ConnectionTrait, Cursor, DbErr, DynIden, EntityTrait, FromQueryResult, - Iden, IntoIdentity, Iterable, JoinType, RelationDef, RelationTrait, Select, SelectModel, - SelectorTrait, + Iden, IntoIdentity, Iterable, JoinType, QueryTrait, RelationDef, RelationTrait, Select, + SelectModel, SelectorTrait, }; use std::fmt::Write; @@ -224,6 +224,7 @@ pub trait CursorPaginationExt { fn cursor_by_columns_and_span( self, + cursor_prefix_alias: Option, order_columns: C, pagination: &SpanFilter, limit: Option, @@ -234,6 +235,7 @@ pub trait CursorPaginationExt { async fn get_paginated_model( self, db: &T, + cursor_prefix_alias: Option, columns: C, curr: &SpanFilter, prev: &mut Option, @@ -255,6 +257,7 @@ where fn cursor_by_columns_and_span( self, + cursor_prefix_alias: Option, order_columns: C, pagination: &SpanFilter, limit: Option, @@ -262,7 +265,11 @@ where where C: IntoIdentity, { - let mut cursor = self.cursor_by(order_columns); + let mut cursor = Cursor::new( + self.into_query(), + cursor_prefix_alias.map_or_else(|| E::default().into_iden(), MagIden::into_iden), + order_columns, + ); if let Some(start) = pagination.start() { cursor.after(start); @@ -286,6 +293,7 @@ where async fn get_paginated_model( self, db: &T, + cursor_prefix_alias: Option, columns: C, curr: &SpanFilter, prev: &mut Option, @@ -298,7 +306,7 @@ where T: ConnectionTrait, { let mut result = self - .cursor_by_columns_and_span(columns, curr, Some(limit + 1)) + .cursor_by_columns_and_span(cursor_prefix_alias, columns, curr, Some(limit + 1)) .into_model::() .all(db) .await?; diff --git a/ext_calckey_model/src/note_model/mod.rs b/ext_calckey_model/src/note_model/mod.rs index 0c92e4d..807c8ce 100644 --- a/ext_calckey_model/src/note_model/mod.rs +++ b/ext_calckey_model/src/note_model/mod.rs @@ -3,8 +3,8 @@ pub mod data; use ext_calckey_model_migration::SelectStatement; use sea_orm::sea_query::{Asterisk, Expr, IntoIden, Query, SelectExpr, SimpleExpr}; use sea_orm::{ - Condition, EntityTrait, Iden, IntoSimpleExpr, JoinType, QueryFilter, QueryOrder, QuerySelect, - QueryTrait, Select, + Condition, EntityTrait, Iden, JoinType, QueryFilter, QueryOrder, QuerySelect, QueryTrait, + Select, }; use std::sync::Arc; @@ -177,6 +177,7 @@ impl NoteResolver { notes_select .get_paginated_model::( self.db.inner(), + Some(note::Entity.base_prefix()), (note::Column::CreatedAt, note::Column::Id), pagination, &mut None, diff --git a/ext_calckey_model/src/notification_model.rs b/ext_calckey_model/src/notification_model.rs index 5941822..66526d8 100644 --- a/ext_calckey_model/src/notification_model.rs +++ b/ext_calckey_model/src/notification_model.rs @@ -7,10 +7,13 @@ use crate::note_model::{NoteResolveOptions, NoteResolver}; use crate::user_model::{UserData, UserResolveOptions, UserResolver}; use crate::{CalckeyDbError, CalckeyModel}; use chrono::{DateTime, Utc}; +use ck::sea_orm_active_enums::NotificationTypeEnum; 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::prelude::Expr; +use sea_orm::sea_query::{IntoCondition, Query}; +use sea_orm::{ActiveEnum, Iden, IntoSimpleExpr, QueryTrait}; use sea_orm::{DbErr, EntityTrait, FromQueryResult, QueryFilter, QueryResult, QuerySelect}; use serde::{Deserialize, Serialize}; @@ -55,12 +58,14 @@ impl ModelPagination for NotificationData { } } -const NOTIFICATION: &str = "notification."; const NOTIFIER: &str = "notifier."; const NOTIFICATION_NOTE: &str = "note."; const ACCESS_TOKEN: &str = "access_token."; -pub struct NotificationResolveOptions {} +pub struct NotificationResolveOptions { + pub note_options: NoteResolveOptions, + pub user_options: UserResolveOptions, +} #[derive(Clone)] pub struct NotificationResolver { @@ -86,11 +91,12 @@ impl NotificationResolver { &self, q: &mut SelectStatement, notification_tbl: &MagIden, - note_options: &NoteResolveOptions, - user_options: &UserResolveOptions, + resolve_options: &NotificationResolveOptions, note_resolver: &NoteResolver, user_resolver: &UserResolver, ) { + q.add_aliased_columns::(¬ification_tbl); + let notifier_tbl = notification_tbl.join_str(NOTIFIER); q.add_aliased_columns::(¬ifier_tbl); q.join_columns( @@ -98,7 +104,7 @@ impl NotificationResolver { notification::Relation::User2.with_from_alias(notification_tbl), ¬ifier_tbl, ); - user_resolver.resolve(q, ¬ifier_tbl, &user_options); + user_resolver.resolve(q, ¬ifier_tbl, &resolve_options.user_options); let token_tbl = notification_tbl.join_str(ACCESS_TOKEN); q.add_aliased_columns::(&token_tbl); @@ -114,14 +120,22 @@ impl NotificationResolver { notification::Relation::Note.with_from_alias(notification_tbl), ¬e_tbl, ); - note_resolver.attach_note(q, ¬e_tbl, 1, 1, note_options, &self.user_resolver); + note_resolver.attach_note( + q, + ¬e_tbl, + 1, + 1, + &resolve_options.note_options, + &self.user_resolver, + ); } pub async fn get( &self, - note_options: &NoteResolveOptions, - user_options: &UserResolveOptions, + resolve_options: &NotificationResolveOptions, user_id: &str, + notification_types: &[NotificationTypeEnum], + unread_only: bool, pagination: &SpanFilter, prev: &mut Option, next: &mut Option, @@ -129,26 +143,46 @@ impl NotificationResolver { ) -> Result, CalckeyDbError> { let notification_tbl = notification::Entity.base_prefix(); - let mut select = notification::Entity::find(); + let mut query = Query::select(); + query.from_as(notification::Entity, notification_tbl.clone()); - let query = QuerySelect::query(&mut select); self.resolve( - query, + &mut query, ¬ification_tbl, - note_options, - user_options, + &resolve_options, &self.note_resolver, &self.user_resolver, ); + let mut select = notification::Entity::find(); + *QuerySelect::query(&mut select) = query; + let notifications = select .filter( notification_tbl .col(notification::Column::NotifieeId) - .eq(user_id), + .eq(user_id) + .and( + notification_tbl.col(notification::Column::Type).is_in( + notification_types + .iter() + .copied() + .map(Expr::val) + .map(|e| e.cast_as(NotificationTypeEnum::name())), + ), + ), ) + .apply_if(unread_only.then_some(()), |s, _| { + s.filter( + notification_tbl + .col(notification::Column::IsRead) + .not() + .into_condition(), + ) + }) .get_paginated_model::( &self.db.0, + Some(notification_tbl), (notification::Column::CreatedAt, notification::Column::Id), pagination, prev, diff --git a/ext_calckey_model/src/user_model.rs b/ext_calckey_model/src/user_model.rs index 67cc5ae..5682e0f 100644 --- a/ext_calckey_model/src/user_model.rs +++ b/ext_calckey_model/src/user_model.rs @@ -200,6 +200,7 @@ impl UserResolver { .filter(follow_request::Column::FolloweeId.eq(followee)) .get_paginated_model::( &self.db.0, + None, ( follow_request::Column::CreatedAt, follow_request::Column::Id, @@ -241,6 +242,7 @@ impl UserResolver { .filter(following::Column::FollowerId.eq(follower)) .get_paginated_model::( &self.db.0, + None, (following::Column::CreatedAt, following::Column::Id), pagination, prev, @@ -279,6 +281,7 @@ impl UserResolver { .filter(following::Column::FolloweeId.eq(followee)) .get_paginated_model::( &self.db.0, + None, (following::Column::CreatedAt, following::Column::Id), pagination, prev, diff --git a/fe_calckey/frontend/client/src/components/MagNote.vue b/fe_calckey/frontend/client/src/components/MagNote.vue index 82a1e22..8047422 100644 --- a/fe_calckey/frontend/client/src/components/MagNote.vue +++ b/fe_calckey/frontend/client/src/components/MagNote.vue @@ -286,7 +286,7 @@ const reactButton = ref(null); let appearNote = $computed( () => magEffectiveNote(note) as packed.PackNoteMaybeFull ); -const isMyRenote = $i && $i.id === note.user.id; +const isMyRenote = $i && $i.id === appearNote.user.id; const showContent = ref(false); const isDeleted = ref(false); const muted = ref(getWordSoftMute(note, $i, defaultStore.state.mutedWords)); diff --git a/fe_calckey/frontend/client/src/components/MkNotification.vue b/fe_calckey/frontend/client/src/components/MkNotification.vue index 04a0f09..3845162 100644 --- a/fe_calckey/frontend/client/src/components/MkNotification.vue +++ b/fe_calckey/frontend/client/src/components/MkNotification.vue @@ -6,13 +6,24 @@ :class="notification.type" >
- - @@ -24,55 +35,51 @@ />
-
- {{ + {{ i18n.ts._notification.pollEnded }} + {{ notification.header }}
{{ i18n.ts._notification.renoted }} - {{ i18n.ts._notification.voted }} - - - - - {{ i18n.ts.youGotNewFollower }} @@ -216,13 +225,13 @@
{{ i18n.ts.followRequestAccepted }} {{ i18n.ts.receiveFollowRequest }} @@ -233,7 +242,7 @@ />
- +
@@ -242,25 +251,23 @@