mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 04:49:41 -05:00
#5251 asked for the last change date of a list, to build an offline Android client that syncs only what moved. Neither half existed: GET /api/boards/:boardId/lists returned {_id, title} and no dates at all, and while the single-list endpoint returns the whole document - modifiedAt included - that date answers the wrong question. It moves when the LIST document changes (title, sort, archived); a card being added, edited or archived does not touch it, and those are the changes the client is asking about. The list endpoint now reports both: modifiedAt - the list document itself cardsModifiedAt - the newest change among the cards in it, null when none One query for the board's cards and a reduction in memory, not a query per list - a query per list is what made this expensive enough to ask about. Archived cards are included on purpose: archiving is one of the changes named in the issue. The reduction is models/lib/listActivityDates.js, a pure helper, so it is testable without Meteor and so the handler stays readable. It reads dateLastActivity as well as modifiedAt and takes whichever is newer, because boards written by older WeKan carry only the former; a card with no usable date is skipped rather than counted as now or as zero; and a list whose cards carry no date is ABSENT from the result, so the endpoint reports null rather than an invented time - a client polling on a wrong date either never syncs or syncs forever. tests/listActivityDates.test.cjs exercises the helper directly: newest wins, per list, legacy field, string and numeric dates, an undated card cannot drag the answer backwards, and a negative case feeding it nulls, strings, numbers, a card with no listId and an Invalid Date - none of which may throw, since the handler passes whatever the database holds. It also pins the endpoint's shape: both fields, one cards query, only the three fields it reduces over, and no `archived: false` on that query. Thanks to mimZD and xet7 ! Fixes #5251,
61 lines
2.4 KiB
JavaScript
61 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
// #5251: "get last change date of a list using API".
|
|
//
|
|
// The asker is building an offline Android client and wants to know whether
|
|
// anything in a list changed - a card added, edited or archived - without
|
|
// fetching every card and diffing it. A list's own `modifiedAt` does not answer
|
|
// that: it moves when the LIST document changes (title, sort, archived), and a
|
|
// card being edited does not touch it.
|
|
//
|
|
// So the answer is two dates per list: the list's own modifiedAt, and the newest
|
|
// modifiedAt among its cards. This is the second one, as a pure reduction over
|
|
// the cards of a board, so the REST handler needs one query for the whole board
|
|
// instead of one per list - and so it is unit-testable without Meteor.
|
|
//
|
|
// Archived cards are the caller's choice: archiving IS a change the asker names,
|
|
// so the handler passes them in. What matters here is that a card with no usable
|
|
// date cannot poison the result, and that a list with no cards gets no date
|
|
// rather than a wrong one.
|
|
|
|
function toTime(value) {
|
|
if (value instanceof Date) {
|
|
const t = value.getTime();
|
|
return Number.isNaN(t) ? null : t;
|
|
}
|
|
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
if (typeof value === 'string') {
|
|
const t = Date.parse(value);
|
|
return Number.isNaN(t) ? null : t;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* The newest card modification per list.
|
|
*
|
|
* @param {Array<{listId: string, modifiedAt: *, dateLastActivity: *}>} cards
|
|
* @returns {Map<string, Date>} listId -> newest date. A list whose cards all
|
|
* lack a usable date is absent from the map, so the caller can report null
|
|
* rather than an invented time.
|
|
*/
|
|
function newestCardChangeByList(cards) {
|
|
const newest = new Map();
|
|
for (const card of Array.isArray(cards) ? cards : []) {
|
|
if (!card || typeof card.listId !== 'string' || card.listId === '') continue;
|
|
// dateLastActivity is what older WeKan wrote; modifiedAt is current. Take
|
|
// whichever is newer, so a board written by an older version still answers.
|
|
const times = [toTime(card.modifiedAt), toTime(card.dateLastActivity)]
|
|
.filter(t => t !== null);
|
|
if (!times.length) continue;
|
|
const t = Math.max(...times);
|
|
const previous = newest.get(card.listId);
|
|
if (previous === undefined || t > previous) newest.set(card.listId, t);
|
|
}
|
|
const out = new Map();
|
|
for (const [listId, t] of newest) out.set(listId, new Date(t));
|
|
return out;
|
|
}
|
|
|
|
module.exports = { newestCardChangeByList, toTime };
|