Add Feedbin integration with configurable instance URL
This commit is contained in:
1 parent
ccb2162bca
commit
c28f2d066d
15 files changed
+937
-5
No files matched your search
@@ -0,0 +1,249 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import { action } from '@ember/object';
|
||||
import { service } from '@ember/service';
|
||||
import { on } from '@ember/modifier';
|
||||
import {
|
||||
FEEDBIN_DEFAULT_URL,
|
||||
authenticationUrl,
|
||||
authorizationHeader,
|
||||
encodeBasicToken,
|
||||
normalizeFeedbinUrl,
|
||||
} from '../../../config/feedbin';
|
||||
|
||||
export default class SettingsIntegrationsFeedbinForm extends Component {
|
||||
@service http;
|
||||
@service notifications;
|
||||
@service sites;
|
||||
@service storage;
|
||||
|
||||
@tracked urlDraft = null;
|
||||
@tracked emailDraft = null;
|
||||
@tracked password = '';
|
||||
@tracked error = null;
|
||||
@tracked submitting = false;
|
||||
|
||||
get integration() {
|
||||
return this.args.props?.integration;
|
||||
}
|
||||
|
||||
get isEdit() {
|
||||
return this.args.props?.mode === 'edit';
|
||||
}
|
||||
|
||||
get url() {
|
||||
return this.urlDraft ?? this.integration?.url ?? FEEDBIN_DEFAULT_URL;
|
||||
}
|
||||
|
||||
get defaultUrl() {
|
||||
return FEEDBIN_DEFAULT_URL;
|
||||
}
|
||||
|
||||
get email() {
|
||||
return this.emailDraft ?? this.integration?.email ?? '';
|
||||
}
|
||||
|
||||
get title() {
|
||||
return this.isEdit ? 'Edit Feedbin' : 'Add Feedbin Integration';
|
||||
}
|
||||
|
||||
get submitLabel() {
|
||||
return this.isEdit ? 'Save' : 'Connect';
|
||||
}
|
||||
|
||||
@action
|
||||
updateUrl(event) {
|
||||
this.urlDraft = event.target.value;
|
||||
this.error = null;
|
||||
}
|
||||
|
||||
@action
|
||||
updateEmail(event) {
|
||||
this.emailDraft = event.target.value;
|
||||
this.error = null;
|
||||
}
|
||||
|
||||
@action
|
||||
updatePassword(event) {
|
||||
this.password = event.target.value;
|
||||
this.error = null;
|
||||
}
|
||||
|
||||
@action
|
||||
async handleSubmit(event) {
|
||||
event.preventDefault();
|
||||
this.error = null;
|
||||
|
||||
const instanceUrl = normalizeFeedbinUrl(this.url);
|
||||
|
||||
if (!instanceUrl) {
|
||||
this.error = 'Please enter a valid Feedbin URL';
|
||||
return;
|
||||
}
|
||||
|
||||
const email = this.email.trim();
|
||||
|
||||
if (!email) {
|
||||
this.error = 'Please enter your Feedbin email';
|
||||
return;
|
||||
}
|
||||
|
||||
// On edit a blank password keeps the stored token, so users can change
|
||||
// their email without re-entering the password.
|
||||
const token = this.password
|
||||
? encodeBasicToken(email, this.password)
|
||||
: this.integration?.token;
|
||||
|
||||
if (!token) {
|
||||
this.error = 'Please enter your Feedbin password';
|
||||
return;
|
||||
}
|
||||
|
||||
this.submitting = true;
|
||||
|
||||
try {
|
||||
await this.http.request(authenticationUrl(instanceUrl), {
|
||||
headers: {
|
||||
Authorization: authorizationHeader(token),
|
||||
Accept: 'application/json',
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
this.error = this.classifyError(err);
|
||||
return;
|
||||
} finally {
|
||||
this.submitting = false;
|
||||
}
|
||||
|
||||
// Only the credential fields are persisted; the edit row also carries
|
||||
// display-only fields (avatar, labels) that must not be stored.
|
||||
await this.storage.set('integrations', 'feedbin', {
|
||||
type: 'feedbin',
|
||||
status: 'active',
|
||||
url: instanceUrl,
|
||||
email,
|
||||
token,
|
||||
createdAt: this.integration?.createdAt ?? Date.now(),
|
||||
});
|
||||
|
||||
await this.sites.reload();
|
||||
this.notifications.startPolling();
|
||||
this.args.close?.();
|
||||
}
|
||||
|
||||
classifyError(err) {
|
||||
if (err?.status === 401 || err?.status === 403) {
|
||||
return 'Invalid email or password';
|
||||
}
|
||||
if (err?.name === 'NetworkError') {
|
||||
return 'Unable to reach Feedbin. Check your connection.';
|
||||
}
|
||||
return 'Unable to verify your Feedbin credentials';
|
||||
}
|
||||
|
||||
<template>
|
||||
<div class="p-6">
|
||||
<h3
|
||||
id="modal-title"
|
||||
class="text-base font-semibold leading-6 text-gray-900"
|
||||
>{{this.title}}</h3>
|
||||
<div class="mt-2">
|
||||
<p class="text-sm text-gray-500">
|
||||
Connect your Feedbin account to see the unread count on your
|
||||
dashboard.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<form {{on "submit" this.handleSubmit}} class="mt-5">
|
||||
<div class="space-y-5">
|
||||
<div>
|
||||
<label
|
||||
for="feedbin-url"
|
||||
class="block text-sm font-medium leading-6 text-gray-900"
|
||||
>
|
||||
Server URL
|
||||
</label>
|
||||
<div class="mt-2">
|
||||
<input
|
||||
type="text"
|
||||
inputmode="url"
|
||||
id="feedbin-url"
|
||||
name="url"
|
||||
value={{this.url}}
|
||||
{{on "input" this.updateUrl}}
|
||||
placeholder={{this.defaultUrl}}
|
||||
class="block w-full rounded-md border-0 px-3 py-2 text-sm text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"
|
||||
/>
|
||||
</div>
|
||||
<p class="mt-2 text-sm text-gray-500">
|
||||
Leave as-is unless you self-host Feedbin.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
for="feedbin-email"
|
||||
class="block text-sm font-medium leading-6 text-gray-900"
|
||||
>
|
||||
Email
|
||||
</label>
|
||||
<div class="mt-2">
|
||||
<input
|
||||
type="email"
|
||||
id="feedbin-email"
|
||||
name="email"
|
||||
autocomplete="username"
|
||||
value={{this.email}}
|
||||
{{on "input" this.updateEmail}}
|
||||
placeholder="you@example.com"
|
||||
class="block w-full rounded-md border-0 px-3 py-2 text-sm text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
for="feedbin-password"
|
||||
class="block text-sm font-medium leading-6 text-gray-900"
|
||||
>
|
||||
Password
|
||||
</label>
|
||||
<div class="mt-2">
|
||||
<input
|
||||
type="password"
|
||||
id="feedbin-password"
|
||||
name="password"
|
||||
autocomplete="current-password"
|
||||
value={{this.password}}
|
||||
{{on "input" this.updatePassword}}
|
||||
placeholder={{if this.isEdit "Leave blank to keep current" ""}}
|
||||
class="block w-full rounded-md border-0 px-3 py-2 text-sm text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{#if this.error}}
|
||||
<p class="text-sm text-red-600">{{this.error}}</p>
|
||||
{{/if}}
|
||||
</div>
|
||||
|
||||
<div class="mt-5 sm:mt-6 flex gap-3">
|
||||
<button
|
||||
type="submit"
|
||||
disabled={{this.submitting}}
|
||||
class="inline-flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:opacity-50"
|
||||
>
|
||||
{{this.submitLabel}}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
{{on "click" @close}}
|
||||
class="inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
export const FEEDBIN_DEFAULT_URL = 'https://feedbin.com';
|
||||
|
||||
// Normalizes the user-supplied instance URL to a bare origin. Feedbin's API
|
||||
// lives on the `api.` subdomain of the instance (self-hosted deployments are
|
||||
// documented to enable `api.<domain>` as well), so the API base is derived
|
||||
// from whatever instance URL is configured.
|
||||
export function normalizeFeedbinUrl(input) {
|
||||
let value = (input ?? '').trim();
|
||||
|
||||
// Reject empty input and anything containing whitespace; the WHATWG parser is
|
||||
// lenient enough to accept some malformed hosts that this should not.
|
||||
if (!value || /\s/.test(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!/^https?:\/\//i.test(value)) {
|
||||
value = `https://${value}`;
|
||||
}
|
||||
|
||||
try {
|
||||
return new URL(value).origin;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function apiBaseUrl(instanceUrl = FEEDBIN_DEFAULT_URL) {
|
||||
const url = new URL(normalizeFeedbinUrl(instanceUrl) ?? FEEDBIN_DEFAULT_URL);
|
||||
const host = url.hostname.startsWith('api.')
|
||||
? url.hostname
|
||||
: `api.${url.hostname}`;
|
||||
|
||||
return `${url.protocol}//${host}`;
|
||||
}
|
||||
|
||||
// Credentials are checked against this endpoint when the form is submitted.
|
||||
export function authenticationUrl(instanceUrl) {
|
||||
return `${apiBaseUrl(instanceUrl)}/v2/authentication.json`;
|
||||
}
|
||||
|
||||
// `read=false` returns unread entries and, via the `X-Feedbin-Record-Count`
|
||||
// response header, the total unread count without downloading every entry id.
|
||||
export function unreadCountUrl(instanceUrl) {
|
||||
return `${apiBaseUrl(instanceUrl)}/v2/entries.json?read=false&per_page=1`;
|
||||
}
|
||||
|
||||
// Basic auth requires the password, so it is stored pre-encoded. Encoding is
|
||||
// done byte-wise to keep non-ASCII passwords intact.
|
||||
export function encodeBasicToken(email, password) {
|
||||
const bytes = new TextEncoder().encode(`${email}:${password}`);
|
||||
let binary = '';
|
||||
|
||||
for (const byte of bytes) {
|
||||
binary += String.fromCharCode(byte);
|
||||
}
|
||||
|
||||
return btoa(binary);
|
||||
}
|
||||
|
||||
export function authorizationHeader(token) {
|
||||
return `Basic ${token}`;
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
import DiscourseForm from '../components/settings/integrations/discourse-form.gjs';
|
||||
import BitcoinForm from '../components/settings/integrations/bitcoin-form.gjs';
|
||||
import FeedbinForm from '../components/settings/integrations/feedbin-form.gjs';
|
||||
|
||||
const FORMS = {
|
||||
discourse: DiscourseForm,
|
||||
bitcoin: BitcoinForm,
|
||||
feedbin: FeedbinForm,
|
||||
};
|
||||
|
||||
export function formFor(type) {
|
||||
|
||||
@@ -4,6 +4,8 @@ const PENCIL_ICON_PATH =
|
||||
const BITCOIN_ICON_PATH =
|
||||
'M23.638 14.904c-1.602 6.43-8.113 10.34-14.542 8.736C2.67 22.05-1.244 15.525.362 9.105 1.962 2.67 8.475-1.243 14.9.358c6.43 1.605 10.342 8.115 8.738 14.546zm-6.35-4.613c.24-1.59-.974-2.45-2.64-3.03l.54-2.153-1.315-.328-.525 2.107c-.345-.087-.705-.167-1.064-.25l.526-2.127-1.32-.33-.54 2.165c-.285-.067-.565-.132-.84-.2l-.006-.02-1.815-.45-.35 1.407s.975.225.955.238c.535.136.63.486.615.766l-.862 3.454c-.04.1-.14.25-.365.195.01.014-.95-.237-.95-.237l-.656 1.51 1.72.43c.32.08.634.16.955.25l-.54 2.2 1.32.33.54-2.18c.36.1.71.19 1.05.28l-.53 2.16 1.31.33.54-2.2c2.28.43 3.99.26 4.71-1.8.58-1.65-.03-2.6-1.22-3.22.87-.2 1.53-.78 1.7-1.96z';
|
||||
|
||||
export const FEEDBIN_ICON_URL = '/img/feedbin.png';
|
||||
|
||||
// Metadata for every integration the app knows about. `hasSettings` controls
|
||||
// whether the Manage screen exposes an edit affordance; `reconnectable` marks
|
||||
// integrations with an auth token that can be refreshed; `supportsMultiple`
|
||||
@@ -33,6 +35,17 @@ export const INTEGRATIONS = [
|
||||
hasSettings: true,
|
||||
reconnectable: false,
|
||||
},
|
||||
{
|
||||
id: 'feedbin',
|
||||
label: 'Feedbin',
|
||||
description: 'Unread count from your Feedbin RSS reader',
|
||||
colorClass: 'bg-emerald-600',
|
||||
iconUrl: FEEDBIN_ICON_URL,
|
||||
available: true,
|
||||
supportsMultiple: false,
|
||||
hasSettings: true,
|
||||
reconnectable: false,
|
||||
},
|
||||
{
|
||||
id: 'gitea',
|
||||
label: 'Gitea',
|
||||
|
||||
@@ -4,6 +4,11 @@ export const SITE_GROUPS = [
|
||||
label: 'Discourse',
|
||||
integrationTypes: ['discourse'],
|
||||
},
|
||||
{
|
||||
id: 'feedbin',
|
||||
label: 'Feedbin',
|
||||
integrationTypes: ['feedbin'],
|
||||
},
|
||||
{
|
||||
id: 'code-forge',
|
||||
label: 'Code Forges',
|
||||
|
||||
@@ -29,6 +29,36 @@ const ADAPTERS = {
|
||||
return this.sites.removeSource(instance.id);
|
||||
},
|
||||
},
|
||||
feedbin: {
|
||||
instances() {
|
||||
return this.sites.all
|
||||
.filter((site) => site.type === 'feedbin')
|
||||
.map((site) => {
|
||||
const state = this.notifications.stateFor(site);
|
||||
const integration = this.sites.integrationFor(site.id);
|
||||
|
||||
return {
|
||||
id: site.id,
|
||||
type: 'feedbin',
|
||||
label: site.label,
|
||||
icon: site.icon,
|
||||
status: 'active',
|
||||
// Passed through so the edit form can prefill the email and keep
|
||||
// the stored token when the password field is left blank.
|
||||
email: integration?.email,
|
||||
token: integration?.token,
|
||||
error: state.error,
|
||||
errorType: state.errorType,
|
||||
// Feedbin uses Basic auth, so there is no token to refresh;
|
||||
// fixing credentials happens through the Settings form.
|
||||
canReconnect: false,
|
||||
};
|
||||
});
|
||||
},
|
||||
remove(instance) {
|
||||
return this.sites.removeSource(instance.id);
|
||||
},
|
||||
},
|
||||
bitcoin: {
|
||||
instances() {
|
||||
if (!this.bitcoin.enabled) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import Service, { service } from '@ember/service';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import { HttpError } from './http';
|
||||
import { authorizationHeader, unreadCountUrl } from '../config/feedbin';
|
||||
|
||||
export const NOTIFICATIONS_DEFAULT_INTERVAL = 60000;
|
||||
|
||||
@@ -30,8 +31,29 @@ async function fetchDiscourseNotifications(integration, http) {
|
||||
};
|
||||
}
|
||||
|
||||
// Feedbin's unread endpoint returns entry ids only, and potentially all of
|
||||
// them. Requesting a single unread entry instead lets us read the total from
|
||||
// the `X-Feedbin-Record-Count` response header, so the badge stays cheap.
|
||||
async function fetchFeedbinNotifications(integration, http) {
|
||||
const response = await http.request(unreadCountUrl(integration.url), {
|
||||
headers: {
|
||||
Authorization: authorizationHeader(integration.token),
|
||||
Accept: 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
const header = response.headers.get('X-Feedbin-Record-Count');
|
||||
const count = Number.parseInt(header ?? '0', 10);
|
||||
|
||||
return {
|
||||
unreadCount: Number.isNaN(count) ? 0 : count,
|
||||
notifications: [],
|
||||
};
|
||||
}
|
||||
|
||||
const ADAPTERS = {
|
||||
discourse: fetchDiscourseNotifications,
|
||||
feedbin: fetchFeedbinNotifications,
|
||||
};
|
||||
|
||||
function defaultState(type) {
|
||||
@@ -124,12 +146,14 @@ export default class NotificationsService extends Service {
|
||||
console.error(`Error fetching ${site.type} notifications:`, err);
|
||||
this.setState(site.id, {
|
||||
status: 'error',
|
||||
...this.classifyError(err),
|
||||
...this.classifyError(err, site),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
classifyError(err) {
|
||||
classifyError(err, site) {
|
||||
const isFeedbin = site?.type === 'feedbin';
|
||||
|
||||
if (err instanceof HttpError) {
|
||||
if (err.errorType === 'not_logged_in') {
|
||||
return {
|
||||
@@ -146,17 +170,23 @@ export default class NotificationsService extends Service {
|
||||
if (err.status === 401 || err.status === 403) {
|
||||
return {
|
||||
errorType: 'unauthorized',
|
||||
error: 'Reconnect this forum',
|
||||
error: isFeedbin
|
||||
? 'Update Feedbin credentials'
|
||||
: 'Reconnect this forum',
|
||||
};
|
||||
}
|
||||
return {
|
||||
errorType: 'http',
|
||||
error: `Forum returned ${err.status}`,
|
||||
error: isFeedbin
|
||||
? `Feedbin returned ${err.status}`
|
||||
: `Forum returned ${err.status}`,
|
||||
};
|
||||
}
|
||||
return {
|
||||
errorType: 'unreachable',
|
||||
error: 'Notifications blocked — enable CORS on the forum or open it',
|
||||
error: isFeedbin
|
||||
? 'Feedbin blocked — enable CORS or open feedbin.com'
|
||||
: 'Notifications blocked — enable CORS on the forum or open it',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import Service, { service } from '@ember/service';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import { SITE_GROUPS } from '../config/sites';
|
||||
import { FEEDBIN_ICON_URL } from '../config/integrations';
|
||||
import { FEEDBIN_DEFAULT_URL } from '../config/feedbin';
|
||||
|
||||
function labelForUrl(url) {
|
||||
try {
|
||||
@@ -69,6 +71,22 @@ export default class SitesService extends Service {
|
||||
buildSite(key, integration) {
|
||||
this.integrationsById.set(key, integration);
|
||||
|
||||
// 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 },
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
id: key,
|
||||
type: integration.type,
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
@@ -67,9 +67,12 @@ module(
|
||||
assert.dom().containsText('Notifications from your discussion forums');
|
||||
assert.dom().containsText('Bitcoin Price Ticker');
|
||||
assert.dom().containsText('Live BTC price on your dashboard');
|
||||
assert.dom().containsText('Feedbin');
|
||||
assert.dom().containsText('Unread count from your Feedbin RSS reader');
|
||||
assert.dom().containsText('Gitea');
|
||||
assert.dom().containsText('Notifications from your Git hosting servers');
|
||||
|
||||
assert.dom('[data-add="feedbin"] img[src="/img/feedbin.png"]').exists();
|
||||
assert.dom('[data-add="gitea"]').isDisabled();
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,248 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'dashtab/tests/helpers';
|
||||
import { render, fillIn, triggerEvent } from '@ember/test-helpers';
|
||||
import Feedbin from 'dashtab/components/settings/integrations/feedbin-form';
|
||||
|
||||
function registerServices(owner, overrides = {}) {
|
||||
const http = overrides.http ?? { request: async () => ({}) };
|
||||
const storage = overrides.storage ?? { set: async () => {} };
|
||||
const sites = overrides.sites ?? { reload: async () => {} };
|
||||
const notifications = overrides.notifications ?? { startPolling: () => {} };
|
||||
|
||||
owner.register('service:http', http, { instantiate: false });
|
||||
owner.register('service:storage', storage, { instantiate: false });
|
||||
owner.register('service:sites', sites, { instantiate: false });
|
||||
owner.register('service:notifications', notifications, {
|
||||
instantiate: false,
|
||||
});
|
||||
}
|
||||
|
||||
module(
|
||||
'Integration | Component | settings/integrations/feedbin-form',
|
||||
function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test('it renders the credential form', async function (assert) {
|
||||
registerServices(this.owner);
|
||||
const noop = () => {};
|
||||
|
||||
await render(<template><Feedbin @close={{noop}} /></template>);
|
||||
|
||||
assert.dom().containsText('Add Feedbin Integration');
|
||||
assert.dom('#feedbin-url').exists().hasValue('https://feedbin.com');
|
||||
assert.dom('#feedbin-email').exists();
|
||||
assert.dom('#feedbin-password').exists();
|
||||
assert.dom('form').exists();
|
||||
});
|
||||
|
||||
test('submitting validates and stores the credentials', async function (assert) {
|
||||
const requests = [];
|
||||
const saved = [];
|
||||
let reloaded = false;
|
||||
let polling = false;
|
||||
let closed = false;
|
||||
|
||||
registerServices(this.owner, {
|
||||
http: {
|
||||
request: async (url, options) => {
|
||||
requests.push({ url, options });
|
||||
return {};
|
||||
},
|
||||
},
|
||||
storage: {
|
||||
set: async (namespace, key, value) =>
|
||||
saved.push({ namespace, key, value }),
|
||||
},
|
||||
sites: {
|
||||
reload: async () => {
|
||||
reloaded = true;
|
||||
},
|
||||
},
|
||||
notifications: {
|
||||
startPolling: () => {
|
||||
polling = true;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const closeDialog = () => {
|
||||
closed = true;
|
||||
};
|
||||
|
||||
await render(<template><Feedbin @close={{closeDialog}} /></template>);
|
||||
await fillIn('#feedbin-email', 'me@example.com');
|
||||
await fillIn('#feedbin-password', 'secret');
|
||||
await triggerEvent('form', 'submit');
|
||||
|
||||
assert.strictEqual(requests.length, 1);
|
||||
assert.strictEqual(
|
||||
requests[0].url,
|
||||
'https://api.feedbin.com/v2/authentication.json'
|
||||
);
|
||||
assert.strictEqual(
|
||||
requests[0].options.headers['Authorization'],
|
||||
`Basic ${btoa('me@example.com:secret')}`
|
||||
);
|
||||
|
||||
assert.strictEqual(saved.length, 1);
|
||||
assert.strictEqual(saved[0].namespace, 'integrations');
|
||||
assert.strictEqual(saved[0].key, 'feedbin');
|
||||
assert.strictEqual(saved[0].value.type, 'feedbin');
|
||||
assert.strictEqual(saved[0].value.status, 'active');
|
||||
assert.strictEqual(saved[0].value.url, 'https://feedbin.com');
|
||||
assert.strictEqual(saved[0].value.email, 'me@example.com');
|
||||
assert.strictEqual(saved[0].value.token, btoa('me@example.com:secret'));
|
||||
|
||||
assert.true(reloaded);
|
||||
assert.true(polling);
|
||||
assert.true(closed);
|
||||
});
|
||||
|
||||
test('submitting a self-hosted URL derives the api subdomain', async function (assert) {
|
||||
const requests = [];
|
||||
const saved = [];
|
||||
|
||||
registerServices(this.owner, {
|
||||
http: {
|
||||
request: async (url, options) => {
|
||||
requests.push({ url, options });
|
||||
return {};
|
||||
},
|
||||
},
|
||||
storage: {
|
||||
set: async (namespace, key, value) => saved.push(value),
|
||||
},
|
||||
});
|
||||
|
||||
const noop = () => {};
|
||||
|
||||
await render(<template><Feedbin @close={{noop}} /></template>);
|
||||
await fillIn('#feedbin-url', 'feedbin.example.com');
|
||||
await fillIn('#feedbin-email', 'me@example.com');
|
||||
await fillIn('#feedbin-password', 'secret');
|
||||
await triggerEvent('form', 'submit');
|
||||
|
||||
assert.strictEqual(
|
||||
requests[0].url,
|
||||
'https://api.feedbin.example.com/v2/authentication.json'
|
||||
);
|
||||
assert.strictEqual(saved[0].url, 'https://feedbin.example.com');
|
||||
});
|
||||
|
||||
test('it rejects an invalid server URL', async function (assert) {
|
||||
const requests = [];
|
||||
|
||||
registerServices(this.owner, {
|
||||
http: {
|
||||
request: async (...args) => requests.push(args),
|
||||
},
|
||||
});
|
||||
|
||||
const noop = () => {};
|
||||
|
||||
await render(<template><Feedbin @close={{noop}} /></template>);
|
||||
await fillIn('#feedbin-url', 'not a valid url');
|
||||
await fillIn('#feedbin-email', 'me@example.com');
|
||||
await fillIn('#feedbin-password', 'secret');
|
||||
await triggerEvent('form', 'submit');
|
||||
|
||||
assert.dom('#feedbin-url').hasValue('not a valid url');
|
||||
assert.dom().containsText('Please enter a valid Feedbin URL');
|
||||
assert.strictEqual(requests.length, 0);
|
||||
});
|
||||
|
||||
test('it reports invalid credentials without storing them', async function (assert) {
|
||||
const saved = [];
|
||||
let closed = false;
|
||||
|
||||
registerServices(this.owner, {
|
||||
http: {
|
||||
request: async () => {
|
||||
throw { status: 401 };
|
||||
},
|
||||
},
|
||||
storage: {
|
||||
set: async (...args) => saved.push(args),
|
||||
},
|
||||
});
|
||||
|
||||
const closeDialog = () => {
|
||||
closed = true;
|
||||
};
|
||||
|
||||
await render(<template><Feedbin @close={{closeDialog}} /></template>);
|
||||
await fillIn('#feedbin-email', 'me@example.com');
|
||||
await fillIn('#feedbin-password', 'wrong');
|
||||
await triggerEvent('form', 'submit');
|
||||
|
||||
assert.dom().containsText('Invalid email or password');
|
||||
assert.strictEqual(saved.length, 0);
|
||||
assert.false(closed);
|
||||
});
|
||||
|
||||
test('it requires an email and password', async function (assert) {
|
||||
registerServices(this.owner);
|
||||
const noop = () => {};
|
||||
|
||||
await render(<template><Feedbin @close={{noop}} /></template>);
|
||||
await triggerEvent('form', 'submit');
|
||||
|
||||
assert.dom().containsText('Please enter your Feedbin email');
|
||||
});
|
||||
|
||||
test('edit mode prefills the email and keeps the stored token when the password is blank', async function (assert) {
|
||||
const requests = [];
|
||||
const saved = [];
|
||||
|
||||
registerServices(this.owner, {
|
||||
http: {
|
||||
request: async (url, options) => {
|
||||
requests.push({ url, options });
|
||||
return {};
|
||||
},
|
||||
},
|
||||
storage: {
|
||||
set: async (namespace, key, value) =>
|
||||
saved.push({ namespace, key, value }),
|
||||
},
|
||||
});
|
||||
|
||||
this.props = {
|
||||
mode: 'edit',
|
||||
integration: {
|
||||
url: 'https://feedbin.example.com',
|
||||
email: 'old@example.com',
|
||||
token: 'OLDT0KEN',
|
||||
colorClass: 'bg-emerald-600',
|
||||
},
|
||||
};
|
||||
|
||||
const noop = () => {};
|
||||
|
||||
await render(
|
||||
<template><Feedbin @props={{this.props}} @close={{noop}} /></template>
|
||||
);
|
||||
|
||||
assert.dom().containsText('Edit Feedbin');
|
||||
assert.dom('#feedbin-url').hasValue('https://feedbin.example.com');
|
||||
assert.dom('#feedbin-email').hasValue('old@example.com');
|
||||
|
||||
await triggerEvent('form', 'submit');
|
||||
|
||||
assert.strictEqual(requests.length, 1);
|
||||
assert.strictEqual(
|
||||
requests[0].url,
|
||||
'https://api.feedbin.example.com/v2/authentication.json'
|
||||
);
|
||||
assert.strictEqual(
|
||||
requests[0].options.headers['Authorization'],
|
||||
'Basic OLDT0KEN'
|
||||
);
|
||||
assert.strictEqual(saved.length, 1);
|
||||
assert.strictEqual(saved[0].value.url, 'https://feedbin.example.com');
|
||||
assert.strictEqual(saved[0].value.email, 'old@example.com');
|
||||
assert.strictEqual(saved[0].value.token, 'OLDT0KEN');
|
||||
assert.strictEqual(saved[0].value.colorClass, undefined);
|
||||
});
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,59 @@
|
||||
import { module, test } from 'qunit';
|
||||
import {
|
||||
authenticationUrl,
|
||||
apiBaseUrl,
|
||||
encodeBasicToken,
|
||||
normalizeFeedbinUrl,
|
||||
unreadCountUrl,
|
||||
} from 'dashtab/config/feedbin';
|
||||
|
||||
module('Unit | Config | feedbin', function () {
|
||||
test('normalizeFeedbinUrl normalizes input and rejects junk', function (assert) {
|
||||
assert.strictEqual(
|
||||
normalizeFeedbinUrl('feedbin.com'),
|
||||
'https://feedbin.com'
|
||||
);
|
||||
assert.strictEqual(
|
||||
normalizeFeedbinUrl('https://feedbin.com/'),
|
||||
'https://feedbin.com'
|
||||
);
|
||||
assert.strictEqual(
|
||||
normalizeFeedbinUrl('http://feedbin.example.com'),
|
||||
'http://feedbin.example.com'
|
||||
);
|
||||
assert.strictEqual(normalizeFeedbinUrl(''), null);
|
||||
assert.strictEqual(normalizeFeedbinUrl(' '), null);
|
||||
assert.strictEqual(normalizeFeedbinUrl('http://'), null);
|
||||
assert.strictEqual(normalizeFeedbinUrl('not a valid url'), null);
|
||||
});
|
||||
|
||||
test('apiBaseUrl derives the api subdomain of the instance', function (assert) {
|
||||
assert.strictEqual(apiBaseUrl(), 'https://api.feedbin.com');
|
||||
assert.strictEqual(
|
||||
apiBaseUrl('https://feedbin.example.com'),
|
||||
'https://api.feedbin.example.com'
|
||||
);
|
||||
assert.strictEqual(
|
||||
apiBaseUrl('https://api.example.com'),
|
||||
'https://api.example.com'
|
||||
);
|
||||
});
|
||||
|
||||
test('endpoint URLs build on the derived api host', function (assert) {
|
||||
assert.strictEqual(
|
||||
authenticationUrl('https://feedbin.example.com'),
|
||||
'https://api.feedbin.example.com/v2/authentication.json'
|
||||
);
|
||||
assert.strictEqual(
|
||||
unreadCountUrl(),
|
||||
'https://api.feedbin.com/v2/entries.json?read=false&per_page=1'
|
||||
);
|
||||
});
|
||||
|
||||
test('encodeBasicToken encodes the email and password pair', function (assert) {
|
||||
assert.strictEqual(
|
||||
encodeBasicToken('me@example.com', 'secret'),
|
||||
btoa('me@example.com:secret')
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,10 @@ class FakeSitesService {
|
||||
@tracked all = [];
|
||||
removed = [];
|
||||
|
||||
integrationFor(id) {
|
||||
return this.records?.[id];
|
||||
}
|
||||
|
||||
async removeSource(id) {
|
||||
this.removed.push(id);
|
||||
this.all = this.all.filter((site) => site.id !== id);
|
||||
@@ -170,4 +174,35 @@ module('Unit | Service | integrations', function (hooks) {
|
||||
await service.remove({ type: 'bitcoin', id: 'bitcoin' });
|
||||
assert.true(bitcoin.disabled);
|
||||
});
|
||||
|
||||
test('feedbin adapter exposes the connected account without reconnect', async function (assert) {
|
||||
const sites = new FakeSitesService();
|
||||
sites.all = [
|
||||
{
|
||||
id: 'feedbin',
|
||||
type: 'feedbin',
|
||||
label: 'Feedbin',
|
||||
href: 'https://feedbin.com',
|
||||
},
|
||||
];
|
||||
sites.records = {
|
||||
feedbin: { email: 'me@example.com', token: 'dG9rZW4=' },
|
||||
};
|
||||
|
||||
register(this.owner, { sites });
|
||||
|
||||
const service = this.owner.lookup('service:integrations');
|
||||
const feedbin = service.all.find((entry) => entry.id === 'feedbin');
|
||||
|
||||
assert.true(feedbin.supportsMultiple === false);
|
||||
assert.true(feedbin.hasSettings);
|
||||
assert.false(feedbin.reconnectable);
|
||||
assert.strictEqual(feedbin.instances.length, 1);
|
||||
assert.strictEqual(feedbin.instances[0].id, 'feedbin');
|
||||
assert.strictEqual(feedbin.instances[0].label, 'Feedbin');
|
||||
assert.notOk(feedbin.instances[0].url);
|
||||
assert.strictEqual(feedbin.instances[0].email, 'me@example.com');
|
||||
assert.strictEqual(feedbin.instances[0].token, 'dG9rZW4=');
|
||||
assert.false(feedbin.instances[0].canReconnect);
|
||||
});
|
||||
});
|
||||
@@ -136,6 +136,138 @@ module('Unit | Service | notifications', function (hooks) {
|
||||
service.stopPolling();
|
||||
});
|
||||
|
||||
test('fetchSite reads the Feedbin unread count from the record count header', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('integrations', 'feedbin', {
|
||||
type: 'feedbin',
|
||||
status: 'active',
|
||||
email: 'me@example.com',
|
||||
token: 'dG9rZW4=',
|
||||
});
|
||||
|
||||
const service = this.owner.lookup('service:notifications');
|
||||
service.fetchAll = async () => {};
|
||||
await service.ready;
|
||||
|
||||
let requestedUrl;
|
||||
let requestedHeaders;
|
||||
globalThis.fetch = async (url, options) => {
|
||||
requestedUrl = url;
|
||||
requestedHeaders = options.headers;
|
||||
return {
|
||||
ok: true,
|
||||
headers: {
|
||||
get: (name) => (name === 'X-Feedbin-Record-Count' ? '42' : null),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const [site] = service.sites.all;
|
||||
await service.fetchSite(site);
|
||||
|
||||
assert.strictEqual(
|
||||
requestedUrl,
|
||||
'https://api.feedbin.com/v2/entries.json?read=false&per_page=1'
|
||||
);
|
||||
assert.strictEqual(requestedHeaders['Authorization'], 'Basic dG9rZW4=');
|
||||
assert.strictEqual(requestedHeaders['Accept'], 'application/json');
|
||||
|
||||
const state = service.stateFor(site);
|
||||
assert.strictEqual(state.status, 'ready');
|
||||
assert.strictEqual(state.unreadCount, 42);
|
||||
assert.deepEqual(state.items, []);
|
||||
|
||||
service.stopPolling();
|
||||
});
|
||||
|
||||
test('fetchSite derives the Feedbin API host from a self-hosted URL', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('integrations', 'feedbin', {
|
||||
type: 'feedbin',
|
||||
status: 'active',
|
||||
url: 'https://feedbin.example.com',
|
||||
email: 'me@example.com',
|
||||
token: 'dG9rZW4=',
|
||||
});
|
||||
|
||||
const service = this.owner.lookup('service:notifications');
|
||||
service.fetchAll = async () => {};
|
||||
await service.ready;
|
||||
|
||||
let requestedUrl;
|
||||
globalThis.fetch = async (url) => {
|
||||
requestedUrl = url;
|
||||
return { ok: true, headers: { get: () => '7' } };
|
||||
};
|
||||
|
||||
const [site] = service.sites.all;
|
||||
await service.fetchSite(site);
|
||||
|
||||
assert.strictEqual(
|
||||
requestedUrl,
|
||||
'https://api.feedbin.example.com/v2/entries.json?read=false&per_page=1'
|
||||
);
|
||||
assert.strictEqual(service.stateFor(site).unreadCount, 7);
|
||||
|
||||
service.stopPolling();
|
||||
});
|
||||
|
||||
test('fetchSite treats a missing Feedbin count header as zero', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('integrations', 'feedbin', {
|
||||
type: 'feedbin',
|
||||
status: 'active',
|
||||
email: 'me@example.com',
|
||||
token: 'dG9rZW4=',
|
||||
});
|
||||
|
||||
const service = this.owner.lookup('service:notifications');
|
||||
service.fetchAll = async () => {};
|
||||
await service.ready;
|
||||
|
||||
globalThis.fetch = async () => ({
|
||||
ok: true,
|
||||
headers: { get: () => null },
|
||||
});
|
||||
|
||||
const [site] = service.sites.all;
|
||||
await service.fetchSite(site);
|
||||
|
||||
assert.strictEqual(service.stateFor(site).unreadCount, 0);
|
||||
|
||||
service.stopPolling();
|
||||
});
|
||||
|
||||
test('fetchSite flags invalid Feedbin credentials', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('integrations', 'feedbin', {
|
||||
type: 'feedbin',
|
||||
status: 'active',
|
||||
email: 'me@example.com',
|
||||
token: 'stale',
|
||||
});
|
||||
|
||||
const service = this.owner.lookup('service:notifications');
|
||||
service.fetchAll = async () => {};
|
||||
await service.ready;
|
||||
|
||||
globalThis.fetch = async () => ({
|
||||
ok: false,
|
||||
status: 401,
|
||||
json: async () => ({}),
|
||||
});
|
||||
|
||||
const [site] = service.sites.all;
|
||||
await service.fetchSite(site);
|
||||
|
||||
const state = service.stateFor(site);
|
||||
assert.strictEqual(state.status, 'error');
|
||||
assert.strictEqual(state.errorType, 'unauthorized');
|
||||
assert.strictEqual(state.error, 'Update Feedbin credentials');
|
||||
|
||||
service.stopPolling();
|
||||
});
|
||||
|
||||
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', {
|
||||
|
||||
@@ -53,6 +53,52 @@ module('Unit | Service | sites', function (hooks) {
|
||||
assert.strictEqual(item.groupId, 'discourse');
|
||||
});
|
||||
|
||||
test('load builds the Feedbin site with a bundled maskable icon', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('integrations', 'feedbin', {
|
||||
type: 'feedbin',
|
||||
status: 'active',
|
||||
email: 'me@example.com',
|
||||
token: 'dG9rZW4=',
|
||||
});
|
||||
|
||||
const service = this.owner.lookup('service:sites');
|
||||
await service.ready;
|
||||
|
||||
assert.strictEqual(service.all.length, 1);
|
||||
|
||||
const [item] = service.all;
|
||||
assert.strictEqual(item.label, 'Feedbin');
|
||||
assert.strictEqual(item.href, 'https://feedbin.com');
|
||||
assert.strictEqual(item.groupId, 'feedbin');
|
||||
assert.notOk(item.url);
|
||||
assert.deepEqual(item.icon, {
|
||||
dataUrl: '/img/feedbin.png',
|
||||
maskable: true,
|
||||
});
|
||||
|
||||
const groups = service.groups;
|
||||
assert.strictEqual(groups.length, 1);
|
||||
assert.strictEqual(groups[0].id, 'feedbin');
|
||||
assert.strictEqual(groups[0].label, 'Feedbin');
|
||||
});
|
||||
|
||||
test('the Feedbin site links to a self-hosted URL when configured', async function (assert) {
|
||||
const storage = this.owner.lookup('service:storage');
|
||||
await storage.set('integrations', 'feedbin', {
|
||||
type: 'feedbin',
|
||||
status: 'active',
|
||||
url: 'https://feedbin.example.com',
|
||||
email: 'me@example.com',
|
||||
token: 'dG9rZW4=',
|
||||
});
|
||||
|
||||
const service = this.owner.lookup('service:sites');
|
||||
await service.ready;
|
||||
|
||||
assert.strictEqual(service.all[0].href, 'https://feedbin.example.com');
|
||||
});
|
||||
|
||||
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', {
|
||||
|
||||
Reference in new issue
Block a user