Files
wekan/server/models/attachments.js
T
Lauri Ojansivu b7589737f5 File docs/Features the way the WeKan menus are.
A feature's guide now sits under the menu it is used from, read from the
templates rather than from where somebody once put it: Header-Bar,
All-Boards, Pages, Board-Header (search and every Board View), Board-Sidebar
(Board Settings, Filter, Members, Labels, Archive), Rules, Swimlanes, Lists,
Cards, Member-Settings, Admin-Panel and Login-Page. Topics used from several
menus (editor, history, theme, translations, email, troubleshooting) are in
General.

- Each area has a README mapping its menu entries to their guides and to the
  template or server module that implements them. Board-Header/README.md
  lists all 40 Board View entries with the template boardBody.jade renders;
  five views (Table, Calendar Mode, Time, Timeline, Map) have no guide of
  their own yet and say so.
- Board-View.md listed 25 views while BOARD_VIEWS has 40; it now lists all
  of them with their English labels.
- Pages keep their file names. Where a released changelog links an old path,
  a "# Moved" page stays there and points to the new one; test-menu.sh skips
  folders holding only those.
- Undo was filed under Login; it is General/Undo now.
- Every link in docs, code comments, scripts and tests follows the move;
  docsLinksResolve checks them all.

Tests that read a doc follow it to its new place. boardViewSettings now fails
when Board-View.md or Board-Header/README.md differs from BOARD_VIEWS, or
names a template boardBody.jade does not render.

Thanks to xet7 !
2026-10-16 23:45:35 +03:00

140 lines
5.7 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
import { DDP } from 'meteor/ddp';
import { Authentication } from '/server/authentication';
import { sendJsonResult } from '/server/apiMiddleware';
import { publicErrorData } from '/server/lib/apiResponseHelpers';
import Attachments from '/models/attachments';
// REST API for the attachment soft delete (docs/Features/General/History/
// History.md §12). Every write here goes through the SAME Meteor methods the
// card and the card history use - `attachments.softDelete` and
// `attachments.restore` in server/attachmentSoftDelete.js - run as the
// request's user, so the permission check, the cover unset, the activity and
// the history row are the ones the UI gets. Nothing here hard-deletes: the one
// hard delete of attachments is the archived-board purge (§12.4).
// The raw collection, not ReactiveCache.getAttachment(): that one falls back to
// the legacy CollectionFS lookup, and a soft-deleted document must be found as
// itself, deletedAt and all.
async function attachmentOfBoard(attachmentId, boardId) {
return Attachments.collection.findOneAsync({
_id: attachmentId,
'meta.boardId': boardId,
});
}
function callAsUser(userId, name, ...args) {
return DDP._CurrentMethodInvocation.withValue(
{ userId },
async () => Meteor.callAsync(name, ...args),
);
}
function serializeDeleted(attachment) {
return {
attachmentId: attachment._id,
attachmentName: attachment.name,
attachmentType: attachment.type,
boardId: attachment.meta.boardId,
swimlaneId: attachment.meta.swimlaneId,
listId: attachment.meta.listId,
cardId: attachment.meta.cardId,
deletedAt: attachment.deletedAt,
deletedBy: attachment.deletedBy,
deleteBatchId: attachment.deleteBatchId,
};
}
/**
* @operation get_board_deleted_attachments
* @tag Attachments
* @summary Get the soft-deleted attachments of a board
*
* @description Deleting an attachment from a card is a soft delete: the file
* is kept, the card hides it, and the card history can restore it. This lists
* the attachments of a board that are currently deleted, with who deleted each
* and when, so a script can find one to restore. The live attachments are at
* `GET /api/boards/:boardId/attachments`. Requires board access.
*
* @param {string} boardId the board ID
* @return_type [{attachmentId: string, attachmentName: string, attachmentType: string, boardId: string, swimlaneId: string, listId: string, cardId: string, deletedAt: string, deletedBy: string, deleteBatchId: string}]
*/
WebApp.handlers.get('/api/boards/:boardId/attachments/deleted', async function(req, res) {
try {
const paramBoardId = req.params.boardId;
await Authentication.checkBoardAccess(req.userId, paramBoardId);
const deleted = await Attachments.collection
.find({ 'meta.boardId': paramBoardId, deletedAt: { $ne: null } })
.fetchAsync();
sendJsonResult(res, { code: 200, data: deleted.map(serializeDeleted) });
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
});
/**
* @operation delete_board_attachment
* @tag Attachments
* @summary Soft-delete an attachment
*
* @description The same "Delete" the card offers: the attachment gets
* `deletedAt`, `deletedBy` and `deleteBatchId`, its file is kept, the card and
* the minicard badge hide it, its cover is unset if it was the card's cover,
* and the card history records who deleted it. Restore it with
* `POST /api/boards/:boardId/attachments/:attachmentId/restore`. There is no
* hard delete of a single attachment over the API. Requires permission to
* edit the card the attachment is on (or write access to the board for a
* board-level attachment). Idempotent: an already-deleted attachment is left
* as it is.
*
* @param {string} boardId the board ID
* @param {string} attachmentId the attachment ID
* @return_type {deleted: boolean, batchId: string}
*/
WebApp.handlers.delete('/api/boards/:boardId/attachments/:attachmentId', async function(req, res) {
try {
Authentication.checkLoggedIn(req.userId);
const attachment = await attachmentOfBoard(req.params.attachmentId, req.params.boardId);
if (!attachment) {
sendJsonResult(res, { code: 404, data: { error: 'Attachment not found' } });
return;
}
const result = await callAsUser(req.userId, 'attachments.softDelete', attachment._id);
sendJsonResult(res, { code: 200, data: result });
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
});
/**
* @operation restore_board_attachment
* @tag Attachments
* @summary Restore a soft-deleted attachment
*
* @description The card history's Restore for an attachment row: clears the
* delete mark so the card, its attachment count and the minicard badge include
* the attachment again, and records the restore in the card history. The
* cover is never re-set by a restore. Requires the same permission as
* deleting. Idempotent: `restored` is false for an attachment that is not
* deleted.
*
* @param {string} boardId the board ID
* @param {string} attachmentId the attachment ID
* @return_type {restored: boolean}
*/
WebApp.handlers.post('/api/boards/:boardId/attachments/:attachmentId/restore', async function(req, res) {
try {
Authentication.checkLoggedIn(req.userId);
const attachment = await attachmentOfBoard(req.params.attachmentId, req.params.boardId);
if (!attachment) {
sendJsonResult(res, { code: 404, data: { error: 'Attachment not found' } });
return;
}
const result = await callAsUser(req.userId, 'attachments.restore', attachment._id);
sendJsonResult(res, { code: 200, data: result });
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
});