Rename Notifications to Sites

This commit is contained in:
raucao committed 2026-10-03 16:39:21 +02:00
1 parent b3682b2cb3
commit df5808b144
16 files changed
+548 -397

No files matched your search

-17
View File
@@ -1,17 +0,0 @@
import Component from '@glimmer/component';
import { service } from '@ember/service';
import NotificationsGroup from './notifications/group.gjs';
export default class NotificationsComponent extends Component {
@service notifications;
<template>
{{#if this.notifications.groups.length}}
<div class="flex items-start gap-4" data-test-notifications ...attributes>
{{#each this.notifications.groups as |group|}}
<NotificationsGroup @id={{group.id}} @items={{group.items}} />
{{/each}}
</div>
{{/if}}
</template>
}
@@ -8,13 +8,15 @@ import Bitcoin from './bitcoin.gjs';
import { startDiscourseAuthorization } from '../../../utils/discourse-auth';
export default class SettingsAddIntegrationList extends Component {
@service sites;
@service notifications;
@service storage;
get connected() {
return this.notifications.sources.map((source) => ({
...source,
canReconnect: source.type === 'discourse',
return this.sites.all.map((site) => ({
...site,
error: this.notifications.stateFor(site).error,
canReconnect: site.type === 'discourse',
}));
}
@@ -29,7 +31,7 @@ export default class SettingsAddIntegrationList extends Component {
@action
async remove(source) {
await this.notifications.removeSource(source.id);
await this.sites.removeSource(source.id);
}
<template>
+28
View File
@@ -0,0 +1,28 @@
import Component from '@glimmer/component';
import { service } from '@ember/service';
import SiteLinksGroup from './site-links/group.gjs';
export default class SiteLinksComponent extends Component {
@service sites;
@service notifications;
get groups() {
return this.sites.groups.map((group) => ({
id: group.id,
items: group.items.map((site) => ({
...site,
...this.notifications.stateFor(site),
})),
}));
}
<template>
{{#if this.groups.length}}
<div class="flex items-start gap-4" data-test-site-links ...attributes>
{{#each this.groups as |group|}}
<SiteLinksGroup @id={{group.id}} @items={{group.items}} />
{{/each}}
</div>
{{/if}}
</template>
}
@@ -1,7 +1,7 @@
import Component from '@glimmer/component';
import GenericItem from './item.gjs';
export default class NotificationsGroupComponent extends Component {
export default class SiteLinksGroupComponent extends Component {
get itemComponent() {
return this.args.itemComponent ?? GenericItem;
}
@@ -4,9 +4,9 @@ import setStyle from '../../modifiers/set-style';
const SAFE_COLOR =
/^(#[0-9a-f]{3,8}|rgba?\([\d\s.,%]+\)|hsla?\([\d\s.,%]+\)|[a-z]+)$/i;
export default class NotificationsItemComponent extends Component {
export default class SiteLinksItemComponent extends Component {
get label() {
return this.args.item?.label ?? 'Notifications';
return this.args.item?.label ?? 'Site';
}
get unreadCount() {
@@ -72,7 +72,7 @@ export default class NotificationsItemComponent extends Component {
...attributes
>
<span
data-test-notifications-tile
data-test-site-links-tile
class="flex size-full items-center justify-center overflow-hidden rounded-xl border border-white/40 bg-gray-950/40 backdrop-blur transition-colors hover:border-white/60
{{if this.isError 'ring-2 ring-red-400/70'}}"
{{setStyle "background-color" this.tileColor}}
@@ -82,12 +82,12 @@ export default class NotificationsItemComponent extends Component {
src={{this.icon.dataUrl}}
alt=""
aria-hidden="true"
data-test-notifications-icon
data-test-site-links-icon
class={{this.imageClass}}
/>
{{else}}
<span
data-test-notifications-fallback
data-test-site-links-fallback
class="flex size-full items-center justify-center bg-indigo-500 text-sm font-semibold"
>{{this.initial}}</span>
{{/if}}
@@ -96,7 +96,7 @@ export default class NotificationsItemComponent extends Component {
{{#if this.hasUnread}}
<span
class="absolute -right-1 -bottom-1 rounded-full bg-red-500 px-1 text-xs font-semibold tabular-nums text-white ring-2 ring-gray-950"
data-test-notifications-count
data-test-site-links-count
>{{this.unreadCount}}</span>
{{/if}}
</a>
@@ -1,4 +1,4 @@
export const NOTIFICATION_GROUPS = [
export const SITE_GROUPS = [
{
id: 'discourse',
label: 'Discourse',
+38 -125
View File
@@ -1,6 +1,5 @@
import Service, { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { NOTIFICATION_GROUPS } from '../config/notifications';
import { HttpError } from './http';
export const NOTIFICATIONS_DEFAULT_INTERVAL = 60000;
@@ -35,127 +34,54 @@ const ADAPTERS = {
discourse: fetchDiscourseNotifications,
};
function labelForUrl(url) {
try {
return new URL(url).hostname;
} catch {
return url;
}
}
function hrefForIntegration(integration) {
if (integration.type === 'discourse' && integration.url) {
return `${integration.url}`;
}
return integration.url ?? null;
function defaultState(type) {
return {
status: ADAPTERS[type] ? 'loading' : 'ready',
unreadCount: 0,
items: [],
};
}
// Polls connected sites for unread notifications and keeps the resulting state
// keyed by site id. Sites themselves (labels, links, icons, grouping) are owned
// by the `sites` service; this service only layers notification data on top.
export default class NotificationsService extends Service {
@service storage;
@service sites;
@service http;
@service siteIcons;
@tracked sources = [];
@tracked loading = false;
@tracked error = null;
@tracked states = {};
intervalId = null;
fetching = false;
ready = null;
integrationBySourceId = new Map();
constructor() {
super(...arguments);
this.ready = this.load();
this.ready = this.setup();
}
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.sources = integrations
.filter(({ data }) => this.isSupportedIntegration(data))
.map(({ key, data }) => this.buildSource(key, data));
this.resolveIcons();
if (this.sources.length > 0) {
this.startPolling();
}
} catch (err) {
console.error('Error loading notification sources:', err);
this.error = 'Unable to load notification sources';
async setup() {
await this.sites.ready;
if (this.sites.all.some((site) => this.isPollable(site))) {
this.startPolling();
}
}
isSupportedIntegration(integration) {
return (
integration?.status === 'active' &&
NOTIFICATION_GROUPS.some((group) =>
group.integrationTypes.includes(integration.type)
)
);
isPollable(site) {
return Boolean(ADAPTERS[site.type]);
}
buildSource(key, integration) {
this.integrationBySourceId.set(key, integration);
stateFor(site) {
return this.states[site.id] ?? defaultState(site.type);
}
return {
id: key,
type: integration.type,
groupId: this.groupIdForType(integration.type),
label: labelForUrl(integration.url),
url: integration.url,
href: hrefForIntegration(integration),
unreadCount: 0,
setState(id, changes) {
const current = this.states[id] ?? {
status: 'loading',
error: null,
errorType: null,
notifications: [],
unreadCount: 0,
items: [],
};
}
groupIdForType(type) {
const group = NOTIFICATION_GROUPS.find((candidate) =>
candidate.integrationTypes.includes(type)
);
return group ? group.id : 'other';
}
// Best-effort: attach a cached site icon to every source that has a URL.
// Runs in the background so slow/broken icon discovery never blocks the
// notification poll.
async resolveIcons() {
await Promise.all(
this.sources
.filter((source) => source.url)
.map(async (source) => {
const icon = await this.siteIcons.iconFor(source.url);
if (icon) {
this.updateSource(source.id, { icon });
}
})
);
}
get groups() {
return NOTIFICATION_GROUPS.map((group) => ({
id: group.id,
label: group.label,
items: this.sources.filter((source) => source.groupId === group.id),
})).filter((group) => group.items.length > 0);
}
updateSource(id, changes) {
this.sources = this.sources.map((source) =>
source.id === id ? { ...source, ...changes } : source
);
this.states = { ...this.states, [id]: { ...current, ...changes } };
}
async fetchAll() {
@@ -164,38 +90,39 @@ export default class NotificationsService extends Service {
}
this.fetching = true;
this.loading = true;
try {
await Promise.all(this.sources.map((source) => this.fetchSource(source)));
await Promise.all(
this.sites.all
.filter((site) => this.isPollable(site))
.map((site) => this.fetchSite(site))
);
} finally {
this.fetching = false;
this.loading = false;
}
}
async fetchSource(source) {
const adapter = ADAPTERS[source.type];
async fetchSite(site) {
const adapter = ADAPTERS[site.type];
if (!adapter) {
this.updateSource(source.id, { status: 'ready' });
return;
}
try {
const result = await adapter(
this.integrationBySourceId.get(source.id),
this.sites.integrationFor(site.id),
this.http
);
this.updateSource(source.id, {
this.setState(site.id, {
status: 'ready',
error: null,
errorType: null,
unreadCount: result.unreadCount,
notifications: result.notifications,
items: result.notifications,
});
} catch (err) {
console.error(`Error fetching ${source.type} notifications:`, err);
this.updateSource(source.id, {
console.error(`Error fetching ${site.type} notifications:`, err);
this.setState(site.id, {
status: 'error',
...this.classifyError(err),
});
@@ -233,20 +160,6 @@ export default class NotificationsService extends Service {
};
}
// Re-reads integrations from storage and rebuilds the sources. Use after a
// source is reconnected or removed so the new token is picked up without a
// full page reload.
async reload() {
this.stopPolling();
this.integrationBySourceId = new Map();
await this.load();
}
async removeSource(id) {
await this.storage.remove('integrations', id);
await this.reload();
}
startPolling() {
this.stopPolling();
this.fetchAll();
+138
View File
@@ -0,0 +1,138 @@
import Service, { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { SITE_GROUPS } from '../config/sites';
function labelForUrl(url) {
try {
return new URL(url).hostname;
} catch {
return 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);
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.
async resolveIcons() {
await Promise.all(
this.list
.filter((site) => site.url)
.map(async (site) => {
const icon = await this.siteIcons.iconFor(site.url);
if (icon) {
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();
}
async removeSource(id) {
await this.storage.remove('integrations', id);
await this.reload();
}
}
+2 -2
View File
@@ -2,7 +2,7 @@ import { pageTitle } from 'ember-page-title';
import { on } from '@ember/modifier';
import DefaultClock from '../components/default-clock.gjs';
import BitcoinTicker from '../components/bitcoin-ticker.gjs';
import Notifications from '../components/notifications.gjs';
import SiteLinks from '../components/site-links.gjs';
import ModalDialog from '../components/modal-dialog.gjs';
import ModalStepOne from '../components/modal-step-one.gjs';
import { default as SettingsAddIntegrationList } from '../components/settings/add-integration/list.gjs';
@@ -29,7 +29,7 @@ import { default as SettingsAddIntegrationList } from '../components/settings/ad
</div>
<div class="col-start-3 row-start-1 flex items-start justify-end pt-12 pr-12">
<Notifications />
<SiteLinks />
</div>
<div class="col-start-2 row-start-1 flex items-start justify-center pt-12">
@@ -1,70 +0,0 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render } from '@ember/test-helpers';
import { tracked } from '@glimmer/tracking';
import Notifications from 'dashtab/components/notifications';
class FakeNotificationsService {
@tracked groups = [];
}
module('Integration | Component | notifications', function (hooks) {
setupRenderingTest(hooks);
test('it renders nothing when there are no sources', async function (assert) {
const notifications = new FakeNotificationsService();
this.owner.register('service:notifications', notifications, {
instantiate: false,
});
await render(<template><Notifications /></template>);
assert.dom('[data-test-notifications]').doesNotExist();
});
test('it renders a group per notification source group', async function (assert) {
const notifications = new FakeNotificationsService();
notifications.groups = [
{
id: 'discourse',
label: 'Discourse',
items: [
{
id: 'discourse-abc',
type: 'discourse',
label: 'forum.example.com',
href: 'https://forum.example.com',
status: 'ready',
unreadCount: 3,
},
],
},
{
id: 'code-forge',
label: 'Code Forges',
items: [
{
id: 'gitea-1',
type: 'gitea',
label: 'git.example.com',
href: 'https://git.example.com',
status: 'ready',
unreadCount: 0,
},
],
},
];
this.owner.register('service:notifications', notifications, {
instantiate: false,
});
await render(<template><Notifications /></template>);
assert.dom('[data-test-notifications]').exists();
assert.dom('[data-group="discourse"]').exists();
assert.dom('[data-group="code-forge"]').exists();
assert.dom('a').exists({ count: 2 });
assert.dom('[data-test-notifications-count]').hasText('3');
});
});
@@ -4,26 +4,36 @@ import { render, click } from '@ember/test-helpers';
import { tracked } from '@glimmer/tracking';
import List from 'dashtab/components/settings/add-integration/list';
class FakeNotificationsService {
@tracked sources = [];
class FakeSitesService {
@tracked all = [];
removed = [];
async removeSource(id) {
this.removed.push(id);
this.sources = this.sources.filter((source) => source.id !== id);
this.all = this.all.filter((site) => site.id !== id);
}
}
class FakeNotificationsService {
stateFor(site) {
return { error: site.error ?? null };
}
}
function registerServices(owner, sites) {
owner.register('service:sites', sites, { instantiate: false });
owner.register('service:notifications', new FakeNotificationsService(), {
instantiate: false,
});
}
module(
'Integration | Component | settings/add-integration/list',
function (hooks) {
setupRenderingTest(hooks);
test('it renders the add options', async function (assert) {
const notifications = new FakeNotificationsService();
this.owner.register('service:notifications', notifications, {
instantiate: false,
});
registerServices(this.owner, new FakeSitesService());
const noop = () => {};
@@ -37,9 +47,9 @@ module(
assert.dom().containsText('Notifications from your Git hosting servers');
});
test('it lists connected integrations with reconnect and remove', async function (assert) {
const notifications = new FakeNotificationsService();
notifications.sources = [
test('it lists connected sites with reconnect and remove', async function (assert) {
const sites = new FakeSitesService();
sites.all = [
{
id: 'discourse-abc',
type: 'discourse',
@@ -48,9 +58,7 @@ module(
error: null,
},
];
this.owner.register('service:notifications', notifications, {
instantiate: false,
});
registerServices(this.owner, sites);
const noop = () => {};
@@ -63,12 +71,12 @@ module(
await click(`${row} [data-test-remove]`);
assert.deepEqual(notifications.removed, ['discourse-abc']);
assert.deepEqual(sites.removed, ['discourse-abc']);
});
test('it shows the error for a failing source', async function (assert) {
const notifications = new FakeNotificationsService();
notifications.sources = [
test('it shows the error for a failing site', async function (assert) {
const sites = new FakeSitesService();
sites.all = [
{
id: 'discourse-bad',
type: 'discourse',
@@ -77,9 +85,7 @@ module(
error: 'Access was rejected — reconnect this forum',
},
];
this.owner.register('service:notifications', notifications, {
instantiate: false,
});
registerServices(this.owner, sites);
const noop = () => {};
@@ -0,0 +1,84 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render } from '@ember/test-helpers';
import { tracked } from '@glimmer/tracking';
import SiteLinks from 'dashtab/components/site-links';
class FakeSitesService {
@tracked groups = [];
}
class FakeNotificationsService {
stateFor(site) {
return { status: 'ready', unreadCount: site.unreadCount ?? 0, items: [] };
}
}
module('Integration | Component | site-links', function (hooks) {
setupRenderingTest(hooks);
test('it renders nothing when there are no connected sites', async function (assert) {
const sites = new FakeSitesService();
this.owner.register('service:sites', sites, { instantiate: false });
this.owner.register(
'service:notifications',
new FakeNotificationsService(),
{
instantiate: false,
}
);
await render(<template><SiteLinks /></template>);
assert.dom('[data-test-site-links]').doesNotExist();
});
test('it renders a group per site group', async function (assert) {
const sites = new FakeSitesService();
sites.groups = [
{
id: 'discourse',
label: 'Discourse',
items: [
{
id: 'discourse-abc',
type: 'discourse',
label: 'forum.example.com',
href: 'https://forum.example.com',
unreadCount: 3,
},
],
},
{
id: 'code-forge',
label: 'Code Forges',
items: [
{
id: 'gitea-1',
type: 'gitea',
label: 'git.example.com',
href: 'https://git.example.com',
unreadCount: 0,
},
],
},
];
this.owner.register('service:sites', sites, { instantiate: false });
this.owner.register(
'service:notifications',
new FakeNotificationsService(),
{
instantiate: false,
}
);
await render(<template><SiteLinks /></template>);
assert.dom('[data-test-site-links]').exists();
assert.dom('[data-group="discourse"]').exists();
assert.dom('[data-group="code-forge"]').exists();
assert.dom('a').exists({ count: 2 });
assert.dom('[data-test-site-links-count]').hasText('3');
});
});
@@ -1,12 +1,12 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render } from '@ember/test-helpers';
import NotificationsGroup from 'dashtab/components/notifications/group';
import SiteLinksGroup from 'dashtab/components/site-links/group';
module('Integration | Component | notifications/group', function (hooks) {
module('Integration | Component | site-links/group', function (hooks) {
setupRenderingTest(hooks);
test('it renders an item per source without a visible label', async function (assert) {
test('it renders an item per site without a visible label', async function (assert) {
this.set('items', [
{
id: 'discourse-abc',
@@ -26,7 +26,7 @@ module('Integration | Component | notifications/group', function (hooks) {
await render(
<template>
<NotificationsGroup @id="discourse" @items={{this.items}} />
<SiteLinksGroup @id="discourse" @items={{this.items}} />
</template>
);
@@ -41,7 +41,7 @@ module('Integration | Component | notifications/group', function (hooks) {
await render(
<template>
<NotificationsGroup @id="discourse" @items={{this.items}} />
<SiteLinksGroup @id="discourse" @items={{this.items}} />
</template>
);
@@ -1,12 +1,12 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render } from '@ember/test-helpers';
import NotificationItem from 'dashtab/components/notifications/item';
import SiteLinksItem from 'dashtab/components/site-links/item';
module('Integration | Component | notifications/item', function (hooks) {
module('Integration | Component | site-links/item', function (hooks) {
setupRenderingTest(hooks);
test('it renders a link to the source with an unread count', async function (assert) {
test('it renders a link to the site with an unread count', async function (assert) {
this.set('item', {
id: 'discourse-abc',
type: 'discourse',
@@ -16,14 +16,14 @@ module('Integration | Component | notifications/item', function (hooks) {
unreadCount: 3,
});
await render(<template><NotificationItem @item={{this.item}} /></template>);
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert.dom('a').hasAttribute('href', 'https://forum.example.com');
assert.dom('a').doesNotHaveAttribute('target', '_blank');
assert.dom('a').hasAttribute('title', 'forum.example.com');
assert.dom('a').hasAttribute('aria-label', 'forum.example.com');
assert.dom('[data-test-notifications-count]').hasText('3');
assert.dom('[data-test-notifications-fallback]').hasText('F');
assert.dom('[data-test-site-links-count]').hasText('3');
assert.dom('[data-test-site-links-fallback]').hasText('F');
});
test('it renders the fetched site icon when one is available', async function (assert) {
@@ -40,16 +40,16 @@ module('Integration | Component | notifications/item', function (hooks) {
},
});
await render(<template><NotificationItem @item={{this.item}} /></template>);
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert.dom('[data-test-notifications-icon]').exists();
assert.dom('[data-test-notifications-fallback]').doesNotExist();
assert.dom('[data-test-site-links-icon]').exists();
assert.dom('[data-test-site-links-fallback]').doesNotExist();
assert
.dom('[data-test-notifications-icon]')
.dom('[data-test-site-links-icon]')
.hasAttribute('src', 'data:image/png;base64,abc');
assert.dom('[data-test-notifications-icon]').hasClass('object-cover');
assert.dom('[data-test-site-links-icon]').hasClass('object-cover');
assert
.dom('[data-test-notifications-tile]')
.dom('[data-test-site-links-tile]')
.hasAttribute('style', /background-color:\s*rgb\(29, 78, 216\)/);
});
@@ -61,11 +61,11 @@ module('Integration | Component | notifications/item', function (hooks) {
icon: { dataUrl: 'data:image/png;base64,abc', maskable: false },
});
await render(<template><NotificationItem @item={{this.item}} /></template>);
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert.dom('[data-test-notifications-icon]').hasClass('object-contain');
assert.dom('[data-test-notifications-icon]').hasClass('rounded-md');
assert.dom('[data-test-notifications-icon]').doesNotHaveClass('size-full');
assert.dom('[data-test-site-links-icon]').hasClass('object-contain');
assert.dom('[data-test-site-links-icon]').hasClass('rounded-md');
assert.dom('[data-test-site-links-icon]').doesNotHaveClass('size-full');
});
test('it ignores a theme color that is not a plain CSS color', async function (assert) {
@@ -80,10 +80,10 @@ module('Integration | Component | notifications/item', function (hooks) {
},
});
await render(<template><NotificationItem @item={{this.item}} /></template>);
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert
.dom('[data-test-notifications-tile]')
.dom('[data-test-site-links-tile]')
.doesNotHaveAttribute('style', /background-color/);
});
@@ -95,13 +95,13 @@ module('Integration | Component | notifications/item', function (hooks) {
unreadCount: 0,
});
await render(<template><NotificationItem @item={{this.item}} /></template>);
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert.dom('[data-test-notifications-count]').doesNotExist();
assert.dom('[data-test-notifications-fallback]').exists();
assert.dom('[data-test-site-links-count]').doesNotExist();
assert.dom('[data-test-site-links-fallback]').exists();
});
test('it explains an unreachable source in the tooltip', async function (assert) {
test('it explains an unreachable site in the tooltip', async function (assert) {
this.set('item', {
label: 'forum.example.com',
href: 'https://forum.example.com',
@@ -111,7 +111,7 @@ module('Integration | Component | notifications/item', function (hooks) {
unreadCount: 0,
});
await render(<template><NotificationItem @item={{this.item}} /></template>);
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert.dom('a').hasAttribute('data-status', 'error');
assert.dom('a').hasAttribute('data-error-type', 'unreachable');
+57 -123
View File
@@ -29,46 +29,21 @@ module('Unit | Service | notifications', function (hooks) {
assert.ok(service);
});
test('load groups active supported integrations', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
clientId: 'abc',
});
await storage.set('integrations', 'bitcoin', {
type: 'bitcoin',
status: 'active',
});
await storage.set('integrations', 'discourse-pending', {
type: 'discourse',
status: 'pending',
url: 'https://pending.example.com',
});
test('stateFor defaults to loading for pollable sites and ready for plain links', function (assert) {
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
assert.strictEqual(service.sources.length, 1);
const pollable = service.stateFor({ id: 'a', type: 'discourse' });
assert.strictEqual(pollable.status, 'loading');
assert.strictEqual(pollable.unreadCount, 0);
const groups = service.groups;
assert.strictEqual(groups.length, 1);
assert.strictEqual(groups[0].id, 'discourse');
assert.strictEqual(groups[0].label, 'Discourse');
assert.strictEqual(groups[0].items.length, 1);
const [item] = groups[0].items;
assert.strictEqual(item.label, 'forum.example.com');
assert.strictEqual(item.href, 'https://forum.example.com');
assert.strictEqual(item.status, 'loading');
const plain = service.stateFor({ id: 'b', type: 'gitea' });
assert.strictEqual(plain.status, 'ready');
assert.strictEqual(plain.unreadCount, 0);
service.stopPolling();
});
test('load starts polling when there are sources', async function (assert) {
test('setup starts polling when a pollable site is connected', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
@@ -85,31 +60,21 @@ module('Unit | Service | notifications', function (hooks) {
service.stopPolling();
});
test('resolveIcons attaches a fetched icon to the source', async function (assert) {
test('setup does not poll when only plain site links are connected', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
await storage.set('integrations', 'gitea-1', {
type: 'gitea',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
clientId: 'abc',
url: 'https://git.example.com',
});
const icon = { dataUrl: 'data:image/png;base64,x', maskable: true };
const siteIcons = this.owner.lookup('service:site-icons');
siteIcons.iconFor = async () => icon;
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
await service.resolveIcons();
assert.deepEqual(service.sources[0].icon, icon);
service.stopPolling();
assert.strictEqual(service.intervalId, null);
});
test('fetchSource normalizes a discourse response', async function (assert) {
test('fetchSite normalizes a discourse response', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
@@ -151,7 +116,8 @@ module('Unit | Service | notifications', function (hooks) {
};
};
await service.fetchSource(service.sources[0]);
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(
requestedUrl,
@@ -160,15 +126,17 @@ module('Unit | Service | notifications', function (hooks) {
assert.strictEqual(requestedHeaders['User-Api-Key'], 'key');
assert.strictEqual(requestedHeaders['User-Api-Client-Id'], 'abc');
const [source] = service.sources;
assert.strictEqual(source.status, 'ready');
assert.strictEqual(source.unreadCount, 1);
assert.strictEqual(source.notifications.length, 2);
assert.strictEqual(source.notifications[0].title, 'Hello there');
assert.strictEqual(source.notifications[1].title, 'Nice');
const state = service.stateFor(site);
assert.strictEqual(state.status, 'ready');
assert.strictEqual(state.unreadCount, 1);
assert.strictEqual(state.items.length, 2);
assert.strictEqual(state.items[0].title, 'Hello there');
assert.strictEqual(state.items[1].title, 'Nice');
service.stopPolling();
});
test('fetchSource marks an unreachable source when the request fails', async function (assert) {
test('fetchSite marks an unreachable site when the request fails', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
@@ -185,15 +153,18 @@ module('Unit | Service | notifications', function (hooks) {
throw new TypeError('Failed to fetch');
};
await service.fetchSource(service.sources[0]);
const [site] = service.sites.all;
await service.fetchSite(site);
const [source] = service.sources;
assert.strictEqual(source.status, 'error');
assert.strictEqual(source.errorType, 'unreachable');
assert.strictEqual(source.unreadCount, 0);
const state = service.stateFor(site);
assert.strictEqual(state.status, 'error');
assert.strictEqual(state.errorType, 'unreachable');
assert.strictEqual(state.unreadCount, 0);
service.stopPolling();
});
test('fetchSource flags an unauthorized response', async function (assert) {
test('fetchSite flags an unauthorized response', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
@@ -212,15 +183,18 @@ module('Unit | Service | notifications', function (hooks) {
json: async () => ({}),
});
await service.fetchSource(service.sources[0]);
const [site] = service.sites.all;
await service.fetchSite(site);
const [source] = service.sources;
assert.strictEqual(source.status, 'error');
assert.strictEqual(source.errorType, 'unauthorized');
assert.strictEqual(source.error, 'Reconnect this forum');
const state = service.stateFor(site);
assert.strictEqual(state.status, 'error');
assert.strictEqual(state.errorType, 'unauthorized');
assert.strictEqual(state.error, 'Reconnect this forum');
service.stopPolling();
});
test('fetchSource surfaces a rejected access token', async function (assert) {
test('fetchSite surfaces a rejected access token', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
@@ -239,63 +213,20 @@ module('Unit | Service | notifications', function (hooks) {
json: async () => ({ error_type: 'invalid_access' }),
});
await service.fetchSource(service.sources[0]);
const [site] = service.sites.all;
await service.fetchSite(site);
const [source] = service.sources;
assert.strictEqual(source.errorType, 'unauthorized');
const state = service.stateFor(site);
assert.strictEqual(state.errorType, 'unauthorized');
assert.strictEqual(
source.error,
state.error,
'Access was rejected — reconnect this forum'
);
});
test('reload picks up newly added integrations', async function (assert) {
const storage = this.owner.lookup('service:storage');
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
assert.strictEqual(service.sources.length, 0);
await storage.set('integrations', 'discourse-new', {
type: 'discourse',
status: 'active',
url: 'https://new.example.com',
apiKey: 'key',
});
await service.reload();
assert.strictEqual(service.sources.length, 1);
assert.strictEqual(service.sources[0].id, 'discourse-new');
service.stopPolling();
});
test('removeSource deletes the integration and reloads', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
assert.strictEqual(service.sources.length, 1);
await service.removeSource('discourse-abc');
assert.strictEqual(service.sources.length, 0);
assert.strictEqual(
await storage.get('integrations', 'discourse-abc'),
null
);
service.stopPolling();
});
test('fetchSource flags other HTTP failures', async function (assert) {
test('fetchSite flags other HTTP failures', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
@@ -314,12 +245,15 @@ module('Unit | Service | notifications', function (hooks) {
json: async () => ({}),
});
await service.fetchSource(service.sources[0]);
const [site] = service.sites.all;
await service.fetchSite(site);
const [source] = service.sources;
assert.strictEqual(source.status, 'error');
assert.strictEqual(source.errorType, 'http');
assert.strictEqual(source.error, 'Forum returned 500');
const state = service.stateFor(site);
assert.strictEqual(state.status, 'error');
assert.strictEqual(state.errorType, 'http');
assert.strictEqual(state.error, 'Forum returned 500');
service.stopPolling();
});
test('stopPolling clears the interval', async function (assert) {
+133
View File
@@ -0,0 +1,133 @@
import { module, test } from 'qunit';
import { setupTest } from 'dashtab/tests/helpers';
module('Unit | Service | sites', function (hooks) {
setupTest(hooks);
hooks.beforeEach(async function () {
const storage = this.owner.lookup('service:storage');
await storage.clear('integrations');
const siteIcons = this.owner.lookup('service:site-icons');
siteIcons.iconFor = async () => undefined;
});
test('it exists', function (assert) {
let service = this.owner.lookup('service:sites');
assert.ok(service);
});
test('load groups active supported integrations', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
clientId: 'abc',
});
await storage.set('integrations', 'bitcoin', {
type: 'bitcoin',
status: 'active',
});
await storage.set('integrations', 'discourse-pending', {
type: 'discourse',
status: 'pending',
url: 'https://pending.example.com',
});
const service = this.owner.lookup('service:sites');
await service.ready;
assert.strictEqual(service.all.length, 1);
const groups = service.groups;
assert.strictEqual(groups.length, 1);
assert.strictEqual(groups[0].id, 'discourse');
assert.strictEqual(groups[0].label, 'Discourse');
assert.strictEqual(groups[0].items.length, 1);
const [item] = groups[0].items;
assert.strictEqual(item.label, 'forum.example.com');
assert.strictEqual(item.href, 'https://forum.example.com');
assert.strictEqual(item.groupId, 'discourse');
});
test('integrationFor exposes the raw record for a site', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
});
const service = this.owner.lookup('service:sites');
await service.ready;
const integration = service.integrationFor('discourse-abc');
assert.strictEqual(integration.apiKey, 'key');
assert.strictEqual(service.integrationFor('missing'), undefined);
});
test('resolveIcons attaches a fetched icon to the site', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
clientId: 'abc',
});
const icon = { dataUrl: 'data:image/png;base64,x', maskable: true };
const siteIcons = this.owner.lookup('service:site-icons');
siteIcons.iconFor = async () => icon;
const service = this.owner.lookup('service:sites');
await service.ready;
await service.resolveIcons();
assert.deepEqual(service.all[0].icon, icon);
});
test('reload picks up newly added integrations', async function (assert) {
const storage = this.owner.lookup('service:storage');
const service = this.owner.lookup('service:sites');
await service.ready;
assert.strictEqual(service.all.length, 0);
await storage.set('integrations', 'discourse-new', {
type: 'discourse',
status: 'active',
url: 'https://new.example.com',
apiKey: 'key',
});
await service.reload();
assert.strictEqual(service.all.length, 1);
assert.strictEqual(service.all[0].id, 'discourse-new');
});
test('removeSource deletes the integration and reloads', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
});
const service = this.owner.lookup('service:sites');
await service.ready;
assert.strictEqual(service.all.length, 1);
await service.removeSource('discourse-abc');
assert.strictEqual(service.all.length, 0);
assert.strictEqual(
await storage.get('integrations', 'discourse-abc'),
null
);
});
});