1 use crate::{activities::receive::get_actor_as_user, objects::FromApub, ActorType, NoteExt};
3 activity::{ActorAndObjectRefExt, Create, Dislike, Like, Remove, Update},
7 use lemmy_db_queries::{source::comment::Comment_, Crud, Likeable};
8 use lemmy_db_schema::source::{
9 comment::{Comment, CommentLike, CommentLikeForm},
12 use lemmy_db_views::comment_view::CommentView;
13 use lemmy_structs::{blocking, comment::CommentResponse, send_local_notifs};
14 use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError};
15 use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};
17 pub(crate) async fn receive_create_comment(
19 context: &LemmyContext,
20 request_counter: &mut i32,
21 ) -> Result<(), LemmyError> {
22 let user = get_actor_as_user(&create, context, request_counter).await?;
23 let note = NoteExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
24 .context(location_info!())?;
26 let comment = Comment::from_apub(¬e, context, user.actor_id()?, request_counter).await?;
28 let post_id = comment.post_id;
29 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
32 // Although mentions could be gotten from the post tags (they are included there), or the ccs,
33 // Its much easier to scrape them from the comment body, since the API has to do that
35 let mentions = scrape_text_for_mentions(&comment.content);
37 send_local_notifs(mentions, comment.clone(), &user, post, context.pool(), true).await?;
40 let comment_view = blocking(context.pool(), move |conn| {
41 CommentView::read(conn, comment.id, None)
45 let res = CommentResponse {
51 context.chat_server().do_send(SendComment {
52 op: UserOperation::CreateComment,
60 pub(crate) async fn receive_update_comment(
62 context: &LemmyContext,
63 request_counter: &mut i32,
64 ) -> Result<(), LemmyError> {
65 let note = NoteExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
66 .context(location_info!())?;
67 let user = get_actor_as_user(&update, context, request_counter).await?;
69 let comment = Comment::from_apub(¬e, context, user.actor_id()?, request_counter).await?;
71 let comment_id = comment.id;
72 let post_id = comment.post_id;
73 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
75 let mentions = scrape_text_for_mentions(&comment.content);
77 send_local_notifs(mentions, comment, &user, post, context.pool(), false).await?;
80 let comment_view = blocking(context.pool(), move |conn| {
81 CommentView::read(conn, comment_id, None)
85 let res = CommentResponse {
91 context.chat_server().do_send(SendComment {
92 op: UserOperation::EditComment,
100 pub(crate) async fn receive_like_comment(
103 context: &LemmyContext,
104 request_counter: &mut i32,
105 ) -> Result<(), LemmyError> {
106 let user = get_actor_as_user(&like, context, request_counter).await?;
108 let comment_id = comment.id;
109 let like_form = CommentLikeForm {
111 post_id: comment.post_id,
115 let user_id = user.id;
116 blocking(context.pool(), move |conn| {
117 CommentLike::remove(conn, user_id, comment_id)?;
118 CommentLike::like(conn, &like_form)
123 let comment_view = blocking(context.pool(), move |conn| {
124 CommentView::read(conn, comment_id, None)
128 // TODO get those recipient actor ids from somewhere
129 let recipient_ids = vec![];
130 let res = CommentResponse {
136 context.chat_server().do_send(SendComment {
137 op: UserOperation::CreateCommentLike,
145 pub(crate) async fn receive_dislike_comment(
148 context: &LemmyContext,
149 request_counter: &mut i32,
150 ) -> Result<(), LemmyError> {
151 let user = get_actor_as_user(&dislike, context, request_counter).await?;
153 let comment_id = comment.id;
154 let like_form = CommentLikeForm {
156 post_id: comment.post_id,
160 let user_id = user.id;
161 blocking(context.pool(), move |conn| {
162 CommentLike::remove(conn, user_id, comment_id)?;
163 CommentLike::like(conn, &like_form)
168 let comment_view = blocking(context.pool(), move |conn| {
169 CommentView::read(conn, comment_id, None)
173 // TODO get those recipient actor ids from somewhere
174 let recipient_ids = vec![];
175 let res = CommentResponse {
181 context.chat_server().do_send(SendComment {
182 op: UserOperation::CreateCommentLike,
190 pub(crate) async fn receive_delete_comment(
191 context: &LemmyContext,
193 ) -> Result<(), LemmyError> {
194 let deleted_comment = blocking(context.pool(), move |conn| {
195 Comment::update_deleted(conn, comment.id, true)
200 let comment_id = deleted_comment.id;
201 let comment_view = blocking(context.pool(), move |conn| {
202 CommentView::read(conn, comment_id, None)
206 // TODO get those recipient actor ids from somewhere
207 let recipient_ids = vec![];
208 let res = CommentResponse {
213 context.chat_server().do_send(SendComment {
214 op: UserOperation::EditComment,
222 pub(crate) async fn receive_remove_comment(
223 context: &LemmyContext,
226 ) -> Result<(), LemmyError> {
227 let removed_comment = blocking(context.pool(), move |conn| {
228 Comment::update_removed(conn, comment.id, true)
233 let comment_id = removed_comment.id;
234 let comment_view = blocking(context.pool(), move |conn| {
235 CommentView::read(conn, comment_id, None)
239 // TODO get those recipient actor ids from somewhere
240 let recipient_ids = vec![];
241 let res = CommentResponse {
246 context.chat_server().do_send(SendComment {
247 op: UserOperation::EditComment,