Files
raucao b057e3edca Add custom site links with icon discovery and upload
Users can add any website as a dashboard quick link from Settings → Site
Links. The link is stored in the integrations namespace as a `custom`
type so it flows through the existing sites/integrations/settings
pipeline, and appears in a new "Links" group.

Its icon is discovered with the existing favicon pipeline; an uploaded
icon overrides it and stays pinned until the user chooses "Use site's
icon" again. resolveIcons and refreshIcon skip pinned sites (and
re-check the live list before writing back) so an in-flight discovery
can never clobber a freshly uploaded icon.

URLs are normalized to http(s) only, with embedded credentials stripped,
so a value can never reach an anchor's href as javascript:/data:.
Uploads are validated, size-capped and downscaled to 192px (SVGs kept
as-is), and only ever rendered through an <img>.

Adds app/utils/site-url.js and app/utils/image-file.js, a custom-link
form, a `custom` registry entry/group and adapter, and tests.
2026-11-13 15:57:57 +04:00

332 lines
9.2 KiB
Plaintext

import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render, click } from '@ember/test-helpers';
import { tracked } from '@glimmer/tracking';
import SiteLinks from 'dashtab/components/settings/global-settings/sections/site-links';
import AddList from 'dashtab/components/settings/integrations/add-list';
import { SITE_LINK_TYPES } from 'dashtab/config/sites';
class FakeSitesService {
@tracked all = [];
removed = [];
reloaded = 0;
refreshed = [];
integrationFor(id) {
return this.records?.[id];
}
async removeSource(id) {
this.removed.push(id);
this.all = this.all.filter((site) => site.id !== id);
}
async reload() {
this.reloaded++;
}
async refreshIcon(url) {
this.refreshed.push(url);
}
}
class FakeNotificationsService {
states = {};
stateFor(site) {
return this.states[site.id] ?? { error: null, errorType: null };
}
}
class FakeBitcoinService {
@tracked enabled = false;
@tracked interval = 30000;
@tracked provider = 'coingecko';
}
class FakeCalendarService {
instancesFor() {
return [];
}
}
class FakeAuthorizationService {
calls = [];
async start(type, url) {
this.calls.push({ type, url });
return { transport: 'extension', integration: { id: 'reconnected' } };
}
}
class FakeIntegrationResultService {
shown = [];
show(result) {
this.shown.push(result);
}
}
class FakeModalService {
opened = null;
open(descriptor) {
this.opened = descriptor;
}
replace(descriptor) {
this.opened = descriptor;
}
}
function register(owner, overrides = {}) {
const sites = overrides.sites ?? new FakeSitesService();
const notifications =
overrides.notifications ?? new FakeNotificationsService();
const bitcoin = overrides.bitcoin ?? new FakeBitcoinService();
const calendar = overrides.calendar ?? new FakeCalendarService();
const authorization =
overrides.authorization ?? new FakeAuthorizationService();
const integrationResult =
overrides.integrationResult ?? new FakeIntegrationResultService();
const modal = overrides.modal ?? new FakeModalService();
owner.register('service:sites', sites, { instantiate: false });
owner.register('service:notifications', notifications, {
instantiate: false,
});
owner.register('service:bitcoin', bitcoin, { instantiate: false });
owner.register('service:calendar', calendar, { instantiate: false });
owner.register('service:authorization', authorization, {
instantiate: false,
});
owner.register('service:integrationResult', integrationResult, {
instantiate: false,
});
owner.register('service:modal', modal, { instantiate: false });
return {
sites,
notifications,
bitcoin,
calendar,
authorization,
integrationResult,
modal,
};
}
module(
'Integration | Component | settings/global-settings/sections/site-links',
function (hooks) {
setupRenderingTest(hooks);
test('it lists the site links and hides other integrations', async function (assert) {
const sites = new FakeSitesService();
sites.all = [
{
id: 'discourse-abc',
type: 'discourse',
label: 'forum.example.com',
url: 'https://forum.example.com',
},
{
id: 'github',
type: 'github',
label: 'GitHub',
href: 'https://github.com/notifications',
},
];
sites.records = { github: { url: 'https://github.com', token: 't' } };
const bitcoin = new FakeBitcoinService();
bitcoin.enabled = true;
register(this.owner, { sites, bitcoin });
await render(<template><SiteLinks /></template>);
assert.dom('[data-instance="discourse-abc"]').exists();
assert.dom('[data-instance="github"]').exists();
assert.dom('[data-instance="bitcoin"]').doesNotExist();
});
test('it sorts the site links alphabetically by name', async function (assert) {
const sites = new FakeSitesService();
sites.all = [
{
id: 'github',
type: 'github',
label: 'GitHub',
href: 'https://github.com/notifications',
},
{
id: 'discourse-a',
type: 'discourse',
label: 'a.example.com',
url: 'https://a.example.com',
},
{
id: 'homeassistant-ha',
type: 'homeassistant',
label: 'ha.example.com',
href: 'https://ha.example.com/',
},
];
sites.records = {
github: { url: 'https://github.com', token: 't' },
'homeassistant-ha': {
url: 'https://ha.example.com',
token: 't',
name: 'Home',
},
};
register(this.owner, { sites });
await render(<template><SiteLinks /></template>);
const ids = [...this.element.querySelectorAll('[data-instance]')].map(
(el) => el.dataset.instance
);
assert.deepEqual(ids, ['discourse-a', 'github', 'homeassistant-ha']);
});
test('it offers settings only where the integration has them', async function (assert) {
const sites = new FakeSitesService();
sites.all = [
{
id: 'discourse-abc',
type: 'discourse',
label: 'forum.example.com',
url: 'https://forum.example.com',
},
{
id: 'github',
type: 'github',
label: 'GitHub',
href: 'https://github.com/notifications',
},
];
sites.records = { github: { url: 'https://github.com', token: 't' } };
register(this.owner, { sites });
await render(<template><SiteLinks /></template>);
assert
.dom('[data-instance="discourse-abc"] [data-test-settings]')
.doesNotExist();
assert.dom('[data-instance="github"] [data-test-settings]').exists();
});
test('it lists custom site links as editable', async function (assert) {
const sites = new FakeSitesService();
sites.all = [
{
id: 'custom-1',
type: 'custom',
label: 'Example',
href: 'https://example.com',
},
];
sites.records = {
'custom-1': { url: 'https://example.com', name: 'Example' },
};
register(this.owner, { sites });
await render(<template><SiteLinks /></template>);
assert.dom('[data-instance="custom-1"]').exists();
assert.dom('[data-instance="custom-1"]').containsText('Custom Link');
assert.dom('[data-instance="custom-1"] [data-test-settings]').exists();
});
test('editing a site link opens its form', async function (assert) {
const sites = new FakeSitesService();
sites.all = [
{
id: 'github',
type: 'github',
label: 'GitHub',
href: 'https://github.com/notifications',
},
];
sites.records = { github: { url: 'https://github.com', token: 't' } };
const { modal } = register(this.owner, { sites });
await render(<template><SiteLinks /></template>);
await click('[data-instance="github"] [data-test-settings]');
assert.ok(modal.opened.component);
assert.strictEqual(modal.opened.props.mode, 'edit');
assert.strictEqual(modal.opened.props.integration.id, 'github');
});
test('reconnecting a broken site link runs the auth flow', async function (assert) {
const sites = new FakeSitesService();
sites.all = [
{
id: 'discourse-bad',
type: 'discourse',
label: 'bad.example.com',
url: 'https://bad.example.com',
},
];
const notifications = new FakeNotificationsService();
notifications.states['discourse-bad'] = {
errorType: 'unauthorized',
error: 'Reconnect this forum',
};
const {
authorization,
sites: registeredSites,
integrationResult,
} = register(this.owner, { sites, notifications });
await render(<template><SiteLinks /></template>);
await click('[data-instance="discourse-bad"] [data-test-reconnect]');
assert.deepEqual(authorization.calls, [
{ type: 'discourse', url: 'https://bad.example.com' },
]);
assert.strictEqual(registeredSites.reloaded, 1);
assert.deepEqual(registeredSites.refreshed, ['https://bad.example.com']);
assert.strictEqual(integrationResult.shown.length, 0);
});
test('removing a site link removes the source', async function (assert) {
const sites = new FakeSitesService();
sites.all = [
{
id: 'discourse-abc',
type: 'discourse',
label: 'forum.example.com',
url: 'https://forum.example.com',
},
];
register(this.owner, { sites });
await render(<template><SiteLinks /></template>);
await click('[data-instance="discourse-abc"] [data-test-remove]');
assert.deepEqual(sites.removed, ['discourse-abc']);
});
test('adding opens the site integration menu', async function (assert) {
const { modal } = register(this.owner);
await render(<template><SiteLinks /></template>);
await click('[data-test-add]');
assert.strictEqual(modal.opened.component, AddList);
assert.deepEqual(modal.opened.props.types, SITE_LINK_TYPES);
});
}
);