2019-03-21 01:22:31 +00:00
|
|
|
//! `ChatServer` is an actor. It maintains list of connection client session.
|
|
|
|
//! And manages available rooms. Peers send messages to other peers in same
|
|
|
|
//! room through `ChatServer`.
|
|
|
|
|
|
|
|
use actix::prelude::*;
|
|
|
|
use rand::{rngs::ThreadRng, Rng};
|
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2019-03-29 04:56:23 +00:00
|
|
|
use serde_json::{Value};
|
2019-03-23 01:42:57 +00:00
|
|
|
use bcrypt::{verify};
|
2019-03-25 03:51:27 +00:00
|
|
|
use std::str::FromStr;
|
2019-04-15 23:12:06 +00:00
|
|
|
use diesel::PgConnection;
|
2019-04-21 07:26:26 +00:00
|
|
|
use failure::Error;
|
2019-05-01 22:44:39 +00:00
|
|
|
use std::time::{SystemTime};
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-04-23 22:05:50 +00:00
|
|
|
use {Crud, Joinable, Likeable, Followable, Bannable, Saveable, establish_connection, naive_now, naive_from_unix, SortType, SearchType, has_slurs, remove_slurs};
|
2019-03-23 01:42:57 +00:00
|
|
|
use actions::community::*;
|
2019-03-25 03:51:27 +00:00
|
|
|
use actions::user::*;
|
2019-03-26 18:00:18 +00:00
|
|
|
use actions::post::*;
|
|
|
|
use actions::comment::*;
|
2019-04-03 06:49:32 +00:00
|
|
|
use actions::post_view::*;
|
2019-04-03 20:59:37 +00:00
|
|
|
use actions::comment_view::*;
|
2019-04-03 23:01:20 +00:00
|
|
|
use actions::category::*;
|
|
|
|
use actions::community_view::*;
|
2019-04-08 05:19:02 +00:00
|
|
|
use actions::user_view::*;
|
2019-04-15 23:12:06 +00:00
|
|
|
use actions::moderator_views::*;
|
|
|
|
use actions::moderator::*;
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
const RATE_LIMIT_MESSAGES: i32 = 30;
|
|
|
|
const RATE_LIMIT_PER_SECOND: i32 = 60;
|
|
|
|
const RATE_LIMIT_REGISTER_MESSAGES: i32 = 1;
|
|
|
|
const RATE_LIMIT_REGISTER_PER_SECOND: i32 = 60;
|
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
#[derive(EnumString,ToString,Debug)]
|
|
|
|
pub enum UserOperation {
|
2019-04-29 16:51:13 +00:00
|
|
|
Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, SaveComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, SavePost, EditCommunity, FollowCommunity, GetFollowedCommunities, GetUserDetails, GetReplies, GetModlog, BanFromCommunity, AddModToCommunity, CreateSite, EditSite, GetSite, AddAdmin, BanUser, Search, MarkAllAsRead
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
#[derive(Fail, Debug)]
|
|
|
|
#[fail(display = "{{\"op\":\"{}\", \"error\":\"{}\"}}", op, message)]
|
2019-03-23 01:42:57 +00:00
|
|
|
pub struct ErrorMessage {
|
|
|
|
op: String,
|
2019-04-21 07:26:26 +00:00
|
|
|
message: String
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
2019-03-21 01:22:31 +00:00
|
|
|
|
|
|
|
/// Chat server sends this messages to session
|
|
|
|
#[derive(Message)]
|
|
|
|
pub struct WSMessage(pub String);
|
|
|
|
|
|
|
|
/// Message for chat server communications
|
|
|
|
|
|
|
|
/// New chat session is created
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(usize)]
|
|
|
|
pub struct Connect {
|
|
|
|
pub addr: Recipient<WSMessage>,
|
2019-05-01 22:44:39 +00:00
|
|
|
pub ip: String,
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Session is disconnected
|
|
|
|
#[derive(Message)]
|
|
|
|
pub struct Disconnect {
|
|
|
|
pub id: usize,
|
2019-05-01 22:44:39 +00:00
|
|
|
pub ip: String,
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send message to specific room
|
|
|
|
#[derive(Message)]
|
|
|
|
pub struct ClientMessage {
|
|
|
|
/// Id of the client session
|
|
|
|
pub id: usize,
|
|
|
|
/// Peer message
|
|
|
|
pub msg: String,
|
|
|
|
/// Room name
|
|
|
|
pub room: String,
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:51:27 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct StandardMessage {
|
|
|
|
/// Id of the client session
|
|
|
|
pub id: usize,
|
|
|
|
/// Peer message
|
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl actix::Message for StandardMessage {
|
|
|
|
type Result = String;
|
|
|
|
}
|
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Login {
|
2019-03-23 01:42:57 +00:00
|
|
|
pub username_or_email: String,
|
2019-03-21 01:22:31 +00:00
|
|
|
pub password: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Register {
|
|
|
|
username: String,
|
|
|
|
email: Option<String>,
|
|
|
|
password: String,
|
2019-04-16 23:04:23 +00:00
|
|
|
password_verify: String,
|
|
|
|
admin: bool,
|
2019-04-26 16:21:20 +00:00
|
|
|
spam_timeri: i64,
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct LoginResponse {
|
|
|
|
op: String,
|
|
|
|
jwt: String
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:51:27 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct CreateCommunity {
|
|
|
|
name: String,
|
2019-04-03 20:59:37 +00:00
|
|
|
title: String,
|
|
|
|
description: Option<String>,
|
|
|
|
category_id: i32 ,
|
2019-03-26 18:00:18 +00:00
|
|
|
auth: String
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2019-04-04 22:29:14 +00:00
|
|
|
pub struct CommunityResponse {
|
2019-03-23 01:42:57 +00:00
|
|
|
op: String,
|
2019-04-03 23:01:20 +00:00
|
|
|
community: CommunityView
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2019-04-05 06:26:38 +00:00
|
|
|
pub struct ListCommunities {
|
2019-04-10 06:19:12 +00:00
|
|
|
sort: String,
|
2019-04-17 18:30:13 +00:00
|
|
|
page: Option<i64>,
|
2019-04-10 06:19:12 +00:00
|
|
|
limit: Option<i64>,
|
2019-04-05 06:26:38 +00:00
|
|
|
auth: Option<String>
|
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct ListCommunitiesResponse {
|
|
|
|
op: String,
|
2019-04-03 23:01:20 +00:00
|
|
|
communities: Vec<CommunityView>
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct ListCategories;
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct ListCategoriesResponse {
|
|
|
|
op: String,
|
|
|
|
categories: Vec<Category>
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct CreatePost {
|
|
|
|
name: String,
|
|
|
|
url: Option<String>,
|
|
|
|
body: Option<String>,
|
|
|
|
community_id: i32,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2019-04-03 20:59:37 +00:00
|
|
|
pub struct PostResponse {
|
2019-03-26 18:00:18 +00:00
|
|
|
op: String,
|
2019-04-03 20:59:37 +00:00
|
|
|
post: PostView
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetPost {
|
2019-03-28 19:32:08 +00:00
|
|
|
id: i32,
|
|
|
|
auth: Option<String>
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetPostResponse {
|
|
|
|
op: String,
|
2019-04-03 06:49:32 +00:00
|
|
|
post: PostView,
|
2019-04-03 23:01:20 +00:00
|
|
|
comments: Vec<CommentView>,
|
2019-04-04 20:53:32 +00:00
|
|
|
community: CommunityView,
|
2019-04-20 04:06:25 +00:00
|
|
|
moderators: Vec<CommunityModeratorView>,
|
|
|
|
admins: Vec<UserView>,
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetPosts {
|
|
|
|
type_: String,
|
|
|
|
sort: String,
|
2019-04-17 18:30:13 +00:00
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
2019-04-03 06:49:32 +00:00
|
|
|
community_id: Option<i32>,
|
|
|
|
auth: Option<String>
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetPostsResponse {
|
|
|
|
op: String,
|
|
|
|
posts: Vec<PostView>,
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetCommunity {
|
2019-04-25 21:52:18 +00:00
|
|
|
id: Option<i32>,
|
|
|
|
name: Option<String>,
|
2019-04-05 06:26:38 +00:00
|
|
|
auth: Option<String>
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetCommunityResponse {
|
|
|
|
op: String,
|
2019-04-04 20:53:32 +00:00
|
|
|
community: CommunityView,
|
2019-04-21 20:52:55 +00:00
|
|
|
moderators: Vec<CommunityModeratorView>,
|
|
|
|
admins: Vec<UserView>,
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct CreateComment {
|
|
|
|
content: String,
|
|
|
|
parent_id: Option<i32>,
|
2019-03-29 04:56:23 +00:00
|
|
|
edit_id: Option<i32>,
|
2019-03-26 18:00:18 +00:00
|
|
|
post_id: i32,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct EditComment {
|
|
|
|
content: String,
|
|
|
|
parent_id: Option<i32>,
|
|
|
|
edit_id: i32,
|
2019-04-15 23:12:06 +00:00
|
|
|
creator_id: i32,
|
2019-03-29 04:56:23 +00:00
|
|
|
post_id: i32,
|
2019-04-15 23:12:06 +00:00
|
|
|
removed: Option<bool>,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: Option<bool>,
|
2019-04-15 23:12:06 +00:00
|
|
|
reason: Option<String>,
|
2019-04-20 18:17:00 +00:00
|
|
|
read: Option<bool>,
|
2019-03-29 04:56:23 +00:00
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct SaveComment {
|
|
|
|
comment_id: i32,
|
|
|
|
save: bool,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2019-04-03 20:59:37 +00:00
|
|
|
pub struct CommentResponse {
|
2019-03-29 04:56:23 +00:00
|
|
|
op: String,
|
|
|
|
comment: CommentView
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct CreateCommentLike {
|
|
|
|
comment_id: i32,
|
|
|
|
post_id: i32,
|
|
|
|
score: i16,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct CreatePostLike {
|
|
|
|
post_id: i32,
|
|
|
|
score: i16,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct CreatePostLikeResponse {
|
|
|
|
op: String,
|
|
|
|
post: PostView
|
|
|
|
}
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct EditPost {
|
|
|
|
edit_id: i32,
|
2019-04-15 23:12:06 +00:00
|
|
|
creator_id: i32,
|
2019-04-03 20:59:37 +00:00
|
|
|
community_id: i32,
|
|
|
|
name: String,
|
|
|
|
url: Option<String>,
|
|
|
|
body: Option<String>,
|
2019-04-20 07:24:59 +00:00
|
|
|
removed: Option<bool>,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: Option<bool>,
|
2019-04-20 07:24:59 +00:00
|
|
|
locked: Option<bool>,
|
2019-04-15 23:12:06 +00:00
|
|
|
reason: Option<String>,
|
2019-04-20 04:06:25 +00:00
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct SavePost {
|
|
|
|
post_id: i32,
|
|
|
|
save: bool,
|
2019-04-03 20:59:37 +00:00
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct EditCommunity {
|
|
|
|
edit_id: i32,
|
2019-04-04 22:29:14 +00:00
|
|
|
name: String,
|
2019-04-03 20:59:37 +00:00
|
|
|
title: String,
|
|
|
|
description: Option<String>,
|
|
|
|
category_id: i32,
|
2019-04-20 07:24:59 +00:00
|
|
|
removed: Option<bool>,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: Option<bool>,
|
2019-04-15 23:12:06 +00:00
|
|
|
reason: Option<String>,
|
|
|
|
expires: Option<i64>,
|
2019-04-03 20:59:37 +00:00
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
2019-04-05 06:26:38 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct FollowCommunity {
|
|
|
|
community_id: i32,
|
|
|
|
follow: bool,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
2019-04-05 19:14:54 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetFollowedCommunities {
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetFollowedCommunitiesResponse {
|
|
|
|
op: String,
|
|
|
|
communities: Vec<CommunityFollowerView>
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetUserDetails {
|
2019-04-25 21:52:18 +00:00
|
|
|
user_id: Option<i32>,
|
|
|
|
username: Option<String>,
|
2019-04-08 05:19:02 +00:00
|
|
|
sort: String,
|
2019-04-17 18:30:13 +00:00
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
2019-04-08 05:19:02 +00:00
|
|
|
community_id: Option<i32>,
|
2019-04-20 04:06:25 +00:00
|
|
|
saved_only: bool,
|
2019-04-29 21:19:00 +00:00
|
|
|
auth: Option<String>,
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetUserDetailsResponse {
|
|
|
|
op: String,
|
|
|
|
user: UserView,
|
|
|
|
follows: Vec<CommunityFollowerView>,
|
|
|
|
moderates: Vec<CommunityModeratorView>,
|
|
|
|
comments: Vec<CommentView>,
|
|
|
|
posts: Vec<PostView>,
|
|
|
|
}
|
2019-04-05 19:14:54 +00:00
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetModlog {
|
|
|
|
mod_user_id: Option<i32>,
|
|
|
|
community_id: Option<i32>,
|
|
|
|
page: Option<i64>,
|
2019-04-17 18:30:13 +00:00
|
|
|
limit: Option<i64>,
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetModlogResponse {
|
|
|
|
op: String,
|
|
|
|
removed_posts: Vec<ModRemovePostView>,
|
|
|
|
locked_posts: Vec<ModLockPostView>,
|
|
|
|
removed_comments: Vec<ModRemoveCommentView>,
|
|
|
|
removed_communities: Vec<ModRemoveCommunityView>,
|
|
|
|
banned_from_community: Vec<ModBanFromCommunityView>,
|
|
|
|
banned: Vec<ModBanView>,
|
|
|
|
added_to_community: Vec<ModAddCommunityView>,
|
|
|
|
added: Vec<ModAddView>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct BanFromCommunity {
|
|
|
|
community_id: i32,
|
|
|
|
user_id: i32,
|
|
|
|
ban: bool,
|
|
|
|
reason: Option<String>,
|
|
|
|
expires: Option<i64>,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct BanFromCommunityResponse {
|
|
|
|
op: String,
|
|
|
|
user: UserView,
|
|
|
|
banned: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct AddModToCommunity {
|
|
|
|
community_id: i32,
|
|
|
|
user_id: i32,
|
|
|
|
added: bool,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct AddModToCommunityResponse {
|
|
|
|
op: String,
|
|
|
|
moderators: Vec<CommunityModeratorView>,
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct CreateSite {
|
|
|
|
name: String,
|
|
|
|
description: Option<String>,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct EditSite {
|
|
|
|
name: String,
|
|
|
|
description: Option<String>,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetSite {
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct SiteResponse {
|
|
|
|
op: String,
|
|
|
|
site: SiteView,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetSiteResponse {
|
|
|
|
op: String,
|
|
|
|
site: Option<SiteView>,
|
|
|
|
admins: Vec<UserView>,
|
|
|
|
banned: Vec<UserView>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct AddAdmin {
|
|
|
|
user_id: i32,
|
|
|
|
added: bool,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct AddAdminResponse {
|
|
|
|
op: String,
|
|
|
|
admins: Vec<UserView>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct BanUser {
|
|
|
|
user_id: i32,
|
|
|
|
ban: bool,
|
|
|
|
reason: Option<String>,
|
|
|
|
expires: Option<i64>,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct BanUserResponse {
|
|
|
|
op: String,
|
|
|
|
user: UserView,
|
|
|
|
banned: bool,
|
|
|
|
}
|
|
|
|
|
2019-04-20 18:17:00 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetReplies {
|
|
|
|
sort: String,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
unread_only: bool,
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetRepliesResponse {
|
|
|
|
op: String,
|
|
|
|
replies: Vec<ReplyView>,
|
|
|
|
}
|
|
|
|
|
2019-04-23 22:05:50 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Search {
|
|
|
|
q: String,
|
|
|
|
type_: String,
|
|
|
|
community_id: Option<i32>,
|
|
|
|
sort: String,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct SearchResponse {
|
|
|
|
op: String,
|
|
|
|
comments: Vec<CommentView>,
|
|
|
|
posts: Vec<PostView>,
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:51:13 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct MarkAllAsRead {
|
|
|
|
auth: String
|
|
|
|
}
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RateLimitBucket {
|
|
|
|
last_checked: SystemTime,
|
|
|
|
allowance: f64
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SessionInfo {
|
|
|
|
pub addr: Recipient<WSMessage>,
|
|
|
|
pub ip: String,
|
|
|
|
}
|
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
/// `ChatServer` manages chat rooms and responsible for coordinating chat
|
|
|
|
/// session. implementation is super primitive
|
|
|
|
pub struct ChatServer {
|
2019-05-01 22:44:39 +00:00
|
|
|
sessions: HashMap<usize, SessionInfo>, // A map from generated random ID to session addr
|
|
|
|
rate_limits: HashMap<String, RateLimitBucket>,
|
2019-03-27 16:59:21 +00:00
|
|
|
rooms: HashMap<i32, HashSet<usize>>, // A map from room / post name to set of connectionIDs
|
2019-03-21 01:22:31 +00:00
|
|
|
rng: ThreadRng,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ChatServer {
|
|
|
|
fn default() -> ChatServer {
|
|
|
|
// default room
|
2019-03-27 16:59:21 +00:00
|
|
|
let rooms = HashMap::new();
|
2019-03-21 01:22:31 +00:00
|
|
|
|
|
|
|
ChatServer {
|
|
|
|
sessions: HashMap::new(),
|
2019-05-01 22:44:39 +00:00
|
|
|
rate_limits: HashMap::new(),
|
2019-03-21 01:22:31 +00:00
|
|
|
rooms: rooms,
|
|
|
|
rng: rand::thread_rng(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChatServer {
|
|
|
|
/// Send message to all users in the room
|
2019-03-27 16:59:21 +00:00
|
|
|
fn send_room_message(&self, room: i32, message: &str, skip_id: usize) {
|
|
|
|
if let Some(sessions) = self.rooms.get(&room) {
|
2019-03-21 01:22:31 +00:00
|
|
|
for id in sessions {
|
|
|
|
if *id != skip_id {
|
2019-05-01 22:44:39 +00:00
|
|
|
if let Some(info) = self.sessions.get(id) {
|
|
|
|
let _ = info.addr.do_send(WSMessage(message.to_owned()));
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn send_community_message(&self, conn: &PgConnection, community_id: i32, message: &str, skip_id: usize) -> Result<(), Error> {
|
2019-04-15 23:12:06 +00:00
|
|
|
let posts = PostView::list(conn,
|
|
|
|
PostListingType::Community,
|
|
|
|
&SortType::New,
|
|
|
|
Some(community_id),
|
|
|
|
None,
|
|
|
|
None,
|
2019-04-23 22:05:50 +00:00
|
|
|
None,
|
2019-04-20 04:06:25 +00:00
|
|
|
false,
|
|
|
|
false,
|
2019-04-17 18:30:13 +00:00
|
|
|
None,
|
2019-04-21 07:26:26 +00:00
|
|
|
Some(9999))?;
|
2019-04-15 23:12:06 +00:00
|
|
|
for post in posts {
|
|
|
|
self.send_room_message(post.id, message, skip_id);
|
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
|
|
|
|
Ok(())
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
2019-05-01 22:44:39 +00:00
|
|
|
|
|
|
|
fn check_rate_limit_register(&mut self, addr: usize) -> Result<(), Error> {
|
|
|
|
self.check_rate_limit_full(addr, RATE_LIMIT_REGISTER_MESSAGES, RATE_LIMIT_REGISTER_PER_SECOND)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_rate_limit(&mut self, addr: usize) -> Result<(), Error> {
|
|
|
|
self.check_rate_limit_full(addr, RATE_LIMIT_MESSAGES, RATE_LIMIT_PER_SECOND)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_rate_limit_full(&mut self, addr: usize, rate: i32, per: i32) -> Result<(), Error> {
|
|
|
|
if let Some(info) = self.sessions.get(&addr) {
|
|
|
|
if let Some(rate_limit) = self.rate_limits.get_mut(&info.ip) {
|
|
|
|
if rate_limit.allowance == -2f64 {
|
|
|
|
rate_limit.allowance = rate as f64;
|
|
|
|
};
|
|
|
|
|
|
|
|
let current = SystemTime::now();
|
|
|
|
let time_passed = current.duration_since(rate_limit.last_checked)?.as_secs() as f64;
|
|
|
|
rate_limit.last_checked = current;
|
|
|
|
rate_limit.allowance += time_passed * (rate as f64 / per as f64);
|
|
|
|
if rate_limit.allowance > rate as f64 {
|
|
|
|
rate_limit.allowance = rate as f64;
|
|
|
|
}
|
|
|
|
|
|
|
|
if rate_limit.allowance < 1.0 {
|
|
|
|
println!("Rate limited IP: {}, time_passed: {}, allowance: {}", &info.ip, time_passed, rate_limit.allowance);
|
|
|
|
Err(ErrorMessage {
|
|
|
|
op: "Rate Limit".to_string(),
|
|
|
|
message: format!("Too many requests. {} per {} seconds", rate, per),
|
|
|
|
})?
|
|
|
|
} else {
|
|
|
|
rate_limit.allowance -= 1.0;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
/// Make actor from `ChatServer`
|
|
|
|
impl Actor for ChatServer {
|
|
|
|
/// We are going to use simple Context, we just need ability to communicate
|
|
|
|
/// with other actors.
|
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handler for Connect message.
|
|
|
|
///
|
|
|
|
/// Register new session and assign unique id to this session
|
|
|
|
impl Handler<Connect> for ChatServer {
|
|
|
|
type Result = usize;
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
fn handle(&mut self, msg: Connect, _ctx: &mut Context<Self>) -> Self::Result {
|
2019-03-21 01:22:31 +00:00
|
|
|
|
|
|
|
// notify all users in same room
|
2019-03-27 16:59:21 +00:00
|
|
|
// self.send_room_message(&"Main".to_owned(), "Someone joined", 0);
|
2019-03-21 01:22:31 +00:00
|
|
|
|
|
|
|
// register session with random id
|
|
|
|
let id = self.rng.gen::<usize>();
|
2019-05-01 22:44:39 +00:00
|
|
|
println!("{} Joined", &msg.ip);
|
|
|
|
|
|
|
|
self.sessions.insert(id, SessionInfo {
|
|
|
|
addr: msg.addr,
|
|
|
|
ip: msg.ip.to_owned(),
|
|
|
|
});
|
|
|
|
|
|
|
|
if self.rate_limits.get(&msg.ip).is_none() {
|
|
|
|
self.rate_limits.insert(msg.ip, RateLimitBucket {
|
|
|
|
last_checked: SystemTime::now(),
|
|
|
|
allowance: -2f64,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// for (k,v) in &self.rate_limits {
|
|
|
|
// println!("{}: {:?}", k,v);
|
|
|
|
// }
|
2019-03-21 01:22:31 +00:00
|
|
|
|
|
|
|
// auto join session to Main room
|
2019-03-27 16:59:21 +00:00
|
|
|
// self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id);
|
2019-03-21 01:22:31 +00:00
|
|
|
|
|
|
|
// send id back
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
/// Handler for Disconnect message.
|
|
|
|
impl Handler<Disconnect> for ChatServer {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: Disconnect, _: &mut Context<Self>) {
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
// let mut rooms: Vec<i32> = Vec::new();
|
2019-03-21 01:22:31 +00:00
|
|
|
|
|
|
|
// remove address
|
|
|
|
if self.sessions.remove(&msg.id).is_some() {
|
|
|
|
// remove session from all rooms
|
2019-04-03 20:59:37 +00:00
|
|
|
for (_id, sessions) in &mut self.rooms {
|
2019-03-21 01:22:31 +00:00
|
|
|
if sessions.remove(&msg.id) {
|
2019-03-27 16:59:21 +00:00
|
|
|
// rooms.push(*id);
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:51:27 +00:00
|
|
|
/// Handler for Message message.
|
|
|
|
impl Handler<StandardMessage> for ChatServer {
|
|
|
|
type Result = MessageResult<StandardMessage>;
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
|
2019-03-25 03:51:27 +00:00
|
|
|
fn handle(&mut self, msg: StandardMessage, _: &mut Context<Self>) -> Self::Result {
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let msg_out = match parse_json_message(self, msg) {
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(e) => e.to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
MessageResult(msg_out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<String, Error> {
|
|
|
|
|
|
|
|
let json: Value = serde_json::from_str(&msg.msg)?;
|
|
|
|
let data = &json["data"].to_string();
|
|
|
|
let op = &json["op"].as_str().unwrap();
|
|
|
|
|
|
|
|
let user_operation: UserOperation = UserOperation::from_str(&op)?;
|
|
|
|
|
|
|
|
match user_operation {
|
|
|
|
UserOperation::Login => {
|
|
|
|
let login: Login = serde_json::from_str(data)?;
|
|
|
|
login.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::Register => {
|
|
|
|
let register: Register = serde_json::from_str(data)?;
|
|
|
|
register.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::CreateCommunity => {
|
|
|
|
let create_community: CreateCommunity = serde_json::from_str(data)?;
|
|
|
|
create_community.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::ListCommunities => {
|
|
|
|
let list_communities: ListCommunities = serde_json::from_str(data)?;
|
|
|
|
list_communities.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::ListCategories => {
|
|
|
|
let list_categories: ListCategories = ListCategories;
|
|
|
|
list_categories.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::CreatePost => {
|
|
|
|
let create_post: CreatePost = serde_json::from_str(data)?;
|
|
|
|
create_post.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::GetPost => {
|
|
|
|
let get_post: GetPost = serde_json::from_str(data)?;
|
|
|
|
get_post.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::GetCommunity => {
|
|
|
|
let get_community: GetCommunity = serde_json::from_str(data)?;
|
|
|
|
get_community.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::CreateComment => {
|
|
|
|
let create_comment: CreateComment = serde_json::from_str(data)?;
|
|
|
|
create_comment.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::EditComment => {
|
|
|
|
let edit_comment: EditComment = serde_json::from_str(data)?;
|
|
|
|
edit_comment.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::SaveComment => {
|
|
|
|
let save_post: SaveComment = serde_json::from_str(data)?;
|
|
|
|
save_post.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::CreateCommentLike => {
|
|
|
|
let create_comment_like: CreateCommentLike = serde_json::from_str(data)?;
|
|
|
|
create_comment_like.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::GetPosts => {
|
|
|
|
let get_posts: GetPosts = serde_json::from_str(data)?;
|
|
|
|
get_posts.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::CreatePostLike => {
|
|
|
|
let create_post_like: CreatePostLike = serde_json::from_str(data)?;
|
|
|
|
create_post_like.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::EditPost => {
|
|
|
|
let edit_post: EditPost = serde_json::from_str(data)?;
|
|
|
|
edit_post.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::SavePost => {
|
|
|
|
let save_post: SavePost = serde_json::from_str(data)?;
|
|
|
|
save_post.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::EditCommunity => {
|
|
|
|
let edit_community: EditCommunity = serde_json::from_str(data)?;
|
|
|
|
edit_community.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::FollowCommunity => {
|
|
|
|
let follow_community: FollowCommunity = serde_json::from_str(data)?;
|
|
|
|
follow_community.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::GetFollowedCommunities => {
|
|
|
|
let followed_communities: GetFollowedCommunities = serde_json::from_str(data)?;
|
|
|
|
followed_communities.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::GetUserDetails => {
|
|
|
|
let get_user_details: GetUserDetails = serde_json::from_str(data)?;
|
|
|
|
get_user_details.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::GetModlog => {
|
|
|
|
let get_modlog: GetModlog = serde_json::from_str(data)?;
|
|
|
|
get_modlog.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::BanFromCommunity => {
|
|
|
|
let ban_from_community: BanFromCommunity = serde_json::from_str(data)?;
|
|
|
|
ban_from_community.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::AddModToCommunity => {
|
|
|
|
let mod_add_to_community: AddModToCommunity = serde_json::from_str(data)?;
|
|
|
|
mod_add_to_community.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::CreateSite => {
|
|
|
|
let create_site: CreateSite = serde_json::from_str(data)?;
|
|
|
|
create_site.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::EditSite => {
|
|
|
|
let edit_site: EditSite = serde_json::from_str(data)?;
|
|
|
|
edit_site.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::GetSite => {
|
|
|
|
let get_site: GetSite = serde_json::from_str(data)?;
|
|
|
|
get_site.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::AddAdmin => {
|
|
|
|
let add_admin: AddAdmin = serde_json::from_str(data)?;
|
|
|
|
add_admin.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::BanUser => {
|
|
|
|
let ban_user: BanUser = serde_json::from_str(data)?;
|
|
|
|
ban_user.perform(chat, msg.id)
|
|
|
|
},
|
|
|
|
UserOperation::GetReplies => {
|
|
|
|
let get_replies: GetReplies = serde_json::from_str(data)?;
|
|
|
|
get_replies.perform(chat, msg.id)
|
|
|
|
},
|
2019-04-23 22:05:50 +00:00
|
|
|
UserOperation::Search => {
|
|
|
|
let search: Search = serde_json::from_str(data)?;
|
|
|
|
search.perform(chat, msg.id)
|
|
|
|
},
|
2019-04-29 16:51:13 +00:00
|
|
|
UserOperation::MarkAllAsRead => {
|
|
|
|
let mark_all_as_read: MarkAllAsRead = serde_json::from_str(data)?;
|
|
|
|
mark_all_as_read.perform(chat, msg.id)
|
|
|
|
},
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:51:27 +00:00
|
|
|
pub trait Perform {
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error>;
|
2019-03-26 18:00:18 +00:00
|
|
|
fn op_type(&self) -> UserOperation;
|
2019-04-21 07:26:26 +00:00
|
|
|
fn error(&self, error_msg: &str) -> ErrorMessage {
|
|
|
|
ErrorMessage {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
message: error_msg.to_string()
|
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
2019-03-25 03:51:27 +00:00
|
|
|
}
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-03-25 03:51:27 +00:00
|
|
|
impl Perform for Login {
|
2019-04-21 07:26:26 +00:00
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::Login
|
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
|
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-03-23 01:42:57 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
// Fetch that username / email
|
2019-03-25 03:51:27 +00:00
|
|
|
let user: User_ = match User_::find_by_email_or_username(&conn, &self.username_or_email) {
|
2019-03-23 01:42:57 +00:00
|
|
|
Ok(user) => user,
|
2019-04-21 07:26:26 +00:00
|
|
|
Err(_e) => return Err(self.error("Couldn't find that username or email"))?
|
2019-03-23 01:42:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Verify the password
|
2019-03-25 03:51:27 +00:00
|
|
|
let valid: bool = verify(&self.password, &user.password_encrypted).unwrap_or(false);
|
2019-03-23 01:42:57 +00:00
|
|
|
if !valid {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Password incorrect"))?
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the jwt
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&LoginResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
jwt: user.jwt()
|
|
|
|
}
|
|
|
|
)?
|
2019-03-23 01:42:57 +00:00
|
|
|
)
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:51:27 +00:00
|
|
|
impl Perform for Register {
|
2019-03-26 18:00:18 +00:00
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::Register
|
|
|
|
}
|
2019-05-01 22:44:39 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-03-21 01:22:31 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
chat.check_rate_limit_register(addr)?;
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
// Make sure passwords match
|
2019-03-25 03:51:27 +00:00
|
|
|
if &self.password != &self.password_verify {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Passwords do not match."))?
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-04-26 16:21:20 +00:00
|
|
|
if self.spam_timeri < 1142 {
|
2019-04-26 14:34:04 +00:00
|
|
|
return Err(self.error("Too fast"))?
|
|
|
|
}
|
|
|
|
|
2019-04-09 18:35:16 +00:00
|
|
|
if has_slurs(&self.username) {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("No slurs"))?
|
2019-04-09 18:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
// Make sure there are no admins
|
2019-04-21 07:26:26 +00:00
|
|
|
if self.admin && UserView::admins(&conn)?.len() > 0 {
|
|
|
|
return Err(self.error("Sorry, there's already an admin."))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
// Register the new user
|
|
|
|
let user_form = UserForm {
|
2019-03-25 03:51:27 +00:00
|
|
|
name: self.username.to_owned(),
|
2019-04-03 06:49:32 +00:00
|
|
|
fedi_name: "rrf".into(),
|
2019-03-25 03:51:27 +00:00
|
|
|
email: self.email.to_owned(),
|
|
|
|
password_encrypted: self.password.to_owned(),
|
2019-03-21 01:22:31 +00:00
|
|
|
preferred_username: None,
|
2019-04-15 23:12:06 +00:00
|
|
|
updated: None,
|
2019-04-16 23:04:23 +00:00
|
|
|
admin: self.admin,
|
|
|
|
banned: false,
|
2019-03-21 01:22:31 +00:00
|
|
|
};
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
// Create the user
|
2019-04-16 23:04:23 +00:00
|
|
|
let inserted_user = match User_::register(&conn, &user_form) {
|
2019-03-23 01:42:57 +00:00
|
|
|
Ok(user) => user,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("User already exists."))?
|
2019-03-25 03:51:27 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
};
|
2019-03-21 01:22:31 +00:00
|
|
|
|
2019-04-25 18:34:58 +00:00
|
|
|
// Sign them up for main community no matter what
|
|
|
|
let community_follower_form = CommunityFollowerForm {
|
|
|
|
community_id: 1,
|
|
|
|
user_id: inserted_user.id,
|
|
|
|
};
|
|
|
|
|
|
|
|
let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_e) => {
|
|
|
|
return Err(self.error("Community follower already exists."))?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// If its an admin, add them as a mod and follower to main
|
2019-04-16 23:04:23 +00:00
|
|
|
if self.admin {
|
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
|
|
|
community_id: 1,
|
2019-04-20 04:06:25 +00:00
|
|
|
user_id: inserted_user.id,
|
2019-04-16 23:04:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Community moderator already exists."))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
};
|
2019-04-20 04:06:25 +00:00
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 01:22:31 +00:00
|
|
|
// Return the jwt
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&LoginResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
jwt: inserted_user.jwt()
|
|
|
|
}
|
|
|
|
)?
|
2019-03-23 01:42:57 +00:00
|
|
|
)
|
2019-03-21 01:22:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
impl Perform for CreateCommunity {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::CreateCommunity
|
|
|
|
}
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-03-25 03:51:27 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
chat.check_rate_limit_register(addr)?;
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
let claims = match Claims::decode(&self.auth) {
|
2019-03-25 03:51:27 +00:00
|
|
|
Ok(claims) => claims.claims,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-03-25 03:51:27 +00:00
|
|
|
}
|
|
|
|
};
|
2019-03-23 01:42:57 +00:00
|
|
|
|
2019-04-09 18:35:16 +00:00
|
|
|
if has_slurs(&self.name) ||
|
|
|
|
has_slurs(&self.title) ||
|
2019-04-21 07:26:26 +00:00
|
|
|
(self.description.is_some() && has_slurs(&self.description.to_owned().unwrap())) {
|
|
|
|
return Err(self.error("No slurs"))?
|
|
|
|
}
|
2019-04-09 18:35:16 +00:00
|
|
|
|
2019-03-25 03:51:27 +00:00
|
|
|
let user_id = claims.id;
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a site ban
|
2019-04-21 07:26:26 +00:00
|
|
|
if UserView::read(&conn, user_id)?.banned {
|
|
|
|
return Err(self.error("You have been banned from the site"))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
// When you create a community, make sure the user becomes a moderator and a follower
|
2019-03-25 03:51:27 +00:00
|
|
|
let community_form = CommunityForm {
|
|
|
|
name: self.name.to_owned(),
|
2019-04-03 20:59:37 +00:00
|
|
|
title: self.title.to_owned(),
|
|
|
|
description: self.description.to_owned(),
|
|
|
|
category_id: self.category_id,
|
2019-04-03 06:49:32 +00:00
|
|
|
creator_id: user_id,
|
2019-04-20 07:24:59 +00:00
|
|
|
removed: None,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: None,
|
2019-04-15 23:12:06 +00:00
|
|
|
updated: None,
|
2019-03-25 03:51:27 +00:00
|
|
|
};
|
2019-03-23 01:42:57 +00:00
|
|
|
|
2019-03-25 03:51:27 +00:00
|
|
|
let inserted_community = match Community::create(&conn, &community_form) {
|
2019-03-23 01:42:57 +00:00
|
|
|
Ok(community) => community,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Community already exists."))?
|
2019-03-25 03:51:27 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
};
|
2019-03-25 03:51:27 +00:00
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
2019-03-25 03:51:27 +00:00
|
|
|
community_id: inserted_community.id,
|
2019-04-03 06:49:32 +00:00
|
|
|
user_id: user_id
|
2019-03-25 03:51:27 +00:00
|
|
|
};
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
|
2019-03-25 03:51:27 +00:00
|
|
|
Ok(user) => user,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Community moderator already exists."))?
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let community_follower_form = CommunityFollowerForm {
|
|
|
|
community_id: inserted_community.id,
|
|
|
|
user_id: user_id
|
|
|
|
};
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) {
|
2019-04-03 06:49:32 +00:00
|
|
|
Ok(user) => user,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Community follower already exists."))?
|
2019-03-25 03:51:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let community_view = CommunityView::read(&conn, inserted_community.id, Some(user_id))?;
|
2019-04-03 23:01:20 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&CommunityResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
community: community_view
|
|
|
|
}
|
|
|
|
)?
|
2019-03-26 18:00:18 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform for ListCommunities {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::ListCommunities
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-04-05 06:26:38 +00:00
|
|
|
let user_id: Option<i32> = match &self.auth {
|
|
|
|
Some(auth) => {
|
|
|
|
match Claims::decode(&auth) {
|
|
|
|
Ok(claims) => {
|
|
|
|
let user_id = claims.claims.id;
|
|
|
|
Some(user_id)
|
|
|
|
}
|
|
|
|
Err(_e) => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => None
|
|
|
|
};
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let sort = SortType::from_str(&self.sort)?;
|
2019-04-10 06:19:12 +00:00
|
|
|
|
2019-04-26 05:50:42 +00:00
|
|
|
let communities: Vec<CommunityView> = CommunityView::list(&conn, user_id, sort, self.page, self.limit)?;
|
2019-03-25 03:51:27 +00:00
|
|
|
|
|
|
|
// Return the jwt
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&ListCommunitiesResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
communities: communities
|
|
|
|
}
|
|
|
|
)?
|
2019-03-26 18:00:18 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
impl Perform for ListCategories {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::ListCategories
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-03 23:01:20 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let categories: Vec<Category> = Category::list_all(&conn)?;
|
2019-04-03 23:01:20 +00:00
|
|
|
|
|
|
|
// Return the jwt
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&ListCategoriesResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
categories: categories
|
|
|
|
}
|
|
|
|
)?
|
2019-04-03 23:01:20 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
impl Perform for CreatePost {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::CreatePost
|
|
|
|
}
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
chat.check_rate_limit_register(addr)?;
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-09 18:35:16 +00:00
|
|
|
if has_slurs(&self.name) ||
|
|
|
|
(self.body.is_some() && has_slurs(&self.body.to_owned().unwrap())) {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("No slurs"))?
|
|
|
|
}
|
2019-04-09 18:35:16 +00:00
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
let user_id = claims.id;
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a community ban
|
2019-04-15 23:12:06 +00:00
|
|
|
if CommunityUserBanView::get(&conn, user_id, self.community_id).is_ok() {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("You have been banned from this community"))?
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a site ban
|
2019-04-21 07:26:26 +00:00
|
|
|
if UserView::read(&conn, user_id)?.banned {
|
|
|
|
return Err(self.error("You have been banned from the site"))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
let post_form = PostForm {
|
|
|
|
name: self.name.to_owned(),
|
|
|
|
url: self.url.to_owned(),
|
|
|
|
body: self.body.to_owned(),
|
|
|
|
community_id: self.community_id,
|
2019-04-03 06:49:32 +00:00
|
|
|
creator_id: user_id,
|
2019-04-20 07:24:59 +00:00
|
|
|
removed: None,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: None,
|
2019-04-20 07:24:59 +00:00
|
|
|
locked: None,
|
2019-03-26 18:00:18 +00:00
|
|
|
updated: None
|
|
|
|
};
|
|
|
|
|
|
|
|
let inserted_post = match Post::create(&conn, &post_form) {
|
|
|
|
Ok(post) => post,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't create Post"))?
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
// They like their own post by default
|
|
|
|
let like_form = PostLikeForm {
|
|
|
|
post_id: inserted_post.id,
|
|
|
|
user_id: user_id,
|
|
|
|
score: 1
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only add the like if the score isnt 0
|
2019-04-03 20:59:37 +00:00
|
|
|
let _inserted_like = match PostLike::like(&conn, &like_form) {
|
2019-04-03 06:49:32 +00:00
|
|
|
Ok(like) => like,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't like post."))?
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
};
|
2019-04-03 23:01:20 +00:00
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
// Refetch the view
|
2019-04-03 23:01:20 +00:00
|
|
|
let post_view = match PostView::read(&conn, inserted_post.id, Some(user_id)) {
|
2019-04-03 20:59:37 +00:00
|
|
|
Ok(post) => post,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't find Post"))?
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
};
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&PostResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
post: post_view
|
|
|
|
}
|
|
|
|
)?
|
2019-03-26 18:00:18 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Perform for GetPost {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::GetPost
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let user_id: Option<i32> = match &self.auth {
|
2019-03-28 19:32:08 +00:00
|
|
|
Some(auth) => {
|
|
|
|
match Claims::decode(&auth) {
|
|
|
|
Ok(claims) => {
|
2019-04-03 06:49:32 +00:00
|
|
|
let user_id = claims.claims.id;
|
|
|
|
Some(user_id)
|
2019-03-28 19:32:08 +00:00
|
|
|
}
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => None
|
2019-03-28 19:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => None
|
|
|
|
};
|
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
let post_view = match PostView::read(&conn, self.id, user_id) {
|
2019-03-26 18:00:18 +00:00
|
|
|
Ok(post) => post,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't find Post"))?
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
// remove session from all rooms
|
2019-04-03 20:59:37 +00:00
|
|
|
for (_n, sessions) in &mut chat.rooms {
|
2019-03-27 16:59:21 +00:00
|
|
|
sessions.remove(&addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if chat.rooms.get_mut(&self.id).is_none() {
|
|
|
|
chat.rooms.insert(self.id, HashSet::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.rooms.get_mut(&self.id).unwrap().insert(addr);
|
|
|
|
|
2019-04-23 22:05:50 +00:00
|
|
|
let comments = CommentView::list(&conn, &SortType::New, Some(self.id), None, None, user_id, false, None, Some(9999))?;
|
2019-03-27 16:59:21 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let community = CommunityView::read(&conn, post_view.community_id, user_id)?;
|
2019-04-03 23:01:20 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let moderators = CommunityModeratorView::for_community(&conn, post_view.community_id)?;
|
2019-04-04 20:53:32 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let admins = UserView::admins(&conn)?;
|
2019-04-20 04:06:25 +00:00
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
// Return the jwt
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&GetPostResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
post: post_view,
|
|
|
|
comments: comments,
|
|
|
|
community: community,
|
|
|
|
moderators: moderators,
|
|
|
|
admins: admins,
|
|
|
|
}
|
|
|
|
)?
|
2019-03-23 01:42:57 +00:00
|
|
|
)
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform for GetCommunity {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::GetCommunity
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
2019-03-25 03:51:27 +00:00
|
|
|
|
2019-04-05 06:26:38 +00:00
|
|
|
let user_id: Option<i32> = match &self.auth {
|
|
|
|
Some(auth) => {
|
|
|
|
match Claims::decode(&auth) {
|
|
|
|
Ok(claims) => {
|
|
|
|
let user_id = claims.claims.id;
|
|
|
|
Some(user_id)
|
|
|
|
}
|
|
|
|
Err(_e) => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => None
|
|
|
|
};
|
|
|
|
|
2019-04-25 21:52:18 +00:00
|
|
|
let community_id = match self.id {
|
|
|
|
Some(id) => id,
|
|
|
|
None => Community::read_from_name(&conn, self.name.to_owned().unwrap_or("main".to_string()))?.id
|
|
|
|
};
|
|
|
|
|
|
|
|
let community_view = match CommunityView::read(&conn, community_id, user_id) {
|
2019-03-26 18:00:18 +00:00
|
|
|
Ok(community) => community,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't find Community"))?
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-25 21:52:18 +00:00
|
|
|
let moderators = match CommunityModeratorView::for_community(&conn, community_id) {
|
2019-04-04 20:53:32 +00:00
|
|
|
Ok(moderators) => moderators,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't find Community"))?
|
2019-04-04 20:53:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-21 20:52:55 +00:00
|
|
|
let admins = UserView::admins(&conn)?;
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
// Return the jwt
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&GetCommunityResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
community: community_view,
|
2019-04-21 20:52:55 +00:00
|
|
|
moderators: moderators,
|
|
|
|
admins: admins,
|
2019-04-21 07:26:26 +00:00
|
|
|
}
|
|
|
|
)?
|
2019-03-26 18:00:18 +00:00
|
|
|
)
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
impl Perform for CreateComment {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::CreateComment
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
chat.check_rate_limit(addr)?;
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a community ban
|
2019-04-21 07:26:26 +00:00
|
|
|
let post = Post::read(&conn, self.post_id)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("You have been banned from this community"))?
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a site ban
|
2019-04-21 07:26:26 +00:00
|
|
|
if UserView::read(&conn, user_id)?.banned {
|
|
|
|
return Err(self.error("You have been banned from the site"))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2019-04-09 18:35:16 +00:00
|
|
|
let content_slurs_removed = remove_slurs(&self.content.to_owned());
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
let comment_form = CommentForm {
|
2019-04-09 18:35:16 +00:00
|
|
|
content: content_slurs_removed,
|
2019-03-26 18:00:18 +00:00
|
|
|
parent_id: self.parent_id.to_owned(),
|
|
|
|
post_id: self.post_id,
|
2019-04-03 06:49:32 +00:00
|
|
|
creator_id: user_id,
|
2019-04-15 23:12:06 +00:00
|
|
|
removed: None,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: None,
|
2019-04-20 18:17:00 +00:00
|
|
|
read: None,
|
2019-03-26 18:00:18 +00:00
|
|
|
updated: None
|
|
|
|
};
|
|
|
|
|
|
|
|
let inserted_comment = match Comment::create(&conn, &comment_form) {
|
|
|
|
Ok(comment) => comment,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't create Comment"))?
|
2019-03-28 19:53:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// You like your own comment by default
|
|
|
|
let like_form = CommentLikeForm {
|
|
|
|
comment_id: inserted_comment.id,
|
|
|
|
post_id: self.post_id,
|
2019-04-03 06:49:32 +00:00
|
|
|
user_id: user_id,
|
2019-03-28 19:53:29 +00:00
|
|
|
score: 1
|
|
|
|
};
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
let _inserted_like = match CommentLike::like(&conn, &like_form) {
|
2019-03-28 19:53:29 +00:00
|
|
|
Ok(like) => like,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't like comment."))?
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let comment_view = CommentView::read(&conn, inserted_comment.id, Some(user_id))?;
|
2019-03-28 19:53:29 +00:00
|
|
|
|
|
|
|
let mut comment_sent = comment_view.clone();
|
|
|
|
comment_sent.my_vote = None;
|
2019-04-03 20:59:37 +00:00
|
|
|
comment_sent.user_id = None;
|
2019-03-28 19:32:08 +00:00
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
let comment_out = serde_json::to_string(
|
2019-04-03 20:59:37 +00:00
|
|
|
&CommentResponse {
|
2019-03-26 18:00:18 +00:00
|
|
|
op: self.op_type().to_string(),
|
2019-03-28 19:32:08 +00:00
|
|
|
comment: comment_view
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
)?;
|
2019-03-27 16:59:21 +00:00
|
|
|
|
2019-03-28 19:53:29 +00:00
|
|
|
let comment_sent_out = serde_json::to_string(
|
2019-04-03 20:59:37 +00:00
|
|
|
&CommentResponse {
|
2019-03-28 19:53:29 +00:00
|
|
|
op: self.op_type().to_string(),
|
|
|
|
comment: comment_sent
|
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
)?;
|
2019-04-03 23:01:20 +00:00
|
|
|
|
2019-03-28 19:53:29 +00:00
|
|
|
chat.send_room_message(self.post_id, &comment_sent_out, addr);
|
2019-03-27 16:59:21 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(comment_out)
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
impl Perform for EditComment {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::EditComment
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-03-29 04:56:23 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-03-29 04:56:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2019-04-25 15:51:23 +00:00
|
|
|
let orig_comment = CommentView::read(&conn, self.edit_id, None)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2019-04-22 18:32:50 +00:00
|
|
|
// You are allowed to mark the comment as read even if you're banned.
|
|
|
|
if self.read.is_none() {
|
2019-04-25 15:33:25 +00:00
|
|
|
|
|
|
|
// Verify its the creator or a mod, or an admin
|
|
|
|
let mut editors: Vec<i32> = vec![self.creator_id];
|
|
|
|
editors.append(
|
|
|
|
&mut CommunityModeratorView::for_community(&conn, orig_comment.community_id)
|
|
|
|
?
|
|
|
|
.into_iter()
|
|
|
|
.map(|m| m.user_id)
|
|
|
|
.collect()
|
|
|
|
);
|
|
|
|
editors.append(
|
|
|
|
&mut UserView::admins(&conn)
|
|
|
|
?
|
|
|
|
.into_iter()
|
|
|
|
.map(|a| a.id)
|
|
|
|
.collect()
|
|
|
|
);
|
|
|
|
|
|
|
|
if !editors.contains(&user_id) {
|
|
|
|
return Err(self.error("Not allowed to edit comment."))?
|
|
|
|
}
|
|
|
|
|
2019-04-22 18:32:50 +00:00
|
|
|
// Check for a community ban
|
|
|
|
if CommunityUserBanView::get(&conn, user_id, orig_comment.community_id).is_ok() {
|
|
|
|
return Err(self.error("You have been banned from this community"))?
|
|
|
|
}
|
2019-04-05 00:25:21 +00:00
|
|
|
|
2019-04-22 18:32:50 +00:00
|
|
|
// Check for a site ban
|
|
|
|
if UserView::read(&conn, user_id)?.banned {
|
|
|
|
return Err(self.error("You have been banned from the site"))?
|
|
|
|
}
|
2019-04-25 15:33:25 +00:00
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 18:35:16 +00:00
|
|
|
let content_slurs_removed = remove_slurs(&self.content.to_owned());
|
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
let comment_form = CommentForm {
|
2019-04-09 18:35:16 +00:00
|
|
|
content: content_slurs_removed,
|
2019-03-29 04:56:23 +00:00
|
|
|
parent_id: self.parent_id,
|
|
|
|
post_id: self.post_id,
|
2019-04-15 23:12:06 +00:00
|
|
|
creator_id: self.creator_id,
|
|
|
|
removed: self.removed.to_owned(),
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: self.deleted.to_owned(),
|
2019-04-20 18:17:00 +00:00
|
|
|
read: self.read.to_owned(),
|
2019-04-25 15:51:23 +00:00
|
|
|
updated: if self.read.is_some() { orig_comment.updated } else {Some(naive_now())}
|
2019-05-01 22:44:39 +00:00
|
|
|
};
|
2019-03-29 04:56:23 +00:00
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
let _updated_comment = match Comment::update(&conn, self.edit_id, &comment_form) {
|
2019-03-29 04:56:23 +00:00
|
|
|
Ok(comment) => comment,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't update Comment"))?
|
2019-03-29 04:56:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
// Mod tables
|
|
|
|
if let Some(removed) = self.removed.to_owned() {
|
|
|
|
let form = ModRemoveCommentForm {
|
|
|
|
mod_user_id: user_id,
|
|
|
|
comment_id: self.edit_id,
|
|
|
|
removed: Some(removed),
|
|
|
|
reason: self.reason.to_owned(),
|
|
|
|
};
|
2019-04-21 07:26:26 +00:00
|
|
|
ModRemoveComment::create(&conn, &form)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let comment_view = CommentView::read(&conn, self.edit_id, Some(user_id))?;
|
2019-03-29 04:56:23 +00:00
|
|
|
|
|
|
|
let mut comment_sent = comment_view.clone();
|
|
|
|
comment_sent.my_vote = None;
|
2019-04-03 20:59:37 +00:00
|
|
|
comment_sent.user_id = None;
|
2019-03-29 04:56:23 +00:00
|
|
|
|
|
|
|
let comment_out = serde_json::to_string(
|
2019-04-03 20:59:37 +00:00
|
|
|
&CommentResponse {
|
2019-03-29 04:56:23 +00:00
|
|
|
op: self.op_type().to_string(),
|
|
|
|
comment: comment_view
|
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
)?;
|
2019-03-29 04:56:23 +00:00
|
|
|
|
|
|
|
let comment_sent_out = serde_json::to_string(
|
2019-04-03 20:59:37 +00:00
|
|
|
&CommentResponse {
|
2019-03-29 04:56:23 +00:00
|
|
|
op: self.op_type().to_string(),
|
|
|
|
comment: comment_sent
|
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
)?;
|
2019-04-03 23:01:20 +00:00
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
chat.send_room_message(self.post_id, &comment_sent_out, addr);
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(comment_out)
|
2019-03-29 04:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
impl Perform for SaveComment {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::SaveComment
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-20 04:06:25 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
|
|
|
let comment_saved_form = CommentSavedForm {
|
|
|
|
comment_id: self.comment_id,
|
|
|
|
user_id: user_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.save {
|
|
|
|
match CommentSaved::save(&conn, &comment_saved_form) {
|
|
|
|
Ok(comment) => comment,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldnt do comment save"))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
match CommentSaved::unsave(&conn, &comment_saved_form) {
|
|
|
|
Ok(comment) => comment,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldnt do comment save"))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let comment_view = CommentView::read(&conn, self.comment_id, Some(user_id))?;
|
2019-04-20 04:06:25 +00:00
|
|
|
|
|
|
|
let comment_out = serde_json::to_string(
|
|
|
|
&CommentResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
comment: comment_view
|
|
|
|
}
|
|
|
|
)
|
2019-04-21 07:26:26 +00:00
|
|
|
?;
|
2019-04-20 04:06:25 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(comment_out)
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
impl Perform for CreateCommentLike {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::CreateCommentLike
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-03-28 19:32:08 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
chat.check_rate_limit(addr)?;
|
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-03-28 19:32:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a community ban
|
2019-04-21 07:26:26 +00:00
|
|
|
let post = Post::read(&conn, self.post_id)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("You have been banned from this community"))?
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a site ban
|
2019-04-21 07:26:26 +00:00
|
|
|
if UserView::read(&conn, user_id)?.banned {
|
|
|
|
return Err(self.error("You have been banned from the site"))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
let like_form = CommentLikeForm {
|
|
|
|
comment_id: self.comment_id,
|
|
|
|
post_id: self.post_id,
|
2019-04-03 06:49:32 +00:00
|
|
|
user_id: user_id,
|
2019-03-28 19:32:08 +00:00
|
|
|
score: self.score
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove any likes first
|
2019-04-21 07:26:26 +00:00
|
|
|
CommentLike::remove(&conn, &like_form)?;
|
2019-03-28 19:32:08 +00:00
|
|
|
|
|
|
|
// Only add the like if the score isnt 0
|
|
|
|
if &like_form.score != &0 {
|
2019-04-03 20:59:37 +00:00
|
|
|
let _inserted_like = match CommentLike::like(&conn, &like_form) {
|
2019-03-28 19:32:08 +00:00
|
|
|
Ok(like) => like,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't like comment."))?
|
2019-03-28 19:32:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Have to refetch the comment to get the current state
|
2019-04-21 07:26:26 +00:00
|
|
|
let liked_comment = CommentView::read(&conn, self.comment_id, Some(user_id))?;
|
2019-03-28 19:32:08 +00:00
|
|
|
|
|
|
|
let mut liked_comment_sent = liked_comment.clone();
|
|
|
|
liked_comment_sent.my_vote = None;
|
2019-04-03 20:59:37 +00:00
|
|
|
liked_comment_sent.user_id = None;
|
2019-03-28 19:32:08 +00:00
|
|
|
|
|
|
|
let like_out = serde_json::to_string(
|
2019-04-03 20:59:37 +00:00
|
|
|
&CommentResponse {
|
2019-03-28 19:32:08 +00:00
|
|
|
op: self.op_type().to_string(),
|
|
|
|
comment: liked_comment
|
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
)?;
|
2019-03-28 19:32:08 +00:00
|
|
|
|
|
|
|
let like_sent_out = serde_json::to_string(
|
2019-04-03 20:59:37 +00:00
|
|
|
&CommentResponse {
|
2019-03-28 19:32:08 +00:00
|
|
|
op: self.op_type().to_string(),
|
|
|
|
comment: liked_comment_sent
|
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
)?;
|
2019-03-28 19:32:08 +00:00
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
chat.send_room_message(self.post_id, &like_sent_out, addr);
|
2019-03-28 19:32:08 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(like_out)
|
2019-03-28 19:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
impl Perform for GetPosts {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::GetPosts
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-03 06:49:32 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let user_id: Option<i32> = match &self.auth {
|
|
|
|
Some(auth) => {
|
|
|
|
match Claims::decode(&auth) {
|
|
|
|
Ok(claims) => {
|
|
|
|
let user_id = claims.claims.id;
|
|
|
|
Some(user_id)
|
|
|
|
}
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => None
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => None
|
|
|
|
};
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let type_ = PostListingType::from_str(&self.type_)?;
|
|
|
|
let sort = SortType::from_str(&self.sort)?;
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2019-04-23 22:05:50 +00:00
|
|
|
let posts = match PostView::list(&conn,
|
|
|
|
type_,
|
|
|
|
&sort,
|
|
|
|
self.community_id,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
user_id,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
self.page,
|
|
|
|
self.limit) {
|
2019-04-03 06:49:32 +00:00
|
|
|
Ok(posts) => posts,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't get posts"))?
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Return the jwt
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&GetPostsResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
posts: posts
|
|
|
|
}
|
|
|
|
)?
|
2019-04-03 06:49:32 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Perform for CreatePostLike {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::CreatePostLike
|
|
|
|
}
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-04-03 06:49:32 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-05-01 22:44:39 +00:00
|
|
|
chat.check_rate_limit(addr)?;
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a community ban
|
2019-04-21 07:26:26 +00:00
|
|
|
let post = Post::read(&conn, self.post_id)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("You have been banned from this community"))?
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a site ban
|
2019-04-21 07:26:26 +00:00
|
|
|
if UserView::read(&conn, user_id)?.banned {
|
|
|
|
return Err(self.error("You have been banned from the site"))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let like_form = PostLikeForm {
|
|
|
|
post_id: self.post_id,
|
|
|
|
user_id: user_id,
|
|
|
|
score: self.score
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove any likes first
|
2019-04-21 07:26:26 +00:00
|
|
|
PostLike::remove(&conn, &like_form)?;
|
2019-04-03 06:49:32 +00:00
|
|
|
|
|
|
|
// Only add the like if the score isnt 0
|
|
|
|
if &like_form.score != &0 {
|
2019-04-03 20:59:37 +00:00
|
|
|
let _inserted_like = match PostLike::like(&conn, &like_form) {
|
2019-04-03 06:49:32 +00:00
|
|
|
Ok(like) => like,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't like post."))?
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
let post_view = match PostView::read(&conn, self.post_id, Some(user_id)) {
|
2019-04-03 06:49:32 +00:00
|
|
|
Ok(post) => post,
|
2019-04-03 20:59:37 +00:00
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't find Post"))?
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// just output the score
|
|
|
|
|
|
|
|
let like_out = serde_json::to_string(
|
|
|
|
&CreatePostLikeResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
post: post_view
|
|
|
|
}
|
2019-04-21 07:26:26 +00:00
|
|
|
)?;
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(like_out)
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
impl Perform for EditPost {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::EditPost
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-04-03 20:59:37 +00:00
|
|
|
|
2019-04-09 18:35:16 +00:00
|
|
|
if has_slurs(&self.name) ||
|
|
|
|
(self.body.is_some() && has_slurs(&self.body.to_owned().unwrap())) {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("No slurs"))?
|
|
|
|
}
|
2019-04-09 18:35:16 +00:00
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2019-04-20 07:24:59 +00:00
|
|
|
// Verify its the creator or a mod or admin
|
|
|
|
let mut editors: Vec<i32> = vec![self.creator_id];
|
|
|
|
editors.append(
|
|
|
|
&mut CommunityModeratorView::for_community(&conn, self.community_id)
|
2019-04-21 07:26:26 +00:00
|
|
|
?
|
2019-04-15 23:12:06 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|m| m.user_id)
|
2019-04-20 07:24:59 +00:00
|
|
|
.collect()
|
2019-04-21 07:26:26 +00:00
|
|
|
);
|
2019-04-20 07:24:59 +00:00
|
|
|
editors.append(
|
|
|
|
&mut UserView::admins(&conn)
|
2019-04-21 07:26:26 +00:00
|
|
|
?
|
2019-04-20 07:24:59 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|a| a.id)
|
|
|
|
.collect()
|
|
|
|
);
|
2019-04-15 23:12:06 +00:00
|
|
|
if !editors.contains(&user_id) {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not allowed to edit post."))?
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a community ban
|
2019-04-15 23:12:06 +00:00
|
|
|
if CommunityUserBanView::get(&conn, user_id, self.community_id).is_ok() {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("You have been banned from this community"))?
|
2019-04-05 00:25:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a site ban
|
2019-04-21 07:26:26 +00:00
|
|
|
if UserView::read(&conn, user_id)?.banned {
|
|
|
|
return Err(self.error("You have been banned from the site"))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
let post_form = PostForm {
|
|
|
|
name: self.name.to_owned(),
|
|
|
|
url: self.url.to_owned(),
|
|
|
|
body: self.body.to_owned(),
|
2019-04-15 23:12:06 +00:00
|
|
|
creator_id: self.creator_id.to_owned(),
|
2019-04-03 20:59:37 +00:00
|
|
|
community_id: self.community_id,
|
2019-04-15 23:12:06 +00:00
|
|
|
removed: self.removed.to_owned(),
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: self.deleted.to_owned(),
|
2019-04-15 23:12:06 +00:00
|
|
|
locked: self.locked.to_owned(),
|
2019-04-03 20:59:37 +00:00
|
|
|
updated: Some(naive_now())
|
|
|
|
};
|
|
|
|
|
|
|
|
let _updated_post = match Post::update(&conn, self.edit_id, &post_form) {
|
|
|
|
Ok(post) => post,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't update Post"))?
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
// Mod tables
|
2019-04-20 07:24:59 +00:00
|
|
|
if let Some(removed) = self.removed.to_owned() {
|
2019-04-15 23:12:06 +00:00
|
|
|
let form = ModRemovePostForm {
|
|
|
|
mod_user_id: user_id,
|
|
|
|
post_id: self.edit_id,
|
2019-04-20 07:24:59 +00:00
|
|
|
removed: Some(removed),
|
2019-04-15 23:12:06 +00:00
|
|
|
reason: self.reason.to_owned(),
|
|
|
|
};
|
2019-04-21 07:26:26 +00:00
|
|
|
ModRemovePost::create(&conn, &form)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 07:24:59 +00:00
|
|
|
if let Some(locked) = self.locked.to_owned() {
|
2019-04-15 23:12:06 +00:00
|
|
|
let form = ModLockPostForm {
|
|
|
|
mod_user_id: user_id,
|
|
|
|
post_id: self.edit_id,
|
2019-04-20 07:24:59 +00:00
|
|
|
locked: Some(locked),
|
2019-04-15 23:12:06 +00:00
|
|
|
};
|
2019-04-21 07:26:26 +00:00
|
|
|
ModLockPost::create(&conn, &form)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let post_view = PostView::read(&conn, self.edit_id, Some(user_id))?;
|
2019-04-03 20:59:37 +00:00
|
|
|
|
|
|
|
let mut post_sent = post_view.clone();
|
|
|
|
post_sent.my_vote = None;
|
|
|
|
|
|
|
|
let post_out = serde_json::to_string(
|
|
|
|
&PostResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
post: post_view
|
|
|
|
}
|
|
|
|
)
|
2019-04-21 07:26:26 +00:00
|
|
|
?;
|
2019-04-03 20:59:37 +00:00
|
|
|
|
|
|
|
let post_sent_out = serde_json::to_string(
|
|
|
|
&PostResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
post: post_sent
|
|
|
|
}
|
|
|
|
)
|
2019-04-21 07:26:26 +00:00
|
|
|
?;
|
2019-04-03 23:01:20 +00:00
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
chat.send_room_message(self.edit_id, &post_sent_out, addr);
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(post_out)
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 22:29:14 +00:00
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
impl Perform for SavePost {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::SavePost
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-20 04:06:25 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
|
|
|
let post_saved_form = PostSavedForm {
|
|
|
|
post_id: self.post_id,
|
|
|
|
user_id: user_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.save {
|
|
|
|
match PostSaved::save(&conn, &post_saved_form) {
|
|
|
|
Ok(post) => post,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldnt do post save"))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
match PostSaved::unsave(&conn, &post_saved_form) {
|
|
|
|
Ok(post) => post,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldnt do post save"))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let post_view = PostView::read(&conn, self.post_id, Some(user_id))?;
|
2019-04-20 04:06:25 +00:00
|
|
|
|
|
|
|
let post_out = serde_json::to_string(
|
|
|
|
&PostResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
post: post_view
|
|
|
|
}
|
|
|
|
)
|
2019-04-21 07:26:26 +00:00
|
|
|
?;
|
2019-04-20 04:06:25 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(post_out)
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
impl Perform for EditCommunity {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::EditCommunity
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-04-04 22:29:14 +00:00
|
|
|
|
2019-04-09 18:35:16 +00:00
|
|
|
if has_slurs(&self.name) || has_slurs(&self.title) {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("No slurs"))?
|
2019-04-09 18:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-04 22:29:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
// Check for a site ban
|
2019-04-21 07:26:26 +00:00
|
|
|
if UserView::read(&conn, user_id)?.banned {
|
|
|
|
return Err(self.error("You have been banned from the site"))?
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 00:25:21 +00:00
|
|
|
// Verify its a mod
|
2019-04-21 20:52:55 +00:00
|
|
|
let mut editors: Vec<i32> = Vec::new();
|
|
|
|
editors.append(
|
|
|
|
&mut CommunityModeratorView::for_community(&conn, self.edit_id)
|
|
|
|
?
|
|
|
|
.into_iter()
|
|
|
|
.map(|m| m.user_id)
|
|
|
|
.collect()
|
|
|
|
);
|
|
|
|
editors.append(
|
|
|
|
&mut UserView::admins(&conn)
|
|
|
|
?
|
|
|
|
.into_iter()
|
|
|
|
.map(|a| a.id)
|
|
|
|
.collect()
|
|
|
|
);
|
|
|
|
if !editors.contains(&user_id) {
|
|
|
|
return Err(self.error("Not allowed to edit community"))?
|
|
|
|
}
|
2019-04-05 00:25:21 +00:00
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
let community_form = CommunityForm {
|
|
|
|
name: self.name.to_owned(),
|
|
|
|
title: self.title.to_owned(),
|
|
|
|
description: self.description.to_owned(),
|
|
|
|
category_id: self.category_id.to_owned(),
|
|
|
|
creator_id: user_id,
|
2019-04-15 23:12:06 +00:00
|
|
|
removed: self.removed.to_owned(),
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: self.deleted.to_owned(),
|
2019-04-04 22:29:14 +00:00
|
|
|
updated: Some(naive_now())
|
|
|
|
};
|
|
|
|
|
|
|
|
let _updated_community = match Community::update(&conn, self.edit_id, &community_form) {
|
|
|
|
Ok(community) => community,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't update Community"))?
|
2019-04-04 22:29:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
// Mod tables
|
2019-04-20 07:24:59 +00:00
|
|
|
if let Some(removed) = self.removed.to_owned() {
|
2019-04-15 23:12:06 +00:00
|
|
|
let expires = match self.expires {
|
|
|
|
Some(time) => Some(naive_from_unix(time)),
|
|
|
|
None => None
|
|
|
|
};
|
|
|
|
let form = ModRemoveCommunityForm {
|
|
|
|
mod_user_id: user_id,
|
|
|
|
community_id: self.edit_id,
|
2019-04-20 07:24:59 +00:00
|
|
|
removed: Some(removed),
|
2019-04-15 23:12:06 +00:00
|
|
|
reason: self.reason.to_owned(),
|
|
|
|
expires: expires
|
|
|
|
};
|
2019-04-21 07:26:26 +00:00
|
|
|
ModRemoveCommunity::create(&conn, &form)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
2019-04-04 22:29:14 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let community_view = CommunityView::read(&conn, self.edit_id, Some(user_id))?;
|
2019-04-04 22:29:14 +00:00
|
|
|
|
|
|
|
let community_out = serde_json::to_string(
|
|
|
|
&CommunityResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
community: community_view
|
|
|
|
}
|
|
|
|
)
|
2019-04-21 07:26:26 +00:00
|
|
|
?;
|
2019-04-04 22:29:14 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let community_view_sent = CommunityView::read(&conn, self.edit_id, None)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
let community_sent = serde_json::to_string(
|
|
|
|
&CommunityResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
community: community_view_sent
|
|
|
|
}
|
|
|
|
)
|
2019-04-21 07:26:26 +00:00
|
|
|
?;
|
2019-04-04 22:29:14 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
chat.send_community_message(&conn, self.edit_id, &community_sent, addr)?;
|
2019-04-04 22:29:14 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(community_out)
|
2019-04-04 22:29:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-05 06:26:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
impl Perform for FollowCommunity {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::FollowCommunity
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-05 06:26:38 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-05 06:26:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
|
|
|
let community_follower_form = CommunityFollowerForm {
|
|
|
|
community_id: self.community_id,
|
|
|
|
user_id: user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.follow {
|
|
|
|
|
|
|
|
match CommunityFollower::follow(&conn, &community_follower_form) {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Community follower already exists."))?
|
2019-04-05 06:26:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
match CommunityFollower::ignore(&conn, &community_follower_form) {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Community follower already exists."))?
|
2019-04-05 06:26:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let community_view = CommunityView::read(&conn, self.community_id, Some(user_id))?;
|
2019-04-05 06:26:38 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&CommunityResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
community: community_view
|
|
|
|
}
|
|
|
|
)?
|
2019-04-05 06:26:38 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 19:14:54 +00:00
|
|
|
impl Perform for GetFollowedCommunities {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::GetFollowedCommunities
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-05 19:14:54 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-05 19:14:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2019-04-17 23:42:29 +00:00
|
|
|
let communities: Vec<CommunityFollowerView> = match CommunityFollowerView::for_user(&conn, user_id) {
|
|
|
|
Ok(communities) => communities,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("System error, try logging out and back in."))?
|
2019-04-17 23:42:29 +00:00
|
|
|
}
|
|
|
|
};
|
2019-04-05 19:14:54 +00:00
|
|
|
|
|
|
|
// Return the jwt
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&GetFollowedCommunitiesResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
communities: communities
|
|
|
|
}
|
|
|
|
)?
|
2019-04-05 19:14:54 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-04-05 06:26:38 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
impl Perform for GetUserDetails {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::GetUserDetails
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-08 05:19:02 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-04-29 21:19:00 +00:00
|
|
|
let user_id: Option<i32> = match &self.auth {
|
|
|
|
Some(auth) => {
|
|
|
|
match Claims::decode(&auth) {
|
|
|
|
Ok(claims) => {
|
|
|
|
let user_id = claims.claims.id;
|
|
|
|
Some(user_id)
|
|
|
|
}
|
|
|
|
Err(_e) => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => None
|
|
|
|
};
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
//TODO add save
|
2019-04-21 07:26:26 +00:00
|
|
|
let sort = SortType::from_str(&self.sort)?;
|
2019-04-08 05:19:02 +00:00
|
|
|
|
2019-04-25 21:52:18 +00:00
|
|
|
let user_details_id = match self.user_id {
|
|
|
|
Some(id) => id,
|
|
|
|
None => User_::read_from_name(&conn, self.username.to_owned().unwrap_or("admin".to_string()))?.id
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_view = UserView::read(&conn, user_details_id)?;
|
|
|
|
|
2019-04-23 22:05:50 +00:00
|
|
|
// If its saved only, you don't care what creator it was
|
2019-04-20 04:06:25 +00:00
|
|
|
let posts = if self.saved_only {
|
2019-04-23 22:05:50 +00:00
|
|
|
PostView::list(&conn,
|
|
|
|
PostListingType::All,
|
|
|
|
&sort,
|
|
|
|
self.community_id,
|
|
|
|
None,
|
|
|
|
None,
|
2019-04-25 21:52:18 +00:00
|
|
|
Some(user_details_id),
|
2019-04-23 22:05:50 +00:00
|
|
|
self.saved_only,
|
|
|
|
false,
|
|
|
|
self.page,
|
|
|
|
self.limit)?
|
2019-04-20 04:06:25 +00:00
|
|
|
} else {
|
2019-04-23 22:05:50 +00:00
|
|
|
PostView::list(&conn,
|
|
|
|
PostListingType::All,
|
|
|
|
&sort,
|
|
|
|
self.community_id,
|
2019-04-25 21:52:18 +00:00
|
|
|
Some(user_details_id),
|
2019-04-23 22:05:50 +00:00
|
|
|
None,
|
2019-04-29 21:19:00 +00:00
|
|
|
user_id,
|
2019-04-23 22:05:50 +00:00
|
|
|
self.saved_only,
|
|
|
|
false,
|
|
|
|
self.page,
|
|
|
|
self.limit)?
|
2019-04-20 04:06:25 +00:00
|
|
|
};
|
|
|
|
let comments = if self.saved_only {
|
2019-04-23 22:05:50 +00:00
|
|
|
CommentView::list(&conn,
|
|
|
|
&sort,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
2019-04-25 21:52:18 +00:00
|
|
|
Some(user_details_id),
|
2019-04-23 22:05:50 +00:00
|
|
|
self.saved_only,
|
|
|
|
self.page,
|
|
|
|
self.limit)?
|
2019-04-20 04:06:25 +00:00
|
|
|
} else {
|
2019-04-23 22:05:50 +00:00
|
|
|
CommentView::list(&conn,
|
|
|
|
&sort,
|
|
|
|
None,
|
2019-04-25 21:52:18 +00:00
|
|
|
Some(user_details_id),
|
2019-04-23 22:05:50 +00:00
|
|
|
None,
|
2019-04-29 21:19:00 +00:00
|
|
|
user_id,
|
2019-04-23 22:05:50 +00:00
|
|
|
self.saved_only,
|
|
|
|
self.page,
|
|
|
|
self.limit)?
|
2019-04-20 04:06:25 +00:00
|
|
|
};
|
|
|
|
|
2019-04-25 21:52:18 +00:00
|
|
|
let follows = CommunityFollowerView::for_user(&conn, user_details_id)?;
|
|
|
|
let moderates = CommunityModeratorView::for_user(&conn, user_details_id)?;
|
2019-04-08 05:19:02 +00:00
|
|
|
|
|
|
|
// Return the jwt
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&GetUserDetailsResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
user: user_view,
|
|
|
|
follows: follows,
|
|
|
|
moderates: moderates,
|
|
|
|
comments: comments,
|
|
|
|
posts: posts,
|
|
|
|
}
|
|
|
|
)?
|
2019-04-08 05:19:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
impl Perform for GetModlog {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::GetModlog
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let removed_posts = ModRemovePostView::list(&conn, self.community_id, self.mod_user_id, self.page, self.limit)?;
|
|
|
|
let locked_posts = ModLockPostView::list(&conn, self.community_id, self.mod_user_id, self.page, self.limit)?;
|
|
|
|
let removed_comments = ModRemoveCommentView::list(&conn, self.community_id, self.mod_user_id, self.page, self.limit)?;
|
|
|
|
let banned_from_community = ModBanFromCommunityView::list(&conn, self.community_id, self.mod_user_id, self.page, self.limit)?;
|
|
|
|
let added_to_community = ModAddCommunityView::list(&conn, self.community_id, self.mod_user_id, self.page, self.limit)?;
|
2019-04-17 15:50:44 +00:00
|
|
|
|
|
|
|
// These arrays are only for the full modlog, when a community isn't given
|
|
|
|
let mut removed_communities = Vec::new();
|
|
|
|
let mut banned = Vec::new();
|
|
|
|
let mut added = Vec::new();
|
|
|
|
|
|
|
|
if self.community_id.is_none() {
|
2019-04-21 07:26:26 +00:00
|
|
|
removed_communities = ModRemoveCommunityView::list(&conn, self.mod_user_id, self.page, self.limit)?;
|
|
|
|
banned = ModBanView::list(&conn, self.mod_user_id, self.page, self.limit)?;
|
|
|
|
added = ModAddView::list(&conn, self.mod_user_id, self.page, self.limit)?;
|
2019-04-17 15:50:44 +00:00
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
// Return the jwt
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&GetModlogResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
removed_posts: removed_posts,
|
|
|
|
locked_posts: locked_posts,
|
|
|
|
removed_comments: removed_comments,
|
|
|
|
removed_communities: removed_communities,
|
|
|
|
banned_from_community: banned_from_community,
|
|
|
|
banned: banned,
|
|
|
|
added_to_community: added_to_community,
|
|
|
|
added: added,
|
|
|
|
}
|
|
|
|
)?
|
2019-04-15 23:12:06 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 18:17:00 +00:00
|
|
|
impl Perform for GetReplies {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::GetReplies
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-20 18:17:00 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-20 18:17:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let sort = SortType::from_str(&self.sort)?;
|
2019-04-20 18:17:00 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let replies = ReplyView::get_replies(&conn, user_id, &sort, self.unread_only, self.page, self.limit)?;
|
2019-04-20 18:17:00 +00:00
|
|
|
|
|
|
|
// Return the jwt
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&GetRepliesResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
replies: replies,
|
|
|
|
}
|
|
|
|
)?
|
2019-04-20 18:17:00 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
impl Perform for BanFromCommunity {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::BanFromCommunity
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
|
|
|
let community_user_ban_form = CommunityUserBanForm {
|
|
|
|
community_id: self.community_id,
|
|
|
|
user_id: self.user_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.ban {
|
|
|
|
match CommunityUserBan::ban(&conn, &community_user_ban_form) {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Community user ban already exists"))?
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
match CommunityUserBan::unban(&conn, &community_user_ban_form) {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Community user ban already exists"))?
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let expires = match self.expires {
|
|
|
|
Some(time) => Some(naive_from_unix(time)),
|
|
|
|
None => None
|
|
|
|
};
|
|
|
|
|
|
|
|
let form = ModBanFromCommunityForm {
|
|
|
|
mod_user_id: user_id,
|
|
|
|
other_user_id: self.user_id,
|
|
|
|
community_id: self.community_id,
|
|
|
|
reason: self.reason.to_owned(),
|
|
|
|
banned: Some(self.ban),
|
|
|
|
expires: expires,
|
|
|
|
};
|
2019-04-21 07:26:26 +00:00
|
|
|
ModBanFromCommunity::create(&conn, &form)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let user_view = UserView::read(&conn, self.user_id)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
let res = serde_json::to_string(
|
|
|
|
&BanFromCommunityResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
user: user_view,
|
|
|
|
banned: self.ban
|
|
|
|
}
|
|
|
|
)
|
2019-04-21 07:26:26 +00:00
|
|
|
?;
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
chat.send_community_message(&conn, self.community_id, &res, addr)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(res)
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform for AddModToCommunity {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::AddModToCommunity
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, chat: &mut ChatServer, addr: usize) -> Result<String, Error> {
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
|
|
|
let community_moderator_form = CommunityModeratorForm {
|
|
|
|
community_id: self.community_id,
|
|
|
|
user_id: self.user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.added {
|
|
|
|
match CommunityModerator::join(&conn, &community_moderator_form) {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Community moderator already exists."))?
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
match CommunityModerator::leave(&conn, &community_moderator_form) {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Community moderator already exists."))?
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModAddCommunityForm {
|
|
|
|
mod_user_id: user_id,
|
|
|
|
other_user_id: self.user_id,
|
|
|
|
community_id: self.community_id,
|
|
|
|
removed: Some(!self.added),
|
|
|
|
};
|
2019-04-21 07:26:26 +00:00
|
|
|
ModAddCommunity::create(&conn, &form)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let moderators = CommunityModeratorView::for_community(&conn, self.community_id)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
let res = serde_json::to_string(
|
|
|
|
&AddModToCommunityResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
moderators: moderators,
|
|
|
|
}
|
|
|
|
)
|
2019-04-21 07:26:26 +00:00
|
|
|
?;
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
chat.send_community_message(&conn, self.community_id, &res, addr)?;
|
2019-04-15 23:12:06 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(res)
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
impl Perform for CreateSite {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::CreateSite
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if has_slurs(&self.name) ||
|
|
|
|
(self.description.is_some() && has_slurs(&self.description.to_owned().unwrap())) {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("No slurs"))?
|
|
|
|
}
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
|
|
|
// Make sure user is an admin
|
2019-04-21 07:26:26 +00:00
|
|
|
if !UserView::read(&conn, user_id)?.admin {
|
|
|
|
return Err(self.error("Not an admin."))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let site_form = SiteForm {
|
|
|
|
name: self.name.to_owned(),
|
|
|
|
description: self.description.to_owned(),
|
|
|
|
creator_id: user_id,
|
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
match Site::create(&conn, &site_form) {
|
|
|
|
Ok(site) => site,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Site exists already"))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let site_view = SiteView::read(&conn)?;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&SiteResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
site: site_view,
|
|
|
|
}
|
|
|
|
)?
|
2019-04-16 23:04:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform for EditSite {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::EditSite
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if has_slurs(&self.name) ||
|
|
|
|
(self.description.is_some() && has_slurs(&self.description.to_owned().unwrap())) {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("No slurs"))?
|
|
|
|
}
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
|
|
|
// Make sure user is an admin
|
2019-04-21 07:26:26 +00:00
|
|
|
if UserView::read(&conn, user_id)?.admin == false {
|
|
|
|
return Err(self.error("Not an admin."))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let found_site = Site::read(&conn, 1)?;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
let site_form = SiteForm {
|
|
|
|
name: self.name.to_owned(),
|
|
|
|
description: self.description.to_owned(),
|
|
|
|
creator_id: found_site.creator_id,
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
};
|
|
|
|
|
|
|
|
match Site::update(&conn, 1, &site_form) {
|
|
|
|
Ok(site) => site,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't update site."))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let site_view = SiteView::read(&conn)?;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&SiteResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
site: site_view,
|
|
|
|
}
|
|
|
|
)?
|
2019-04-16 23:04:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform for GetSite {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::GetSite
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
// It can return a null site in order to redirect
|
|
|
|
let site_view = match Site::read(&conn, 1) {
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(_site) => Some(SiteView::read(&conn)?),
|
2019-04-16 23:04:23 +00:00
|
|
|
Err(_e) => None
|
|
|
|
};
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let admins = UserView::admins(&conn)?;
|
|
|
|
let banned = UserView::banned(&conn)?;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&GetSiteResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
site: site_view,
|
|
|
|
admins: admins,
|
|
|
|
banned: banned,
|
|
|
|
}
|
|
|
|
)?
|
2019-04-16 23:04:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform for AddAdmin {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::AddAdmin
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
|
|
|
// Make sure user is an admin
|
2019-04-21 07:26:26 +00:00
|
|
|
if UserView::read(&conn, user_id)?.admin == false {
|
|
|
|
return Err(self.error("Not an admin."))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let read_user = User_::read(&conn, self.user_id)?;
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
let user_form = UserForm {
|
|
|
|
name: read_user.name,
|
|
|
|
fedi_name: read_user.fedi_name,
|
|
|
|
email: read_user.email,
|
|
|
|
password_encrypted: read_user.password_encrypted,
|
|
|
|
preferred_username: read_user.preferred_username,
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
admin: self.added,
|
|
|
|
banned: read_user.banned,
|
|
|
|
};
|
|
|
|
|
|
|
|
match User_::update(&conn, self.user_id, &user_form) {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't update user"))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModAddForm {
|
|
|
|
mod_user_id: user_id,
|
|
|
|
other_user_id: self.user_id,
|
|
|
|
removed: Some(!self.added),
|
|
|
|
};
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
ModAdd::create(&conn, &form)?;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let admins = UserView::admins(&conn)?;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
let res = serde_json::to_string(
|
|
|
|
&AddAdminResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
admins: admins,
|
|
|
|
}
|
|
|
|
)
|
2019-04-21 07:26:26 +00:00
|
|
|
?;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
|
|
|
|
Ok(res)
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform for BanUser {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::BanUser
|
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Not logged in."))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
|
|
|
// Make sure user is an admin
|
2019-04-21 07:26:26 +00:00
|
|
|
if UserView::read(&conn, user_id)?.admin == false {
|
|
|
|
return Err(self.error("Not an admin."))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let read_user = User_::read(&conn, self.user_id)?;
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
let user_form = UserForm {
|
|
|
|
name: read_user.name,
|
|
|
|
fedi_name: read_user.fedi_name,
|
|
|
|
email: read_user.email,
|
|
|
|
password_encrypted: read_user.password_encrypted,
|
|
|
|
preferred_username: read_user.preferred_username,
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
admin: read_user.admin,
|
|
|
|
banned: self.ban,
|
|
|
|
};
|
|
|
|
|
|
|
|
match User_::update(&conn, self.user_id, &user_form) {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_e) => {
|
2019-04-21 07:26:26 +00:00
|
|
|
return Err(self.error("Couldn't update user"))?
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let expires = match self.expires {
|
|
|
|
Some(time) => Some(naive_from_unix(time)),
|
|
|
|
None => None
|
|
|
|
};
|
|
|
|
|
|
|
|
let form = ModBanForm {
|
|
|
|
mod_user_id: user_id,
|
|
|
|
other_user_id: self.user_id,
|
|
|
|
reason: self.reason.to_owned(),
|
|
|
|
banned: Some(self.ban),
|
|
|
|
expires: expires,
|
|
|
|
};
|
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
ModBan::create(&conn, &form)?;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
let user_view = UserView::read(&conn, self.user_id)?;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
let res = serde_json::to_string(
|
|
|
|
&BanUserResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
user: user_view,
|
|
|
|
banned: self.ban
|
|
|
|
}
|
|
|
|
)
|
2019-04-21 07:26:26 +00:00
|
|
|
?;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
2019-04-21 07:26:26 +00:00
|
|
|
Ok(res)
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 22:05:50 +00:00
|
|
|
|
|
|
|
impl Perform for Search {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::Search
|
|
|
|
}
|
|
|
|
|
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let sort = SortType::from_str(&self.sort)?;
|
|
|
|
let type_ = SearchType::from_str(&self.type_)?;
|
|
|
|
|
|
|
|
let mut posts = Vec::new();
|
|
|
|
let mut comments = Vec::new();
|
|
|
|
|
|
|
|
match type_ {
|
|
|
|
SearchType::Posts => {
|
|
|
|
posts = PostView::list(&conn,
|
|
|
|
PostListingType::All,
|
|
|
|
&sort,
|
|
|
|
self.community_id,
|
|
|
|
None,
|
|
|
|
Some(self.q.to_owned()),
|
|
|
|
None,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
self.page,
|
|
|
|
self.limit)?;
|
|
|
|
},
|
|
|
|
SearchType::Comments => {
|
|
|
|
comments = CommentView::list(&conn,
|
2019-05-01 22:44:39 +00:00
|
|
|
&sort,
|
2019-04-23 22:05:50 +00:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
Some(self.q.to_owned()),
|
|
|
|
None,
|
|
|
|
false,
|
|
|
|
self.page,
|
|
|
|
self.limit)?;
|
|
|
|
},
|
|
|
|
SearchType::Both => {
|
|
|
|
posts = PostView::list(&conn,
|
|
|
|
PostListingType::All,
|
|
|
|
&sort,
|
|
|
|
self.community_id,
|
|
|
|
None,
|
|
|
|
Some(self.q.to_owned()),
|
|
|
|
None,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
self.page,
|
|
|
|
self.limit)?;
|
|
|
|
comments = CommentView::list(&conn,
|
2019-05-01 22:44:39 +00:00
|
|
|
&sort,
|
2019-04-23 22:05:50 +00:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
Some(self.q.to_owned()),
|
|
|
|
None,
|
|
|
|
false,
|
|
|
|
self.page,
|
|
|
|
self.limit)?;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Return the jwt
|
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&SearchResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
comments: comments,
|
|
|
|
posts: posts,
|
|
|
|
}
|
|
|
|
)?
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-04-29 16:51:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
impl Perform for MarkAllAsRead {
|
|
|
|
fn op_type(&self) -> UserOperation {
|
|
|
|
UserOperation::MarkAllAsRead
|
|
|
|
}
|
|
|
|
|
|
|
|
fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> Result<String, Error> {
|
|
|
|
|
|
|
|
let conn = establish_connection();
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&self.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => {
|
|
|
|
return Err(self.error("Not logged in."))?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
|
|
|
let replies = ReplyView::get_replies(&conn, user_id, &SortType::New, true, Some(1), Some(999))?;
|
|
|
|
|
|
|
|
for reply in &replies {
|
|
|
|
let comment_form = CommentForm {
|
|
|
|
content: reply.to_owned().content,
|
|
|
|
parent_id: reply.to_owned().parent_id,
|
|
|
|
post_id: reply.to_owned().post_id,
|
|
|
|
creator_id: reply.to_owned().creator_id,
|
|
|
|
removed: None,
|
2019-04-29 19:14:54 +00:00
|
|
|
deleted: None,
|
2019-04-29 16:51:13 +00:00
|
|
|
read: Some(true),
|
|
|
|
updated: reply.to_owned().updated
|
|
|
|
};
|
|
|
|
|
|
|
|
let _updated_comment = match Comment::update(&conn, reply.id, &comment_form) {
|
|
|
|
Ok(comment) => comment,
|
|
|
|
Err(_e) => {
|
|
|
|
return Err(self.error("Couldn't update Comment"))?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let replies = ReplyView::get_replies(&conn, user_id, &SortType::New, true, Some(1), Some(999))?;
|
|
|
|
|
|
|
|
Ok(
|
|
|
|
serde_json::to_string(
|
|
|
|
&GetRepliesResponse {
|
|
|
|
op: self.op_type().to_string(),
|
|
|
|
replies: replies,
|
|
|
|
}
|
|
|
|
)?
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|