api endpoint for all languages, fix tests, context, adjust migrations

This commit is contained in:
Felix Ableitner 2021-04-28 17:06:06 +02:00
parent 9d66314c32
commit 4c66675e55
7 changed files with 39 additions and 5 deletions

View file

@ -1,4 +1,4 @@
use lemmy_db_schema::{CommunityId, PersonId};
use lemmy_db_schema::{CommunityId, PersonId, PrimaryLanguageTag};
use lemmy_db_views::{
comment_view::CommentView,
local_user_view::LocalUserSettingsView,
@ -88,6 +88,7 @@ pub struct EditSite {
pub open_registration: Option<bool>,
pub enable_nsfw: Option<bool>,
pub community_creation_admin_only: Option<bool>,
pub languages: Vec<PrimaryLanguageTag>,
pub auth: String,
}
@ -140,3 +141,11 @@ pub struct FederatedInstances {
pub allowed: Option<Vec<String>>,
pub blocked: Option<Vec<String>>,
}
#[derive(Deserialize)]
pub struct GetLanguages {}
#[derive(Serialize, Clone)]
pub struct LanguagesResponse {
pub languages: Vec<PrimaryLanguageTag>,
}

View file

@ -7,6 +7,7 @@ use lemmy_api_common::{
person::Register,
site::*,
};
use lemmy_db_schema::PrimaryLanguageTag;
use lemmy_db_views::site_view::SiteView;
use lemmy_db_views_actor::person_view::PersonViewSafe;
use lemmy_utils::{settings::structs::Settings, version, ConnectionId, LemmyError};
@ -98,3 +99,22 @@ impl PerformCrud for GetSite {
})
}
}
#[async_trait::async_trait(?Send)]
impl PerformCrud for GetLanguages {
type Response = LanguagesResponse;
async fn perform(
&self,
_context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<LanguagesResponse, LemmyError> {
// TODO: get an actual language list
// https://github.com/pyfisch/rust-language-tags/issues/32
let languages = vec!["en", "es"]
.iter()
.map(|l| PrimaryLanguageTag(l.to_string()))
.collect();
Ok(LanguagesResponse { languages })
}
}

View file

@ -10,7 +10,7 @@ pub fn lemmy_context() -> Result<Vec<AnyBase>, LemmyError> {
"sensitive": "as:sensitive",
"stickied": "as:stickied",
"pt": "https://join.lemmy.ml#",
"comments_enabled": {
"commentsEnabled": {
"type": "sc:Boolean",
"id": "pt:commentsEnabled"
},
@ -19,6 +19,9 @@ pub fn lemmy_context() -> Result<Vec<AnyBase>, LemmyError> {
"type": "sc:Text",
"id": "as:alsoKnownAs"
},
"languages": {
"type": "sc:Text"
},
}))?;
Ok(vec![
AnyBase::from(context()),

View file

@ -522,6 +522,7 @@ mod tests {
name: bot_post_name,
creator_id: inserted_bot.id,
community_id: inserted_community.id,
language: Some(PrimaryLanguageTag("en".to_string())),
..PostForm::default()
};

View file

@ -2,5 +2,5 @@ ALTER TABLE comment DROP COLUMN language;
ALTER TABLE post DROP COLUMN language;
ALTER TABLE private_message DROP COLUMN language;
DROP TABLE discussion_languages;
ALTER TABLE local_user DROP COLUMN discussion_languages;
ALTER TABLE local_user RENAME COLUMN interface_language TO lang;

View file

@ -2,5 +2,5 @@ ALTER TABLE comment ADD COLUMN language TEXT NOT NULL;
ALTER TABLE post ADD COLUMN language TEXT NOT NULL;
ALTER TABLE private_message ADD COLUMN language TEXT NOT NULL;
CREATE TABLE discussion_languages(id INTEGER PRIMARY KEY, local_user_id INT NOT NULL, language TEXT NOT NULL);
ALTER TABLE local_user ADD COLUMN discussion_languages TEXT[] NOT NULL;
ALTER TABLE local_user RENAME COLUMN lang TO interface_language;

View file

@ -21,7 +21,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
.route("", web::put().to(route_post_crud::<EditSite>))
.route("/transfer", web::post().to(route_post::<TransferSite>))
.route("/config", web::get().to(route_get::<GetSiteConfig>))
.route("/config", web::put().to(route_post::<SaveSiteConfig>)),
.route("/config", web::put().to(route_post::<SaveSiteConfig>))
.route("/languages", web::get().to(route_get_crud::<GetLanguages>)),
)
.service(
web::resource("/modlog")