Enable site languages for new user (fixes #5234) (#5235)

* Enable site languages for new user (fixes #5234)

* test coverage
This commit is contained in:
Nutomic 2024-11-28 23:03:14 +00:00 committed by GitHub
parent 5a403bc250
commit f6de3b0388
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 5 deletions

View file

@ -23,7 +23,12 @@ import {
unfollows, unfollows,
saveUserSettingsBio, saveUserSettingsBio,
} from "./shared"; } from "./shared";
import { LemmyHttp, SaveUserSettings, UploadImage } from "lemmy-js-client"; import {
EditSite,
LemmyHttp,
SaveUserSettings,
UploadImage,
} from "lemmy-js-client";
import { GetPosts } from "lemmy-js-client/dist/types/GetPosts"; import { GetPosts } from "lemmy-js-client/dist/types/GetPosts";
beforeAll(setupLogins); beforeAll(setupLogins);
@ -149,9 +154,14 @@ test("Create user with Arabic name", async () => {
}); });
test("Create user with accept-language", async () => { test("Create user with accept-language", async () => {
const edit: EditSite = {
discussion_languages: [32],
};
await alpha.editSite(edit);
let lemmy_http = new LemmyHttp(alphaUrl, { let lemmy_http = new LemmyHttp(alphaUrl, {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language#syntax // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language#syntax
headers: { "Accept-Language": "fr-CH, en;q=0.8, de;q=0.7, *;q=0.5" }, headers: { "Accept-Language": "fr-CH, en;q=0.8, *;q=0.5" },
}); });
let user = await registerUser(lemmy_http, alphaUrl); let user = await registerUser(lemmy_http, alphaUrl);

View file

@ -21,8 +21,9 @@ use lemmy_api_common::{
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::PersonAggregates, aggregates::structs::PersonAggregates,
newtypes::{InstanceId, OAuthProviderId}, newtypes::{InstanceId, OAuthProviderId, SiteId},
source::{ source::{
actor_language::SiteLanguage,
captcha_answer::{CaptchaAnswer, CheckCaptchaAnswer}, captcha_answer::{CaptchaAnswer, CheckCaptchaAnswer},
language::Language, language::Language,
local_site::LocalSite, local_site::LocalSite,
@ -145,7 +146,13 @@ pub async fn register(
..LocalUserInsertForm::new(inserted_person.id, Some(data.password.to_string())) ..LocalUserInsertForm::new(inserted_person.id, Some(data.password.to_string()))
}; };
let inserted_local_user = create_local_user(&context, language_tags, &local_user_form).await?; let inserted_local_user = create_local_user(
&context,
language_tags,
&local_user_form,
local_site.site_id,
)
.await?;
if local_site.site_setup && require_registration_application { if local_site.site_setup && require_registration_application {
if let Some(answer) = data.answer.clone() { if let Some(answer) = data.answer.clone() {
@ -358,7 +365,13 @@ pub async fn authenticate_with_oauth(
..LocalUserInsertForm::new(person.id, None) ..LocalUserInsertForm::new(person.id, None)
}; };
local_user = create_local_user(&context, language_tags, &local_user_form).await?; local_user = create_local_user(
&context,
language_tags,
&local_user_form,
local_site.site_id,
)
.await?;
// Create the oauth account // Create the oauth account
let oauth_account_form = let oauth_account_form =
@ -449,15 +462,23 @@ async fn create_local_user(
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
language_tags: Vec<String>, language_tags: Vec<String>,
local_user_form: &LocalUserInsertForm, local_user_form: &LocalUserInsertForm,
local_site_id: SiteId,
) -> Result<LocalUser, LemmyError> { ) -> Result<LocalUser, LemmyError> {
let all_languages = Language::read_all(&mut context.pool()).await?; let all_languages = Language::read_all(&mut context.pool()).await?;
// use hashset to avoid duplicates // use hashset to avoid duplicates
let mut language_ids = HashSet::new(); let mut language_ids = HashSet::new();
// Enable languages from `Accept-Language` header
for l in language_tags { for l in language_tags {
if let Some(found) = all_languages.iter().find(|all| all.code == l) { if let Some(found) = all_languages.iter().find(|all| all.code == l) {
language_ids.insert(found.id); language_ids.insert(found.id);
} }
} }
// Enable site languages. Ignored if all languages are enabled.
let discussion_languages = SiteLanguage::read(&mut context.pool(), local_site_id).await?;
language_ids.extend(discussion_languages);
let language_ids = language_ids.into_iter().collect(); let language_ids = language_ids.into_iter().collect();
let inserted_local_user = let inserted_local_user =