2 activities::community::announce::AnnounceActivity,
4 fetch::fetch_remote_object,
6 person::get_or_fetch_and_upsert_person,
9 objects::{community::Group, FromApub},
11 use activitystreams::collection::{CollectionExt, OrderedCollection};
13 use diesel::result::Error::NotFound;
14 use lemmy_api_common::blocking;
15 use lemmy_apub_lib::ActivityHandler;
16 use lemmy_db_queries::{source::community::Community_, ApubObject, Joinable};
17 use lemmy_db_schema::source::community::{Community, CommunityModerator, CommunityModeratorForm};
18 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
19 use lemmy_utils::{location_info, LemmyError};
20 use lemmy_websocket::LemmyContext;
24 /// Get a community from its apub ID.
26 /// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
27 /// Otherwise it is fetched from the remote instance, stored and returned.
28 pub(crate) async fn get_or_fetch_and_upsert_community(
30 context: &LemmyContext,
31 recursion_counter: &mut i32,
32 ) -> Result<Community, LemmyError> {
33 let apub_id_owned = apub_id.to_owned();
34 let community = blocking(context.pool(), move |conn| {
35 Community::read_from_apub_id(conn, &apub_id_owned.into())
40 Ok(c) if !c.local && should_refetch_actor(c.last_refreshed_at) => {
41 debug!("Fetching and updating from remote community: {}", apub_id);
42 fetch_remote_community(apub_id, context, Some(c), recursion_counter).await
46 debug!("Fetching and creating remote community: {}", apub_id);
47 fetch_remote_community(apub_id, context, None, recursion_counter).await
49 Err(e) => Err(e.into()),
53 /// Request a community by apub ID from a remote instance, including moderators. If `old_community`,
54 /// is set, this is an update for a community which is already known locally. If not, we don't know
55 /// the community yet and also pull the outbox, to get some initial posts.
56 async fn fetch_remote_community(
58 context: &LemmyContext,
59 old_community: Option<Community>,
60 request_counter: &mut i32,
61 ) -> Result<Community, LemmyError> {
62 let group = fetch_remote_object::<Group>(context.client(), apub_id, request_counter).await;
64 if let Some(c) = old_community.to_owned() {
65 if is_deleted(&group) {
66 blocking(context.pool(), move |conn| {
67 Community::update_deleted(conn, c.id, true)
70 } else if group.is_err() {
71 // If fetching failed, return the existing data.
77 let community = Community::from_apub(&group, context, apub_id, request_counter).await?;
79 update_community_mods(&group, &community, context, request_counter).await?;
81 // only fetch outbox for new communities, otherwise this can create an infinite loop
82 if old_community.is_none() {
83 fetch_community_outbox(context, &group.outbox, request_counter).await?
89 async fn update_community_mods(
91 community: &Community,
92 context: &LemmyContext,
93 request_counter: &mut i32,
94 ) -> Result<(), LemmyError> {
95 let new_moderators = fetch_community_mods(context, group, request_counter).await?;
96 let community_id = community.id;
97 let current_moderators = blocking(context.pool(), move |conn| {
98 CommunityModeratorView::for_community(conn, community_id)
101 // Remove old mods from database which arent in the moderators collection anymore
102 for mod_user in ¤t_moderators {
103 if !new_moderators.contains(&mod_user.moderator.actor_id.clone().into()) {
104 let community_moderator_form = CommunityModeratorForm {
105 community_id: mod_user.community.id,
106 person_id: mod_user.moderator.id,
108 blocking(context.pool(), move |conn| {
109 CommunityModerator::leave(conn, &community_moderator_form)
115 // Add new mods to database which have been added to moderators collection
116 for mod_uri in new_moderators {
117 let mod_user = get_or_fetch_and_upsert_person(&mod_uri, context, request_counter).await?;
119 if !current_moderators
122 .map(|c| c.moderator.actor_id.clone())
123 .any(|x| x == mod_user.actor_id)
125 let community_moderator_form = CommunityModeratorForm {
126 community_id: community.id,
127 person_id: mod_user.id,
129 blocking(context.pool(), move |conn| {
130 CommunityModerator::join(conn, &community_moderator_form)
139 async fn fetch_community_outbox(
140 context: &LemmyContext,
142 recursion_counter: &mut i32,
143 ) -> Result<(), LemmyError> {
145 fetch_remote_object::<OrderedCollection>(context.client(), outbox, recursion_counter).await?;
146 let outbox_activities = outbox.items().context(location_info!())?.clone();
147 let mut outbox_activities = outbox_activities.many().context(location_info!())?;
148 if outbox_activities.len() > 20 {
149 outbox_activities = outbox_activities[0..20].to_vec();
152 for announce in outbox_activities {
153 // TODO: instead of converting like this, we should create a struct CommunityOutbox with
154 // AnnounceActivity as inner type, but that gives me stackoverflow
155 let ser = serde_json::to_string(&announce)?;
156 let announce: AnnounceActivity = serde_json::from_str(&ser)?;
157 announce.receive(context, recursion_counter).await?;
163 pub(crate) async fn fetch_community_mods(
164 context: &LemmyContext,
166 recursion_counter: &mut i32,
167 ) -> Result<Vec<Url>, LemmyError> {
168 if let Some(mods_url) = &group.moderators {
170 fetch_remote_object::<OrderedCollection>(context.client(), mods_url, recursion_counter)
174 .map(|i| i.as_many())
176 .context(location_info!())?
178 .filter_map(|i| i.as_xsd_any_uri())
179 .map(|u| u.to_owned())