2020-01-21 07:02:03 +01:00
|
|
|
use crate::{
|
2021-01-30 11:24:16 +01:00
|
|
|
ap_url, db_conn::DbConn, instance::*, medias::Media, posts::Post, safe_string::SafeString,
|
|
|
|
schema::blogs, users::User, Connection, Error, PlumeRocket, Result, CONFIG, ITEMS_PER_PAGE,
|
2020-01-21 07:02:03 +01:00
|
|
|
};
|
2019-10-30 11:22:28 +01:00
|
|
|
use activitypub::{
|
|
|
|
actor::Group,
|
|
|
|
collection::{OrderedCollection, OrderedCollectionPage},
|
|
|
|
object::Image,
|
|
|
|
CustomObject,
|
|
|
|
};
|
2022-02-22 16:48:06 +01:00
|
|
|
use activitystreams::{
|
|
|
|
actor::AsApActor,
|
|
|
|
base::AnyBase,
|
|
|
|
object::{kind::ImageType, ApObject, ApObjectExt, Image as Image07, ObjectExt},
|
|
|
|
prelude::*,
|
|
|
|
};
|
2018-09-27 23:06:40 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2019-10-07 19:08:20 +02:00
|
|
|
use diesel::{self, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl, SaveChangesDsl};
|
2018-05-19 09:39:59 +02:00
|
|
|
use openssl::{
|
|
|
|
hash::MessageDigest,
|
|
|
|
pkey::{PKey, Private},
|
|
|
|
rsa::Rsa,
|
2018-11-24 12:44:17 +01:00
|
|
|
sign::{Signer, Verifier},
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::activity_pub::{
|
2022-02-22 16:48:06 +01:00
|
|
|
inbox::{AsActor, FromId, FromId07},
|
|
|
|
sign, ActivityStream, ApSignature, CustomGroup as CustomGroup07, Id, IntoId, PublicKey, Source,
|
|
|
|
ToAsString, ToAsUri,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2020-01-21 07:02:03 +01:00
|
|
|
use url::Url;
|
|
|
|
use webfinger::*;
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-06-21 22:39:44 +02:00
|
|
|
pub type CustomGroup = CustomObject<ApSignature, Group>;
|
2018-04-23 12:29:27 +02:00
|
|
|
|
2019-03-12 19:40:54 +01:00
|
|
|
#[derive(Queryable, Identifiable, Clone, AsChangeset)]
|
2019-03-22 19:51:36 +01:00
|
|
|
#[changeset_options(treat_none_as_null = "true")]
|
2018-04-23 12:29:27 +02:00
|
|
|
pub struct Blog {
|
|
|
|
pub id: i32,
|
|
|
|
pub actor_id: String,
|
|
|
|
pub title: String,
|
|
|
|
pub summary: String,
|
|
|
|
pub outbox_url: String,
|
|
|
|
pub inbox_url: String,
|
2018-04-30 19:46:27 +02:00
|
|
|
pub instance_id: i32,
|
2018-09-27 23:06:40 +02:00
|
|
|
pub creation_date: NaiveDateTime,
|
2018-05-03 21:11:04 +02:00
|
|
|
pub ap_url: String,
|
|
|
|
pub private_key: Option<String>,
|
2018-11-24 12:44:17 +01:00
|
|
|
pub public_key: String,
|
2019-03-06 18:28:10 +01:00
|
|
|
pub fqn: String,
|
2019-03-22 19:51:36 +01:00
|
|
|
pub summary_html: SafeString,
|
|
|
|
pub icon_id: Option<i32>,
|
|
|
|
pub banner_id: Option<i32>,
|
2019-08-21 00:42:04 +02:00
|
|
|
pub theme: Option<String>,
|
2018-04-23 12:29:27 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 21:35:03 +01:00
|
|
|
#[derive(Default, Insertable)]
|
2018-04-23 12:29:27 +02:00
|
|
|
#[table_name = "blogs"]
|
|
|
|
pub struct NewBlog {
|
|
|
|
pub actor_id: String,
|
|
|
|
pub title: String,
|
|
|
|
pub summary: String,
|
|
|
|
pub outbox_url: String,
|
|
|
|
pub inbox_url: String,
|
2018-05-01 20:02:29 +02:00
|
|
|
pub instance_id: i32,
|
2018-05-03 21:11:04 +02:00
|
|
|
pub ap_url: String,
|
|
|
|
pub private_key: Option<String>,
|
2018-11-24 12:44:17 +01:00
|
|
|
pub public_key: String,
|
2019-03-22 19:51:36 +01:00
|
|
|
pub summary_html: SafeString,
|
|
|
|
pub icon_id: Option<i32>,
|
|
|
|
pub banner_id: Option<i32>,
|
2019-08-21 00:42:04 +02:00
|
|
|
pub theme: Option<String>,
|
2018-04-23 12:29:27 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 10:21:52 +01:00
|
|
|
const BLOG_PREFIX: &str = "~";
|
2018-06-21 19:42:17 +02:00
|
|
|
|
2018-04-23 12:29:27 +02:00
|
|
|
impl Blog {
|
2019-03-04 21:35:03 +01:00
|
|
|
insert!(blogs, NewBlog, |inserted, conn| {
|
|
|
|
let instance = inserted.get_instance(conn)?;
|
|
|
|
if inserted.outbox_url.is_empty() {
|
2019-03-20 17:56:17 +01:00
|
|
|
inserted.outbox_url = instance.compute_box(BLOG_PREFIX, &inserted.actor_id, "outbox");
|
2019-03-04 21:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if inserted.inbox_url.is_empty() {
|
2019-03-20 17:56:17 +01:00
|
|
|
inserted.inbox_url = instance.compute_box(BLOG_PREFIX, &inserted.actor_id, "inbox");
|
2019-03-04 21:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if inserted.ap_url.is_empty() {
|
2019-03-20 17:56:17 +01:00
|
|
|
inserted.ap_url = instance.compute_box(BLOG_PREFIX, &inserted.actor_id, "");
|
2019-03-04 21:35:03 +01:00
|
|
|
}
|
2019-03-06 18:28:10 +01:00
|
|
|
|
|
|
|
if inserted.fqn.is_empty() {
|
|
|
|
if instance.local {
|
|
|
|
inserted.fqn = inserted.actor_id.clone();
|
|
|
|
} else {
|
|
|
|
inserted.fqn = format!("{}@{}", inserted.actor_id, instance.public_domain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 21:35:03 +01:00
|
|
|
inserted.save_changes(conn).map_err(Error::from)
|
|
|
|
});
|
2018-06-18 15:44:23 +02:00
|
|
|
get!(blogs);
|
2018-11-26 10:21:52 +01:00
|
|
|
find_by!(blogs, find_by_ap_url, ap_url as &str);
|
|
|
|
find_by!(blogs, find_by_name, actor_id as &str, instance_id as i32);
|
2018-04-23 12:29:27 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn get_instance(&self, conn: &Connection) -> Result<Instance> {
|
|
|
|
Instance::get(conn, self.instance_id)
|
2018-06-21 19:53:57 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn list_authors(&self, conn: &Connection) -> Result<Vec<User>> {
|
2020-01-21 07:02:03 +01:00
|
|
|
use crate::schema::blog_authors;
|
|
|
|
use crate::schema::users;
|
2018-11-24 12:44:17 +01:00
|
|
|
let authors_ids = blog_authors::table
|
|
|
|
.filter(blog_authors::blog_id.eq(self.id))
|
|
|
|
.select(blog_authors::author_id);
|
|
|
|
users::table
|
|
|
|
.filter(users::id.eq_any(authors_ids))
|
2018-07-18 23:08:49 +02:00
|
|
|
.load::<User>(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-07-18 23:08:49 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn count_authors(&self, conn: &Connection) -> Result<i64> {
|
2020-01-21 07:02:03 +01:00
|
|
|
use crate::schema::blog_authors;
|
2018-12-14 23:16:18 +01:00
|
|
|
blog_authors::table
|
|
|
|
.filter(blog_authors::blog_id.eq(self.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-12-29 09:36:07 +01:00
|
|
|
pub fn find_for_author(conn: &Connection, author: &User) -> Result<Vec<Blog>> {
|
2020-01-21 07:02:03 +01:00
|
|
|
use crate::schema::blog_authors;
|
2018-11-24 12:44:17 +01:00
|
|
|
let author_ids = blog_authors::table
|
|
|
|
.filter(blog_authors::author_id.eq(author.id))
|
|
|
|
.select(blog_authors::blog_id);
|
|
|
|
blogs::table
|
|
|
|
.filter(blogs::id.eq_any(author_ids))
|
2018-06-10 19:55:08 +02:00
|
|
|
.load::<Blog>(conn)
|
2018-12-29 09:36:07 +01:00
|
|
|
.map_err(Error::from)
|
2018-06-10 19:55:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 11:24:16 +01:00
|
|
|
pub fn find_by_fqn(conn: &DbConn, fqn: &str) -> Result<Blog> {
|
2019-03-06 18:28:10 +01:00
|
|
|
let from_db = blogs::table
|
|
|
|
.filter(blogs::fqn.eq(fqn))
|
2021-01-30 11:24:16 +01:00
|
|
|
.first(&**conn)
|
2019-10-07 19:08:20 +02:00
|
|
|
.optional()?;
|
2019-03-06 18:28:10 +01:00
|
|
|
if let Some(from_db) = from_db {
|
|
|
|
Ok(from_db)
|
|
|
|
} else {
|
2021-01-30 11:24:16 +01:00
|
|
|
Blog::fetch_from_webfinger(conn, fqn)
|
2018-05-13 19:00:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 11:24:16 +01:00
|
|
|
fn fetch_from_webfinger(conn: &DbConn, acct: &str) -> Result<Blog> {
|
2019-07-31 11:38:49 +02:00
|
|
|
resolve_with_prefix(Prefix::Group, acct.to_owned(), true)?
|
2019-03-20 17:56:17 +01:00
|
|
|
.links
|
2018-12-29 09:36:07 +01:00
|
|
|
.into_iter()
|
|
|
|
.find(|l| l.mime_type == Some(String::from("application/activity+json")))
|
|
|
|
.ok_or(Error::Webfinger)
|
2021-11-27 23:53:13 +01:00
|
|
|
.and_then(|l| {
|
|
|
|
Blog::from_id(
|
|
|
|
conn,
|
|
|
|
&l.href.ok_or(Error::MissingApProperty)?,
|
|
|
|
None,
|
|
|
|
CONFIG.proxy(),
|
|
|
|
)
|
|
|
|
.map_err(|(_, e)| e)
|
|
|
|
})
|
2018-05-13 19:00:47 +02:00
|
|
|
}
|
|
|
|
|
2019-03-22 19:51:36 +01:00
|
|
|
pub fn to_activity(&self, conn: &Connection) -> Result<CustomGroup> {
|
2018-06-21 23:07:04 +02:00
|
|
|
let mut blog = Group::default();
|
2018-11-24 12:44:17 +01:00
|
|
|
blog.ap_actor_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_preferred_username_string(self.actor_id.clone())?;
|
2019-03-20 17:56:17 +01:00
|
|
|
blog.object_props.set_name_string(self.title.clone())?;
|
2018-11-24 12:44:17 +01:00
|
|
|
blog.ap_actor_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_outbox_string(self.outbox_url.clone())?;
|
2018-11-24 12:44:17 +01:00
|
|
|
blog.ap_actor_props
|
2018-12-29 09:36:07 +01:00
|
|
|
.set_inbox_string(self.inbox_url.clone())?;
|
2019-03-22 19:51:36 +01:00
|
|
|
blog.object_props
|
|
|
|
.set_summary_string(self.summary_html.to_string())?;
|
|
|
|
blog.ap_object_props.set_source_object(Source {
|
|
|
|
content: self.summary.clone(),
|
|
|
|
media_type: String::from("text/markdown"),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let mut icon = Image::default();
|
|
|
|
icon.object_props.set_url_string(
|
|
|
|
self.icon_id
|
2019-05-10 22:59:34 +02:00
|
|
|
.and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok())
|
2019-03-22 19:51:36 +01:00
|
|
|
.unwrap_or_default(),
|
|
|
|
)?;
|
|
|
|
icon.object_props.set_attributed_to_link(
|
|
|
|
self.icon_id
|
|
|
|
.and_then(|id| {
|
|
|
|
Media::get(conn, id)
|
|
|
|
.and_then(|m| Ok(User::get(conn, m.owner_id)?.into_id()))
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| Id::new(String::new())),
|
|
|
|
)?;
|
|
|
|
blog.object_props.set_icon_object(icon)?;
|
|
|
|
|
|
|
|
let mut banner = Image::default();
|
|
|
|
banner.object_props.set_url_string(
|
|
|
|
self.banner_id
|
2019-05-10 22:59:34 +02:00
|
|
|
.and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok())
|
2019-03-22 19:51:36 +01:00
|
|
|
.unwrap_or_default(),
|
|
|
|
)?;
|
|
|
|
banner.object_props.set_attributed_to_link(
|
|
|
|
self.banner_id
|
|
|
|
.and_then(|id| {
|
|
|
|
Media::get(conn, id)
|
|
|
|
.and_then(|m| Ok(User::get(conn, m.owner_id)?.into_id()))
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| Id::new(String::new())),
|
|
|
|
)?;
|
|
|
|
blog.object_props.set_image_object(banner)?;
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
blog.object_props.set_id_string(self.ap_url.clone())?;
|
2018-06-21 23:07:04 +02:00
|
|
|
|
|
|
|
let mut public_key = PublicKey::default();
|
2019-03-20 17:56:17 +01:00
|
|
|
public_key.set_id_string(format!("{}#main-key", self.ap_url))?;
|
|
|
|
public_key.set_owner_string(self.ap_url.clone())?;
|
|
|
|
public_key.set_public_key_pem_string(self.public_key.clone())?;
|
2018-06-21 23:07:04 +02:00
|
|
|
let mut ap_signature = ApSignature::default();
|
2019-03-20 17:56:17 +01:00
|
|
|
ap_signature.set_public_key_publickey(public_key)?;
|
2018-06-21 23:07:04 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(CustomGroup::new(blog, ap_signature))
|
2018-06-21 19:09:18 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn outbox(&self, conn: &Connection) -> Result<ActivityStream<OrderedCollection>> {
|
2022-02-22 17:48:19 +01:00
|
|
|
self.outbox_collection(conn)
|
|
|
|
.map(|coll| ActivityStream::new(coll))
|
|
|
|
}
|
|
|
|
pub fn outbox_collection(&self, conn: &Connection) -> Result<OrderedCollection> {
|
2018-05-16 20:20:44 +02:00
|
|
|
let mut coll = OrderedCollection::default();
|
2021-01-15 17:05:42 +01:00
|
|
|
coll.collection_props.items = serde_json::to_value(self.get_activities(conn))?;
|
2018-11-24 12:44:17 +01:00
|
|
|
coll.collection_props
|
2021-01-15 17:05:42 +01:00
|
|
|
.set_total_items_u64(self.get_activities(conn).len() as u64)?;
|
2019-10-30 11:22:28 +01:00
|
|
|
coll.collection_props
|
|
|
|
.set_first_link(Id::new(ap_url(&format!("{}?page=1", &self.outbox_url))))?;
|
|
|
|
coll.collection_props
|
|
|
|
.set_last_link(Id::new(ap_url(&format!(
|
|
|
|
"{}?page={}",
|
|
|
|
&self.outbox_url,
|
2021-01-15 17:05:42 +01:00
|
|
|
(self.get_activities(conn).len() as u64 + ITEMS_PER_PAGE as u64 - 1) as u64
|
2019-10-30 11:22:28 +01:00
|
|
|
/ ITEMS_PER_PAGE as u64
|
|
|
|
))))?;
|
2022-02-22 17:48:19 +01:00
|
|
|
Ok(coll)
|
2019-10-30 11:22:28 +01:00
|
|
|
}
|
|
|
|
pub fn outbox_page(
|
|
|
|
&self,
|
|
|
|
conn: &Connection,
|
|
|
|
(min, max): (i32, i32),
|
|
|
|
) -> Result<ActivityStream<OrderedCollectionPage>> {
|
|
|
|
let mut coll = OrderedCollectionPage::default();
|
2021-11-27 23:53:13 +01:00
|
|
|
let acts = self.get_activity_page(conn, (min, max));
|
2019-10-30 11:22:28 +01:00
|
|
|
//This still doesn't do anything because the outbox
|
|
|
|
//doesn't do anything yet
|
|
|
|
coll.collection_page_props.set_next_link(Id::new(&format!(
|
|
|
|
"{}?page={}",
|
|
|
|
&self.outbox_url,
|
|
|
|
min / ITEMS_PER_PAGE + 1
|
|
|
|
)))?;
|
|
|
|
coll.collection_page_props.set_prev_link(Id::new(&format!(
|
|
|
|
"{}?page={}",
|
|
|
|
&self.outbox_url,
|
|
|
|
min / ITEMS_PER_PAGE - 1
|
|
|
|
)))?;
|
|
|
|
coll.collection_props.items = serde_json::to_value(acts)?;
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(ActivityStream::new(coll))
|
2018-04-29 19:49:56 +02:00
|
|
|
}
|
2021-01-15 17:05:42 +01:00
|
|
|
fn get_activities(&self, _conn: &Connection) -> Vec<serde_json::Value> {
|
|
|
|
vec![]
|
2018-04-29 19:49:56 +02:00
|
|
|
}
|
2019-10-30 11:22:28 +01:00
|
|
|
fn get_activity_page(
|
|
|
|
&self,
|
|
|
|
_conn: &Connection,
|
|
|
|
(_min, _max): (i32, i32),
|
2021-01-15 17:05:42 +01:00
|
|
|
) -> Vec<serde_json::Value> {
|
|
|
|
vec![]
|
2019-10-30 11:22:28 +01:00
|
|
|
}
|
2018-05-03 21:11:04 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn get_keypair(&self) -> Result<PKey<Private>> {
|
2019-03-20 17:56:17 +01:00
|
|
|
PKey::from_rsa(Rsa::private_key_from_pem(
|
2021-11-27 23:53:13 +01:00
|
|
|
self.private_key
|
|
|
|
.clone()
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
.as_ref(),
|
2019-03-20 17:56:17 +01:00
|
|
|
)?)
|
|
|
|
.map_err(Error::from)
|
2018-05-03 21:11:04 +02:00
|
|
|
}
|
2018-06-18 23:50:40 +02:00
|
|
|
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn webfinger(&self, conn: &Connection) -> Result<Webfinger> {
|
|
|
|
Ok(Webfinger {
|
2018-11-24 12:44:17 +01:00
|
|
|
subject: format!(
|
|
|
|
"acct:{}@{}",
|
|
|
|
self.actor_id,
|
2018-12-29 09:36:07 +01:00
|
|
|
self.get_instance(conn)?.public_domain
|
2018-11-24 12:44:17 +01:00
|
|
|
),
|
2018-06-21 16:48:54 +02:00
|
|
|
aliases: vec![self.ap_url.clone()],
|
2018-06-18 23:50:40 +02:00
|
|
|
links: vec![
|
|
|
|
Link {
|
|
|
|
rel: String::from("http://webfinger.net/rel/profile-page"),
|
|
|
|
mime_type: None,
|
2018-07-26 21:35:35 +02:00
|
|
|
href: Some(self.ap_url.clone()),
|
2018-11-24 12:44:17 +01:00
|
|
|
template: None,
|
2018-06-18 23:50:40 +02:00
|
|
|
},
|
|
|
|
Link {
|
|
|
|
rel: String::from("http://schemas.google.com/g/2010#updates-from"),
|
|
|
|
mime_type: Some(String::from("application/atom+xml")),
|
2018-12-29 09:36:07 +01:00
|
|
|
href: Some(self.get_instance(conn)?.compute_box(
|
2018-11-24 12:44:17 +01:00
|
|
|
BLOG_PREFIX,
|
2018-11-26 10:21:52 +01:00
|
|
|
&self.actor_id,
|
2018-11-24 12:44:17 +01:00
|
|
|
"feed.atom",
|
|
|
|
)),
|
|
|
|
template: None,
|
2018-06-18 23:50:40 +02:00
|
|
|
},
|
|
|
|
Link {
|
|
|
|
rel: String::from("self"),
|
|
|
|
mime_type: Some(String::from("application/activity+json")),
|
2018-07-26 21:35:35 +02:00
|
|
|
href: Some(self.ap_url.clone()),
|
2018-11-24 12:44:17 +01:00
|
|
|
template: None,
|
|
|
|
},
|
|
|
|
],
|
2018-12-29 09:36:07 +01:00
|
|
|
})
|
2018-06-18 23:50:40 +02:00
|
|
|
}
|
2018-06-21 19:23:01 +02:00
|
|
|
|
2019-03-22 19:51:36 +01:00
|
|
|
pub fn icon_url(&self, conn: &Connection) -> String {
|
|
|
|
self.icon_id
|
2019-05-10 22:59:34 +02:00
|
|
|
.and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok())
|
2019-08-21 00:42:04 +02:00
|
|
|
.unwrap_or_else(|| "/static/images/default-avatar.png".to_string())
|
2019-03-22 19:51:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn banner_url(&self, conn: &Connection) -> Option<String> {
|
|
|
|
self.banner_id
|
|
|
|
.and_then(|i| Media::get(conn, i).ok())
|
2019-05-10 22:59:34 +02:00
|
|
|
.and_then(|c| c.url().ok())
|
2019-03-22 19:51:36 +01:00
|
|
|
}
|
|
|
|
|
2021-01-07 14:51:46 +01:00
|
|
|
pub fn delete(&self, conn: &Connection) -> Result<()> {
|
2021-11-27 23:53:13 +01:00
|
|
|
for post in Post::get_for_blog(conn, self)? {
|
2021-01-07 14:51:46 +01:00
|
|
|
post.delete(conn)?;
|
2018-10-20 15:03:59 +02:00
|
|
|
}
|
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-20 15:03:59 +02:00
|
|
|
}
|
2018-04-23 12:29:27 +02:00
|
|
|
}
|
2018-04-23 14:00:11 +02:00
|
|
|
|
2018-05-19 00:04:30 +02:00
|
|
|
impl IntoId for Blog {
|
|
|
|
fn into_id(self) -> Id {
|
2018-05-18 10:04:40 +02:00
|
|
|
Id::new(self.ap_url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 11:24:16 +01:00
|
|
|
impl FromId<DbConn> for Blog {
|
2019-04-17 19:31:47 +02:00
|
|
|
type Error = Error;
|
|
|
|
type Object = CustomGroup;
|
|
|
|
|
2021-01-30 11:24:16 +01:00
|
|
|
fn from_db(conn: &DbConn, id: &str) -> Result<Self> {
|
2021-11-27 23:53:13 +01:00
|
|
|
Self::find_by_ap_url(conn, id)
|
2019-04-17 19:31:47 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 11:24:16 +01:00
|
|
|
fn from_activity(conn: &DbConn, acct: CustomGroup) -> Result<Self> {
|
2019-04-17 19:31:47 +02:00
|
|
|
let url = Url::parse(&acct.object.object_props.id_string()?)?;
|
2021-11-27 23:53:13 +01:00
|
|
|
let inst = url.host_str().ok_or(Error::Url)?;
|
2021-01-30 11:24:16 +01:00
|
|
|
let instance = Instance::find_by_domain(conn, inst).or_else(|_| {
|
2019-04-17 19:31:47 +02:00
|
|
|
Instance::insert(
|
2021-01-30 11:24:16 +01:00
|
|
|
conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
NewInstance {
|
|
|
|
public_domain: inst.to_owned(),
|
|
|
|
name: inst.to_owned(),
|
|
|
|
local: false,
|
|
|
|
// We don't really care about all the following for remote instances
|
|
|
|
long_description: SafeString::new(""),
|
|
|
|
short_description: SafeString::new(""),
|
|
|
|
default_license: String::new(),
|
|
|
|
open_registrations: true,
|
|
|
|
short_description_html: String::new(),
|
|
|
|
long_description_html: String::new(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
let icon_id = acct
|
|
|
|
.object
|
|
|
|
.object_props
|
|
|
|
.icon_image()
|
|
|
|
.ok()
|
|
|
|
.and_then(|icon| {
|
2019-05-25 20:23:45 +02:00
|
|
|
let owner = icon.object_props.attributed_to_link::<Id>().ok()?;
|
2019-04-17 19:31:47 +02:00
|
|
|
Media::save_remote(
|
2021-01-30 11:24:16 +01:00
|
|
|
conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
icon.object_props.url_string().ok()?,
|
2021-01-30 11:24:16 +01:00
|
|
|
&User::from_id(conn, &owner, None, CONFIG.proxy()).ok()?,
|
2019-04-17 19:31:47 +02:00
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.map(|m| m.id);
|
|
|
|
|
|
|
|
let banner_id = acct
|
|
|
|
.object
|
|
|
|
.object_props
|
|
|
|
.image_image()
|
|
|
|
.ok()
|
|
|
|
.and_then(|banner| {
|
2019-05-25 20:23:45 +02:00
|
|
|
let owner = banner.object_props.attributed_to_link::<Id>().ok()?;
|
2019-04-17 19:31:47 +02:00
|
|
|
Media::save_remote(
|
2021-01-30 11:24:16 +01:00
|
|
|
conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
banner.object_props.url_string().ok()?,
|
2021-01-30 11:24:16 +01:00
|
|
|
&User::from_id(conn, &owner, None, CONFIG.proxy()).ok()?,
|
2019-04-17 19:31:47 +02:00
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.map(|m| m.id);
|
|
|
|
|
|
|
|
let name = acct.object.ap_actor_props.preferred_username_string()?;
|
|
|
|
if name.contains(&['<', '>', '&', '@', '\'', '"', ' ', '\t'][..]) {
|
|
|
|
return Err(Error::InvalidValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
Blog::insert(
|
2021-01-30 11:24:16 +01:00
|
|
|
conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
NewBlog {
|
|
|
|
actor_id: name.clone(),
|
|
|
|
title: acct.object.object_props.name_string().unwrap_or(name),
|
|
|
|
outbox_url: acct.object.ap_actor_props.outbox_string()?,
|
|
|
|
inbox_url: acct.object.ap_actor_props.inbox_string()?,
|
|
|
|
summary: acct
|
|
|
|
.object
|
|
|
|
.ap_object_props
|
|
|
|
.source_object::<Source>()
|
|
|
|
.map(|s| s.content)
|
|
|
|
.unwrap_or_default(),
|
|
|
|
instance_id: instance.id,
|
|
|
|
ap_url: acct.object.object_props.id_string()?,
|
|
|
|
public_key: acct
|
|
|
|
.custom_props
|
|
|
|
.public_key_publickey()?
|
|
|
|
.public_key_pem_string()?,
|
|
|
|
private_key: None,
|
|
|
|
banner_id,
|
|
|
|
icon_id,
|
|
|
|
summary_html: SafeString::new(
|
|
|
|
&acct
|
|
|
|
.object
|
|
|
|
.object_props
|
|
|
|
.summary_string()
|
|
|
|
.unwrap_or_default(),
|
|
|
|
),
|
2019-08-21 00:42:04 +02:00
|
|
|
theme: None,
|
2019-04-17 19:31:47 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2021-11-24 14:50:16 +01:00
|
|
|
|
|
|
|
fn get_sender() -> &'static dyn sign::Signer {
|
|
|
|
Instance::get_local_instance_user().expect("Failed to local instance user")
|
|
|
|
}
|
2019-04-17 19:31:47 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 16:48:06 +01:00
|
|
|
impl FromId07<DbConn> for Blog {
|
|
|
|
type Error = Error;
|
|
|
|
type Object = CustomGroup07;
|
|
|
|
|
|
|
|
fn from_db07(conn: &DbConn, id: &str) -> Result<Self> {
|
|
|
|
Self::find_by_ap_url(conn, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_activity07(conn: &DbConn, acct: CustomGroup07) -> Result<Self> {
|
|
|
|
let (name, outbox_url, inbox_url) = {
|
|
|
|
let actor = acct.ap_actor_ref();
|
|
|
|
let name = actor
|
|
|
|
.preferred_username()
|
|
|
|
.ok_or(Error::MissingApProperty)?
|
|
|
|
.to_string();
|
|
|
|
if name.contains(&['<', '>', '&', '@', '\'', '"', ' ', '\t'][..]) {
|
|
|
|
return Err(Error::InvalidValue);
|
|
|
|
}
|
|
|
|
(
|
|
|
|
name,
|
|
|
|
actor.outbox()?.ok_or(Error::MissingApProperty)?.to_string(),
|
|
|
|
actor.inbox()?.to_string(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut new_blog = NewBlog {
|
|
|
|
actor_id: name.to_string(),
|
|
|
|
outbox_url,
|
|
|
|
inbox_url,
|
|
|
|
public_key: acct.ext_one.public_key.public_key_pem.to_string(),
|
|
|
|
private_key: None,
|
|
|
|
theme: None,
|
|
|
|
..NewBlog::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let object = ApObject::new(acct.inner);
|
|
|
|
new_blog.title = object
|
|
|
|
.name()
|
|
|
|
.and_then(|name| name.to_as_string())
|
|
|
|
.unwrap_or_else(|| name);
|
|
|
|
new_blog.summary_html = SafeString::new(
|
|
|
|
&object
|
|
|
|
.summary()
|
|
|
|
.and_then(|summary| summary.to_as_string())
|
|
|
|
.unwrap_or_default(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let icon_id = object
|
|
|
|
.icon()
|
|
|
|
.and_then(|icons| {
|
|
|
|
icons.iter().next().and_then(|icon| {
|
|
|
|
let icon = icon.to_owned().extend::<Image07, ImageType>().ok()??;
|
|
|
|
let owner = icon.attributed_to()?.to_as_uri()?;
|
|
|
|
Media::save_remote(
|
|
|
|
conn,
|
|
|
|
icon.url()?.to_as_uri()?,
|
|
|
|
&User::from_id07(conn, &owner, None, CONFIG.proxy()).ok()?,
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.map(|m| m.id);
|
|
|
|
new_blog.icon_id = icon_id;
|
|
|
|
|
|
|
|
let banner_id = object
|
|
|
|
.image()
|
|
|
|
.and_then(|banners| {
|
|
|
|
banners.iter().next().and_then(|banner| {
|
|
|
|
let banner = banner.to_owned().extend::<Image07, ImageType>().ok()??;
|
|
|
|
let owner = banner.attributed_to()?.to_as_uri()?;
|
|
|
|
Media::save_remote(
|
|
|
|
conn,
|
|
|
|
banner.url()?.to_as_uri()?,
|
|
|
|
&User::from_id(conn, &owner, None, CONFIG.proxy()).ok()?,
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.map(|m| m.id);
|
|
|
|
new_blog.banner_id = banner_id;
|
|
|
|
|
|
|
|
let source = object
|
|
|
|
.source()
|
|
|
|
.and_then(|s| s.as_xsd_string())
|
|
|
|
.unwrap_or_default()
|
|
|
|
.to_string();
|
|
|
|
new_blog.summary = source;
|
|
|
|
|
|
|
|
let any_base = AnyBase::from_extended(object)?;
|
|
|
|
let id = any_base.id().ok_or(Error::MissingApProperty)?;
|
|
|
|
new_blog.ap_url = id.to_string();
|
|
|
|
|
|
|
|
let inst = id
|
|
|
|
.authority_components()
|
|
|
|
.ok_or(Error::Url)?
|
|
|
|
.host()
|
|
|
|
.to_string();
|
|
|
|
let instance = Instance::find_by_domain(conn, &inst).or_else(|_| {
|
|
|
|
Instance::insert(
|
|
|
|
conn,
|
|
|
|
NewInstance {
|
|
|
|
public_domain: inst.to_owned(),
|
|
|
|
name: inst.to_owned(),
|
|
|
|
local: false,
|
|
|
|
// We don't really care about all the following for remote instances
|
|
|
|
long_description: SafeString::new(""),
|
|
|
|
short_description: SafeString::new(""),
|
|
|
|
default_license: String::new(),
|
|
|
|
open_registrations: true,
|
|
|
|
short_description_html: String::new(),
|
|
|
|
long_description_html: String::new(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
new_blog.instance_id = instance.id;
|
|
|
|
|
|
|
|
Blog::insert(conn, new_blog)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_sender07() -> &'static dyn sign::Signer {
|
|
|
|
Instance::get_local_instance_user().expect("Failed to local instance user")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 19:31:47 +02:00
|
|
|
impl AsActor<&PlumeRocket> for Blog {
|
2018-05-18 10:04:40 +02:00
|
|
|
fn get_inbox_url(&self) -> String {
|
|
|
|
self.inbox_url.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_shared_inbox_url(&self) -> Option<String> {
|
|
|
|
None
|
|
|
|
}
|
2018-07-18 15:49:13 +02:00
|
|
|
|
|
|
|
fn is_local(&self) -> bool {
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local()
|
|
|
|
.map(|i| self.instance_id == i.id)
|
|
|
|
.unwrap_or(false)
|
2018-07-18 15:49:13 +02:00
|
|
|
}
|
2018-05-18 10:04:40 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 21:11:04 +02:00
|
|
|
impl sign::Signer for Blog {
|
2018-06-21 17:25:32 +02:00
|
|
|
fn get_key_id(&self) -> String {
|
2018-06-21 16:48:54 +02:00
|
|
|
format!("{}#main-key", self.ap_url)
|
2018-05-03 21:11:04 +02:00
|
|
|
}
|
|
|
|
|
2021-11-24 13:41:44 +01:00
|
|
|
fn sign(&self, to_sign: &str) -> sign::Result<Vec<u8>> {
|
|
|
|
let key = self.get_keypair().map_err(|_| sign::Error())?;
|
2019-03-20 17:56:17 +01:00
|
|
|
let mut signer = Signer::new(MessageDigest::sha256(), &key)?;
|
|
|
|
signer.update(to_sign.as_bytes())?;
|
2021-11-24 13:41:44 +01:00
|
|
|
signer.sign_to_vec().map_err(sign::Error::from)
|
2018-05-03 21:11:04 +02:00
|
|
|
}
|
2018-09-28 23:18:01 +02:00
|
|
|
|
2021-11-24 13:41:44 +01:00
|
|
|
fn verify(&self, data: &str, signature: &[u8]) -> sign::Result<bool> {
|
2019-03-20 17:56:17 +01:00
|
|
|
let key = PKey::from_rsa(Rsa::public_key_from_pem(self.public_key.as_ref())?)?;
|
2018-12-29 09:36:07 +01:00
|
|
|
let mut verifier = Verifier::new(MessageDigest::sha256(), &key)?;
|
2019-03-20 17:56:17 +01:00
|
|
|
verifier.update(data.as_bytes())?;
|
2021-11-24 13:41:44 +01:00
|
|
|
verifier.verify(signature).map_err(sign::Error::from)
|
2018-09-28 23:18:01 +02:00
|
|
|
}
|
2018-05-03 21:11:04 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 15:12:59 +02:00
|
|
|
impl NewBlog {
|
|
|
|
pub fn new_local(
|
|
|
|
actor_id: String,
|
|
|
|
title: String,
|
|
|
|
summary: String,
|
2018-11-24 12:44:17 +01:00
|
|
|
instance_id: i32,
|
2018-12-29 09:36:07 +01:00
|
|
|
) -> Result<NewBlog> {
|
2018-05-03 21:11:04 +02:00
|
|
|
let (pub_key, priv_key) = sign::gen_keypair();
|
2018-12-29 09:36:07 +01:00
|
|
|
Ok(NewBlog {
|
2018-11-26 10:21:52 +01:00
|
|
|
actor_id,
|
|
|
|
title,
|
|
|
|
summary,
|
|
|
|
instance_id,
|
2018-12-29 09:36:07 +01:00
|
|
|
public_key: String::from_utf8(pub_key).or(Err(Error::Signature))?,
|
|
|
|
private_key: Some(String::from_utf8(priv_key).or(Err(Error::Signature))?),
|
2019-03-04 21:35:03 +01:00
|
|
|
..NewBlog::default()
|
2018-12-29 09:36:07 +01:00
|
|
|
})
|
2018-04-23 15:12:59 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::*;
|
2020-01-21 07:02:03 +01:00
|
|
|
use crate::{
|
2022-02-22 18:09:11 +01:00
|
|
|
blog_authors::*,
|
|
|
|
instance::tests as instance_tests,
|
|
|
|
medias::NewMedia,
|
|
|
|
post_authors::{NewPostAuthor, PostAuthor},
|
|
|
|
posts::{NewPost, Post},
|
|
|
|
tests::db,
|
|
|
|
users::tests as usersTests,
|
|
|
|
Connection as Conn, ITEMS_PER_PAGE,
|
2020-01-21 07:02:03 +01:00
|
|
|
};
|
2022-02-22 17:07:57 +01:00
|
|
|
use assert_json_diff::assert_json_eq;
|
2018-11-24 12:44:17 +01:00
|
|
|
use diesel::Connection;
|
2022-02-22 17:07:57 +01:00
|
|
|
use serde_json::to_value;
|
2018-11-24 12:44:17 +01:00
|
|
|
|
2018-12-09 18:44:26 +01:00
|
|
|
pub(crate) fn fill_database(conn: &Conn) -> (Vec<User>, Vec<Blog>) {
|
2018-11-24 12:44:17 +01:00
|
|
|
instance_tests::fill_database(conn);
|
|
|
|
let users = usersTests::fill_database(conn);
|
2019-03-20 17:56:17 +01:00
|
|
|
let blog1 = Blog::insert(
|
|
|
|
conn,
|
|
|
|
NewBlog::new_local(
|
|
|
|
"BlogName".to_owned(),
|
|
|
|
"Blog name".to_owned(),
|
|
|
|
"This is a small blog".to_owned(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local().unwrap().id,
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let blog2 = Blog::insert(
|
|
|
|
conn,
|
|
|
|
NewBlog::new_local(
|
2018-11-24 12:44:17 +01:00
|
|
|
"MyBlog".to_owned(),
|
|
|
|
"My blog".to_owned(),
|
|
|
|
"Welcome to my blog".to_owned(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local().unwrap().id,
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let blog3 = Blog::insert(
|
|
|
|
conn,
|
|
|
|
NewBlog::new_local(
|
2018-11-24 12:44:17 +01:00
|
|
|
"WhyILikePlume".to_owned(),
|
|
|
|
"Why I like Plume".to_owned(),
|
|
|
|
"In this blog I will explay you why I like Plume so much".to_owned(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local().unwrap().id,
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
2018-12-09 18:44:26 +01:00
|
|
|
blog_id: blog1.id,
|
2018-11-24 12:44:17 +01:00
|
|
|
author_id: users[0].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
2018-12-09 18:44:26 +01:00
|
|
|
blog_id: blog1.id,
|
2018-11-24 12:44:17 +01:00
|
|
|
author_id: users[1].id,
|
|
|
|
is_owner: false,
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
2018-12-09 18:44:26 +01:00
|
|
|
blog_id: blog2.id,
|
2018-11-24 12:44:17 +01:00
|
|
|
author_id: users[1].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
BlogAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewBlogAuthor {
|
2018-12-09 18:44:26 +01:00
|
|
|
blog_id: blog3.id,
|
2018-11-24 12:44:17 +01:00
|
|
|
author_id: users[2].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-02-22 18:09:11 +01:00
|
|
|
|
|
|
|
for i in 1..(ITEMS_PER_PAGE * 4 + 3) {
|
|
|
|
let title = format!("Post {}", i);
|
|
|
|
let content = format!("Content for post {}.", i);
|
|
|
|
let post = Post::insert(
|
|
|
|
conn,
|
|
|
|
NewPost {
|
|
|
|
blog_id: blog1.id,
|
|
|
|
slug: title.clone(),
|
|
|
|
title: title.clone(),
|
|
|
|
content: SafeString::new(&content),
|
|
|
|
published: true,
|
|
|
|
license: "CC-0".into(),
|
|
|
|
creation_date: None,
|
|
|
|
ap_url: format!("{}/{}", blog1.ap_url, title),
|
|
|
|
subtitle: "".into(),
|
|
|
|
source: content,
|
|
|
|
cover_id: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
PostAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewPostAuthor {
|
|
|
|
post_id: post.id,
|
|
|
|
author_id: users[0].id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
(users, vec![blog1, blog2, blog3])
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_instance() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
fill_database(&conn);
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
let blog = Blog::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-11-24 12:44:17 +01:00
|
|
|
NewBlog::new_local(
|
|
|
|
"SomeName".to_owned(),
|
|
|
|
"Some name".to_owned(),
|
|
|
|
"This is some blog".to_owned(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local().unwrap().id,
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
assert_eq!(
|
2021-01-30 15:15:07 +01:00
|
|
|
blog.get_instance(&conn).unwrap().id,
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local().unwrap().id
|
2019-03-20 17:56:17 +01:00
|
|
|
);
|
2018-11-24 12:44:17 +01:00
|
|
|
// TODO add tests for remote instance
|
|
|
|
Ok(())
|
2019-10-07 19:08:20 +02:00
|
|
|
})
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn authors() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (user, _) = fill_database(&conn);
|
2018-11-24 12:44:17 +01:00
|
|
|
|
2018-12-09 18:44:26 +01:00
|
|
|
let b1 = Blog::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-12-09 18:44:26 +01:00
|
|
|
NewBlog::new_local(
|
|
|
|
"SomeName".to_owned(),
|
|
|
|
"Some name".to_owned(),
|
|
|
|
"This is some blog".to_owned(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local().unwrap().id,
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-12-09 18:44:26 +01:00
|
|
|
let b2 = Blog::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-12-09 18:44:26 +01:00
|
|
|
NewBlog::new_local(
|
|
|
|
"Blog".to_owned(),
|
|
|
|
"Blog".to_owned(),
|
|
|
|
"I've named my blog Blog".to_owned(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local().unwrap().id,
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let blog = vec![b1, b2];
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
BlogAuthor::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-11-24 12:44:17 +01:00
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[0].id,
|
|
|
|
author_id: user[0].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
BlogAuthor::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-11-24 12:44:17 +01:00
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[0].id,
|
|
|
|
author_id: user[1].id,
|
|
|
|
is_owner: false,
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
BlogAuthor::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-11-24 12:44:17 +01:00
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[1].id,
|
|
|
|
author_id: user[0].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(blog[0]
|
2021-01-30 15:15:07 +01:00
|
|
|
.list_authors(&conn)
|
2019-03-20 17:56:17 +01:00
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|a| a.id == user[0].id));
|
|
|
|
assert!(blog[0]
|
2021-01-30 15:15:07 +01:00
|
|
|
.list_authors(&conn)
|
2019-03-20 17:56:17 +01:00
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|a| a.id == user[1].id));
|
|
|
|
assert!(blog[1]
|
2021-01-30 15:15:07 +01:00
|
|
|
.list_authors(&conn)
|
2019-03-20 17:56:17 +01:00
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|a| a.id == user[0].id));
|
|
|
|
assert!(!blog[1]
|
2021-01-30 15:15:07 +01:00
|
|
|
.list_authors(&conn)
|
2019-03-20 17:56:17 +01:00
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|a| a.id == user[1].id));
|
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(Blog::find_for_author(&conn, &user[0])
|
2019-03-20 17:56:17 +01:00
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|b| b.id == blog[0].id));
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(Blog::find_for_author(&conn, &user[1])
|
2019-03-20 17:56:17 +01:00
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|b| b.id == blog[0].id));
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(Blog::find_for_author(&conn, &user[0])
|
2019-03-20 17:56:17 +01:00
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|b| b.id == blog[1].id));
|
2021-01-30 15:15:07 +01:00
|
|
|
assert!(!Blog::find_for_author(&conn, &user[1])
|
2019-03-20 17:56:17 +01:00
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|b| b.id == blog[1].id));
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(())
|
2019-10-07 19:08:20 +02:00
|
|
|
})
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_local() {
|
2021-01-30 15:15:07 +01:00
|
|
|
let conn = &db();
|
2018-11-24 12:44:17 +01:00
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
fill_database(&conn);
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
let blog = Blog::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-11-24 12:44:17 +01:00
|
|
|
NewBlog::new_local(
|
|
|
|
"SomeName".to_owned(),
|
|
|
|
"Some name".to_owned(),
|
|
|
|
"This is some blog".to_owned(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local().unwrap().id,
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
assert_eq!(Blog::find_by_fqn(&conn, "SomeName").unwrap().id, blog.id);
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(())
|
2019-10-07 19:08:20 +02:00
|
|
|
})
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_fqn() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
fill_database(&conn);
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
let blog = Blog::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-11-24 12:44:17 +01:00
|
|
|
NewBlog::new_local(
|
|
|
|
"SomeName".to_owned(),
|
|
|
|
"Some name".to_owned(),
|
|
|
|
"This is some blog".to_owned(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local().unwrap().id,
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
2019-03-06 18:28:10 +01:00
|
|
|
assert_eq!(blog.fqn, "SomeName");
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(())
|
2019-10-07 19:08:20 +02:00
|
|
|
})
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delete() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (_, blogs) = fill_database(&conn);
|
2018-11-24 12:44:17 +01:00
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
blogs[0].delete(&conn).unwrap();
|
|
|
|
assert!(Blog::get(&conn, blogs[0].id).is_err());
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(())
|
2019-10-07 19:08:20 +02:00
|
|
|
})
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delete_via_user() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
2021-01-30 15:15:07 +01:00
|
|
|
let (user, _) = fill_database(&conn);
|
2018-11-24 12:44:17 +01:00
|
|
|
|
2018-12-09 18:44:26 +01:00
|
|
|
let b1 = Blog::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-12-09 18:44:26 +01:00
|
|
|
NewBlog::new_local(
|
|
|
|
"SomeName".to_owned(),
|
|
|
|
"Some name".to_owned(),
|
|
|
|
"This is some blog".to_owned(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local().unwrap().id,
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-12-09 18:44:26 +01:00
|
|
|
let b2 = Blog::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-12-09 18:44:26 +01:00
|
|
|
NewBlog::new_local(
|
|
|
|
"Blog".to_owned(),
|
|
|
|
"Blog".to_owned(),
|
|
|
|
"I've named my blog Blog".to_owned(),
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local().unwrap().id,
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let blog = vec![b1, b2];
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
BlogAuthor::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-11-24 12:44:17 +01:00
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[0].id,
|
|
|
|
author_id: user[0].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
BlogAuthor::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-11-24 12:44:17 +01:00
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[0].id,
|
|
|
|
author_id: user[1].id,
|
|
|
|
is_owner: false,
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
|
|
|
BlogAuthor::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2018-11-24 12:44:17 +01:00
|
|
|
NewBlogAuthor {
|
|
|
|
blog_id: blog[1].id,
|
|
|
|
author_id: user[0].id,
|
|
|
|
is_owner: true,
|
|
|
|
},
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-24 12:44:17 +01:00
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
user[0].delete(&conn).unwrap();
|
|
|
|
assert!(Blog::get(&conn, blog[0].id).is_ok());
|
|
|
|
assert!(Blog::get(&conn, blog[1].id).is_err());
|
|
|
|
user[1].delete(&conn).unwrap();
|
|
|
|
assert!(Blog::get(&conn, blog[0].id).is_err());
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(())
|
2019-10-07 19:08:20 +02:00
|
|
|
})
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn self_federation() {
|
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, mut blogs) = fill_database(&conn);
|
2019-04-17 19:31:47 +02:00
|
|
|
blogs[0].icon_id = Some(
|
|
|
|
Media::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
NewMedia {
|
|
|
|
file_path: "aaa.png".into(),
|
|
|
|
alt_text: String::new(),
|
|
|
|
is_remote: false,
|
|
|
|
remote_url: None,
|
|
|
|
sensitive: false,
|
|
|
|
content_warning: None,
|
|
|
|
owner_id: users[0].id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.id,
|
|
|
|
);
|
|
|
|
blogs[0].banner_id = Some(
|
|
|
|
Media::insert(
|
2021-01-30 15:15:07 +01:00
|
|
|
&conn,
|
2019-04-17 19:31:47 +02:00
|
|
|
NewMedia {
|
|
|
|
file_path: "bbb.png".into(),
|
|
|
|
alt_text: String::new(),
|
|
|
|
is_remote: false,
|
|
|
|
remote_url: None,
|
|
|
|
sensitive: false,
|
|
|
|
content_warning: None,
|
|
|
|
owner_id: users[0].id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.id,
|
|
|
|
);
|
2021-01-30 15:15:07 +01:00
|
|
|
let _: Blog = blogs[0].save_changes(&**conn).unwrap();
|
2019-04-17 19:31:47 +02:00
|
|
|
|
2021-01-30 15:15:07 +01:00
|
|
|
let ap_repr = blogs[0].to_activity(&conn).unwrap();
|
|
|
|
blogs[0].delete(&conn).unwrap();
|
|
|
|
let blog = Blog::from_activity(&conn, ap_repr).unwrap();
|
2019-04-17 19:31:47 +02:00
|
|
|
|
|
|
|
assert_eq!(blog.actor_id, blogs[0].actor_id);
|
|
|
|
assert_eq!(blog.title, blogs[0].title);
|
|
|
|
assert_eq!(blog.summary, blogs[0].summary);
|
|
|
|
assert_eq!(blog.outbox_url, blogs[0].outbox_url);
|
|
|
|
assert_eq!(blog.inbox_url, blogs[0].inbox_url);
|
|
|
|
assert_eq!(blog.instance_id, blogs[0].instance_id);
|
|
|
|
assert_eq!(blog.ap_url, blogs[0].ap_url);
|
|
|
|
assert_eq!(blog.public_key, blogs[0].public_key);
|
|
|
|
assert_eq!(blog.fqn, blogs[0].fqn);
|
|
|
|
assert_eq!(blog.summary_html, blogs[0].summary_html);
|
2021-01-30 15:15:07 +01:00
|
|
|
assert_eq!(blog.icon_url(&conn), blogs[0].icon_url(&conn));
|
|
|
|
assert_eq!(blog.banner_url(&conn), blogs[0].banner_url(&conn));
|
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
|
|
|
}
|
2022-02-22 17:07:57 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_activity() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
let (_users, blogs) = fill_database(&conn);
|
|
|
|
let blog = &blogs[0];
|
|
|
|
let act = blog.to_activity(conn)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"followers": null,
|
|
|
|
"following": null,
|
|
|
|
"icon": {
|
|
|
|
"attributedTo": "",
|
|
|
|
"type": "Image",
|
|
|
|
"url": ""
|
|
|
|
},
|
|
|
|
"id": "https://plu.me/~/BlogName/",
|
|
|
|
"image": {
|
|
|
|
"attributedTo": "",
|
|
|
|
"type": "Image",
|
|
|
|
"url": ""
|
|
|
|
},
|
|
|
|
"inbox": "https://plu.me/~/BlogName/inbox",
|
|
|
|
"liked": null,
|
|
|
|
"name": "Blog name",
|
|
|
|
"outbox": "https://plu.me/~/BlogName/outbox",
|
|
|
|
"preferredUsername": "BlogName",
|
|
|
|
"publicKey": {
|
|
|
|
"id": "https://plu.me/~/BlogName/#main-key",
|
|
|
|
"owner": "https://plu.me/~/BlogName/",
|
|
|
|
"publicKeyPem": blog.public_key
|
|
|
|
},
|
|
|
|
"source": {
|
|
|
|
"content": "This is a small blog",
|
|
|
|
"mediaType": "text/markdown"
|
|
|
|
},
|
|
|
|
"summary": "",
|
|
|
|
"type": "Group"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
2022-02-22 18:09:11 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn outbox_collection() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, Error, _>(|| {
|
|
|
|
let (_users, blogs) = fill_database(conn);
|
|
|
|
let blog = &blogs[0];
|
|
|
|
let act = blog.outbox_collection(conn)?;
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"items": [],
|
|
|
|
"totalItems": 0,
|
|
|
|
"first": "https://plu.me/~/BlogName/outbox?page=1",
|
|
|
|
"last": "https://plu.me/~/BlogName/outbox?page=0",
|
|
|
|
"type": "OrderedCollection"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_json_eq!(to_value(act)?, expected);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|