mirror of https://github.com/LemmyNet/lemmy
commit
396ec117b3
@ -1,11 +0,0 @@
|
||||
pub mod user;
|
||||
pub mod community;
|
||||
pub mod post;
|
||||
pub mod comment;
|
||||
pub mod post_view;
|
||||
pub mod comment_view;
|
||||
pub mod category;
|
||||
pub mod community_view;
|
||||
pub mod user_view;
|
||||
pub mod moderator;
|
||||
pub mod moderator_views;
|
@ -0,0 +1,316 @@
|
||||
use super::*;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreateComment {
|
||||
content: String,
|
||||
parent_id: Option<i32>,
|
||||
edit_id: Option<i32>,
|
||||
pub post_id: i32,
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct EditComment {
|
||||
content: String,
|
||||
parent_id: Option<i32>,
|
||||
edit_id: i32,
|
||||
creator_id: i32,
|
||||
pub post_id: i32,
|
||||
removed: Option<bool>,
|
||||
deleted: Option<bool>,
|
||||
reason: Option<String>,
|
||||
read: Option<bool>,
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct SaveComment {
|
||||
comment_id: i32,
|
||||
save: bool,
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct CommentResponse {
|
||||
op: String,
|
||||
pub comment: CommentView
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreateCommentLike {
|
||||
comment_id: i32,
|
||||
pub post_id: i32,
|
||||
score: i16,
|
||||
auth: String
|
||||
}
|
||||
|
||||
|
||||
impl Perform<CommentResponse> for Oper<CreateComment> {
|
||||
fn perform(&self) -> Result<CommentResponse, Error> {
|
||||
let data: &CreateComment = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
// Check for a community ban
|
||||
let post = Post::read(&conn, data.post_id)?;
|
||||
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
|
||||
return Err(APIError::err(&self.op, "You have been banned from this community"))?
|
||||
}
|
||||
|
||||
// Check for a site ban
|
||||
if UserView::read(&conn, user_id)?.banned {
|
||||
return Err(APIError::err(&self.op, "You have been banned from the site"))?
|
||||
}
|
||||
|
||||
let content_slurs_removed = remove_slurs(&data.content.to_owned());
|
||||
|
||||
let comment_form = CommentForm {
|
||||
content: content_slurs_removed,
|
||||
parent_id: data.parent_id.to_owned(),
|
||||
post_id: data.post_id,
|
||||
creator_id: user_id,
|
||||
removed: None,
|
||||
deleted: None,
|
||||
read: None,
|
||||
updated: None
|
||||
};
|
||||
|
||||
let inserted_comment = match Comment::create(&conn, &comment_form) {
|
||||
Ok(comment) => comment,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't create Comment"))?
|
||||
}
|
||||
};
|
||||
|
||||
// You like your own comment by default
|
||||
let like_form = CommentLikeForm {
|
||||
comment_id: inserted_comment.id,
|
||||
post_id: data.post_id,
|
||||
user_id: user_id,
|
||||
score: 1
|
||||
};
|
||||
|
||||
let _inserted_like = match CommentLike::like(&conn, &like_form) {
|
||||
Ok(like) => like,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't like comment."))?
|
||||
}
|
||||
};
|
||||
|
||||
let comment_view = CommentView::read(&conn, inserted_comment.id, Some(user_id))?;
|
||||
|
||||
Ok(
|
||||
CommentResponse {
|
||||
op: self.op.to_string(),
|
||||
comment: comment_view
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<CommentResponse> for Oper<EditComment> {
|
||||
fn perform(&self) -> Result<CommentResponse, Error> {
|
||||
let data: &EditComment = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
let orig_comment = CommentView::read(&conn, data.edit_id, None)?;
|
||||
|
||||
// You are allowed to mark the comment as read even if you're banned.
|
||||
if data.read.is_none() {
|
||||
|
||||
// Verify its the creator or a mod, or an admin
|
||||
let mut editors: Vec<i32> = vec![data.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(APIError::err(&self.op, "Not allowed to edit comment."))?
|
||||
}
|
||||
|
||||
// Check for a community ban
|
||||
if CommunityUserBanView::get(&conn, user_id, orig_comment.community_id).is_ok() {
|
||||
return Err(APIError::err(&self.op, "You have been banned from this community"))?
|
||||
}
|
||||
|
||||
// Check for a site ban
|
||||
if UserView::read(&conn, user_id)?.banned {
|
||||
return Err(APIError::err(&self.op, "You have been banned from the site"))?
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let content_slurs_removed = remove_slurs(&data.content.to_owned());
|
||||
|
||||
let comment_form = CommentForm {
|
||||
content: content_slurs_removed,
|
||||
parent_id: data.parent_id,
|
||||
post_id: data.post_id,
|
||||
creator_id: data.creator_id,
|
||||
removed: data.removed.to_owned(),
|
||||
deleted: data.deleted.to_owned(),
|
||||
read: data.read.to_owned(),
|
||||
updated: if data.read.is_some() { orig_comment.updated } else {Some(naive_now())}
|
||||
};
|
||||
|
||||
let _updated_comment = match Comment::update(&conn, data.edit_id, &comment_form) {
|
||||
Ok(comment) => comment,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't update Comment"))?
|
||||
}
|
||||
};
|
||||
|
||||
// Mod tables
|
||||
if let Some(removed) = data.removed.to_owned() {
|
||||
let form = ModRemoveCommentForm {
|
||||
mod_user_id: user_id,
|
||||
comment_id: data.edit_id,
|
||||
removed: Some(removed),
|
||||
reason: data.reason.to_owned(),
|
||||
};
|
||||
ModRemoveComment::create(&conn, &form)?;
|
||||
}
|
||||
|
||||
|
||||
let comment_view = CommentView::read(&conn, data.edit_id, Some(user_id))?;
|
||||
|
||||
Ok(
|
||||
CommentResponse {
|
||||
op: self.op.to_string(),
|
||||
comment: comment_view
|
||||
}
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<CommentResponse> for Oper<SaveComment> {
|
||||
fn perform(&self) -> Result<CommentResponse, Error> {
|
||||
let data: &SaveComment = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
let comment_saved_form = CommentSavedForm {
|
||||
comment_id: data.comment_id,
|
||||
user_id: user_id,
|
||||
};
|
||||
|
||||
if data.save {
|
||||
match CommentSaved::save(&conn, &comment_saved_form) {
|
||||
Ok(comment) => comment,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldnt do comment save"))?
|
||||
}
|
||||
};
|
||||
} else {
|
||||
match CommentSaved::unsave(&conn, &comment_saved_form) {
|
||||
Ok(comment) => comment,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldnt do comment save"))?
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let comment_view = CommentView::read(&conn, data.comment_id, Some(user_id))?;
|
||||
|
||||
Ok(
|
||||
CommentResponse {
|
||||
op: self.op.to_string(),
|
||||
comment: comment_view
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<CommentResponse> for Oper<CreateCommentLike> {
|
||||
fn perform(&self) -> Result<CommentResponse, Error> {
|
||||
let data: &CreateCommentLike = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
// Check for a community ban
|
||||
let post = Post::read(&conn, data.post_id)?;
|
||||
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
|
||||
return Err(APIError::err(&self.op, "You have been banned from this community"))?
|
||||
}
|
||||
|
||||
// Check for a site ban
|
||||
if UserView::read(&conn, user_id)?.banned {
|
||||
return Err(APIError::err(&self.op, "You have been banned from the site"))?
|
||||
}
|
||||
|
||||
let like_form = CommentLikeForm {
|
||||
comment_id: data.comment_id,
|
||||
post_id: data.post_id,
|
||||
user_id: user_id,
|
||||
score: data.score
|
||||
};
|
||||
|
||||
// Remove any likes first
|
||||
CommentLike::remove(&conn, &like_form)?;
|
||||
|
||||
// Only add the like if the score isnt 0
|
||||
if &like_form.score != &0 {
|
||||
let _inserted_like = match CommentLike::like(&conn, &like_form) {
|
||||
Ok(like) => like,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't like comment."))?
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Have to refetch the comment to get the current state
|
||||
let liked_comment = CommentView::read(&conn, data.comment_id, Some(user_id))?;
|
||||
|
||||
Ok(
|
||||
CommentResponse {
|
||||
op: self.op.to_string(),
|
||||
comment: liked_comment
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,559 @@
|
||||
use super::*;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetCommunity {
|
||||
id: Option<i32>,
|
||||
name: Option<String>,
|
||||
auth: Option<String>
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetCommunityResponse {
|
||||
op: String,
|
||||
community: CommunityView,
|
||||
moderators: Vec<CommunityModeratorView>,
|
||||
admins: Vec<UserView>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreateCommunity {
|
||||
name: String,
|
||||
title: String,
|
||||
description: Option<String>,
|
||||
category_id: i32 ,
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct CommunityResponse {
|
||||
op: String,
|
||||
pub community: CommunityView
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ListCommunities {
|
||||
sort: String,
|
||||
page: Option<i64>,
|
||||
limit: Option<i64>,
|
||||
auth: Option<String>
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ListCommunitiesResponse {
|
||||
op: String,
|
||||
communities: Vec<CommunityView>
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct BanFromCommunity {
|
||||
pub 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 {
|
||||
pub community_id: i32,
|
||||
user_id: i32,
|
||||
added: bool,
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct AddModToCommunityResponse {
|
||||
op: String,
|
||||
moderators: Vec<CommunityModeratorView>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct EditCommunity {
|
||||
pub edit_id: i32,
|
||||
name: String,
|
||||
title: String,
|
||||
description: Option<String>,
|
||||
category_id: i32,
|
||||
removed: Option<bool>,
|
||||
deleted: Option<bool>,
|
||||
reason: Option<String>,
|
||||
expires: Option<i64>,
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct FollowCommunity {
|
||||
community_id: i32,
|
||||
follow: bool,
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetFollowedCommunities {
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetFollowedCommunitiesResponse {
|
||||
op: String,
|
||||
communities: Vec<CommunityFollowerView>
|
||||
}
|
||||
|
||||
impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
|
||||
fn perform(&self) -> Result<GetCommunityResponse, Error> {
|
||||
let data: &GetCommunity = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let user_id: Option<i32> = match &data.auth {
|
||||
Some(auth) => {
|
||||
match Claims::decode(&auth) {
|
||||
Ok(claims) => {
|
||||
let user_id = claims.claims.id;
|
||||
Some(user_id)
|
||||
}
|
||||
Err(_e) => None
|
||||
}
|
||||
}
|
||||
None => None
|
||||
};
|
||||
|
||||
let community_id = match data.id {
|
||||
Some(id) => id,
|
||||
None => Community::read_from_name(&conn, data.name.to_owned().unwrap_or("main".to_string()))?.id
|
||||
};
|
||||
|
||||
let community_view = match CommunityView::read(&conn, community_id, user_id) {
|
||||
Ok(community) => community,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't find Community"))?
|
||||
}
|
||||
};
|
||||
|
||||
let moderators = match CommunityModeratorView::for_community(&conn, community_id) {
|
||||
Ok(moderators) => moderators,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't find Community"))?
|
||||
}
|
||||
};
|
||||
|
||||
let admins = UserView::admins(&conn)?;
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
GetCommunityResponse {
|
||||
op: self.op.to_string(),
|
||||
community: community_view,
|
||||
moderators: moderators,
|
||||
admins: admins,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<CommunityResponse> for Oper<CreateCommunity> {
|
||||
fn perform(&self) -> Result<CommunityResponse, Error> {
|
||||
let data: &CreateCommunity = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
if has_slurs(&data.name) ||
|
||||
has_slurs(&data.title) ||
|
||||
(data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) {
|
||||
return Err(APIError::err(&self.op, "No slurs"))?
|
||||
}
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
// Check for a site ban
|
||||
if UserView::read(&conn, user_id)?.banned {
|
||||
return Err(APIError::err(&self.op, "You have been banned from the site"))?
|
||||
}
|
||||
|
||||
// When you create a community, make sure the user becomes a moderator and a follower
|
||||
let community_form = CommunityForm {
|
||||
name: data.name.to_owned(),
|
||||
title: data.title.to_owned(),
|
||||
description: data.description.to_owned(),
|
||||
category_id: data.category_id,
|
||||
creator_id: user_id,
|
||||
removed: None,
|
||||
deleted: None,
|
||||
updated: None,
|
||||
};
|
||||
|
||||
let inserted_community = match Community::create(&conn, &community_form) {
|
||||
Ok(community) => community,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Community already exists."))?
|
||||
}
|
||||
};
|
||||
|
||||
let community_moderator_form = CommunityModeratorForm {
|
||||
community_id: inserted_community.id,
|
||||
user_id: user_id
|
||||
};
|
||||
|
||||
let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Community moderator already exists."))?
|
||||
}
|
||||
};
|
||||
|
||||
let community_follower_form = CommunityFollowerForm {
|
||||
community_id: inserted_community.id,
|
||||
user_id: user_id
|
||||
};
|
||||
|
||||
let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Community follower already exists."))?
|
||||
}
|
||||
};
|
||||
|
||||
let community_view = CommunityView::read(&conn, inserted_community.id, Some(user_id))?;
|
||||
|
||||
Ok(
|
||||
CommunityResponse {
|
||||
op: self.op.to_string(),
|
||||
community: community_view
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<CommunityResponse> for Oper<EditCommunity> {
|
||||
fn perform(&self) -> Result<CommunityResponse, Error> {
|
||||
let data: &EditCommunity = &self.data;
|
||||
|
||||
if has_slurs(&data.name) || has_slurs(&data.title) {
|
||||
return Err(APIError::err(&self.op, "No slurs"))?
|
||||
}
|
||||
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
// Check for a site ban
|
||||
if UserView::read(&conn, user_id)?.banned {
|
||||
return Err(APIError::err(&self.op, "You have been banned from the site"))?
|
||||
}
|
||||
|
||||
// Verify its a mod
|
||||
let mut editors: Vec<i32> = Vec::new();
|
||||
editors.append(
|
||||
&mut CommunityModeratorView::for_community(&conn, data.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(APIError::err(&self.op, "Not allowed to edit community"))?
|
||||
}
|
||||
|
||||
let community_form = CommunityForm {
|
||||
name: data.name.to_owned(),
|
||||
title: data.title.to_owned(),
|
||||
description: data.description.to_owned(),
|
||||
category_id: data.category_id.to_owned(),
|
||||
creator_id: user_id,
|
||||
removed: data.removed.to_owned(),
|
||||
deleted: data.deleted.to_owned(),
|
||||
updated: Some(naive_now())
|
||||
};
|
||||
|
||||
let _updated_community = match Community::update(&conn, data.edit_id, &community_form) {
|
||||
Ok(community) => community,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't update Community"))?
|
||||
}
|
||||
};
|
||||
|
||||
// Mod tables
|
||||
if let Some(removed) = data.removed.to_owned() {
|
||||
let expires = match data.expires {
|
||||
Some(time) => Some(naive_from_unix(time)),
|
||||
None => None
|
||||
};
|
||||
let form = ModRemoveCommunityForm {
|
||||
mod_user_id: user_id,
|
||||
community_id: data.edit_id,
|
||||
removed: Some(removed),
|
||||
reason: data.reason.to_owned(),
|
||||
expires: expires
|
||||
};
|
||||
ModRemoveCommunity::create(&conn, &form)?;
|
||||
}
|
||||
|
||||
let community_view = CommunityView::read(&conn, data.edit_id, Some(user_id))?;
|
||||
|
||||
Ok(
|
||||
CommunityResponse {
|
||||
op: self.op.to_string(),
|
||||
community: community_view
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
|
||||
fn perform(&self) -> Result<ListCommunitiesResponse, Error> {
|
||||
let data: &ListCommunities = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let user_id: Option<i32> = match &data.auth {
|
||||
Some(auth) => {
|
||||
match Claims::decode(&auth) {
|
||||
Ok(claims) => {
|
||||
let user_id = claims.claims.id;
|
||||
Some(user_id)
|
||||
}
|
||||
Err(_e) => None
|
||||
}
|
||||
}
|
||||
None => None
|
||||
};
|
||||
|
||||
let sort = SortType::from_str(&data.sort)?;
|
||||
|
||||
let communities: Vec<CommunityView> = CommunityView::list(&conn, user_id, sort, data.page, data.limit)?;
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
ListCommunitiesResponse {
|
||||
op: self.op.to_string(),
|
||||
communities: communities
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Perform<CommunityResponse> for Oper<FollowCommunity> {
|
||||
fn perform(&self) -> Result<CommunityResponse, Error> {
|
||||
let data: &FollowCommunity = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
let community_follower_form = CommunityFollowerForm {
|
||||
community_id: data.community_id,
|
||||
user_id: user_id
|
||||
};
|
||||
|
||||
if data.follow {
|
||||
match CommunityFollower::follow(&conn, &community_follower_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Community follower already exists."))?
|
||||
}
|
||||
};
|
||||
} else {
|
||||
match CommunityFollower::ignore(&conn, &community_follower_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Community follower already exists."))?
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let community_view = CommunityView::read(&conn, data.community_id, Some(user_id))?;
|
||||
|
||||
Ok(
|
||||
CommunityResponse {
|
||||
op: self.op.to_string(),
|
||||
community: community_view
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
|
||||
fn perform(&self) -> Result<GetFollowedCommunitiesResponse, Error> {
|
||||
let data: &GetFollowedCommunities = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
let communities: Vec<CommunityFollowerView> = match CommunityFollowerView::for_user(&conn, user_id) {
|
||||
Ok(communities) => communities,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "System error, try logging out and back in."))?
|
||||
}
|
||||
};
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
GetFollowedCommunitiesResponse {
|
||||
op: self.op.to_string(),
|
||||
communities: communities
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
|
||||
fn perform(&self) -> Result<BanFromCommunityResponse, Error> {
|
||||
let data: &BanFromCommunity = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
let community_user_ban_form = CommunityUserBanForm {
|
||||
community_id: data.community_id,
|
||||
user_id: data.user_id,
|
||||
};
|
||||
|
||||
if data.ban {
|
||||
match CommunityUserBan::ban(&conn, &community_user_ban_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Community user ban already exists"))?
|
||||
}
|
||||
};
|
||||
} else {
|
||||
match CommunityUserBan::unban(&conn, &community_user_ban_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Community user ban already exists"))?
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Mod tables
|
||||
let expires = match data.expires {
|
||||
Some(time) => Some(naive_from_unix(time)),
|
||||
None => None
|
||||
};
|
||||
|
||||
let form = ModBanFromCommunityForm {
|
||||
mod_user_id: user_id,
|
||||
other_user_id: data.user_id,
|
||||
community_id: data.community_id,
|
||||
reason: data.reason.to_owned(),
|
||||
banned: Some(data.ban),
|
||||
expires: expires,
|
||||
};
|
||||
ModBanFromCommunity::create(&conn, &form)?;
|
||||
|
||||
let user_view = UserView::read(&conn, data.user_id)?;
|
||||
|
||||
Ok(
|
||||
BanFromCommunityResponse {
|
||||
op: self.op.to_string(),
|
||||
user: user_view,
|
||||
banned: data.ban
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
|
||||
fn perform(&self) -> Result<AddModToCommunityResponse, Error> {
|
||||
let data: &AddModToCommunity = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
let community_moderator_form = CommunityModeratorForm {
|
||||
community_id: data.community_id,
|
||||
user_id: data.user_id
|
||||
};
|
||||
|
||||
if data.added {
|
||||
match CommunityModerator::join(&conn, &community_moderator_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Community moderator already exists."))?
|
||||
}
|
||||
};
|
||||
} else {
|
||||
match CommunityModerator::leave(&conn, &community_moderator_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Community moderator already exists."))?
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Mod tables
|
||||
let form = ModAddCommunityForm {
|
||||
mod_user_id: user_id,
|
||||
other_user_id: data.user_id,
|
||||
community_id: data.community_id,
|
||||
removed: Some(!data.added),
|
||||
};
|
||||
ModAddCommunity::create(&conn, &form)?;
|
||||
|
||||
let moderators = CommunityModeratorView::for_community(&conn, data.community_id)?;
|
||||
|
||||
Ok(
|
||||
AddModToCommunityResponse {
|
||||
op: self.op.to_string(),
|
||||
moderators: moderators,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use failure::Error;
|
||||
use db::*;
|
||||
use db::community::*;
|
||||
use db::user::*;
|
||||
use db::post::*;
|
||||
use db::comment::*;
|
||||
use db::post_view::*;
|
||||
use db::comment_view::*;
|
||||
use db::category::*;
|
||||
use db::community_view::*;
|
||||
use db::user_view::*;
|
||||
use db::moderator_views::*;
|
||||
use db::moderator::*;
|
||||
use {has_slurs, remove_slurs, Settings, naive_now, naive_from_unix};
|
||||
|
||||
pub mod user;
|
||||
pub mod community;
|
||||
pub mod post;
|
||||
pub mod comment;
|
||||
pub mod site;
|
||||
|
||||
#[derive(EnumString,ToString,Debug)]
|
||||
pub enum UserOperation {
|
||||
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
|
||||
}
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
#[fail(display = "{{\"op\":\"{}\", \"error\":\"{}\"}}", op, message)]
|
||||
pub struct APIError {
|
||||
pub op: String,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl APIError {
|
||||
pub fn err(op: &UserOperation, msg: &str) -> Self {
|
||||
APIError {
|
||||
op: op.to_string(),
|
||||
message: msg.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Oper<T> {
|
||||
op: UserOperation,
|
||||
data: T
|
||||
}
|
||||
|
||||
impl <T> Oper<T> {
|
||||
pub fn new(op: UserOperation, data: T) -> Oper<T> {
|
||||
Oper {
|
||||
op: op,
|
||||
data: data
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Perform<T> {
|
||||
fn perform(&self) -> Result<T, Error> where T: Sized;
|
||||
}
|
@ -0,0 +1,469 @@
|
||||
use super::*;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreatePost {
|
||||
name: String,
|
||||
url: Option<String>,
|
||||
body: Option<String>,
|
||||
community_id: i32,
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct PostResponse {
|
||||
op: String,
|
||||
pub post: PostView
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetPost {
|
||||
pub id: i32,
|
||||
auth: Option<String>
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetPostResponse {
|
||||
op: String,
|
||||
post: PostView,
|
||||
comments: Vec<CommentView>,
|
||||
community: CommunityView,
|
||||
moderators: Vec<CommunityModeratorView>,
|
||||
admins: Vec<UserView>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetPosts {
|
||||
type_: String,
|
||||
sort: String,
|
||||
page: Option<i64>,
|
||||
limit: Option<i64>,
|
||||
community_id: Option<i32>,
|
||||
auth: Option<String>
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetPostsResponse {
|
||||
op: String,
|
||||
posts: Vec<PostView>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreatePostLike {
|
||||
post_id: i32,
|
||||
score: i16,
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreatePostLikeResponse {
|
||||
op: String,
|
||||
post: PostView
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct EditPost {
|
||||
pub edit_id: i32,
|
||||
creator_id: i32,
|
||||
community_id: i32,
|
||||
name: String,
|
||||
url: Option<String>,
|
||||
body: Option<String>,
|
||||
removed: Option<bool>,
|
||||
deleted: Option<bool>,
|
||||
locked: Option<bool>,
|
||||
reason: Option<String>,
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct SavePost {
|
||||
post_id: i32,
|
||||
save: bool,
|
||||
auth: String
|
||||
}
|
||||
|
||||
impl Perform<PostResponse> for Oper<CreatePost> {
|
||||
fn perform(&self) -> Result<PostResponse, Error> {
|
||||
let data: &CreatePost = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
if has_slurs(&data.name) ||
|
||||
(data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) {
|
||||
return Err(APIError::err(&self.op, "No slurs"))?
|
||||
}
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
// Check for a community ban
|
||||
if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() {
|
||||
return Err(APIError::err(&self.op, "You have been banned from this community"))?
|
||||
}
|
||||
|
||||
// Check for a site ban
|
||||
if UserView::read(&conn, user_id)?.banned {
|
||||
return Err(APIError::err(&self.op, "You have been banned from the site"))?
|
||||
}
|
||||
|
||||
let post_form = PostForm {
|
||||
name: data.name.to_owned(),
|
||||
url: data.url.to_owned(),
|
||||
body: data.body.to_owned(),
|
||||
community_id: data.community_id,
|
||||
creator_id: user_id,
|
||||
removed: None,
|
||||
deleted: None,
|
||||
locked: None,
|
||||
updated: None
|
||||
};
|
||||
|
||||
let inserted_post = match Post::create(&conn, &post_form) {
|
||||
Ok(post) => post,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't create Post"))?
|
||||
}
|
||||
};
|
||||
|
||||
// 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
|
||||
let _inserted_like = match PostLike::like(&conn, &like_form) {
|
||||
Ok(like) => like,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't like post."))?
|
||||
}
|
||||
};
|
||||
|
||||
// Refetch the view
|
||||
let post_view = match PostView::read(&conn, inserted_post.id, Some(user_id)) {
|
||||
Ok(post) => post,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't find Post"))?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(
|
||||
PostResponse {
|
||||
op: self.op.to_string(),
|
||||
post: post_view
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<GetPostResponse> for Oper<GetPost> {
|
||||
fn perform(&self) -> Result<GetPostResponse, Error> {
|
||||
let data: &GetPost = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let user_id: Option<i32> = match &data.auth {
|
||||
Some(auth) => {
|
||||
match Claims::decode(&auth) {
|
||||
Ok(claims) => {
|
||||
let user_id = claims.claims.id;
|
||||
Some(user_id)
|
||||
}
|
||||
Err(_e) => None
|
||||
}
|
||||
}
|
||||
None => None
|
||||
};
|
||||
|
||||
let post_view = match PostView::read(&conn, data.id, user_id) {
|
||||
Ok(post) => post,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't find Post"))?
|
||||
}
|
||||
};
|
||||
|
||||
let comments = CommentView::list(&conn, &SortType::New, Some(data.id), None, None, user_id, false, None, Some(9999))?;
|
||||
|
||||
let community = CommunityView::read(&conn, post_view.community_id, user_id)?;
|
||||
|
||||
let moderators = CommunityModeratorView::for_community(&conn, post_view.community_id)?;
|
||||
|
||||
let admins = UserView::admins(&conn)?;
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
GetPostResponse {
|
||||
op: self.op.to_string(),
|
||||
post: post_view,
|
||||
comments: comments,
|
||||
community: community,
|
||||
moderators: moderators,
|
||||
admins: admins,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Perform<GetPostsResponse> for Oper<GetPosts> {
|
||||
fn perform(&self) -> Result<GetPostsResponse, Error> {
|
||||
let data: &GetPosts = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let user_id: Option<i32> = match &data.auth {
|
||||
Some(auth) => {
|
||||
match Claims::decode(&auth) {
|
||||
Ok(claims) => {
|
||||
let user_id = claims.claims.id;
|
||||
Some(user_id)
|
||||
}
|
||||
Err(_e) => None
|
||||
}
|
||||
}
|
||||
None => None
|
||||
};
|
||||
|
||||
let type_ = PostListingType::from_str(&data.type_)?;
|
||||
let sort = SortType::from_str(&data.sort)?;
|
||||
|
||||
let posts = match PostView::list(&conn,
|
||||
type_,
|
||||
&sort,
|
||||
data.community_id,
|
||||
None,
|
||||
None,
|
||||
user_id,
|
||||
false,
|
||||
false,
|
||||
data.page,
|
||||
data.limit) {
|
||||
Ok(posts) => posts,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't get posts"))?
|
||||
}
|
||||
};
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
GetPostsResponse {
|
||||
op: self.op.to_string(),
|
||||
posts: posts
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<CreatePostLikeResponse> for Oper<CreatePostLike> {
|
||||
fn perform(&self) -> Result<CreatePostLikeResponse, Error> {
|
||||
let data: &CreatePostLike = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
// Check for a community ban
|
||||
let post = Post::read(&conn, data.post_id)?;
|
||||
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() {
|
||||
return Err(APIError::err(&self.op, "You have been banned from this community"))?
|
||||
}
|
||||
|
||||
// Check for a site ban
|
||||
if UserView::read(&conn, user_id)?.banned {
|
||||
return Err(APIError::err(&self.op, "You have been banned from the site"))?
|
||||
}
|
||||
|
||||
let like_form = PostLikeForm {
|
||||
post_id: data.post_id,
|
||||
user_id: user_id,
|
||||
score: data.score
|
||||
};
|
||||
|
||||
// Remove any likes first
|
||||
PostLike::remove(&conn, &like_form)?;
|
||||
|
||||
// Only add the like if the score isnt 0
|
||||
if &like_form.score != &0 {
|
||||
let _inserted_like = match PostLike::like(&conn, &like_form) {
|
||||
Ok(like) => like,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't like post."))?
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let post_view = match PostView::read(&conn, data.post_id, Some(user_id)) {
|
||||
Ok(post) => post,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't find Post"))?
|
||||
}
|
||||
};
|
||||
|
||||
// just output the score
|
||||
Ok(
|
||||
CreatePostLikeResponse {
|
||||
op: self.op.to_string(),
|
||||
post: post_view
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<PostResponse> for Oper<EditPost> {
|
||||
fn perform(&self) -> Result<PostResponse, Error> {
|
||||
let data: &EditPost = &self.data;
|
||||
if has_slurs(&data.name) ||
|
||||
(data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) {
|
||||
return Err(APIError::err(&self.op, "No slurs"))?
|
||||
}
|
||||
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
// Verify its the creator or a mod or admin
|
||||
let mut editors: Vec<i32> = vec![data.creator_id];
|
||||
editors.append(
|
||||
&mut CommunityModeratorView::for_community(&conn, data.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(APIError::err(&self.op, "Not allowed to edit post."))?
|
||||
}
|
||||
|
||||
// Check for a community ban
|
||||
if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() {
|
||||
return Err(APIError::err(&self.op, "You have been banned from this community"))?
|
||||
}
|
||||
|
||||
// Check for a site ban
|
||||
if UserView::read(&conn, user_id)?.banned {
|
||||
return Err(APIError::err(&self.op, "You have been banned from the site"))?
|
||||
}
|
||||
|
||||
let post_form = PostForm {
|
||||
name: data.name.to_owned(),
|
||||
url: data.url.to_owned(),
|
||||
body: data.body.to_owned(),
|
||||
creator_id: data.creator_id.to_owned(),
|
||||
community_id: data.community_id,
|
||||
removed: data.removed.to_owned(),
|
||||
deleted: data.deleted.to_owned(),
|
||||
locked: data.locked.to_owned(),
|
||||
updated: Some(naive_now())
|
||||
};
|
||||
|
||||
let _updated_post = match Post::update(&conn, data.edit_id, &post_form) {
|
||||
Ok(post) => post,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't update Post"))?
|
||||
}
|
||||
};
|
||||
|
||||
// Mod tables
|
||||
if let Some(removed) = data.removed.to_owned() {
|
||||
let form = ModRemovePostForm {
|
||||
mod_user_id: user_id,
|
||||
post_id: data.edit_id,
|
||||
removed: Some(removed),
|
||||
reason: data.reason.to_owned(),
|
||||
};
|
||||
ModRemovePost::create(&conn, &form)?;
|
||||
}
|
||||
|
||||
if let Some(locked) = data.locked.to_owned() {
|
||||
let form = ModLockPostForm {
|
||||
mod_user_id: user_id,
|
||||
post_id: data.edit_id,
|
||||
locked: Some(locked),
|
||||
};
|
||||
ModLockPost::create(&conn, &form)?;
|
||||
}
|
||||
|
||||
let post_view = PostView::read(&conn, data.edit_id, Some(user_id))?;
|
||||
|
||||
Ok(
|
||||
PostResponse {
|
||||
op: self.op.to_string(),
|
||||
post: post_view
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<PostResponse> for Oper<SavePost> {
|
||||
fn perform(&self) -> Result<PostResponse, Error> {
|
||||
let data: &SavePost = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
let post_saved_form = PostSavedForm {
|
||||
post_id: data.post_id,
|
||||
user_id: user_id,
|
||||
};
|
||||
|
||||
if data.save {
|
||||
match PostSaved::save(&conn, &post_saved_form) {
|
||||
Ok(post) => post,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldnt do post save"))?
|
||||
}
|
||||
};
|
||||
} else {
|
||||
match PostSaved::unsave(&conn, &post_saved_form) {
|
||||
Ok(post) => post,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldnt do post save"))?
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let post_view = PostView::read(&conn, data.post_id, Some(user_id))?;
|
||||
|
||||
Ok(
|
||||
PostResponse {
|
||||
op: self.op.to_string(),
|
||||
post: post_view
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,336 @@
|
||||
use super::*;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ListCategories;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ListCategoriesResponse {
|
||||
op: String,
|
||||
categories: Vec<Category>
|
||||
}
|
||||
|
||||
#[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>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetModlog {
|
||||
mod_user_id: Option<i32>,
|
||||
community_id: Option<i32>,
|
||||
page: Option<i64>,
|
||||
limit: Option<i64>,
|
||||
}
|
||||
|
||||
#[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 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>,
|
||||
}
|
||||
|
||||
impl Perform<ListCategoriesResponse> for Oper<ListCategories> {
|
||||
fn perform(&self) -> Result<ListCategoriesResponse, Error> {
|
||||
let _data: &ListCategories = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let categories: Vec<Category> = Category::list_all(&conn)?;
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
ListCategoriesResponse {
|
||||
op: self.op.to_string(),
|
||||
categories: categories
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<GetModlogResponse> for Oper<GetModlog> {
|
||||
fn perform(&self) -> Result<GetModlogResponse, Error> {
|
||||
let data: &GetModlog = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let removed_posts = ModRemovePostView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
|
||||
let locked_posts = ModLockPostView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
|
||||
let removed_comments = ModRemoveCommentView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
|
||||
let banned_from_community = ModBanFromCommunityView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
|
||||
let added_to_community = ModAddCommunityView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
|
||||
|
||||
// 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 data.community_id.is_none() {
|
||||
removed_communities = ModRemoveCommunityView::list(&conn, data.mod_user_id, data.page, data.limit)?;
|
||||
banned = ModBanView::list(&conn, data.mod_user_id, data.page, data.limit)?;
|
||||
added = ModAddView::list(&conn, data.mod_user_id, data.page, data.limit)?;
|
||||
}
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
GetModlogResponse {
|
||||
op: self.op.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,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<SiteResponse> for Oper<CreateSite> {
|
||||
fn perform(&self) -> Result<SiteResponse, Error> {
|
||||
let data: &CreateSite = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
if has_slurs(&data.name) ||
|
||||
(data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) {
|
||||
return Err(APIError::err(&self.op, "No slurs"))?
|
||||
}
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
// Make sure user is an admin
|
||||
if !UserView::read(&conn, user_id)?.admin {
|
||||
return Err(APIError::err(&self.op, "Not an admin."))?
|
||||
}
|
||||
|
||||
let site_form = SiteForm {
|
||||
name: data.name.to_owned(),
|
||||
description: data.description.to_owned(),
|
||||
creator_id: user_id,
|
||||
updated: None,
|
||||
};
|
||||
|
||||
match Site::create(&conn, &site_form) {
|
||||
Ok(site) => site,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Site exists already"))?
|
||||
}
|
||||
};
|
||||
|
||||
let site_view = SiteView::read(&conn)?;
|
||||
|
||||
Ok(
|
||||
SiteResponse {
|
||||
op: self.op.to_string(),
|
||||
site: site_view,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Perform<SiteResponse> for Oper<EditSite> {
|
||||
fn perform(&self) -> Result<SiteResponse, Error> {
|
||||
let data: &EditSite = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
if has_slurs(&data.name) ||
|
||||
(data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) {
|
||||
return Err(APIError::err(&self.op, "No slurs"))?
|
||||
}
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
// Make sure user is an admin
|
||||
if UserView::read(&conn, user_id)?.admin == false {
|
||||
return Err(APIError::err(&self.op, "Not an admin."))?
|
||||
}
|
||||
|
||||
let found_site = Site::read(&conn, 1)?;
|
||||
|
||||
let site_form = SiteForm {
|
||||
name: data.name.to_owned(),
|
||||
description: data.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) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't update site."))?
|
||||
}
|
||||
};
|
||||
|
||||
let site_view = SiteView::read(&conn)?;
|
||||
|
||||
Ok(
|
||||
SiteResponse {
|
||||
op: self.op.to_string(),
|
||||
site: site_view,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<GetSiteResponse> for Oper<GetSite> {
|
||||
fn perform(&self) -> Result<GetSiteResponse, Error> {
|
||||
let _data: &GetSite = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
// It can return a null site in order to redirect
|
||||
let site_view = match Site::read(&conn, 1) {
|
||||
Ok(_site) => Some(SiteView::read(&conn)?),
|
||||
Err(_e) => None
|
||||
};
|
||||
|
||||
let admins = UserView::admins(&conn)?;
|
||||
let banned = UserView::banned(&conn)?;
|
||||
|
||||
Ok(
|
||||
GetSiteResponse {
|
||||
op: self.op.to_string(),
|
||||
site: site_view,
|
||||
admins: admins,
|
||||
banned: banned,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<SearchResponse> for Oper<Search> {
|
||||
fn perform(&self) -> Result<SearchResponse, Error> {
|
||||
let data: &Search = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let sort = SortType::from_str(&data.sort)?;
|
||||
let type_ = SearchType::from_str(&data.type_)?;
|
||||
|
||||
let mut posts = Vec::new();
|
||||
let mut comments = Vec::new();
|
||||
|
||||
match type_ {
|
||||
SearchType::Posts => {
|
||||
posts = PostView::list(&conn,
|
||||
PostListingType::All,
|
||||
&sort,
|
||||
data.community_id,
|
||||
None,
|
||||
Some(data.q.to_owned()),
|
||||
None,
|
||||
false,
|
||||
false,
|
||||
data.page,
|
||||
data.limit)?;
|
||||
},
|
||||
SearchType::Comments => {
|
||||
comments = CommentView::list(&conn,
|
||||
&sort,
|
||||
None,
|
||||
None,
|
||||
Some(data.q.to_owned()),
|
||||
None,
|
||||
false,
|
||||
data.page,
|
||||
data.limit)?;
|
||||
},
|
||||
SearchType::Both => {
|
||||
posts = PostView::list(&conn,
|
||||
PostListingType::All,
|
||||
&sort,
|
||||
data.community_id,
|
||||
None,
|
||||
Some(data.q.to_owned()),
|
||||
None,
|
||||
false,
|
||||
false,
|
||||
data.page,
|
||||
data.limit)?;
|
||||
comments = CommentView::list(&conn,
|
||||
&sort,
|
||||
None,
|
||||
None,
|
||||
Some(data.q.to_owned()),
|
||||
None,
|
||||
false,
|
||||
data.page,
|
||||
data.limit)?;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
SearchResponse {
|
||||
op: self.op.to_string(),
|
||||
comments: comments,
|
||||
posts: posts,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,503 @@
|
||||
use super::*;
|
||||
use std::str::FromStr;
|
||||
use bcrypt::{verify};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Login {
|
||||
username_or_email: String,
|
||||
password: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Register {
|
||||
username: String,
|
||||
email: Option<String>,
|
||||
password: String,
|
||||
password_verify: String,
|
||||
admin: bool,
|
||||
spam_timeri: i64,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct LoginResponse {
|
||||
op: String,
|
||||
jwt: String
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetUserDetails {
|
||||
user_id: Option<i32>,
|
||||
username: Option<String>,
|
||||
sort: String,
|
||||
page: Option<i64>,
|
||||
limit: Option<i64>,
|
||||
community_id: Option<i32>,
|
||||
saved_only: bool,
|
||||
auth: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetUserDetailsResponse {
|
||||
op: String,
|
||||
user: UserView,
|
||||
follows: Vec<CommunityFollowerView>,
|
||||
moderates: Vec<CommunityModeratorView>,
|
||||
comments: Vec<CommentView>,
|
||||
posts: Vec<PostView>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetRepliesResponse {
|
||||
op: String,
|
||||
replies: Vec<ReplyView>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct MarkAllAsRead {
|
||||
auth: String
|
||||
}
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GetReplies {
|
||||
sort: String,
|
||||
page: Option<i64>,
|
||||
limit: Option<i64>,
|
||||
unread_only: bool,
|
||||
auth: String
|
||||
}
|
||||
|
||||
impl Perform<LoginResponse> for Oper<Login> {
|
||||
fn perform(&self) -> Result<LoginResponse, Error> {
|
||||
let data: &Login = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
// Fetch that username / email
|
||||
let user: User_ = match User_::find_by_email_or_username(&conn, &data.username_or_email) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => return Err(APIError::err(&self.op, "Couldn't find that username or email"))?
|
||||
};
|
||||
|
||||
// Verify the password
|
||||
let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false);
|
||||
if !valid {
|
||||
return Err(APIError::err(&self.op, "Password incorrect"))?
|
||||
}
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
LoginResponse {
|
||||
op: self.op.to_string(),
|
||||
jwt: user.jwt()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Perform<LoginResponse> for Oper<Register> {
|
||||
fn perform(&self) -> Result<LoginResponse, Error> {
|
||||
let data: &Register = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
// Make sure passwords match
|
||||
if &data.password != &data.password_verify {
|
||||
return Err(APIError::err(&self.op, "Passwords do not match."))?
|
||||
}
|
||||
|
||||
if data.spam_timeri < 1142 {
|
||||
return Err(APIError::err(&self.op, "Too fast"))?
|
||||
}
|
||||
|
||||
if has_slurs(&data.username) {
|
||||
return Err(APIError::err(&self.op, "No slurs"))?
|
||||
}
|
||||
|
||||
// Make sure there are no admins
|
||||
if data.admin && UserView::admins(&conn)?.len() > 0 {
|
||||
return Err(APIError::err(&self.op, "Sorry, there's already an admin."))?
|
||||
}
|
||||
|
||||
// Register the new user
|
||||
let user_form = UserForm {
|
||||
name: data.username.to_owned(),
|
||||
fedi_name: Settings::get().hostname.into(),
|
||||
email: data.email.to_owned(),
|
||||
password_encrypted: data.password.to_owned(),
|
||||
preferred_username: None,
|
||||
updated: None,
|
||||
admin: data.admin,
|
||||
banned: false,
|
||||
};
|
||||
|
||||
// Create the user
|
||||
let inserted_user = match User_::register(&conn, &user_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "User already exists."))?
|
||||
}
|
||||
};
|
||||
|
||||
// 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(APIError::err(&self.op, "Community follower already exists."))?
|
||||
}
|
||||
};
|
||||
|
||||
// If its an admin, add them as a mod and follower to main
|
||||
if data.admin {
|
||||
let community_moderator_form = CommunityModeratorForm {
|
||||
community_id: 1,
|
||||
user_id: inserted_user.id,
|
||||
};
|
||||
|
||||
let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Community moderator already exists."))?
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
LoginResponse {
|
||||
op: self.op.to_string(),
|
||||
jwt: inserted_user.jwt()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
|
||||
fn perform(&self) -> Result<GetUserDetailsResponse, Error> {
|
||||
let data: &GetUserDetails = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let user_id: Option<i32> = match &data.auth {
|
||||
Some(auth) => {
|
||||
match Claims::decode(&auth) {
|
||||
Ok(claims) => {
|
||||
let user_id = claims.claims.id;
|
||||
Some(user_id)
|
||||
}
|
||||
Err(_e) => None
|
||||
}
|
||||
}
|
||||
None => None
|
||||
};
|
||||
|
||||
//TODO add save
|
||||
let sort = SortType::from_str(&data.sort)?;
|
||||
|
||||
let user_details_id = match data.user_id {
|
||||
Some(id) => id,
|
||||
None => User_::read_from_name(&conn, data.username.to_owned().unwrap_or("admin".to_string()))?.id
|
||||
};
|
||||
|
||||
let user_view = UserView::read(&conn, user_details_id)?;
|
||||
|
||||
// If its saved only, you don't care what creator it was
|
||||
let posts = if data.saved_only {
|
||||
PostView::list(&conn,
|
||||
PostListingType::All,
|
||||
&sort,
|
||||
data.community_id,
|
||||
None,
|
||||
None,
|
||||
Some(user_details_id),
|
||||
data.saved_only,
|
||||
false,
|
||||
data.page,
|
||||
data.limit)?
|
||||
} else {
|
||||
PostView::list(&conn,
|
||||
PostListingType::All,
|
||||
&sort,
|
||||
data.community_id,
|
||||
Some(user_details_id),
|
||||
None,
|
||||
user_id,
|
||||
data.saved_only,
|
||||
false,
|
||||
data.page,
|
||||
data.limit)?
|
||||
};
|
||||
let comments = if data.saved_only {
|
||||
CommentView::list(&conn,
|
||||
&sort,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(user_details_id),
|
||||
data.saved_only,
|
||||
data.page,
|
||||
data.limit)?
|
||||
} else {
|
||||
CommentView::list(&conn,
|
||||
&sort,
|
||||
None,
|
||||
Some(user_details_id),
|
||||
None,
|
||||
user_id,
|
||||
data.saved_only,
|
||||
data.page,
|
||||
data.limit)?
|
||||
};
|
||||
|
||||
let follows = CommunityFollowerView::for_user(&conn, user_details_id)?;
|
||||
let moderates = CommunityModeratorView::for_user(&conn, user_details_id)?;
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
GetUserDetailsResponse {
|
||||
op: self.op.to_string(),
|
||||
user: user_view,
|
||||
follows: follows,
|
||||
moderates: moderates,
|
||||
comments: comments,
|
||||
posts: posts,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Perform<AddAdminResponse> for Oper<AddAdmin> {
|
||||
fn perform(&self) -> Result<AddAdminResponse, Error> {
|
||||
let data: &AddAdmin = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
// Make sure user is an admin
|
||||
if UserView::read(&conn, user_id)?.admin == false {
|
||||
return Err(APIError::err(&self.op, "Not an admin."))?
|
||||
}
|
||||
|
||||
let read_user = User_::read(&conn, data.user_id)?;
|
||||
|
||||
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: data.added,
|
||||
banned: read_user.banned,
|
||||
};
|
||||
|
||||
match User_::update(&conn, data.user_id, &user_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't update user"))?
|
||||
}
|
||||
};
|
||||
|
||||
// Mod tables
|
||||
let form = ModAddForm {
|
||||
mod_user_id: user_id,
|
||||
other_user_id: data.user_id,
|
||||
removed: Some(!data.added),
|
||||
};
|
||||
|
||||
ModAdd::create(&conn, &form)?;
|
||||
|
||||
let admins = UserView::admins(&conn)?;
|
||||
|
||||
Ok(
|
||||
AddAdminResponse {
|
||||
op: self.op.to_string(),
|
||||
admins: admins,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<BanUserResponse> for Oper<BanUser> {
|
||||
fn perform(&self) -> Result<BanUserResponse, Error> {
|
||||
let data: &BanUser = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
// Make sure user is an admin
|
||||
if UserView::read(&conn, user_id)?.admin == false {
|
||||
return Err(APIError::err(&self.op, "Not an admin."))?
|
||||
}
|
||||
|
||||
let read_user = User_::read(&conn, data.user_id)?;
|
||||
|
||||
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: data.ban,
|
||||
};
|
||||
|
||||
match User_::update(&conn, data.user_id, &user_form) {
|
||||
Ok(user) => user,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Couldn't update user"))?
|
||||
}
|
||||
};
|
||||
|
||||
// Mod tables
|
||||
let expires = match data.expires {
|
||||
Some(time) => Some(naive_from_unix(time)),
|
||||
None => None
|
||||
};
|
||||
|
||||
let form = ModBanForm {
|
||||
mod_user_id: user_id,
|
||||
other_user_id: data.user_id,
|
||||
reason: data.reason.to_owned(),
|
||||
banned: Some(data.ban),
|
||||
expires: expires,
|
||||
};
|
||||
|
||||
ModBan::create(&conn, &form)?;
|
||||
|
||||
let user_view = UserView::read(&conn, data.user_id)?;
|
||||
|
||||
Ok(
|
||||
BanUserResponse {
|
||||
op: self.op.to_string(),
|
||||
user: user_view,
|
||||
banned: data.ban
|
||||
}
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<GetRepliesResponse> for Oper<GetReplies> {
|
||||
fn perform(&self) -> Result<GetRepliesResponse, Error> {
|
||||
let data: &GetReplies = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
let user_id = claims.id;
|
||||
|
||||
let sort = SortType::from_str(&data.sort)?;
|
||||
|
||||
let replies = ReplyView::get_replies(&conn, user_id, &sort, data.unread_only, data.page, data.limit)?;
|
||||
|
||||
// Return the jwt
|
||||
Ok(
|
||||
GetRepliesResponse {
|
||||
op: self.op.to_string(),
|
||||
replies: replies,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
|
||||
fn perform(&self) -> Result<GetRepliesResponse, Error> {
|
||||
let data: &MarkAllAsRead = &self.data;
|
||||
let conn = establish_connection();
|
||||
|
||||
let claims = match Claims::decode(&data.auth) {
|
||||
Ok(claims) => claims.claims,
|
||||
Err(_e) => {
|
||||
return Err(APIError::err(&self.op, "Not logged in."))?
|
||||
}
|
||||
};
|
||||
|
||||
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,
|
||||
deleted: None,
|
||||
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(APIError::err(&self.op, "Couldn't update Comment"))?
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let replies = ReplyView::get_replies(&conn, user_id, &SortType::New, true, Some(1), Some(999))?;
|
||||
|
||||
Ok(
|
||||
GetRepliesResponse {
|
||||
op: self.op.to_string(),
|
||||
replies: replies,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
use diesel::*;
|
||||
use diesel::dsl::*;
|
||||
use diesel::result::Error;
|
||||
use {Settings};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod user;
|
||||
pub mod community;
|
||||
pub mod post;
|
||||
pub mod comment;
|
||||
pub mod post_view;
|
||||
pub mod comment_view;
|
||||
pub mod category;
|
||||
pub mod community_view;
|
||||
pub mod user_view;
|
||||
pub mod moderator;
|
||||
pub mod moderator_views;
|
||||
|
||||
pub trait Crud<T> {
|
||||
fn create(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
|
||||
fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> where Self: Sized;
|
||||
fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error> where Self: Sized;
|
||||
fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error> where Self: Sized;
|
||||
}
|
||||
|
||||
pub trait Followable<T> {
|
||||
fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
|
||||
fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
|
||||
}
|
||||
|
||||
pub trait Joinable<T> {
|
||||
fn join(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
|
||||
fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
|
||||
}
|
||||
|
||||
pub trait Likeable<T> {
|
||||
fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error> where Self: Sized;
|
||||
fn like(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
|
||||
fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
|
||||
}
|
||||
|
||||
pub trait Bannable<T> {
|
||||
fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
|
||||
fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
|
||||
}
|
||||
|
||||
pub trait Saveable<T> {
|
||||
fn save(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
|
||||
fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
|
||||
}
|
||||
|
||||
pub trait Readable<T> {
|
||||
fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
|
||||
fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
|
||||
}
|
||||
|
||||
pub fn establish_connection() -> PgConnection {
|
||||
let db_url = Settings::get().db_url;
|
||||
PgConnection::establish(&db_url)
|
||||
.expect(&format!("Error connecting to {}", db_url))
|
||||
}
|
||||
|
||||
#[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
|
||||
pub enum SortType {
|
||||
Hot, New, TopDay, TopWeek, TopMonth, TopYear, TopAll
|
||||
}
|
||||
|
||||
#[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
|
||||
pub enum SearchType {
|
||||
Both, Comments, Posts
|
||||
}
|
||||
|
||||
pub fn fuzzy_search(q: &str) -> String {
|
||||
let replaced = q.replace(" ", "%");
|
||||
format!("%{}%", replaced)
|
||||
}
|
||||
|
||||
pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
|
||||
let page = page.unwrap_or(1);
|
||||
let limit = limit.unwrap_or(10);
|
||||
let offset = limit * (page - 1);
|
||||
(limit, offset)
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::fuzzy_search;
|
||||
#[test] fn test_fuzzy_search() {
|
||||
let test = "This is a fuzzy search";
|
||||
assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,486 @@
|
||||
//! `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};
|
||||
use serde_json::{Value};
|
||||
use std::str::FromStr;
|
||||
use failure::Error;
|
||||
use std::time::{SystemTime};
|
||||
|
||||
use api::*;
|
||||
use api::user::*;
|
||||
use api::community::*;
|
||||
use api::post::*;
|
||||
use api::comment::*;
|
||||
use api::site::*;
|
||||
|
||||
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;
|
||||
|
||||
|
||||
/// 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>,
|
||||
pub ip: String,
|
||||
}
|
||||
|
||||
/// Session is disconnected
|
||||
#[derive(Message)]
|
||||
pub struct Disconnect {
|
||||
pub id: usize,
|
||||
pub ip: String,
|
||||
}
|
||||
|
||||
/// 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,
|
||||
}
|
||||
|
||||
#[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;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RateLimitBucket {
|
||||
last_checked: SystemTime,
|
||||
allowance: f64
|
||||
}
|
||||
|
||||
pub struct SessionInfo {
|
||||
pub addr: Recipient<WSMessage>,
|
||||
pub ip: String,
|
||||
}
|
||||
|
||||
/// `ChatServer` manages chat rooms and responsible for coordinating chat
|
||||
/// session. implementation is super primitive
|
||||
pub struct ChatServer {
|
||||
sessions: HashMap<usize, SessionInfo>, // A map from generated random ID to session addr
|
||||
rate_limits: HashMap<String, RateLimitBucket>,
|
||||
rooms: HashMap<i32, HashSet<usize>>, // A map from room / post name to set of connectionIDs
|
||||
rng: ThreadRng,
|
||||
}
|
||||
|
||||
impl Default for ChatServer {
|
||||
fn default() -> ChatServer {
|
||||
// default room
|
||||
let rooms = HashMap::new();
|
||||
|
||||
ChatServer {
|
||||
sessions: HashMap::new(),
|
||||
rate_limits: HashMap::new(),
|
||||
rooms: rooms,
|
||||
rng: rand::thread_rng(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ChatServer {
|
||||
/// Send message to all users in the room
|
||||
fn send_room_message(&self, room: &i32, message: &str, skip_id: usize) {
|
||||
if let Some(sessions) = self.rooms.get(room) {
|
||||
for id in sessions {
|
||||
if *id != skip_id {
|
||||
if let Some(info) = self.sessions.get(id) {
|
||||
let _ = info.addr.do_send(WSMessage(message.to_owned()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn join_room(&mut self, room_id: i32, id: usize) {
|
||||
// remove session from all rooms
|
||||
for (_n, mut sessions) in &mut self.rooms {
|
||||
sessions.remove(&id);
|
||||
}
|
||||
|
||||
// If the room doesn't exist yet
|
||||
if self.rooms.get_mut(&room_id).is_none() {
|
||||
self.rooms.insert(room_id, HashSet::new());
|
||||
}
|
||||
|
||||
&self.rooms.get_mut(&room_id).unwrap().insert(id);
|
||||
}
|
||||
|
||||
fn send_community_message(&self, community_id: &i32, message: &str, skip_id: usize) -> Result<(), Error> {
|
||||
use db::*;
|
||||
use db::post_view::*;
|
||||
let conn = establish_connection();
|
||||
let posts = PostView::list(&conn,
|
||||
PostListingType::Community,
|
||||
&SortType::New,
|
||||
Some(*community_id),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
false,
|
||||
false,
|
||||
None,
|
||||
Some(9999))?;
|
||||
for post in posts {
|
||||
self.send_room_message(&post.id, message, skip_id);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_rate_limit_register(&mut self, id: usize) -> Result<(), Error> {
|
||||
self.check_rate_limit_full(id, RATE_LIMIT_REGISTER_MESSAGES, RATE_LIMIT_REGISTER_PER_SECOND)
|
||||
}
|
||||
|
||||
fn check_rate_limit(&mut self, id: usize) -> Result<(), Error> {
|
||||
self.check_rate_limit_full(id, RATE_LIMIT_MESSAGES, RATE_LIMIT_PER_SECOND)
|
||||
}
|
||||
|
||||
fn check_rate_limit_full(&mut self, id: usize, rate: i32, per: i32) -> Result<(), Error> {
|
||||
if let Some(info) = self.sessions.get(&id) {
|
||||
if let Some(rate_limit) = self.rate_limits.get_mut(&info.ip) {
|
||||
// The initial value
|
||||
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(APIError {
|
||||
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(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// 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;
|
||||
|
||||
fn handle(&mut self, msg: Connect, _ctx: &mut Context<Self>) -> Self::Result {
|
||||
|
||||
// notify all users in same room
|
||||
// self.send_room_message(&"Main".to_owned(), "Someone joined", 0);
|
||||
|
||||
// register session with random id
|
||||
let id = self.rng.gen::<usize>();
|
||||
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);
|
||||
// }
|
||||
|
||||
// auto join session to Main room
|
||||
// self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id);
|
||||
|
||||
// send id back
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
/// Handler for Disconnect message.
|
||||
impl Handler<Disconnect> for ChatServer {
|
||||
type Result = ();
|
||||
|
||||
fn handle(&mut self, msg: Disconnect, _: &mut Context<Self>) {
|
||||
|
||||
// let mut rooms: Vec<i32> = Vec::new();
|
||||
|
||||
// remove address
|
||||
if self.sessions.remove(&msg.id).is_some() {
|
||||
// remove session from all rooms
|
||||
for (_id, sessions) in &mut self.rooms {
|
||||
if sessions.remove(&msg.id) {
|
||||
// rooms.push(*id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Handler for Message message.
|
||||
impl Handler<StandardMessage> for ChatServer {
|
||||
type Result = MessageResult<StandardMessage>;
|
||||
|
||||
|
||||
fn handle(&mut self, msg: StandardMessage, _: &mut Context<Self>) -> Self::Result {
|
||||
|
||||
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)?;
|
||||
let res = Oper::new(user_operation, login).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::Register => {
|
||||
chat.check_rate_limit_register(msg.id)?;
|
||||
let register: Register = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, register).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::GetUserDetails => {
|
||||
let get_user_details: GetUserDetails = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, get_user_details).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::AddAdmin => {
|
||||
let add_admin: AddAdmin = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, add_admin).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::BanUser => {
|
||||
let ban_user: BanUser = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, ban_user).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::GetReplies => {
|
||||
let get_replies: GetReplies = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, get_replies).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::MarkAllAsRead => {
|
||||
let mark_all_as_read: MarkAllAsRead = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, mark_all_as_read).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::GetCommunity => {
|
||||
let get_community: GetCommunity = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, get_community).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::ListCommunities => {
|
||||
let list_communities: ListCommunities = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, list_communities).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::CreateCommunity => {
|
||||
chat.check_rate_limit_register(msg.id)?;
|
||||
let create_community: CreateCommunity = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, create_community).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::EditCommunity => {
|
||||
let edit_community: EditCommunity = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, edit_community).perform()?;
|
||||
let mut community_sent: CommunityResponse = res.clone();
|
||||
community_sent.community.user_id = None;
|
||||
community_sent.community.subscribed = None;
|
||||
let community_sent_str = serde_json::to_string(&community_sent)?;
|
||||
chat.send_community_message(&community_sent.community.id, &community_sent_str, msg.id)?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::FollowCommunity => {
|
||||
let follow_community: FollowCommunity = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, follow_community).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::GetFollowedCommunities => {
|
||||
let followed_communities: GetFollowedCommunities = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, followed_communities).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::BanFromCommunity => {
|
||||
let ban_from_community: BanFromCommunity = serde_json::from_str(data)?;
|
||||
let community_id = ban_from_community.community_id;
|
||||
let res = Oper::new(user_operation, ban_from_community).perform()?;
|
||||
let res_str = serde_json::to_string(&res)?;
|
||||
chat.send_community_message(&community_id, &res_str, msg.id)?;
|
||||
Ok(res_str)
|
||||
},
|
||||
UserOperation::AddModToCommunity => {
|
||||
let mod_add_to_community: AddModToCommunity = serde_json::from_str(data)?;
|
||||
let community_id = mod_add_to_community.community_id;
|
||||
let res = Oper::new(user_operation, mod_add_to_community).perform()?;
|
||||
let res_str = serde_json::to_string(&res)?;
|
||||
chat.send_community_message(&community_id, &res_str, msg.id)?;
|
||||
Ok(res_str)
|
||||
},
|
||||
UserOperation::ListCategories => {
|
||||
let list_categories: ListCategories = ListCategories;
|
||||
let res = Oper::new(user_operation, list_categories).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::CreatePost => {
|
||||
chat.check_rate_limit_register(msg.id)?;
|
||||
let create_post: CreatePost = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, create_post).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::GetPost => {
|
||||
let get_post: GetPost = serde_json::from_str(data)?;
|
||||
chat.join_room(get_post.id, msg.id);
|
||||
let res = Oper::new(user_operation, get_post).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::GetPosts => {
|
||||
let get_posts: GetPosts = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, get_posts).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::CreatePostLike => {
|
||||
chat.check_rate_limit(msg.id)?;
|
||||
let create_post_like: CreatePostLike = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, create_post_like).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::EditPost => {
|
||||
let edit_post: EditPost = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, edit_post).perform()?;
|
||||
let mut post_sent = res.clone();
|
||||
post_sent.post.my_vote = None;
|
||||
let post_sent_str = serde_json::to_string(&post_sent)?;
|
||||
chat.send_room_message(&post_sent.post.id, &post_sent_str, msg.id);
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::SavePost => {
|
||||
let save_post: SavePost = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, save_post).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::CreateComment => {
|
||||
chat.check_rate_limit(msg.id)?;
|
||||
let create_comment: CreateComment = serde_json::from_str(data)?;
|
||||
let post_id = create_comment.post_id;
|
||||
let res = Oper::new(user_operation, create_comment).perform()?;
|
||||
let mut comment_sent = res.clone();
|
||||
comment_sent.comment.my_vote = None;
|
||||
comment_sent.comment.user_id = None;
|
||||
let comment_sent_str = serde_json::to_string(&comment_sent)?;
|
||||
chat.send_room_message(&post_id, &comment_sent_str, msg.id);
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::EditComment => {
|
||||
let edit_comment: EditComment = serde_json::from_str(data)?;
|
||||
let post_id = edit_comment.post_id;
|
||||
let res = Oper::new(user_operation, edit_comment).perform()?;
|
||||
let mut comment_sent = res.clone();
|
||||
comment_sent.comment.my_vote = None;
|
||||
comment_sent.comment.user_id = None;
|
||||
let comment_sent_str = serde_json::to_string(&comment_sent)?;
|
||||
chat.send_room_message(&post_id, &comment_sent_str, msg.id);
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::SaveComment => {
|
||||
let save_comment: SaveComment = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, save_comment).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::CreateCommentLike => {
|
||||
chat.check_rate_limit(msg.id)?;
|
||||
let create_comment_like: CreateCommentLike = serde_json::from_str(data)?;
|
||||
let post_id = create_comment_like.post_id;
|
||||
let res = Oper::new(user_operation, create_comment_like).perform()?;
|
||||
let mut comment_sent = res.clone();
|
||||
comment_sent.comment.my_vote = None;
|
||||
comment_sent.comment.user_id = None;
|
||||
let comment_sent_str = serde_json::to_string(&comment_sent)?;
|
||||
chat.send_room_message(&post_id, &comment_sent_str, msg.id);
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::GetModlog => {
|
||||
let get_modlog: GetModlog = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, get_modlog).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::CreateSite => {
|
||||
let create_site: CreateSite = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, create_site).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::EditSite => {
|
||||
let edit_site: EditSite = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, edit_site).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::GetSite => {
|
||||
let get_site: GetSite = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, get_site).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
UserOperation::Search => {
|
||||
let search: Search = serde_json::from_str(data)?;
|
||||
let res = Oper::new(user_operation, search).perform()?;
|
||||
Ok(serde_json::to_string(&res)?)
|
||||
},
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue