- conn.build_transaction().read_write().run(|conn| {
- use crate::schema::local_user_language::dsl::*;
- // Clear the current user languages
- delete(local_user_language.filter(local_user_id.eq(for_local_user_id))).execute(conn)?;
-
- let lang_ids = convert_update_languages(conn, language_ids)?;
- for l in lang_ids {
- let form = LocalUserLanguageForm {
- local_user_id: for_local_user_id,
- language_id: l,
- };
- insert_into(local_user_language)
- .values(form)
- .get_result::<Self>(conn)?;
- }
- Ok(())
- })
+ let conn = &mut get_conn(pool).await?;
+ let mut lang_ids = convert_update_languages(conn, language_ids).await?;
+
+ // No need to update if languages are unchanged
+ let current = LocalUserLanguage::read(&mut conn.into(), for_local_user_id).await?;
+ if current == lang_ids {
+ return Ok(());
+ }
+
+ // TODO: Force enable undetermined language for all users. This is necessary because many posts
+ // don't have a language tag (e.g. those from other federated platforms), so Lemmy users
+ // won't see them if undetermined language is disabled.
+ // This hack can be removed once a majority of posts have language tags, or when it is
+ // clearer for new users that they need to enable undetermined language.
+ // See https://github.com/LemmyNet/lemmy-ui/issues/999
+ if !lang_ids.contains(&UNDETERMINED_ID) {
+ lang_ids.push(UNDETERMINED_ID);
+ }
+
+ conn
+ .build_transaction()
+ .run(|conn| {
+ Box::pin(async move {
+ use crate::schema::local_user_language::dsl::{local_user_id, local_user_language};
+ // Clear the current user languages
+ delete(local_user_language.filter(local_user_id.eq(for_local_user_id)))
+ .execute(conn)
+ .await?;
+
+ for l in lang_ids {
+ let form = LocalUserLanguageForm {
+ local_user_id: for_local_user_id,
+ language_id: l,
+ };
+ insert_into(local_user_language)
+ .values(form)
+ .get_result::<Self>(conn)
+ .await?;
+ }
+ Ok(())
+ }) as _
+ })
+ .await