]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/comment/mod.rs
Merge apub, apub_receive crates (fixes #1621)
[lemmy.git] / crates / apub / src / activities / comment / mod.rs
1 use crate::fetcher::person::get_or_fetch_and_upsert_person;
2 use lemmy_api_common::{blocking, comment::CommentResponse, send_local_notifs};
3 use lemmy_db_queries::Crud;
4 use lemmy_db_schema::{
5   source::{comment::Comment, post::Post},
6   CommentId,
7   LocalUserId,
8 };
9 use lemmy_db_views::comment_view::CommentView;
10 use lemmy_utils::{utils::scrape_text_for_mentions, LemmyError};
11 use lemmy_websocket::{messages::SendComment, LemmyContext};
12 use url::Url;
13
14 pub mod create;
15 pub mod update;
16
17 async fn get_notif_recipients(
18   actor: &Url,
19   comment: &Comment,
20   context: &LemmyContext,
21   request_counter: &mut i32,
22 ) -> Result<Vec<LocalUserId>, LemmyError> {
23   let post_id = comment.post_id;
24   let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
25   let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?;
26
27   // Note:
28   // Although mentions could be gotten from the post tags (they are included there), or the ccs,
29   // Its much easier to scrape them from the comment body, since the API has to do that
30   // anyway.
31   // TODO: for compatibility with other projects, it would be much better to read this from cc or tags
32   let mentions = scrape_text_for_mentions(&comment.content);
33   send_local_notifs(mentions, comment.clone(), actor, post, context.pool(), true).await
34 }
35
36 // TODO: in many call sites we are setting an empty vec for recipient_ids, we should get the actual
37 //       recipient actors from somewhere
38 pub(crate) async fn send_websocket_message<
39   OP: ToString + Send + lemmy_websocket::OperationType + 'static,
40 >(
41   comment_id: CommentId,
42   recipient_ids: Vec<LocalUserId>,
43   op: OP,
44   context: &LemmyContext,
45 ) -> Result<(), LemmyError> {
46   // Refetch the view
47   let comment_view = blocking(context.pool(), move |conn| {
48     CommentView::read(conn, comment_id, None)
49   })
50   .await??;
51
52   let res = CommentResponse {
53     comment_view,
54     recipient_ids,
55     form_id: None,
56   };
57
58   context.chat_server().do_send(SendComment {
59     op,
60     comment: res,
61     websocket_id: None,
62   });
63
64   Ok(())
65 }