2018-09-27 23:06:40 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2018-11-24 12:44:17 +01:00
|
|
|
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
2018-05-13 14:44:18 +02:00
|
|
|
|
2018-07-26 15:46:10 +02:00
|
|
|
use comments::Comment;
|
|
|
|
use follows::Follow;
|
|
|
|
use likes::Like;
|
|
|
|
use mentions::Mention;
|
|
|
|
use posts::Post;
|
|
|
|
use reshares::Reshare;
|
2018-05-13 14:44:18 +02:00
|
|
|
use schema::notifications;
|
2018-11-24 12:44:17 +01:00
|
|
|
use users::User;
|
2018-12-29 09:36:07 +01:00
|
|
|
use {Connection, Error, Result};
|
2018-05-13 14:44:18 +02:00
|
|
|
|
2018-07-26 15:46:10 +02:00
|
|
|
pub mod notification_kind {
|
2018-11-26 10:21:52 +01:00
|
|
|
pub const COMMENT: &str = "COMMENT";
|
|
|
|
pub const FOLLOW: &str = "FOLLOW";
|
|
|
|
pub const LIKE: &str = "LIKE";
|
|
|
|
pub const MENTION: &str = "MENTION";
|
|
|
|
pub const RESHARE: &str = "RESHARE";
|
2018-07-26 15:46:10 +02:00
|
|
|
}
|
|
|
|
|
2019-03-12 19:40:54 +01:00
|
|
|
#[derive(Clone, Queryable, Identifiable)]
|
2018-05-13 14:44:18 +02:00
|
|
|
pub struct Notification {
|
|
|
|
pub id: i32,
|
2018-05-24 12:12:27 +02:00
|
|
|
pub user_id: i32,
|
2018-09-27 23:06:40 +02:00
|
|
|
pub creation_date: NaiveDateTime,
|
2018-07-26 15:46:10 +02:00
|
|
|
pub kind: String,
|
2018-11-24 12:44:17 +01:00
|
|
|
pub object_id: i32,
|
2018-05-13 14:44:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "notifications"]
|
|
|
|
pub struct NewNotification {
|
2018-06-17 22:19:27 +02:00
|
|
|
pub user_id: i32,
|
2018-07-26 15:46:10 +02:00
|
|
|
pub kind: String,
|
2018-11-24 12:44:17 +01:00
|
|
|
pub object_id: i32,
|
2018-05-13 14:44:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Notification {
|
2018-06-18 15:57:38 +02:00
|
|
|
insert!(notifications, NewNotification);
|
2018-06-18 15:44:23 +02:00
|
|
|
get!(notifications);
|
2018-05-13 15:35:55 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn find_for_user(conn: &Connection, user: &User) -> Result<Vec<Notification>> {
|
2018-11-24 12:44:17 +01:00
|
|
|
notifications::table
|
|
|
|
.filter(notifications::user_id.eq(user.id))
|
2018-05-24 12:12:27 +02:00
|
|
|
.order_by(notifications::creation_date.desc())
|
2018-05-13 15:35:55 +02:00
|
|
|
.load::<Notification>(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-05-13 15:35:55 +02:00
|
|
|
}
|
2018-07-25 15:33:54 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn count_for_user(conn: &Connection, user: &User) -> Result<i64> {
|
2018-12-14 23:16:18 +01:00
|
|
|
notifications::table
|
|
|
|
.filter(notifications::user_id.eq(user.id))
|
|
|
|
.count()
|
|
|
|
.get_result(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-12-14 23:16:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
pub fn page_for_user(
|
|
|
|
conn: &Connection,
|
|
|
|
user: &User,
|
|
|
|
(min, max): (i32, i32),
|
2018-12-29 09:36:07 +01:00
|
|
|
) -> Result<Vec<Notification>> {
|
2018-11-24 12:44:17 +01:00
|
|
|
notifications::table
|
|
|
|
.filter(notifications::user_id.eq(user.id))
|
2018-07-25 15:33:54 +02:00
|
|
|
.order_by(notifications::creation_date.desc())
|
|
|
|
.offset(min.into())
|
|
|
|
.limit((max - min).into())
|
|
|
|
.load::<Notification>(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-07-25 15:33:54 +02:00
|
|
|
}
|
2018-07-26 15:46:10 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn find<S: Into<String>>(conn: &Connection, kind: S, obj: i32) -> Result<Notification> {
|
2018-11-24 12:44:17 +01:00
|
|
|
notifications::table
|
|
|
|
.filter(notifications::kind.eq(kind.into()))
|
2018-09-08 00:29:50 +02:00
|
|
|
.filter(notifications::object_id.eq(obj))
|
|
|
|
.get_result::<Notification>(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-09-08 00:29:50 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn get_url(&self, conn: &Connection) -> Option<String> {
|
|
|
|
match self.kind.as_ref() {
|
2018-12-29 09:36:07 +01:00
|
|
|
notification_kind::COMMENT => self.get_post(conn).and_then(|p| Some(format!("{}#comment-{}", p.url(conn).ok()?, self.object_id))),
|
2019-03-06 18:28:10 +01:00
|
|
|
notification_kind::FOLLOW => Some(format!("/@/{}/", self.get_actor(conn).ok()?.fqn)),
|
2018-12-29 09:36:07 +01:00
|
|
|
notification_kind::MENTION => Mention::get(conn, self.object_id).and_then(|mention|
|
|
|
|
mention.get_post(conn).and_then(|p| p.url(conn))
|
|
|
|
.or_else(|_| {
|
|
|
|
let comment = mention.get_comment(conn)?;
|
|
|
|
Ok(format!("{}#comment-{}", comment.get_post(conn)?.url(conn)?, comment.id))
|
2018-12-06 18:54:16 +01:00
|
|
|
})
|
2018-12-29 09:36:07 +01:00
|
|
|
).ok(),
|
2018-12-06 18:54:16 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_post(&self, conn: &Connection) -> Option<Post> {
|
|
|
|
match self.kind.as_ref() {
|
2018-12-29 09:36:07 +01:00
|
|
|
notification_kind::COMMENT => Comment::get(conn, self.object_id).and_then(|comment| comment.get_post(conn)).ok(),
|
|
|
|
notification_kind::LIKE => Like::get(conn, self.object_id).and_then(|like| Post::get(conn, like.post_id)).ok(),
|
|
|
|
notification_kind::RESHARE => Reshare::get(conn, self.object_id).and_then(|reshare| reshare.get_post(conn)).ok(),
|
2018-12-06 18:54:16 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn get_actor(&self, conn: &Connection) -> Result<User> {
|
|
|
|
Ok(match self.kind.as_ref() {
|
|
|
|
notification_kind::COMMENT => Comment::get(conn, self.object_id)?.get_author(conn)?,
|
|
|
|
notification_kind::FOLLOW => User::get(conn, Follow::get(conn, self.object_id)?.follower_id)?,
|
|
|
|
notification_kind::LIKE => User::get(conn, Like::get(conn, self.object_id)?.user_id)?,
|
|
|
|
notification_kind::MENTION => Mention::get(conn, self.object_id)?.get_user(conn)?,
|
|
|
|
notification_kind::RESHARE => Reshare::get(conn, self.object_id)?.get_user(conn)?,
|
2018-12-06 18:54:16 +01:00
|
|
|
_ => unreachable!("Notification::get_actor: Unknow type"),
|
2018-12-29 09:36:07 +01:00
|
|
|
})
|
2018-12-06 18:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn icon_class(&self) -> &'static str {
|
|
|
|
match self.kind.as_ref() {
|
|
|
|
notification_kind::COMMENT => "icon-message-circle",
|
|
|
|
notification_kind::FOLLOW => "icon-user-plus",
|
|
|
|
notification_kind::LIKE => "icon-heart",
|
|
|
|
notification_kind::MENTION => "icon-at-sign",
|
|
|
|
notification_kind::RESHARE => "icon-repeat",
|
|
|
|
_ => unreachable!("Notification::get_actor: Unknow type"),
|
|
|
|
}
|
2018-07-26 15:46:10 +02:00
|
|
|
}
|
2018-10-27 21:48:38 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn delete(&self, conn: &Connection) -> Result<()> {
|
2018-11-24 12:44:17 +01:00
|
|
|
diesel::delete(self)
|
|
|
|
.execute(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map(|_| ())
|
|
|
|
.map_err(Error::from)
|
2018-10-27 21:48:38 +02:00
|
|
|
}
|
2018-05-13 14:44:18 +02:00
|
|
|
}
|