Use generics to reduce code duplication in websocket

pull/442/head
Felix 2020-01-19 14:25:50 +01:00
parent baf77bb6be
commit e518954bca
1 changed files with 52 additions and 98 deletions

View File

@ -3,7 +3,7 @@
//! room through `ChatServer`. //! room through `ChatServer`.
use actix::prelude::*; use actix::prelude::*;
use diesel::r2d2::{ConnectionManager, Pool}; use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
use diesel::PgConnection; use diesel::PgConnection;
use failure::Error; use failure::Error;
use rand::{rngs::ThreadRng, Rng}; use rand::{rngs::ThreadRng, Rng};
@ -312,6 +312,21 @@ where
Ok(serde_json::to_string(&response)?) Ok(serde_json::to_string(&response)?)
} }
fn do_user_operation<'a, Data, Response>(
op: UserOperation,
data: &str,
conn: &PooledConnection<ConnectionManager<PgConnection>>,
) -> Result<String, Error>
where
for<'de> Data: Deserialize<'de> + 'a,
Response: Serialize,
Oper<Data>: Perform<Response>,
{
let parsed_data: Data = serde_json::from_str(data)?;
let res = Oper::new(parsed_data).perform(&conn)?;
to_json_string(&op, &res)
}
fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<String, Error> { fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<String, Error> {
let json: Value = serde_json::from_str(&msg.msg)?; let json: Value = serde_json::from_str(&msg.msg)?;
let data = &json["data"].to_string(); let data = &json["data"].to_string();
@ -323,75 +338,47 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
let user_operation: UserOperation = UserOperation::from_str(&op)?; let user_operation: UserOperation = UserOperation::from_str(&op)?;
// TODO: none of the chat messages are going to work if stuff is submitted via http api,
// need to move that handling elsewhere
match user_operation { match user_operation {
UserOperation::Login => { UserOperation::Login => do_user_operation::<Login, LoginResponse>(user_operation, data, &conn),
let login: Login = serde_json::from_str(data)?;
let res = Oper::new(login).perform(&conn)?;
to_json_string(&user_operation, &res)
}
UserOperation::Register => { UserOperation::Register => {
let register: Register = serde_json::from_str(data)?; chat.check_rate_limit_register(msg.id)?;
let res = Oper::new(register).perform(&conn); do_user_operation::<Register, LoginResponse>(user_operation, data, &conn)
if res.is_ok() {
chat.check_rate_limit_register(msg.id)?;
}
to_json_string(&user_operation, &res?)
} }
UserOperation::GetUserDetails => { UserOperation::GetUserDetails => {
let get_user_details: GetUserDetails = serde_json::from_str(data)?; do_user_operation::<GetUserDetails, GetUserDetailsResponse>(user_operation, data, &conn)
let res = Oper::new(get_user_details).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::SaveUserSettings => { UserOperation::SaveUserSettings => {
let save_user_settings: SaveUserSettings = serde_json::from_str(data)?; do_user_operation::<SaveUserSettings, LoginResponse>(user_operation, data, &conn)
let res = Oper::new(save_user_settings).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::AddAdmin => { UserOperation::AddAdmin => {
let add_admin: AddAdmin = serde_json::from_str(data)?; do_user_operation::<AddAdmin, AddAdminResponse>(user_operation, data, &conn)
let res = Oper::new(add_admin).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::BanUser => { UserOperation::BanUser => {
let ban_user: BanUser = serde_json::from_str(data)?; do_user_operation::<BanUser, BanUserResponse>(user_operation, data, &conn)
let res = Oper::new(ban_user).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::GetReplies => { UserOperation::GetReplies => {
let get_replies: GetReplies = serde_json::from_str(data)?; do_user_operation::<GetReplies, GetRepliesResponse>(user_operation, data, &conn)
let res = Oper::new(get_replies).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::GetUserMentions => { UserOperation::GetUserMentions => {
let get_user_mentions: GetUserMentions = serde_json::from_str(data)?; do_user_operation::<GetUserMentions, GetUserMentionsResponse>(user_operation, data, &conn)
let res = Oper::new(get_user_mentions).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::EditUserMention => { UserOperation::EditUserMention => {
let edit_user_mention: EditUserMention = serde_json::from_str(data)?; do_user_operation::<EditUserMention, UserMentionResponse>(user_operation, data, &conn)
let res = Oper::new(edit_user_mention).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::MarkAllAsRead => { UserOperation::MarkAllAsRead => {
let mark_all_as_read: MarkAllAsRead = serde_json::from_str(data)?; do_user_operation::<MarkAllAsRead, GetRepliesResponse>(user_operation, data, &conn)
let res = Oper::new(mark_all_as_read).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::GetCommunity => { UserOperation::GetCommunity => {
let get_community: GetCommunity = serde_json::from_str(data)?; do_user_operation::<GetCommunity, GetCommunityResponse>(user_operation, data, &conn)
let res = Oper::new(get_community).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::ListCommunities => { UserOperation::ListCommunities => {
let list_communities: ListCommunities = serde_json::from_str(data)?; do_user_operation::<ListCommunities, ListCommunitiesResponse>(user_operation, data, &conn)
let res = Oper::new(list_communities).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::CreateCommunity => { UserOperation::CreateCommunity => {
chat.check_rate_limit_register(msg.id)?; chat.check_rate_limit_register(msg.id)?;
let create_community: CreateCommunity = serde_json::from_str(data)?; do_user_operation::<CreateCommunity, CommunityResponse>(user_operation, data, &conn)
let res = Oper::new(create_community).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::EditCommunity => { UserOperation::EditCommunity => {
let edit_community: EditCommunity = serde_json::from_str(data)?; let edit_community: EditCommunity = serde_json::from_str(data)?;
@ -404,15 +391,12 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
to_json_string(&user_operation, &res) to_json_string(&user_operation, &res)
} }
UserOperation::FollowCommunity => { UserOperation::FollowCommunity => {
let follow_community: FollowCommunity = serde_json::from_str(data)?; do_user_operation::<FollowCommunity, CommunityResponse>(user_operation, data, &conn)
let res = Oper::new(follow_community).perform(&conn)?;
to_json_string(&user_operation, &res)
}
UserOperation::GetFollowedCommunities => {
let followed_communities: GetFollowedCommunities = serde_json::from_str(data)?;
let res = Oper::new(followed_communities).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::GetFollowedCommunities => do_user_operation::<
GetFollowedCommunities,
GetFollowedCommunitiesResponse,
>(user_operation, data, &conn),
UserOperation::BanFromCommunity => { UserOperation::BanFromCommunity => {
let ban_from_community: BanFromCommunity = serde_json::from_str(data)?; let ban_from_community: BanFromCommunity = serde_json::from_str(data)?;
let community_id = ban_from_community.community_id; let community_id = ban_from_community.community_id;
@ -430,15 +414,11 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
Ok(res_str) Ok(res_str)
} }
UserOperation::ListCategories => { UserOperation::ListCategories => {
let list_categories: ListCategories = ListCategories; do_user_operation::<ListCategories, ListCategoriesResponse>(user_operation, data, &conn)
let res = Oper::new(list_categories).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::CreatePost => { UserOperation::CreatePost => {
chat.check_rate_limit_post(msg.id)?; chat.check_rate_limit_post(msg.id)?;
let create_post: CreatePost = serde_json::from_str(data)?; do_user_operation::<CreatePost, PostResponse>(user_operation, data, &conn)
let res = Oper::new(create_post).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::GetPost => { UserOperation::GetPost => {
let get_post: GetPost = serde_json::from_str(data)?; let get_post: GetPost = serde_json::from_str(data)?;
@ -447,15 +427,11 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
to_json_string(&user_operation, &res) to_json_string(&user_operation, &res)
} }
UserOperation::GetPosts => { UserOperation::GetPosts => {
let get_posts: GetPosts = serde_json::from_str(data)?; do_user_operation::<GetPosts, GetPostsResponse>(user_operation, data, &conn)
let res = Oper::new(get_posts).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::CreatePostLike => { UserOperation::CreatePostLike => {
chat.check_rate_limit_message(msg.id)?; chat.check_rate_limit_message(msg.id)?;
let create_post_like: CreatePostLike = serde_json::from_str(data)?; do_user_operation::<CreatePostLike, CreatePostLikeResponse>(user_operation, data, &conn)
let res = Oper::new(create_post_like).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::EditPost => { UserOperation::EditPost => {
let edit_post: EditPost = serde_json::from_str(data)?; let edit_post: EditPost = serde_json::from_str(data)?;
@ -467,9 +443,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
to_json_string(&user_operation, &res) to_json_string(&user_operation, &res)
} }
UserOperation::SavePost => { UserOperation::SavePost => {
let save_post: SavePost = serde_json::from_str(data)?; do_user_operation::<SavePost, PostResponse>(user_operation, data, &conn)
let res = Oper::new(save_post).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::CreateComment => { UserOperation::CreateComment => {
chat.check_rate_limit_message(msg.id)?; chat.check_rate_limit_message(msg.id)?;
@ -495,9 +469,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
to_json_string(&user_operation, &res) to_json_string(&user_operation, &res)
} }
UserOperation::SaveComment => { UserOperation::SaveComment => {
let save_comment: SaveComment = serde_json::from_str(data)?; do_user_operation::<SaveComment, CommentResponse>(user_operation, data, &conn)
let res = Oper::new(save_comment).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::CreateCommentLike => { UserOperation::CreateCommentLike => {
chat.check_rate_limit_message(msg.id)?; chat.check_rate_limit_message(msg.id)?;
@ -512,19 +484,13 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
to_json_string(&user_operation, &res) to_json_string(&user_operation, &res)
} }
UserOperation::GetModlog => { UserOperation::GetModlog => {
let get_modlog: GetModlog = serde_json::from_str(data)?; do_user_operation::<GetModlog, GetModlogResponse>(user_operation, data, &conn)
let res = Oper::new(get_modlog).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::CreateSite => { UserOperation::CreateSite => {
let create_site: CreateSite = serde_json::from_str(data)?; do_user_operation::<CreateSite, SiteResponse>(user_operation, data, &conn)
let res = Oper::new(create_site).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::EditSite => { UserOperation::EditSite => {
let edit_site: EditSite = serde_json::from_str(data)?; do_user_operation::<EditSite, SiteResponse>(user_operation, data, &conn)
let res = Oper::new(edit_site).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::GetSite => { UserOperation::GetSite => {
let online: usize = chat.sessions.len(); let online: usize = chat.sessions.len();
@ -534,34 +500,22 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
to_json_string(&user_operation, &res) to_json_string(&user_operation, &res)
} }
UserOperation::Search => { UserOperation::Search => {
let search: Search = serde_json::from_str(data)?; do_user_operation::<Search, SearchResponse>(user_operation, data, &conn)
let res = Oper::new(search).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::TransferCommunity => { UserOperation::TransferCommunity => {
let transfer_community: TransferCommunity = serde_json::from_str(data)?; do_user_operation::<TransferCommunity, GetCommunityResponse>(user_operation, data, &conn)
let res = Oper::new(transfer_community).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::TransferSite => { UserOperation::TransferSite => {
let transfer_site: TransferSite = serde_json::from_str(data)?; do_user_operation::<TransferSite, GetSiteResponse>(user_operation, data, &conn)
let res = Oper::new(transfer_site).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::DeleteAccount => { UserOperation::DeleteAccount => {
let delete_account: DeleteAccount = serde_json::from_str(data)?; do_user_operation::<DeleteAccount, LoginResponse>(user_operation, data, &conn)
let res = Oper::new(delete_account).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::PasswordReset => { UserOperation::PasswordReset => {
let password_reset: PasswordReset = serde_json::from_str(data)?; do_user_operation::<PasswordReset, PasswordResetResponse>(user_operation, data, &conn)
let res = Oper::new(password_reset).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
UserOperation::PasswordChange => { UserOperation::PasswordChange => {
let password_change: PasswordChange = serde_json::from_str(data)?; do_user_operation::<PasswordChange, LoginResponse>(user_operation, data, &conn)
let res = Oper::new(password_change).perform(&conn)?;
to_json_string(&user_operation, &res)
} }
} }
} }