From 4c66675e55434791f8faed0ecb9cd347f1649fe8 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 28 Apr 2021 17:06:06 +0200 Subject: [PATCH] api endpoint for all languages, fix tests, context, adjust migrations --- crates/api_common/src/site.rs | 11 +++++++++- crates/api_crud/src/site/read.rs | 20 +++++++++++++++++++ crates/apub/src/extensions/context.rs | 5 ++++- crates/db_views/src/post_view.rs | 1 + .../down.sql | 2 +- .../up.sql | 2 +- src/api_routes.rs | 3 ++- 7 files changed, 39 insertions(+), 5 deletions(-) rename migrations/{2021-04-16-130927_language-tags => 2021-04-28-122254_language-tags}/down.sql (77%) rename migrations/{2021-04-16-130927_language-tags => 2021-04-28-122254_language-tags}/up.sql (68%) diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index e5c167f42..39be75966 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -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, pub enable_nsfw: Option, pub community_creation_admin_only: Option, + pub languages: Vec, pub auth: String, } @@ -140,3 +141,11 @@ pub struct FederatedInstances { pub allowed: Option>, pub blocked: Option>, } + +#[derive(Deserialize)] +pub struct GetLanguages {} + +#[derive(Serialize, Clone)] +pub struct LanguagesResponse { + pub languages: Vec, +} diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index 33282e6bd..f7d0d7169 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -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, + _websocket_id: Option, + ) -> Result { + // 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 }) + } +} diff --git a/crates/apub/src/extensions/context.rs b/crates/apub/src/extensions/context.rs index 63ae2c6d0..c85342b93 100644 --- a/crates/apub/src/extensions/context.rs +++ b/crates/apub/src/extensions/context.rs @@ -10,7 +10,7 @@ pub fn lemmy_context() -> Result, 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, LemmyError> { "type": "sc:Text", "id": "as:alsoKnownAs" }, + "languages": { + "type": "sc:Text" + }, }))?; Ok(vec![ AnyBase::from(context()), diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index c2e2c8b40..3b50d145e 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -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() }; diff --git a/migrations/2021-04-16-130927_language-tags/down.sql b/migrations/2021-04-28-122254_language-tags/down.sql similarity index 77% rename from migrations/2021-04-16-130927_language-tags/down.sql rename to migrations/2021-04-28-122254_language-tags/down.sql index 36125b78d..a5f9ba2cf 100644 --- a/migrations/2021-04-16-130927_language-tags/down.sql +++ b/migrations/2021-04-28-122254_language-tags/down.sql @@ -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; diff --git a/migrations/2021-04-16-130927_language-tags/up.sql b/migrations/2021-04-28-122254_language-tags/up.sql similarity index 68% rename from migrations/2021-04-16-130927_language-tags/up.sql rename to migrations/2021-04-28-122254_language-tags/up.sql index 0b882d460..312b9e203 100644 --- a/migrations/2021-04-16-130927_language-tags/up.sql +++ b/migrations/2021-04-28-122254_language-tags/up.sql @@ -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; diff --git a/src/api_routes.rs b/src/api_routes.rs index f8e8f7e18..f1f928fc5 100644 --- a/src/api_routes.rs +++ b/src/api_routes.rs @@ -21,7 +21,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { .route("", web::put().to(route_post_crud::)) .route("/transfer", web::post().to(route_post::)) .route("/config", web::get().to(route_get::)) - .route("/config", web::put().to(route_post::)), + .route("/config", web::put().to(route_post::)) + .route("/languages", web::get().to(route_get_crud::)), ) .service( web::resource("/modlog")