-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #604 from GiganticMinecraft/feat/notifications
通知機能の実装
- Loading branch information
Showing
29 changed files
with
862 additions
and
92 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod form; | ||
pub mod notification; | ||
pub mod repository; | ||
pub mod search; | ||
pub mod types; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod models; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use derive_getters::Getters; | ||
use serde::Deserialize; | ||
|
||
use crate::{ | ||
form::models::MessageId, | ||
types::authorization_guard::{AuthorizationGuard, AuthorizationGuardDefinitions, Create}, | ||
user::models::User, | ||
}; | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub enum NotificationSource { | ||
Message(MessageId), | ||
} | ||
|
||
pub type NotificationId = types::Id<Notification>; | ||
|
||
#[derive(Deserialize, Getters, Debug)] | ||
pub struct Notification { | ||
id: NotificationId, | ||
source: NotificationSource, | ||
recipient: User, | ||
is_read: bool, | ||
} | ||
|
||
impl Notification { | ||
/// [`Notification`] を新しく作成します。 | ||
/// | ||
/// # Examples | ||
/// ``` | ||
/// use domain::{ | ||
/// form::models::MessageId, | ||
/// notification::models::{Notification, NotificationSource}, | ||
/// user::models::User, | ||
/// }; | ||
/// | ||
/// let source = NotificationSource::Message(MessageId::new()); | ||
/// let recipient = User { | ||
/// id: Default::default(), | ||
/// name: "Alice".to_string(), | ||
/// role: Default::default(), | ||
/// }; | ||
/// let notification = Notification::new(source, recipient); | ||
/// | ||
/// assert!(!notification.is_read()); | ||
/// ``` | ||
pub fn new(source: NotificationSource, recipient: User) -> Self { | ||
Self { | ||
id: NotificationId::new(), | ||
source, | ||
recipient, | ||
is_read: false, | ||
} | ||
} | ||
|
||
/// [`Notification`] の各フィールドを指定して新しく作成します。 | ||
/// | ||
/// # Examples | ||
/// ``` | ||
/// use domain::{ | ||
/// form::models::MessageId, | ||
/// notification::models::{Notification, NotificationId, NotificationSource}, | ||
/// user::models::User, | ||
/// }; | ||
/// use uuid::Uuid; | ||
/// | ||
/// let id = NotificationId::new(); | ||
/// | ||
/// let source = NotificationSource::Message(MessageId::new()); | ||
/// let recipient = User { | ||
/// id: Uuid::new_v4(), | ||
/// name: "Alice".to_string(), | ||
/// role: Default::default(), | ||
/// }; | ||
/// | ||
/// let notification = unsafe { Notification::from_raw_parts(id, source, recipient, false) }; | ||
/// ``` | ||
/// | ||
/// # Safety | ||
/// この関数は [`Notification`] のバリデーションをスキップするため、 | ||
/// データベースからすでにバリデーションされているデータを読み出すときなど、 | ||
/// データの信頼性が保証されている場合にのみ使用してください。 | ||
pub unsafe fn from_raw_parts( | ||
id: NotificationId, | ||
source: NotificationSource, | ||
recipient: User, | ||
is_read: bool, | ||
) -> Self { | ||
Self { | ||
id, | ||
source, | ||
recipient, | ||
is_read, | ||
} | ||
} | ||
} | ||
|
||
impl AuthorizationGuardDefinitions<Notification> for Notification { | ||
fn can_create(&self, actor: &User) -> bool { | ||
self.recipient().id == actor.id | ||
} | ||
|
||
fn can_read(&self, actor: &User) -> bool { | ||
self.recipient().id == actor.id | ||
} | ||
|
||
fn can_update(&self, actor: &User) -> bool { | ||
self.recipient().id == actor.id | ||
} | ||
|
||
fn can_delete(&self, actor: &User) -> bool { | ||
self.recipient().id == actor.id | ||
} | ||
} | ||
|
||
impl From<Notification> for AuthorizationGuard<Notification, Create> { | ||
fn from(value: Notification) -> Self { | ||
AuthorizationGuard::new(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
pub mod form_repository; | ||
pub mod notification_repository; | ||
pub mod search_repository; | ||
pub mod user_repository; | ||
|
||
pub trait Repositories: Send + Sync { | ||
type ConcreteFormRepository: form_repository::FormRepository; | ||
type ConcreteUserRepository: user_repository::UserRepository; | ||
type ConcreteSearchRepository: search_repository::SearchRepository; | ||
type ConcreteNotificationRepository: notification_repository::NotificationRepository; | ||
|
||
fn form_repository(&self) -> &Self::ConcreteFormRepository; | ||
fn user_repository(&self) -> &Self::ConcreteUserRepository; | ||
fn search_repository(&self) -> &Self::ConcreteSearchRepository; | ||
fn notification_repository(&self) -> &Self::ConcreteNotificationRepository; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use async_trait::async_trait; | ||
use errors::Error; | ||
use uuid::Uuid; | ||
|
||
use crate::{ | ||
notification::models::{Notification, NotificationId}, | ||
types::authorization_guard::{AuthorizationGuard, Read, Update}, | ||
user::models::User, | ||
}; | ||
|
||
#[async_trait] | ||
pub trait NotificationRepository: Send + Sync + 'static { | ||
async fn create(&self, notification: &Notification) -> Result<(), Error>; | ||
async fn fetch_by_recipient_id( | ||
&self, | ||
recipient_id: Uuid, | ||
) -> Result<Vec<AuthorizationGuard<Notification, Read>>, Error>; | ||
async fn fetch_by_notification_ids( | ||
&self, | ||
notification_ids: Vec<NotificationId>, | ||
) -> Result<Vec<AuthorizationGuard<Notification, Read>>, Error>; | ||
async fn update_read_status( | ||
&self, | ||
actor: &User, | ||
notifications: Vec<(AuthorizationGuard<Notification, Update>, bool)>, | ||
) -> Result<(), Error>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.