5 #[derive(Serialize, Deserialize, Debug)]
7 username_or_email: String,
11 #[derive(Serialize, Deserialize)]
14 email: Option<String>,
16 password_verify: String,
21 #[derive(Serialize, Deserialize)]
22 pub struct SaveUserSettings {
27 #[derive(Serialize, Deserialize)]
28 pub struct LoginResponse {
33 #[derive(Serialize, Deserialize)]
34 pub struct GetUserDetails {
36 username: Option<String>,
40 community_id: Option<i32>,
45 #[derive(Serialize, Deserialize)]
46 pub struct GetUserDetailsResponse {
49 follows: Vec<CommunityFollowerView>,
50 moderates: Vec<CommunityModeratorView>,
51 comments: Vec<CommentView>,
55 #[derive(Serialize, Deserialize)]
56 pub struct GetRepliesResponse {
58 replies: Vec<ReplyView>,
61 #[derive(Serialize, Deserialize)]
62 pub struct MarkAllAsRead {
66 #[derive(Serialize, Deserialize)]
73 #[derive(Serialize, Deserialize)]
74 pub struct AddAdminResponse {
76 admins: Vec<UserView>,
79 #[derive(Serialize, Deserialize)]
83 reason: Option<String>,
88 #[derive(Serialize, Deserialize)]
89 pub struct BanUserResponse {
95 #[derive(Serialize, Deserialize)]
96 pub struct GetReplies {
104 impl Perform<LoginResponse> for Oper<Login> {
105 fn perform(&self) -> Result<LoginResponse, Error> {
106 let data: &Login = &self.data;
107 let conn = establish_connection();
109 // Fetch that username / email
110 let user: User_ = match User_::find_by_email_or_username(&conn, &data.username_or_email) {
112 Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_that_username_or_email"))?
115 // Verify the password
116 let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false);
118 return Err(APIError::err(&self.op, "password_incorrect"))?
124 op: self.op.to_string(),
132 impl Perform<LoginResponse> for Oper<Register> {
133 fn perform(&self) -> Result<LoginResponse, Error> {
134 let data: &Register = &self.data;
135 let conn = establish_connection();
137 // Make sure passwords match
138 if &data.password != &data.password_verify {
139 return Err(APIError::err(&self.op, "passwords_dont_match"))?
142 if has_slurs(&data.username) {
143 return Err(APIError::err(&self.op, "no_slurs"))?
146 // Make sure there are no admins
147 if data.admin && UserView::admins(&conn)?.len() > 0 {
148 return Err(APIError::err(&self.op, "admin_already_created"))?
151 // Register the new user
152 let user_form = UserForm {
153 name: data.username.to_owned(),
154 fedi_name: Settings::get().hostname.into(),
155 email: data.email.to_owned(),
156 password_encrypted: data.password.to_owned(),
157 preferred_username: None,
161 show_nsfw: data.show_nsfw,
165 let inserted_user = match User_::register(&conn, &user_form) {
168 return Err(APIError::err(&self.op, "user_already_exists"))?
172 // Create the main community if it doesn't exist
173 let main_community: Community = match Community::read(&conn, 2) {
176 let community_form = CommunityForm {
177 name: "main".to_string(),
178 title: "The Default Community".to_string(),
179 description: Some("The Default Community".to_string()),
182 creator_id: inserted_user.id,
187 Community::create(&conn, &community_form).unwrap()
191 // Sign them up for main community no matter what
192 let community_follower_form = CommunityFollowerForm {
193 community_id: main_community.id,
194 user_id: inserted_user.id,
197 let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) {
200 return Err(APIError::err(&self.op, "community_follower_already_exists"))?
204 // If its an admin, add them as a mod and follower to main
206 let community_moderator_form = CommunityModeratorForm {
207 community_id: main_community.id,
208 user_id: inserted_user.id,
211 let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
214 return Err(APIError::err(&self.op, "community_moderator_already_exists"))?
223 op: self.op.to_string(),
224 jwt: inserted_user.jwt()
230 impl Perform<LoginResponse> for Oper<SaveUserSettings> {
231 fn perform(&self) -> Result<LoginResponse, Error> {
232 let data: &SaveUserSettings = &self.data;
233 let conn = establish_connection();
235 let claims = match Claims::decode(&data.auth) {
236 Ok(claims) => claims.claims,
238 return Err(APIError::err(&self.op, "not_logged_in"))?
242 let user_id = claims.id;
244 let read_user = User_::read(&conn, user_id)?;
246 let user_form = UserForm {
247 name: read_user.name,
248 fedi_name: read_user.fedi_name,
249 email: read_user.email,
250 password_encrypted: read_user.password_encrypted,
251 preferred_username: read_user.preferred_username,
252 updated: Some(naive_now()),
253 admin: read_user.admin,
254 banned: read_user.banned,
255 show_nsfw: data.show_nsfw,
258 let updated_user = match User_::update(&conn, user_id, &user_form) {
261 return Err(APIError::err(&self.op, "couldnt_update_user"))?
268 op: self.op.to_string(),
269 jwt: updated_user.jwt()
275 impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
276 fn perform(&self) -> Result<GetUserDetailsResponse, Error> {
277 let data: &GetUserDetails = &self.data;
278 let conn = establish_connection();
280 let user_claims: Option<Claims> = match &data.auth {
282 match Claims::decode(&auth) {
292 let user_id = match &user_claims {
293 Some(claims) => Some(claims.id),
297 let show_nsfw = match &user_claims {
298 Some(claims) => claims.show_nsfw,
303 let sort = SortType::from_str(&data.sort)?;
305 let user_details_id = match data.user_id {
307 None => User_::read_from_name(&conn, data.username.to_owned().unwrap_or("admin".to_string()))?.id
310 let user_view = UserView::read(&conn, user_details_id)?;
312 // If its saved only, you don't care what creator it was
313 let posts = if data.saved_only {
316 PostListingType::All,
322 Some(user_details_id),
331 PostListingType::All,
334 Some(user_details_id),
344 let comments = if data.saved_only {
351 Some(user_details_id),
360 Some(user_details_id),
368 let follows = CommunityFollowerView::for_user(&conn, user_details_id)?;
369 let moderates = CommunityModeratorView::for_user(&conn, user_details_id)?;
373 GetUserDetailsResponse {
374 op: self.op.to_string(),
377 moderates: moderates,
386 impl Perform<AddAdminResponse> for Oper<AddAdmin> {
387 fn perform(&self) -> Result<AddAdminResponse, Error> {
388 let data: &AddAdmin = &self.data;
389 let conn = establish_connection();
391 let claims = match Claims::decode(&data.auth) {
392 Ok(claims) => claims.claims,
394 return Err(APIError::err(&self.op, "not_logged_in"))?
398 let user_id = claims.id;
400 // Make sure user is an admin
401 if UserView::read(&conn, user_id)?.admin == false {
402 return Err(APIError::err(&self.op, "not_an_admin"))?
405 let read_user = User_::read(&conn, data.user_id)?;
407 let user_form = UserForm {
408 name: read_user.name,
409 fedi_name: read_user.fedi_name,
410 email: read_user.email,
411 password_encrypted: read_user.password_encrypted,
412 preferred_username: read_user.preferred_username,
413 updated: Some(naive_now()),
415 banned: read_user.banned,
416 show_nsfw: read_user.show_nsfw,
419 match User_::update(&conn, data.user_id, &user_form) {
422 return Err(APIError::err(&self.op, "couldnt_update_user"))?
427 let form = ModAddForm {
428 mod_user_id: user_id,
429 other_user_id: data.user_id,
430 removed: Some(!data.added),
433 ModAdd::create(&conn, &form)?;
435 let site_creator_id = Site::read(&conn, 1)?.creator_id;
436 let mut admins = UserView::admins(&conn)?;
437 let creator_index = admins.iter().position(|r| r.id == site_creator_id).unwrap();
438 let creator_user = admins.remove(creator_index);
439 admins.insert(0, creator_user);
443 op: self.op.to_string(),
450 impl Perform<BanUserResponse> for Oper<BanUser> {
451 fn perform(&self) -> Result<BanUserResponse, Error> {
452 let data: &BanUser = &self.data;
453 let conn = establish_connection();
455 let claims = match Claims::decode(&data.auth) {
456 Ok(claims) => claims.claims,
458 return Err(APIError::err(&self.op, "not_logged_in"))?
462 let user_id = claims.id;
464 // Make sure user is an admin
465 if UserView::read(&conn, user_id)?.admin == false {
466 return Err(APIError::err(&self.op, "not_an_admin"))?
469 let read_user = User_::read(&conn, data.user_id)?;
471 let user_form = UserForm {
472 name: read_user.name,
473 fedi_name: read_user.fedi_name,
474 email: read_user.email,
475 password_encrypted: read_user.password_encrypted,
476 preferred_username: read_user.preferred_username,
477 updated: Some(naive_now()),
478 admin: read_user.admin,
480 show_nsfw: read_user.show_nsfw,
483 match User_::update(&conn, data.user_id, &user_form) {
486 return Err(APIError::err(&self.op, "couldnt_update_user"))?
491 let expires = match data.expires {
492 Some(time) => Some(naive_from_unix(time)),
496 let form = ModBanForm {
497 mod_user_id: user_id,
498 other_user_id: data.user_id,
499 reason: data.reason.to_owned(),
500 banned: Some(data.ban),
504 ModBan::create(&conn, &form)?;
506 let user_view = UserView::read(&conn, data.user_id)?;
510 op: self.op.to_string(),
519 impl Perform<GetRepliesResponse> for Oper<GetReplies> {
520 fn perform(&self) -> Result<GetRepliesResponse, Error> {
521 let data: &GetReplies = &self.data;
522 let conn = establish_connection();
524 let claims = match Claims::decode(&data.auth) {
525 Ok(claims) => claims.claims,
527 return Err(APIError::err(&self.op, "not_logged_in"))?
531 let user_id = claims.id;
533 let sort = SortType::from_str(&data.sort)?;
535 let replies = ReplyView::get_replies(&conn, user_id, &sort, data.unread_only, data.page, data.limit)?;
540 op: self.op.to_string(),
547 impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
548 fn perform(&self) -> Result<GetRepliesResponse, Error> {
549 let data: &MarkAllAsRead = &self.data;
550 let conn = establish_connection();
552 let claims = match Claims::decode(&data.auth) {
553 Ok(claims) => claims.claims,
555 return Err(APIError::err(&self.op, "not_logged_in"))?
559 let user_id = claims.id;
561 let replies = ReplyView::get_replies(&conn, user_id, &SortType::New, true, Some(1), Some(999))?;
563 for reply in &replies {
564 let comment_form = CommentForm {
565 content: reply.to_owned().content,
566 parent_id: reply.to_owned().parent_id,
567 post_id: reply.to_owned().post_id,
568 creator_id: reply.to_owned().creator_id,
572 updated: reply.to_owned().updated
575 let _updated_comment = match Comment::update(&conn, reply.id, &comment_form) {
576 Ok(comment) => comment,
578 return Err(APIError::err(&self.op, "couldnt_update_comment"))?
583 let replies = ReplyView::get_replies(&conn, user_id, &SortType::New, true, Some(1), Some(999))?;
587 op: self.op.to_string(),