Files
wekan/server/methods/sinceLastVisit.js
T
Lauri Ojansivu 083c863fb7 Summary, Since your last visit, At risk, Suggested members and Forecast.
Five more of the features other kanban apps call AI, done with plain code
(docs/Features/General/Assistants/Assistants.md):

- Board menu -> Summary: what each person did in the last day or week -
  done, moved, added, commented - and what they have overdue, from the
  activities and History; Copy as text gives Markdown with card links.
- Board menu -> Since your last visit (N): what others changed since the
  visit before this one, by card. The visit is kept in profile.boardVisits,
  at most 500 boards.
- At risk: a weighted score of overdue, due soon with little done, blocked,
  idle in a middle list and sent back; a triangle on the minicard and the
  reasons in the opened card.
- Suggested members in the Members and Assignees popups, from who handles
  the same labels, list and swimlane.
- Forecast done date: the 50th and 85th percentile of the board's cycle
  times, counting only the cards that took at least as long as this one
  has been open. The server sends the times once per board, numbers only.

Summary and Since your last visit are features of Admin Panel / Settings /
Visibility / Features; At risk, Suggested members and Forecast are rows of
Board Settings / Card and of the Admin Panel's Card fields. Every server
answer applies the board's read rules and the assigned-only role.
Translated in every language.

The guards of the earlier helpers are brought up to date as well: the date
words of models/lib/titleDueDate.js are a reviewed RegExp site,
server/methods/ruleSuggestions.js is a board-admin-only path, the Rules
page's list branch carries the suggestions, and Tigre's new strings use its
own word for a board.

Thanks to xet7 !
2026-10-17 15:52:26 +03:00

70 lines
3.3 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { check, Match } from 'meteor/check';
import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';
import Activities from '/models/activities';
import Cards from '/models/cards';
import Users from '/models/users';
import { ReactiveCache } from '/imports/reactiveCache';
import { canReadBoard } from '/models/lib/boardVisibility';
import { featureOnFor } from '/server/lib/assistGate';
// Board menu -> Since your last visit
// (docs/Features/General/Since-Last-Visit/Since-Last-Visit.md). markVisited
// is called when a board opens and answers with the visit BEFORE this one,
// which is what the view lists from; it is kept while the feature is hidden,
// so turning it on again shows the right changes. The list reads a board the
// caller may read, and for an assigned-only member only their cards.
const { sinceOf, sinceLastVisit, boardVisitsModifier, TYPES } = require('/models/lib/sinceLastVisit');
const { assignedOnlyCardScope, mergeCardScope } = require('/models/lib/boardCardScope');
const ID = /^[A-Za-z0-9_-]{1,64}$/;
const MAX_ROWS = 5000;
async function readableBoard(userId, boardId) {
if (!userId || !ID.test(boardId)) return null;
const board = await ReactiveCache.getBoard(boardId);
return board && canReadBoard(userId, board) ? board : null;
}
Meteor.methods({
async 'boards.markVisited'(boardId) {
check(boardId, String);
const board = await readableBoard(this.userId, boardId);
if (!board) return null;
const user = await ReactiveCache.getUser(this.userId);
const visits = (user && user.profile && user.profile.boardVisits) || {};
const previous = visits[boardId] || null;
await Users.updateAsync(this.userId, boardVisitsModifier(visits, boardId, new Date()));
return previous;
},
async 'boards.sinceLastVisit'(boardId, previousVisit) {
check(boardId, String);
check(previousVisit, Match.Maybe(Date));
if (!(await featureOnFor(this.userId, 'since-last-visit'))) return [];
const board = await readableBoard(this.userId, boardId);
if (!board) return [];
const since = sinceOf(previousVisit);
const activities = await Activities.find(
{ boardId, activityType: { $in: TYPES }, createdAt: { $gte: since }, userId: { $ne: this.userId } },
{ fields: { activityType: 1, userId: 1, cardId: 1, listName: 1, createdAt: 1 }, sort: { createdAt: -1 }, limit: MAX_ROWS },
).fetchAsync();
const cardIds = [...new Set(activities.map(a => a.cardId).filter(Boolean))];
const visible = await Cards.find(
mergeCardScope({ _id: { $in: cardIds }, boardId }, assignedOnlyCardScope(board, this.userId)),
{ fields: { title: 1 } },
).fetchAsync();
const cards = {};
for (const card of visible) cards[card._id] = card;
const userIds = [...new Set(activities.map(a => a.userId))];
const usernames = {};
for (const user of await Users.find({ _id: { $in: userIds } }, { fields: { username: 1 } }).fetchAsync()) {
usernames[user._id] = user.username;
}
return sinceLastVisit({ activities, cards, usernames, userId: this.userId, since });
},
});
DDPRateLimiter.addRule({ type: 'method', name: 'boards.markVisited', connectionId: () => true }, 20, 10000);
DDPRateLimiter.addRule({ type: 'method', name: 'boards.sinceLastVisit', connectionId: () => true }, 20, 10000);