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,
16 use lemmy_structs::{blocking, comment::CommentResponse, send_local_notifs};
17 use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError};
18 use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};
20 pub(crate) async fn receive_create_comment(
22 context: &LemmyContext,
23 request_counter: &mut i32,
24 ) -> Result<(), LemmyError> {
25 let user = get_actor_as_user(&create, context, request_counter).await?;
26 let note = NoteExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
27 .context(location_info!())?;
29 let comment = Comment::from_apub(¬e, context, user.actor_id()?, request_counter).await?;
31 let post_id = comment.post_id;
32 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
35 // Although mentions could be gotten from the post tags (they are included there), or the ccs,
36 // Its much easier to scrape them from the comment body, since the API has to do that
38 let mentions = scrape_text_for_mentions(&comment.content);
40 send_local_notifs(mentions, comment.clone(), &user, post, context.pool(), true).await?;
43 let comment_view = blocking(context.pool(), move |conn| {
44 CommentView::read(conn, comment.id, None)
48 let res = CommentResponse {
54 context.chat_server().do_send(SendComment {
55 op: UserOperation::CreateComment,
63 pub(crate) async fn receive_update_comment(
65 context: &LemmyContext,
66 request_counter: &mut i32,
67 ) -> Result<(), LemmyError> {
68 let note = NoteExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
69 .context(location_info!())?;
70 let user = get_actor_as_user(&update, context, request_counter).await?;
72 let comment = Comment::from_apub(¬e, context, user.actor_id()?, request_counter).await?;
74 let comment_id = comment.id;
75 let post_id = comment.post_id;
76 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
78 let mentions = scrape_text_for_mentions(&comment.content);
80 send_local_notifs(mentions, comment, &user, post, context.pool(), false).await?;
83 let comment_view = blocking(context.pool(), move |conn| {
84 CommentView::read(conn, comment_id, None)
88 let res = CommentResponse {
94 context.chat_server().do_send(SendComment {
95 op: UserOperation::EditComment,
103 pub(crate) async fn receive_like_comment(
106 context: &LemmyContext,
107 request_counter: &mut i32,
108 ) -> Result<(), LemmyError> {
109 let user = get_actor_as_user(&like, context, request_counter).await?;
111 let comment_id = comment.id;
112 let like_form = CommentLikeForm {
114 post_id: comment.post_id,
118 let user_id = user.id;
119 blocking(context.pool(), move |conn| {
120 CommentLike::remove(conn, user_id, comment_id)?;
121 CommentLike::like(conn, &like_form)
126 let comment_view = blocking(context.pool(), move |conn| {
127 CommentView::read(conn, comment_id, None)
131 // TODO get those recipient actor ids from somewhere
132 let recipient_ids = vec![];
133 let res = CommentResponse {
139 context.chat_server().do_send(SendComment {
140 op: UserOperation::CreateCommentLike,
148 pub(crate) async fn receive_dislike_comment(
151 context: &LemmyContext,
152 request_counter: &mut i32,
153 ) -> Result<(), LemmyError> {
154 let user = get_actor_as_user(&dislike, context, request_counter).await?;
156 let comment_id = comment.id;
157 let like_form = CommentLikeForm {
159 post_id: comment.post_id,
163 let user_id = user.id;
164 blocking(context.pool(), move |conn| {
165 CommentLike::remove(conn, user_id, comment_id)?;
166 CommentLike::like(conn, &like_form)
171 let comment_view = blocking(context.pool(), move |conn| {
172 CommentView::read(conn, comment_id, None)
176 // TODO get those recipient actor ids from somewhere
177 let recipient_ids = vec![];
178 let res = CommentResponse {
184 context.chat_server().do_send(SendComment {
185 op: UserOperation::CreateCommentLike,
193 pub(crate) async fn receive_delete_comment(
194 context: &LemmyContext,
196 ) -> Result<(), LemmyError> {
197 let deleted_comment = blocking(context.pool(), move |conn| {
198 Comment::update_deleted(conn, comment.id, true)
203 let comment_id = deleted_comment.id;
204 let comment_view = blocking(context.pool(), move |conn| {
205 CommentView::read(conn, comment_id, None)
209 // TODO get those recipient actor ids from somewhere
210 let recipient_ids = vec![];
211 let res = CommentResponse {
216 context.chat_server().do_send(SendComment {
217 op: UserOperation::EditComment,
225 pub(crate) async fn receive_remove_comment(
226 context: &LemmyContext,
229 ) -> Result<(), LemmyError> {
230 let removed_comment = blocking(context.pool(), move |conn| {
231 Comment::update_removed(conn, comment.id, true)
236 let comment_id = removed_comment.id;
237 let comment_view = blocking(context.pool(), move |conn| {
238 CommentView::read(conn, comment_id, None)
242 // TODO get those recipient actor ids from somewhere
243 let recipient_ids = vec![];
244 let res = CommentResponse {
249 context.chat_server().do_send(SendComment {
250 op: UserOperation::EditComment,