WebsocketInfo,
},
DbPool,
+ LemmyContext,
LemmyError,
};
-use actix_web::client::Client;
+use actix_web::web::Data;
use lemmy_db::{
comment::*,
comment_view::*,
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<CommentResponse, LemmyError> {
let data: &CreateComment = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let content_slurs_removed = remove_slurs(&data.content.to_owned());
// Check for a community ban
let post_id = data.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
- check_community_ban(user.id, post.community_id, pool).await?;
+ check_community_ban(user.id, post.community_id, context.pool()).await?;
// Check if post is locked, no new comments
if post.locked {
// Create the comment
let comment_form2 = comment_form.clone();
- let inserted_comment =
- match blocking(pool, move |conn| Comment::create(&conn, &comment_form2)).await? {
- Ok(comment) => comment,
- Err(_e) => return Err(APIError::err("couldnt_create_comment").into()),
- };
+ let inserted_comment = match blocking(context.pool(), move |conn| {
+ Comment::create(&conn, &comment_form2)
+ })
+ .await?
+ {
+ Ok(comment) => comment,
+ Err(_e) => return Err(APIError::err("couldnt_create_comment").into()),
+ };
// Necessary to update the ap_id
let inserted_comment_id = inserted_comment.id;
- let updated_comment: Comment = match blocking(pool, move |conn| {
+ let updated_comment: Comment = match blocking(context.pool(), move |conn| {
let apub_id =
make_apub_endpoint(EndpointType::Comment, &inserted_comment_id.to_string()).to_string();
Comment::update_ap_id(&conn, inserted_comment_id, apub_id)
Err(_e) => return Err(APIError::err("couldnt_create_comment").into()),
};
- updated_comment.send_create(&user, &client, pool).await?;
+ updated_comment.send_create(&user, context).await?;
// Scan the comment for user mentions, add those rows
let mentions = scrape_text_for_mentions(&comment_form.content);
- let recipient_ids =
- send_local_notifs(mentions, updated_comment.clone(), &user, post, pool, true).await?;
+ let recipient_ids = send_local_notifs(
+ mentions,
+ updated_comment.clone(),
+ &user,
+ post,
+ context.pool(),
+ true,
+ )
+ .await?;
// You like your own comment by default
let like_form = CommentLikeForm {
};
let like = move |conn: &'_ _| CommentLike::like(&conn, &like_form);
- if blocking(pool, like).await?.is_err() {
+ if blocking(context.pool(), like).await?.is_err() {
return Err(APIError::err("couldnt_like_comment").into());
}
- updated_comment.send_like(&user, &client, pool).await?;
+ updated_comment.send_like(&user, context).await?;
let user_id = user.id;
- let comment_view = blocking(pool, move |conn| {
+ let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(&conn, inserted_comment.id, Some(user_id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<CommentResponse, LemmyError> {
let data: &EditComment = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let edit_id = data.edit_id;
- let orig_comment =
- blocking(pool, move |conn| CommentView::read(&conn, edit_id, None)).await??;
+ let orig_comment = blocking(context.pool(), move |conn| {
+ CommentView::read(&conn, edit_id, None)
+ })
+ .await??;
- check_community_ban(user.id, orig_comment.community_id, pool).await?;
+ check_community_ban(user.id, orig_comment.community_id, context.pool()).await?;
// Verify that only the creator can edit
if user.id != orig_comment.creator_id {
// Do the update
let content_slurs_removed = remove_slurs(&data.content.to_owned());
let edit_id = data.edit_id;
- let updated_comment = match blocking(pool, move |conn| {
+ let updated_comment = match blocking(context.pool(), move |conn| {
Comment::update_content(conn, edit_id, &content_slurs_removed)
})
.await?
};
// Send the apub update
- updated_comment.send_update(&user, &client, pool).await?;
+ updated_comment.send_update(&user, context).await?;
// Do the mentions / recipients
let post_id = orig_comment.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let updated_comment_content = updated_comment.content.to_owned();
let mentions = scrape_text_for_mentions(&updated_comment_content);
- let recipient_ids =
- send_local_notifs(mentions, updated_comment, &user, post, pool, false).await?;
+ let recipient_ids = send_local_notifs(
+ mentions,
+ updated_comment,
+ &user,
+ post,
+ context.pool(),
+ false,
+ )
+ .await?;
let edit_id = data.edit_id;
let user_id = user.id;
- let comment_view = blocking(pool, move |conn| {
+ let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, edit_id, Some(user_id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<CommentResponse, LemmyError> {
let data: &DeleteComment = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let edit_id = data.edit_id;
- let orig_comment =
- blocking(pool, move |conn| CommentView::read(&conn, edit_id, None)).await??;
+ let orig_comment = blocking(context.pool(), move |conn| {
+ CommentView::read(&conn, edit_id, None)
+ })
+ .await??;
- check_community_ban(user.id, orig_comment.community_id, pool).await?;
+ check_community_ban(user.id, orig_comment.community_id, context.pool()).await?;
// Verify that only the creator can delete
if user.id != orig_comment.creator_id {
// Do the delete
let deleted = data.deleted;
- let updated_comment = match blocking(pool, move |conn| {
+ let updated_comment = match blocking(context.pool(), move |conn| {
Comment::update_deleted(conn, edit_id, deleted)
})
.await?
// Send the apub message
if deleted {
- updated_comment.send_delete(&user, &client, pool).await?;
+ updated_comment.send_delete(&user, context).await?;
} else {
- updated_comment
- .send_undo_delete(&user, &client, pool)
- .await?;
+ updated_comment.send_undo_delete(&user, context).await?;
}
// Refetch it
let edit_id = data.edit_id;
let user_id = user.id;
- let comment_view = blocking(pool, move |conn| {
+ let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, edit_id, Some(user_id))
})
.await??;
// Build the recipients
let post_id = comment_view.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let mentions = vec![];
- let recipient_ids =
- send_local_notifs(mentions, updated_comment, &user, post, pool, false).await?;
+ let recipient_ids = send_local_notifs(
+ mentions,
+ updated_comment,
+ &user,
+ post,
+ context.pool(),
+ false,
+ )
+ .await?;
let mut res = CommentResponse {
comment: comment_view,
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<CommentResponse, LemmyError> {
let data: &RemoveComment = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let edit_id = data.edit_id;
- let orig_comment =
- blocking(pool, move |conn| CommentView::read(&conn, edit_id, None)).await??;
+ let orig_comment = blocking(context.pool(), move |conn| {
+ CommentView::read(&conn, edit_id, None)
+ })
+ .await??;
- check_community_ban(user.id, orig_comment.community_id, pool).await?;
+ check_community_ban(user.id, orig_comment.community_id, context.pool()).await?;
// Verify that only a mod or admin can remove
- is_mod_or_admin(pool, user.id, orig_comment.community_id).await?;
+ is_mod_or_admin(context.pool(), user.id, orig_comment.community_id).await?;
// Do the remove
let removed = data.removed;
- let updated_comment = match blocking(pool, move |conn| {
+ let updated_comment = match blocking(context.pool(), move |conn| {
Comment::update_removed(conn, edit_id, removed)
})
.await?
removed: Some(removed),
reason: data.reason.to_owned(),
};
- blocking(pool, move |conn| ModRemoveComment::create(conn, &form)).await??;
+ blocking(context.pool(), move |conn| {
+ ModRemoveComment::create(conn, &form)
+ })
+ .await??;
// Send the apub message
if removed {
- updated_comment.send_remove(&user, &client, pool).await?;
+ updated_comment.send_remove(&user, context).await?;
} else {
- updated_comment
- .send_undo_remove(&user, &client, pool)
- .await?;
+ updated_comment.send_undo_remove(&user, context).await?;
}
// Refetch it
let edit_id = data.edit_id;
let user_id = user.id;
- let comment_view = blocking(pool, move |conn| {
+ let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, edit_id, Some(user_id))
})
.await??;
// Build the recipients
let post_id = comment_view.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let mentions = vec![];
- let recipient_ids =
- send_local_notifs(mentions, updated_comment, &user, post, pool, false).await?;
+ let recipient_ids = send_local_notifs(
+ mentions,
+ updated_comment,
+ &user,
+ post,
+ context.pool(),
+ false,
+ )
+ .await?;
let mut res = CommentResponse {
comment: comment_view,
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<CommentResponse, LemmyError> {
let data: &MarkCommentAsRead = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let edit_id = data.edit_id;
- let orig_comment =
- blocking(pool, move |conn| CommentView::read(&conn, edit_id, None)).await??;
+ let orig_comment = blocking(context.pool(), move |conn| {
+ CommentView::read(&conn, edit_id, None)
+ })
+ .await??;
- check_community_ban(user.id, orig_comment.community_id, pool).await?;
+ check_community_ban(user.id, orig_comment.community_id, context.pool()).await?;
// Verify that only the recipient can mark as read
// Needs to fetch the parent comment / post to get the recipient
let parent_id = orig_comment.parent_id;
match parent_id {
Some(pid) => {
- let parent_comment =
- blocking(pool, move |conn| CommentView::read(&conn, pid, None)).await??;
+ let parent_comment = blocking(context.pool(), move |conn| {
+ CommentView::read(&conn, pid, None)
+ })
+ .await??;
if user.id != parent_comment.creator_id {
return Err(APIError::err("no_comment_edit_allowed").into());
}
}
None => {
let parent_post_id = orig_comment.post_id;
- let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
+ let parent_post =
+ blocking(context.pool(), move |conn| Post::read(conn, parent_post_id)).await??;
if user.id != parent_post.creator_id {
return Err(APIError::err("no_comment_edit_allowed").into());
}
// Do the mark as read
let read = data.read;
- match blocking(pool, move |conn| Comment::update_read(conn, edit_id, read)).await? {
+ match blocking(context.pool(), move |conn| {
+ Comment::update_read(conn, edit_id, read)
+ })
+ .await?
+ {
Ok(comment) => comment,
Err(_e) => return Err(APIError::err("couldnt_update_comment").into()),
};
// Refetch it
let edit_id = data.edit_id;
let user_id = user.id;
- let comment_view = blocking(pool, move |conn| {
+ let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, edit_id, Some(user_id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<CommentResponse, LemmyError> {
let data: &SaveComment = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let comment_saved_form = CommentSavedForm {
comment_id: data.comment_id,
if data.save {
let save_comment = move |conn: &'_ _| CommentSaved::save(conn, &comment_saved_form);
- if blocking(pool, save_comment).await?.is_err() {
+ if blocking(context.pool(), save_comment).await?.is_err() {
return Err(APIError::err("couldnt_save_comment").into());
}
} else {
let unsave_comment = move |conn: &'_ _| CommentSaved::unsave(conn, &comment_saved_form);
- if blocking(pool, unsave_comment).await?.is_err() {
+ if blocking(context.pool(), unsave_comment).await?.is_err() {
return Err(APIError::err("couldnt_save_comment").into());
}
}
let comment_id = data.comment_id;
let user_id = user.id;
- let comment_view = blocking(pool, move |conn| {
+ let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, comment_id, Some(user_id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<CommentResponse, LemmyError> {
let data: &CreateCommentLike = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let mut recipient_ids = Vec::new();
// Don't do a downvote if site has downvotes disabled
if data.score == -1 {
- let site = blocking(pool, move |conn| SiteView::read(conn)).await??;
+ let site = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
if !site.enable_downvotes {
return Err(APIError::err("downvotes_disabled").into());
}
}
let comment_id = data.comment_id;
- let orig_comment =
- blocking(pool, move |conn| CommentView::read(&conn, comment_id, None)).await??;
+ let orig_comment = blocking(context.pool(), move |conn| {
+ CommentView::read(&conn, comment_id, None)
+ })
+ .await??;
let post_id = orig_comment.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
- check_community_ban(user.id, post.community_id, pool).await?;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
+ check_community_ban(user.id, post.community_id, context.pool()).await?;
let comment_id = data.comment_id;
- let comment = blocking(pool, move |conn| Comment::read(conn, comment_id)).await??;
+ let comment = blocking(context.pool(), move |conn| Comment::read(conn, comment_id)).await??;
// Add to recipient ids
match comment.parent_id {
Some(parent_id) => {
- let parent_comment = blocking(pool, move |conn| Comment::read(conn, parent_id)).await??;
+ let parent_comment =
+ blocking(context.pool(), move |conn| Comment::read(conn, parent_id)).await??;
if parent_comment.creator_id != user.id {
- let parent_user = blocking(pool, move |conn| {
+ let parent_user = blocking(context.pool(), move |conn| {
User_::read(conn, parent_comment.creator_id)
})
.await??;
// Remove any likes first
let user_id = user.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
CommentLike::remove(conn, user_id, comment_id)
})
.await??;
if do_add {
let like_form2 = like_form.clone();
let like = move |conn: &'_ _| CommentLike::like(conn, &like_form2);
- if blocking(pool, like).await?.is_err() {
+ if blocking(context.pool(), like).await?.is_err() {
return Err(APIError::err("couldnt_like_comment").into());
}
if like_form.score == 1 {
- comment.send_like(&user, &client, pool).await?;
+ comment.send_like(&user, context).await?;
} else if like_form.score == -1 {
- comment.send_dislike(&user, &client, pool).await?;
+ comment.send_dislike(&user, context).await?;
}
} else {
- comment.send_undo_like(&user, &client, pool).await?;
+ comment.send_undo_like(&user, context).await?;
}
// Have to refetch the comment to get the current state
let comment_id = data.comment_id;
let user_id = user.id;
- let liked_comment = blocking(pool, move |conn| {
+ let liked_comment = blocking(context.pool(), move |conn| {
CommentView::read(conn, comment_id, Some(user_id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetCommentsResponse, LemmyError> {
let data: &GetComments = &self;
- let user = get_user_from_jwt_opt(&data.auth, pool).await?;
+ let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let user_id = user.map(|u| u.id);
let type_ = ListingType::from_str(&data.type_)?;
let community_id = data.community_id;
let page = data.page;
let limit = data.limit;
- let comments = blocking(pool, move |conn| {
+ let comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn)
.listing_type(type_)
.sort(&sort)
UserOperation,
WebsocketInfo,
},
- DbPool,
};
-use actix_web::client::Client;
use anyhow::Context;
use lemmy_db::{
comment::Comment,
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetCommunityResponse, LemmyError> {
let data: &GetCommunity = &self;
- let user = get_user_from_jwt_opt(&data.auth, pool).await?;
+ let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let user_id = user.map(|u| u.id);
let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
let community = match data.id {
- Some(id) => blocking(pool, move |conn| Community::read(conn, id)).await??,
- None => match blocking(pool, move |conn| Community::read_from_name(conn, &name)).await? {
+ Some(id) => blocking(context.pool(), move |conn| Community::read(conn, id)).await??,
+ None => match blocking(context.pool(), move |conn| {
+ Community::read_from_name(conn, &name)
+ })
+ .await?
+ {
Ok(community) => community,
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
},
};
let community_id = community.id;
- let community_view = match blocking(pool, move |conn| {
+ let community_view = match blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, user_id)
})
.await?
};
let community_id = community.id;
- let moderators: Vec<CommunityModeratorView> = match blocking(pool, move |conn| {
+ let moderators: Vec<CommunityModeratorView> = match blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(conn, community_id)
})
.await?
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<CommunityResponse, LemmyError> {
let data: &CreateCommunity = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.name)?;
check_slurs(&data.title)?;
// Double check for duplicate community actor_ids
let actor_id = make_apub_endpoint(EndpointType::Community, &data.name).to_string();
let actor_id_cloned = actor_id.to_owned();
- let community_dupe = blocking(pool, move |conn| {
+ let community_dupe = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, &actor_id_cloned)
})
.await?;
published: None,
};
- let inserted_community =
- match blocking(pool, move |conn| Community::create(conn, &community_form)).await? {
- Ok(community) => community,
- Err(_e) => return Err(APIError::err("community_already_exists").into()),
- };
+ let inserted_community = match blocking(context.pool(), move |conn| {
+ Community::create(conn, &community_form)
+ })
+ .await?
+ {
+ Ok(community) => community,
+ Err(_e) => return Err(APIError::err("community_already_exists").into()),
+ };
let community_moderator_form = CommunityModeratorForm {
community_id: inserted_community.id,
};
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
- if blocking(pool, join).await?.is_err() {
+ if blocking(context.pool(), join).await?.is_err() {
return Err(APIError::err("community_moderator_already_exists").into());
}
};
let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
- if blocking(pool, follow).await?.is_err() {
+ if blocking(context.pool(), follow).await?.is_err() {
return Err(APIError::err("community_follower_already_exists").into());
}
let user_id = user.id;
- let community_view = blocking(pool, move |conn| {
+ let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, inserted_community.id, Some(user_id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<CommunityResponse, LemmyError> {
let data: &EditCommunity = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.title)?;
check_slurs_opt(&data.description)?;
// Verify its a mod (only mods can edit it)
let edit_id = data.edit_id;
- let mods: Vec<i32> = blocking(pool, move |conn| {
+ let mods: Vec<i32> = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(conn, edit_id)
.map(|v| v.into_iter().map(|m| m.user_id).collect())
})
}
let edit_id = data.edit_id;
- let read_community = blocking(pool, move |conn| Community::read(conn, edit_id)).await??;
+ let read_community =
+ blocking(context.pool(), move |conn| Community::read(conn, edit_id)).await??;
let icon = diesel_option_overwrite(&data.icon);
let banner = diesel_option_overwrite(&data.banner);
};
let edit_id = data.edit_id;
- match blocking(pool, move |conn| {
+ match blocking(context.pool(), move |conn| {
Community::update(conn, edit_id, &community_form)
})
.await?
let edit_id = data.edit_id;
let user_id = user.id;
- let community_view = blocking(pool, move |conn| {
+ let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, edit_id, Some(user_id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<CommunityResponse, LemmyError> {
let data: &DeleteCommunity = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Verify its the creator (only a creator can delete the community)
let edit_id = data.edit_id;
- let read_community = blocking(pool, move |conn| Community::read(conn, edit_id)).await??;
+ let read_community =
+ blocking(context.pool(), move |conn| Community::read(conn, edit_id)).await??;
if read_community.creator_id != user.id {
return Err(APIError::err("no_community_edit_allowed").into());
}
// Do the delete
let edit_id = data.edit_id;
let deleted = data.deleted;
- let updated_community = match blocking(pool, move |conn| {
+ let updated_community = match blocking(context.pool(), move |conn| {
Community::update_deleted(conn, edit_id, deleted)
})
.await?
// Send apub messages
if deleted {
- updated_community.send_delete(&user, &client, pool).await?;
+ updated_community.send_delete(&user, context).await?;
} else {
- updated_community
- .send_undo_delete(&user, &client, pool)
- .await?;
+ updated_community.send_undo_delete(&user, context).await?;
}
let edit_id = data.edit_id;
let user_id = user.id;
- let community_view = blocking(pool, move |conn| {
+ let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, edit_id, Some(user_id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<CommunityResponse, LemmyError> {
let data: &RemoveCommunity = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Verify its an admin (only an admin can remove a community)
- is_admin(pool, user.id).await?;
+ is_admin(context.pool(), user.id).await?;
// Do the remove
let edit_id = data.edit_id;
let removed = data.removed;
- let updated_community = match blocking(pool, move |conn| {
+ let updated_community = match blocking(context.pool(), move |conn| {
Community::update_removed(conn, edit_id, removed)
})
.await?
reason: data.reason.to_owned(),
expires,
};
- blocking(pool, move |conn| ModRemoveCommunity::create(conn, &form)).await??;
+ blocking(context.pool(), move |conn| {
+ ModRemoveCommunity::create(conn, &form)
+ })
+ .await??;
// Apub messages
if removed {
- updated_community.send_remove(&user, &client, pool).await?;
+ updated_community.send_remove(&user, context).await?;
} else {
- updated_community
- .send_undo_remove(&user, &client, pool)
- .await?;
+ updated_community.send_undo_remove(&user, context).await?;
}
let edit_id = data.edit_id;
let user_id = user.id;
- let community_view = blocking(pool, move |conn| {
+ let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, edit_id, Some(user_id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<ListCommunitiesResponse, LemmyError> {
let data: &ListCommunities = &self;
- let user = get_user_from_jwt_opt(&data.auth, pool).await?;
+ let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let user_id = match &user {
Some(user) => Some(user.id),
let page = data.page;
let limit = data.limit;
- let communities = blocking(pool, move |conn| {
+ let communities = blocking(context.pool(), move |conn| {
CommunityQueryBuilder::create(conn)
.sort(&sort)
.for_user(user_id)
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<CommunityResponse, LemmyError> {
let data: &FollowCommunity = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let community_id = data.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let community_follower_form = CommunityFollowerForm {
community_id: data.community_id,
user_id: user.id,
if community.local {
if data.follow {
let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
- if blocking(pool, follow).await?.is_err() {
+ if blocking(context.pool(), follow).await?.is_err() {
return Err(APIError::err("community_follower_already_exists").into());
}
} else {
let unfollow =
move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
- if blocking(pool, unfollow).await?.is_err() {
+ if blocking(context.pool(), unfollow).await?.is_err() {
return Err(APIError::err("community_follower_already_exists").into());
}
}
} else if data.follow {
// Dont actually add to the community followers here, because you need
// to wait for the accept
- user
- .send_follow(&community.actor_id()?, &client, pool)
- .await?;
+ user.send_follow(&community.actor_id()?, context).await?;
} else {
- user
- .send_unfollow(&community.actor_id()?, &client, pool)
- .await?;
+ user.send_unfollow(&community.actor_id()?, context).await?;
let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
- if blocking(pool, unfollow).await?.is_err() {
+ if blocking(context.pool(), unfollow).await?.is_err() {
return Err(APIError::err("community_follower_already_exists").into());
}
}
let community_id = data.community_id;
let user_id = user.id;
- let community_view = blocking(pool, move |conn| {
+ let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, Some(user_id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetFollowedCommunitiesResponse, LemmyError> {
let data: &GetFollowedCommunities = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let user_id = user.id;
- let communities = match blocking(pool, move |conn| {
+ let communities = match blocking(context.pool(), move |conn| {
CommunityFollowerView::for_user(conn, user_id)
})
.await?
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<BanFromCommunityResponse, LemmyError> {
let data: &BanFromCommunity = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let community_id = data.community_id;
let banned_user_id = data.user_id;
// Verify that only mods or admins can ban
- is_mod_or_admin(pool, user.id, community_id).await?;
+ is_mod_or_admin(context.pool(), user.id, community_id).await?;
let community_user_ban_form = CommunityUserBanForm {
community_id: data.community_id,
if data.ban {
let ban = move |conn: &'_ _| CommunityUserBan::ban(conn, &community_user_ban_form);
- if blocking(pool, ban).await?.is_err() {
+ if blocking(context.pool(), ban).await?.is_err() {
return Err(APIError::err("community_user_already_banned").into());
}
} else {
let unban = move |conn: &'_ _| CommunityUserBan::unban(conn, &community_user_ban_form);
- if blocking(pool, unban).await?.is_err() {
+ if blocking(context.pool(), unban).await?.is_err() {
return Err(APIError::err("community_user_already_banned").into());
}
}
// Remove/Restore their data if that's desired
if let Some(remove_data) = data.remove_data {
// Posts
- blocking(pool, move |conn: &'_ _| {
+ blocking(context.pool(), move |conn: &'_ _| {
Post::update_removed_for_creator(conn, banned_user_id, Some(community_id), remove_data)
})
.await??;
// Comments
// Diesel doesn't allow updates with joins, so this has to be a loop
- let comments = blocking(pool, move |conn| {
+ let comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn)
.for_creator_id(banned_user_id)
.for_community_id(community_id)
for comment in &comments {
let comment_id = comment.id;
- blocking(pool, move |conn: &'_ _| {
+ blocking(context.pool(), move |conn: &'_ _| {
Comment::update_removed(conn, comment_id, remove_data)
})
.await??;
banned: Some(data.ban),
expires,
};
- blocking(pool, move |conn| ModBanFromCommunity::create(conn, &form)).await??;
+ blocking(context.pool(), move |conn| {
+ ModBanFromCommunity::create(conn, &form)
+ })
+ .await??;
let user_id = data.user_id;
- let user_view = blocking(pool, move |conn| UserView::get_user_secure(conn, user_id)).await??;
+ let user_view = blocking(context.pool(), move |conn| {
+ UserView::get_user_secure(conn, user_id)
+ })
+ .await??;
let res = BanFromCommunityResponse {
user: user_view,
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<AddModToCommunityResponse, LemmyError> {
let data: &AddModToCommunity = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let community_moderator_form = CommunityModeratorForm {
community_id: data.community_id,
let community_id = data.community_id;
// Verify that only mods or admins can add mod
- is_mod_or_admin(pool, user.id, community_id).await?;
+ is_mod_or_admin(context.pool(), user.id, community_id).await?;
if data.added {
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
- if blocking(pool, join).await?.is_err() {
+ if blocking(context.pool(), join).await?.is_err() {
return Err(APIError::err("community_moderator_already_exists").into());
}
} else {
let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
- if blocking(pool, leave).await?.is_err() {
+ if blocking(context.pool(), leave).await?.is_err() {
return Err(APIError::err("community_moderator_already_exists").into());
}
}
community_id: data.community_id,
removed: Some(!data.added),
};
- blocking(pool, move |conn| ModAddCommunity::create(conn, &form)).await??;
+ blocking(context.pool(), move |conn| {
+ ModAddCommunity::create(conn, &form)
+ })
+ .await??;
let community_id = data.community_id;
- let moderators = blocking(pool, move |conn| {
+ let moderators = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(conn, community_id)
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetCommunityResponse, LemmyError> {
let data: &TransferCommunity = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let community_id = data.community_id;
- let read_community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let read_community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
- let site_creator_id =
- blocking(pool, move |conn| Site::read(conn, 1).map(|s| s.creator_id)).await??;
+ let site_creator_id = blocking(context.pool(), move |conn| {
+ Site::read(conn, 1).map(|s| s.creator_id)
+ })
+ .await??;
- let mut admins = blocking(pool, move |conn| UserView::admins(conn)).await??;
+ let mut admins = blocking(context.pool(), move |conn| UserView::admins(conn)).await??;
let creator_index = admins
.iter()
let community_id = data.community_id;
let new_creator = data.user_id;
let update = move |conn: &'_ _| Community::update_creator(conn, community_id, new_creator);
- if blocking(pool, update).await?.is_err() {
+ if blocking(context.pool(), update).await?.is_err() {
return Err(APIError::err("couldnt_update_community").into());
};
// You also have to re-do the community_moderator table, reordering it.
let community_id = data.community_id;
- let mut community_mods = blocking(pool, move |conn| {
+ let mut community_mods = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(conn, community_id)
})
.await??;
community_mods.insert(0, creator_user);
let community_id = data.community_id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
CommunityModerator::delete_for_community(conn, community_id)
})
.await??;
};
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
- if blocking(pool, join).await?.is_err() {
+ if blocking(context.pool(), join).await?.is_err() {
return Err(APIError::err("community_moderator_already_exists").into());
}
}
community_id: data.community_id,
removed: Some(false),
};
- blocking(pool, move |conn| ModAddCommunity::create(conn, &form)).await??;
+ blocking(context.pool(), move |conn| {
+ ModAddCommunity::create(conn, &form)
+ })
+ .await??;
let community_id = data.community_id;
let user_id = user.id;
- let community_view = match blocking(pool, move |conn| {
+ let community_view = match blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, Some(user_id))
})
.await?
};
let community_id = data.community_id;
- let moderators = match blocking(pool, move |conn| {
+ let moderators = match blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(conn, community_id)
})
.await?
-use crate::{api::claims::Claims, blocking, websocket::WebsocketInfo, DbPool, LemmyError};
-use actix_web::client::Client;
+use crate::{
+ api::claims::Claims,
+ blocking,
+ websocket::WebsocketInfo,
+ DbPool,
+ LemmyContext,
+ LemmyError,
+};
+use actix_web::web::Data;
use lemmy_db::{
community::*,
community_view::*,
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<Self::Response, LemmyError>;
}
UserOperation,
WebsocketInfo,
},
- DbPool,
+ LemmyContext,
LemmyError,
};
-use actix_web::client::Client;
+use actix_web::web::Data;
use lemmy_db::{
comment_view::*,
community_view::*,
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<PostResponse, LemmyError> {
let data: &CreatePost = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.name)?;
check_slurs_opt(&data.body)?;
return Err(APIError::err("invalid_post_title").into());
}
- check_community_ban(user.id, data.community_id, pool).await?;
+ check_community_ban(user.id, data.community_id, context.pool()).await?;
if let Some(url) = data.url.as_ref() {
match Url::parse(url) {
// Fetch Iframely and pictrs cached image
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
- fetch_iframely_and_pictrs_data(&client, data.url.to_owned()).await;
+ fetch_iframely_and_pictrs_data(context.client(), data.url.to_owned()).await;
let post_form = PostForm {
name: data.name.trim().to_owned(),
published: None,
};
- let inserted_post = match blocking(pool, move |conn| Post::create(conn, &post_form)).await? {
- Ok(post) => post,
- Err(e) => {
- let err_type = if e.to_string() == "value too long for type character varying(200)" {
- "post_title_too_long"
- } else {
- "couldnt_create_post"
- };
-
- return Err(APIError::err(err_type).into());
- }
- };
+ let inserted_post =
+ match blocking(context.pool(), move |conn| Post::create(conn, &post_form)).await? {
+ Ok(post) => post,
+ Err(e) => {
+ let err_type = if e.to_string() == "value too long for type character varying(200)" {
+ "post_title_too_long"
+ } else {
+ "couldnt_create_post"
+ };
+
+ return Err(APIError::err(err_type).into());
+ }
+ };
let inserted_post_id = inserted_post.id;
- let updated_post = match blocking(pool, move |conn| {
+ let updated_post = match blocking(context.pool(), move |conn| {
let apub_id =
make_apub_endpoint(EndpointType::Post, &inserted_post_id.to_string()).to_string();
Post::update_ap_id(conn, inserted_post_id, apub_id)
Err(_e) => return Err(APIError::err("couldnt_create_post").into()),
};
- updated_post.send_create(&user, &client, pool).await?;
+ updated_post.send_create(&user, context).await?;
// They like their own post by default
let like_form = PostLikeForm {
};
let like = move |conn: &'_ _| PostLike::like(conn, &like_form);
- if blocking(pool, like).await?.is_err() {
+ if blocking(context.pool(), like).await?.is_err() {
return Err(APIError::err("couldnt_like_post").into());
}
- updated_post.send_like(&user, &client, pool).await?;
+ updated_post.send_like(&user, context).await?;
// Refetch the view
let inserted_post_id = inserted_post.id;
- let post_view = match blocking(pool, move |conn| {
+ let post_view = match blocking(context.pool(), move |conn| {
PostView::read(conn, inserted_post_id, Some(user.id))
})
.await?
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetPostResponse, LemmyError> {
let data: &GetPost = &self;
- let user = get_user_from_jwt_opt(&data.auth, pool).await?;
+ let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let user_id = user.map(|u| u.id);
let id = data.id;
- let post_view = match blocking(pool, move |conn| PostView::read(conn, id, user_id)).await? {
+ let post_view = match blocking(context.pool(), move |conn| {
+ PostView::read(conn, id, user_id)
+ })
+ .await?
+ {
Ok(post) => post,
Err(_e) => return Err(APIError::err("couldnt_find_post").into()),
};
let id = data.id;
- let comments = blocking(pool, move |conn| {
+ let comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn)
.for_post_id(id)
.my_user_id(user_id)
.await??;
let community_id = post_view.community_id;
- let community = blocking(pool, move |conn| {
+ let community = blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, user_id)
})
.await??;
let community_id = post_view.community_id;
- let moderators = blocking(pool, move |conn| {
+ let moderators = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(conn, community_id)
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetPostsResponse, LemmyError> {
let data: &GetPosts = &self;
- let user = get_user_from_jwt_opt(&data.auth, pool).await?;
+ let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let user_id = match &user {
Some(user) => Some(user.id),
let limit = data.limit;
let community_id = data.community_id;
let community_name = data.community_name.to_owned();
- let posts = match blocking(pool, move |conn| {
+ let posts = match blocking(context.pool(), move |conn| {
PostQueryBuilder::create(conn)
.listing_type(type_)
.sort(&sort)
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<PostResponse, LemmyError> {
let data: &CreatePostLike = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Don't do a downvote if site has downvotes disabled
if data.score == -1 {
- let site = blocking(pool, move |conn| SiteView::read(conn)).await??;
+ let site = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
if !site.enable_downvotes {
return Err(APIError::err("downvotes_disabled").into());
}
// Check for a community ban
let post_id = data.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
- check_community_ban(user.id, post.community_id, pool).await?;
+ check_community_ban(user.id, post.community_id, context.pool()).await?;
let like_form = PostLikeForm {
post_id: data.post_id,
// Remove any likes first
let user_id = user.id;
- blocking(pool, move |conn| PostLike::remove(conn, user_id, post_id)).await??;
+ blocking(context.pool(), move |conn| {
+ PostLike::remove(conn, user_id, post_id)
+ })
+ .await??;
// Only add the like if the score isnt 0
let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
if do_add {
let like_form2 = like_form.clone();
let like = move |conn: &'_ _| PostLike::like(conn, &like_form2);
- if blocking(pool, like).await?.is_err() {
+ if blocking(context.pool(), like).await?.is_err() {
return Err(APIError::err("couldnt_like_post").into());
}
if like_form.score == 1 {
- post.send_like(&user, &client, pool).await?;
+ post.send_like(&user, context).await?;
} else if like_form.score == -1 {
- post.send_dislike(&user, &client, pool).await?;
+ post.send_dislike(&user, context).await?;
}
} else {
- post.send_undo_like(&user, &client, pool).await?;
+ post.send_undo_like(&user, context).await?;
}
let post_id = data.post_id;
let user_id = user.id;
- let post_view = match blocking(pool, move |conn| {
+ let post_view = match blocking(context.pool(), move |conn| {
PostView::read(conn, post_id, Some(user_id))
})
.await?
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<PostResponse, LemmyError> {
let data: &EditPost = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.name)?;
check_slurs_opt(&data.body)?;
}
let edit_id = data.edit_id;
- let orig_post = blocking(pool, move |conn| Post::read(conn, edit_id)).await??;
+ let orig_post = blocking(context.pool(), move |conn| Post::read(conn, edit_id)).await??;
- check_community_ban(user.id, orig_post.community_id, pool).await?;
+ check_community_ban(user.id, orig_post.community_id, context.pool()).await?;
// Verify that only the creator can edit
if !Post::is_post_creator(user.id, orig_post.creator_id) {
// Fetch Iframely and Pictrs cached image
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
- fetch_iframely_and_pictrs_data(&client, data.url.to_owned()).await;
+ fetch_iframely_and_pictrs_data(context.client(), data.url.to_owned()).await;
let post_form = PostForm {
name: data.name.trim().to_owned(),
};
let edit_id = data.edit_id;
- let res = blocking(pool, move |conn| Post::update(conn, edit_id, &post_form)).await?;
+ let res = blocking(context.pool(), move |conn| {
+ Post::update(conn, edit_id, &post_form)
+ })
+ .await?;
let updated_post: Post = match res {
Ok(post) => post,
Err(e) => {
};
// Send apub update
- updated_post.send_update(&user, &client, pool).await?;
+ updated_post.send_update(&user, context).await?;
let edit_id = data.edit_id;
- let post_view = blocking(pool, move |conn| {
+ let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, edit_id, Some(user.id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<PostResponse, LemmyError> {
let data: &DeletePost = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let edit_id = data.edit_id;
- let orig_post = blocking(pool, move |conn| Post::read(conn, edit_id)).await??;
+ let orig_post = blocking(context.pool(), move |conn| Post::read(conn, edit_id)).await??;
- check_community_ban(user.id, orig_post.community_id, pool).await?;
+ check_community_ban(user.id, orig_post.community_id, context.pool()).await?;
// Verify that only the creator can delete
if !Post::is_post_creator(user.id, orig_post.creator_id) {
// Update the post
let edit_id = data.edit_id;
let deleted = data.deleted;
- let updated_post = blocking(pool, move |conn| {
+ let updated_post = blocking(context.pool(), move |conn| {
Post::update_deleted(conn, edit_id, deleted)
})
.await??;
// apub updates
if deleted {
- updated_post.send_delete(&user, &client, pool).await?;
+ updated_post.send_delete(&user, context).await?;
} else {
- updated_post.send_undo_delete(&user, &client, pool).await?;
+ updated_post.send_undo_delete(&user, context).await?;
}
// Refetch the post
let edit_id = data.edit_id;
- let post_view = blocking(pool, move |conn| {
+ let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, edit_id, Some(user.id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<PostResponse, LemmyError> {
let data: &RemovePost = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let edit_id = data.edit_id;
- let orig_post = blocking(pool, move |conn| Post::read(conn, edit_id)).await??;
+ let orig_post = blocking(context.pool(), move |conn| Post::read(conn, edit_id)).await??;
- check_community_ban(user.id, orig_post.community_id, pool).await?;
+ check_community_ban(user.id, orig_post.community_id, context.pool()).await?;
// Verify that only the mods can remove
- is_mod_or_admin(pool, user.id, orig_post.community_id).await?;
+ is_mod_or_admin(context.pool(), user.id, orig_post.community_id).await?;
// Update the post
let edit_id = data.edit_id;
let removed = data.removed;
- let updated_post = blocking(pool, move |conn| {
+ let updated_post = blocking(context.pool(), move |conn| {
Post::update_removed(conn, edit_id, removed)
})
.await??;
removed: Some(removed),
reason: data.reason.to_owned(),
};
- blocking(pool, move |conn| ModRemovePost::create(conn, &form)).await??;
+ blocking(context.pool(), move |conn| {
+ ModRemovePost::create(conn, &form)
+ })
+ .await??;
// apub updates
if removed {
- updated_post.send_remove(&user, &client, pool).await?;
+ updated_post.send_remove(&user, context).await?;
} else {
- updated_post.send_undo_remove(&user, &client, pool).await?;
+ updated_post.send_undo_remove(&user, context).await?;
}
// Refetch the post
let edit_id = data.edit_id;
let user_id = user.id;
- let post_view = blocking(pool, move |conn| {
+ let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, edit_id, Some(user_id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<PostResponse, LemmyError> {
let data: &LockPost = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let edit_id = data.edit_id;
- let orig_post = blocking(pool, move |conn| Post::read(conn, edit_id)).await??;
+ let orig_post = blocking(context.pool(), move |conn| Post::read(conn, edit_id)).await??;
- check_community_ban(user.id, orig_post.community_id, pool).await?;
+ check_community_ban(user.id, orig_post.community_id, context.pool()).await?;
// Verify that only the mods can lock
- is_mod_or_admin(pool, user.id, orig_post.community_id).await?;
+ is_mod_or_admin(context.pool(), user.id, orig_post.community_id).await?;
// Update the post
let edit_id = data.edit_id;
let locked = data.locked;
- let updated_post =
- blocking(pool, move |conn| Post::update_locked(conn, edit_id, locked)).await??;
+ let updated_post = blocking(context.pool(), move |conn| {
+ Post::update_locked(conn, edit_id, locked)
+ })
+ .await??;
// Mod tables
let form = ModLockPostForm {
post_id: data.edit_id,
locked: Some(locked),
};
- blocking(pool, move |conn| ModLockPost::create(conn, &form)).await??;
+ blocking(context.pool(), move |conn| ModLockPost::create(conn, &form)).await??;
// apub updates
- updated_post.send_update(&user, &client, pool).await?;
+ updated_post.send_update(&user, context).await?;
// Refetch the post
let edit_id = data.edit_id;
- let post_view = blocking(pool, move |conn| {
+ let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, edit_id, Some(user.id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<PostResponse, LemmyError> {
let data: &StickyPost = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let edit_id = data.edit_id;
- let orig_post = blocking(pool, move |conn| Post::read(conn, edit_id)).await??;
+ let orig_post = blocking(context.pool(), move |conn| Post::read(conn, edit_id)).await??;
- check_community_ban(user.id, orig_post.community_id, pool).await?;
+ check_community_ban(user.id, orig_post.community_id, context.pool()).await?;
// Verify that only the mods can sticky
- is_mod_or_admin(pool, user.id, orig_post.community_id).await?;
+ is_mod_or_admin(context.pool(), user.id, orig_post.community_id).await?;
// Update the post
let edit_id = data.edit_id;
let stickied = data.stickied;
- let updated_post = blocking(pool, move |conn| {
+ let updated_post = blocking(context.pool(), move |conn| {
Post::update_stickied(conn, edit_id, stickied)
})
.await??;
post_id: data.edit_id,
stickied: Some(stickied),
};
- blocking(pool, move |conn| ModStickyPost::create(conn, &form)).await??;
+ blocking(context.pool(), move |conn| {
+ ModStickyPost::create(conn, &form)
+ })
+ .await??;
// Apub updates
// TODO stickied should pry work like locked for ease of use
- updated_post.send_update(&user, &client, pool).await?;
+ updated_post.send_update(&user, context).await?;
// Refetch the post
let edit_id = data.edit_id;
- let post_view = blocking(pool, move |conn| {
+ let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, edit_id, Some(user.id))
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<PostResponse, LemmyError> {
let data: &SavePost = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let post_saved_form = PostSavedForm {
post_id: data.post_id,
if data.save {
let save = move |conn: &'_ _| PostSaved::save(conn, &post_saved_form);
- if blocking(pool, save).await?.is_err() {
+ if blocking(context.pool(), save).await?.is_err() {
return Err(APIError::err("couldnt_save_post").into());
}
} else {
let unsave = move |conn: &'_ _| PostSaved::unsave(conn, &post_saved_form);
- if blocking(pool, unsave).await?.is_err() {
+ if blocking(context.pool(), unsave).await?.is_err() {
return Err(APIError::err("couldnt_save_post").into());
}
}
let post_id = data.post_id;
let user_id = user.id;
- let post_view = blocking(pool, move |conn| {
+ let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, post_id, Some(user_id))
})
.await??;
UserOperation,
WebsocketInfo,
},
- DbPool,
+ LemmyContext,
LemmyError,
};
-use actix_web::client::Client;
+use actix_web::web::Data;
use anyhow::Context;
use lemmy_db::{
category::*,
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<ListCategoriesResponse, LemmyError> {
let _data: &ListCategories = &self;
- let categories = blocking(pool, move |conn| Category::list_all(conn)).await??;
+ let categories = blocking(context.pool(), move |conn| Category::list_all(conn)).await??;
// Return the jwt
Ok(ListCategoriesResponse { categories })
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetModlogResponse, LemmyError> {
let data: &GetModlog = &self;
let mod_user_id = data.mod_user_id;
let page = data.page;
let limit = data.limit;
- let removed_posts = blocking(pool, move |conn| {
+ let removed_posts = blocking(context.pool(), move |conn| {
ModRemovePostView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
- let locked_posts = blocking(pool, move |conn| {
+ let locked_posts = blocking(context.pool(), move |conn| {
ModLockPostView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
- let stickied_posts = blocking(pool, move |conn| {
+ let stickied_posts = blocking(context.pool(), move |conn| {
ModStickyPostView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
- let removed_comments = blocking(pool, move |conn| {
+ let removed_comments = blocking(context.pool(), move |conn| {
ModRemoveCommentView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
- let banned_from_community = blocking(pool, move |conn| {
+ let banned_from_community = blocking(context.pool(), move |conn| {
ModBanFromCommunityView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
- let added_to_community = blocking(pool, move |conn| {
+ let added_to_community = blocking(context.pool(), move |conn| {
ModAddCommunityView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
// These arrays are only for the full modlog, when a community isn't given
let (removed_communities, banned, added) = if data.community_id.is_none() {
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
Ok((
ModRemoveCommunityView::list(conn, mod_user_id, page, limit)?,
ModBanView::list(conn, mod_user_id, page, limit)?,
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<SiteResponse, LemmyError> {
let data: &CreateSite = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.name)?;
check_slurs_opt(&data.description)?;
// Make sure user is an admin
- is_admin(pool, user.id).await?;
+ is_admin(context.pool(), user.id).await?;
let site_form = SiteForm {
name: data.name.to_owned(),
};
let create_site = move |conn: &'_ _| Site::create(conn, &site_form);
- if blocking(pool, create_site).await?.is_err() {
+ if blocking(context.pool(), create_site).await?.is_err() {
return Err(APIError::err("site_already_exists").into());
}
- let site_view = blocking(pool, move |conn| SiteView::read(conn)).await??;
+ let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
Ok(SiteResponse { site: site_view })
}
type Response = SiteResponse;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<SiteResponse, LemmyError> {
let data: &EditSite = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.name)?;
check_slurs_opt(&data.description)?;
// Make sure user is an admin
- is_admin(pool, user.id).await?;
+ is_admin(context.pool(), user.id).await?;
- let found_site = blocking(pool, move |conn| Site::read(conn, 1)).await??;
+ let found_site = blocking(context.pool(), move |conn| Site::read(conn, 1)).await??;
let icon = diesel_option_overwrite(&data.icon);
let banner = diesel_option_overwrite(&data.banner);
};
let update_site = move |conn: &'_ _| Site::update(conn, 1, &site_form);
- if blocking(pool, update_site).await?.is_err() {
+ if blocking(context.pool(), update_site).await?.is_err() {
return Err(APIError::err("couldnt_update_site").into());
}
- let site_view = blocking(pool, move |conn| SiteView::read(conn)).await??;
+ let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
let res = SiteResponse { site: site_view };
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<GetSiteResponse, LemmyError> {
let data: &GetSite = &self;
// TODO refactor this a little
- let res = blocking(pool, move |conn| Site::read(conn, 1)).await?;
+ let res = blocking(context.pool(), move |conn| Site::read(conn, 1)).await?;
let site_view = if res.is_ok() {
- Some(blocking(pool, move |conn| SiteView::read(conn)).await??)
+ Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
} else if let Some(setup) = Settings::get().setup.as_ref() {
let register = Register {
username: setup.admin_username.to_owned(),
captcha_uuid: None,
captcha_answer: None,
};
- let login_response = register
- .perform(pool, websocket_info.clone(), client.clone())
- .await?;
+ let login_response = register.perform(context, websocket_info.clone()).await?;
info!("Admin {} created", setup.admin_username);
let create_site = CreateSite {
enable_nsfw: true,
auth: login_response.jwt,
};
- create_site
- .perform(pool, websocket_info.clone(), client.clone())
- .await?;
+ create_site.perform(context, websocket_info.clone()).await?;
info!("Site {} created", setup.site_name);
- Some(blocking(pool, move |conn| SiteView::read(conn)).await??)
+ Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
} else {
None
};
- let mut admins = blocking(pool, move |conn| UserView::admins(conn)).await??;
+ let mut admins = blocking(context.pool(), move |conn| UserView::admins(conn)).await??;
// Make sure the site creator is the top admin
if let Some(site_view) = site_view.to_owned() {
}
}
- let banned = blocking(pool, move |conn| UserView::banned(conn)).await??;
+ let banned = blocking(context.pool(), move |conn| UserView::banned(conn)).await??;
let online = if let Some(ws) = websocket_info {
ws.chatserver.send(GetUsersOnline).await.unwrap_or(1)
0
};
- let my_user = get_user_from_jwt_opt(&data.auth, pool).await?.map(|mut u| {
- u.password_encrypted = "".to_string();
- u.private_key = None;
- u.public_key = None;
- u
- });
+ let my_user = get_user_from_jwt_opt(&data.auth, context.pool())
+ .await?
+ .map(|mut u| {
+ u.password_encrypted = "".to_string();
+ u.private_key = None;
+ u.public_key = None;
+ u
+ });
Ok(GetSiteResponse {
site: site_view,
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<SearchResponse, LemmyError> {
let data: &Search = &self;
dbg!(&data);
- match search_by_apub_id(&data.q, &client, pool).await {
+ match search_by_apub_id(&data.q, context).await {
Ok(r) => return Ok(r),
Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
}
- let user = get_user_from_jwt_opt(&data.auth, pool).await?;
+ let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let user_id = user.map(|u| u.id);
let type_ = SearchType::from_str(&data.type_)?;
let community_id = data.community_id;
match type_ {
SearchType::Posts => {
- posts = blocking(pool, move |conn| {
+ posts = blocking(context.pool(), move |conn| {
PostQueryBuilder::create(conn)
.sort(&sort)
.show_nsfw(true)
.await??;
}
SearchType::Comments => {
- comments = blocking(pool, move |conn| {
+ comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(&conn)
.sort(&sort)
.search_term(q)
.await??;
}
SearchType::Communities => {
- communities = blocking(pool, move |conn| {
+ communities = blocking(context.pool(), move |conn| {
CommunityQueryBuilder::create(conn)
.sort(&sort)
.search_term(q)
.await??;
}
SearchType::Users => {
- users = blocking(pool, move |conn| {
+ users = blocking(context.pool(), move |conn| {
UserQueryBuilder::create(conn)
.sort(&sort)
.search_term(q)
.await??;
}
SearchType::All => {
- posts = blocking(pool, move |conn| {
+ posts = blocking(context.pool(), move |conn| {
PostQueryBuilder::create(conn)
.sort(&sort)
.show_nsfw(true)
let q = data.q.to_owned();
let sort = SortType::from_str(&data.sort)?;
- comments = blocking(pool, move |conn| {
+ comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn)
.sort(&sort)
.search_term(q)
let q = data.q.to_owned();
let sort = SortType::from_str(&data.sort)?;
- communities = blocking(pool, move |conn| {
+ communities = blocking(context.pool(), move |conn| {
CommunityQueryBuilder::create(conn)
.sort(&sort)
.search_term(q)
let q = data.q.to_owned();
let sort = SortType::from_str(&data.sort)?;
- users = blocking(pool, move |conn| {
+ users = blocking(context.pool(), move |conn| {
UserQueryBuilder::create(conn)
.sort(&sort)
.search_term(q)
.await??;
}
SearchType::Url => {
- posts = blocking(pool, move |conn| {
+ posts = blocking(context.pool(), move |conn| {
PostQueryBuilder::create(conn)
.sort(&sort)
.show_nsfw(true)
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetSiteResponse, LemmyError> {
let data: &TransferSite = &self;
- let mut user = get_user_from_jwt(&data.auth, pool).await?;
+ let mut user = get_user_from_jwt(&data.auth, context.pool()).await?;
// TODO add a User_::read_safe() for this.
user.password_encrypted = "".to_string();
user.private_key = None;
user.public_key = None;
- let read_site = blocking(pool, move |conn| Site::read(conn, 1)).await??;
+ let read_site = blocking(context.pool(), move |conn| Site::read(conn, 1)).await??;
// Make sure user is the creator
if read_site.creator_id != user.id {
let new_creator_id = data.user_id;
let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
- if blocking(pool, transfer_site).await?.is_err() {
+ if blocking(context.pool(), transfer_site).await?.is_err() {
return Err(APIError::err("couldnt_update_site").into());
};
removed: Some(false),
};
- blocking(pool, move |conn| ModAdd::create(conn, &form)).await??;
+ blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
- let site_view = blocking(pool, move |conn| SiteView::read(conn)).await??;
+ let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
- let mut admins = blocking(pool, move |conn| UserView::admins(conn)).await??;
+ let mut admins = blocking(context.pool(), move |conn| UserView::admins(conn)).await??;
let creator_index = admins
.iter()
.position(|r| r.id == site_view.creator_id)
let creator_user = admins.remove(creator_index);
admins.insert(0, creator_user);
- let banned = blocking(pool, move |conn| UserView::banned(conn)).await??;
+ let banned = blocking(context.pool(), move |conn| UserView::banned(conn)).await??;
Ok(GetSiteResponse {
site: Some(site_view),
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetSiteConfigResponse, LemmyError> {
let data: &GetSiteConfig = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Only let admins read this
- is_admin(pool, user.id).await?;
+ is_admin(context.pool(), user.id).await?;
let config_hjson = Settings::read_config_file()?;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetSiteConfigResponse, LemmyError> {
let data: &SaveSiteConfig = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Only let admins read this
- let admins = blocking(pool, move |conn| UserView::admins(conn)).await??;
+ let admins = blocking(context.pool(), move |conn| UserView::admins(conn)).await??;
let admin_ids: Vec<i32> = admins.into_iter().map(|m| m.id).collect();
if !admin_ids.contains(&user.id) {
UserOperation,
WebsocketInfo,
},
- DbPool,
+ LemmyContext,
LemmyError,
};
-use actix_web::client::Client;
+use actix_web::web::Data;
use anyhow::Context;
use bcrypt::verify;
use captcha::{gen, Difficulty};
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<LoginResponse, LemmyError> {
let data: &Login = &self;
// Fetch that username / email
let username_or_email = data.username_or_email.clone();
- let user = match blocking(pool, move |conn| {
+ let user = match blocking(context.pool(), move |conn| {
User_::find_by_email_or_username(conn, &username_or_email)
})
.await?
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<LoginResponse, LemmyError> {
let data: &Register = &self;
// Make sure site has open registration
- if let Ok(site) = blocking(pool, move |conn| SiteView::read(conn)).await? {
+ if let Ok(site) = blocking(context.pool(), move |conn| SiteView::read(conn)).await? {
let site: SiteView = site;
if !site.open_registration {
return Err(APIError::err("registration_closed").into());
check_slurs(&data.username)?;
// Make sure there are no admins
- let any_admins = blocking(pool, move |conn| {
+ let any_admins = blocking(context.pool(), move |conn| {
UserView::admins(conn).map(|a| a.is_empty())
})
.await??;
};
// Create the user
- let inserted_user = match blocking(pool, move |conn| User_::register(conn, &user_form)).await? {
+ let inserted_user = match blocking(context.pool(), move |conn| {
+ User_::register(conn, &user_form)
+ })
+ .await?
+ {
Ok(user) => user,
Err(e) => {
let err_type = if e.to_string()
let main_community_keypair = generate_actor_keypair()?;
// Create the main community if it doesn't exist
- let main_community = match blocking(pool, move |conn| Community::read(conn, 2)).await? {
+ let main_community = match blocking(context.pool(), move |conn| Community::read(conn, 2))
+ .await?
+ {
Ok(c) => c,
Err(_e) => {
let default_community_name = "main";
icon: None,
banner: None,
};
- blocking(pool, move |conn| Community::create(conn, &community_form)).await??
+ blocking(context.pool(), move |conn| {
+ Community::create(conn, &community_form)
+ })
+ .await??
}
};
};
let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
- if blocking(pool, follow).await?.is_err() {
+ if blocking(context.pool(), follow).await?.is_err() {
return Err(APIError::err("community_follower_already_exists").into());
};
};
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
- if blocking(pool, join).await?.is_err() {
+ if blocking(context.pool(), join).await?.is_err() {
return Err(APIError::err("community_moderator_already_exists").into());
}
}
async fn perform(
&self,
- _pool: &DbPool,
+ _context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<Self::Response, LemmyError> {
let captcha_settings = Settings::get().captcha;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<LoginResponse, LemmyError> {
let data: &SaveUserSettings = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let user_id = user.id;
- let read_user = blocking(pool, move |conn| User_::read(conn, user_id)).await??;
+ let read_user = blocking(context.pool(), move |conn| User_::read(conn, user_id)).await??;
let bio = match &data.bio {
Some(bio) => {
return Err(APIError::err("password_incorrect").into());
}
let new_password = new_password.to_owned();
- let user = blocking(pool, move |conn| {
+ let user = blocking(context.pool(), move |conn| {
User_::update_password(conn, user_id, &new_password)
})
.await??;
last_refreshed_at: None,
};
- let res = blocking(pool, move |conn| User_::update(conn, user_id, &user_form)).await?;
+ let res = blocking(context.pool(), move |conn| {
+ User_::update(conn, user_id, &user_form)
+ })
+ .await?;
let updated_user: User_ = match res {
Ok(user) => user,
Err(e) => {
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetUserDetailsResponse, LemmyError> {
let data: &GetUserDetails = &self;
- let user = get_user_from_jwt_opt(&data.auth, pool).await?;
+ let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let show_nsfw = match &user {
Some(user) => user.show_nsfw,
let user_details_id = match data.user_id {
Some(id) => id,
None => {
- let user = blocking(pool, move |conn| User_::read_from_name(conn, &username)).await?;
+ let user = blocking(context.pool(), move |conn| {
+ User_::read_from_name(conn, &username)
+ })
+ .await?;
match user {
Ok(user) => user.id,
Err(_e) => return Err(APIError::err("couldnt_find_that_username_or_email").into()),
}
};
- let user_view = blocking(pool, move |conn| {
+ let user_view = blocking(context.pool(), move |conn| {
UserView::get_user_secure(conn, user_details_id)
})
.await??;
let saved_only = data.saved_only;
let community_id = data.community_id;
let user_id = user.map(|u| u.id);
- let (posts, comments) = blocking(pool, move |conn| {
+ let (posts, comments) = blocking(context.pool(), move |conn| {
let mut posts_query = PostQueryBuilder::create(conn)
.sort(&sort)
.show_nsfw(show_nsfw)
})
.await??;
- let follows = blocking(pool, move |conn| {
+ let follows = blocking(context.pool(), move |conn| {
CommunityFollowerView::for_user(conn, user_details_id)
})
.await??;
- let moderates = blocking(pool, move |conn| {
+ let moderates = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_user(conn, user_details_id)
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<AddAdminResponse, LemmyError> {
let data: &AddAdmin = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Make sure user is an admin
- is_admin(pool, user.id).await?;
+ is_admin(context.pool(), user.id).await?;
let added = data.added;
let added_user_id = data.user_id;
let add_admin = move |conn: &'_ _| User_::add_admin(conn, added_user_id, added);
- if blocking(pool, add_admin).await?.is_err() {
+ if blocking(context.pool(), add_admin).await?.is_err() {
return Err(APIError::err("couldnt_update_user").into());
}
removed: Some(!data.added),
};
- blocking(pool, move |conn| ModAdd::create(conn, &form)).await??;
+ blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
- let site_creator_id =
- blocking(pool, move |conn| Site::read(conn, 1).map(|s| s.creator_id)).await??;
+ let site_creator_id = blocking(context.pool(), move |conn| {
+ Site::read(conn, 1).map(|s| s.creator_id)
+ })
+ .await??;
- let mut admins = blocking(pool, move |conn| UserView::admins(conn)).await??;
+ let mut admins = blocking(context.pool(), move |conn| UserView::admins(conn)).await??;
let creator_index = admins
.iter()
.position(|r| r.id == site_creator_id)
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<BanUserResponse, LemmyError> {
let data: &BanUser = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Make sure user is an admin
- is_admin(pool, user.id).await?;
+ is_admin(context.pool(), user.id).await?;
let ban = data.ban;
let banned_user_id = data.user_id;
let ban_user = move |conn: &'_ _| User_::ban_user(conn, banned_user_id, ban);
- if blocking(pool, ban_user).await?.is_err() {
+ if blocking(context.pool(), ban_user).await?.is_err() {
return Err(APIError::err("couldnt_update_user").into());
}
// Remove their data if that's desired
if let Some(remove_data) = data.remove_data {
// Posts
- blocking(pool, move |conn: &'_ _| {
+ blocking(context.pool(), move |conn: &'_ _| {
Post::update_removed_for_creator(conn, banned_user_id, None, remove_data)
})
.await??;
// Communities
- blocking(pool, move |conn: &'_ _| {
+ blocking(context.pool(), move |conn: &'_ _| {
Community::update_removed_for_creator(conn, banned_user_id, remove_data)
})
.await??;
// Comments
- blocking(pool, move |conn: &'_ _| {
+ blocking(context.pool(), move |conn: &'_ _| {
Comment::update_removed_for_creator(conn, banned_user_id, remove_data)
})
.await??;
expires,
};
- blocking(pool, move |conn| ModBan::create(conn, &form)).await??;
+ blocking(context.pool(), move |conn| ModBan::create(conn, &form)).await??;
let user_id = data.user_id;
- let user_view = blocking(pool, move |conn| UserView::get_user_secure(conn, user_id)).await??;
+ let user_view = blocking(context.pool(), move |conn| {
+ UserView::get_user_secure(conn, user_id)
+ })
+ .await??;
let res = BanUserResponse {
user: user_view,
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetRepliesResponse, LemmyError> {
let data: &GetReplies = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let sort = SortType::from_str(&data.sort)?;
let limit = data.limit;
let unread_only = data.unread_only;
let user_id = user.id;
- let replies = blocking(pool, move |conn| {
+ let replies = blocking(context.pool(), move |conn| {
ReplyQueryBuilder::create(conn, user_id)
.sort(&sort)
.unread_only(unread_only)
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetUserMentionsResponse, LemmyError> {
let data: &GetUserMentions = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let sort = SortType::from_str(&data.sort)?;
let limit = data.limit;
let unread_only = data.unread_only;
let user_id = user.id;
- let mentions = blocking(pool, move |conn| {
+ let mentions = blocking(context.pool(), move |conn| {
UserMentionQueryBuilder::create(conn, user_id)
.sort(&sort)
.unread_only(unread_only)
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<UserMentionResponse, LemmyError> {
let data: &MarkUserMentionAsRead = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let user_mention_id = data.user_mention_id;
- let read_user_mention =
- blocking(pool, move |conn| UserMention::read(conn, user_mention_id)).await??;
+ let read_user_mention = blocking(context.pool(), move |conn| {
+ UserMention::read(conn, user_mention_id)
+ })
+ .await??;
if user.id != read_user_mention.recipient_id {
return Err(APIError::err("couldnt_update_comment").into());
let user_mention_id = read_user_mention.id;
let read = data.read;
let update_mention = move |conn: &'_ _| UserMention::update_read(conn, user_mention_id, read);
- if blocking(pool, update_mention).await?.is_err() {
+ if blocking(context.pool(), update_mention).await?.is_err() {
return Err(APIError::err("couldnt_update_comment").into());
};
let user_mention_id = read_user_mention.id;
let user_id = user.id;
- let user_mention_view = blocking(pool, move |conn| {
+ let user_mention_view = blocking(context.pool(), move |conn| {
UserMentionView::read(conn, user_mention_id, user_id)
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<GetRepliesResponse, LemmyError> {
let data: &MarkAllAsRead = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let user_id = user.id;
- let replies = blocking(pool, move |conn| {
+ let replies = blocking(context.pool(), move |conn| {
ReplyQueryBuilder::create(conn, user_id)
.unread_only(true)
.page(1)
for reply in &replies {
let reply_id = reply.id;
let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
- if blocking(pool, mark_as_read).await?.is_err() {
+ if blocking(context.pool(), mark_as_read).await?.is_err() {
return Err(APIError::err("couldnt_update_comment").into());
}
}
// Mark all user mentions as read
let update_user_mentions = move |conn: &'_ _| UserMention::mark_all_as_read(conn, user_id);
- if blocking(pool, update_user_mentions).await?.is_err() {
+ if blocking(context.pool(), update_user_mentions)
+ .await?
+ .is_err()
+ {
return Err(APIError::err("couldnt_update_comment").into());
}
// Mark all private_messages as read
let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, user_id);
- if blocking(pool, update_pm).await?.is_err() {
+ if blocking(context.pool(), update_pm).await?.is_err() {
return Err(APIError::err("couldnt_update_private_message").into());
}
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<LoginResponse, LemmyError> {
let data: &DeleteAccount = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Verify the password
let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false);
// Comments
let user_id = user.id;
let permadelete = move |conn: &'_ _| Comment::permadelete_for_creator(conn, user_id);
- if blocking(pool, permadelete).await?.is_err() {
+ if blocking(context.pool(), permadelete).await?.is_err() {
return Err(APIError::err("couldnt_update_comment").into());
}
// Posts
let permadelete = move |conn: &'_ _| Post::permadelete_for_creator(conn, user_id);
- if blocking(pool, permadelete).await?.is_err() {
+ if blocking(context.pool(), permadelete).await?.is_err() {
return Err(APIError::err("couldnt_update_post").into());
}
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<PasswordResetResponse, LemmyError> {
let data: &PasswordReset = &self;
// Fetch that email
let email = data.email.clone();
- let user = match blocking(pool, move |conn| User_::find_by_email(conn, &email)).await? {
+ let user = match blocking(context.pool(), move |conn| {
+ User_::find_by_email(conn, &email)
+ })
+ .await?
+ {
Ok(user) => user,
Err(_e) => return Err(APIError::err("couldnt_find_that_username_or_email").into()),
};
// Insert the row
let token2 = token.clone();
let user_id = user.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
PasswordResetRequest::create_token(conn, user_id, &token2)
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<LoginResponse, LemmyError> {
let data: &PasswordChange = &self;
// Fetch the user_id from the token
let token = data.token.clone();
- let user_id = blocking(pool, move |conn| {
+ let user_id = blocking(context.pool(), move |conn| {
PasswordResetRequest::read_from_token(conn, &token).map(|p| p.user_id)
})
.await??;
// Update the user with the new password
let password = data.password.clone();
- let updated_user = match blocking(pool, move |conn| {
+ let updated_user = match blocking(context.pool(), move |conn| {
User_::update_password(conn, user_id, &password)
})
.await?
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<PrivateMessageResponse, LemmyError> {
let data: &CreatePrivateMessage = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let hostname = &format!("https://{}", Settings::get().hostname);
published: None,
};
- let inserted_private_message = match blocking(pool, move |conn| {
+ let inserted_private_message = match blocking(context.pool(), move |conn| {
PrivateMessage::create(conn, &private_message_form)
})
.await?
};
let inserted_private_message_id = inserted_private_message.id;
- let updated_private_message = match blocking(pool, move |conn| {
+ let updated_private_message = match blocking(context.pool(), move |conn| {
let apub_id = make_apub_endpoint(
EndpointType::PrivateMessage,
&inserted_private_message_id.to_string(),
Err(_e) => return Err(APIError::err("couldnt_create_private_message").into()),
};
- updated_private_message
- .send_create(&user, &client, pool)
- .await?;
+ updated_private_message.send_create(&user, context).await?;
// Send notifications to the recipient
let recipient_id = data.recipient_id;
- let recipient_user = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;
+ let recipient_user =
+ blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
if recipient_user.send_notifications_to_email {
if let Some(email) = recipient_user.email {
let subject = &format!(
}
}
- let message = blocking(pool, move |conn| {
+ let message = blocking(context.pool(), move |conn| {
PrivateMessageView::read(conn, inserted_private_message.id)
})
.await??;
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<PrivateMessageResponse, LemmyError> {
let data: &EditPrivateMessage = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Checking permissions
let edit_id = data.edit_id;
- let orig_private_message =
- blocking(pool, move |conn| PrivateMessage::read(conn, edit_id)).await??;
+ let orig_private_message = blocking(context.pool(), move |conn| {
+ PrivateMessage::read(conn, edit_id)
+ })
+ .await??;
if user.id != orig_private_message.creator_id {
return Err(APIError::err("no_private_message_edit_allowed").into());
}
// Doing the update
let content_slurs_removed = remove_slurs(&data.content);
let edit_id = data.edit_id;
- let updated_private_message = match blocking(pool, move |conn| {
+ let updated_private_message = match blocking(context.pool(), move |conn| {
PrivateMessage::update_content(conn, edit_id, &content_slurs_removed)
})
.await?
};
// Send the apub update
- updated_private_message
- .send_update(&user, &client, pool)
- .await?;
+ updated_private_message.send_update(&user, context).await?;
let edit_id = data.edit_id;
- let message = blocking(pool, move |conn| PrivateMessageView::read(conn, edit_id)).await??;
+ let message = blocking(context.pool(), move |conn| {
+ PrivateMessageView::read(conn, edit_id)
+ })
+ .await??;
let recipient_id = message.recipient_id;
let res = PrivateMessageResponse { message };
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- client: Client,
) -> Result<PrivateMessageResponse, LemmyError> {
let data: &DeletePrivateMessage = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Checking permissions
let edit_id = data.edit_id;
- let orig_private_message =
- blocking(pool, move |conn| PrivateMessage::read(conn, edit_id)).await??;
+ let orig_private_message = blocking(context.pool(), move |conn| {
+ PrivateMessage::read(conn, edit_id)
+ })
+ .await??;
if user.id != orig_private_message.creator_id {
return Err(APIError::err("no_private_message_edit_allowed").into());
}
// Doing the update
let edit_id = data.edit_id;
let deleted = data.deleted;
- let updated_private_message = match blocking(pool, move |conn| {
+ let updated_private_message = match blocking(context.pool(), move |conn| {
PrivateMessage::update_deleted(conn, edit_id, deleted)
})
.await?
// Send the apub update
if data.deleted {
- updated_private_message
- .send_delete(&user, &client, pool)
- .await?;
+ updated_private_message.send_delete(&user, context).await?;
} else {
updated_private_message
- .send_undo_delete(&user, &client, pool)
+ .send_undo_delete(&user, context)
.await?;
}
let edit_id = data.edit_id;
- let message = blocking(pool, move |conn| PrivateMessageView::read(conn, edit_id)).await??;
+ let message = blocking(context.pool(), move |conn| {
+ PrivateMessageView::read(conn, edit_id)
+ })
+ .await??;
let recipient_id = message.recipient_id;
let res = PrivateMessageResponse { message };
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<PrivateMessageResponse, LemmyError> {
let data: &MarkPrivateMessageAsRead = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Checking permissions
let edit_id = data.edit_id;
- let orig_private_message =
- blocking(pool, move |conn| PrivateMessage::read(conn, edit_id)).await??;
+ let orig_private_message = blocking(context.pool(), move |conn| {
+ PrivateMessage::read(conn, edit_id)
+ })
+ .await??;
if user.id != orig_private_message.recipient_id {
return Err(APIError::err("couldnt_update_private_message").into());
}
// Doing the update
let edit_id = data.edit_id;
let read = data.read;
- match blocking(pool, move |conn| {
+ match blocking(context.pool(), move |conn| {
PrivateMessage::update_read(conn, edit_id, read)
})
.await?
// No need to send an apub update
let edit_id = data.edit_id;
- let message = blocking(pool, move |conn| PrivateMessageView::read(conn, edit_id)).await??;
+ let message = blocking(context.pool(), move |conn| {
+ PrivateMessageView::read(conn, edit_id)
+ })
+ .await??;
let recipient_id = message.recipient_id;
let res = PrivateMessageResponse { message };
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
_websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<PrivateMessagesResponse, LemmyError> {
let data: &GetPrivateMessages = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let user_id = user.id;
let page = data.page;
let limit = data.limit;
let unread_only = data.unread_only;
- let messages = blocking(pool, move |conn| {
+ let messages = blocking(context.pool(), move |conn| {
PrivateMessageQueryBuilder::create(&conn, user_id)
.page(page)
.limit(limit)
async fn perform(
&self,
- pool: &DbPool,
+ context: &Data<LemmyContext>,
websocket_info: Option<WebsocketInfo>,
- _client: Client,
) -> Result<UserJoinResponse, LemmyError> {
let data: &UserJoin = &self;
- let user = get_user_from_jwt(&data.auth, pool).await?;
+ let user = get_user_from_jwt(&data.auth, context.pool()).await?;
if let Some(ws) = websocket_info {
if let Some(id) = ws.id {
ActorType,
},
request::retry_custom,
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::base::AnyBase;
community: &Community,
to: Vec<Url>,
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError> {
- insert_activity(creator.id, activity.clone(), true, pool).await?;
+ insert_activity(creator.id, activity.clone(), true, context.pool()).await?;
// if this is a local community, we need to do an announce from the community instead
if community.local {
- do_announce(activity, &community, creator, client, pool).await?;
+ do_announce(activity, &community, creator, context).await?;
} else {
- send_activity(client, &activity, creator, to).await?;
+ send_activity(context.client(), &activity, creator, to).await?;
}
Ok(())
ToApub,
},
blocking,
- routes::DbPoolParam,
DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{
Update,
},
base::AnyBase,
- context,
link::Mention,
object::{kind::NoteType, Note, Tombstone},
prelude::*,
public,
};
-use actix_web::{body::Body, client::Client, web::Path, HttpResponse};
+use actix_web::{body::Body, web, web::Path, HttpResponse};
use anyhow::Context;
use itertools::Itertools;
use lemmy_db::{
/// Return the post json over HTTP.
pub async fn get_apub_comment(
info: Path<CommentQuery>,
- db: DbPoolParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let id = info.comment_id.parse::<i32>()?;
- let comment = blocking(&db, move |conn| Comment::read(conn, id)).await??;
+ let comment = blocking(context.pool(), move |conn| Comment::read(conn, id)).await??;
if !comment.deleted {
- Ok(create_apub_response(&comment.to_apub(&db).await?))
+ Ok(create_apub_response(
+ &comment.to_apub(context.pool()).await?,
+ ))
} else {
Ok(create_apub_tombstone_response(&comment.to_tombstone()?))
}
comment
// Not needed when the Post is embedded in a collection (like for community outbox)
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(Url::parse(&self.ap_id)?)
.set_published(convert_datetime(self.published))
.set_to(community.actor_id)
/// Parse an ActivityPub note received from another instance into a Lemmy comment
async fn from_apub(
note: &Note,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
expected_domain: Option<Url>,
) -> Result<CommentForm, LemmyError> {
let creator_actor_id = ¬e
.as_single_xsd_any_uri()
.context(location_info!())?;
- let creator = get_or_fetch_and_upsert_user(creator_actor_id, client, pool).await?;
+ let creator = get_or_fetch_and_upsert_user(creator_actor_id, context).await?;
let mut in_reply_tos = note
.in_reply_to()
let post_ap_id = in_reply_tos.next().context(location_info!())??;
// This post, or the parent comment might not yet exist on this server yet, fetch them.
- let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?;
+ let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
// The 2nd item, if it exists, is the parent comment apub_id
// For deeply nested comments, FromApub automatically gets called recursively
Some(parent_comment_uri) => {
let parent_comment_ap_id = &parent_comment_uri?;
let parent_comment =
- get_or_fetch_and_insert_comment(&parent_comment_ap_id, client, pool).await?;
+ get_or_fetch_and_insert_comment(&parent_comment_ap_id, context).await?;
Some(parent_comment.id)
}
#[async_trait::async_trait(?Send)]
impl ApubObjectType for Comment {
/// Send out information about a newly created comment, to the followers of the community.
- async fn send_create(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
- let maa =
- collect_non_local_mentions_and_addresses(&self.content, &community, client, pool).await?;
+ let maa = collect_non_local_mentions_and_addresses(&self.content, &community, context).await?;
let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?);
create
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(CreateType::Create)?)
.set_to(public())
.set_many_ccs(maa.addressed_ccs.to_owned())
&community,
maa.inboxes,
create.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
}
/// Send out information about an edited post, to the followers of the community.
- async fn send_update(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
- let maa =
- collect_non_local_mentions_and_addresses(&self.content, &community, client, pool).await?;
+ let maa = collect_non_local_mentions_and_addresses(&self.content, &community, context).await?;
let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?);
update
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UpdateType::Update)?)
.set_to(public())
.set_many_ccs(maa.addressed_ccs.to_owned())
&community,
maa.inboxes,
update.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
}
- async fn send_delete(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
delete
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
delete.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
async fn send_undo_delete(
&self,
creator: &User_,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
// Generate a fake delete activity, with the correct object
let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
delete
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity
let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
undo
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
undo.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
}
- async fn send_remove(
- &self,
- mod_: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut remove = Remove::new(mod_.actor_id.to_owned(), note.into_any_base()?);
remove
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
remove.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
}
- async fn send_undo_remove(
- &self,
- mod_: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
// Generate a fake delete activity, with the correct object
let mut remove = Remove::new(mod_.actor_id.to_owned(), note.into_any_base()?);
remove
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity
let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?);
undo
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
undo.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
#[async_trait::async_trait(?Send)]
impl ApubLikeableType for Comment {
- async fn send_like(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?);
like
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(LikeType::Like)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
like.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
}
- async fn send_dislike(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut dislike = Dislike::new(creator.actor_id.to_owned(), note.into_any_base()?);
dislike
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
dislike.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
async fn send_undo_like(
&self,
creator: &User_,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?);
like
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity
let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?);
undo
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
undo.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
async fn collect_non_local_mentions_and_addresses(
content: &str,
community: &Community,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<MentionsAndAddresses, LemmyError> {
let mut addressed_ccs = vec![community.get_followers_url()?];
let mut mention_inboxes: Vec<Url> = Vec::new();
for mention in &mentions {
// TODO should it be fetching it every time?
- if let Ok(actor_id) = fetch_webfinger_url(mention, client).await {
+ if let Ok(actor_id) = fetch_webfinger_url(mention, context.client()).await {
debug!("mention actor_id: {}", actor_id);
addressed_ccs.push(actor_id.to_owned().to_string().parse()?);
- let mention_user = get_or_fetch_and_upsert_user(&actor_id, client, pool).await?;
+ let mention_user = get_or_fetch_and_upsert_user(&actor_id, context).await?;
let shared_inbox = mention_user.get_shared_inbox_url()?;
mention_inboxes.push(shared_inbox);
ToApub,
},
blocking,
- routes::DbPoolParam,
DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{
actor::{kind::GroupType, ApActor, Endpoints, Group},
base::{AnyBase, BaseExt},
collection::{OrderedCollection, UnorderedCollection},
- context,
object::{Image, Tombstone},
prelude::*,
public,
};
use activitystreams_ext::Ext2;
-use actix_web::{body::Body, client::Client, web, HttpResponse};
+use actix_web::{body::Body, web, HttpResponse};
use anyhow::Context;
use itertools::Itertools;
use lemmy_db::{
let mut group = Group::new();
group
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(Url::parse(&self.actor_id)?)
.set_name(self.name.to_owned())
.set_published(convert_datetime(self.published))
async fn send_accept_follow(
&self,
follow: Follow,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError> {
let actor_uri = follow
.actor()?
.as_single_xsd_any_uri()
.context(location_info!())?;
- let actor = get_or_fetch_and_upsert_actor(actor_uri, client, pool).await?;
+ let actor = get_or_fetch_and_upsert_actor(actor_uri, context).await?;
let mut accept = Accept::new(self.actor_id.to_owned(), follow.into_any_base()?);
let to = actor.get_inbox_url()?;
accept
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(AcceptType::Accept)?)
.set_to(to.clone());
- insert_activity(self.creator_id, accept.clone(), true, pool).await?;
+ insert_activity(self.creator_id, accept.clone(), true, context.pool()).await?;
- send_activity(client, &accept.into_any_base()?, self, vec![to]).await?;
+ send_activity(context.client(), &accept.into_any_base()?, self, vec![to]).await?;
Ok(())
}
- async fn send_delete(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let group = self.to_apub(pool).await?;
+ async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let group = self.to_apub(context.pool()).await?;
let mut delete = Delete::new(creator.actor_id.to_owned(), group.into_any_base()?);
delete
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![self.get_followers_url()?]);
- insert_activity(self.creator_id, delete.clone(), true, pool).await?;
+ insert_activity(self.creator_id, delete.clone(), true, context.pool()).await?;
- let inboxes = self.get_follower_inboxes(pool).await?;
+ let inboxes = self.get_follower_inboxes(context.pool()).await?;
// Note: For an accept, since it was automatic, no one pushed a button,
// the community was the actor.
// But for delete, the creator is the actor, and does the signing
- send_activity(client, &delete.into_any_base()?, creator, inboxes).await?;
+ send_activity(context.client(), &delete.into_any_base()?, creator, inboxes).await?;
Ok(())
}
async fn send_undo_delete(
&self,
creator: &User_,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError> {
- let group = self.to_apub(pool).await?;
+ let group = self.to_apub(context.pool()).await?;
let mut delete = Delete::new(creator.actor_id.to_owned(), group.into_any_base()?);
delete
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![self.get_followers_url()?]);
let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
undo
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![self.get_followers_url()?]);
- insert_activity(self.creator_id, undo.clone(), true, pool).await?;
+ insert_activity(self.creator_id, undo.clone(), true, context.pool()).await?;
- let inboxes = self.get_follower_inboxes(pool).await?;
+ let inboxes = self.get_follower_inboxes(context.pool()).await?;
// Note: For an accept, since it was automatic, no one pushed a button,
// the community was the actor.
// But for delete, the creator is the actor, and does the signing
- send_activity(client, &undo.into_any_base()?, creator, inboxes).await?;
+ send_activity(context.client(), &undo.into_any_base()?, creator, inboxes).await?;
Ok(())
}
- async fn send_remove(
- &self,
- mod_: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let group = self.to_apub(pool).await?;
+ async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let group = self.to_apub(context.pool()).await?;
let mut remove = Remove::new(mod_.actor_id.to_owned(), group.into_any_base()?);
remove
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![self.get_followers_url()?]);
- insert_activity(mod_.id, remove.clone(), true, pool).await?;
+ insert_activity(mod_.id, remove.clone(), true, context.pool()).await?;
- let inboxes = self.get_follower_inboxes(pool).await?;
+ let inboxes = self.get_follower_inboxes(context.pool()).await?;
// Note: For an accept, since it was automatic, no one pushed a button,
// the community was the actor.
// But for delete, the creator is the actor, and does the signing
- send_activity(client, &remove.into_any_base()?, mod_, inboxes).await?;
+ send_activity(context.client(), &remove.into_any_base()?, mod_, inboxes).await?;
Ok(())
}
- async fn send_undo_remove(
- &self,
- mod_: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let group = self.to_apub(pool).await?;
+ async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let group = self.to_apub(context.pool()).await?;
let mut remove = Remove::new(mod_.actor_id.to_owned(), group.into_any_base()?);
remove
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![self.get_followers_url()?]);
// Undo that fake activity
let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?);
undo
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(LikeType::Like)?)
.set_to(public())
.set_many_ccs(vec![self.get_followers_url()?]);
- insert_activity(mod_.id, undo.clone(), true, pool).await?;
+ insert_activity(mod_.id, undo.clone(), true, context.pool()).await?;
- let inboxes = self.get_follower_inboxes(pool).await?;
+ let inboxes = self.get_follower_inboxes(context.pool()).await?;
// Note: For an accept, since it was automatic, no one pushed a button,
// the community was the actor.
// But for remove , the creator is the actor, and does the signing
- send_activity(client, &undo.into_any_base()?, mod_, inboxes).await?;
+ send_activity(context.client(), &undo.into_any_base()?, mod_, inboxes).await?;
Ok(())
}
async fn send_follow(
&self,
_follow_actor_id: &Url,
- _client: &Client,
- _pool: &DbPool,
+ _context: &LemmyContext,
) -> Result<(), LemmyError> {
unimplemented!()
}
async fn send_unfollow(
&self,
_follow_actor_id: &Url,
- _client: &Client,
- _pool: &DbPool,
+ _context: &LemmyContext,
) -> Result<(), LemmyError> {
unimplemented!()
}
/// Parse an ActivityPub group received from another instance into a Lemmy community.
async fn from_apub(
group: &GroupExt,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
expected_domain: Option<Url>,
) -> Result<Self, LemmyError> {
let creator_and_moderator_uris = group.inner.attributed_to().context(location_info!())?;
.as_xsd_any_uri()
.context(location_info!())?;
- let creator = get_or_fetch_and_upsert_user(creator_uri, client, pool).await?;
+ let creator = get_or_fetch_and_upsert_user(creator_uri, context).await?;
let name = group
.inner
.name()
/// Return the community json over HTTP.
pub async fn get_apub_community_http(
info: web::Path<CommunityQuery>,
- db: DbPoolParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
- let community = blocking(&db, move |conn| {
+ let community = blocking(context.pool(), move |conn| {
Community::read_from_name(conn, &info.community_name)
})
.await??;
if !community.deleted {
- let apub = community.to_apub(&db).await?;
+ let apub = community.to_apub(context.pool()).await?;
Ok(create_apub_response(&apub))
} else {
/// Returns an empty followers collection, only populating the size (for privacy).
pub async fn get_apub_community_followers(
info: web::Path<CommunityQuery>,
- db: DbPoolParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
- let community = blocking(&db, move |conn| {
+ let community = blocking(context.pool(), move |conn| {
Community::read_from_name(&conn, &info.community_name)
})
.await??;
let community_id = community.id;
- let community_followers = blocking(&db, move |conn| {
+ let community_followers = blocking(context.pool(), move |conn| {
CommunityFollowerView::for_community(&conn, community_id)
})
.await??;
let mut collection = UnorderedCollection::new();
collection
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(community.get_followers_url()?)
.set_total_items(community_followers.len() as u64);
Ok(create_apub_response(&collection))
pub async fn get_apub_community_outbox(
info: web::Path<CommunityQuery>,
- db: DbPoolParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
- let community = blocking(&db, move |conn| {
+ let community = blocking(context.pool(), move |conn| {
Community::read_from_name(&conn, &info.community_name)
})
.await??;
let community_id = community.id;
- let posts = blocking(&db, move |conn| {
+ let posts = blocking(context.pool(), move |conn| {
Post::list_for_community(conn, community_id)
})
.await??;
let mut pages: Vec<AnyBase> = vec![];
for p in posts {
- pages.push(p.to_apub(&db).await?.into_any_base()?);
+ pages.push(p.to_apub(context.pool()).await?.into_any_base()?);
}
let len = pages.len();
let mut collection = OrderedCollection::new();
collection
.set_many_items(pages)
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(community.get_outbox_url()?)
.set_total_items(len as u64);
Ok(create_apub_response(&collection))
activity: AnyBase,
community: &Community,
sender: &User_,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError> {
let mut announce = Announce::new(community.actor_id.to_owned(), activity);
announce
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(AnnounceType::Announce)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
- insert_activity(community.creator_id, announce.clone(), true, pool).await?;
+ insert_activity(community.creator_id, announce.clone(), true, context.pool()).await?;
- let mut to: Vec<Url> = community.get_follower_inboxes(pool).await?;
+ let mut to: Vec<Url> = community.get_follower_inboxes(context.pool()).await?;
// dont send to the local instance, nor to the instance where the activity originally came from,
// because that would result in a database error (same data inserted twice)
let community_shared_inbox = community.get_shared_inbox_url()?;
to.retain(|x| x != &community_shared_inbox);
- send_activity(client, &announce.into_any_base()?, community, to).await?;
+ send_activity(context.client(), &announce.into_any_base()?, community, to).await?;
Ok(())
}
},
blocking,
request::{retry, RecvError},
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{base::BaseExt, collection::OrderedCollection, object::Note, prelude::*};
/// http://lemmy_alpha:8540/comment/2
pub async fn search_by_apub_id(
query: &str,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<SearchResponse, LemmyError> {
// Parse the shorthand query url
let query_url = if query.contains('@') {
};
let domain = query_url.domain().context("url has no domain")?;
- let response = match fetch_remote_object::<SearchAcceptedObjects>(client, &query_url).await? {
- SearchAcceptedObjects::Person(p) => {
- let user_uri = p.inner.id(domain)?.context("person has no id")?;
-
- let user = get_or_fetch_and_upsert_user(&user_uri, client, pool).await?;
+ let response =
+ match fetch_remote_object::<SearchAcceptedObjects>(context.client(), &query_url).await? {
+ SearchAcceptedObjects::Person(p) => {
+ let user_uri = p.inner.id(domain)?.context("person has no id")?;
- response.users =
- vec![blocking(pool, move |conn| UserView::get_user_secure(conn, user.id)).await??];
+ let user = get_or_fetch_and_upsert_user(&user_uri, context).await?;
- response
- }
- SearchAcceptedObjects::Group(g) => {
- let community_uri = g.inner.id(domain)?.context("group has no id")?;
+ response.users = vec![
+ blocking(context.pool(), move |conn| {
+ UserView::get_user_secure(conn, user.id)
+ })
+ .await??,
+ ];
- let community = get_or_fetch_and_upsert_community(community_uri, client, pool).await?;
+ response
+ }
+ SearchAcceptedObjects::Group(g) => {
+ let community_uri = g.inner.id(domain)?.context("group has no id")?;
- response.communities = vec![
- blocking(pool, move |conn| {
- CommunityView::read(conn, community.id, None)
- })
- .await??,
- ];
+ let community = get_or_fetch_and_upsert_community(community_uri, context).await?;
- response
- }
- SearchAcceptedObjects::Page(p) => {
- let post_form = PostForm::from_apub(&p, client, pool, Some(query_url)).await?;
+ response.communities = vec![
+ blocking(context.pool(), move |conn| {
+ CommunityView::read(conn, community.id, None)
+ })
+ .await??,
+ ];
- let p = blocking(pool, move |conn| Post::upsert(conn, &post_form)).await??;
- response.posts = vec![blocking(pool, move |conn| PostView::read(conn, p.id, None)).await??];
+ response
+ }
+ SearchAcceptedObjects::Page(p) => {
+ let post_form = PostForm::from_apub(&p, context, Some(query_url)).await?;
- response
- }
- SearchAcceptedObjects::Comment(c) => {
- let comment_form = CommentForm::from_apub(&c, client, pool, Some(query_url)).await?;
+ let p = blocking(context.pool(), move |conn| Post::upsert(conn, &post_form)).await??;
+ response.posts =
+ vec![blocking(context.pool(), move |conn| PostView::read(conn, p.id, None)).await??];
- let c = blocking(pool, move |conn| Comment::upsert(conn, &comment_form)).await??;
- response.comments =
- vec![blocking(pool, move |conn| CommentView::read(conn, c.id, None)).await??];
+ response
+ }
+ SearchAcceptedObjects::Comment(c) => {
+ let comment_form = CommentForm::from_apub(&c, context, Some(query_url)).await?;
- response
- }
- };
+ let c = blocking(context.pool(), move |conn| {
+ Comment::upsert(conn, &comment_form)
+ })
+ .await??;
+ response.comments = vec![
+ blocking(context.pool(), move |conn| {
+ CommentView::read(conn, c.id, None)
+ })
+ .await??,
+ ];
+
+ response
+ }
+ };
Ok(response)
}
pub async fn get_or_fetch_and_upsert_actor(
apub_id: &Url,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<Box<dyn ActorType>, LemmyError> {
- let user = get_or_fetch_and_upsert_user(apub_id, client, pool).await;
+ let user = get_or_fetch_and_upsert_user(apub_id, context).await;
let actor: Box<dyn ActorType> = match user {
Ok(u) => Box::new(u),
- Err(_) => Box::new(get_or_fetch_and_upsert_community(apub_id, client, pool).await?),
+ Err(_) => Box::new(get_or_fetch_and_upsert_community(apub_id, context).await?),
};
Ok(actor)
}
/// Check if a remote user exists, create if not found, if its too old update it.Fetch a user, insert/update it in the database and return the user.
pub async fn get_or_fetch_and_upsert_user(
apub_id: &Url,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<User_, LemmyError> {
let apub_id_owned = apub_id.to_owned();
- let user = blocking(pool, move |conn| {
+ let user = blocking(context.pool(), move |conn| {
User_::read_from_actor_id(conn, apub_id_owned.as_ref())
})
.await?;
// If its older than a day, re-fetch it
Ok(u) if !u.local && should_refetch_actor(u.last_refreshed_at) => {
debug!("Fetching and updating from remote user: {}", apub_id);
- let person = fetch_remote_object::<PersonExt>(client, apub_id).await?;
+ let person = fetch_remote_object::<PersonExt>(context.client(), apub_id).await?;
- let mut uf = UserForm::from_apub(&person, client, pool, Some(apub_id.to_owned())).await?;
+ let mut uf = UserForm::from_apub(&person, context, Some(apub_id.to_owned())).await?;
uf.last_refreshed_at = Some(naive_now());
- let user = blocking(pool, move |conn| User_::update(conn, u.id, &uf)).await??;
+ let user = blocking(context.pool(), move |conn| User_::update(conn, u.id, &uf)).await??;
Ok(user)
}
Ok(u) => Ok(u),
Err(NotFound {}) => {
debug!("Fetching and creating remote user: {}", apub_id);
- let person = fetch_remote_object::<PersonExt>(client, apub_id).await?;
+ let person = fetch_remote_object::<PersonExt>(context.client(), apub_id).await?;
- let uf = UserForm::from_apub(&person, client, pool, Some(apub_id.to_owned())).await?;
- let user = blocking(pool, move |conn| User_::create(conn, &uf)).await??;
+ let uf = UserForm::from_apub(&person, context, Some(apub_id.to_owned())).await?;
+ let user = blocking(context.pool(), move |conn| User_::create(conn, &uf)).await??;
Ok(user)
}
/// Check if a remote community exists, create if not found, if its too old update it.Fetch a community, insert/update it in the database and return the community.
pub async fn get_or_fetch_and_upsert_community(
apub_id: &Url,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<Community, LemmyError> {
let apub_id_owned = apub_id.to_owned();
- let community = blocking(pool, move |conn| {
+ let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, apub_id_owned.as_str())
})
.await?;
match community {
Ok(c) if !c.local && should_refetch_actor(c.last_refreshed_at) => {
debug!("Fetching and updating from remote community: {}", apub_id);
- fetch_remote_community(apub_id, client, pool, Some(c.id)).await
+ fetch_remote_community(apub_id, context, Some(c.id)).await
}
Ok(c) => Ok(c),
Err(NotFound {}) => {
debug!("Fetching and creating remote community: {}", apub_id);
- fetch_remote_community(apub_id, client, pool, None).await
+ fetch_remote_community(apub_id, context, None).await
}
Err(e) => Err(e.into()),
}
async fn fetch_remote_community(
apub_id: &Url,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
community_id: Option<i32>,
) -> Result<Community, LemmyError> {
- let group = fetch_remote_object::<GroupExt>(client, apub_id).await?;
+ let group = fetch_remote_object::<GroupExt>(context.client(), apub_id).await?;
- let cf = CommunityForm::from_apub(&group, client, pool, Some(apub_id.to_owned())).await?;
- let community = blocking(pool, move |conn| {
+ let cf = CommunityForm::from_apub(&group, context, Some(apub_id.to_owned())).await?;
+ let community = blocking(context.pool(), move |conn| {
if let Some(cid) = community_id {
Community::update(conn, cid, &cf)
} else {
let mut creator_and_moderators = Vec::new();
for uri in creator_and_moderator_uris {
- let c_or_m = get_or_fetch_and_upsert_user(uri, client, pool).await?;
+ let c_or_m = get_or_fetch_and_upsert_user(uri, context).await?;
creator_and_moderators.push(c_or_m);
}
// TODO: need to make this work to update mods of existing communities
if community_id.is_none() {
let community_id = community.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
for mod_ in creator_and_moderators {
let community_moderator_form = CommunityModeratorForm {
community_id,
// fetch outbox (maybe make this conditional)
let outbox =
- fetch_remote_object::<OrderedCollection>(client, &community.get_outbox_url()?).await?;
+ fetch_remote_object::<OrderedCollection>(context.client(), &community.get_outbox_url()?)
+ .await?;
let outbox_items = outbox.items().context(location_info!())?.clone();
let mut outbox_items = outbox_items.many().context(location_info!())?;
if outbox_items.len() > 20 {
}
for o in outbox_items {
let page = PageExt::from_any_base(o)?.context(location_info!())?;
- let post = PostForm::from_apub(&page, client, pool, None).await?;
+ let post = PostForm::from_apub(&page, context, None).await?;
let post_ap_id = post.ap_id.clone();
// Check whether the post already exists in the local db
- let existing = blocking(pool, move |conn| Post::read_from_apub_id(conn, &post_ap_id)).await?;
+ let existing = blocking(context.pool(), move |conn| {
+ Post::read_from_apub_id(conn, &post_ap_id)
+ })
+ .await?;
match existing {
- Ok(e) => blocking(pool, move |conn| Post::update(conn, e.id, &post)).await??,
- Err(_) => blocking(pool, move |conn| Post::create(conn, &post)).await??,
+ Ok(e) => blocking(context.pool(), move |conn| Post::update(conn, e.id, &post)).await??,
+ Err(_) => blocking(context.pool(), move |conn| Post::create(conn, &post)).await??,
};
// TODO: we need to send a websocket update here
}
pub async fn get_or_fetch_and_insert_post(
post_ap_id: &Url,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<Post, LemmyError> {
let post_ap_id_owned = post_ap_id.to_owned();
- let post = blocking(pool, move |conn| {
+ let post = blocking(context.pool(), move |conn| {
Post::read_from_apub_id(conn, post_ap_id_owned.as_str())
})
.await?;
Ok(p) => Ok(p),
Err(NotFound {}) => {
debug!("Fetching and creating remote post: {}", post_ap_id);
- let post = fetch_remote_object::<PageExt>(client, post_ap_id).await?;
- let post_form = PostForm::from_apub(&post, client, pool, Some(post_ap_id.to_owned())).await?;
+ let post = fetch_remote_object::<PageExt>(context.client(), post_ap_id).await?;
+ let post_form = PostForm::from_apub(&post, context, Some(post_ap_id.to_owned())).await?;
- let post = blocking(pool, move |conn| Post::create(conn, &post_form)).await??;
+ let post = blocking(context.pool(), move |conn| Post::create(conn, &post_form)).await??;
Ok(post)
}
pub async fn get_or_fetch_and_insert_comment(
comment_ap_id: &Url,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<Comment, LemmyError> {
let comment_ap_id_owned = comment_ap_id.to_owned();
- let comment = blocking(pool, move |conn| {
+ let comment = blocking(context.pool(), move |conn| {
Comment::read_from_apub_id(conn, comment_ap_id_owned.as_str())
})
.await?;
"Fetching and creating remote comment and its parents: {}",
comment_ap_id
);
- let comment = fetch_remote_object::<Note>(client, comment_ap_id).await?;
+ let comment = fetch_remote_object::<Note>(context.client(), comment_ap_id).await?;
let comment_form =
- CommentForm::from_apub(&comment, client, pool, Some(comment_ap_id.to_owned())).await?;
+ CommentForm::from_apub(&comment, context, Some(comment_ap_id.to_owned())).await?;
- let comment = blocking(pool, move |conn| Comment::create(conn, &comment_form)).await??;
+ let comment = blocking(context.pool(), move |conn| {
+ Comment::create(conn, &comment_form)
+ })
+ .await??;
Ok(comment)
}
},
shared_inbox::{get_community_id_from_activity, receive_unhandled_activity},
},
- routes::ChatServerParam,
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{
base::{AnyBase, BaseExt},
prelude::ExtendsExt,
};
-use actix_web::{client::Client, HttpResponse};
+use actix_web::HttpResponse;
use anyhow::Context;
use lemmy_utils::location_info;
pub async fn receive_announce(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let announce = Announce::from_any_base(activity)?.context(location_info!())?;
let object = announce.object();
let object2 = object.clone().one().context(location_info!())?;
match kind {
- Some("Create") => receive_create(object2, client, pool, chat_server).await,
- Some("Update") => receive_update(object2, client, pool, chat_server).await,
- Some("Like") => receive_like(object2, client, pool, chat_server).await,
- Some("Dislike") => receive_dislike(object2, client, pool, chat_server).await,
- Some("Delete") => receive_delete(object2, client, pool, chat_server).await,
- Some("Remove") => receive_remove(object2, client, pool, chat_server).await,
- Some("Undo") => receive_undo(object2, client, pool, chat_server).await,
+ Some("Create") => receive_create(object2, context).await,
+ Some("Update") => receive_update(object2, context).await,
+ Some("Like") => receive_like(object2, context).await,
+ Some("Dislike") => receive_dislike(object2, context).await,
+ Some("Delete") => receive_delete(object2, context).await,
+ Some("Remove") => receive_remove(object2, context).await,
+ Some("Undo") => receive_undo(object2, context).await,
_ => receive_unhandled_activity(announce),
}
}
PageExt,
},
blocking,
- routes::ChatServerParam,
websocket::{
server::{SendComment, SendPost},
UserOperation,
},
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{activity::Create, base::AnyBase, object::Note, prelude::*};
-use actix_web::{client::Client, HttpResponse};
+use actix_web::HttpResponse;
use anyhow::Context;
use lemmy_db::{
comment::{Comment, CommentForm},
pub async fn receive_create(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let create = Create::from_any_base(activity)?.context(location_info!())?;
// ensure that create and actor come from the same instance
- let user = get_user_from_activity(&create, client, pool).await?;
+ let user = get_user_from_activity(&create, context).await?;
create.id(user.actor_id()?.domain().context(location_info!())?)?;
match create.object().as_single_kind_str() {
- Some("Page") => receive_create_post(create, client, pool, chat_server).await,
- Some("Note") => receive_create_comment(create, client, pool, chat_server).await,
+ Some("Page") => receive_create_post(create, context).await,
+ Some("Note") => receive_create_comment(create, context).await,
_ => receive_unhandled_activity(create),
}
}
async fn receive_create_post(
create: Create,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(&create, client, pool).await?;
+ let user = get_user_from_activity(&create, context).await?;
let page = PageExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let post = PostForm::from_apub(&page, client, pool, Some(user.actor_id()?)).await?;
+ let post = PostForm::from_apub(&page, context, Some(user.actor_id()?)).await?;
// Using an upsert, since likes (which fetch the post), sometimes come in before the create
// resulting in double posts.
- let inserted_post = blocking(pool, move |conn| Post::upsert(conn, &post)).await??;
+ let inserted_post = blocking(context.pool(), move |conn| Post::upsert(conn, &post)).await??;
// Refetch the view
let inserted_post_id = inserted_post.id;
- let post_view = blocking(pool, move |conn| {
+ let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, inserted_post_id, None)
})
.await??;
let res = PostResponse { post: post_view };
- chat_server.do_send(SendPost {
+ context.chat_server().do_send(SendPost {
op: UserOperation::CreatePost,
post: res,
my_id: None,
});
- announce_if_community_is_local(create, &user, client, pool).await?;
+ announce_if_community_is_local(create, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_create_comment(
create: Create,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(&create, client, pool).await?;
+ let user = get_user_from_activity(&create, context).await?;
let note = Note::from_any_base(create.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let comment = CommentForm::from_apub(¬e, client, pool, Some(user.actor_id()?)).await?;
+ let comment = CommentForm::from_apub(¬e, context, Some(user.actor_id()?)).await?;
- let inserted_comment = blocking(pool, move |conn| Comment::upsert(conn, &comment)).await??;
+ let inserted_comment =
+ blocking(context.pool(), move |conn| Comment::upsert(conn, &comment)).await??;
let post_id = inserted_comment.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
// Note:
// Although mentions could be gotten from the post tags (they are included there), or the ccs,
// Its much easier to scrape them from the comment body, since the API has to do that
// anyway.
let mentions = scrape_text_for_mentions(&inserted_comment.content);
- let recipient_ids =
- send_local_notifs(mentions, inserted_comment.clone(), &user, post, pool, true).await?;
+ let recipient_ids = send_local_notifs(
+ mentions,
+ inserted_comment.clone(),
+ &user,
+ post,
+ context.pool(),
+ true,
+ )
+ .await?;
// Refetch the view
- let comment_view = blocking(pool, move |conn| {
+ let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, inserted_comment.id, None)
})
.await??;
form_id: None,
};
- chat_server.do_send(SendComment {
+ context.chat_server().do_send(SendComment {
op: UserOperation::CreateComment,
comment: res,
my_id: None,
});
- announce_if_community_is_local(create, &user, client, pool).await?;
+ announce_if_community_is_local(create, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
PageExt,
},
blocking,
- routes::ChatServerParam,
websocket::{
server::{SendComment, SendCommunityRoomMessage, SendPost},
UserOperation,
},
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{activity::Delete, base::AnyBase, object::Note, prelude::*};
-use actix_web::{client::Client, HttpResponse};
+use actix_web::HttpResponse;
use anyhow::Context;
use lemmy_db::{
comment::{Comment, CommentForm},
pub async fn receive_delete(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let delete = Delete::from_any_base(activity)?.context(location_info!())?;
match delete.object().as_single_kind_str() {
- Some("Page") => receive_delete_post(delete, client, pool, chat_server).await,
- Some("Note") => receive_delete_comment(delete, client, pool, chat_server).await,
- Some("Group") => receive_delete_community(delete, client, pool, chat_server).await,
+ Some("Page") => receive_delete_post(delete, context).await,
+ Some("Note") => receive_delete_comment(delete, context).await,
+ Some("Group") => receive_delete_community(delete, context).await,
_ => receive_unhandled_activity(delete),
}
}
async fn receive_delete_post(
delete: Delete,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(&delete, client, pool).await?;
+ let user = get_user_from_activity(&delete, context).await?;
let page = PageExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let post_ap_id = PostForm::from_apub(&page, client, pool, Some(user.actor_id()?))
+ let post_ap_id = PostForm::from_apub(&page, context, Some(user.actor_id()?))
.await?
.get_ap_id()?;
- let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?;
+ let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
let post_form = PostForm {
name: post.name.to_owned(),
published: None,
};
let post_id = post.id;
- blocking(pool, move |conn| Post::update(conn, post_id, &post_form)).await??;
+ blocking(context.pool(), move |conn| {
+ Post::update(conn, post_id, &post_form)
+ })
+ .await??;
// Refetch the view
let post_id = post.id;
- let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
+ let post_view = blocking(context.pool(), move |conn| {
+ PostView::read(conn, post_id, None)
+ })
+ .await??;
let res = PostResponse { post: post_view };
- chat_server.do_send(SendPost {
+ context.chat_server().do_send(SendPost {
op: UserOperation::EditPost,
post: res,
my_id: None,
});
- announce_if_community_is_local(delete, &user, client, pool).await?;
+ announce_if_community_is_local(delete, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_delete_comment(
delete: Delete,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(&delete, client, pool).await?;
+ let user = get_user_from_activity(&delete, context).await?;
let note = Note::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let comment_ap_id = CommentForm::from_apub(¬e, client, pool, Some(user.actor_id()?))
+ let comment_ap_id = CommentForm::from_apub(¬e, context, Some(user.actor_id()?))
.await?
.get_ap_id()?;
- let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
+ let comment = get_or_fetch_and_insert_comment(&comment_ap_id, context).await?;
let comment_form = CommentForm {
content: comment.content.to_owned(),
local: comment.local,
};
let comment_id = comment.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
Comment::update(conn, comment_id, &comment_form)
})
.await??;
// Refetch the view
let comment_id = comment.id;
- let comment_view =
- blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
+ let comment_view = blocking(context.pool(), move |conn| {
+ CommentView::read(conn, comment_id, None)
+ })
+ .await??;
// TODO get those recipient actor ids from somewhere
let recipient_ids = vec![];
form_id: None,
};
- chat_server.do_send(SendComment {
+ context.chat_server().do_send(SendComment {
op: UserOperation::EditComment,
comment: res,
my_id: None,
});
- announce_if_community_is_local(delete, &user, client, pool).await?;
+ announce_if_community_is_local(delete, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_delete_community(
delete: Delete,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let group = GroupExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let user = get_user_from_activity(&delete, client, pool).await?;
+ let user = get_user_from_activity(&delete, context).await?;
- let community_actor_id = CommunityForm::from_apub(&group, client, pool, Some(user.actor_id()?))
+ let community_actor_id = CommunityForm::from_apub(&group, context, Some(user.actor_id()?))
.await?
.actor_id;
- let community = blocking(pool, move |conn| {
+ let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, &community_actor_id)
})
.await??;
};
let community_id = community.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
Community::update(conn, community_id, &community_form)
})
.await??;
let community_id = community.id;
let res = CommunityResponse {
- community: blocking(pool, move |conn| {
+ community: blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, None)
})
.await??,
let community_id = res.community.id;
- chat_server.do_send(SendCommunityRoomMessage {
+ context.chat_server().do_send(SendCommunityRoomMessage {
op: UserOperation::EditCommunity,
response: res,
community_id,
my_id: None,
});
- announce_if_community_is_local(delete, &user, client, pool).await?;
+ announce_if_community_is_local(delete, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
PageExt,
},
blocking,
- routes::ChatServerParam,
websocket::{
server::{SendComment, SendPost},
UserOperation,
},
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{activity::Dislike, base::AnyBase, object::Note, prelude::*};
-use actix_web::{client::Client, HttpResponse};
+use actix_web::HttpResponse;
use anyhow::Context;
use lemmy_db::{
comment::{CommentForm, CommentLike, CommentLikeForm},
pub async fn receive_dislike(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let dislike = Dislike::from_any_base(activity)?.context(location_info!())?;
match dislike.object().as_single_kind_str() {
- Some("Page") => receive_dislike_post(dislike, client, pool, chat_server).await,
- Some("Note") => receive_dislike_comment(dislike, client, pool, chat_server).await,
+ Some("Page") => receive_dislike_post(dislike, context).await,
+ Some("Note") => receive_dislike_comment(dislike, context).await,
_ => receive_unhandled_activity(dislike),
}
}
async fn receive_dislike_post(
dislike: Dislike,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(&dislike, client, pool).await?;
+ let user = get_user_from_activity(&dislike, context).await?;
let page = PageExt::from_any_base(
dislike
.object()
)?
.context(location_info!())?;
- let post = PostForm::from_apub(&page, client, pool, None).await?;
+ let post = PostForm::from_apub(&page, context, None).await?;
- let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, client, pool)
+ let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
.await?
.id;
score: -1,
};
let user_id = user.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
PostLike::remove(conn, user_id, post_id)?;
PostLike::like(conn, &like_form)
})
.await??;
// Refetch the view
- let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
+ let post_view = blocking(context.pool(), move |conn| {
+ PostView::read(conn, post_id, None)
+ })
+ .await??;
let res = PostResponse { post: post_view };
- chat_server.do_send(SendPost {
+ context.chat_server().do_send(SendPost {
op: UserOperation::CreatePostLike,
post: res,
my_id: None,
});
- announce_if_community_is_local(dislike, &user, client, pool).await?;
+ announce_if_community_is_local(dislike, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_dislike_comment(
dislike: Dislike,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let note = Note::from_any_base(
dislike
.context(location_info!())?,
)?
.context(location_info!())?;
- let user = get_user_from_activity(&dislike, client, pool).await?;
+ let user = get_user_from_activity(&dislike, context).await?;
- let comment = CommentForm::from_apub(¬e, client, pool, None).await?;
+ let comment = CommentForm::from_apub(¬e, context, None).await?;
- let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, client, pool)
+ let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
.await?
.id;
score: -1,
};
let user_id = user.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
CommentLike::remove(conn, user_id, comment_id)?;
CommentLike::like(conn, &like_form)
})
.await??;
// Refetch the view
- let comment_view =
- blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
+ let comment_view = blocking(context.pool(), move |conn| {
+ CommentView::read(conn, comment_id, None)
+ })
+ .await??;
// TODO get those recipient actor ids from somewhere
let recipient_ids = vec![];
form_id: None,
};
- chat_server.do_send(SendComment {
+ context.chat_server().do_send(SendComment {
op: UserOperation::CreateCommentLike,
comment: res,
my_id: None,
});
- announce_if_community_is_local(dislike, &user, client, pool).await?;
+ announce_if_community_is_local(dislike, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
PageExt,
},
blocking,
- routes::ChatServerParam,
websocket::{
server::{SendComment, SendPost},
UserOperation,
},
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{activity::Like, base::AnyBase, object::Note, prelude::*};
-use actix_web::{client::Client, HttpResponse};
+use actix_web::HttpResponse;
use anyhow::Context;
use lemmy_db::{
comment::{CommentForm, CommentLike, CommentLikeForm},
pub async fn receive_like(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let like = Like::from_any_base(activity)?.context(location_info!())?;
match like.object().as_single_kind_str() {
- Some("Page") => receive_like_post(like, client, pool, chat_server).await,
- Some("Note") => receive_like_comment(like, client, pool, chat_server).await,
+ Some("Page") => receive_like_post(like, context).await,
+ Some("Note") => receive_like_comment(like, context).await,
_ => receive_unhandled_activity(like),
}
}
-async fn receive_like_post(
- like: Like,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
-) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(&like, client, pool).await?;
+async fn receive_like_post(like: Like, context: &LemmyContext) -> Result<HttpResponse, LemmyError> {
+ let user = get_user_from_activity(&like, context).await?;
let page = PageExt::from_any_base(like.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let post = PostForm::from_apub(&page, client, pool, None).await?;
+ let post = PostForm::from_apub(&page, context, None).await?;
- let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, client, pool)
+ let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
.await?
.id;
score: 1,
};
let user_id = user.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
PostLike::remove(conn, user_id, post_id)?;
PostLike::like(conn, &like_form)
})
.await??;
// Refetch the view
- let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
+ let post_view = blocking(context.pool(), move |conn| {
+ PostView::read(conn, post_id, None)
+ })
+ .await??;
let res = PostResponse { post: post_view };
- chat_server.do_send(SendPost {
+ context.chat_server().do_send(SendPost {
op: UserOperation::CreatePostLike,
post: res,
my_id: None,
});
- announce_if_community_is_local(like, &user, client, pool).await?;
+ announce_if_community_is_local(like, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_like_comment(
like: Like,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let note = Note::from_any_base(like.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let user = get_user_from_activity(&like, client, pool).await?;
+ let user = get_user_from_activity(&like, context).await?;
- let comment = CommentForm::from_apub(¬e, client, pool, None).await?;
+ let comment = CommentForm::from_apub(¬e, context, None).await?;
- let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, client, pool)
+ let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
.await?
.id;
score: 1,
};
let user_id = user.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
CommentLike::remove(conn, user_id, comment_id)?;
CommentLike::like(conn, &like_form)
})
.await??;
// Refetch the view
- let comment_view =
- blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
+ let comment_view = blocking(context.pool(), move |conn| {
+ CommentView::read(conn, comment_id, None)
+ })
+ .await??;
// TODO get those recipient actor ids from somewhere
let recipient_ids = vec![];
form_id: None,
};
- chat_server.do_send(SendComment {
+ context.chat_server().do_send(SendComment {
op: UserOperation::CreateCommentLike,
comment: res,
my_id: None,
});
- announce_if_community_is_local(like, &user, client, pool).await?;
+ announce_if_community_is_local(like, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
PageExt,
},
blocking,
- routes::ChatServerParam,
websocket::{
server::{SendComment, SendCommunityRoomMessage, SendPost},
UserOperation,
},
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{activity::Remove, base::AnyBase, object::Note, prelude::*};
-use actix_web::{client::Client, HttpResponse};
+use actix_web::HttpResponse;
use anyhow::{anyhow, Context};
use lemmy_db::{
comment::{Comment, CommentForm},
pub async fn receive_remove(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let remove = Remove::from_any_base(activity)?.context(location_info!())?;
- let actor = get_user_from_activity(&remove, client, pool).await?;
+ let actor = get_user_from_activity(&remove, context).await?;
let community = get_community_id_from_activity(&remove)?;
if actor.actor_id()?.domain() != community.domain() {
return Err(anyhow!("Remove activities are only allowed on local objects").into());
}
match remove.object().as_single_kind_str() {
- Some("Page") => receive_remove_post(remove, client, pool, chat_server).await,
- Some("Note") => receive_remove_comment(remove, client, pool, chat_server).await,
- Some("Group") => receive_remove_community(remove, client, pool, chat_server).await,
+ Some("Page") => receive_remove_post(remove, context).await,
+ Some("Note") => receive_remove_comment(remove, context).await,
+ Some("Group") => receive_remove_community(remove, context).await,
_ => receive_unhandled_activity(remove),
}
}
async fn receive_remove_post(
remove: Remove,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let mod_ = get_user_from_activity(&remove, client, pool).await?;
+ let mod_ = get_user_from_activity(&remove, context).await?;
let page = PageExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let post_ap_id = PostForm::from_apub(&page, client, pool, None)
+ let post_ap_id = PostForm::from_apub(&page, context, None)
.await?
.get_ap_id()?;
- let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?;
+ let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
let post_form = PostForm {
name: post.name.to_owned(),
published: None,
};
let post_id = post.id;
- blocking(pool, move |conn| Post::update(conn, post_id, &post_form)).await??;
+ blocking(context.pool(), move |conn| {
+ Post::update(conn, post_id, &post_form)
+ })
+ .await??;
// Refetch the view
let post_id = post.id;
- let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
+ let post_view = blocking(context.pool(), move |conn| {
+ PostView::read(conn, post_id, None)
+ })
+ .await??;
let res = PostResponse { post: post_view };
- chat_server.do_send(SendPost {
+ context.chat_server().do_send(SendPost {
op: UserOperation::EditPost,
post: res,
my_id: None,
});
- announce_if_community_is_local(remove, &mod_, client, pool).await?;
+ announce_if_community_is_local(remove, &mod_, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_remove_comment(
remove: Remove,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let mod_ = get_user_from_activity(&remove, client, pool).await?;
+ let mod_ = get_user_from_activity(&remove, context).await?;
let note = Note::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let comment_ap_id = CommentForm::from_apub(¬e, client, pool, None)
+ let comment_ap_id = CommentForm::from_apub(¬e, context, None)
.await?
.get_ap_id()?;
- let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
+ let comment = get_or_fetch_and_insert_comment(&comment_ap_id, context).await?;
let comment_form = CommentForm {
content: comment.content.to_owned(),
local: comment.local,
};
let comment_id = comment.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
Comment::update(conn, comment_id, &comment_form)
})
.await??;
// Refetch the view
let comment_id = comment.id;
- let comment_view =
- blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
+ let comment_view = blocking(context.pool(), move |conn| {
+ CommentView::read(conn, comment_id, None)
+ })
+ .await??;
// TODO get those recipient actor ids from somewhere
let recipient_ids = vec![];
form_id: None,
};
- chat_server.do_send(SendComment {
+ context.chat_server().do_send(SendComment {
op: UserOperation::EditComment,
comment: res,
my_id: None,
});
- announce_if_community_is_local(remove, &mod_, client, pool).await?;
+ announce_if_community_is_local(remove, &mod_, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_remove_community(
remove: Remove,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let mod_ = get_user_from_activity(&remove, client, pool).await?;
+ let mod_ = get_user_from_activity(&remove, context).await?;
let group = GroupExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let community_actor_id = CommunityForm::from_apub(&group, client, pool, Some(mod_.actor_id()?))
+ let community_actor_id = CommunityForm::from_apub(&group, context, Some(mod_.actor_id()?))
.await?
.actor_id;
- let community = blocking(pool, move |conn| {
+ let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, &community_actor_id)
})
.await??;
};
let community_id = community.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
Community::update(conn, community_id, &community_form)
})
.await??;
let community_id = community.id;
let res = CommunityResponse {
- community: blocking(pool, move |conn| {
+ community: blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, None)
})
.await??,
let community_id = res.community.id;
- chat_server.do_send(SendCommunityRoomMessage {
+ context.chat_server().do_send(SendCommunityRoomMessage {
op: UserOperation::EditCommunity,
response: res,
community_id,
my_id: None,
});
- announce_if_community_is_local(remove, &mod_, client, pool).await?;
+ announce_if_community_is_local(remove, &mod_, context).await?;
Ok(HttpResponse::Ok().finish())
}
PageExt,
},
blocking,
- routes::ChatServerParam,
websocket::{
server::{SendComment, SendCommunityRoomMessage, SendPost},
UserOperation,
},
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{
object::Note,
prelude::*,
};
-use actix_web::{client::Client, HttpResponse};
+use actix_web::HttpResponse;
use anyhow::{anyhow, Context};
use lemmy_db::{
comment::{Comment, CommentForm, CommentLike},
pub async fn receive_undo(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let undo = Undo::from_any_base(activity)?.context(location_info!())?;
match undo.object().as_single_kind_str() {
- Some("Delete") => receive_undo_delete(undo, client, pool, chat_server).await,
- Some("Remove") => receive_undo_remove(undo, client, pool, chat_server).await,
- Some("Like") => receive_undo_like(undo, client, pool, chat_server).await,
- Some("Dislike") => receive_undo_dislike(undo, client, pool, chat_server).await,
+ Some("Delete") => receive_undo_delete(undo, context).await,
+ Some("Remove") => receive_undo_remove(undo, context).await,
+ Some("Like") => receive_undo_like(undo, context).await,
+ Some("Dislike") => receive_undo_dislike(undo, context).await,
_ => receive_unhandled_activity(undo),
}
}
async fn receive_undo_delete(
undo: Undo,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let delete = Delete::from_any_base(undo.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
.as_single_kind_str()
.context(location_info!())?;
match type_ {
- "Note" => receive_undo_delete_comment(undo, &delete, client, pool, chat_server).await,
- "Page" => receive_undo_delete_post(undo, &delete, client, pool, chat_server).await,
- "Group" => receive_undo_delete_community(undo, &delete, client, pool, chat_server).await,
+ "Note" => receive_undo_delete_comment(undo, &delete, context).await,
+ "Page" => receive_undo_delete_post(undo, &delete, context).await,
+ "Group" => receive_undo_delete_community(undo, &delete, context).await,
d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
}
}
async fn receive_undo_remove(
undo: Undo,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let remove = Remove::from_any_base(undo.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
.as_single_kind_str()
.context(location_info!())?;
match type_ {
- "Note" => receive_undo_remove_comment(undo, &remove, client, pool, chat_server).await,
- "Page" => receive_undo_remove_post(undo, &remove, client, pool, chat_server).await,
- "Group" => receive_undo_remove_community(undo, &remove, client, pool, chat_server).await,
+ "Note" => receive_undo_remove_comment(undo, &remove, context).await,
+ "Page" => receive_undo_remove_post(undo, &remove, context).await,
+ "Group" => receive_undo_remove_community(undo, &remove, context).await,
d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
}
}
-async fn receive_undo_like(
- undo: Undo,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
-) -> Result<HttpResponse, LemmyError> {
+async fn receive_undo_like(undo: Undo, context: &LemmyContext) -> Result<HttpResponse, LemmyError> {
let like = Like::from_any_base(undo.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
check_is_undo_valid(&undo, &like)?;
.as_single_kind_str()
.context(location_info!())?;
match type_ {
- "Note" => receive_undo_like_comment(undo, &like, client, pool, chat_server).await,
- "Page" => receive_undo_like_post(undo, &like, client, pool, chat_server).await,
+ "Note" => receive_undo_like_comment(undo, &like, context).await,
+ "Page" => receive_undo_like_post(undo, &like, context).await,
d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
}
}
async fn receive_undo_dislike(
undo: Undo,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let dislike = Dislike::from_any_base(undo.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
.as_single_kind_str()
.context(location_info!())?;
match type_ {
- "Note" => receive_undo_dislike_comment(undo, &dislike, client, pool, chat_server).await,
- "Page" => receive_undo_dislike_post(undo, &dislike, client, pool, chat_server).await,
+ "Note" => receive_undo_dislike_comment(undo, &dislike, context).await,
+ "Page" => receive_undo_dislike_post(undo, &dislike, context).await,
d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
}
}
async fn receive_undo_delete_comment(
undo: Undo,
delete: &Delete,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(delete, client, pool).await?;
+ let user = get_user_from_activity(delete, context).await?;
let note = Note::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let comment_ap_id = CommentForm::from_apub(¬e, client, pool, Some(user.actor_id()?))
+ let comment_ap_id = CommentForm::from_apub(¬e, context, Some(user.actor_id()?))
.await?
.get_ap_id()?;
- let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
+ let comment = get_or_fetch_and_insert_comment(&comment_ap_id, context).await?;
let comment_form = CommentForm {
content: comment.content.to_owned(),
local: comment.local,
};
let comment_id = comment.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
Comment::update(conn, comment_id, &comment_form)
})
.await??;
// Refetch the view
let comment_id = comment.id;
- let comment_view =
- blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
+ let comment_view = blocking(context.pool(), move |conn| {
+ CommentView::read(conn, comment_id, None)
+ })
+ .await??;
// TODO get those recipient actor ids from somewhere
let recipient_ids = vec![];
form_id: None,
};
- chat_server.do_send(SendComment {
+ context.chat_server().do_send(SendComment {
op: UserOperation::EditComment,
comment: res,
my_id: None,
});
- announce_if_community_is_local(undo, &user, client, pool).await?;
+ announce_if_community_is_local(undo, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_undo_remove_comment(
undo: Undo,
remove: &Remove,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let mod_ = get_user_from_activity(remove, client, pool).await?;
+ let mod_ = get_user_from_activity(remove, context).await?;
let note = Note::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let comment_ap_id = CommentForm::from_apub(¬e, client, pool, None)
+ let comment_ap_id = CommentForm::from_apub(¬e, context, None)
.await?
.get_ap_id()?;
- let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
+ let comment = get_or_fetch_and_insert_comment(&comment_ap_id, context).await?;
let comment_form = CommentForm {
content: comment.content.to_owned(),
local: comment.local,
};
let comment_id = comment.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
Comment::update(conn, comment_id, &comment_form)
})
.await??;
// Refetch the view
let comment_id = comment.id;
- let comment_view =
- blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
+ let comment_view = blocking(context.pool(), move |conn| {
+ CommentView::read(conn, comment_id, None)
+ })
+ .await??;
// TODO get those recipient actor ids from somewhere
let recipient_ids = vec![];
form_id: None,
};
- chat_server.do_send(SendComment {
+ context.chat_server().do_send(SendComment {
op: UserOperation::EditComment,
comment: res,
my_id: None,
});
- announce_if_community_is_local(undo, &mod_, client, pool).await?;
+ announce_if_community_is_local(undo, &mod_, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_undo_delete_post(
undo: Undo,
delete: &Delete,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(delete, client, pool).await?;
+ let user = get_user_from_activity(delete, context).await?;
let page = PageExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let post_ap_id = PostForm::from_apub(&page, client, pool, Some(user.actor_id()?))
+ let post_ap_id = PostForm::from_apub(&page, context, Some(user.actor_id()?))
.await?
.get_ap_id()?;
- let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?;
+ let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
let post_form = PostForm {
name: post.name.to_owned(),
published: None,
};
let post_id = post.id;
- blocking(pool, move |conn| Post::update(conn, post_id, &post_form)).await??;
+ blocking(context.pool(), move |conn| {
+ Post::update(conn, post_id, &post_form)
+ })
+ .await??;
// Refetch the view
let post_id = post.id;
- let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
+ let post_view = blocking(context.pool(), move |conn| {
+ PostView::read(conn, post_id, None)
+ })
+ .await??;
let res = PostResponse { post: post_view };
- chat_server.do_send(SendPost {
+ context.chat_server().do_send(SendPost {
op: UserOperation::EditPost,
post: res,
my_id: None,
});
- announce_if_community_is_local(undo, &user, client, pool).await?;
+ announce_if_community_is_local(undo, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_undo_remove_post(
undo: Undo,
remove: &Remove,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let mod_ = get_user_from_activity(remove, client, pool).await?;
+ let mod_ = get_user_from_activity(remove, context).await?;
let page = PageExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let post_ap_id = PostForm::from_apub(&page, client, pool, None)
+ let post_ap_id = PostForm::from_apub(&page, context, None)
.await?
.get_ap_id()?;
- let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?;
+ let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
let post_form = PostForm {
name: post.name.to_owned(),
published: None,
};
let post_id = post.id;
- blocking(pool, move |conn| Post::update(conn, post_id, &post_form)).await??;
+ blocking(context.pool(), move |conn| {
+ Post::update(conn, post_id, &post_form)
+ })
+ .await??;
// Refetch the view
let post_id = post.id;
- let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
+ let post_view = blocking(context.pool(), move |conn| {
+ PostView::read(conn, post_id, None)
+ })
+ .await??;
let res = PostResponse { post: post_view };
- chat_server.do_send(SendPost {
+ context.chat_server().do_send(SendPost {
op: UserOperation::EditPost,
post: res,
my_id: None,
});
- announce_if_community_is_local(undo, &mod_, client, pool).await?;
+ announce_if_community_is_local(undo, &mod_, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_undo_delete_community(
undo: Undo,
delete: &Delete,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(delete, client, pool).await?;
+ let user = get_user_from_activity(delete, context).await?;
let group = GroupExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let community_actor_id = CommunityForm::from_apub(&group, client, pool, Some(user.actor_id()?))
+ let community_actor_id = CommunityForm::from_apub(&group, context, Some(user.actor_id()?))
.await?
.actor_id;
- let community = blocking(pool, move |conn| {
+ let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, &community_actor_id)
})
.await??;
};
let community_id = community.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
Community::update(conn, community_id, &community_form)
})
.await??;
let community_id = community.id;
let res = CommunityResponse {
- community: blocking(pool, move |conn| {
+ community: blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, None)
})
.await??,
let community_id = res.community.id;
- chat_server.do_send(SendCommunityRoomMessage {
+ context.chat_server().do_send(SendCommunityRoomMessage {
op: UserOperation::EditCommunity,
response: res,
community_id,
my_id: None,
});
- announce_if_community_is_local(undo, &user, client, pool).await?;
+ announce_if_community_is_local(undo, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_undo_remove_community(
undo: Undo,
remove: &Remove,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let mod_ = get_user_from_activity(remove, client, pool).await?;
+ let mod_ = get_user_from_activity(remove, context).await?;
let group = GroupExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let community_actor_id = CommunityForm::from_apub(&group, client, pool, Some(mod_.actor_id()?))
+ let community_actor_id = CommunityForm::from_apub(&group, context, Some(mod_.actor_id()?))
.await?
.actor_id;
- let community = blocking(pool, move |conn| {
+ let community = blocking(context.pool(), move |conn| {
Community::read_from_actor_id(conn, &community_actor_id)
})
.await??;
};
let community_id = community.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
Community::update(conn, community_id, &community_form)
})
.await??;
let community_id = community.id;
let res = CommunityResponse {
- community: blocking(pool, move |conn| {
+ community: blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, None)
})
.await??,
let community_id = res.community.id;
- chat_server.do_send(SendCommunityRoomMessage {
+ context.chat_server().do_send(SendCommunityRoomMessage {
op: UserOperation::EditCommunity,
response: res,
community_id,
my_id: None,
});
- announce_if_community_is_local(undo, &mod_, client, pool).await?;
+ announce_if_community_is_local(undo, &mod_, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_undo_like_comment(
undo: Undo,
like: &Like,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(like, client, pool).await?;
+ let user = get_user_from_activity(like, context).await?;
let note = Note::from_any_base(like.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let comment = CommentForm::from_apub(¬e, client, pool, None).await?;
+ let comment = CommentForm::from_apub(¬e, context, None).await?;
- let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, client, pool)
+ let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
.await?
.id;
let user_id = user.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
CommentLike::remove(conn, user_id, comment_id)
})
.await??;
// Refetch the view
- let comment_view =
- blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
+ let comment_view = blocking(context.pool(), move |conn| {
+ CommentView::read(conn, comment_id, None)
+ })
+ .await??;
// TODO get those recipient actor ids from somewhere
let recipient_ids = vec![];
form_id: None,
};
- chat_server.do_send(SendComment {
+ context.chat_server().do_send(SendComment {
op: UserOperation::CreateCommentLike,
comment: res,
my_id: None,
});
- announce_if_community_is_local(undo, &user, client, pool).await?;
+ announce_if_community_is_local(undo, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_undo_like_post(
undo: Undo,
like: &Like,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(like, client, pool).await?;
+ let user = get_user_from_activity(like, context).await?;
let page = PageExt::from_any_base(like.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let post = PostForm::from_apub(&page, client, pool, None).await?;
+ let post = PostForm::from_apub(&page, context, None).await?;
- let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, client, pool)
+ let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
.await?
.id;
let user_id = user.id;
- blocking(pool, move |conn| PostLike::remove(conn, user_id, post_id)).await??;
+ blocking(context.pool(), move |conn| {
+ PostLike::remove(conn, user_id, post_id)
+ })
+ .await??;
// Refetch the view
- let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
+ let post_view = blocking(context.pool(), move |conn| {
+ PostView::read(conn, post_id, None)
+ })
+ .await??;
let res = PostResponse { post: post_view };
- chat_server.do_send(SendPost {
+ context.chat_server().do_send(SendPost {
op: UserOperation::CreatePostLike,
post: res,
my_id: None,
});
- announce_if_community_is_local(undo, &user, client, pool).await?;
+ announce_if_community_is_local(undo, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_undo_dislike_comment(
undo: Undo,
dislike: &Dislike,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(dislike, client, pool).await?;
+ let user = get_user_from_activity(dislike, context).await?;
let note = Note::from_any_base(
dislike
.object()
)?
.context(location_info!())?;
- let comment = CommentForm::from_apub(¬e, client, pool, None).await?;
+ let comment = CommentForm::from_apub(¬e, context, None).await?;
- let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, client, pool)
+ let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
.await?
.id;
let user_id = user.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
CommentLike::remove(conn, user_id, comment_id)
})
.await??;
// Refetch the view
- let comment_view =
- blocking(pool, move |conn| CommentView::read(conn, comment_id, None)).await??;
+ let comment_view = blocking(context.pool(), move |conn| {
+ CommentView::read(conn, comment_id, None)
+ })
+ .await??;
// TODO get those recipient actor ids from somewhere
let recipient_ids = vec![];
form_id: None,
};
- chat_server.do_send(SendComment {
+ context.chat_server().do_send(SendComment {
op: UserOperation::CreateCommentLike,
comment: res,
my_id: None,
});
- announce_if_community_is_local(undo, &user, client, pool).await?;
+ announce_if_community_is_local(undo, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_undo_dislike_post(
undo: Undo,
dislike: &Dislike,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(dislike, client, pool).await?;
+ let user = get_user_from_activity(dislike, context).await?;
let page = PageExt::from_any_base(
dislike
.object()
)?
.context(location_info!())?;
- let post = PostForm::from_apub(&page, client, pool, None).await?;
+ let post = PostForm::from_apub(&page, context, None).await?;
- let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, client, pool)
+ let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
.await?
.id;
let user_id = user.id;
- blocking(pool, move |conn| PostLike::remove(conn, user_id, post_id)).await??;
+ blocking(context.pool(), move |conn| {
+ PostLike::remove(conn, user_id, post_id)
+ })
+ .await??;
// Refetch the view
- let post_view = blocking(pool, move |conn| PostView::read(conn, post_id, None)).await??;
+ let post_view = blocking(context.pool(), move |conn| {
+ PostView::read(conn, post_id, None)
+ })
+ .await??;
let res = PostResponse { post: post_view };
- chat_server.do_send(SendPost {
+ context.chat_server().do_send(SendPost {
op: UserOperation::CreatePostLike,
post: res,
my_id: None,
});
- announce_if_community_is_local(undo, &user, client, pool).await?;
+ announce_if_community_is_local(undo, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
PageExt,
},
blocking,
- routes::ChatServerParam,
websocket::{
server::{SendComment, SendPost},
UserOperation,
},
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{activity::Update, base::AnyBase, object::Note, prelude::*};
-use actix_web::{client::Client, HttpResponse};
+use actix_web::HttpResponse;
use anyhow::Context;
use lemmy_db::{
comment::{Comment, CommentForm},
pub async fn receive_update(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let update = Update::from_any_base(activity)?.context(location_info!())?;
// ensure that update and actor come from the same instance
- let user = get_user_from_activity(&update, client, pool).await?;
+ let user = get_user_from_activity(&update, context).await?;
update.id(user.actor_id()?.domain().context(location_info!())?)?;
match update.object().as_single_kind_str() {
- Some("Page") => receive_update_post(update, client, pool, chat_server).await,
- Some("Note") => receive_update_comment(update, client, pool, chat_server).await,
+ Some("Page") => receive_update_post(update, context).await,
+ Some("Note") => receive_update_comment(update, context).await,
_ => receive_unhandled_activity(update),
}
}
async fn receive_update_post(
update: Update,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
- let user = get_user_from_activity(&update, client, pool).await?;
+ let user = get_user_from_activity(&update, context).await?;
let page = PageExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let post = PostForm::from_apub(&page, client, pool, Some(user.actor_id()?)).await?;
+ let post = PostForm::from_apub(&page, context, Some(user.actor_id()?)).await?;
- let original_post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, client, pool)
+ let original_post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
.await?
.id;
- blocking(pool, move |conn| {
+ blocking(context.pool(), move |conn| {
Post::update(conn, original_post_id, &post)
})
.await??;
// Refetch the view
- let post_view = blocking(pool, move |conn| {
+ let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, original_post_id, None)
})
.await??;
let res = PostResponse { post: post_view };
- chat_server.do_send(SendPost {
+ context.chat_server().do_send(SendPost {
op: UserOperation::EditPost,
post: res,
my_id: None,
});
- announce_if_community_is_local(update, &user, client, pool).await?;
+ announce_if_community_is_local(update, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
async fn receive_update_comment(
update: Update,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let note = Note::from_any_base(update.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
- let user = get_user_from_activity(&update, client, pool).await?;
+ let user = get_user_from_activity(&update, context).await?;
- let comment = CommentForm::from_apub(¬e, client, pool, Some(user.actor_id()?)).await?;
+ let comment = CommentForm::from_apub(¬e, context, Some(user.actor_id()?)).await?;
- let original_comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, client, pool)
+ let original_comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
.await?
.id;
- let updated_comment = blocking(pool, move |conn| {
+ let updated_comment = blocking(context.pool(), move |conn| {
Comment::update(conn, original_comment_id, &comment)
})
.await??;
let post_id = updated_comment.post_id;
- let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let mentions = scrape_text_for_mentions(&updated_comment.content);
- let recipient_ids =
- send_local_notifs(mentions, updated_comment, &user, post, pool, false).await?;
+ let recipient_ids = send_local_notifs(
+ mentions,
+ updated_comment,
+ &user,
+ post,
+ context.pool(),
+ false,
+ )
+ .await?;
// Refetch the view
- let comment_view = blocking(pool, move |conn| {
+ let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, original_comment_id, None)
})
.await??;
form_id: None,
};
- chat_server.do_send(SendComment {
+ context.chat_server().do_send(SendComment {
op: UserOperation::EditComment,
comment: res,
my_id: None,
});
- announce_if_community_is_local(update, &user, client, pool).await?;
+ announce_if_community_is_local(update, &user, context).await?;
Ok(HttpResponse::Ok().finish())
}
ActorType,
},
blocking,
- routes::{ChatServerParam, DbPoolParam},
+ LemmyContext,
LemmyError,
};
use activitystreams::{
base::AnyBase,
prelude::*,
};
-use actix_web::{client::Client, web, HttpRequest, HttpResponse};
+use actix_web::{web, HttpRequest, HttpResponse};
use anyhow::{anyhow, Context};
use lemmy_db::{
community::{Community, CommunityFollower, CommunityFollowerForm},
request: HttpRequest,
input: web::Json<AcceptedActivities>,
path: web::Path<String>,
- db: DbPoolParam,
- client: web::Data<Client>,
- _chat_server: ChatServerParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
let activity = input.into_inner();
let path = path.into_inner();
- let community = blocking(&db, move |conn| Community::read_from_name(&conn, &path)).await??;
+ let community = blocking(&context.pool(), move |conn| {
+ Community::read_from_name(&conn, &path)
+ })
+ .await??;
if !community.local {
return Err(
.context(location_info!())?;
check_is_apub_id_valid(user_uri)?;
- let user = get_or_fetch_and_upsert_user(&user_uri, &client, &db).await?;
+ let user = get_or_fetch_and_upsert_user(&user_uri, &context).await?;
verify(&request, &user)?;
let kind = activity.kind().context(location_info!())?;
let user_id = user.id;
let res = match kind {
- ValidTypes::Follow => handle_follow(any_base, user, community, &client, &db).await,
- ValidTypes::Undo => handle_undo_follow(any_base, user, community, &db).await,
+ ValidTypes::Follow => handle_follow(any_base, user, community, &context).await,
+ ValidTypes::Undo => handle_undo_follow(any_base, user, community, &context).await,
};
- insert_activity(user_id, activity.clone(), false, &db).await?;
+ insert_activity(user_id, activity.clone(), false, context.pool()).await?;
res
}
activity: AnyBase,
user: User_,
community: Community,
- client: &Client,
- db: &DbPoolParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let follow = Follow::from_any_base(activity)?.context(location_info!())?;
let community_follower_form = CommunityFollowerForm {
};
// This will fail if they're already a follower, but ignore the error.
- blocking(db, move |conn| {
+ blocking(&context.pool(), move |conn| {
CommunityFollower::follow(&conn, &community_follower_form).ok()
})
.await?;
- community.send_accept_follow(follow, &client, db).await?;
+ community.send_accept_follow(follow, context).await?;
Ok(HttpResponse::Ok().finish())
}
activity: AnyBase,
user: User_,
community: Community,
- db: &DbPoolParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let _undo = Undo::from_any_base(activity)?.context(location_info!())?;
};
// This will fail if they aren't a follower, but ignore the error.
- blocking(db, move |conn| {
+ blocking(&context.pool(), move |conn| {
CommunityFollower::unfollow(&conn, &community_follower_form).ok()
})
.await?;
},
insert_activity,
},
- routes::{ChatServerParam, DbPoolParam},
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{
object::AsObject,
prelude::*,
};
-use actix_web::{client::Client, web, HttpRequest, HttpResponse};
+use actix_web::{web, HttpRequest, HttpResponse};
use anyhow::Context;
use lemmy_db::user::User_;
use lemmy_utils::location_info;
pub async fn shared_inbox(
request: HttpRequest,
input: web::Json<AcceptedActivities>,
- client: web::Data<Client>,
- pool: DbPoolParam,
- chat_server: ChatServerParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
let activity = input.into_inner();
check_is_apub_id_valid(sender)?;
check_is_apub_id_valid(&community)?;
- let actor = get_or_fetch_and_upsert_actor(sender, &client, &pool).await?;
+ let actor = get_or_fetch_and_upsert_actor(sender, &context).await?;
verify(&request, actor.as_ref())?;
let any_base = activity.clone().into_any_base()?;
let kind = activity.kind().context(location_info!())?;
let res = match kind {
- ValidTypes::Announce => receive_announce(any_base, &client, &pool, chat_server).await,
- ValidTypes::Create => receive_create(any_base, &client, &pool, chat_server).await,
- ValidTypes::Update => receive_update(any_base, &client, &pool, chat_server).await,
- ValidTypes::Like => receive_like(any_base, &client, &pool, chat_server).await,
- ValidTypes::Dislike => receive_dislike(any_base, &client, &pool, chat_server).await,
- ValidTypes::Remove => receive_remove(any_base, &client, &pool, chat_server).await,
- ValidTypes::Delete => receive_delete(any_base, &client, &pool, chat_server).await,
- ValidTypes::Undo => receive_undo(any_base, &client, &pool, chat_server).await,
+ ValidTypes::Announce => receive_announce(any_base, &context).await,
+ ValidTypes::Create => receive_create(any_base, &context).await,
+ ValidTypes::Update => receive_update(any_base, &context).await,
+ ValidTypes::Like => receive_like(any_base, &context).await,
+ ValidTypes::Dislike => receive_dislike(any_base, &context).await,
+ ValidTypes::Remove => receive_remove(any_base, &context).await,
+ ValidTypes::Delete => receive_delete(any_base, &context).await,
+ ValidTypes::Undo => receive_undo(any_base, &context).await,
};
- insert_activity(actor.user_id(), activity.clone(), false, &pool).await?;
+ insert_activity(actor.user_id(), activity.clone(), false, context.pool()).await?;
res
}
pub(in crate::apub::inbox) async fn get_user_from_activity<T, A>(
activity: &T,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<User_, LemmyError>
where
T: AsBase<A> + ActorAndObjectRef,
{
let actor = activity.actor()?;
let user_uri = actor.as_single_xsd_any_uri().context(location_info!())?;
- get_or_fetch_and_upsert_user(&user_uri, client, pool).await
+ get_or_fetch_and_upsert_user(&user_uri, context).await
}
pub(in crate::apub::inbox) fn get_community_id_from_activity<T, A>(
pub(in crate::apub::inbox) async fn announce_if_community_is_local<T, Kind>(
activity: T,
user: &User_,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError>
where
T: AsObject<Kind>,
let community_uri = community_followers_uri
.to_string()
.replace("/followers", "");
- let community =
- get_or_fetch_and_upsert_community(&Url::parse(&community_uri)?, client, pool).await?;
+ let community = get_or_fetch_and_upsert_community(&Url::parse(&community_uri)?, context).await?;
if community.local {
- do_announce(activity.into_any_base()?, &community, &user, client, pool).await?;
+ do_announce(activity.into_any_base()?, &community, &user, context).await?;
}
Ok(())
}
FromApub,
},
blocking,
- routes::{ChatServerParam, DbPoolParam},
websocket::{server::SendUserRoomMessage, UserOperation},
- DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{
object::Note,
prelude::*,
};
-use actix_web::{client::Client, web, HttpRequest, HttpResponse};
+use actix_web::{web, HttpRequest, HttpResponse};
use anyhow::Context;
use lemmy_db::{
community::{CommunityFollower, CommunityFollowerForm},
request: HttpRequest,
input: web::Json<AcceptedActivities>,
path: web::Path<String>,
- client: web::Data<Client>,
- pool: DbPoolParam,
- chat_server: ChatServerParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
let activity = input.into_inner();
let username = path.into_inner();
check_is_apub_id_valid(actor_uri)?;
- let actor = get_or_fetch_and_upsert_actor(actor_uri, &client, &pool).await?;
+ let actor = get_or_fetch_and_upsert_actor(actor_uri, &context).await?;
verify(&request, actor.as_ref())?;
let any_base = activity.clone().into_any_base()?;
let kind = activity.kind().context(location_info!())?;
let res = match kind {
- ValidTypes::Accept => receive_accept(any_base, username, &client, &pool).await,
- ValidTypes::Create => {
- receive_create_private_message(any_base, &client, &pool, chat_server).await
- }
- ValidTypes::Update => {
- receive_update_private_message(any_base, &client, &pool, chat_server).await
- }
- ValidTypes::Delete => {
- receive_delete_private_message(any_base, &client, &pool, chat_server).await
- }
- ValidTypes::Undo => {
- receive_undo_delete_private_message(any_base, &client, &pool, chat_server).await
- }
+ ValidTypes::Accept => receive_accept(any_base, username, &context).await,
+ ValidTypes::Create => receive_create_private_message(any_base, &context).await,
+ ValidTypes::Update => receive_update_private_message(any_base, &context).await,
+ ValidTypes::Delete => receive_delete_private_message(any_base, &context).await,
+ ValidTypes::Undo => receive_undo_delete_private_message(any_base, &context).await,
};
- insert_activity(actor.user_id(), activity.clone(), false, &pool).await?;
+ insert_activity(actor.user_id(), activity.clone(), false, context.pool()).await?;
res
}
async fn receive_accept(
activity: AnyBase,
username: String,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let accept = Accept::from_any_base(activity)?.context(location_info!())?;
let community_uri = accept
.single_xsd_any_uri()
.context(location_info!())?;
- let community = get_or_fetch_and_upsert_community(&community_uri, client, pool).await?;
+ let community = get_or_fetch_and_upsert_community(&community_uri, context).await?;
- let user = blocking(pool, move |conn| User_::read_from_name(conn, &username)).await??;
+ let user = blocking(&context.pool(), move |conn| {
+ User_::read_from_name(conn, &username)
+ })
+ .await??;
// Now you need to add this to the community follower
let community_follower_form = CommunityFollowerForm {
};
// This will fail if they're already a follower
- blocking(pool, move |conn| {
+ blocking(&context.pool(), move |conn| {
CommunityFollower::follow(conn, &community_follower_form).ok()
})
.await?;
async fn receive_create_private_message(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let create = Create::from_any_base(activity)?.context(location_info!())?;
let note = Note::from_any_base(
.context(location_info!())?;
let domain = Some(create.id_unchecked().context(location_info!())?.to_owned());
- let private_message = PrivateMessageForm::from_apub(¬e, client, pool, domain).await?;
+ let private_message = PrivateMessageForm::from_apub(¬e, context, domain).await?;
- let inserted_private_message = blocking(pool, move |conn| {
+ let inserted_private_message = blocking(&context.pool(), move |conn| {
PrivateMessage::create(conn, &private_message)
})
.await??;
- let message = blocking(pool, move |conn| {
+ let message = blocking(&context.pool(), move |conn| {
PrivateMessageView::read(conn, inserted_private_message.id)
})
.await??;
let recipient_id = res.message.recipient_id;
- chat_server.do_send(SendUserRoomMessage {
+ context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::CreatePrivateMessage,
response: res,
recipient_id,
async fn receive_update_private_message(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let update = Update::from_any_base(activity)?.context(location_info!())?;
let note = Note::from_any_base(
.context(location_info!())?;
let domain = Some(update.id_unchecked().context(location_info!())?.to_owned());
- let private_message_form = PrivateMessageForm::from_apub(¬e, client, pool, domain).await?;
+ let private_message_form = PrivateMessageForm::from_apub(¬e, context, domain).await?;
let private_message_ap_id = private_message_form.ap_id.clone();
- let private_message = blocking(pool, move |conn| {
+ let private_message = blocking(&context.pool(), move |conn| {
PrivateMessage::read_from_apub_id(conn, &private_message_ap_id)
})
.await??;
let private_message_id = private_message.id;
- blocking(pool, move |conn| {
+ blocking(&context.pool(), move |conn| {
PrivateMessage::update(conn, private_message_id, &private_message_form)
})
.await??;
let private_message_id = private_message.id;
- let message = blocking(pool, move |conn| {
+ let message = blocking(&context.pool(), move |conn| {
PrivateMessageView::read(conn, private_message_id)
})
.await??;
let recipient_id = res.message.recipient_id;
- chat_server.do_send(SendUserRoomMessage {
+ context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage,
response: res,
recipient_id,
async fn receive_delete_private_message(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let delete = Delete::from_any_base(activity)?.context(location_info!())?;
let note = Note::from_any_base(
.context(location_info!())?;
let domain = Some(delete.id_unchecked().context(location_info!())?.to_owned());
- let private_message_form = PrivateMessageForm::from_apub(¬e, client, pool, domain).await?;
+ let private_message_form = PrivateMessageForm::from_apub(¬e, context, domain).await?;
let private_message_ap_id = private_message_form.ap_id;
- let private_message = blocking(pool, move |conn| {
+ let private_message = blocking(&context.pool(), move |conn| {
PrivateMessage::read_from_apub_id(conn, &private_message_ap_id)
})
.await??;
};
let private_message_id = private_message.id;
- blocking(pool, move |conn| {
+ blocking(&context.pool(), move |conn| {
PrivateMessage::update(conn, private_message_id, &private_message_form)
})
.await??;
let private_message_id = private_message.id;
- let message = blocking(pool, move |conn| {
+ let message = blocking(&context.pool(), move |conn| {
PrivateMessageView::read(&conn, private_message_id)
})
.await??;
let recipient_id = res.message.recipient_id;
- chat_server.do_send(SendUserRoomMessage {
+ context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage,
response: res,
recipient_id,
async fn receive_undo_delete_private_message(
activity: AnyBase,
- client: &Client,
- pool: &DbPool,
- chat_server: ChatServerParam,
+ context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let undo = Undo::from_any_base(activity)?.context(location_info!())?;
let delete = Delete::from_any_base(undo.object().as_one().context(location_info!())?.to_owned())?
.context(location_info!())?;
let domain = Some(undo.id_unchecked().context(location_info!())?.to_owned());
- let private_message = PrivateMessageForm::from_apub(¬e, client, pool, domain).await?;
+ let private_message = PrivateMessageForm::from_apub(¬e, context, domain).await?;
let private_message_ap_id = private_message.ap_id.clone();
- let private_message_id = blocking(pool, move |conn| {
+ let private_message_id = blocking(&context.pool(), move |conn| {
PrivateMessage::read_from_apub_id(conn, &private_message_ap_id).map(|pm| pm.id)
})
.await??;
updated: Some(naive_now()),
};
- blocking(pool, move |conn| {
+ blocking(&context.pool(), move |conn| {
PrivateMessage::update(conn, private_message_id, &private_message_form)
})
.await??;
- let message = blocking(pool, move |conn| {
+ let message = blocking(&context.pool(), move |conn| {
PrivateMessageView::read(&conn, private_message_id)
})
.await??;
let recipient_id = res.message.recipient_id;
- chat_server.do_send(SendUserRoomMessage {
+ context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage,
response: res,
recipient_id,
request::{retry, RecvError},
routes::webfinger::WebFingerResponse,
DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{
/// Converts an object from ActivityPub type to Lemmy internal type.
///
/// * `apub` The object to read from
- /// * `client` Web client to fetch remote actors with
- /// * `pool` Database connection
+ /// * `context` LemmyContext which holds DB pool, HTTP client etc
/// * `expected_domain` If present, ensure that the apub object comes from the same domain as
/// this URL
///
async fn from_apub(
apub: &Self::ApubType,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
expected_domain: Option<Url>,
) -> Result<Self, LemmyError>
where
#[async_trait::async_trait(?Send)]
pub trait ApubObjectType {
- async fn send_create(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError>;
- async fn send_update(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError>;
- async fn send_delete(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError>;
+ async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
+ async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
+ async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_delete(
&self,
creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError>;
- async fn send_remove(
- &self,
- mod_: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError>;
- async fn send_undo_remove(
- &self,
- mod_: &User_,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError>;
+ async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
+ async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
}
pub(in crate::apub) fn check_actor_domain<T, Kind>(
#[async_trait::async_trait(?Send)]
pub trait ApubLikeableType {
- async fn send_like(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError>;
- async fn send_dislike(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError>;
- async fn send_undo_like(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError>;
+ async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
+ async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
+ async fn send_undo_like(&self, creator: &User_, context: &LemmyContext)
+ -> Result<(), LemmyError>;
}
#[async_trait::async_trait(?Send)]
async fn send_follow(
&self,
follow_actor_id: &Url,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_unfollow(
&self,
follow_actor_id: &Url,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError>;
#[allow(unused_variables)]
async fn send_accept_follow(
&self,
follow: Follow,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError>;
- async fn send_delete(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError>;
+ async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_delete(
&self,
creator: &User_,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError>;
- async fn send_remove(
- &self,
- mod_: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError>;
- async fn send_undo_remove(
- &self,
- mod_: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError>;
+ async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
+ async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
/// For a given community, returns the inboxes of all followers.
async fn get_follower_inboxes(&self, pool: &DbPool) -> Result<Vec<Url>, LemmyError>;
ToApub,
},
blocking,
- routes::DbPoolParam,
DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{
Undo,
Update,
},
- context,
object::{kind::PageType, Image, Object, Page, Tombstone},
prelude::*,
public,
};
use activitystreams_ext::Ext1;
-use actix_web::{body::Body, client::Client, web, HttpResponse};
+use actix_web::{body::Body, web, HttpResponse};
use anyhow::Context;
use lemmy_db::{
community::Community,
/// Return the post json over HTTP.
pub async fn get_apub_post(
info: web::Path<PostQuery>,
- db: DbPoolParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let id = info.post_id.parse::<i32>()?;
- let post = blocking(&db, move |conn| Post::read(conn, id)).await??;
+ let post = blocking(context.pool(), move |conn| Post::read(conn, id)).await??;
if !post.deleted {
- Ok(create_apub_response(&post.to_apub(&db).await?))
+ Ok(create_apub_response(&post.to_apub(context.pool()).await?))
} else {
Ok(create_apub_tombstone_response(&post.to_tombstone()?))
}
// Not needed when the Post is embedded in a collection (like for community outbox)
// TODO: need to set proper context defining sensitive/commentsEnabled fields
// https://git.asonix.dog/Aardwolf/activitystreams/issues/5
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(self.ap_id.parse::<Url>()?)
// Use summary field to be consistent with mastodon content warning.
// https://mastodon.xyz/@Louisa/103987265222901387.json
/// Parse an ActivityPub page received from another instance into a Lemmy post.
async fn from_apub(
page: &PageExt,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
expected_domain: Option<Url>,
) -> Result<PostForm, LemmyError> {
let ext = &page.ext_one;
.as_single_xsd_any_uri()
.context(location_info!())?;
- let creator = get_or_fetch_and_upsert_user(creator_actor_id, client, pool).await?;
+ let creator = get_or_fetch_and_upsert_user(creator_actor_id, context).await?;
let community_actor_id = page
.inner
.as_single_xsd_any_uri()
.context(location_info!())?;
- let community = get_or_fetch_and_upsert_community(community_actor_id, client, pool).await?;
+ let community = get_or_fetch_and_upsert_community(community_actor_id, context).await?;
let thumbnail_url = match &page.inner.image() {
Some(any_image) => Image::from_any_base(
#[async_trait::async_trait(?Send)]
impl ApubObjectType for Post {
/// Send out information about a newly created post, to the followers of the community.
- async fn send_create(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let page = self.to_apub(pool).await?;
+ async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut create = Create::new(creator.actor_id.to_owned(), page.into_any_base()?);
create
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(CreateType::Create)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
create.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
}
/// Send out information about an edited post, to the followers of the community.
- async fn send_update(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let page = self.to_apub(pool).await?;
+ async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut update = Update::new(creator.actor_id.to_owned(), page.into_any_base()?);
update
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UpdateType::Update)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
update.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
}
- async fn send_delete(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let page = self.to_apub(pool).await?;
+ async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut delete = Delete::new(creator.actor_id.to_owned(), page.into_any_base()?);
delete
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
delete.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
async fn send_undo_delete(
&self,
creator: &User_,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError> {
- let page = self.to_apub(pool).await?;
+ let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut delete = Delete::new(creator.actor_id.to_owned(), page.into_any_base()?);
delete
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity
let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
undo
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
undo.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
}
- async fn send_remove(
- &self,
- mod_: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let page = self.to_apub(pool).await?;
+ async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut remove = Remove::new(mod_.actor_id.to_owned(), page.into_any_base()?);
remove
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
remove.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
}
- async fn send_undo_remove(
- &self,
- mod_: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let page = self.to_apub(pool).await?;
+ async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut remove = Remove::new(mod_.actor_id.to_owned(), page.into_any_base()?);
remove
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity
let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?);
undo
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
undo.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
#[async_trait::async_trait(?Send)]
impl ApubLikeableType for Post {
- async fn send_like(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let page = self.to_apub(pool).await?;
+ async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut like = Like::new(creator.actor_id.to_owned(), page.into_any_base()?);
like
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(LikeType::Like)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
like.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
}
- async fn send_dislike(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let page = self.to_apub(pool).await?;
+ async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut dislike = Dislike::new(creator.actor_id.to_owned(), page.into_any_base()?);
dislike
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
dislike.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
async fn send_undo_like(
&self,
creator: &User_,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError> {
- let page = self.to_apub(pool).await?;
+ let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
- let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
+ let community = blocking(context.pool(), move |conn| {
+ Community::read(conn, community_id)
+ })
+ .await??;
let mut like = Like::new(creator.actor_id.to_owned(), page.into_any_base()?);
like
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(LikeType::Like)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
// Undo that fake activity
let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?);
undo
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.get_followers_url()?]);
&community,
vec![community.get_shared_inbox_url()?],
undo.into_any_base()?,
- client,
- pool,
+ context,
)
.await?;
Ok(())
},
blocking,
DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{
Undo,
Update,
},
- context,
object::{kind::NoteType, Note, Tombstone},
prelude::*,
};
-use actix_web::client::Client;
use anyhow::Context;
use lemmy_db::{
private_message::{PrivateMessage, PrivateMessageForm},
let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;
private_message
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(Url::parse(&self.ap_id.to_owned())?)
.set_published(convert_datetime(self.published))
.set_content(self.content.to_owned())
/// Parse an ActivityPub note received from another instance into a Lemmy Private message
async fn from_apub(
note: &Note,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
expected_domain: Option<Url>,
) -> Result<PrivateMessageForm, LemmyError> {
let creator_actor_id = note
.single_xsd_any_uri()
.context(location_info!())?;
- let creator = get_or_fetch_and_upsert_user(&creator_actor_id, client, pool).await?;
+ let creator = get_or_fetch_and_upsert_user(&creator_actor_id, context).await?;
let recipient_actor_id = note
.to()
.context(location_info!())?
.clone()
.single_xsd_any_uri()
.context(location_info!())?;
- let recipient = get_or_fetch_and_upsert_user(&recipient_actor_id, client, pool).await?;
+ let recipient = get_or_fetch_and_upsert_user(&recipient_actor_id, context).await?;
let ap_id = note.id_unchecked().context(location_info!())?.to_string();
check_is_apub_id_valid(&Url::parse(&ap_id)?)?;
#[async_trait::async_trait(?Send)]
impl ApubObjectType for PrivateMessage {
/// Send out information about a newly created private message
- async fn send_create(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let note = self.to_apub(context.pool()).await?;
let recipient_id = self.recipient_id;
- let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;
+ let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?);
let to = recipient.get_inbox_url()?;
create
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(CreateType::Create)?)
.set_to(to.clone());
- insert_activity(creator.id, create.clone(), true, pool).await?;
+ insert_activity(creator.id, create.clone(), true, context.pool()).await?;
- send_activity(client, &create.into_any_base()?, creator, vec![to]).await?;
+ send_activity(
+ context.client(),
+ &create.into_any_base()?,
+ creator,
+ vec![to],
+ )
+ .await?;
Ok(())
}
/// Send out information about an edited post, to the followers of the community.
- async fn send_update(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let note = self.to_apub(context.pool()).await?;
let recipient_id = self.recipient_id;
- let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;
+ let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?);
let to = recipient.get_inbox_url()?;
update
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UpdateType::Update)?)
.set_to(to.clone());
- insert_activity(creator.id, update.clone(), true, pool).await?;
+ insert_activity(creator.id, update.clone(), true, context.pool()).await?;
- send_activity(client, &update.into_any_base()?, creator, vec![to]).await?;
+ send_activity(
+ context.client(),
+ &update.into_any_base()?,
+ creator,
+ vec![to],
+ )
+ .await?;
Ok(())
}
- async fn send_delete(
- &self,
- creator: &User_,
- client: &Client,
- pool: &DbPool,
- ) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
+ let note = self.to_apub(context.pool()).await?;
let recipient_id = self.recipient_id;
- let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;
+ let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
let to = recipient.get_inbox_url()?;
delete
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(to.clone());
- insert_activity(creator.id, delete.clone(), true, pool).await?;
+ insert_activity(creator.id, delete.clone(), true, context.pool()).await?;
- send_activity(client, &delete.into_any_base()?, creator, vec![to]).await?;
+ send_activity(
+ context.client(),
+ &delete.into_any_base()?,
+ creator,
+ vec![to],
+ )
+ .await?;
Ok(())
}
async fn send_undo_delete(
&self,
creator: &User_,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError> {
- let note = self.to_apub(pool).await?;
+ let note = self.to_apub(context.pool()).await?;
let recipient_id = self.recipient_id;
- let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;
+ let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
let to = recipient.get_inbox_url()?;
delete
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(to.clone());
// Undo that fake activity
let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
undo
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(to.clone());
- insert_activity(creator.id, undo.clone(), true, pool).await?;
+ insert_activity(creator.id, undo.clone(), true, context.pool()).await?;
- send_activity(client, &undo.into_any_base()?, creator, vec![to]).await?;
+ send_activity(context.client(), &undo.into_any_base()?, creator, vec![to]).await?;
Ok(())
}
- async fn send_remove(
- &self,
- _mod_: &User_,
- _client: &Client,
- _pool: &DbPool,
- ) -> Result<(), LemmyError> {
+ async fn send_remove(&self, _mod_: &User_, _context: &LemmyContext) -> Result<(), LemmyError> {
unimplemented!()
}
async fn send_undo_remove(
&self,
_mod_: &User_,
- _client: &Client,
- _pool: &DbPool,
+ _context: &LemmyContext,
) -> Result<(), LemmyError> {
unimplemented!()
}
ToApub,
},
blocking,
- routes::DbPoolParam,
DbPool,
+ LemmyContext,
LemmyError,
};
use activitystreams::{
Undo,
},
actor::{ApActor, Endpoints, Person},
- context,
object::{Image, Tombstone},
prelude::*,
};
use activitystreams_ext::Ext1;
-use actix_web::{body::Body, client::Client, web, HttpResponse};
+use actix_web::{body::Body, web, HttpResponse};
use anyhow::Context;
use lemmy_db::{
naive_now,
// TODO go through all these to_string and to_owned()
let mut person = Person::new();
person
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(Url::parse(&self.actor_id)?)
.set_name(self.name.to_owned())
.set_published(convert_datetime(self.published));
async fn send_follow(
&self,
follow_actor_id: &Url,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError> {
let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id.as_str());
follow
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(FollowType::Follow)?);
- let follow_actor = get_or_fetch_and_upsert_actor(follow_actor_id, client, pool).await?;
+ let follow_actor = get_or_fetch_and_upsert_actor(follow_actor_id, context).await?;
let to = follow_actor.get_inbox_url()?;
- insert_activity(self.id, follow.clone(), true, pool).await?;
+ insert_activity(self.id, follow.clone(), true, context.pool()).await?;
- send_activity(client, &follow.into_any_base()?, self, vec![to]).await?;
+ send_activity(context.client(), &follow.into_any_base()?, self, vec![to]).await?;
Ok(())
}
async fn send_unfollow(
&self,
follow_actor_id: &Url,
- client: &Client,
- pool: &DbPool,
+ context: &LemmyContext,
) -> Result<(), LemmyError> {
let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id.as_str());
follow
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(FollowType::Follow)?);
- let follow_actor = get_or_fetch_and_upsert_actor(follow_actor_id, client, pool).await?;
+ let follow_actor = get_or_fetch_and_upsert_actor(follow_actor_id, context).await?;
let to = follow_actor.get_inbox_url()?;
// Undo that fake activity
let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?);
undo
- .set_context(context())
+ .set_context(activitystreams::context())
.set_id(generate_activity_id(UndoType::Undo)?);
- insert_activity(self.id, undo.clone(), true, pool).await?;
+ insert_activity(self.id, undo.clone(), true, context.pool()).await?;
- send_activity(client, &undo.into_any_base()?, self, vec![to]).await?;
+ send_activity(context.client(), &undo.into_any_base()?, self, vec![to]).await?;
Ok(())
}
- async fn send_delete(
- &self,
- _creator: &User_,
- _client: &Client,
- _pool: &DbPool,
- ) -> Result<(), LemmyError> {
+ async fn send_delete(&self, _creator: &User_, _context: &LemmyContext) -> Result<(), LemmyError> {
unimplemented!()
}
async fn send_undo_delete(
&self,
_creator: &User_,
- _client: &Client,
- _pool: &DbPool,
+ _context: &LemmyContext,
) -> Result<(), LemmyError> {
unimplemented!()
}
- async fn send_remove(
- &self,
- _creator: &User_,
- _client: &Client,
- _pool: &DbPool,
- ) -> Result<(), LemmyError> {
+ async fn send_remove(&self, _creator: &User_, _context: &LemmyContext) -> Result<(), LemmyError> {
unimplemented!()
}
async fn send_undo_remove(
&self,
_creator: &User_,
- _client: &Client,
- _pool: &DbPool,
+ _context: &LemmyContext,
) -> Result<(), LemmyError> {
unimplemented!()
}
async fn send_accept_follow(
&self,
_follow: Follow,
- _client: &Client,
- _pool: &DbPool,
+ _context: &LemmyContext,
) -> Result<(), LemmyError> {
unimplemented!()
}
/// Parse an ActivityPub person received from another instance into a Lemmy user.
async fn from_apub(
person: &PersonExt,
- _: &Client,
- _: &DbPool,
+ _context: &LemmyContext,
expected_domain: Option<Url>,
) -> Result<Self, LemmyError> {
let avatar = match person.icon() {
/// Return the user json over HTTP.
pub async fn get_apub_user_http(
info: web::Path<UserQuery>,
- db: DbPoolParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let user_name = info.into_inner().user_name;
- let user = blocking(&db, move |conn| {
+ let user = blocking(context.pool(), move |conn| {
User_::find_by_email_or_username(conn, &user_name)
})
.await??;
- let u = user.to_apub(&db).await?;
+ let u = user.to_apub(context.pool()).await?;
Ok(create_apub_response(&u))
}
pub mod version;
pub mod websocket;
-use crate::request::{retry, RecvError};
+use crate::{
+ request::{retry, RecvError},
+ websocket::server::ChatServer,
+};
+use actix::Addr;
use actix_web::{client::Client, dev::ConnectionInfo};
use anyhow::anyhow;
use lemmy_utils::{get_apub_protocol_string, settings::Settings};
impl actix_web::error::ResponseError for LemmyError {}
+pub struct LemmyContext {
+ pub pool: DbPool,
+ pub chat_server: Addr<ChatServer>,
+ pub client: Client,
+}
+
+impl LemmyContext {
+ pub fn create(pool: DbPool, chat_server: Addr<ChatServer>, client: Client) -> LemmyContext {
+ LemmyContext {
+ pool,
+ chat_server,
+ client,
+ }
+ }
+ pub fn pool(&self) -> &DbPool {
+ &self.pool
+ }
+ pub fn chat_server(&self) -> &Addr<ChatServer> {
+ &self.chat_server
+ }
+ pub fn client(&self) -> &Client {
+ &self.client
+ }
+}
+
+impl Clone for LemmyContext {
+ fn clone(&self) -> Self {
+ LemmyContext {
+ pool: self.pool.clone(),
+ chat_server: self.chat_server.clone(),
+ client: self.client.clone(),
+ }
+ }
+}
+
#[derive(Deserialize, Debug)]
pub struct IframelyResponse {
title: Option<String>,
rate_limit::{rate_limiter::RateLimiter, RateLimit},
routes::*,
websocket::server::*,
+ LemmyContext,
LemmyError,
};
use lemmy_utils::{settings::Settings, CACHE_CONTROL_REGEX};
rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
};
- // Set up websocket server
- let server = ChatServer::startup(pool.clone(), rate_limiter.clone(), Client::default()).start();
-
println!(
"Starting http server at {}:{}",
settings.bind, settings.port
// Create Http server with websocket support
HttpServer::new(move || {
+ let chat_server =
+ ChatServer::startup(pool.clone(), rate_limiter.clone(), Client::default()).start();
+ let context = LemmyContext::create(pool.clone(), chat_server, Client::default());
let settings = Settings::get();
let rate_limiter = rate_limiter.clone();
App::new()
.wrap_fn(add_cache_headers)
.wrap(middleware::Logger::default())
- .data(pool.clone())
- .data(server.clone())
- .data(Client::default())
+ .data(context)
// The routes
.configure(|cfg| api::config(cfg, &rate_limiter))
.configure(federation::config)
use crate::{
api::{comment::*, community::*, post::*, site::*, user::*, Perform},
rate_limit::RateLimit,
- routes::{ChatServerParam, DbPoolParam},
websocket::WebsocketInfo,
+ LemmyContext,
};
-use actix_web::{client::Client, error::ErrorBadRequest, *};
+use actix_web::{error::ErrorBadRequest, *};
use serde::Serialize;
pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
async fn perform<Request>(
data: Request,
- client: &Client,
- db: DbPoolParam,
- chat_server: ChatServerParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error>
where
Request: Perform,
Request: Send + 'static,
{
let ws_info = WebsocketInfo {
- chatserver: chat_server.get_ref().to_owned(),
+ chatserver: context.chat_server().to_owned(),
id: None,
};
let res = data
- .perform(&db, Some(ws_info), client.clone())
+ .perform(&context, Some(ws_info))
.await
.map(|json| HttpResponse::Ok().json(json))
.map_err(ErrorBadRequest)?;
async fn route_get<Data>(
data: web::Query<Data>,
- client: web::Data<Client>,
- db: DbPoolParam,
- chat_server: ChatServerParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error>
where
Data: Serialize + Send + 'static + Perform,
{
- perform::<Data>(data.0, &client, db, chat_server).await
+ perform::<Data>(data.0, context).await
}
async fn route_post<Data>(
data: web::Json<Data>,
- client: web::Data<Client>,
- db: DbPoolParam,
- chat_server: ChatServerParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error>
where
Data: Serialize + Send + 'static + Perform,
{
- perform::<Data>(data.0, &client, db, chat_server).await
+ perform::<Data>(data.0, context).await
}
-use crate::{api::claims::Claims, blocking, routes::DbPoolParam, LemmyError};
+use crate::{api::claims::Claims, blocking, LemmyContext, LemmyError};
use actix_web::{error::ErrorBadRequest, *};
use anyhow::anyhow;
use chrono::{DateTime, NaiveDateTime, Utc};
-use diesel::{
- r2d2::{ConnectionManager, Pool},
- PgConnection,
-};
+use diesel::PgConnection;
use lemmy_db::{
comment_view::{ReplyQueryBuilder, ReplyView},
community::Community,
.route("/feeds/all.xml", web::get().to(get_all_feed));
}
-async fn get_all_feed(info: web::Query<Params>, db: DbPoolParam) -> Result<HttpResponse, Error> {
+async fn get_all_feed(
+ info: web::Query<Params>,
+ context: web::Data<LemmyContext>,
+) -> Result<HttpResponse, Error> {
let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
- let rss = blocking(&db, move |conn| get_feed_all_data(conn, &sort_type))
- .await?
- .map_err(ErrorBadRequest)?;
+ let rss = blocking(context.pool(), move |conn| {
+ get_feed_all_data(conn, &sort_type)
+ })
+ .await?
+ .map_err(ErrorBadRequest)?;
Ok(
HttpResponse::Ok()
async fn get_feed(
path: web::Path<(String, String)>,
info: web::Query<Params>,
- db: web::Data<Pool<ConnectionManager<PgConnection>>>,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error> {
let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
let param = path.1.to_owned();
- let builder = blocking(&db, move |conn| match request_type {
+ let builder = blocking(context.pool(), move |conn| match request_type {
RequestType::User => get_feed_user(conn, &sort_type, param),
RequestType::Community => get_feed_community(conn, &sort_type, param),
RequestType::Front => get_feed_front(conn, &sort_type, param),
pub mod nodeinfo;
pub mod webfinger;
pub mod websocket;
-
-use crate::websocket::server::ChatServer;
-use actix::prelude::*;
-use actix_web::*;
-use diesel::{
- r2d2::{ConnectionManager, Pool},
- PgConnection,
-};
-
-pub type DbPoolParam = web::Data<Pool<ConnectionManager<PgConnection>>>;
-pub type ChatServerParam = web::Data<Addr<ChatServer>>;
-use crate::{blocking, routes::DbPoolParam, version, LemmyError};
+use crate::{blocking, version, LemmyContext, LemmyError};
use actix_web::{body::Body, error::ErrorBadRequest, *};
use anyhow::anyhow;
use lemmy_db::site_view::SiteView;
Ok(HttpResponse::Ok().json(node_info))
}
-async fn node_info(db: DbPoolParam) -> Result<HttpResponse, Error> {
- let site_view = blocking(&db, SiteView::read)
+async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Error> {
+ let site_view = blocking(context.pool(), SiteView::read)
.await?
.map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?;
-use crate::{blocking, routes::DbPoolParam, LemmyError};
+use crate::{blocking, LemmyContext, LemmyError};
use actix_web::{error::ErrorBadRequest, web::Query, *};
use anyhow::anyhow;
use lemmy_db::{community::Community, user::User_};
/// https://radical.town/.well-known/webfinger?resource=acct:felix@radical.town
async fn get_webfinger_response(
info: Query<Params>,
- db: DbPoolParam,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error> {
let community_regex_parsed = WEBFINGER_COMMUNITY_REGEX
.captures(&info.resource)
let url = if let Some(community_name) = community_regex_parsed {
let community_name = community_name.as_str().to_owned();
// Make sure the requested community exists.
- blocking(&db, move |conn| {
+ blocking(context.pool(), move |conn| {
Community::read_from_name(conn, &community_name)
})
.await?
} else if let Some(user_name) = user_regex_parsed {
let user_name = user_name.as_str().to_owned();
// Make sure the requested user exists.
- blocking(&db, move |conn| User_::read_from_name(conn, &user_name))
- .await?
- .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?
- .actor_id
+ blocking(context.pool(), move |conn| {
+ User_::read_from_name(conn, &user_name)
+ })
+ .await?
+ .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?
+ .actor_id
} else {
return Err(ErrorBadRequest(LemmyError::from(anyhow!("not_found"))));
};
use crate::{
get_ip,
websocket::server::{ChatServer, *},
+ LemmyContext,
};
use actix::prelude::*;
use actix_web::*;
pub async fn chat_route(
req: HttpRequest,
stream: web::Payload,
- chat_server: web::Data<Addr<ChatServer>>,
+ context: web::Data<LemmyContext>,
) -> Result<HttpResponse, Error> {
ws::start(
WSSession {
- cs_addr: chat_server.get_ref().to_owned(),
+ cs_addr: context.chat_server().to_owned(),
id: 0,
hb: Instant::now(),
ip: get_ip(&req.connection_info()),
websocket::UserOperation,
CommunityId,
ConnectionId,
- DbPool,
IPAddr,
+ LemmyContext,
LemmyError,
PostId,
UserId,
};
-use actix_web::client::Client;
+use actix_web::{client::Client, web};
use anyhow::Context as acontext;
use lemmy_db::naive_now;
use lemmy_utils::location_info;
let user_operation: UserOperation = UserOperation::from_str(&op)?;
- let args = Args {
- client,
+ let context = LemmyContext {
pool,
+ chat_server: addr,
+ client,
+ };
+ let args = Args {
+ context: &context,
rate_limiter,
- chatserver: addr,
id: msg.id,
ip,
op: user_operation.clone(),
}
struct Args<'a> {
- client: Client,
- pool: DbPool,
+ context: &'a LemmyContext,
rate_limiter: RateLimit,
- chatserver: Addr<ChatServer>,
id: ConnectionId,
ip: IPAddr,
op: UserOperation,
Data: Perform,
{
let Args {
- client,
- pool,
+ context,
rate_limiter,
- chatserver,
id,
ip,
op,
} = args;
let ws_info = WebsocketInfo {
- chatserver,
+ chatserver: context.chat_server().to_owned(),
id: Some(id),
};
let data = data.to_string();
let op2 = op.clone();
- let client = client.clone();
let fut = async move {
- let pool = pool.clone();
let parsed_data: Data = serde_json::from_str(&data)?;
- let res = parsed_data.perform(&pool, Some(ws_info), client).await?;
+ let res = parsed_data
+ .perform(&web::Data::new(context.to_owned()), Some(ws_info))
+ .await?;
to_json_string(&op, &res)
};