Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

custom-profile-fields: Make URL pattern optional for external accounts.

Niloth P3e78efe62247
diff --git a/web/src/settings_profile_fields.ts b/web/src/settings_profile_fields.ts
index 12f211c8f98..47042e2e795 100644
--- a/web/src/settings_profile_fields.ts
+++ b/web/src/settings_profile_fields.ts
@@ -822,17 +822,23 @@ function set_up_select_field(): void {
export function get_external_account_link(
field_data: settings_components.ExternalAccountFieldData,
value: string,
-): string {
+): string | undefined {
const field_subtype = field_data.subtype;
let field_url_pattern: string;
if (field_subtype === "custom") {
- assert(field_data.url_pattern !== undefined);
+ if (!field_data.url_pattern) {
+ return undefined;
+ }
field_url_pattern = field_data.url_pattern;
} else {
const external_account = realm.realm_default_external_accounts[field_subtype];
assert(external_account !== undefined);
field_url_pattern = external_account.url_pattern;
+ // Use as text fields when there is no URL pattern
+ if (!field_url_pattern) {
+ return field_url_pattern;
+ }
}
return field_url_pattern.replace("%(username)s", () => value);
}
diff --git a/web/src/user_profile.ts b/web/src/user_profile.ts
index 9fc6e3f33cb..98c0bc2cc45 100644
--- a/web/src/user_profile.ts
+++ b/web/src/user_profile.ts
@@ -519,10 +519,13 @@ export function get_custom_profile_field_data(
);
profile_field.value = field_value.value;
profile_field.subtype = field_data.subtype;
- profile_field.link = settings_profile_fields.get_external_account_link(
+ const link = settings_profile_fields.get_external_account_link(
field_data,
field_value.value,
);
+ if (link !== undefined) {
+ profile_field.link = link;
+ }
break;
}
default:
diff --git a/web/templates/popovers/user_card/user_card_popover_custom_fields.hbs b/web/templates/popovers/user_card/user_card_popover_custom_fields.hbs
index 31c6e113b64..c0de9290ae6 100644
--- a/web/templates/popovers/user_card/user_card_popover_custom_fields.hbs
+++ b/web/templates/popovers/user_card/user_card_popover_custom_fields.hbs
@@ -8,10 +8,15 @@
<i class="zulip-icon zulip-icon-copy" aria-hidden="true"></i>
</span>
{{else if this.is_external_account}}
- <a href="{{this.link}}" target="_blank" rel="noopener noreferrer" class="custom-profile-field-value custom-profile-field-link" data-tippy-content="{{this.name}}" tabindex="0">
+ {{#if this.link}}
+ <a href="{{this.link}}" target="_blank" rel="noopener noreferrer" class="custom-profile-field-value custom-profile-field-link" data-tippy-content="{{this.name}}" tabindex="0">
+ {{> ../../default_external_account_icon subtype=this.subtype}}
+ <span class="custom-profile-field-text">{{this.value}}</span>
+ </a>
+ {{else}}
{{> ../../default_external_account_icon subtype=this.subtype}}
<span class="custom-profile-field-text">{{this.value}}</span>
- </a>
+ {{/if}}
{{else if this.rendered_value}}
<div class="custom-profile-field-value rendered_markdown" data-tippy-content="{{this.name}}">
{{rendered_markdown this.rendered_value}}
diff --git a/web/templates/user_custom_profile_fields.hbs b/web/templates/user_custom_profile_fields.hbs
index 73283c816a7..701fc158e28 100644
--- a/web/templates/user_custom_profile_fields.hbs
+++ b/web/templates/user_custom_profile_fields.hbs
@@ -11,10 +11,15 @@
</span>
</div>
{{else if this.is_external_account}}
- <a tabindex="0" href="{{this.link}}" target="_blank" rel="noopener noreferrer" class="value custom-profile-fields-link {{#if ../for_user_card_popover}}tippy-zulip-tooltip{{/if}}" data-tippy-content="{{this.name}}">
+ {{#if this.link}}
+ <a tabindex="0" href="{{this.link}}" target="_blank" rel="noopener noreferrer" class="value custom-profile-fields-link {{#if ../for_user_card_popover}}tippy-zulip-tooltip{{/if}}" data-tippy-content="{{this.name}}">
+ {{> default_external_account_icon subtype=this.subtype}}
+ {{this.value}}
+ </a>
+ {{else}}
{{> default_external_account_icon subtype=this.subtype}}
{{this.value}}
- </a>
+ {{/if}}
{{else if this.is_user_field}}
<div class="user-type-custom-field-pill-container" data-field-id="{{this.id}}"></div>
{{else}}
diff --git a/zerver/lib/external_accounts.py b/zerver/lib/external_accounts.py
index 8157bc7bb2c..271cd16c3bf 100644
--- a/zerver/lib/external_accounts.py
+++ b/zerver/lib/external_accounts.py
@@ -29,6 +29,9 @@ class ExternalAccount:
url_pattern: str # Field URL linkifier
+# Set url_pattern as "%(username)s" if there's no common URL pattern,
+# and the user should enter the full URL of their link.
+# Set url_pattern as "" to make the field a text field.
DEFAULT_EXTERNAL_ACCOUNTS = {
"twitter": ExternalAccount(
text="Twitter",
@@ -65,11 +68,7 @@ def validate_external_account_field_data(field_data: ProfileFieldData) -> Profil
field_validator("field_data", field_data)
field_subtype = field_data.get("subtype")
- if field_subtype not in DEFAULT_EXTERNAL_ACCOUNTS:
- if field_subtype == "custom":
- if "url_pattern" not in field_data:
- raise ValidationError(_("Custom external account must define URL pattern"))
- else:
- raise ValidationError(_("Invalid external account type"))
+ if field_subtype not in DEFAULT_EXTERNAL_ACCOUNTS and field_subtype != "custom":
+ raise ValidationError(_("Invalid external account type"))
return field_data
diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py
index 0d4eda6ffe5..ee747d31ad0 100644
--- a/zerver/lib/validator.py
+++ b/zerver/lib/validator.py
@@ -365,6 +365,10 @@ def check_capped_url(max_length: int) -> Validator[str]:
def check_external_account_url_pattern(var_name: str, val: object) -> str:
s = check_string(var_name, val)
+ # Allow empty URL pattern
+ if not s:
+ return s
+
if s.count("%(username)s") != 1:
raise ValidationError(_("URL pattern must contain '%(username)s'."))
url_val = s.replace("%(username)s", "username")
diff --git a/zerver/tests/test_custom_profile_data.py b/zerver/tests/test_custom_profile_data.py
index 523b911ab57..d974958a3b6 100644
--- a/zerver/tests/test_custom_profile_data.py
+++ b/zerver/tests/test_custom_profile_data.py
@@ -292,15 +292,26 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):
self.assertEqual(twitter_field.name, "Twitter username")
self.assertEqual(orjson.loads(twitter_field.field_data)["subtype"], "twitter")
- data["name"] = "Reddit"
+ data["name"] = "Reddit without URL"
data["field_data"] = orjson.dumps(
{
"subtype": "custom",
}
).decode()
result = self.client_post("/json/realm/profile_fields", info=data)
- self.assert_json_error(result, "Custom external account must define URL pattern")
+ self.assert_json_success(result)
+ data["name"] = "Reddit with empty URL"
+ data["field_data"] = orjson.dumps(
+ {
+ "subtype": "custom",
+ "url_pattern": "",
+ }
+ ).decode()
+ result = self.client_post("/json/realm/profile_fields", info=data)
+ self.assert_json_success(result)
+
+ data["name"] = "Reddit"
data["field_data"] = orjson.dumps(
{
"subtype": "custom",