Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix known groups/users label-related default key and typing #1337

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/cmd/known_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn print_group(group: &KnownGroup) {
print!(r#" {}: {{ "label": {{"#, json!(group.role));

// Sort by key to get consistent ordering (hashmap order is random).
let mut labels = group.label.0.iter().collect::<Vec<_>>();
let mut labels = group.label.iter().collect::<Vec<_>>();
labels.sort();
for (lang, label) in labels {
print!(" {}: {}", json!(lang), json!(label));
Expand Down
18 changes: 13 additions & 5 deletions backend/src/model/translated_string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, fmt, str::FromStr};
use std::{collections::HashMap, fmt, ops::Deref, str::FromStr};
use bytes::BytesMut;
use fallible_iterator::FallibleIterator;
use juniper::{GraphQLScalar, InputValue, ScalarValue};
Expand All @@ -13,7 +13,7 @@ use crate::prelude::*;
#[derive(Serialize, Deserialize, Clone, GraphQLScalar)]
#[serde(try_from = "HashMap<LangKey, String>")]
#[graphql(parse_token(String))]
pub(crate) struct TranslatedString(pub(crate) HashMap<LangKey, String>);
pub(crate) struct TranslatedString(HashMap<LangKey, String>);

impl TranslatedString {
pub(crate) fn default(&self) -> &str {
Expand All @@ -35,6 +35,13 @@ impl TranslatedString {
}
}

impl Deref for TranslatedString {
type Target = HashMap<LangKey, String>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl TryFrom<HashMap<LangKey, String>> for TranslatedString {
type Error = Error;

Expand Down Expand Up @@ -112,14 +119,15 @@ impl<'a> FromSql<'a> for TranslatedString {
_: &postgres_types::Type,
raw: &'a [u8],
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
postgres_protocol::types::hstore_from_sql(raw)?
let map: HashMap<LangKey, String> = postgres_protocol::types::hstore_from_sql(raw)?
.map(|(k, v)| {
let v = v.ok_or("translated label contained null value in hstore")?;
let k = k.parse()?;
Ok((k, v.to_owned()))
})
.collect()
.map(Self)
.collect()?;

Ok(map.try_into()?)
}

fn accepts(ty: &postgres_types::Type) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion frontend/relay.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
"Cursor": "string",
"ByteSpan": "string",
"ExtraMetadata": "Record<string, Record<string, string[]>>",
"TranslatedString": "Record<string, string>",
"TranslatedString": "{ default: string } & Record<string, string | undefined>",
},
schemaExtensions: [APP_PATH],
};
2 changes: 1 addition & 1 deletion frontend/src/routes/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ const MetaDataEdit: React.FC<MetaDataEditProps> = ({ onSave, disabled, knownRole
[user.userRole, {
actions: new Set(["read", "write"]),
info: {
label: { "_": user.displayName },
label: { "default": user.displayName },
implies: null,
large: false,
},
Expand Down
17 changes: 10 additions & 7 deletions frontend/src/ui/Access.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ const AclSelect: React.FC<AclSelectProps> = ({ acl, inheritedAcl, kind }) => {
prev.set(option.role, {
actions: new Set([permissionLevels.default]),
info: {
label: info?.label ?? { "_": option.label },
label: info?.label ?? { "default": option.label },
implies: [...info?.implies ?? new Set()],
large: info?.large ?? false,
},
Expand Down Expand Up @@ -883,7 +883,7 @@ export const AclEditButtons: React.FC<AclEditButtonsProps> = ({
// ===== Helper functions
// ==============================================================================================

type TranslatedLabel = Record<string, string>;
type TranslatedLabel = { default: string } & Record<string, string | undefined>;

/** Returns a label for the role, if known to Tobira. */
const getLabel = (role: string, label: TranslatedLabel | undefined, i18n: i18n) => {
Expand Down Expand Up @@ -925,11 +925,14 @@ const insertBuiltinRoleInfo = (
i18n: i18n,
addAnonymous: boolean,
) => {
const keyToTranslatedString = (key: ParseKeys): TranslatedLabel => Object.fromEntries(
i18n.languages
.filter(lng => i18n.exists(key, { lng }))
.map(lng => [lng, i18n.t(key, { lng })])
);
const keyToTranslatedString = (key: ParseKeys): TranslatedLabel => ({
default: i18n.t(key, { lng: "en" }),
...Object.fromEntries(
i18n.languages
.filter(lng => i18n.exists(key, { lng }))
.map(lng => [lng, i18n.t(key, { lng })])
),
});

const anonymousInfo = {
implies: [],
Expand Down
Loading