Files
wekan/models/lib/myAttachmentsQuery.js
T
Lauri Ojansivu a9dd6b05b0 Add a My Attachments page to the member menu.
A third sibling of My Cards / My Due Cards: every attachment the current
user has uploaded, across every board they can see (their own boards, and
public ones), in one place. The menu entry sits right after My Due Cards
(client/components/users/userHeader.jade), and its own route (/my-attachments,
config/router.js) renders a dedicated page
(client/components/main/myAttachments.jade/.js) grouped board > swimlane >
list > card, reusing the same "open the card in place" popup mechanism
myCards.js's .js-minicard handler uses (#3640) so a click never navigates
away and loses the reader's place in a list spanning many boards.

The new `myAttachments` publication (server/publications/cards.js) filters
by BOTH the uploader (this user's own attachments only) and board visibility,
using the same boardVisibilitySelectors() the All Boards list and the board
publication already use (GHSA-gwc4-fw7p-gw58) rather than a hand-rolled
duplicate - so an attachment on a board the user cannot see is never
published, even if its userId field somehow matches. The query shape itself
lives in a small pure module, models/lib/myAttachmentsQuery.js, so it is
unit-tested without a database.

Thanks to Jieiku and xet7 !

Closes #3461,
2026-09-10 20:07:59 +03:00

55 lines
2.0 KiB
JavaScript

// Pure, dependency-free construction of the "My Attachments" (#3461) query:
// which attachments the CURRENT user may see on their own listing page - only
// what THEY uploaded (`userId`), and only on a board they can see (a boardId
// from the visible set the caller already computed with
// models/lib/boardVisibilitySelectors.js). No Meteor imports, so it is unit
// tested directly with plain Node (tests/myAttachments.test.cjs).
//
// Kept separate from server/publications/cards.js so the SHAPE of the query -
// "mine, and only on a board I can see" - is checkable without a database,
// the same way dueCards/myCards' board-visibility filtering is checkable by
// reading the source in tests/databaseQueryBounds.test.cjs.
const DEFAULT_LIMIT = 200;
const MAX_LIMIT = 500;
/**
* @param {string} userId the current user - the UPLOADER, not just the viewer
* @param {string[]} visibleBoardIds every board this user may currently see
* (public boards + boards they belong to; see boardVisibilitySelectors.js)
* @return {object|null} the Mongo selector, or null when there is nothing to
* query - no signed-in user, or a user who can see no board at all
*/
function myAttachmentsSelector(userId, visibleBoardIds) {
if (!userId || typeof userId !== 'string') return null;
const boardIds = Array.isArray(visibleBoardIds)
? visibleBoardIds.filter(id => typeof id === 'string' && id)
: [];
if (boardIds.length === 0) return null;
return {
userId,
'meta.boardId': { $in: boardIds },
};
}
/**
* Clamp caller-supplied paging to a safe range, the same way dueCards does.
* @param {number} [limit]
* @param {number} [skip]
* @return {{limit: number, skip: number}}
*/
function myAttachmentsPageOptions(limit, skip) {
return {
limit: Math.max(1, Math.min(Math.floor(limit) || DEFAULT_LIMIT, MAX_LIMIT)),
skip: Math.max(0, Math.floor(skip) || 0),
};
}
module.exports = {
DEFAULT_LIMIT,
MAX_LIMIT,
myAttachmentsSelector,
myAttachmentsPageOptions,
};