4 #[derive(Serialize, Deserialize)]
5 pub struct ListCategories;
7 #[derive(Serialize, Deserialize)]
8 pub struct ListCategoriesResponse {
10 categories: Vec<Category>,
13 #[derive(Serialize, Deserialize)]
17 community_id: Option<i32>,
23 #[derive(Serialize, Deserialize)]
24 pub struct SearchResponse {
27 comments: Vec<CommentView>,
29 communities: Vec<CommunityView>,
33 #[derive(Serialize, Deserialize)]
34 pub struct GetModlog {
35 mod_user_id: Option<i32>,
36 community_id: Option<i32>,
41 #[derive(Serialize, Deserialize)]
42 pub struct GetModlogResponse {
44 removed_posts: Vec<ModRemovePostView>,
45 locked_posts: Vec<ModLockPostView>,
46 stickied_posts: Vec<ModStickyPostView>,
47 removed_comments: Vec<ModRemoveCommentView>,
48 removed_communities: Vec<ModRemoveCommunityView>,
49 banned_from_community: Vec<ModBanFromCommunityView>,
50 banned: Vec<ModBanView>,
51 added_to_community: Vec<ModAddCommunityView>,
52 added: Vec<ModAddView>,
55 #[derive(Serialize, Deserialize)]
56 pub struct CreateSite {
58 description: Option<String>,
62 #[derive(Serialize, Deserialize)]
65 description: Option<String>,
69 #[derive(Serialize, Deserialize)]
72 #[derive(Serialize, Deserialize)]
73 pub struct SiteResponse {
78 #[derive(Serialize, Deserialize)]
79 pub struct GetSiteResponse {
81 site: Option<SiteView>,
82 admins: Vec<UserView>,
83 banned: Vec<UserView>,
87 #[derive(Serialize, Deserialize)]
88 pub struct TransferSite {
93 impl Perform<ListCategoriesResponse> for Oper<ListCategories> {
94 fn perform(&self) -> Result<ListCategoriesResponse, Error> {
95 let _data: &ListCategories = &self.data;
96 let conn = establish_connection();
98 let categories: Vec<Category> = Category::list_all(&conn)?;
101 Ok(ListCategoriesResponse {
102 op: self.op.to_string(),
103 categories: categories,
108 impl Perform<GetModlogResponse> for Oper<GetModlog> {
109 fn perform(&self) -> Result<GetModlogResponse, Error> {
110 let data: &GetModlog = &self.data;
111 let conn = establish_connection();
113 let removed_posts = ModRemovePostView::list(
120 let locked_posts = ModLockPostView::list(
127 let stickied_posts = ModStickyPostView::list(
134 let removed_comments = ModRemoveCommentView::list(
141 let banned_from_community = ModBanFromCommunityView::list(
148 let added_to_community = ModAddCommunityView::list(
156 // These arrays are only for the full modlog, when a community isn't given
157 let mut removed_communities = Vec::new();
158 let mut banned = Vec::new();
159 let mut added = Vec::new();
161 if data.community_id.is_none() {
162 removed_communities =
163 ModRemoveCommunityView::list(&conn, data.mod_user_id, data.page, data.limit)?;
164 banned = ModBanView::list(&conn, data.mod_user_id, data.page, data.limit)?;
165 added = ModAddView::list(&conn, data.mod_user_id, data.page, data.limit)?;
169 Ok(GetModlogResponse {
170 op: self.op.to_string(),
171 removed_posts: removed_posts,
172 locked_posts: locked_posts,
173 stickied_posts: stickied_posts,
174 removed_comments: removed_comments,
175 removed_communities: removed_communities,
176 banned_from_community: banned_from_community,
178 added_to_community: added_to_community,
184 impl Perform<SiteResponse> for Oper<CreateSite> {
185 fn perform(&self) -> Result<SiteResponse, Error> {
186 let data: &CreateSite = &self.data;
187 let conn = establish_connection();
189 let claims = match Claims::decode(&data.auth) {
190 Ok(claims) => claims.claims,
191 Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
194 if has_slurs(&data.name)
195 || (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap()))
197 return Err(APIError::err(&self.op, "no_slurs"))?;
200 let user_id = claims.id;
202 // Make sure user is an admin
203 if !UserView::read(&conn, user_id)?.admin {
204 return Err(APIError::err(&self.op, "not_an_admin"))?;
207 let site_form = SiteForm {
208 name: data.name.to_owned(),
209 description: data.description.to_owned(),
214 match Site::create(&conn, &site_form) {
216 Err(_e) => return Err(APIError::err(&self.op, "site_already_exists"))?,
219 let site_view = SiteView::read(&conn)?;
222 op: self.op.to_string(),
228 impl Perform<SiteResponse> for Oper<EditSite> {
229 fn perform(&self) -> Result<SiteResponse, Error> {
230 let data: &EditSite = &self.data;
231 let conn = establish_connection();
233 let claims = match Claims::decode(&data.auth) {
234 Ok(claims) => claims.claims,
235 Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
238 if has_slurs(&data.name)
239 || (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap()))
241 return Err(APIError::err(&self.op, "no_slurs"))?;
244 let user_id = claims.id;
246 // Make sure user is an admin
247 if UserView::read(&conn, user_id)?.admin == false {
248 return Err(APIError::err(&self.op, "not_an_admin"))?;
251 let found_site = Site::read(&conn, 1)?;
253 let site_form = SiteForm {
254 name: data.name.to_owned(),
255 description: data.description.to_owned(),
256 creator_id: found_site.creator_id,
257 updated: Some(naive_now()),
260 match Site::update(&conn, 1, &site_form) {
262 Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_site"))?,
265 let site_view = SiteView::read(&conn)?;
268 op: self.op.to_string(),
274 impl Perform<GetSiteResponse> for Oper<GetSite> {
275 fn perform(&self) -> Result<GetSiteResponse, Error> {
276 let _data: &GetSite = &self.data;
277 let conn = establish_connection();
279 // It can return a null site in order to redirect
280 let site_view = match Site::read(&conn, 1) {
281 Ok(_site) => Some(SiteView::read(&conn)?),
285 let mut admins = UserView::admins(&conn)?;
286 if site_view.is_some() {
287 let site_creator_id = site_view.to_owned().unwrap().creator_id;
288 let creator_index = admins.iter().position(|r| r.id == site_creator_id).unwrap();
289 let creator_user = admins.remove(creator_index);
290 admins.insert(0, creator_user);
293 let banned = UserView::banned(&conn)?;
296 op: self.op.to_string(),
305 impl Perform<SearchResponse> for Oper<Search> {
306 fn perform(&self) -> Result<SearchResponse, Error> {
307 let data: &Search = &self.data;
308 let conn = establish_connection();
310 let sort = SortType::from_str(&data.sort)?;
311 let type_ = SearchType::from_str(&data.type_)?;
313 let mut posts = Vec::new();
314 let mut comments = Vec::new();
315 let mut communities = Vec::new();
316 let mut users = Vec::new();
318 // TODO no clean / non-nsfw searching rn
321 SearchType::Posts => {
322 posts = PostView::list(
324 PostListingType::All,
328 Some(data.q.to_owned()),
338 SearchType::Comments => {
339 comments = CommentView::list(
344 Some(data.q.to_owned()),
351 SearchType::Communities => {
352 communities = CommunityView::list(
357 Some(data.q.to_owned()),
362 SearchType::Users => {
363 users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?;
366 posts = PostView::list(
368 PostListingType::All,
372 Some(data.q.to_owned()),
381 comments = CommentView::list(
386 Some(data.q.to_owned()),
392 communities = CommunityView::list(
397 Some(data.q.to_owned()),
401 users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?;
404 posts = PostView::list(
406 PostListingType::All,
411 Some(data.q.to_owned()),
424 op: self.op.to_string(),
425 type_: data.type_.to_owned(),
428 communities: communities,
434 impl Perform<GetSiteResponse> for Oper<TransferSite> {
435 fn perform(&self) -> Result<GetSiteResponse, Error> {
436 let data: &TransferSite = &self.data;
437 let conn = establish_connection();
439 let claims = match Claims::decode(&data.auth) {
440 Ok(claims) => claims.claims,
441 Err(_e) => return Err(APIError::err(&self.op, "not_logged_in"))?,
444 let user_id = claims.id;
446 let read_site = Site::read(&conn, 1)?;
448 // Make sure user is the creator
449 if read_site.creator_id != user_id {
450 return Err(APIError::err(&self.op, "not_an_admin"))?;
453 let site_form = SiteForm {
454 name: read_site.name,
455 description: read_site.description,
456 creator_id: data.user_id,
457 updated: Some(naive_now()),
460 match Site::update(&conn, 1, &site_form) {
462 Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_site"))?,
466 let form = ModAddForm {
467 mod_user_id: user_id,
468 other_user_id: data.user_id,
469 removed: Some(false),
472 ModAdd::create(&conn, &form)?;
474 let site_view = SiteView::read(&conn)?;
476 let mut admins = UserView::admins(&conn)?;
477 let creator_index = admins
479 .position(|r| r.id == site_view.creator_id)
481 let creator_user = admins.remove(creator_index);
482 admins.insert(0, creator_user);
484 let banned = UserView::banned(&conn)?;
487 op: self.op.to_string(),
488 site: Some(site_view),