Skip to content

Commit

Permalink
Merge events on the back end instead of the front end, and show the r…
Browse files Browse the repository at this point in the history
…equired change (if any)
  • Loading branch information
Rosuav committed Jan 17, 2025
1 parent 0002f07 commit aecf81e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
36 changes: 6 additions & 30 deletions httpstatic/chan_calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,6 @@ import {lindt, replace_content, DOM, on} from "https://rosuav.github.io/choc/fac
const {A, BUTTON, H2, IMG, LI, P, SPAN, TABLE, TBODY, TD, TH, THEAD, TIME, TR, UL} = lindt; //autoimport
import {simpleconfirm} from "$$static||utils.js$$";

//Given two sorted lists of events, return an array of pairs. If two events have the same
//time_t, return [time_t, ev1, ev2]; otherwise return [time_t, ev1, null] or [time_t, null, ev2]
//as required. The resultant array will be in time_t order.
//This is largely a merge operation, but with matching timestamps merging the elements instead
//of zipping them together.
function pair_events(arr1, arr2) {
arr1 = arr1 || []; arr2 = arr2 || [];
const ret = [];
let d1 = 0, d2 = 0, s1 = arr1.length, s2 = arr2.length;
while (d1 < s1 && d2 < s2) {
const t1 = arr1[d1].time_t, t2 = arr2[d2].time_t;
if (t1 === t2)
ret.push([t1, arr1[d1++], arr2[d2++]]);
else if (t1 < t2)
ret.push([t1, arr1[d1++], null]);
else
ret.push([t2, arr2[d2++], null]);
}
//And collect any residue. One of these loops won't have any content to process.
for (let i = d1; i < s1; ++i)
ret.push([arr1[i].time_t, arr1[i], null]);
for (let i = d2; i < s2; ++i)
ret.push([arr2[i].time_t, null, arr2[i]]);
return ret;
}

function ordinal(n) {
switch (n) {
case 1: return "1st";
Expand Down Expand Up @@ -84,13 +58,15 @@ export function render(data) {
TABLE([
THEAD(TR([
TH("Date/time"),
TH("Status"),
TH(data.synchronized_calendar || "Google Calendar"),
TH("Twitch schedule"),
])),
TBODY(pair_events(data.sync?.events, data.sync?.segments).map(([ts, ev, seg]) => TR([
TD(RELATIVETIME(ts)),
TD(ev ? A({href: ev.htmlLink}, ev.summary) : "-"),
TD(seg ? seg.title : "-"),
TBODY((data.sync.paired_events || []).map(ev => TR([
TD(RELATIVETIME(ev.time_t)),
TD(ev.action),
TD(ev.google ? A({href: ev.google.htmlLink}, ev.google.summary) : "-"),
TD(ev.twitch ? ev.twitch.title : "-"),
]))),
]),
P([
Expand Down
19 changes: 18 additions & 1 deletion modules/http/chan_calendar.pike
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ __async__ void synchronize(int userid) {
"variables": timespan | (["singleEvents": "true", "orderBy": "startTime"]),
])));
array events = singles->items || ({ });
mapping timeslots = ([]);
foreach (events, mapping ev) {
string|zero rr = recurrence_rule[ev->recurringEventId];
//Note that we assume that an event starts and ends in the same timezone (eg Australia/Melbourne).
Expand All @@ -184,20 +185,36 @@ __async__ void synchronize(int userid) {
mapping tw = m_delete(existing_schedule, start);
//For now, assume that once we've seen one event from a recurring set, we've seen 'em all.
//TODO: Handle single-instance deletion or moving of an event.
string action = "OK"; //No action needed
if (!tw) action = "New";
//TODO: See if the event fully matches; if it doesn't, action = "Update"
timeslots[start] = ([
"action": action,
"time_t": start,
"twitch": tw,
"google": ev,
]);
if (rr == "*Done*") continue;
werror("%s EVENT %O->%O %O %O %s\n", tw ? "EXISTING" : "NEW", ev->start->dateTime, ev->end->dateTime, ev->start->timeZone, rr, ev->summary);
//TODO: If "Category: ...." is in ev->description, set the category_id for the Twitch event
if (rr) recurrence_rule[ev->recurringEventId] = "*Done*";
}
if (sizeof(existing_schedule)) werror("Delete me: %O\n", existing_schedule);
foreach (existing_schedule; int start; mapping tw) timeslots[start] = ([
"action": "Delete",
"time_t": start,
"twitch": tw,
"google": 0,
]);
//Guarantee that events are sorted by timestamp
sort(events->time_t, events);
sort(twitch->time_t, twitch);
array paired_events = values(timeslots); sort(indices(timeslots), paired_events);
synchronization_cache[userid] = ([
"expires": time() + 3600,
"synctime": ctime(time()), //TODO format on front end, can't be bothered now
"events": events,
"segments": twitch,
"paired_events": paired_events,
]);
send_updates_all("#" + userid);
}
Expand Down

0 comments on commit aecf81e

Please sign in to comment.