Files
raucao 4f3c4334bd Use only normal API keys for OpenRouter spend
OpenRouter now rejects management keys from the browser on both
/api/v1/keys and /api/v1/analytics/query, so the management-key flow can
no longer work in the extension. Read the configured key's own usage from
GET /api/v1/key instead, which returns UTC daily, weekly, and monthly
buckets and works from the browser.

Drop the key-type selection, key listing/selection, pagination, and the
management/analytics endpoints. Validate the key against /key before
persisting, and write a fresh record so legacy keyType/keyIds fields are
dropped. Keep the browser-block hint for anyone still storing a
management key.
2026-11-16 22:16:35 +04:00

323 lines
9.8 KiB
JavaScript

import Service, { service } from '@ember/service';
import { INTEGRATIONS, integrationFor } from '../config/integrations';
import { displayHost } from '../utils/display-host';
// Bridges the static integration registry with the live instance state owned by
// the `sites` (auth-based) and `bitcoin` services, so the Add/Manage screens can
// read one uniform model instead of special-casing each integration type.
const ADAPTERS = {
discourse: {
instances() {
return this.sites.all
.filter((site) => site.type === 'discourse')
.map((site) => {
const state = this.notifications.stateFor(site);
return {
id: site.id,
type: 'discourse',
label: site.label,
url: site.url,
icon: site.icon,
status: 'active',
error: state.error,
errorType: state.errorType,
canReconnect: state.errorType === 'unauthorized',
};
});
},
remove(instance) {
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);
},
},
github: {
instances() {
return this.sites.all
.filter((site) => site.type === 'github')
.map((site) => {
const state = this.notifications.stateFor(site);
const integration = this.sites.integrationFor(site.id);
return {
id: site.id,
type: 'github',
label: site.label,
icon: site.icon,
status: 'active',
// Passed through so the edit form can prefill the host and keep
// the stored token when the token field is left blank.
url: integration?.url,
token: integration?.token,
error: state.error,
errorType: state.errorType,
// GitHub uses a personal access token, so fixing a rejected
// credential happens through the Settings form.
canReconnect: false,
};
});
},
remove(instance) {
return this.sites.removeSource(instance.id);
},
},
gitea: {
instances() {
return this.sites.all
.filter((site) => site.type === 'gitea')
.map((site) => {
const state = this.notifications.stateFor(site);
const integration = this.sites.integrationFor(site.id);
return {
id: site.id,
type: 'gitea',
label: site.label,
icon: site.icon,
status: 'active',
// Passed through so the edit form can prefill the host and keep
// the stored token when the token field is left blank.
url: integration?.url,
token: integration?.token,
error: state.error,
errorType: state.errorType,
// Gitea uses an access token, so fixing a rejected credential
// happens through the Settings form.
canReconnect: false,
};
});
},
remove(instance) {
return this.sites.removeSource(instance.id);
},
},
homeassistant: {
instances() {
return this.sites.all
.filter((site) => site.type === 'homeassistant')
.map((site) => {
const state = this.notifications.stateFor(site);
const integration = this.sites.integrationFor(site.id);
return {
id: site.id,
type: 'homeassistant',
label: site.label,
icon: site.icon,
status: 'active',
// Passed through so the edit form can prefill the host, custom
// name, and keep the stored token when the token field is blank.
url: integration?.url,
name: integration?.name,
token: integration?.token,
error: state.error,
errorType: state.errorType,
// Home Assistant uses a long-lived access token, so fixing a
// rejected credential happens through the Settings form.
canReconnect: false,
};
});
},
remove(instance) {
return this.sites.removeSource(instance.id);
},
},
custom: {
instances() {
return this.sites.all
.filter((site) => site.type === 'custom')
.map((site) => {
const integration = this.sites.integrationFor(site.id);
return {
id: site.id,
type: 'custom',
label: site.label,
// The built site's `url` is owned by icon discovery; read the
// stored fields from the raw record instead.
url: integration?.url,
name: integration?.name,
customIcon: integration?.customIcon,
icon: site.icon,
status: 'active',
canReconnect: false,
};
});
},
remove(instance) {
return this.sites.removeSource(instance.id);
},
},
bitcoin: {
instances() {
if (!this.bitcoin.enabled) {
return [];
}
return [
{
id: 'bitcoin',
type: 'bitcoin',
label: 'Bitcoin Price Ticker',
status: 'active',
interval: this.bitcoin.interval,
provider: this.bitcoin.provider,
},
];
},
remove() {
return this.bitcoin.disable();
},
},
openrouter: {
instances() {
if (!this.openrouter.enabled) {
return [];
}
return [
{
id: 'openrouter',
type: 'openrouter',
label: 'OpenRouter Spend',
status: 'active',
apiKey: this.openrouter.apiKey,
interval: this.openrouter.interval,
granularity: this.openrouter.granularity,
},
];
},
remove() {
return this.openrouter.disable();
},
},
'calendar-caldav': {
instances() {
return this.calendar.instancesFor('calendar-caldav').map((instance) => ({
id: instance.id,
type: 'calendar-caldav',
label: instance.label,
url: instance.url,
// Passed through so the edit wizard can prefill the account and its
// calendar selection.
username: instance.username,
password: instance.password,
calendars: instance.calendars,
selectedCalendarIds: instance.selectedCalendarIds,
status: 'active',
canReconnect: false,
}));
},
remove(instance) {
return this.calendar.remove(instance);
},
},
'calendar-ics': {
instances() {
return this.calendar.instancesFor('calendar-ics').map((instance) => ({
id: instance.id,
type: 'calendar-ics',
label: instance.label,
url: instance.url,
status: 'active',
canReconnect: false,
}));
},
remove(instance) {
return this.calendar.remove(instance);
},
},
};
export default class IntegrationsService extends Service {
@service sites;
@service bitcoin;
@service openrouter;
@service calendar;
@service notifications;
// Registry metadata enriched with each type's live instances. Used by the Add
// screen to decide what can still be connected.
get all() {
return INTEGRATIONS.map((integration) => ({
...integration,
instances: this.instancesFor(integration.id),
}));
}
// Flat, per-instance rows for the Manage screen: one row per connected item,
// regardless of whether the integration supports multiple instances. Each row
// carries the registry metadata (label, avatar, actions) alongside its live
// state, so the component can render every row the same way. Integrations
// with settings are sorted to the top; the stable sort keeps registry order
// within each group.
get rows() {
return INTEGRATIONS.flatMap((integration) =>
this.instancesFor(integration.id).map((instance) => ({
...instance,
type: integration.id,
// Every row uses the shared registry label as its title. A per-instance
// custom name (Home Assistant) becomes the byline instead, so the title
// stays the integration name.
label: integration.label,
byline:
instance.name || displayHost(instance.url) || integration.description,
colorClass: integration.colorClass,
iconPath: integration.iconPath,
iconTransform: integration.iconTransform,
iconFill: integration.iconFill,
iconUrl: integration.iconUrl,
iconInset: integration.iconInset,
hasSettings: integration.hasSettings,
reconnectable: integration.reconnectable,
}))
).sort((a, b) => Number(b.hasSettings) - Number(a.hasSettings));
}
integrationFor(type) {
return integrationFor(type);
}
instancesFor(type) {
const adapter = ADAPTERS[type];
return adapter ? adapter.instances.call(this) : [];
}
async remove(instance) {
const adapter = ADAPTERS[instance.type];
if (adapter) {
await adapter.remove.call(this, instance);
}
}
}