impl Perform<CommentResponse> for Oper<CreateComment> {
fn perform(&self) -> Result<CommentResponse, Error> {
- let data: CreateComment = self.data;
+ let data: &CreateComment = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
// Check for a community ban
let post = Post::read(&conn, data.post_id)?;
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
- return Err(APIError::err(self.op, "You have been banned from this community"))?
+ return Err(APIError::err(&self.op, "You have been banned from this community"))?
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
- return Err(APIError::err(self.op, "You have been banned from the site"))?
+ return Err(APIError::err(&self.op, "You have been banned from the site"))?
}
let content_slurs_removed = remove_slurs(&data.content.to_owned());
let inserted_comment = match Comment::create(&conn, &comment_form) {
Ok(comment) => comment,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't create Comment"))?
+ return Err(APIError::err(&self.op, "Couldn't create Comment"))?
}
};
let _inserted_like = match CommentLike::like(&conn, &like_form) {
Ok(like) => like,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't like comment."))?
+ return Err(APIError::err(&self.op, "Couldn't like comment."))?
}
};
impl Perform<CommentResponse> for Oper<EditComment> {
fn perform(&self) -> Result<CommentResponse, Error> {
- let data: EditComment = self.data;
+ let data: &EditComment = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
);
if !editors.contains(&user_id) {
- return Err(APIError::err(self.op, "Not allowed to edit comment."))?
+ return Err(APIError::err(&self.op, "Not allowed to edit comment."))?
}
// Check for a community ban
if CommunityUserBanView::get(&conn, user_id, orig_comment.community_id).is_ok() {
- return Err(APIError::err(self.op, "You have been banned from this community"))?
+ return Err(APIError::err(&self.op, "You have been banned from this community"))?
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
- return Err(APIError::err(self.op, "You have been banned from the site"))?
+ return Err(APIError::err(&self.op, "You have been banned from the site"))?
}
}
let _updated_comment = match Comment::update(&conn, data.edit_id, &comment_form) {
Ok(comment) => comment,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't update Comment"))?
+ return Err(APIError::err(&self.op, "Couldn't update Comment"))?
}
};
impl Perform<CommentResponse> for Oper<SaveComment> {
fn perform(&self) -> Result<CommentResponse, Error> {
- let data: SaveComment = self.data;
+ let data: &SaveComment = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
match CommentSaved::save(&conn, &comment_saved_form) {
Ok(comment) => comment,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldnt do comment save"))?
+ return Err(APIError::err(&self.op, "Couldnt do comment save"))?
}
};
} else {
match CommentSaved::unsave(&conn, &comment_saved_form) {
Ok(comment) => comment,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldnt do comment save"))?
+ return Err(APIError::err(&self.op, "Couldnt do comment save"))?
}
};
}
impl Perform<CommentResponse> for Oper<CreateCommentLike> {
fn perform(&self) -> Result<CommentResponse, Error> {
- let data: CreateCommentLike = self.data;
+ let data: &CreateCommentLike = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
// Check for a community ban
let post = Post::read(&conn, data.post_id)?;
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
- return Err(APIError::err(self.op, "You have been banned from this community"))?
+ return Err(APIError::err(&self.op, "You have been banned from this community"))?
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
- return Err(APIError::err(self.op, "You have been banned from the site"))?
+ return Err(APIError::err(&self.op, "You have been banned from the site"))?
}
let like_form = CommentLikeForm {
let _inserted_like = match CommentLike::like(&conn, &like_form) {
Ok(like) => like,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't like comment."))?
+ return Err(APIError::err(&self.op, "Couldn't like comment."))?
}
};
}
communities: Vec<CommunityView>
}
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, Clone)]
pub struct BanFromCommunity {
pub community_id: i32,
user_id: i32,
impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
fn perform(&self) -> Result<GetCommunityResponse, Error> {
- let data: GetCommunity = self.data;
+ let data: &GetCommunity = &self.data;
let conn = establish_connection();
let user_id: Option<i32> = match &data.auth {
let community_view = match CommunityView::read(&conn, community_id, user_id) {
Ok(community) => community,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't find Community"))?
+ return Err(APIError::err(&self.op, "Couldn't find Community"))?
}
};
let moderators = match CommunityModeratorView::for_community(&conn, community_id) {
Ok(moderators) => moderators,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't find Community"))?
+ return Err(APIError::err(&self.op, "Couldn't find Community"))?
}
};
impl Perform<CommunityResponse> for Oper<CreateCommunity> {
fn perform(&self) -> Result<CommunityResponse, Error> {
- let data: CreateCommunity = self.data;
+ let data: &CreateCommunity = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
if has_slurs(&data.name) ||
has_slurs(&data.title) ||
(data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) {
- return Err(APIError::err(self.op, "No slurs"))?
+ return Err(APIError::err(&self.op, "No slurs"))?
}
let user_id = claims.id;
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
- return Err(APIError::err(self.op, "You have been banned from the site"))?
+ return Err(APIError::err(&self.op, "You have been banned from the site"))?
}
// When you create a community, make sure the user becomes a moderator and a follower
let inserted_community = match Community::create(&conn, &community_form) {
Ok(community) => community,
Err(_e) => {
- return Err(APIError::err(self.op, "Community already exists."))?
+ return Err(APIError::err(&self.op, "Community already exists."))?
}
};
let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Community moderator already exists."))?
+ return Err(APIError::err(&self.op, "Community moderator already exists."))?
}
};
let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Community follower already exists."))?
+ return Err(APIError::err(&self.op, "Community follower already exists."))?
}
};
impl Perform<CommunityResponse> for Oper<EditCommunity> {
fn perform(&self) -> Result<CommunityResponse, Error> {
- let data: EditCommunity = self.data;
+ let data: &EditCommunity = &self.data;
if has_slurs(&data.name) || has_slurs(&data.title) {
- return Err(APIError::err(self.op, "No slurs"))?
+ return Err(APIError::err(&self.op, "No slurs"))?
}
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
- return Err(APIError::err(self.op, "You have been banned from the site"))?
+ return Err(APIError::err(&self.op, "You have been banned from the site"))?
}
// Verify its a mod
.collect()
);
if !editors.contains(&user_id) {
- return Err(APIError::err(self.op, "Not allowed to edit community"))?
+ return Err(APIError::err(&self.op, "Not allowed to edit community"))?
}
let community_form = CommunityForm {
let _updated_community = match Community::update(&conn, data.edit_id, &community_form) {
Ok(community) => community,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't update Community"))?
+ return Err(APIError::err(&self.op, "Couldn't update Community"))?
}
};
impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
fn perform(&self) -> Result<ListCommunitiesResponse, Error> {
- let data: ListCommunities = self.data;
+ let data: &ListCommunities = &self.data;
let conn = establish_connection();
let user_id: Option<i32> = match &data.auth {
impl Perform<CommunityResponse> for Oper<FollowCommunity> {
fn perform(&self) -> Result<CommunityResponse, Error> {
- let data: FollowCommunity = self.data;
+ let data: &FollowCommunity = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
match CommunityFollower::follow(&conn, &community_follower_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Community follower already exists."))?
+ return Err(APIError::err(&self.op, "Community follower already exists."))?
}
};
} else {
match CommunityFollower::ignore(&conn, &community_follower_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Community follower already exists."))?
+ return Err(APIError::err(&self.op, "Community follower already exists."))?
}
};
}
impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
fn perform(&self) -> Result<GetFollowedCommunitiesResponse, Error> {
- let data: GetFollowedCommunities = self.data;
+ let data: &GetFollowedCommunities = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
let communities: Vec<CommunityFollowerView> = match CommunityFollowerView::for_user(&conn, user_id) {
Ok(communities) => communities,
Err(_e) => {
- return Err(APIError::err(self.op, "System error, try logging out and back in."))?
+ return Err(APIError::err(&self.op, "System error, try logging out and back in."))?
}
};
impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
fn perform(&self) -> Result<BanFromCommunityResponse, Error> {
- let data: BanFromCommunity = self.data;
+ let data: &BanFromCommunity = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
match CommunityUserBan::ban(&conn, &community_user_ban_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Community user ban already exists"))?
+ return Err(APIError::err(&self.op, "Community user ban already exists"))?
}
};
} else {
match CommunityUserBan::unban(&conn, &community_user_ban_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Community user ban already exists"))?
+ return Err(APIError::err(&self.op, "Community user ban already exists"))?
}
};
}
impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
fn perform(&self) -> Result<AddModToCommunityResponse, Error> {
- let data: AddModToCommunity = self.data;
+ let data: &AddModToCommunity = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
match CommunityModerator::join(&conn, &community_moderator_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Community moderator already exists."))?
+ return Err(APIError::err(&self.op, "Community moderator already exists."))?
}
};
} else {
match CommunityModerator::leave(&conn, &community_moderator_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Community moderator already exists."))?
+ return Err(APIError::err(&self.op, "Community moderator already exists."))?
}
};
}
#[derive(Fail, Debug)]
#[fail(display = "{{\"op\":\"{}\", \"error\":\"{}\"}}", op, message)]
pub struct APIError {
- op: String,
- message: String,
+ pub op: String,
+ pub message: String,
}
impl APIError {
- pub fn err(op: UserOperation, msg: &str) -> Self {
+ pub fn err(op: &UserOperation, msg: &str) -> Self {
APIError {
op: op.to_string(),
message: msg.to_string(),
impl Perform<PostResponse> for Oper<CreatePost> {
fn perform(&self) -> Result<PostResponse, Error> {
- let data: CreatePost = self.data;
+ let data: &CreatePost = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
if has_slurs(&data.name) ||
(data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) {
- return Err(APIError::err(self.op, "No slurs"))?
+ return Err(APIError::err(&self.op, "No slurs"))?
}
let user_id = claims.id;
// Check for a community ban
if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() {
- return Err(APIError::err(self.op, "You have been banned from this community"))?
+ return Err(APIError::err(&self.op, "You have been banned from this community"))?
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
- return Err(APIError::err(self.op, "You have been banned from the site"))?
+ return Err(APIError::err(&self.op, "You have been banned from the site"))?
}
let post_form = PostForm {
let inserted_post = match Post::create(&conn, &post_form) {
Ok(post) => post,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't create Post"))?
+ return Err(APIError::err(&self.op, "Couldn't create Post"))?
}
};
let _inserted_like = match PostLike::like(&conn, &like_form) {
Ok(like) => like,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't like post."))?
+ return Err(APIError::err(&self.op, "Couldn't like post."))?
}
};
let post_view = match PostView::read(&conn, inserted_post.id, Some(user_id)) {
Ok(post) => post,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't find Post"))?
+ return Err(APIError::err(&self.op, "Couldn't find Post"))?
}
};
impl Perform<GetPostResponse> for Oper<GetPost> {
fn perform(&self) -> Result<GetPostResponse, Error> {
- let data: GetPost = self.data;
+ let data: &GetPost = &self.data;
let conn = establish_connection();
let user_id: Option<i32> = match &data.auth {
let post_view = match PostView::read(&conn, data.id, user_id) {
Ok(post) => post,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't find Post"))?
+ return Err(APIError::err(&self.op, "Couldn't find Post"))?
}
};
impl Perform<GetPostsResponse> for Oper<GetPosts> {
fn perform(&self) -> Result<GetPostsResponse, Error> {
- let data: GetPosts = self.data;
+ let data: &GetPosts = &self.data;
let conn = establish_connection();
let user_id: Option<i32> = match &data.auth {
data.limit) {
Ok(posts) => posts,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't get posts"))?
+ return Err(APIError::err(&self.op, "Couldn't get posts"))?
}
};
impl Perform<CreatePostLikeResponse> for Oper<CreatePostLike> {
fn perform(&self) -> Result<CreatePostLikeResponse, Error> {
- let data: CreatePostLike = self.data;
+ let data: &CreatePostLike = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
// Check for a community ban
let post = Post::read(&conn, data.post_id)?;
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
- return Err(APIError::err(self.op, "You have been banned from this community"))?
+ return Err(APIError::err(&self.op, "You have been banned from this community"))?
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
- return Err(APIError::err(self.op, "You have been banned from the site"))?
+ return Err(APIError::err(&self.op, "You have been banned from the site"))?
}
let like_form = PostLikeForm {
let _inserted_like = match PostLike::like(&conn, &like_form) {
Ok(like) => like,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't like post."))?
+ return Err(APIError::err(&self.op, "Couldn't like post."))?
}
};
}
let post_view = match PostView::read(&conn, data.post_id, Some(user_id)) {
Ok(post) => post,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't find Post"))?
+ return Err(APIError::err(&self.op, "Couldn't find Post"))?
}
};
impl Perform<PostResponse> for Oper<EditPost> {
fn perform(&self) -> Result<PostResponse, Error> {
- let data: EditPost = self.data;
+ let data: &EditPost = &self.data;
if has_slurs(&data.name) ||
(data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) {
- return Err(APIError::err(self.op, "No slurs"))?
+ return Err(APIError::err(&self.op, "No slurs"))?
}
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
.collect()
);
if !editors.contains(&user_id) {
- return Err(APIError::err(self.op, "Not allowed to edit post."))?
+ return Err(APIError::err(&self.op, "Not allowed to edit post."))?
}
// Check for a community ban
if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() {
- return Err(APIError::err(self.op, "You have been banned from this community"))?
+ return Err(APIError::err(&self.op, "You have been banned from this community"))?
}
// Check for a site ban
if UserView::read(&conn, user_id)?.banned {
- return Err(APIError::err(self.op, "You have been banned from the site"))?
+ return Err(APIError::err(&self.op, "You have been banned from the site"))?
}
let post_form = PostForm {
let _updated_post = match Post::update(&conn, data.edit_id, &post_form) {
Ok(post) => post,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't update Post"))?
+ return Err(APIError::err(&self.op, "Couldn't update Post"))?
}
};
impl Perform<PostResponse> for Oper<SavePost> {
fn perform(&self) -> Result<PostResponse, Error> {
- let data: SavePost = self.data;
+ let data: &SavePost = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
match PostSaved::save(&conn, &post_saved_form) {
Ok(post) => post,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldnt do post save"))?
+ return Err(APIError::err(&self.op, "Couldnt do post save"))?
}
};
} else {
match PostSaved::unsave(&conn, &post_saved_form) {
Ok(post) => post,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldnt do post save"))?
+ return Err(APIError::err(&self.op, "Couldnt do post save"))?
}
};
}
impl Perform<ListCategoriesResponse> for Oper<ListCategories> {
fn perform(&self) -> Result<ListCategoriesResponse, Error> {
- let data: ListCategories = self.data;
+ let _data: &ListCategories = &self.data;
let conn = establish_connection();
let categories: Vec<Category> = Category::list_all(&conn)?;
impl Perform<GetModlogResponse> for Oper<GetModlog> {
fn perform(&self) -> Result<GetModlogResponse, Error> {
- let data: GetModlog = self.data;
+ let data: &GetModlog = &self.data;
let conn = establish_connection();
let removed_posts = ModRemovePostView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
impl Perform<SiteResponse> for Oper<CreateSite> {
fn perform(&self) -> Result<SiteResponse, Error> {
- let data: CreateSite = self.data;
+ let data: &CreateSite = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
if has_slurs(&data.name) ||
(data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) {
- return Err(APIError::err(self.op, "No slurs"))?
+ return Err(APIError::err(&self.op, "No slurs"))?
}
let user_id = claims.id;
// Make sure user is an admin
if !UserView::read(&conn, user_id)?.admin {
- return Err(APIError::err(self.op, "Not an admin."))?
+ return Err(APIError::err(&self.op, "Not an admin."))?
}
let site_form = SiteForm {
match Site::create(&conn, &site_form) {
Ok(site) => site,
Err(_e) => {
- return Err(APIError::err(self.op, "Site exists already"))?
+ return Err(APIError::err(&self.op, "Site exists already"))?
}
};
impl Perform<SiteResponse> for Oper<EditSite> {
fn perform(&self) -> Result<SiteResponse, Error> {
- let data: EditSite = self.data;
+ let data: &EditSite = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
if has_slurs(&data.name) ||
(data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) {
- return Err(APIError::err(self.op, "No slurs"))?
+ return Err(APIError::err(&self.op, "No slurs"))?
}
let user_id = claims.id;
// Make sure user is an admin
if UserView::read(&conn, user_id)?.admin == false {
- return Err(APIError::err(self.op, "Not an admin."))?
+ return Err(APIError::err(&self.op, "Not an admin."))?
}
let found_site = Site::read(&conn, 1)?;
match Site::update(&conn, 1, &site_form) {
Ok(site) => site,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't update site."))?
+ return Err(APIError::err(&self.op, "Couldn't update site."))?
}
};
impl Perform<GetSiteResponse> for Oper<GetSite> {
fn perform(&self) -> Result<GetSiteResponse, Error> {
- let data: GetSite = self.data;
+ let _data: &GetSite = &self.data;
let conn = establish_connection();
// It can return a null site in order to redirect
impl Perform<SearchResponse> for Oper<Search> {
fn perform(&self) -> Result<SearchResponse, Error> {
- let data: Search = self.data;
+ let data: &Search = &self.data;
let conn = establish_connection();
let sort = SortType::from_str(&data.sort)?;
impl Perform<LoginResponse> for Oper<Login> {
fn perform(&self) -> Result<LoginResponse, Error> {
- let data: Login = self.data;
+ let data: &Login = &self.data;
let conn = establish_connection();
// Fetch that username / email
let user: User_ = match User_::find_by_email_or_username(&conn, &data.username_or_email) {
Ok(user) => user,
- Err(_e) => return Err(APIError::err(self.op, "Couldn't find that username or email"))?
+ Err(_e) => return Err(APIError::err(&self.op, "Couldn't find that username or email"))?
};
// Verify the password
let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false);
if !valid {
- return Err(APIError::err(self.op, "Password incorrect"))?
+ return Err(APIError::err(&self.op, "Password incorrect"))?
}
// Return the jwt
impl Perform<LoginResponse> for Oper<Register> {
fn perform(&self) -> Result<LoginResponse, Error> {
- let data: Register = self.data;
+ let data: &Register = &self.data;
let conn = establish_connection();
// Make sure passwords match
if &data.password != &data.password_verify {
- return Err(APIError::err(self.op, "Passwords do not match."))?
+ return Err(APIError::err(&self.op, "Passwords do not match."))?
}
if data.spam_timeri < 1142 {
- return Err(APIError::err(self.op, "Too fast"))?
+ return Err(APIError::err(&self.op, "Too fast"))?
}
if has_slurs(&data.username) {
- return Err(APIError::err(self.op, "No slurs"))?
+ return Err(APIError::err(&self.op, "No slurs"))?
}
// Make sure there are no admins
if data.admin && UserView::admins(&conn)?.len() > 0 {
- return Err(APIError::err(self.op, "Sorry, there's already an admin."))?
+ return Err(APIError::err(&self.op, "Sorry, there's already an admin."))?
}
// Register the new user
let inserted_user = match User_::register(&conn, &user_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "User already exists."))?
+ return Err(APIError::err(&self.op, "User already exists."))?
}
};
let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Community follower already exists."))?
+ return Err(APIError::err(&self.op, "Community follower already exists."))?
}
};
let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Community moderator already exists."))?
+ return Err(APIError::err(&self.op, "Community moderator already exists."))?
}
};
impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
fn perform(&self) -> Result<GetUserDetailsResponse, Error> {
- let data: GetUserDetails = self.data;
+ let data: &GetUserDetails = &self.data;
let conn = establish_connection();
let user_id: Option<i32> = match &data.auth {
impl Perform<AddAdminResponse> for Oper<AddAdmin> {
fn perform(&self) -> Result<AddAdminResponse, Error> {
- let data: AddAdmin = self.data;
+ let data: &AddAdmin = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
// Make sure user is an admin
if UserView::read(&conn, user_id)?.admin == false {
- return Err(APIError::err(self.op, "Not an admin."))?
+ return Err(APIError::err(&self.op, "Not an admin."))?
}
let read_user = User_::read(&conn, data.user_id)?;
match User_::update(&conn, data.user_id, &user_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't update user"))?
+ return Err(APIError::err(&self.op, "Couldn't update user"))?
}
};
impl Perform<BanUserResponse> for Oper<BanUser> {
fn perform(&self) -> Result<BanUserResponse, Error> {
- let data: BanUser = self.data;
+ let data: &BanUser = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
// Make sure user is an admin
if UserView::read(&conn, user_id)?.admin == false {
- return Err(APIError::err(self.op, "Not an admin."))?
+ return Err(APIError::err(&self.op, "Not an admin."))?
}
let read_user = User_::read(&conn, data.user_id)?;
match User_::update(&conn, data.user_id, &user_form) {
Ok(user) => user,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't update user"))?
+ return Err(APIError::err(&self.op, "Couldn't update user"))?
}
};
impl Perform<GetRepliesResponse> for Oper<GetReplies> {
fn perform(&self) -> Result<GetRepliesResponse, Error> {
- let data: GetReplies = self.data;
+ let data: &GetReplies = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
fn perform(&self) -> Result<GetRepliesResponse, Error> {
- let data: MarkAllAsRead = self.data;
+ let data: &MarkAllAsRead = &self.data;
let conn = establish_connection();
let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims,
Err(_e) => {
- return Err(APIError::err(self.op, "Not logged in."))?
+ return Err(APIError::err(&self.op, "Not logged in."))?
}
};
let _updated_comment = match Comment::update(&conn, reply.id, &comment_form) {
Ok(comment) => comment,
Err(_e) => {
- return Err(APIError::err(self.op, "Couldn't update Comment"))?
+ return Err(APIError::err(&self.op, "Couldn't update Comment"))?
}
};
}
#[cfg(test)]
mod tests {
- use establish_connection;
use super::*;
#[test]
fn test_crud() {
#[cfg(test)]
mod tests {
- use establish_connection;
use super::*;
use super::super::post::*;
use super::super::community::*;
use super::super::user::*;
- use Crud;
#[test]
fn test_crud() {
let conn = establish_connection();
#[cfg(test)]
mod tests {
- use establish_connection;
use super::*;
use super::super::post::*;
use super::super::community::*;
use super::super::user::*;
use super::super::comment::*;
- use {Crud,Likeable};
#[test]
fn test_crud() {
let conn = establish_connection();
#[cfg(test)]
mod tests {
- use establish_connection;
use super::*;
use super::super::user::*;
- use Crud;
#[test]
fn test_crud() {
let conn = establish_connection();
let offset = limit * (page - 1);
(limit, offset)
}
+#[cfg(test)]
+mod tests {
+ use super::fuzzy_search;
+ #[test] fn test_fuzzy_search() {
+ let test = "This is a fuzzy search";
+ assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
+ }
+}
+
#[cfg(test)]
mod tests {
- use establish_connection;
use super::*;
use super::super::user::*;
use super::super::post::*;
#[cfg(test)]
mod tests {
- use establish_connection;
- use Crud;
use super::*;
use super::super::community::*;
use super::super::user::*;
#[cfg(test)]
mod tests {
- use {establish_connection, Crud, Likeable};
use super::*;
use super::super::community::*;
use super::super::user::*;
#[cfg(test)]
mod tests {
- use establish_connection;
- use super::{User_, UserForm};
- use Crud;
+ use super::*;
#[test]
fn test_crud() {
let conn = establish_connection();
#[cfg(test)]
mod tests {
- use {Settings, is_email_regex, remove_slurs, has_slurs, fuzzy_search};
+ use {Settings, is_email_regex, remove_slurs, has_slurs};
#[test]
fn test_api() {
assert_eq!(Settings::get().api_endpoint(), "rrr/api/v1");
assert!(!has_slurs(slur_free));
}
- #[test] fn test_fuzzy_search() {
- let test = "This is a fuzzy search";
- assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
- }
}
lazy_static! {
use lemmy_server::actix_web::server::HttpServer;
use lemmy_server::actix_web::{ws, App, Error, HttpRequest, HttpResponse, fs::NamedFile, fs};
use lemmy_server::websocket::server::*;
-use lemmy_server::establish_connection;
+use lemmy_server::db::establish_connection;
embed_migrations!();
}
ws::Message::Text(text) => {
let m = text.trim().to_owned();
+ println!("WEBSOCKET MESSAGE: {:?} from id: {}", &m, self.id);
ctx.state()
.addr
Ok(res) => ctx.text(res),
Err(e) => {
eprintln!("{}", &e);
- // ctx.text(e);
}
}
- // Ok(res) => ctx.text(res),
- // // something is wrong with chat server
- // _ => ctx.stop(),
fut::ok(())
})
.wait(ctx);
-
- // we check for /sss type of messages
- // if m.starts_with('/') {
- // let v: Vec<&str> = m.splitn(2, ' ').collect();
- // match v[0] {
- // "/list" => {
- // // Send ListRooms message to chat server and wait for
- // // response
- // println!("List rooms");
- // ctx.state()
- // .addr
- // .send(ListRooms)
- // .into_actor(self)
- // .then(|res, _, ctx| {
- // match res {
- // Ok(rooms) => {
- // for room in rooms {
- // ctx.text(room);
- // }
- // }
- // _ => println!("Something is wrong"),
- // }
- // fut::ok(())
- // })
- // .wait(ctx)
- // .wait(ctx) pauses all events in context,
- // so actor wont receive any new messages until it get list
- // of rooms back
- // }
- // "/join" => {
- // if v.len() == 2 {
- // self.room = v[1].to_owned();
- // ctx.state().addr.do_send(Join {
- // id: self.id,
- // name: self.room.clone(),
- // });
-
- // ctx.text("joined");
- // } else {
- // ctx.text("!!! room name is required");
- // }
- // }
- // "/name" => {
- // if v.len() == 2 {
- // self.name = Some(v[1].to_owned());
- // } else {
- // ctx.text("!!! name is required");
- // }
- // }
- // _ => ctx.text(format!("!!! unknown command: {:?}", m)),
- // }
- // } else {
- // let msg = if let Some(ref name) = self.name {
- // format!("{}: {}", name, m)
- // } else {
- // m.to_owned()
- // };
- // send message to chat server
- // ctx.state().addr.do_send(ClientMessage {
- // id: self.id,
- // msg: msg,
- // room: self.room.clone(),
- // })
- // }
}
ws::Message::Binary(_bin) => println!("Unexpected binary"),
ws::Message::Close(_) => {
impl ChatServer {
/// Send message to all users in the room
- fn send_room_message(&self, room: i32, message: &str, skip_id: usize) {
- if let Some(sessions) = self.rooms.get(&room) {
+ fn send_room_message(&self, room: &i32, message: &str, skip_id: usize) {
+ if let Some(sessions) = self.rooms.get(room) {
for id in sessions {
if *id != skip_id {
if let Some(info) = self.sessions.get(id) {
}
}
- fn join_room(&self, room_id: i32, id: usize) {
+ fn join_room(&mut self, room_id: i32, id: usize) {
// remove session from all rooms
- for (_n, sessions) in &mut self.rooms {
+ for (_n, mut sessions) in &mut self.rooms {
sessions.remove(&id);
}
self.rooms.insert(room_id, HashSet::new());
}
- self.rooms.get_mut(&room_id).unwrap().insert(id);
+ &self.rooms.get_mut(&room_id).unwrap().insert(id);
}
- fn send_community_message(&self, community_id: i32, message: &str, skip_id: usize) -> Result<(), Error> {
+ fn send_community_message(&self, community_id: &i32, message: &str, skip_id: usize) -> Result<(), Error> {
use db::*;
use db::post_view::*;
let conn = establish_connection();
let posts = PostView::list(&conn,
PostListingType::Community,
&SortType::New,
- Some(community_id),
+ Some(*community_id),
None,
None,
None,
None,
Some(9999))?;
for post in posts {
- self.send_room_message(post.id, message, skip_id);
+ self.send_room_message(&post.id, message, skip_id);
}
Ok(())
community_sent.community.user_id = None;
community_sent.community.subscribed = None;
let community_sent_str = serde_json::to_string(&community_sent)?;
- chat.send_community_message(edit_community.edit_id, &community_sent_str, msg.id)?;
+ chat.send_community_message(&community_sent.community.id, &community_sent_str, msg.id)?;
Ok(serde_json::to_string(&res)?)
},
UserOperation::FollowCommunity => {
},
UserOperation::BanFromCommunity => {
let ban_from_community: BanFromCommunity = serde_json::from_str(data)?;
+ let community_id = ban_from_community.community_id;
let res = Oper::new(user_operation, ban_from_community).perform()?;
let res_str = serde_json::to_string(&res)?;
- chat.send_community_message(ban_from_community.community_id, &res_str, msg.id)?;
+ chat.send_community_message(&community_id, &res_str, msg.id)?;
Ok(res_str)
},
UserOperation::AddModToCommunity => {
let mod_add_to_community: AddModToCommunity = serde_json::from_str(data)?;
+ let community_id = mod_add_to_community.community_id;
let res = Oper::new(user_operation, mod_add_to_community).perform()?;
let res_str = serde_json::to_string(&res)?;
- chat.send_community_message(mod_add_to_community.community_id, &res_str, msg.id)?;
+ chat.send_community_message(&community_id, &res_str, msg.id)?;
Ok(res_str)
},
UserOperation::ListCategories => {
let mut post_sent = res.clone();
post_sent.post.my_vote = None;
let post_sent_str = serde_json::to_string(&post_sent)?;
- chat.send_room_message(edit_post.edit_id, &post_sent_str, msg.id);
+ chat.send_room_message(&post_sent.post.id, &post_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
},
UserOperation::SavePost => {
UserOperation::CreateComment => {
chat.check_rate_limit(msg.id)?;
let create_comment: CreateComment = serde_json::from_str(data)?;
+ let post_id = create_comment.post_id;
let res = Oper::new(user_operation, create_comment).perform()?;
let mut comment_sent = res.clone();
comment_sent.comment.my_vote = None;
comment_sent.comment.user_id = None;
let comment_sent_str = serde_json::to_string(&comment_sent)?;
- chat.send_room_message(create_comment.post_id, &comment_sent_str, msg.id);
+ chat.send_room_message(&post_id, &comment_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
},
UserOperation::EditComment => {
let edit_comment: EditComment = serde_json::from_str(data)?;
+ let post_id = edit_comment.post_id;
let res = Oper::new(user_operation, edit_comment).perform()?;
let mut comment_sent = res.clone();
comment_sent.comment.my_vote = None;
comment_sent.comment.user_id = None;
let comment_sent_str = serde_json::to_string(&comment_sent)?;
- chat.send_room_message(edit_comment.post_id, &comment_sent_str, msg.id);
+ chat.send_room_message(&post_id, &comment_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
},
UserOperation::SaveComment => {
UserOperation::CreateCommentLike => {
chat.check_rate_limit(msg.id)?;
let create_comment_like: CreateCommentLike = serde_json::from_str(data)?;
+ let post_id = create_comment_like.post_id;
let res = Oper::new(user_operation, create_comment_like).perform()?;
let mut comment_sent = res.clone();
comment_sent.comment.my_vote = None;
comment_sent.comment.user_id = None;
let comment_sent_str = serde_json::to_string(&comment_sent)?;
- chat.send_room_message(create_comment_like.post_id, &comment_sent_str, msg.id);
+ chat.send_room_message(&post_id, &comment_sent_str, msg.id);
Ok(serde_json::to_string(&res)?)
},
UserOperation::GetModlog => {