mirror of
https://github.com/agessaman/meshcore-bot.git
synced 2026-11-23 04:37:57 +00:00
The date range and Weeks fields now sit behind a "Date range and weeks" toggle right under the timing controls, with a summary of any limits beside it, so the common path is shorter on a phone and a limited schedule still shows its limits when collapsed. The section opens for entries that have limits, and once when Weeks is combined with one day a month so the warning is seen. The "no 5th one" hint only shows for a 5th weekday and names it. In the table, cron fields and dates no longer break at a hyphen, an open-ended range reads "from" or "until", the message column keeps a readable width on phones, and the page header wraps instead of squeezing the button. The monthly warning is only in the Weeks description while it shows, and the toggle reads out the summary.
666 lines
31 KiB
HTML
666 lines
31 KiB
HTML
{% extends "base.html" %}
|
|
|
|
{% block title %}Scheduled Messages - MeshCore Bot{% endblock %}
|
|
|
|
{% block extra_css %}
|
|
<style>
|
|
/* Keep the parse-error text readable when the table scrolls sideways on a phone. */
|
|
.schedule-message-cell { min-width: 16rem; }
|
|
.limit-toggle .fa-chevron-right { transition: transform 0.15s ease-out; }
|
|
.limit-toggle[aria-expanded="true"] .fa-chevron-right { transform: rotate(90deg); }
|
|
@media (prefers-reduced-motion: reduce) {
|
|
.limit-toggle .fa-chevron-right { transition: none; }
|
|
}
|
|
</style>
|
|
{% endblock %}
|
|
|
|
{% block content %}
|
|
<div class="container-fluid">
|
|
<div class="row">
|
|
<div class="col-12">
|
|
<div class="d-flex flex-wrap gap-2 justify-content-between align-items-center mb-4">
|
|
<h1 class="mb-0">
|
|
<i class="fas fa-clock"></i> Scheduled Messages
|
|
</h1>
|
|
<button class="btn btn-primary" id="addScheduleBtn">
|
|
<i class="fas fa-plus"></i> New scheduled message
|
|
</button>
|
|
</div>
|
|
|
|
<div class="alert alert-info" role="alert">
|
|
Each scheduled message is a transmission on a shared medium. Pick the
|
|
longest interval that still does the job. Changes are written to
|
|
<code>config.ini</code> and applied without restarting the bot.
|
|
</div>
|
|
|
|
<div id="statusArea"></div>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>Schedule</th>
|
|
<th>Next run</th>
|
|
<th>Channel</th>
|
|
<th>Message</th>
|
|
<th class="text-end">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="scheduleTableBody">
|
|
<tr><td colspan="5" class="text-muted">Loading…</td></tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Add / edit modal -->
|
|
<div class="modal fade" id="scheduleModal" tabindex="-1" aria-hidden="true" aria-labelledby="scheduleModalTitle">
|
|
<div class="modal-dialog modal-lg">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="scheduleModalTitle">New scheduled message</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label class="form-label" for="scheduleMode">When should it send?</label>
|
|
<select class="form-select" id="scheduleMode">
|
|
<option value="daily">Every day at a time</option>
|
|
<option value="times">Several times a day</option>
|
|
<option value="weekly">Certain days of the week</option>
|
|
<option value="monthly">One day a month</option>
|
|
<option value="interval_hours">Every N hours</option>
|
|
<option value="interval_minutes">Every N minutes</option>
|
|
<option value="advanced">Advanced (cron)</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="row g-3 mb-3">
|
|
<div class="col-md-6" id="fieldTime">
|
|
<label class="form-label" for="scheduleTime">Time</label>
|
|
<input type="time" class="form-control" id="scheduleTime" value="08:00">
|
|
</div>
|
|
<div class="col-md-6 d-none" id="fieldTimes">
|
|
<label class="form-label" for="scheduleTimes">Hours (comma separated)</label>
|
|
<input type="text" class="form-control" id="scheduleTimes" value="6,12,18"
|
|
placeholder="6,12,18">
|
|
<div class="form-text">Whole hours in 24-hour form.</div>
|
|
</div>
|
|
<div class="col-md-6 d-none" id="fieldEveryHours">
|
|
<label class="form-label" for="scheduleEveryHours">Every N hours</label>
|
|
<input type="number" class="form-control" id="scheduleEveryHours"
|
|
min="1" max="23" value="2">
|
|
</div>
|
|
<div class="col-md-6 d-none" id="fieldEveryMinutes">
|
|
<label class="form-label" for="scheduleEveryMinutes">Every N minutes</label>
|
|
<input type="number" class="form-control" id="scheduleEveryMinutes"
|
|
min="1" max="59" value="30">
|
|
</div>
|
|
<div class="col-md-6 d-none" id="fieldMonthly">
|
|
<label class="form-label" for="scheduleOrdinal">Which day</label>
|
|
<div class="input-group">
|
|
<select class="form-select" id="scheduleOrdinal" aria-label="Which one">
|
|
<option value="1st">1st</option>
|
|
<option value="2nd">2nd</option>
|
|
<option value="3rd">3rd</option>
|
|
<option value="4th">4th</option>
|
|
<option value="5th">5th</option>
|
|
<option value="last">Last</option>
|
|
</select>
|
|
<select class="form-select" id="scheduleWeekday" aria-label="Day">
|
|
<option value="mon">Monday</option>
|
|
<option value="tue">Tuesday</option>
|
|
<option value="wed">Wednesday</option>
|
|
<option value="thu">Thursday</option>
|
|
<option value="fri">Friday</option>
|
|
<option value="sat">Saturday</option>
|
|
<option value="sun">Sunday</option>
|
|
<option value="day">day of the month</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-text d-none" id="monthlyFifthHint"></div>
|
|
</div>
|
|
<div class="col-12 d-none" id="fieldDays">
|
|
<label class="form-label">Days</label>
|
|
<div id="dayToggles" class="d-flex flex-wrap gap-2"></div>
|
|
</div>
|
|
<div class="col-12 d-none" id="fieldCron">
|
|
<label class="form-label" for="scheduleCron">Cron expression</label>
|
|
<input type="text" class="form-control font-monospace" id="scheduleCron"
|
|
placeholder="0 6,12,18 * * *">
|
|
<div class="form-text">
|
|
Five fields: minute hour day-of-month month day-of-week.
|
|
Day-of-week is 0=Monday here, not Sunday, so prefer names:
|
|
three-letter months and weekdays (<code>jan</code>, <code>mon</code>)
|
|
work in any field that takes them. Presets like
|
|
<code>@daily</code> and <code>@hourly</code> also work.
|
|
Day-of-month also takes <code>last-fri</code>,
|
|
<code>4th-tue</code> or <code>1st-tue,3rd-tue</code>.
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<button type="button" class="btn btn-link p-0 text-decoration-none limit-toggle"
|
|
id="limitToggle" aria-expanded="false" aria-controls="limitFields"
|
|
aria-describedby="limitSummary">
|
|
<i class="fas fa-chevron-right fa-fw small" aria-hidden="true"></i>
|
|
Date range and weeks
|
|
</button>
|
|
<span class="small text-muted ms-1" id="limitSummary">Not limited</span>
|
|
<div class="d-none border-start ps-3 mt-2" id="limitFields">
|
|
<div class="row g-2 mb-1">
|
|
<div class="col-sm-6">
|
|
<label class="form-label" for="scheduleStart">Start date <span class="text-muted">(optional)</span></label>
|
|
<input type="date" class="form-control" id="scheduleStart">
|
|
</div>
|
|
<div class="col-sm-6">
|
|
<label class="form-label" for="scheduleEnd">End date <span class="text-muted">(optional)</span></label>
|
|
<input type="date" class="form-control" id="scheduleEnd">
|
|
</div>
|
|
</div>
|
|
<div class="form-text mb-3">
|
|
The end date is included in full. Leave both blank to run indefinitely.
|
|
</div>
|
|
|
|
<label class="form-label" for="scheduleWeek">Weeks</label>
|
|
<select class="form-select" id="scheduleWeek" aria-describedby="scheduleWeekHelp">
|
|
<option value="">Every week</option>
|
|
<option value="*/2">Every other week (odd ISO weeks)</option>
|
|
<option value="2-53/2">Every other week (even ISO weeks)</option>
|
|
</select>
|
|
<div class="form-text text-warning d-none" id="weekMonthlyNote">
|
|
With one day a month, Weeks keeps that day only in matching ISO weeks,
|
|
so most months are skipped. It is not "every other month".
|
|
</div>
|
|
<div class="form-text" id="scheduleWeekHelp">
|
|
For a fortnightly net. Weeks are ISO week numbers, so in a 53-week year
|
|
the odd setting runs two weeks in a row at New Year.
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label" for="scheduleChannel">Channel</label>
|
|
<input type="text" class="form-control" id="scheduleChannel" placeholder="Public">
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label" for="scheduleScope">Flood scope <span class="text-muted">(optional)</span></label>
|
|
<input type="text" class="form-control" id="scheduleScope" placeholder="#sea">
|
|
<div class="form-text">Leave blank for global flood.</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label" for="scheduleMessage">Message</label>
|
|
<textarea class="form-control" id="scheduleMessage" rows="3"
|
|
placeholder="Hello from the bot"></textarea>
|
|
<div class="form-text">
|
|
Use <code>\n</code> for a line break. <code>{cmd:wx Seattle}</code> sends a
|
|
command's output; those may not run more often than every 15 minutes.
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card bg-body-tertiary">
|
|
<div class="card-body py-2">
|
|
<div class="small text-muted">Resolved schedule</div>
|
|
<div class="font-monospace" id="previewCron">—</div>
|
|
<div class="small text-muted mt-2">Next runs</div>
|
|
<div id="previewRuns" class="small">—</div>
|
|
<div id="previewError" class="small text-danger mt-2"></div>
|
|
<div id="previewWarning" class="small text-warning mt-2"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
|
<button type="button" class="btn btn-primary" id="saveScheduleBtn">Save</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{% endblock %}
|
|
|
|
{% block extra_js %}
|
|
<script nonce="{{ g.csp_nonce }}">
|
|
(function () {
|
|
'use strict';
|
|
|
|
// APScheduler day-of-week is 0=Monday, which is not the Vixie cron convention.
|
|
const DAYS = [
|
|
{ value: 'mon', label: 'Mon' }, { value: 'tue', label: 'Tue' },
|
|
{ value: 'wed', label: 'Wed' }, { value: 'thu', label: 'Thu' },
|
|
{ value: 'fri', label: 'Fri' }, { value: 'sat', label: 'Sat' },
|
|
{ value: 'sun', label: 'Sun' }
|
|
];
|
|
|
|
let modal = null;
|
|
let editingSchedule = null;
|
|
let previewTimer = null;
|
|
let lastMode = null;
|
|
|
|
const $ = (id) => document.getElementById(id);
|
|
|
|
function showStatus(message, kind) {
|
|
const area = $('statusArea');
|
|
area.innerHTML = '<div class="alert alert-' + kind + ' alert-dismissible fade show">'
|
|
+ escapeHtml(message)
|
|
+ '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
|
|
}
|
|
|
|
function escapeHtml(value) {
|
|
const div = document.createElement('div');
|
|
div.textContent = value === null || value === undefined ? '' : String(value);
|
|
return div.innerHTML;
|
|
}
|
|
|
|
function api(path, options) {
|
|
const opts = Object.assign({ headers: {} }, options || {});
|
|
opts.headers['Content-Type'] = 'application/json';
|
|
opts.headers['X-Requested-With'] = 'XMLHttpRequest';
|
|
return fetch(path, opts).then(async (resp) => {
|
|
const data = await resp.json().catch(() => ({}));
|
|
if (!resp.ok || data.success === false) {
|
|
throw new Error(data.error || ('Request failed (' + resp.status + ')'));
|
|
}
|
|
return data;
|
|
});
|
|
}
|
|
|
|
function buildDayToggles() {
|
|
$('dayToggles').innerHTML = DAYS.map((d) =>
|
|
'<div class="form-check form-check-inline">'
|
|
+ '<input class="form-check-input day-toggle" type="checkbox" value="' + d.value + '" id="day-' + d.value + '">'
|
|
+ '<label class="form-check-label" for="day-' + d.value + '">' + d.label + '</label>'
|
|
+ '</div>'
|
|
).join('');
|
|
document.querySelectorAll('.day-toggle').forEach((el) =>
|
|
el.addEventListener('change', schedulePreview));
|
|
}
|
|
|
|
function selectedDays() {
|
|
return Array.from(document.querySelectorAll('.day-toggle:checked')).map((el) => el.value);
|
|
}
|
|
|
|
/** Turn the friendly controls into a cron string the bot accepts. */
|
|
function composeCron() {
|
|
const mode = $('scheduleMode').value;
|
|
if (mode === 'advanced') return $('scheduleCron').value.trim();
|
|
|
|
if (mode === 'interval_minutes') {
|
|
const n = parseInt($('scheduleEveryMinutes').value, 10);
|
|
return Number.isFinite(n) && n > 0 ? '*/' + n + ' * * * *' : '';
|
|
}
|
|
if (mode === 'interval_hours') {
|
|
const n = parseInt($('scheduleEveryHours').value, 10);
|
|
return Number.isFinite(n) && n > 0 ? '0 */' + n + ' * * *' : '';
|
|
}
|
|
if (mode === 'times') {
|
|
const hours = $('scheduleTimes').value.split(',')
|
|
.map((h) => h.trim()).filter((h) => h !== '').join(',');
|
|
return hours ? '0 ' + hours + ' * * *' : '';
|
|
}
|
|
|
|
const time = $('scheduleTime').value || '08:00';
|
|
const [hh, mm] = time.split(':');
|
|
const minute = String(parseInt(mm, 10) || 0);
|
|
const hour = String(parseInt(hh, 10) || 0);
|
|
if (mode === 'weekly') {
|
|
const days = selectedDays();
|
|
return days.length ? minute + ' ' + hour + ' * * ' + days.join(',') : '';
|
|
}
|
|
if (mode === 'monthly') {
|
|
const ordinal = $('scheduleOrdinal').value;
|
|
const weekday = $('scheduleWeekday').value;
|
|
let day;
|
|
if (weekday === 'day') {
|
|
day = ordinal === 'last' ? 'last' : String(parseInt(ordinal, 10));
|
|
} else {
|
|
day = ordinal + '-' + weekday;
|
|
}
|
|
return minute + ' ' + hour + ' ' + day + ' * *';
|
|
}
|
|
return minute + ' ' + hour + ' * * *';
|
|
}
|
|
|
|
const WEEKDAY_NAMES = {
|
|
mon: 'Monday', tue: 'Tuesday', wed: 'Wednesday', thu: 'Thursday',
|
|
fri: 'Friday', sat: 'Saturday', sun: 'Sunday'
|
|
};
|
|
|
|
function weekLabel(week) {
|
|
return { '*/2': 'every other week (odd)', '2-53/2': 'every other week (even)' }[week]
|
|
|| 'ISO weeks ' + week;
|
|
}
|
|
|
|
/** Date bounds as HTML, each date kept whole so it never breaks at a hyphen. */
|
|
function boundsHtml(start, end) {
|
|
const date = (d) => '<span class="text-nowrap">' + escapeHtml(d) + '</span>';
|
|
if (start && end) return date(start) + ' → ' + date(end);
|
|
if (start) return 'from ' + date(start);
|
|
return 'until ' + date(end);
|
|
}
|
|
|
|
function setLimitsOpen(open) {
|
|
$('limitToggle').setAttribute('aria-expanded', open ? 'true' : 'false');
|
|
$('limitFields').classList.toggle('d-none', !open);
|
|
}
|
|
|
|
function syncLimitSummary() {
|
|
const start = $('scheduleStart').value;
|
|
const end = $('scheduleEnd').value;
|
|
const week = $('scheduleWeek').value;
|
|
const parts = [];
|
|
if (start || end) parts.push(boundsHtml(start, end));
|
|
if (week) parts.push(escapeHtml(weekLabel(week)));
|
|
$('limitSummary').innerHTML = parts.length ? parts.join(', ') : 'Not limited';
|
|
}
|
|
|
|
function syncModeFields() {
|
|
const mode = $('scheduleMode').value;
|
|
const show = (id, on) => $(id).classList.toggle('d-none', !on);
|
|
show('fieldTime', mode === 'daily' || mode === 'weekly' || mode === 'monthly');
|
|
show('fieldMonthly', mode === 'monthly');
|
|
const weekday = $('scheduleWeekday').value;
|
|
const fifthWeekday = mode === 'monthly' && $('scheduleOrdinal').value === '5th' && weekday !== 'day';
|
|
$('monthlyFifthHint').textContent = fifthWeekday
|
|
? 'Months with no 5th ' + WEEKDAY_NAMES[weekday] + ' are skipped.' : '';
|
|
show('monthlyFifthHint', fifthWeekday);
|
|
const weekMonthly = mode === 'monthly' && $('scheduleWeek').value !== '';
|
|
show('weekMonthlyNote', weekMonthly);
|
|
$('scheduleWeek').setAttribute('aria-describedby',
|
|
weekMonthly ? 'weekMonthlyNote scheduleWeekHelp' : 'scheduleWeekHelp');
|
|
// Weeks set earlier, then a switch to one day a month: open the section once so the
|
|
// warning is seen, without reopening it every time after the user collapses it.
|
|
if (weekMonthly && lastMode !== 'monthly') setLimitsOpen(true);
|
|
lastMode = mode;
|
|
syncLimitSummary();
|
|
show('fieldTimes', mode === 'times');
|
|
show('fieldEveryHours', mode === 'interval_hours');
|
|
show('fieldEveryMinutes', mode === 'interval_minutes');
|
|
show('fieldDays', mode === 'weekly');
|
|
show('fieldCron', mode === 'advanced');
|
|
schedulePreview();
|
|
}
|
|
|
|
function schedulePreview() {
|
|
clearTimeout(previewTimer);
|
|
previewTimer = setTimeout(runPreview, 250);
|
|
}
|
|
|
|
async function runPreview() {
|
|
const cron = composeCron();
|
|
$('previewCron').textContent = cron || '—';
|
|
$('previewError').textContent = '';
|
|
$('previewWarning').textContent = '';
|
|
if (!cron) {
|
|
$('previewRuns').textContent = '—';
|
|
return;
|
|
}
|
|
try {
|
|
const data = await api('/api/scheduled-messages/preview', {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
schedule: cron,
|
|
message: $('scheduleMessage').value,
|
|
start: $('scheduleStart').value,
|
|
end: $('scheduleEnd').value,
|
|
week: $('scheduleWeek').value,
|
|
channel: $('scheduleChannel').value,
|
|
scope: $('scheduleScope').value,
|
|
count: 5
|
|
})
|
|
});
|
|
if (data.valid) {
|
|
$('previewRuns').innerHTML = (data.next_runs || [])
|
|
.map((iso) => escapeHtml(new Date(iso).toLocaleString())).join('<br>') || '—';
|
|
} else {
|
|
$('previewRuns').textContent = '—';
|
|
$('previewError').textContent = data.error || 'Invalid schedule';
|
|
}
|
|
if (data.entry_error) $('previewError').textContent = data.entry_error;
|
|
if (data.warning) $('previewWarning').textContent = data.warning;
|
|
} catch (err) {
|
|
$('previewRuns').textContent = '—';
|
|
$('previewError').textContent = err.message;
|
|
}
|
|
}
|
|
|
|
function openModal(entry) {
|
|
editingSchedule = entry ? entry.schedule : null;
|
|
$('scheduleModalTitle').textContent = entry ? 'Edit scheduled message' : 'New scheduled message';
|
|
$('scheduleChannel').value = entry ? entry.channel : '';
|
|
$('scheduleScope').value = entry && entry.scope ? entry.scope : '';
|
|
$('scheduleMessage').value = entry ? entry.message : '';
|
|
$('scheduleStart').value = entry && entry.start ? entry.start : '';
|
|
$('scheduleEnd').value = entry && entry.end ? entry.end : '';
|
|
resetBuilder();
|
|
setWeek(entry && entry.week ? entry.week : '');
|
|
$('scheduleCron').value = entry ? entry.schedule : '';
|
|
// An existing entry opens in the friendly mode that reproduces it exactly, or in
|
|
// Advanced, so the stored cron is never silently rewritten.
|
|
$('scheduleMode').value = entry ? detectMode(entry.schedule) : 'daily';
|
|
setLimitsOpen(Boolean(entry && (entry.start || entry.end || entry.week)));
|
|
lastMode = null;
|
|
syncModeFields();
|
|
modal.show();
|
|
}
|
|
|
|
function setWeek(week) {
|
|
const select = $('scheduleWeek');
|
|
select.querySelectorAll('option[data-custom]').forEach((o) => o.remove());
|
|
if (week && !Array.from(select.options).some((o) => o.value === week)) {
|
|
// Keep a hand-written week= expression rather than dropping it on save.
|
|
const opt = document.createElement('option');
|
|
opt.value = week;
|
|
opt.textContent = 'Weeks ' + week + ' (from config)';
|
|
opt.dataset.custom = '1';
|
|
select.appendChild(opt);
|
|
}
|
|
select.value = week || '';
|
|
}
|
|
|
|
/** Put every builder control back to its default, so nothing carries over between opens. */
|
|
function resetBuilder() {
|
|
document.querySelectorAll('.day-toggle').forEach((el) => { el.checked = false; });
|
|
$('scheduleOrdinal').value = '1st';
|
|
$('scheduleWeekday').value = 'mon';
|
|
$('scheduleTime').value = '08:00';
|
|
$('scheduleTimes').value = '6,12,18';
|
|
$('scheduleEveryHours').value = '2';
|
|
$('scheduleEveryMinutes').value = '30';
|
|
}
|
|
|
|
/** Pick the builder mode whose controls reproduce `schedule` exactly, else Advanced. */
|
|
function detectMode(schedule) {
|
|
const raw = schedule || '';
|
|
let fixed;
|
|
if ((fixed = /^\*\/(\d{1,2}) \* \* \* \*$/.exec(raw))) {
|
|
$('scheduleEveryMinutes').value = fixed[1];
|
|
$('scheduleMode').value = 'interval_minutes';
|
|
return composeCron() === raw ? 'interval_minutes' : 'advanced';
|
|
}
|
|
if ((fixed = /^0 \*\/(\d{1,2}) \* \* \*$/.exec(raw))) {
|
|
$('scheduleEveryHours').value = fixed[1];
|
|
$('scheduleMode').value = 'interval_hours';
|
|
return composeCron() === raw ? 'interval_hours' : 'advanced';
|
|
}
|
|
if ((fixed = /^0 (\d{1,2}(?:,\d{1,2})+) \* \* \*$/.exec(raw))) {
|
|
$('scheduleTimes').value = fixed[1];
|
|
$('scheduleMode').value = 'times';
|
|
return composeCron() === raw ? 'times' : 'advanced';
|
|
}
|
|
const m = /^(\d{1,2}) (\d{1,2}) (\S+) \* (\S+)$/.exec(schedule || '');
|
|
if (!m) return 'advanced';
|
|
const [, minute, hour, dom, dow] = m;
|
|
$('scheduleTime').value = hour.padStart(2, '0') + ':' + minute.padStart(2, '0');
|
|
let mode = null;
|
|
if (dom === '*' && dow === '*') {
|
|
mode = 'daily';
|
|
} else if (dom === '*') {
|
|
const days = dow.split(',');
|
|
document.querySelectorAll('.day-toggle').forEach((el) => {
|
|
el.checked = days.includes(el.value);
|
|
});
|
|
mode = 'weekly';
|
|
} else if (dow === '*') {
|
|
const pos = /^(1st|2nd|3rd|4th|5th|last)-(mon|tue|wed|thu|fri|sat|sun)$/.exec(dom);
|
|
const nth = { '1': '1st', '2': '2nd', '3': '3rd', '4': '4th', '5': '5th' }[dom];
|
|
if (pos) {
|
|
$('scheduleOrdinal').value = pos[1];
|
|
$('scheduleWeekday').value = pos[2];
|
|
mode = 'monthly';
|
|
} else if (dom === 'last' || nth) {
|
|
$('scheduleOrdinal').value = dom === 'last' ? 'last' : nth;
|
|
$('scheduleWeekday').value = 'day';
|
|
mode = 'monthly';
|
|
}
|
|
}
|
|
if (!mode) return 'advanced';
|
|
$('scheduleMode').value = mode;
|
|
return composeCron() === schedule ? mode : 'advanced';
|
|
}
|
|
|
|
async function save() {
|
|
const cron = composeCron();
|
|
const payload = {
|
|
schedule: cron,
|
|
channel: $('scheduleChannel').value,
|
|
scope: $('scheduleScope').value,
|
|
message: $('scheduleMessage').value,
|
|
start: $('scheduleStart').value,
|
|
end: $('scheduleEnd').value,
|
|
week: $('scheduleWeek').value
|
|
};
|
|
const editing = editingSchedule !== null;
|
|
if (editing) payload.original_schedule = editingSchedule;
|
|
|
|
const btn = $('saveScheduleBtn');
|
|
btn.disabled = true;
|
|
try {
|
|
const result = await api('/api/scheduled-messages', {
|
|
method: editing ? 'PUT' : 'POST',
|
|
body: JSON.stringify(payload)
|
|
});
|
|
modal.hide();
|
|
// reload_queued false means the file changed but the running bot did not.
|
|
showStatus(
|
|
result.message || 'Saved.',
|
|
result.reload_queued === false ? 'warning' : 'success'
|
|
);
|
|
load();
|
|
} catch (err) {
|
|
showStatus(err.message, 'danger');
|
|
} finally {
|
|
btn.disabled = false;
|
|
}
|
|
}
|
|
|
|
async function remove(entry) {
|
|
if (!confirm('Delete this scheduled message?\n\n'
|
|
+ entry.schedule + '\n' + entry.channel + ': ' + entry.message)) return;
|
|
try {
|
|
const result = await api('/api/scheduled-messages', {
|
|
method: 'DELETE',
|
|
body: JSON.stringify({ schedule: entry.schedule })
|
|
});
|
|
showStatus(
|
|
result.message || 'Deleted.',
|
|
result.reload_queued === false ? 'warning' : 'success'
|
|
);
|
|
load();
|
|
} catch (err) {
|
|
showStatus(err.message, 'danger');
|
|
}
|
|
}
|
|
|
|
function renderRow(entry) {
|
|
const tr = document.createElement('tr');
|
|
|
|
const nextRun = (entry.next_runs && entry.next_runs.length)
|
|
? new Date(entry.next_runs[0]).toLocaleString()
|
|
: '—';
|
|
|
|
// Wrap between cron fields only, never inside one at a hyphen (2nd-wed, 1st-tue,3rd-tue).
|
|
let scheduleCell = '<span class="font-monospace">' + String(entry.schedule).split(' ')
|
|
.map((field) => '<span class="text-nowrap">' + escapeHtml(field) + '</span>').join(' ')
|
|
+ '</span>';
|
|
if (entry.error) {
|
|
scheduleCell += '<br><span class="badge bg-danger">Not scheduled</span>';
|
|
} else if (entry.finished) {
|
|
scheduleCell += '<br><span class="badge bg-secondary">Finished</span>';
|
|
} else if (entry.deprecated) {
|
|
scheduleCell += '<br><span class="badge bg-warning text-dark">Deprecated format</span>';
|
|
}
|
|
if (entry.start || entry.end) {
|
|
scheduleCell += '<br><span class="small text-muted">' + boundsHtml(entry.start, entry.end) + '</span>';
|
|
}
|
|
if (entry.week) {
|
|
scheduleCell += '<br><span class="small text-muted">' + escapeHtml(weekLabel(entry.week)) + '</span>';
|
|
}
|
|
|
|
const scopeBadge = entry.scope
|
|
? ' <span class="badge bg-secondary">' + escapeHtml(entry.scope) + '</span>' : '';
|
|
|
|
tr.innerHTML =
|
|
'<td>' + scheduleCell + '</td>'
|
|
+ '<td>' + escapeHtml(nextRun) + '</td>'
|
|
+ '<td>' + escapeHtml(entry.channel) + scopeBadge + '</td>'
|
|
+ '<td class="text-break schedule-message-cell">' + escapeHtml(entry.message)
|
|
+ (entry.error ? '<br><span class="small text-danger">' + escapeHtml(entry.error) + '</span>' : '')
|
|
+ '</td>'
|
|
+ '<td class="text-end text-nowrap">'
|
|
+ '<button class="btn btn-sm btn-outline-secondary me-1 edit-btn">Edit</button>'
|
|
+ '<button class="btn btn-sm btn-outline-danger delete-btn">Delete</button>'
|
|
+ '</td>';
|
|
|
|
tr.querySelector('.edit-btn').addEventListener('click', () => openModal(entry));
|
|
tr.querySelector('.delete-btn').addEventListener('click', () => remove(entry));
|
|
return tr;
|
|
}
|
|
|
|
async function load() {
|
|
const body = $('scheduleTableBody');
|
|
try {
|
|
const data = await api('/api/scheduled-messages');
|
|
body.innerHTML = '';
|
|
if (!data.entries || !data.entries.length) {
|
|
body.innerHTML = '<tr><td colspan="5" class="text-muted">'
|
|
+ 'No scheduled messages yet.</td></tr>';
|
|
return;
|
|
}
|
|
data.entries.forEach((entry) => body.appendChild(renderRow(entry)));
|
|
} catch (err) {
|
|
body.innerHTML = '<tr><td colspan="5" class="text-danger">'
|
|
+ escapeHtml(err.message) + '</td></tr>';
|
|
}
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
modal = new bootstrap.Modal($('scheduleModal'));
|
|
buildDayToggles();
|
|
$('addScheduleBtn').addEventListener('click', () => openModal(null));
|
|
$('saveScheduleBtn').addEventListener('click', save);
|
|
$('scheduleMode').addEventListener('change', syncModeFields);
|
|
['scheduleWeek', 'scheduleOrdinal', 'scheduleWeekday', 'scheduleStart', 'scheduleEnd'].forEach((id) =>
|
|
$(id).addEventListener('change', syncModeFields));
|
|
$('limitToggle').addEventListener('click', () =>
|
|
setLimitsOpen($('limitToggle').getAttribute('aria-expanded') !== 'true'));
|
|
['scheduleTime', 'scheduleTimes', 'scheduleEveryHours', 'scheduleEveryMinutes',
|
|
'scheduleCron', 'scheduleMessage', 'scheduleStart', 'scheduleEnd',
|
|
'scheduleOrdinal', 'scheduleWeekday', 'scheduleWeek', 'scheduleChannel',
|
|
'scheduleScope'].forEach((id) =>
|
|
$(id).addEventListener('input', schedulePreview));
|
|
syncModeFields();
|
|
load();
|
|
});
|
|
})();
|
|
</script>
|
|
{% endblock %}
|