summaryrefslogtreecommitdiffstats
path: root/server/src/api
diff options
context:
space:
mode:
authorFelix Ableitner <me@nutomic.com>2020-04-03 07:22:39 +0200
committerFelix Ableitner <me@nutomic.com>2020-04-03 07:24:46 +0200
commit6a7a262912dbf0ef725261eb6fc2aaaa7a01c84d (patch)
tree59d7dfa6856427acdd66ec142a236c92a8ac403f /server/src/api
parent96c3621a801d96a8d2bffc849d871c297683e387 (diff)
parent9197b39ed6f19b536510318b5446ec2ebb303396 (diff)
Merge branch 'federation_add_fed_columns' of https://yerbamate.dev/dessalines/lemmy into federation
Diffstat (limited to 'server/src/api')
-rw-r--r--server/src/api/community.rs20
-rw-r--r--server/src/api/site.rs48
-rw-r--r--server/src/api/user.rs51
3 files changed, 101 insertions, 18 deletions
diff --git a/server/src/api/community.rs b/server/src/api/community.rs
index 2bc6e357..d0c4b8d3 100644
--- a/server/src/api/community.rs
+++ b/server/src/api/community.rs
@@ -1,5 +1,6 @@
use super::*;
use crate::apub::puller::{get_all_communities, get_remote_community};
+use crate::apub::{gen_keypair_str, make_apub_endpoint, EndpointType};
use crate::settings::Settings;
use diesel::PgConnection;
use std::str::FromStr;
@@ -208,6 +209,8 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
}
// When you create a community, make sure the user becomes a moderator and a follower
+ let (community_public_key, community_private_key) = gen_keypair_str();
+
let community_form = CommunityForm {
name: data.name.to_owned(),
title: data.title.to_owned(),
@@ -218,6 +221,11 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
deleted: None,
nsfw: data.nsfw,
updated: None,
+ actor_id: make_apub_endpoint(EndpointType::Community, &data.name).to_string(),
+ local: true,
+ private_key: Some(community_private_key),
+ public_key: Some(community_public_key),
+ last_refreshed_at: None,
};
let inserted_community = match Community::create(&conn, &community_form) {
@@ -298,6 +306,8 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
return Err(APIError::err("no_community_edit_allowed").into());
}
+ let read_community = Community::read(&conn, data.edit_id)?;
+
let community_form = CommunityForm {
name: data.name.to_owned(),
title: data.title.to_owned(),
@@ -308,6 +318,11 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
deleted: data.deleted.to_owned(),
nsfw: data.nsfw,
updated: Some(naive_now()),
+ actor_id: read_community.actor_id,
+ local: read_community.local,
+ private_key: read_community.private_key,
+ public_key: read_community.public_key,
+ last_refreshed_at: None,
};
let _updated_community = match Community::update(&conn, data.edit_id, &community_form) {
@@ -571,6 +586,11 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
deleted: None,
nsfw: read_community.nsfw,
updated: Some(naive_now()),
+ actor_id: read_community.actor_id,
+ local: read_community.local,
+ private_key: read_community.private_key,
+ public_key: read_community.public_key,
+ last_refreshed_at: None,
};
let _updated_community = match Community::update(&conn, data.community_id, &community_form) {
diff --git a/server/src/api/site.rs b/server/src/api/site.rs
index ef1a2828..6bd90149 100644
--- a/server/src/api/site.rs
+++ b/server/src/api/site.rs
@@ -1,5 +1,9 @@
use super::*;
+use crate::api::user::Register;
+use crate::api::{Oper, Perform};
+use crate::settings::Settings;
use diesel::PgConnection;
+use log::info;
use std::str::FromStr;
#[derive(Serialize, Deserialize)]
@@ -53,12 +57,12 @@ pub struct GetModlogResponse {
#[derive(Serialize, Deserialize)]
pub struct CreateSite {
- name: String,
- description: Option<String>,
- enable_downvotes: bool,
- open_registration: bool,
- enable_nsfw: bool,
- auth: String,
+ pub name: String,
+ pub description: Option<String>,
+ pub enable_downvotes: bool,
+ pub open_registration: bool,
+ pub enable_nsfw: bool,
+ pub auth: String,
}
#[derive(Serialize, Deserialize)]
@@ -277,10 +281,34 @@ impl Perform<GetSiteResponse> for Oper<GetSite> {
fn perform(&self, conn: &PgConnection) -> Result<GetSiteResponse, Error> {
let _data: &GetSite = &self.data;
- // 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 site = Site::read(&conn, 1);
+ let site_view = if site.is_ok() {
+ Some(SiteView::read(&conn)?)
+ } else if let Some(setup) = Settings::get().setup.as_ref() {
+ let register = Register {
+ username: setup.admin_username.to_owned(),
+ email: setup.admin_email.to_owned(),
+ password: setup.admin_password.to_owned(),
+ password_verify: setup.admin_password.to_owned(),
+ admin: true,
+ show_nsfw: true,
+ };
+ let login_response = Oper::new(register).perform(&conn)?;
+ info!("Admin {} created", setup.admin_username);
+
+ let create_site = CreateSite {
+ name: setup.site_name.to_owned(),
+ description: None,
+ enable_downvotes: false,
+ open_registration: false,
+ enable_nsfw: false,
+ auth: login_response.jwt,
+ };
+ Oper::new(create_site).perform(&conn)?;
+ info!("Site {} created", setup.site_name);
+ Some(SiteView::read(&conn)?)
+ } else {
+ None
};
let mut admins = UserView::admins(&conn)?;
diff --git a/server/src/api/user.rs b/server/src/api/user.rs
index 333fd949..59a3d623 100644
--- a/server/src/api/user.rs
+++ b/server/src/api/user.rs
@@ -1,4 +1,5 @@
use super::*;
+use crate::apub::{gen_keypair_str, make_apub_endpoint, EndpointType};
use crate::settings::Settings;
use crate::{generate_random_string, send_email};
use bcrypt::verify;
@@ -14,12 +15,12 @@ pub struct Login {
#[derive(Serialize, Deserialize)]
pub struct Register {
- username: String,
- email: Option<String>,
- password: String,
- password_verify: String,
- admin: bool,
- show_nsfw: bool,
+ pub username: String,
+ pub email: Option<String>,
+ pub password: String,
+ pub password_verify: String,
+ pub admin: bool,
+ pub show_nsfw: bool,
}
#[derive(Serialize, Deserialize)]
@@ -42,7 +43,7 @@ pub struct SaveUserSettings {
#[derive(Serialize, Deserialize)]
pub struct LoginResponse {
- jwt: String,
+ pub jwt: String,
}
#[derive(Serialize, Deserialize)]
@@ -250,6 +251,8 @@ impl Perform<LoginResponse> for Oper<Register> {
return Err(APIError::err("admin_already_created").into());
}
+ let (user_public_key, user_private_key) = gen_keypair_str();
+
// Register the new user
let user_form = UserForm {
name: data.username.to_owned(),
@@ -269,6 +272,12 @@ impl Perform<LoginResponse> for Oper<Register> {
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
+ actor_id: make_apub_endpoint(EndpointType::User, &data.username).to_string(),
+ bio: None,
+ local: true,
+ private_key: Some(user_private_key),
+ public_key: Some(user_public_key),
+ last_refreshed_at: None,
};
// Create the user
@@ -287,12 +296,15 @@ impl Perform<LoginResponse> for Oper<Register> {
}
};
+ let (community_public_key, community_private_key) = gen_keypair_str();
+
// Create the main community if it doesn't exist
let main_community: Community = match Community::read(&conn, 2) {
Ok(c) => c,
Err(_e) => {
+ let default_community_name = "main";
let community_form = CommunityForm {
- name: "main".to_string(),
+ name: default_community_name.to_string(),
title: "The Default Community".to_string(),
description: Some("The Default Community".to_string()),
category_id: 1,
@@ -301,6 +313,11 @@ impl Perform<LoginResponse> for Oper<Register> {
removed: None,
deleted: None,
updated: None,
+ actor_id: make_apub_endpoint(EndpointType::Community, default_community_name).to_string(),
+ local: true,
+ private_key: Some(community_private_key),
+ public_key: Some(community_public_key),
+ last_refreshed_at: None,
};
Community::create(&conn, &community_form).unwrap()
}
@@ -403,6 +420,12 @@ impl Perform<LoginResponse> for Oper<SaveUserSettings> {
lang: data.lang.to_owned(),
show_avatars: data.show_avatars,
send_notifications_to_email: data.send_notifications_to_email,
+ actor_id: read_user.actor_id,
+ bio: read_user.bio,
+ local: read_user.local,
+ private_key: read_user.private_key,
+ public_key: read_user.public_key,
+ last_refreshed_at: None,
};
let updated_user = match User_::update(&conn, user_id, &user_form) {
@@ -561,6 +584,12 @@ impl Perform<AddAdminResponse> for Oper<AddAdmin> {
lang: read_user.lang,
show_avatars: read_user.show_avatars,
send_notifications_to_email: read_user.send_notifications_to_email,
+ actor_id: read_user.actor_id,
+ bio: read_user.bio,
+ local: read_user.local,
+ private_key: read_user.private_key,
+ public_key: read_user.public_key,
+ last_refreshed_at: None,
};
match User_::update(&conn, data.user_id, &user_form) {
@@ -624,6 +653,12 @@ impl Perform<BanUserResponse> for Oper<BanUser> {
lang: read_user.lang,
show_avatars: read_user.show_avatars,
send_notifications_to_email: read_user.send_notifications_to_email,
+ actor_id: read_user.actor_id,
+ bio: read_user.bio,
+ local: read_user.local,
+ private_key: read_user.private_key,
+ public_key: read_user.public_key,
+ last_refreshed_at: None,
};
match User_::update(&conn, data.user_id, &user_form) {