LuminaWebsite/assets/js/dues-remind.js
Maaxxs f02b980a97 Legion dues: the bot reminds open members by DM; premades open in the leadership's order
Asked for: "bei den Legionsbeiträgen soll der bot auch die leute drauf erinnern dass ihr beitrag fällig ist ...
die nachricht ist frei definierbar über die website (mit markierungen und erwähnungen) ... eine standardnachricht,
welche zur Legion passt" and "es soll wenn man die seite aufruft standardmäßig die von uns definierte reihenfolge
angezeigt werden".

Dues reminders (includes/dues_reminders.php, admin/dues.php "Erinnerungen per Discord"):
- Who: everyone still open in the running cycle with a linked Discord - not who paid, is exempt or sits in a
  suspended cycle. Automatic (switch, off by default) at the start of the cycle and/or N days before its end, at
  a set hour (settings dues_remind_*); a missed moment is caught up for 24 hours at most, so switching it on in the
  middle of a cycle sends nothing. "Jetzt erinnern" for everyone open right now and "Testnachricht an mich" with
  the text from the form, saved or not.
- The text is free: Discord formatting, placeholders {name}, {erwähnung}, {betrag}, {zyklus}, {bis}, mentions of
  members and channels (channel list from the bot API), toolbar and live preview with the own values
  (assets/js/dues-remind.js). The default text greets with "Arieluma" and is stored empty so improvements arrive.
- Flow: the bot (new cogs/dues_reminders.py, already deployed) asks bot_api.php?action=dues_reminders every minute;
  the website queues due reminders in dues_reminders (at most once per member, cycle and kind) and hands out the
  waiting ones with the finished text, the bot sends them without pings and reports back via
  action=dues_reminders_done, then logs to the log channel. The text is dropped after sending, the log stays 90
  days. The section lists the runs of the running cycle with delivered and not deliverable (with reason).
- Schema version 64 (table also in sql/schema.sql); privacy policy (what is stored, the bot, retention), data export
  section, account deletion, data reset, audit log; patch notes and README.

Premades:
- The page opens in the order the leadership set: own premades are no longer pulled to the top. A chosen sort only
  lives in the address (?sort=) - it survives a reload, not a new visit; the former browser entry
  lumina_premades_sort is removed (and left the privacy policy). Patch notes of the sort and order entries adjusted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 01:32:43 +02:00

100 lines
4.6 KiB
JavaScript

/**
* Erinnerungen an die Legionsbeitraege (admin/dues.php, Bereich "Erinnerungen per Discord"): Werkzeugleiste ueber dem
* Text (Formatierung um die Auswahl legen, Platzhalter und Erwaehnungen einfuegen), Zeichenzaehler, "Standardtext" und
* eine Live-Vorschau, wie die PN im Discord aussieht - mit den eigenen Werten fuer die Platzhalter.
*/
(function () {
'use strict';
var textarea = document.querySelector('[data-remind-message]');
var preview = document.querySelector('[data-remind-preview]');
if (!textarea || !preview) return;
var counter = document.querySelector('[data-remind-count]');
var sample = JSON.parse(preview.getAttribute('data-sample') || '{}');
var users = JSON.parse(preview.getAttribute('data-users') || '{}');
var channels = JSON.parse(preview.getAttribute('data-channels') || '{}');
var max = parseInt(textarea.getAttribute('maxlength'), 10) || 1500;
function escapeHtml(text) {
var div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Discord-Formatierung nachbilden: Platzhalter einsetzen (wie die Website), dann Erwaehnungen, `Code`, **fett**,
// __unterstrichen__, ~~durchgestrichen~~, *kursiv* bzw. _kursiv_ und Zitate ("> " am Zeilenanfang)
function render(text) {
Object.keys(sample).forEach(function (key) { text = text.split(key).join(sample[key]); });
return escapeHtml(text)
.replace(/&lt;@!?(\d+)&gt;/g, function (all, id) {
return '<span class="dues-remind-pill">@' + escapeHtml(users[id] || 'unbekannt') + '</span>';
})
.replace(/&lt;#(\d+)&gt;/g, function (all, id) {
return '<span class="dues-remind-pill">#' + escapeHtml(channels[id] || 'unbekannter-kanal') + '</span>';
})
.replace(/`([^`\n]+)`/g, '<code>$1</code>')
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
.replace(/__(.+?)__/g, '<u>$1</u>')
.replace(/~~(.+?)~~/g, '<s>$1</s>')
.replace(/(^|[^*])\*([^*\n]+)\*/g, '$1<em>$2</em>')
.replace(/(^|[^\w])_([^_\n]+)_(?!\w)/g, '$1<em>$2</em>')
.split('\n')
.map(function (line) {
return line.indexOf('&gt; ') === 0 ? '<span class="dues-remind-quote">' + line.slice(5) + '</span>' : line;
})
.join('<br>');
}
function update() {
preview.innerHTML = render(textarea.value);
if (counter) counter.textContent = textarea.value.length + ' / ' + max + ' Zeichen';
}
// Um die Auswahl legen (Formatierung) bzw. an ihrer Stelle einfuegen (Platzhalter, Erwaehnungen)
function edit(before, after, replace) {
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var selected = replace ? '' : textarea.value.slice(start, end);
textarea.value = textarea.value.slice(0, start) + before + selected + after + textarea.value.slice(end);
textarea.focus();
// Eingefuegtes: Cursor dahinter - Formatierung: die Auswahl bleibt markiert (ohne Auswahl steht er dazwischen)
var stop = start + before.length + selected.length;
textarea.setSelectionRange(replace ? stop : start + before.length, stop);
update();
}
document.querySelectorAll('[data-wrap]').forEach(function (button) {
button.addEventListener('click', function () {
edit(button.getAttribute('data-wrap'), button.getAttribute('data-wrap'), false);
});
});
document.querySelectorAll('[data-insert]').forEach(function (button) {
button.addEventListener('click', function () { edit(button.getAttribute('data-insert'), '', true); });
});
document.querySelectorAll('[data-insert-select]').forEach(function (select) {
select.addEventListener('change', function () {
if (!select.value) return;
edit(select.value, '', true);
select.value = '';
});
});
var reset = document.querySelector('[data-remind-default]');
if (reset) {
reset.addEventListener('click', function () {
var fallback = textarea.getAttribute('data-default') || '';
if (textarea.value === fallback) return;
window.luminaDialog.confirm('Dein bisheriger Text wird durch den Standardtext ersetzt. Gespeichert ist erst, wenn du speicherst.', {
title: 'Standardtext übernehmen?', okLabel: 'Übernehmen'
}).then(function (ok) {
if (!ok) return;
textarea.value = fallback;
update();
});
});
}
textarea.addEventListener('input', update);
update();
})();