1 use crate::{activities::receive::get_actor_as_user, objects::FromApub, ActorType, NoteExt};
3 activity::{ActorAndObjectRefExt, Create, Dislike, Like, Remove, Update},
9 comment::{Comment, CommentLike, CommentLikeForm},
12 views::comment_view::CommentView,
15 use lemmy_structs::{blocking, comment::CommentResponse, send_local_notifs};
16 use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError};
17 use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};
19 pub(crate) async fn receive_create_comment(
21 context: &LemmyContext,
22 request_counter: &mut i32,
23 ) -> Result<(), LemmyError> {
24 let user = get_actor_as_user(&create, context, request_counter).await?;
25 let note = NoteExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
26 .context(location_info!())?;
28 let comment = Comment::from_apub(¬e, context, user.actor_id()?, request_counter).await?;
30 let post_id = comment.post_id;
31 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
34 // Although mentions could be gotten from the post tags (they are included there), or the ccs,
35 // Its much easier to scrape them from the comment body, since the API has to do that
37 let mentions = scrape_text_for_mentions(&comment.content);
39 send_local_notifs(mentions, comment.clone(), &user, post, context.pool(), true).await?;
42 let comment_view = blocking(context.pool(), move |conn| {
43 CommentView::read(conn, comment.id, None)
47 let res = CommentResponse {
53 context.chat_server().do_send(SendComment {
54 op: UserOperation::CreateComment,
62 pub(crate) async fn receive_update_comment(
64 context: &LemmyContext,
65 request_counter: &mut i32,
66 ) -> Result<(), LemmyError> {
67 let note = NoteExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
68 .context(location_info!())?;
69 let user = get_actor_as_user(&update, context, request_counter).await?;
71 let comment = Comment::from_apub(¬e, context, user.actor_id()?, request_counter).await?;
73 let comment_id = comment.id;
74 let post_id = comment.post_id;
75 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
77 let mentions = scrape_text_for_mentions(&comment.content);
79 send_local_notifs(mentions, comment, &user, post, context.pool(), false).await?;
82 let comment_view = blocking(context.pool(), move |conn| {
83 CommentView::read(conn, comment_id, None)
87 let res = CommentResponse {
93 context.chat_server().do_send(SendComment {
94 op: UserOperation::EditComment,
102 pub(crate) async fn receive_like_comment(
105 context: &LemmyContext,
106 request_counter: &mut i32,
107 ) -> Result<(), LemmyError> {
108 let user = get_actor_as_user(&like, context, request_counter).await?;
110 let comment_id = comment.id;
111 let like_form = CommentLikeForm {
113 post_id: comment.post_id,
117 let user_id = user.id;
118 blocking(context.pool(), move |conn| {
119 CommentLike::remove(conn, user_id, comment_id)?;
120 CommentLike::like(conn, &like_form)
125 let comment_view = blocking(context.pool(), move |conn| {
126 CommentView::read(conn, comment_id, None)
130 // TODO get those recipient actor ids from somewhere
131 let recipient_ids = vec![];
132 let res = CommentResponse {
138 context.chat_server().do_send(SendComment {
139 op: UserOperation::CreateCommentLike,
147 pub(crate) async fn receive_dislike_comment(
150 context: &LemmyContext,
151 request_counter: &mut i32,
152 ) -> Result<(), LemmyError> {
153 let user = get_actor_as_user(&dislike, context, request_counter).await?;
155 let comment_id = comment.id;
156 let like_form = CommentLikeForm {
158 post_id: comment.post_id,
162 let user_id = user.id;
163 blocking(context.pool(), move |conn| {
164 CommentLike::remove(conn, user_id, comment_id)?;
165 CommentLike::like(conn, &like_form)
170 let comment_view = blocking(context.pool(), move |conn| {
171 CommentView::read(conn, comment_id, None)
175 // TODO get those recipient actor ids from somewhere
176 let recipient_ids = vec![];
177 let res = CommentResponse {
183 context.chat_server().do_send(SendComment {
184 op: UserOperation::CreateCommentLike,
192 pub(crate) async fn receive_delete_comment(
193 context: &LemmyContext,
195 ) -> Result<(), LemmyError> {
196 let deleted_comment = blocking(context.pool(), move |conn| {
197 Comment::update_deleted(conn, comment.id, true)
202 let comment_id = deleted_comment.id;
203 let comment_view = blocking(context.pool(), move |conn| {
204 CommentView::read(conn, comment_id, None)
208 // TODO get those recipient actor ids from somewhere
209 let recipient_ids = vec![];
210 let res = CommentResponse {
215 context.chat_server().do_send(SendComment {
216 op: UserOperation::EditComment,
224 pub(crate) async fn receive_remove_comment(
225 context: &LemmyContext,
228 ) -> Result<(), LemmyError> {
229 let removed_comment = blocking(context.pool(), move |conn| {
230 Comment::update_removed(conn, comment.id, true)
235 let comment_id = removed_comment.id;
236 let comment_view = blocking(context.pool(), move |conn| {
237 CommentView::read(conn, comment_id, None)
241 // TODO get those recipient actor ids from somewhere
242 let recipient_ids = vec![];
243 let res = CommentResponse {
248 context.chat_server().do_send(SendComment {
249 op: UserOperation::EditComment,