Delete notifications when deleting comments (#499)

* Implement find_for_comment for notifications

* Delete notifications when deleting a comment

This should tackle #463

* Apply rustfmt

* Implement `find_for_mention` and remove order by from `find_for_comment`

There is no need to order the notifications

* Delete notifications for mentions

* Fix notifications for comments and mentions
This commit is contained in:
Valentin Brandl 2019-03-26 12:45:17 +01:00 committed by Baptiste Gelez
parent f0d6b9d1e8
commit c7ee779f51
4 changed files with 58 additions and 23 deletions

View File

@ -330,9 +330,17 @@ impl<'a> Deletable<Connection, Delete> for Comment {
act.object_props
.set_to_link_vec(vec![Id::new(PUBLIC_VISIBILTY)])?;
for m in Mention::list_for_comment(&conn, self.id)? {
for m in Mention::list_for_comment(conn, self.id)? {
for n in Notification::find_for_mention(conn, &m)? {
n.delete(conn)?;
}
m.delete(conn)?;
}
for n in Notification::find_for_comment(conn, &self)? {
n.delete(conn)?;
}
diesel::update(comments::table)
.filter(comments::in_response_to_id.eq(self.id))
.set(comments::in_response_to_id.eq(self.in_response_to_id))

View File

@ -59,7 +59,7 @@ fn get_rocket_config() -> Result<RocketConfig, RocketError> {
pub struct LogoConfig {
pub main: String,
pub favicon: String,
pub other: Vec<Icon> //url, size, type
pub other: Vec<Icon>, //url, size, type
}
#[derive(Serialize)]
@ -82,14 +82,12 @@ impl Icon {
}
}
impl Default for LogoConfig {
fn default() -> Self {
let to_icon = |(src, sizes, image_type): &(&str, Option<&str>, Option<&str>)| Icon {
src: str::to_owned(src),
sizes: sizes.map(str::to_owned),
image_type: image_type.map(str::to_owned)
image_type: image_type.map(str::to_owned),
};
let icons = [
(
@ -132,15 +130,16 @@ impl Default for LogoConfig {
Some("512x512"),
Some("image/png"),
),
(
"icons/trwnh/feather/plumeFeather.svg",
None,
None,
)
].iter().map(to_icon).collect();
("icons/trwnh/feather/plumeFeather.svg", None, None),
]
.iter()
.map(to_icon)
.collect();
let custom_main = var("PLUME_LOGO").ok();
let custom_favicon = var("PLUME_LOGO_FAVICON").ok().or_else(|| custom_main.clone());
let custom_favicon = var("PLUME_LOGO_FAVICON")
.ok()
.or_else(|| custom_main.clone());
let other = if let Some(main) = custom_main.clone() {
let ext = |path: &str| match path.rsplitn(2, '.').next() {
Some("png") => Some("image/png".to_owned()),
@ -150,9 +149,13 @@ impl Default for LogoConfig {
_ => None,
};
let mut custom_icons = env::vars()
.filter_map(|(var, val)| if var.starts_with("PLUME_LOGO_") {
.filter_map(|(var, val)| {
if var.starts_with("PLUME_LOGO_") {
Some((var[11..].to_owned(), val))
} else { None })
} else {
None
}
})
.filter_map(|(var, val)| var.parse::<u64>().ok().map(|var| (var, val)))
.map(|(dim, src)| Icon {
image_type: ext(&src),
@ -171,8 +174,11 @@ impl Default for LogoConfig {
};
LogoConfig {
main: custom_main.unwrap_or_else(|| "icons/trwnh/feather/plumeFeather256.png".to_owned()),
favicon: custom_favicon.unwrap_or_else(|| "icons/trwnh/feather-filled/plumeFeatherFilled64.png".to_owned()),
main: custom_main
.unwrap_or_else(|| "icons/trwnh/feather/plumeFeather256.png".to_owned()),
favicon: custom_favicon.unwrap_or_else(|| {
"icons/trwnh/feather-filled/plumeFeatherFilled64.png".to_owned()
}),
other,
}
}

View File

@ -48,6 +48,22 @@ impl Notification {
.map_err(Error::from)
}
pub fn find_for_mention(conn: &Connection, mention: &Mention) -> Result<Vec<Notification>> {
notifications::table
.filter(notifications::kind.eq(notification_kind::MENTION))
.filter(notifications::object_id.eq(mention.id))
.load::<Notification>(conn)
.map_err(Error::from)
}
pub fn find_for_comment(conn: &Connection, comment: &Comment) -> Result<Vec<Notification>> {
notifications::table
.filter(notifications::kind.eq(notification_kind::COMMENT))
.filter(notifications::object_id.eq(comment.id))
.load::<Notification>(conn)
.map_err(Error::from)
}
pub fn count_for_user(conn: &Connection, user: &User) -> Result<i64> {
notifications::table
.filter(notifications::user_id.eq(user.id))

View File

@ -7,7 +7,11 @@ use validator::Validate;
use std::time::Duration;
use plume_common::{
activity_pub::{broadcast, inbox::Deletable, ActivityStream, ApRequest},
activity_pub::{
broadcast,
inbox::{Deletable, Notify},
ActivityStream, ApRequest,
},
utils,
};
use plume_models::{
@ -60,6 +64,7 @@ pub fn create(
},
)
.expect("comments::create: insert error");
comm.notify(&*conn).expect("comments::create: notify error");
let new_comment = comm
.create_activity(&*conn)
.expect("comments::create: activity error");
@ -70,8 +75,8 @@ pub fn create(
&*conn,
&Mention::build_activity(&*conn, &ment)
.expect("comments::create: build mention error"),
post.id,
true,
comm.id,
false,
true,
)
.expect("comments::create: mention save error");