3 fetch::fetch_remote_object,
5 person::get_or_fetch_and_upsert_person,
8 inbox::person_inbox::receive_announce,
12 use activitystreams::{
14 collection::{CollectionExt, OrderedCollection},
17 use diesel::result::Error::NotFound;
18 use lemmy_api_structs::blocking;
19 use lemmy_db_queries::{source::community::Community_, ApubObject, Joinable};
20 use lemmy_db_schema::{
21 source::community::{Community, CommunityModerator, CommunityModeratorForm},
24 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
25 use lemmy_utils::{location_info, LemmyError};
26 use lemmy_websocket::LemmyContext;
30 /// Get a community from its apub ID.
32 /// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
33 /// Otherwise it is fetched from the remote instance, stored and returned.
34 pub(crate) async fn get_or_fetch_and_upsert_community(
36 context: &LemmyContext,
37 recursion_counter: &mut i32,
38 ) -> Result<Community, LemmyError> {
39 let apub_id_owned = apub_id.to_owned();
40 let community = blocking(context.pool(), move |conn| {
41 Community::read_from_apub_id(conn, &apub_id_owned.into())
46 Ok(c) if !c.local && should_refetch_actor(c.last_refreshed_at) => {
47 debug!("Fetching and updating from remote community: {}", apub_id);
48 fetch_remote_community(apub_id, context, Some(c), recursion_counter).await
52 debug!("Fetching and creating remote community: {}", apub_id);
53 fetch_remote_community(apub_id, context, None, recursion_counter).await
55 Err(e) => Err(e.into()),
59 /// Request a community by apub ID from a remote instance, including moderators. If `old_community`,
60 /// is set, this is an update for a community which is already known locally. If not, we don't know
61 /// the community yet and also pull the outbox, to get some initial posts.
62 async fn fetch_remote_community(
64 context: &LemmyContext,
65 old_community: Option<Community>,
66 request_counter: &mut i32,
67 ) -> Result<Community, LemmyError> {
68 let group = fetch_remote_object::<GroupExt>(context.client(), apub_id, request_counter).await;
70 if let Some(c) = old_community.to_owned() {
71 if is_deleted(&group) {
72 blocking(context.pool(), move |conn| {
73 Community::update_deleted(conn, c.id, true)
76 } else if group.is_err() {
77 // If fetching failed, return the existing data.
84 Community::from_apub(&group, context, apub_id.to_owned(), request_counter, false).await?;
86 update_community_mods(&group, &community, context, request_counter).await?;
88 // only fetch outbox for new communities, otherwise this can create an infinite loop
89 if old_community.is_none() {
90 let outbox = group.inner.outbox()?.context(location_info!())?;
91 fetch_community_outbox(context, outbox, &community, request_counter).await?
97 async fn update_community_mods(
99 community: &Community,
100 context: &LemmyContext,
101 request_counter: &mut i32,
102 ) -> Result<(), LemmyError> {
103 let new_moderators = fetch_community_mods(context, group, request_counter).await?;
104 let community_id = community.id;
105 let current_moderators = blocking(context.pool(), move |conn| {
106 CommunityModeratorView::for_community(&conn, community_id)
109 // Remove old mods from database which arent in the moderators collection anymore
110 for mod_user in ¤t_moderators {
111 if !new_moderators.contains(&&mod_user.moderator.actor_id.clone().into()) {
112 let community_moderator_form = CommunityModeratorForm {
113 community_id: mod_user.community.id,
114 person_id: mod_user.moderator.id,
116 blocking(context.pool(), move |conn| {
117 CommunityModerator::leave(conn, &community_moderator_form)
123 // Add new mods to database which have been added to moderators collection
124 for mod_uri in new_moderators {
125 let mod_user = get_or_fetch_and_upsert_person(&mod_uri, context, request_counter).await?;
126 let current_mod_uris: Vec<DbUrl> = current_moderators
129 .map(|c| c.moderator.actor_id.clone())
131 if !current_mod_uris.contains(&mod_user.actor_id) {
132 let community_moderator_form = CommunityModeratorForm {
133 community_id: community.id,
134 person_id: mod_user.id,
136 blocking(context.pool(), move |conn| {
137 CommunityModerator::join(conn, &community_moderator_form)
146 async fn fetch_community_outbox(
147 context: &LemmyContext,
149 community: &Community,
150 recursion_counter: &mut i32,
151 ) -> Result<(), LemmyError> {
153 fetch_remote_object::<OrderedCollection>(context.client(), outbox, recursion_counter).await?;
154 let outbox_activities = outbox.items().context(location_info!())?.clone();
155 let mut outbox_activities = outbox_activities.many().context(location_info!())?;
156 if outbox_activities.len() > 20 {
157 outbox_activities = outbox_activities[0..20].to_vec();
160 for activity in outbox_activities {
161 receive_announce(context, activity, community, recursion_counter).await?;
167 pub(crate) async fn fetch_community_mods(
168 context: &LemmyContext,
170 recursion_counter: &mut i32,
171 ) -> Result<Vec<Url>, LemmyError> {
172 if let Some(mods_url) = &group.ext_one.moderators {
174 fetch_remote_object::<OrderedCollection>(context.client(), mods_url, recursion_counter)
178 .map(|i| i.as_many())
180 .context(location_info!())?
182 .filter_map(|i| i.as_xsd_any_uri())
183 .map(|u| u.to_owned())