2 extensions::{context::lemmy_context, group_extensions::GroupExtension},
3 fetcher::get_or_fetch_and_upsert_user,
8 get_source_markdown_value,
9 set_content_and_source,
17 use activitystreams::{
18 actor::{kind::GroupType, ApActor, Endpoints, Group},
20 object::{ApObject, Image, Tombstone},
23 use activitystreams_ext::Ext2;
26 community::{Community, CommunityForm},
28 views::community_moderator_view::CommunityModeratorView,
31 use lemmy_structs::blocking;
34 utils::{check_slurs, check_slurs_opt, convert_datetime},
37 use lemmy_websocket::LemmyContext;
40 #[async_trait::async_trait(?Send)]
41 impl ToApub for Community {
42 type ApubType = GroupExt;
44 async fn to_apub(&self, pool: &DbPool) -> Result<GroupExt, LemmyError> {
45 // The attributed to, is an ordered vector with the creator actor_ids first,
46 // then the rest of the moderators
47 // TODO Technically the instance admins can mod the community, but lets
48 // ignore that for now
50 let moderators = blocking(pool, move |conn| {
51 CommunityModeratorView::for_community(&conn, id)
54 let moderators: Vec<String> = moderators
56 .map(|m| m.moderator.actor_id)
59 let mut group = ApObject::new(Group::new());
61 .set_many_contexts(lemmy_context()?)
62 .set_id(Url::parse(&self.actor_id)?)
63 .set_name(self.title.to_owned())
64 .set_published(convert_datetime(self.published))
65 .set_many_attributed_tos(moderators);
67 if let Some(u) = self.updated.to_owned() {
68 group.set_updated(convert_datetime(u));
70 if let Some(d) = self.description.to_owned() {
71 set_content_and_source(&mut group, &d)?;
74 if let Some(icon_url) = &self.icon {
75 let mut image = Image::new();
76 image.set_url(Url::parse(icon_url)?);
77 group.set_icon(image.into_any_base()?);
80 if let Some(banner_url) = &self.banner {
81 let mut image = Image::new();
82 image.set_url(Url::parse(banner_url)?);
83 group.set_image(image.into_any_base()?);
86 let mut ap_actor = ApActor::new(self.get_inbox_url()?, group);
88 .set_preferred_username(self.name.to_owned())
89 .set_outbox(self.get_outbox_url()?)
90 .set_followers(self.get_followers_url()?)
91 .set_endpoints(Endpoints {
92 shared_inbox: Some(self.get_shared_inbox_url()?),
97 let category_id = self.category_id;
98 let group_extension = blocking(pool, move |conn| {
99 GroupExtension::new(conn, category_id, nsfw)
106 self.get_public_key_ext()?,
110 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
111 create_tombstone(self.deleted, &self.actor_id, self.updated, GroupType::Group)
115 #[async_trait::async_trait(?Send)]
116 impl FromApub for Community {
117 type ApubType = GroupExt;
119 /// Converts a `Group` to `Community`.
122 context: &LemmyContext,
123 expected_domain: Url,
124 request_counter: &mut i32,
125 ) -> Result<Community, LemmyError> {
126 get_object_from_apub(group, context, expected_domain, request_counter).await
130 #[async_trait::async_trait(?Send)]
131 impl FromApubToForm<GroupExt> for CommunityForm {
134 context: &LemmyContext,
135 expected_domain: Url,
136 request_counter: &mut i32,
137 ) -> Result<Self, LemmyError> {
138 let creator_and_moderator_uris = group.inner.attributed_to().context(location_info!())?;
139 let creator_uri = creator_and_moderator_uris
141 .context(location_info!())?
144 .context(location_info!())?
146 .context(location_info!())?;
148 let creator = get_or_fetch_and_upsert_user(creator_uri, context, request_counter).await?;
151 .preferred_username()
152 .context(location_info!())?
157 .context(location_info!())?
159 .context(location_info!())?
161 .context(location_info!())?
164 let description = get_source_markdown_value(group)?;
167 check_slurs(&title)?;
168 check_slurs_opt(&description)?;
170 let icon = match group.icon() {
171 Some(any_image) => Some(
172 Image::from_any_base(any_image.as_one().context(location_info!())?.clone())
173 .context(location_info!())?
174 .context(location_info!())?
176 .context(location_info!())?
177 .as_single_xsd_any_uri()
178 .map(|u| u.to_string()),
183 let banner = match group.image() {
184 Some(any_image) => Some(
185 Image::from_any_base(any_image.as_one().context(location_info!())?.clone())
186 .context(location_info!())?
187 .context(location_info!())?
189 .context(location_info!())?
190 .as_single_xsd_any_uri()
191 .map(|u| u.to_string()),
200 category_id: group.ext_one.category.identifier.parse::<i32>()?,
201 creator_id: creator.id,
203 published: group.inner.published().map(|u| u.to_owned().naive_local()),
204 updated: group.inner.updated().map(|u| u.to_owned().naive_local()),
206 nsfw: group.ext_one.sensitive,
207 actor_id: Some(check_object_domain(group, expected_domain)?),
210 public_key: Some(group.ext_two.to_owned().public_key.public_key_pem),
211 last_refreshed_at: Some(naive_now()),