Skip to content

Commit

Permalink
Work on support for org style urls, minor formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Firepup6500 committed Feb 13, 2024
1 parent 06aa43a commit 7c4debc
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Discourse custom profile link

Displays a custom "user field" as a link on a user's profile,
as directed by [this tutorial](https://meta.discourse.org/t/link-custom-user-field-to-external-website/41218).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
{{#each this.links as |link|}}
<a href="{{link.[2]}}{{link.[0]}}" target="_blank">{{link.[1]}}</a>
{{/each}}
{{#if this.orgs}}
{{#each this.orgs as |org|}}
<a href="{{org.[2]}}">{{link.[0]}}: {{link.[1]}}</a>
{{/each}}
{{/if}}
</span>
</h3>
{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export default class CustomProfileLink extends Component {
const ids = settings.custom_profile_link_user_field_ids.replace(/_/g, "").split(/\|/).map(Number);
const labels = settings.custom_profile_link_labels.split(/\|/);
const prefixes = settings.custom_profile_link_prefixes.split(/\|/);
const orgs = settings.custom_profile_link_orgs.split(/\|/);
const parsedSettings = { "ids": ids, "labels": labels, "prefixes": prefixes /*, "orgs": {"labels": orgLabels, "name-ids": orgNames, "url-ids": orgUrls} */ }
const parsedSettings = { "ids": ids, "labels": labels, "prefixes": prefixes}
if (settings.custom_profile_link_debug_mode) console.debug("[Custom Profile Link] Parsed settings dump follows", parsedSettings);
let links = [];
if (settings.custom_profile_link_debug_mode) console.debug("[Custom Profile Link] args dump:", this.args.outletArgs);
Expand All @@ -24,4 +23,24 @@ export default class CustomProfileLink extends Component {
return links;
}
}
get orgs() {
const orgsData = settings.custom_profile_link_orgs.split(/\|/);
let orgs = [];
if (!this.args.outletArgs.user.get('user_fields')) {
return undefined;
} else {
for (let i = 0; i < orgsData.length; i++) {
// fmt: Label,name-id,url-id
let data = orgsData[i].split(/,/);
data[1] = parseInt(data[1]);
data[2] = parseInt(data[2]);
if ((!this.args.outletArgs.user.get('user_fields')[data[1]] || !this.args.outletArgs.user.get('user_fields')[data[2]]) && settings.custom_profile_link_debug_mode) console.debug(`[Custom Profile Link] Part of orgs data missing. user_fields dump follows.`, this.args.outletArgs.user.get("user_fields"));
// Handle only having a url
if (!this.args.outletArgs.user.get('user_fields')[data[1]] && !!this.args.outletArgs.user.get('user_fields')[data[2]]) data[1] = data[2];
if ((!!this.args.outletArgs.user.get('user_fields')[data[1]] && !!this.args.outletArgs.user.get('user_fields')[data[2]])) orgs.push([data[0], this.args.outletArgs.user.get('user_fields')[data[1]], this.args.outletArgs.user.get('user_fields')[data[2]]])
}
}
if (settings.custom_profile_link_debug_mode) console.debug("[Custom Profile Link] orgs built, dump:", orgs);
return orgs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
{{#each this.links as |link|}}
<a href="{{link.[2]}}{{link.[0]}}" target="_blank">{{link.[1]}}</a>
{{/each}}
{{#if this.orgs}}
{{#each this.orgs as |org|}}
<a href="{{org.[2]}}">{{link.[0]}}: {{link.[1]}}</a>
{{/each}}
{{/if}}
</span>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export default class CustomProfileLink extends Component {
const ids = settings.custom_profile_link_user_field_ids.replace(/_/g, "").split(/\|/).map(Number);
const labels = settings.custom_profile_link_labels.split(/\|/);
const prefixes = settings.custom_profile_link_prefixes.split(/\|/);
const orgs = settings.custom_profile_link_orgs.split(/\|/);
const parsedSettings = { "ids": ids, "labels": labels, "prefixes": prefixes /*, "orgs": {"labels": orgLabels, "name-ids": orgNames, "url-ids": orgUrls} */ }
const parsedSettings = { "ids": ids, "labels": labels, "prefixes": prefixes}
if (settings.custom_profile_link_debug_mode) console.debug("[Custom Profile Link] Parsed settings dump follows", parsedSettings);
let links = [];
if (settings.custom_profile_link_debug_mode) console.debug("[Custom Profile Link] args dump:", this.args.outletArgs);
Expand All @@ -24,4 +23,24 @@ export default class CustomProfileLink extends Component {
return links;
}
}
get orgs() {
const orgsData = settings.custom_profile_link_orgs.split(/\|/);
let orgs = [];
if (!this.args.outletArgs.model.get('user_fields')) {
return undefined;
} else {
for (let i = 0; i < orgsData.length; i++) {
// fmt: Label,name-id,url-id
let data = orgsData[i].split(/,/);
data[1] = parseInt(data[1]);
data[2] = parseInt(data[2]);
if ((!this.args.outletArgs.model.get('user_fields')[data[1]] || !this.args.outletArgs.model.get('user_fields')[data[2]]) && settings.custom_profile_link_debug_mode) console.debug(`[Custom Profile Link] Part of orgs data missing. user_fields dump follows.`, this.args.outletArgs.model.get("user_fields"));
// Handle only having a url
if (!this.args.outletArgs.model.get('user_fields')[data[1]] && !!this.args.outletArgs.model.get('user_fields')[data[2]]) data[1] = data[2];
if ((!!this.args.outletArgs.model.get('user_fields')[data[1]] && !!this.args.outletArgs.model.get('user_fields')[data[2]])) orgs.push([data[0], this.args.outletArgs.model.get('user_fields')[data[1]], this.args.outletArgs.model.get('user_fields')[data[2]]])
}
}
if (settings.custom_profile_link_debug_mode) console.debug("[Custom Profile Link] orgs built, dump:", orgs);
return orgs;
}
}
2 changes: 1 addition & 1 deletion settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ custom_profile_link_prefixes:
custom_profile_link_orgs:
type: list
default: "Organization,1,2|"
description: "Organization style links, format of label,id-of-name,id-of-url"
description: "Organization style links, format of: label,id-of-name,id-of-url"

custom_profile_link_debug_mode:
type: bool
Expand Down

0 comments on commit 7c4debc

Please sign in to comment.