summaryrefslogtreecommitdiffstats
path: root/server/src/api/mod.rs
blob: 5ffb57d89db0b8bb02745f0b480644906e349c7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use crate::db::category::*;
use crate::db::comment::*;
use crate::db::comment_view::*;
use crate::db::community::*;
use crate::db::community_view::*;
use crate::db::moderator::*;
use crate::db::moderator_views::*;
use crate::db::post::*;
use crate::db::post_view::*;
use crate::db::user::*;
use crate::db::user_view::*;
use crate::db::*;
use crate::{has_slurs, naive_from_unix, naive_now, remove_slurs, Settings};
use failure::Error;
use serde::{Deserialize, Serialize};

pub mod comment;
pub mod community;
pub mod post;
pub mod site;
pub mod user;

#[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,
  SaveUserSettings,
  TransferCommunity,
  TransferSite,
  DeleteAccount,
}

#[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;
}