2022-05-02 19:12:59 +02:00
|
|
|
use activitystreams::activity::{Announce, Create, Delete, Follow, Like, Undo, Update};
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
comments::Comment,
|
2021-01-30 11:24:16 +01:00
|
|
|
db_conn::DbConn,
|
2019-04-17 19:31:47 +02:00
|
|
|
follows, likes,
|
|
|
|
posts::{Post, PostUpdate},
|
|
|
|
reshares::Reshare,
|
|
|
|
users::User,
|
2021-01-30 11:24:16 +01:00
|
|
|
Error, CONFIG,
|
2019-04-17 19:31:47 +02:00
|
|
|
};
|
|
|
|
use plume_common::activity_pub::inbox::Inbox;
|
|
|
|
|
|
|
|
macro_rules! impl_into_inbox_result {
|
|
|
|
( $( $t:ty => $variant:ident ),+ ) => {
|
|
|
|
$(
|
|
|
|
impl From<$t> for InboxResult {
|
|
|
|
fn from(x: $t) -> InboxResult {
|
|
|
|
InboxResult::$variant(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum InboxResult {
|
|
|
|
Commented(Comment),
|
|
|
|
Followed(follows::Follow),
|
|
|
|
Liked(likes::Like),
|
|
|
|
Other,
|
|
|
|
Post(Post),
|
|
|
|
Reshared(Reshare),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<()> for InboxResult {
|
|
|
|
fn from(_: ()) -> InboxResult {
|
|
|
|
InboxResult::Other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_into_inbox_result! {
|
|
|
|
Comment => Commented,
|
|
|
|
follows::Follow => Followed,
|
|
|
|
likes::Like => Liked,
|
|
|
|
Post => Post,
|
|
|
|
Reshare => Reshared
|
|
|
|
}
|
|
|
|
|
2021-01-30 13:44:29 +01:00
|
|
|
pub fn inbox(conn: &DbConn, act: serde_json::Value) -> Result<InboxResult, Error> {
|
|
|
|
Inbox::handle(conn, act)
|
2022-05-02 19:12:59 +02:00
|
|
|
.with::<User, Announce, Post>(CONFIG.proxy())
|
|
|
|
.with::<User, Create, Comment>(CONFIG.proxy())
|
|
|
|
.with::<User, Create, Post>(CONFIG.proxy())
|
|
|
|
.with::<User, Delete, Comment>(CONFIG.proxy())
|
|
|
|
.with::<User, Delete, Post>(CONFIG.proxy())
|
|
|
|
.with::<User, Delete, User>(CONFIG.proxy())
|
|
|
|
.with::<User, Follow, User>(CONFIG.proxy())
|
|
|
|
.with::<User, Like, Post>(CONFIG.proxy())
|
|
|
|
.with::<User, Undo, Reshare>(CONFIG.proxy())
|
|
|
|
.with::<User, Undo, follows::Follow>(CONFIG.proxy())
|
|
|
|
.with::<User, Undo, likes::Like>(CONFIG.proxy())
|
|
|
|
.with::<User, Update, PostUpdate>(CONFIG.proxy())
|
2019-04-17 19:31:47 +02:00
|
|
|
.done()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::InboxResult;
|
|
|
|
use crate::blogs::tests::fill_database as blog_fill_db;
|
2021-01-30 15:15:07 +01:00
|
|
|
use crate::db_conn::DbConn;
|
2019-04-17 19:31:47 +02:00
|
|
|
use crate::safe_string::SafeString;
|
2021-01-30 15:15:07 +01:00
|
|
|
use crate::tests::db;
|
2019-04-17 19:31:47 +02:00
|
|
|
use diesel::Connection;
|
|
|
|
|
|
|
|
pub fn fill_database(
|
2021-01-30 15:15:07 +01:00
|
|
|
conn: &DbConn,
|
2019-04-17 19:31:47 +02:00
|
|
|
) -> (
|
|
|
|
Vec<crate::posts::Post>,
|
|
|
|
Vec<crate::users::User>,
|
|
|
|
Vec<crate::blogs::Blog>,
|
|
|
|
) {
|
|
|
|
use crate::post_authors::*;
|
|
|
|
use crate::posts::*;
|
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
let (users, blogs) = blog_fill_db(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
let post = Post::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
NewPost {
|
|
|
|
blog_id: blogs[0].id,
|
|
|
|
slug: "testing".to_owned(),
|
|
|
|
title: "Testing".to_owned(),
|
|
|
|
content: crate::safe_string::SafeString::new("Hello"),
|
|
|
|
published: true,
|
|
|
|
license: "WTFPL".to_owned(),
|
|
|
|
creation_date: None,
|
|
|
|
ap_url: format!("https://plu.me/~/{}/testing", blogs[0].actor_id),
|
2022-04-30 23:04:58 +02:00
|
|
|
subtitle: "Bye".to_string(),
|
|
|
|
source: "Hello".to_string(),
|
2019-04-17 19:31:47 +02:00
|
|
|
cover_id: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
PostAuthor::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
NewPostAuthor {
|
|
|
|
post_id: post.id,
|
|
|
|
author_id: users[0].id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
(vec![post], users, blogs)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn announce_post() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (posts, users, _) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/announce/1",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": posts[0].ap_url,
|
|
|
|
"type": "Announce",
|
|
|
|
});
|
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
match super::inbox(&conn, act).unwrap() {
|
2019-04-17 19:31:47 +02:00
|
|
|
super::InboxResult::Reshared(r) => {
|
|
|
|
assert_eq!(r.post_id, posts[0].id);
|
|
|
|
assert_eq!(r.user_id, users[0].id);
|
|
|
|
assert_eq!(r.ap_url, "https://plu.me/announce/1".to_owned());
|
|
|
|
}
|
|
|
|
_ => panic!("Unexpected result"),
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_comment() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (posts, users, _) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/activity",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": {
|
|
|
|
"type": "Note",
|
|
|
|
"id": "https://plu.me/comment/1",
|
|
|
|
"attributedTo": users[0].ap_url,
|
|
|
|
"inReplyTo": posts[0].ap_url,
|
|
|
|
"content": "Hello.",
|
|
|
|
"to": [plume_common::activity_pub::PUBLIC_VISIBILITY]
|
|
|
|
},
|
|
|
|
"type": "Create",
|
|
|
|
});
|
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
match super::inbox(&conn, act).unwrap() {
|
2019-04-17 19:31:47 +02:00
|
|
|
super::InboxResult::Commented(c) => {
|
|
|
|
assert_eq!(c.author_id, users[0].id);
|
|
|
|
assert_eq!(c.post_id, posts[0].id);
|
|
|
|
assert_eq!(c.in_response_to_id, None);
|
|
|
|
assert_eq!(c.content, SafeString::new("Hello."));
|
|
|
|
assert!(c.public_visibility);
|
|
|
|
}
|
|
|
|
_ => panic!("Unexpected result"),
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-01 00:37:47 +01:00
|
|
|
#[test]
|
|
|
|
fn spoof_comment() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2020-12-01 00:37:47 +01:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (posts, users, _) = fill_database(&conn);
|
2020-12-01 00:37:47 +01:00
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/activity",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": {
|
|
|
|
"type": "Note",
|
|
|
|
"id": "https://plu.me/comment/1",
|
|
|
|
"attributedTo": users[1].ap_url,
|
|
|
|
"inReplyTo": posts[0].ap_url,
|
|
|
|
"content": "Hello.",
|
|
|
|
"to": [plume_common::activity_pub::PUBLIC_VISIBILITY]
|
|
|
|
},
|
|
|
|
"type": "Create",
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(matches!(
|
2021-01-30 15:15:07 +01:00
|
|
|
super::inbox(&conn, act.clone()),
|
2020-12-01 00:37:47 +01:00
|
|
|
Err(super::Error::Inbox(
|
|
|
|
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
|
|
|
|
))
|
|
|
|
));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:35:20 +01:00
|
|
|
#[test]
|
|
|
|
fn spoof_comment_by_object_with_id() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2020-12-10 16:35:20 +01:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (posts, users, _) = fill_database(&conn);
|
2020-12-10 16:35:20 +01:00
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/activity",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": {
|
|
|
|
"type": "Note",
|
|
|
|
"id": "https://plu.me/comment/1",
|
|
|
|
"attributedTo": {
|
|
|
|
"id": users[1].ap_url
|
|
|
|
},
|
|
|
|
"inReplyTo": posts[0].ap_url,
|
|
|
|
"content": "Hello.",
|
|
|
|
"to": [plume_common::activity_pub::PUBLIC_VISIBILITY]
|
|
|
|
},
|
|
|
|
"type": "Create",
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(matches!(
|
2021-01-30 15:15:07 +01:00
|
|
|
super::inbox(&conn, act.clone()),
|
2020-12-10 16:35:20 +01:00
|
|
|
Err(super::Error::Inbox(
|
|
|
|
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
|
|
|
|
))
|
|
|
|
));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn spoof_comment_by_object_without_id() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2020-12-10 16:35:20 +01:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (posts, users, _) = fill_database(&conn);
|
2020-12-10 16:35:20 +01:00
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/activity",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": {
|
|
|
|
"type": "Note",
|
|
|
|
"id": "https://plu.me/comment/1",
|
|
|
|
"attributedTo": {},
|
|
|
|
"inReplyTo": posts[0].ap_url,
|
|
|
|
"content": "Hello.",
|
|
|
|
"to": [plume_common::activity_pub::PUBLIC_VISIBILITY]
|
|
|
|
},
|
|
|
|
"type": "Create",
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(matches!(
|
2021-01-30 15:15:07 +01:00
|
|
|
super::inbox(&conn, act.clone()),
|
2020-12-10 16:35:20 +01:00
|
|
|
Err(super::Error::Inbox(
|
|
|
|
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
|
|
|
|
))
|
|
|
|
));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
#[test]
|
|
|
|
fn create_post() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (_, users, blogs) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/activity",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": {
|
|
|
|
"type": "Article",
|
2022-04-30 23:04:58 +02:00
|
|
|
"id": "https://plu.me/~/BlogName/testing",
|
2019-04-17 19:31:47 +02:00
|
|
|
"attributedTo": [users[0].ap_url, blogs[0].ap_url],
|
|
|
|
"content": "Hello.",
|
|
|
|
"name": "My Article",
|
|
|
|
"summary": "Bye.",
|
|
|
|
"source": {
|
|
|
|
"content": "Hello.",
|
|
|
|
"mediaType": "text/markdown"
|
|
|
|
},
|
|
|
|
"published": "2014-12-12T12:12:12Z",
|
|
|
|
"to": [plume_common::activity_pub::PUBLIC_VISIBILITY]
|
|
|
|
},
|
|
|
|
"type": "Create",
|
|
|
|
});
|
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
match super::inbox(&conn, act).unwrap() {
|
2019-04-17 19:31:47 +02:00
|
|
|
super::InboxResult::Post(p) => {
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(p.is_author(&conn, users[0].id).unwrap());
|
2022-04-30 23:04:58 +02:00
|
|
|
assert_eq!(p.source, "Hello".to_owned());
|
2019-04-17 19:31:47 +02:00
|
|
|
assert_eq!(p.blog_id, blogs[0].id);
|
2022-04-30 23:04:58 +02:00
|
|
|
assert_eq!(p.content, SafeString::new("Hello"));
|
|
|
|
assert_eq!(p.subtitle, "Bye".to_owned());
|
|
|
|
assert_eq!(p.title, "Testing".to_owned());
|
2019-04-17 19:31:47 +02:00
|
|
|
}
|
|
|
|
_ => panic!("Unexpected result"),
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-01 00:37:47 +01:00
|
|
|
#[test]
|
|
|
|
fn spoof_post() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2020-12-01 00:37:47 +01:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (_, users, blogs) = fill_database(&conn);
|
2020-12-01 00:37:47 +01:00
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/activity",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": {
|
|
|
|
"type": "Article",
|
|
|
|
"id": "https://plu.me/~/Blog/my-article",
|
|
|
|
"attributedTo": [users[1].ap_url, blogs[0].ap_url],
|
|
|
|
"content": "Hello.",
|
|
|
|
"name": "My Article",
|
|
|
|
"summary": "Bye.",
|
|
|
|
"source": {
|
|
|
|
"content": "Hello.",
|
|
|
|
"mediaType": "text/markdown"
|
|
|
|
},
|
|
|
|
"published": "2014-12-12T12:12:12Z",
|
|
|
|
"to": [plume_common::activity_pub::PUBLIC_VISIBILITY]
|
|
|
|
},
|
|
|
|
"type": "Create",
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(matches!(
|
2021-01-30 15:15:07 +01:00
|
|
|
super::inbox(&conn, act.clone()),
|
2020-12-01 00:37:47 +01:00
|
|
|
Err(super::Error::Inbox(
|
2020-12-10 16:35:20 +01:00
|
|
|
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
|
|
|
|
))
|
|
|
|
));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn spoof_post_by_object_with_id() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2020-12-10 16:35:20 +01:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (_, users, blogs) = fill_database(&conn);
|
2020-12-10 16:35:20 +01:00
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/activity",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": {
|
|
|
|
"type": "Article",
|
|
|
|
"id": "https://plu.me/~/Blog/my-article",
|
|
|
|
"attributedTo": [
|
|
|
|
{"id": users[1].ap_url},
|
|
|
|
blogs[0].ap_url
|
|
|
|
],
|
|
|
|
"content": "Hello.",
|
|
|
|
"name": "My Article",
|
|
|
|
"summary": "Bye.",
|
|
|
|
"source": {
|
|
|
|
"content": "Hello.",
|
|
|
|
"mediaType": "text/markdown"
|
|
|
|
},
|
|
|
|
"published": "2014-12-12T12:12:12Z",
|
|
|
|
"to": [plume_common::activity_pub::PUBLIC_VISIBILITY]
|
|
|
|
},
|
|
|
|
"type": "Create",
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(matches!(
|
2021-01-30 15:15:07 +01:00
|
|
|
super::inbox(&conn, act.clone()),
|
2020-12-10 16:35:20 +01:00
|
|
|
Err(super::Error::Inbox(
|
|
|
|
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
|
|
|
|
))
|
|
|
|
));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn spoof_post_by_object_without_id() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2020-12-10 16:35:20 +01:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (_, users, blogs) = fill_database(&conn);
|
2020-12-10 16:35:20 +01:00
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/activity",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": {
|
|
|
|
"type": "Article",
|
|
|
|
"id": "https://plu.me/~/Blog/my-article",
|
|
|
|
"attributedTo": [{}, blogs[0].ap_url],
|
|
|
|
"content": "Hello.",
|
|
|
|
"name": "My Article",
|
|
|
|
"summary": "Bye.",
|
|
|
|
"source": {
|
|
|
|
"content": "Hello.",
|
|
|
|
"mediaType": "text/markdown"
|
|
|
|
},
|
|
|
|
"published": "2014-12-12T12:12:12Z",
|
|
|
|
"to": [plume_common::activity_pub::PUBLIC_VISIBILITY]
|
|
|
|
},
|
|
|
|
"type": "Create",
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(matches!(
|
2021-01-30 15:15:07 +01:00
|
|
|
super::inbox(&conn, act.clone()),
|
2020-12-10 16:35:20 +01:00
|
|
|
Err(super::Error::Inbox(
|
2020-12-01 00:37:47 +01:00
|
|
|
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
|
|
|
|
))
|
|
|
|
));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
#[test]
|
|
|
|
fn delete_comment() {
|
|
|
|
use crate::comments::*;
|
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (posts, users, _) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
Comment::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
NewComment {
|
|
|
|
content: SafeString::new("My comment"),
|
|
|
|
in_response_to_id: None,
|
|
|
|
post_id: posts[0].id,
|
|
|
|
author_id: users[0].id,
|
|
|
|
ap_url: Some("https://plu.me/comment/1".to_owned()),
|
|
|
|
sensitive: false,
|
|
|
|
spoiler_text: "spoiler".to_owned(),
|
|
|
|
public_visibility: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let fail_act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/delete",
|
|
|
|
"actor": users[1].ap_url, // Not the author of the comment, it should fail
|
|
|
|
"object": "https://plu.me/comment/1",
|
|
|
|
"type": "Delete",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, fail_act).is_err());
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let ok_act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/delete",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": "https://plu.me/comment/1",
|
|
|
|
"type": "Delete",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, ok_act).is_ok());
|
2019-04-17 19:31:47 +02:00
|
|
|
Ok(())
|
2019-10-07 19:08:20 +02:00
|
|
|
})
|
2019-04-17 19:31:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delete_post() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (posts, users, _) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let fail_act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/delete",
|
|
|
|
"actor": users[1].ap_url, // Not the author of the post, it should fail
|
|
|
|
"object": posts[0].ap_url,
|
|
|
|
"type": "Delete",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, fail_act).is_err());
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let ok_act = json!({
|
|
|
|
"id": "https://plu.me/comment/1/delete",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": posts[0].ap_url,
|
|
|
|
"type": "Delete",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, ok_act).is_ok());
|
2019-04-17 19:31:47 +02:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-28 19:01:41 +02:00
|
|
|
#[test]
|
|
|
|
fn delete_user() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-28 19:01:41 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (_, users, _) = fill_database(&conn);
|
2019-04-28 19:01:41 +02:00
|
|
|
|
|
|
|
let fail_act = json!({
|
|
|
|
"id": "https://plu.me/@/Admin#delete",
|
|
|
|
"actor": users[1].ap_url, // Not the same account
|
|
|
|
"object": users[0].ap_url,
|
|
|
|
"type": "Delete",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, fail_act).is_err());
|
2019-04-28 19:01:41 +02:00
|
|
|
|
|
|
|
let ok_act = json!({
|
|
|
|
"id": "https://plu.me/@/Admin#delete",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": users[0].ap_url,
|
|
|
|
"type": "Delete",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, ok_act).is_ok());
|
|
|
|
assert!(crate::users::User::get(&conn, users[0].id).is_err());
|
2019-04-28 19:01:41 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
#[test]
|
|
|
|
fn follow() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (_, users, _) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/follow/1",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": users[1].ap_url,
|
|
|
|
"type": "Follow",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
match super::inbox(&conn, act).unwrap() {
|
2019-04-17 19:31:47 +02:00
|
|
|
InboxResult::Followed(f) => {
|
|
|
|
assert_eq!(f.follower_id, users[0].id);
|
|
|
|
assert_eq!(f.following_id, users[1].id);
|
|
|
|
assert_eq!(f.ap_url, "https://plu.me/follow/1".to_owned());
|
|
|
|
}
|
|
|
|
_ => panic!("Unexpected result"),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn like() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (posts, users, _) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/like/1",
|
|
|
|
"actor": users[1].ap_url,
|
|
|
|
"object": posts[0].ap_url,
|
|
|
|
"type": "Like",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
match super::inbox(&conn, act).unwrap() {
|
2019-04-17 19:31:47 +02:00
|
|
|
InboxResult::Liked(l) => {
|
|
|
|
assert_eq!(l.user_id, users[1].id);
|
|
|
|
assert_eq!(l.post_id, posts[0].id);
|
|
|
|
assert_eq!(l.ap_url, "https://plu.me/like/1".to_owned());
|
|
|
|
}
|
|
|
|
_ => panic!("Unexpected result"),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn undo_reshare() {
|
|
|
|
use crate::reshares::*;
|
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (posts, users, _) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let announce = Reshare::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
NewReshare {
|
|
|
|
post_id: posts[0].id,
|
|
|
|
user_id: users[1].id,
|
|
|
|
ap_url: "https://plu.me/announce/1".to_owned(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let fail_act = json!({
|
|
|
|
"id": "https://plu.me/undo/1",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": announce.ap_url,
|
|
|
|
"type": "Undo",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, fail_act).is_err());
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let ok_act = json!({
|
|
|
|
"id": "https://plu.me/undo/1",
|
|
|
|
"actor": users[1].ap_url,
|
|
|
|
"object": announce.ap_url,
|
|
|
|
"type": "Undo",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, ok_act).is_ok());
|
2019-04-17 19:31:47 +02:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn undo_follow() {
|
|
|
|
use crate::follows::*;
|
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (_, users, _) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let follow = Follow::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
NewFollow {
|
|
|
|
follower_id: users[0].id,
|
|
|
|
following_id: users[1].id,
|
|
|
|
ap_url: "https://plu.me/follow/1".to_owned(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let fail_act = json!({
|
|
|
|
"id": "https://plu.me/undo/1",
|
|
|
|
"actor": users[2].ap_url,
|
|
|
|
"object": follow.ap_url,
|
|
|
|
"type": "Undo",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, fail_act).is_err());
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let ok_act = json!({
|
|
|
|
"id": "https://plu.me/undo/1",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": follow.ap_url,
|
|
|
|
"type": "Undo",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, ok_act).is_ok());
|
2019-04-17 19:31:47 +02:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn undo_like() {
|
|
|
|
use crate::likes::*;
|
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (posts, users, _) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let like = Like::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
NewLike {
|
|
|
|
post_id: posts[0].id,
|
|
|
|
user_id: users[1].id,
|
|
|
|
ap_url: "https://plu.me/like/1".to_owned(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let fail_act = json!({
|
|
|
|
"id": "https://plu.me/undo/1",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": like.ap_url,
|
|
|
|
"type": "Undo",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, fail_act).is_err());
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let ok_act = json!({
|
|
|
|
"id": "https://plu.me/undo/1",
|
|
|
|
"actor": users[1].ap_url,
|
|
|
|
"object": like.ap_url,
|
|
|
|
"type": "Undo",
|
|
|
|
});
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(super::inbox(&conn, ok_act).is_ok());
|
2019-04-17 19:31:47 +02:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn update_post() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = db();
|
2019-04-17 19:31:47 +02:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (posts, users, _) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
let act = json!({
|
|
|
|
"id": "https://plu.me/update/1",
|
|
|
|
"actor": users[0].ap_url,
|
|
|
|
"object": {
|
|
|
|
"type": "Article",
|
|
|
|
"id": posts[0].ap_url,
|
|
|
|
"name": "Mia Artikolo",
|
|
|
|
"summary": "Jes, mi parolas esperanton nun",
|
|
|
|
"content": "<b>Saluton</b>, mi skribas testojn",
|
|
|
|
"source": {
|
|
|
|
"mediaType": "text/markdown",
|
|
|
|
"content": "**Saluton**, mi skribas testojn"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"type": "Update",
|
|
|
|
});
|
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
super::inbox(&conn, act).unwrap();
|
2019-04-17 19:31:47 +02:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|