1 use crate::{activities::receive::get_actor_as_person, objects::FromApub, ActorType, NoteExt};
3 activity::{ActorAndObjectRefExt, Create, Dislike, Like, Update},
7 use lemmy_api_common::{blocking, comment::CommentResponse, send_local_notifs};
8 use lemmy_db_queries::{source::comment::Comment_, Crud, Likeable};
9 use lemmy_db_schema::source::{
10 comment::{Comment, CommentLike, CommentLikeForm},
13 use lemmy_db_views::comment_view::CommentView;
14 use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError};
15 use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation, UserOperationCrud};
17 pub(crate) async fn receive_create_comment(
19 context: &LemmyContext,
20 request_counter: &mut i32,
21 ) -> Result<(), LemmyError> {
22 let person = get_actor_as_person(&create, context, request_counter).await?;
23 let note = NoteExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
24 .context(location_info!())?;
27 Comment::from_apub(¬e, context, person.actor_id(), request_counter, false).await?;
29 let post_id = comment.post_id;
30 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
33 // Although mentions could be gotten from the post tags (they are included there), or the ccs,
34 // Its much easier to scrape them from the comment body, since the API has to do that
36 let mentions = scrape_text_for_mentions(&comment.content);
37 let recipient_ids = send_local_notifs(
48 let comment_view = blocking(context.pool(), move |conn| {
49 CommentView::read(conn, comment.id, None)
53 let res = CommentResponse {
59 context.chat_server().do_send(SendComment {
60 op: UserOperationCrud::CreateComment,
68 pub(crate) async fn receive_update_comment(
70 context: &LemmyContext,
71 request_counter: &mut i32,
72 ) -> Result<(), LemmyError> {
73 let note = NoteExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
74 .context(location_info!())?;
75 let person = get_actor_as_person(&update, context, request_counter).await?;
78 Comment::from_apub(¬e, context, person.actor_id(), request_counter, false).await?;
80 let comment_id = comment.id;
81 let post_id = comment.post_id;
82 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
84 let mentions = scrape_text_for_mentions(&comment.content);
86 send_local_notifs(mentions, comment, person, post, context.pool(), false).await?;
89 let comment_view = blocking(context.pool(), move |conn| {
90 CommentView::read(conn, comment_id, None)
94 let res = CommentResponse {
100 context.chat_server().do_send(SendComment {
101 op: UserOperationCrud::EditComment,
109 pub(crate) async fn receive_like_comment(
112 context: &LemmyContext,
113 request_counter: &mut i32,
114 ) -> Result<(), LemmyError> {
115 let person = get_actor_as_person(&like, context, request_counter).await?;
117 let comment_id = comment.id;
118 let like_form = CommentLikeForm {
120 post_id: comment.post_id,
121 person_id: person.id,
124 let person_id = person.id;
125 blocking(context.pool(), move |conn| {
126 CommentLike::remove(conn, person_id, comment_id)?;
127 CommentLike::like(conn, &like_form)
132 let comment_view = blocking(context.pool(), move |conn| {
133 CommentView::read(conn, comment_id, None)
137 // TODO get those recipient actor ids from somewhere
138 let recipient_ids = vec![];
139 let res = CommentResponse {
145 context.chat_server().do_send(SendComment {
146 op: UserOperation::CreateCommentLike,
154 pub(crate) async fn receive_dislike_comment(
157 context: &LemmyContext,
158 request_counter: &mut i32,
159 ) -> Result<(), LemmyError> {
160 let person = get_actor_as_person(&dislike, context, request_counter).await?;
162 let comment_id = comment.id;
163 let like_form = CommentLikeForm {
165 post_id: comment.post_id,
166 person_id: person.id,
169 let person_id = person.id;
170 blocking(context.pool(), move |conn| {
171 CommentLike::remove(conn, person_id, comment_id)?;
172 CommentLike::like(conn, &like_form)
177 let comment_view = blocking(context.pool(), move |conn| {
178 CommentView::read(conn, comment_id, None)
182 // TODO get those recipient actor ids from somewhere
183 let recipient_ids = vec![];
184 let res = CommentResponse {
190 context.chat_server().do_send(SendComment {
191 op: UserOperation::CreateCommentLike,
199 pub(crate) async fn receive_delete_comment(
200 context: &LemmyContext,
202 ) -> Result<(), LemmyError> {
203 let deleted_comment = blocking(context.pool(), move |conn| {
204 Comment::update_deleted(conn, comment.id, true)
209 let comment_id = deleted_comment.id;
210 let comment_view = blocking(context.pool(), move |conn| {
211 CommentView::read(conn, comment_id, None)
215 // TODO get those recipient actor ids from somewhere
216 let recipient_ids = vec![];
217 let res = CommentResponse {
222 context.chat_server().do_send(SendComment {
223 op: UserOperationCrud::EditComment,
231 pub(crate) async fn receive_remove_comment(
232 context: &LemmyContext,
234 ) -> Result<(), LemmyError> {
235 let removed_comment = blocking(context.pool(), move |conn| {
236 Comment::update_removed(conn, comment.id, true)
241 let comment_id = removed_comment.id;
242 let comment_view = blocking(context.pool(), move |conn| {
243 CommentView::read(conn, comment_id, None)
247 // TODO get those recipient actor ids from somewhere
248 let recipient_ids = vec![];
249 let res = CommentResponse {
254 context.chat_server().do_send(SendComment {
255 op: UserOperationCrud::EditComment,