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.
291 lines
8.7 KiB
JavaScript
291 lines
8.7 KiB
JavaScript
import Service, { service } from '@ember/service';
|
|
import { tracked } from '@glimmer/tracking';
|
|
import { SITE_GROUPS } from '../config/sites';
|
|
import { DISCOURSE_ICON_URL, FEEDBIN_ICON_URL } from '../config/integrations';
|
|
import { FEEDBIN_DEFAULT_URL } from '../config/feedbin';
|
|
import {
|
|
GITHUB_DEFAULT_URL,
|
|
GITHUB_ICON_BACKGROUND,
|
|
GITHUB_ICON_URL,
|
|
isGithubDotCom,
|
|
} from '../config/github';
|
|
import { GITEA_ICON_BACKGROUND, GITEA_ICON_URL } from '../config/gitea';
|
|
import {
|
|
HOME_ASSISTANT_ICON_BACKGROUND,
|
|
HOME_ASSISTANT_ICON_URL,
|
|
} from '../config/home-assistant';
|
|
import { normalizeOrigin } from './site-icons';
|
|
import { labelForUrl } from '../utils/site-url';
|
|
|
|
function hrefForIntegration(integration) {
|
|
if (integration.type === 'discourse' && integration.url) {
|
|
return `${integration.url}`;
|
|
}
|
|
return integration.url ?? null;
|
|
}
|
|
|
|
// Owns the list of connected sites rendered as dashboard tiles. It is the
|
|
// single source of truth for labels, links, icons and grouping; notification
|
|
// polling lives in the `notifications` service and is keyed by site id.
|
|
export default class SitesService extends Service {
|
|
@service storage;
|
|
@service siteIcons;
|
|
|
|
@tracked list = [];
|
|
@tracked error = null;
|
|
|
|
ready = null;
|
|
integrationsById = new Map();
|
|
|
|
constructor() {
|
|
super(...arguments);
|
|
this.ready = this.load();
|
|
}
|
|
|
|
async load() {
|
|
try {
|
|
const keys = await this.storage.keys('integrations');
|
|
const integrations = await Promise.all(
|
|
keys.map(async (key) => ({
|
|
key,
|
|
data: await this.storage.get('integrations', key),
|
|
}))
|
|
);
|
|
|
|
this.integrationsById = new Map();
|
|
this.list = integrations
|
|
.filter(({ data }) => this.isSupportedIntegration(data))
|
|
.map(({ key, data }) => this.buildSite(key, data));
|
|
|
|
this.resolveIcons();
|
|
} catch (err) {
|
|
console.error('Error loading sites:', err);
|
|
this.error = 'Unable to load sites';
|
|
}
|
|
}
|
|
|
|
isSupportedIntegration(integration) {
|
|
return (
|
|
integration?.status === 'active' &&
|
|
SITE_GROUPS.some((group) =>
|
|
group.integrationTypes.includes(integration.type)
|
|
)
|
|
);
|
|
}
|
|
|
|
buildSite(key, integration) {
|
|
this.integrationsById.set(key, integration);
|
|
|
|
// Discourse tiles start with the official icon so the tile is never empty;
|
|
// `url` stays set so `resolveIcons` can replace it with the forum's own
|
|
// favicon once (and if) one is discovered.
|
|
if (integration.type === 'discourse') {
|
|
return {
|
|
id: key,
|
|
type: 'discourse',
|
|
groupId: this.groupIdForType('discourse'),
|
|
label: labelForUrl(integration.url),
|
|
url: integration.url,
|
|
href: hrefForIntegration(integration),
|
|
icon: { dataUrl: DISCOURSE_ICON_URL, maskable: true },
|
|
};
|
|
}
|
|
|
|
// Feedbin uses a bundled maskable icon and links to the configured
|
|
// instance (the default Feedbin URL unless self-hosted). Leaving `url`
|
|
// unset also keeps `resolveIcons` from replacing the bundled art with a
|
|
// discovered favicon.
|
|
if (integration.type === 'feedbin') {
|
|
return {
|
|
id: key,
|
|
type: 'feedbin',
|
|
groupId: this.groupIdForType('feedbin'),
|
|
label: 'Feedbin',
|
|
url: undefined,
|
|
href: integration.url ?? FEEDBIN_DEFAULT_URL,
|
|
icon: { dataUrl: FEEDBIN_ICON_URL, maskable: true },
|
|
};
|
|
}
|
|
|
|
// GitHub.com links straight to the notifications inbox and uses the
|
|
// bundled inverted mark on a dark grey backdrop. Enterprise hosts keep
|
|
// their URL so a favicon can be found.
|
|
if (integration.type === 'github') {
|
|
const dotCom = isGithubDotCom(integration.url);
|
|
const origin = normalizeOrigin(integration.url) ?? GITHUB_DEFAULT_URL;
|
|
|
|
return {
|
|
id: key,
|
|
type: 'github',
|
|
groupId: this.groupIdForType('github'),
|
|
label: dotCom ? 'GitHub' : labelForUrl(integration.url),
|
|
url: dotCom ? undefined : integration.url,
|
|
href: `${origin}/notifications`,
|
|
icon: dotCom
|
|
? {
|
|
dataUrl: GITHUB_ICON_URL,
|
|
maskable: true,
|
|
themeColor: GITHUB_ICON_BACKGROUND,
|
|
}
|
|
: undefined,
|
|
};
|
|
}
|
|
|
|
// Gitea is always self-hosted. It uses the bundled mark on a white
|
|
// backdrop (leaving `url` unset so `resolveIcons` doesn't replace it),
|
|
// links to the root normally, and exposes a notifications link the tile
|
|
// can switch to when there are unread items.
|
|
if (integration.type === 'gitea') {
|
|
return {
|
|
id: key,
|
|
type: 'gitea',
|
|
groupId: this.groupIdForType('gitea'),
|
|
label: labelForUrl(integration.url),
|
|
url: undefined,
|
|
href: integration.url,
|
|
notificationsHref: `${integration.url}/notifications`,
|
|
icon: {
|
|
dataUrl: GITEA_ICON_URL,
|
|
maskable: true,
|
|
themeColor: GITEA_ICON_BACKGROUND,
|
|
},
|
|
};
|
|
}
|
|
|
|
// Home Assistant is always self-hosted and does not expose a discoverable
|
|
// site icon, so it uses the bundled mark. Leaving `url` unset keeps
|
|
// `resolveIcons` from probing the instance with an icon request.
|
|
if (integration.type === 'homeassistant') {
|
|
return {
|
|
id: key,
|
|
type: 'homeassistant',
|
|
groupId: this.groupIdForType('homeassistant'),
|
|
label: integration.name?.trim() || labelForUrl(integration.url),
|
|
url: undefined,
|
|
href: `${integration.url}/`,
|
|
icon: {
|
|
dataUrl: HOME_ASSISTANT_ICON_URL,
|
|
maskable: true,
|
|
themeColor: HOME_ASSISTANT_ICON_BACKGROUND,
|
|
},
|
|
};
|
|
}
|
|
|
|
// A user-defined quick link. `url` is kept for icon discovery even when a
|
|
// custom icon is pinned; `resolveIcons` uses the `customIcon` flag below to
|
|
// leave the user's choice alone.
|
|
if (integration.type === 'custom') {
|
|
return {
|
|
id: key,
|
|
type: 'custom',
|
|
groupId: this.groupIdForType('custom'),
|
|
label: integration.name?.trim() || labelForUrl(integration.url),
|
|
url: integration.url,
|
|
href: integration.url,
|
|
icon: integration.customIcon,
|
|
customIcon: integration.customIcon,
|
|
};
|
|
}
|
|
|
|
return {
|
|
id: key,
|
|
type: integration.type,
|
|
groupId: this.groupIdForType(integration.type),
|
|
label: labelForUrl(integration.url),
|
|
url: integration.url,
|
|
href: hrefForIntegration(integration),
|
|
icon: undefined,
|
|
};
|
|
}
|
|
|
|
integrationFor(id) {
|
|
return this.integrationsById.get(id);
|
|
}
|
|
|
|
groupIdForType(type) {
|
|
const group = SITE_GROUPS.find((candidate) =>
|
|
candidate.integrationTypes.includes(type)
|
|
);
|
|
return group ? group.id : 'other';
|
|
}
|
|
|
|
// Best-effort: attach a cached site icon to every site that has a URL. Runs in
|
|
// the background so slow/broken icon discovery never blocks the tile render.
|
|
// Sites with a user-pinned `customIcon` are skipped, and re-checked before
|
|
// writing back, so an in-flight discovery cannot clobber a freshly uploaded
|
|
// icon after an edit.
|
|
async resolveIcons() {
|
|
await Promise.all(
|
|
this.list
|
|
.filter((site) => site.url && !site.customIcon)
|
|
.map(async (site) => {
|
|
const icon = await this.siteIcons.iconFor(site.url);
|
|
|
|
if (!icon) {
|
|
return;
|
|
}
|
|
|
|
const current = this.list.find(
|
|
(candidate) => candidate.id === site.id
|
|
);
|
|
|
|
if (!current || current.customIcon) {
|
|
return;
|
|
}
|
|
|
|
this.updateSite(site.id, { icon });
|
|
})
|
|
);
|
|
}
|
|
|
|
get all() {
|
|
return this.list;
|
|
}
|
|
|
|
get groups() {
|
|
return SITE_GROUPS.map((group) => ({
|
|
id: group.id,
|
|
label: group.label,
|
|
items: this.list.filter((site) => site.groupId === group.id),
|
|
})).filter((group) => group.items.length > 0);
|
|
}
|
|
|
|
updateSite(id, changes) {
|
|
this.list = this.list.map((site) =>
|
|
site.id === id ? { ...site, ...changes } : site
|
|
);
|
|
}
|
|
|
|
// Re-reads integrations from storage and rebuilds the sites. Use after a
|
|
// site is reconnected or removed so the new record is picked up without a
|
|
// full page reload.
|
|
async reload() {
|
|
await this.load();
|
|
}
|
|
|
|
// Re-resolves a single site's icon, bypassing the icon cache. Used after a
|
|
// reconnect so a forum picks up its current icon without waiting for the
|
|
// cache to expire.
|
|
async refreshIcon(url) {
|
|
const icon = await this.siteIcons.refresh(url);
|
|
if (!icon) {
|
|
return;
|
|
}
|
|
|
|
const origin = normalizeOrigin(url);
|
|
const site = this.list.find(
|
|
(candidate) =>
|
|
!candidate.customIcon && normalizeOrigin(candidate.url) === origin
|
|
);
|
|
|
|
if (site) {
|
|
this.updateSite(site.id, { icon });
|
|
}
|
|
}
|
|
|
|
async removeSource(id) {
|
|
await this.storage.remove('integrations', id);
|
|
await this.reload();
|
|
}
|
|
}
|