2 check_is_apub_id_valid,
4 objects::get_summary_from_string_or_source,
5 protocol::{ImageObject, Source},
7 use activitystreams::{actor::Endpoints, object::kind::ImageType, unparsed::Unparsed};
8 use chrono::{DateTime, FixedOffset, NaiveDateTime};
9 use lemmy_api_common::blocking;
11 signatures::PublicKey,
12 traits::{ActorType, ApubObject},
13 values::MediaTypeMarkdown,
14 verify::verify_domains_match,
16 use lemmy_db_schema::{
18 source::person::{Person as DbPerson, PersonForm},
21 utils::{check_slurs, check_slurs_opt, convert_datetime, markdown_to_html},
24 use lemmy_websocket::LemmyContext;
25 use serde::{Deserialize, Serialize};
26 use serde_with::skip_serializing_none;
30 #[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq)]
36 #[skip_serializing_none]
37 #[derive(Clone, Debug, Deserialize, Serialize)]
38 #[serde(rename_all = "camelCase")]
40 #[serde(rename = "type")]
43 /// username, set at account creation and can never be changed
44 preferred_username: String,
45 /// displayname (can be changed at any time)
47 summary: Option<String>,
48 source: Option<Source>,
50 icon: Option<ImageObject>,
52 image: Option<ImageObject>,
53 matrix_user_id: Option<String>,
55 /// mandatory field in activitypub, currently empty in lemmy
57 endpoints: Endpoints<Url>,
58 public_key: PublicKey,
59 published: Option<DateTime<FixedOffset>>,
60 updated: Option<DateTime<FixedOffset>>,
65 // TODO: can generate this with a derive macro
67 pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> {
68 verify_domains_match(&self.id, expected_domain)?;
73 #[derive(Clone, Debug, PartialEq)]
74 pub struct ApubPerson(DbPerson);
76 impl Deref for ApubPerson {
77 type Target = DbPerson;
78 fn deref(&self) -> &Self::Target {
83 impl From<DbPerson> for ApubPerson {
84 fn from(p: DbPerson) -> Self {
89 #[async_trait::async_trait(?Send)]
90 impl ApubObject for ApubPerson {
91 type DataType = LemmyContext;
92 type ApubType = Person;
93 type TombstoneType = ();
95 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
96 Some(self.last_refreshed_at)
99 async fn read_from_apub_id(
101 context: &LemmyContext,
102 ) -> Result<Option<Self>, LemmyError> {
104 blocking(context.pool(), move |conn| {
105 DbPerson::read_from_apub_id(conn, object_id)
112 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
113 blocking(context.pool(), move |conn| {
114 DbPerson::update_deleted(conn, self.id, true)
120 async fn to_apub(&self, _pool: &LemmyContext) -> Result<Person, LemmyError> {
121 let kind = if self.bot_account {
126 let source = self.bio.clone().map(|bio| Source {
128 media_type: MediaTypeMarkdown::Markdown,
130 let icon = self.avatar.clone().map(|url| ImageObject {
131 kind: ImageType::Image,
134 let image = self.banner.clone().map(|url| ImageObject {
135 kind: ImageType::Image,
139 let person = Person {
141 id: self.actor_id.to_owned().into_inner(),
142 preferred_username: self.name.clone(),
143 name: self.display_name.clone(),
144 summary: self.bio.as_ref().map(|b| markdown_to_html(b)),
148 matrix_user_id: self.matrix_user_id.clone(),
149 published: Some(convert_datetime(self.published)),
150 outbox: generate_outbox_url(&self.actor_id)?.into(),
151 endpoints: Endpoints {
152 shared_inbox: self.shared_inbox_url.clone().map(|s| s.into()),
155 public_key: self.get_public_key()?,
156 updated: self.updated.map(convert_datetime),
157 unparsed: Default::default(),
158 inbox: self.inbox_url.clone().into(),
163 fn to_tombstone(&self) -> Result<(), LemmyError> {
169 context: &LemmyContext,
170 expected_domain: &Url,
171 _request_counter: &mut i32,
172 ) -> Result<ApubPerson, LemmyError> {
173 let actor_id = Some(person.id(expected_domain)?.clone().into());
174 let name = person.preferred_username.clone();
175 let display_name: Option<String> = person.name.clone();
176 let bio = get_summary_from_string_or_source(&person.summary, &person.source);
177 let shared_inbox = person.endpoints.shared_inbox.clone().map(|s| s.into());
178 let bot_account = match person.kind {
179 UserTypes::Person => false,
180 UserTypes::Service => true,
183 let slur_regex = &context.settings().slur_regex();
184 check_slurs(&name, slur_regex)?;
185 check_slurs_opt(&display_name, slur_regex)?;
186 check_slurs_opt(&bio, slur_regex)?;
188 check_is_apub_id_valid(&person.id, false, &context.settings())?;
190 let person_form = PersonForm {
192 display_name: Some(display_name),
195 avatar: Some(person.icon.clone().map(|i| i.url.into())),
196 banner: Some(person.image.clone().map(|i| i.url.into())),
197 published: person.published.map(|u| u.clone().naive_local()),
198 updated: person.updated.map(|u| u.clone().naive_local()),
203 bot_account: Some(bot_account),
205 public_key: Some(Some(person.public_key.public_key_pem.clone())),
206 last_refreshed_at: Some(naive_now()),
207 inbox_url: Some(person.inbox.to_owned().into()),
208 shared_inbox_url: Some(shared_inbox),
209 matrix_user_id: Some(person.matrix_user_id.clone()),
211 let person = blocking(context.pool(), move |conn| {
212 DbPerson::upsert(conn, &person_form)
219 impl ActorType for ApubPerson {
220 fn is_local(&self) -> bool {
223 fn actor_id(&self) -> Url {
224 self.actor_id.to_owned().into_inner()
226 fn name(&self) -> String {
230 fn public_key(&self) -> Option<String> {
231 self.public_key.to_owned()
234 fn private_key(&self) -> Option<String> {
235 self.private_key.to_owned()
238 fn inbox_url(&self) -> Url {
239 self.inbox_url.clone().into()
242 fn shared_inbox_url(&self) -> Option<Url> {
243 self.shared_inbox_url.clone().map(|s| s.into_inner())
250 use crate::objects::tests::{file_to_json_object, init_context};
251 use assert_json_diff::assert_json_include;
252 use lemmy_db_schema::traits::Crud;
253 use serial_test::serial;
257 async fn test_parse_lemmy_person() {
258 let context = init_context();
259 let json = file_to_json_object("assets/lemmy-person.json");
260 let url = Url::parse("https://enterprise.lemmy.ml/u/picard").unwrap();
261 let mut request_counter = 0;
262 let person = ApubPerson::from_apub(&json, &context, &url, &mut request_counter)
266 assert_eq!(person.actor_id.clone().into_inner(), url);
267 assert_eq!(person.display_name, Some("Jean-Luc Picard".to_string()));
268 assert!(person.public_key.is_some());
269 assert!(!person.local);
270 assert_eq!(person.bio.as_ref().unwrap().len(), 39);
271 assert_eq!(request_counter, 0);
273 let to_apub = person.to_apub(&context).await.unwrap();
274 assert_json_include!(actual: json, expected: to_apub);
276 DbPerson::delete(&*context.pool().get().unwrap(), person.id).unwrap();
281 async fn test_parse_pleroma_person() {
282 let context = init_context();
283 let json = file_to_json_object("assets/pleroma-person.json");
284 let url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
285 let mut request_counter = 0;
286 let person = ApubPerson::from_apub(&json, &context, &url, &mut request_counter)
290 assert_eq!(person.actor_id.clone().into_inner(), url);
291 assert_eq!(person.name, "lanodan");
292 assert!(person.public_key.is_some());
293 assert!(!person.local);
294 assert_eq!(request_counter, 0);
295 assert_eq!(person.bio.as_ref().unwrap().len(), 873);
297 DbPerson::delete(&*context.pool().get().unwrap(), person.id).unwrap();