]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/note.rs
Various pedantic clippy fixes (#2568)
[lemmy.git] / crates / apub / src / protocol / objects / note.rs
1 use crate::{
2   fetcher::post_or_comment::PostOrComment,
3   local_instance,
4   mentions::MentionOrValue,
5   objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
6   protocol::{objects::LanguageTag, Source},
7 };
8 use activitypub_federation::{
9   core::object_id::ObjectId,
10   deser::{
11     helpers::{deserialize_one_or_many, deserialize_skip_error},
12     values::MediaTypeMarkdownOrHtml,
13   },
14 };
15 use activitystreams_kinds::object::NoteType;
16 use chrono::{DateTime, FixedOffset};
17 use lemmy_db_schema::{source::post::Post, traits::Crud};
18 use lemmy_utils::error::LemmyError;
19 use lemmy_websocket::LemmyContext;
20 use serde::{Deserialize, Serialize};
21 use serde_with::skip_serializing_none;
22 use std::ops::Deref;
23 use url::Url;
24
25 #[skip_serializing_none]
26 #[derive(Clone, Debug, Deserialize, Serialize)]
27 #[serde(rename_all = "camelCase")]
28 pub struct Note {
29   pub(crate) r#type: NoteType,
30   pub(crate) id: ObjectId<ApubComment>,
31   pub(crate) attributed_to: ObjectId<ApubPerson>,
32   #[serde(deserialize_with = "deserialize_one_or_many")]
33   pub(crate) to: Vec<Url>,
34   #[serde(deserialize_with = "deserialize_one_or_many", default)]
35   pub(crate) cc: Vec<Url>,
36   pub(crate) content: String,
37   pub(crate) in_reply_to: ObjectId<PostOrComment>,
38
39   pub(crate) media_type: Option<MediaTypeMarkdownOrHtml>,
40   #[serde(deserialize_with = "deserialize_skip_error", default)]
41   pub(crate) source: Option<Source>,
42   pub(crate) published: Option<DateTime<FixedOffset>>,
43   pub(crate) updated: Option<DateTime<FixedOffset>>,
44   #[serde(default)]
45   pub(crate) tag: Vec<MentionOrValue>,
46   // lemmy extension
47   pub(crate) distinguished: Option<bool>,
48   pub(crate) language: Option<LanguageTag>,
49 }
50
51 impl Note {
52   pub(crate) async fn get_parents(
53     &self,
54     context: &LemmyContext,
55     request_counter: &mut i32,
56   ) -> Result<(ApubPost, Option<ApubComment>), LemmyError> {
57     // Fetch parent comment chain in a box, otherwise it can cause a stack overflow.
58     let parent = Box::pin(
59       self
60         .in_reply_to
61         .dereference(context, local_instance(context).await, request_counter)
62         .await?,
63     );
64     match parent.deref() {
65       PostOrComment::Post(p) => {
66         let post = p.deref().clone();
67         Ok((post, None))
68       }
69       PostOrComment::Comment(c) => {
70         let post_id = c.post_id;
71         let post = Post::read(context.pool(), post_id).await?;
72         let comment = c.deref().clone();
73         Ok((post.into(), Some(comment)))
74       }
75     }
76   }
77 }