Skip to content

Commit

Permalink
Serve activities in community outbox (ref #1216)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Jan 26, 2021
1 parent a628f19 commit 34dda95
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions crates/apub/src/http/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use crate::{
ActorType,
};
use activitystreams::{
base::{AnyBase, BaseExt, ExtendsExt},
base::BaseExt,
collection::{CollectionExt, OrderedCollection, UnorderedCollection},
};
use actix_web::{body::Body, web, HttpResponse};
use lemmy_db_queries::source::{community::Community_, post::Post_};
use lemmy_db_schema::source::{community::Community, post::Post};
use lemmy_db_queries::source::{activity::Activity_, community::Community_};
use lemmy_db_schema::source::{activity::Activity, community::Community};
use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
Expand Down Expand Up @@ -76,21 +76,17 @@ pub async fn get_apub_community_outbox(
})
.await??;

let community_id = community.id;
let posts = blocking(context.pool(), move |conn| {
Post::list_for_community(conn, community_id)
let community_actor_id = community.actor_id.to_owned();
let activities = blocking(context.pool(), move |conn| {
Activity::read_community_outbox(conn, &community_actor_id)
})
.await??;

let mut pages: Vec<AnyBase> = vec![];
for p in posts {
pages.push(p.to_apub(context.pool()).await?.into_any_base()?);
}

let len = pages.len();
let activities: Vec<String> = activities.iter().map(|a| a.to_string()).collect();
let len = activities.len();
let mut collection = OrderedCollection::new();
collection
.set_many_items(pages)
.set_many_items(activities)
.set_many_contexts(lemmy_context()?)
.set_id(community.get_outbox_url()?)
.set_total_items(len as u64);
Expand Down
1 change: 1 addition & 0 deletions crates/db_queries/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ url = { version = "2.2.0", features = ["serde"] }
lazy_static = "1.4.0"
regex = "1.4.2"
bcrypt = "0.9.0"
diesel_json = "0.1.1"
34 changes: 32 additions & 2 deletions crates/db_queries/src/source/activity.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::Crud;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::source::activity::*;
use lemmy_db_schema::{schema::activity, source::activity::*, Url};
use log::debug;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
fmt::Debug,
io::{Error as IoError, ErrorKind},
Expand Down Expand Up @@ -47,7 +48,20 @@ pub trait Activity_ {
) -> Result<Activity, IoError>
where
T: Serialize + Debug;

fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Activity, Error>;

fn read_community_outbox(
conn: &PgConnection,
community_actor_id: &Url,
) -> Result<Vec<Value>, Error>;
}

#[derive(QueryableByName, Deserialize)]
#[table_name = "activity"]
struct JsonResult {
#[column_name = "data"]
data: Value,
}

impl Activity_ for Activity {
Expand Down Expand Up @@ -83,6 +97,22 @@ impl Activity_ for Activity {
use lemmy_db_schema::schema::activity::dsl::*;
activity.filter(ap_id.eq(object_id)).first::<Self>(conn)
}

fn read_community_outbox(
conn: &PgConnection,
community_actor_id: &Url,
) -> Result<Vec<Value>, Error> {
let res: Vec<JsonResult> = sql_query(format!(
r#"SELECT activity.data FROM activity
WHERE activity.data ->> 'type' = 'Create'
AND activity.data -> 'object' ->> 'type' = 'Page'
AND activity.data -> 'object' ->> 'to' = '{}'
LIMIT 20;"#,
community_actor_id
))
.get_results(conn)?;
Ok(res.iter().map(|i| i.data.to_owned()).collect())
}
}

#[cfg(test)]
Expand Down
14 changes: 12 additions & 2 deletions crates/db_schema/src/source/activity.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
use crate::schema::activity;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::Debug;
use std::{collections::HashMap, fmt::Debug};

#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct ActivityData {
#[serde(rename = "type")]
pub type_: String,
// this holds all the fields which are not explicitly listed above, so they can be deserialized
#[serde(flatten)]
extra: HashMap<String, Value>,
}

#[derive(Queryable, Identifiable, Debug)]
#[table_name = "activity"]
pub struct Activity {
pub id: i32,
Expand Down

0 comments on commit 34dda95

Please sign in to comment.