Add a 'days with events' preference (default 3, range 1-7) that drives the upcoming events widget, and extend the fetch window to 8 days so the largest window can populate. Add a Calendar settings pane (moved under Integrations) that lists the connected CalDAV/ICS sources in a two-column grid with add/edit/remove, and remove calendars from Manage Integrations. Turn the modal service into a stack so add/edit forms open on top of the settings dialog, with one dialog per level, per-entry title ids, and a softened stacked backdrop. Move CALENDAR_ACTOR into its own config module to break the calendar/caldav/ics import cycle.
232 lines
6.9 KiB
Plaintext
232 lines
6.9 KiB
Plaintext
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 {
|
|
GITEA_ACCEPT,
|
|
applicationsUrl,
|
|
authorizationHeader,
|
|
integrationKey,
|
|
normalizeGiteaUrl,
|
|
notificationsCountUrl,
|
|
} from '../../../config/gitea';
|
|
|
|
export default class SettingsIntegrationsGiteaForm extends Component {
|
|
@service http;
|
|
@service notifications;
|
|
@service sites;
|
|
@service storage;
|
|
|
|
@tracked urlDraft = null;
|
|
@tracked token = '';
|
|
@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 ?? '';
|
|
}
|
|
|
|
get applicationsUrl() {
|
|
return applicationsUrl(this.url);
|
|
}
|
|
|
|
get title() {
|
|
return this.isEdit ? 'Edit Gitea' : 'Add Gitea Integration';
|
|
}
|
|
|
|
get submitLabel() {
|
|
return this.isEdit ? 'Save' : 'Connect';
|
|
}
|
|
|
|
@action
|
|
updateUrl(event) {
|
|
this.urlDraft = event.target.value;
|
|
this.error = null;
|
|
}
|
|
|
|
@action
|
|
updateToken(event) {
|
|
this.token = event.target.value;
|
|
this.error = null;
|
|
}
|
|
|
|
@action
|
|
async handleSubmit(event) {
|
|
event.preventDefault();
|
|
this.error = null;
|
|
|
|
const instanceUrl = normalizeGiteaUrl(this.url);
|
|
|
|
if (!instanceUrl) {
|
|
this.error = 'Please enter a valid Gitea URL';
|
|
return;
|
|
}
|
|
|
|
// On edit a blank token keeps the stored one, so users can update the host
|
|
// without re-entering the token.
|
|
const token = this.token.trim() || this.integration?.token;
|
|
|
|
if (!token) {
|
|
this.error = 'Please enter an access token';
|
|
return;
|
|
}
|
|
|
|
this.submitting = true;
|
|
|
|
try {
|
|
// The unread count endpoint doubles as a token and scope check: an
|
|
// invalid token fails with 401, a token without the read:notification
|
|
// permission fails with 403.
|
|
await this.http.request(notificationsCountUrl(instanceUrl), {
|
|
headers: {
|
|
Authorization: authorizationHeader(token),
|
|
Accept: GITEA_ACCEPT,
|
|
},
|
|
});
|
|
} catch (err) {
|
|
this.error = this.classifyError(err);
|
|
return;
|
|
} finally {
|
|
this.submitting = false;
|
|
}
|
|
|
|
// Key each instance by host so several Gitea instances can be connected.
|
|
// Editing keeps the existing key even if the host changes.
|
|
const key = this.integration?.id ?? integrationKey(instanceUrl);
|
|
|
|
// 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', key, {
|
|
type: 'gitea',
|
|
status: 'active',
|
|
url: instanceUrl,
|
|
token,
|
|
createdAt: this.integration?.createdAt ?? Date.now(),
|
|
});
|
|
|
|
await this.sites.reload();
|
|
this.notifications.startPolling();
|
|
this.args.close?.();
|
|
}
|
|
|
|
classifyError(err) {
|
|
if (err?.status === 401) {
|
|
return 'Invalid access token';
|
|
}
|
|
if (err?.status === 403) {
|
|
return 'Token is missing the read:notification scope';
|
|
}
|
|
if (err?.name === 'NetworkError') {
|
|
return 'Unable to reach Gitea. Check the URL and CORS.';
|
|
}
|
|
return 'Unable to verify your Gitea token';
|
|
}
|
|
|
|
<template>
|
|
<div class="p-6">
|
|
<h3
|
|
id={{if @titleId @titleId "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 a Gitea instance to see your unread notification count on the
|
|
dashboard.
|
|
</p>
|
|
</div>
|
|
|
|
<form {{on "submit" this.handleSubmit}} class="mt-5">
|
|
<div class="space-y-5">
|
|
<div>
|
|
<label
|
|
for="gitea-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="gitea-url"
|
|
name="url"
|
|
value={{this.url}}
|
|
{{on "input" this.updateUrl}}
|
|
placeholder="gitea.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>
|
|
<p class="mt-2 text-sm text-gray-500">
|
|
The URL of your Gitea or Forgejo instance.
|
|
</p>
|
|
</div>
|
|
|
|
<div>
|
|
<label
|
|
for="gitea-token"
|
|
class="block text-sm font-medium leading-6 text-gray-900"
|
|
>
|
|
Access token
|
|
</label>
|
|
<div class="mt-2">
|
|
<input
|
|
type="password"
|
|
id="gitea-token"
|
|
name="token"
|
|
autocomplete="off"
|
|
value={{this.token}}
|
|
{{on "input" this.updateToken}}
|
|
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>
|
|
<p class="mt-2 text-sm text-gray-500">
|
|
Create a token with the "read:notification" scope in
|
|
{{#if this.applicationsUrl}}
|
|
<a
|
|
href={{this.applicationsUrl}}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
class="text-indigo-600 hover:text-indigo-500"
|
|
>your instance's applications settings</a>.
|
|
{{else}}
|
|
your instance's applications settings.
|
|
{{/if}}
|
|
</p>
|
|
</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>
|
|
}
|