mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Delete on an attachment used to remove the document and its file at once. It is now the 'attachments.softDelete' method (History.md section 12): the document gets deletedAt/deletedBy/deleteBatchId with the same helpers lists use, the file is kept, and the card's cover is unset if this was it. Every card-facing read - the opened card's gallery and "Attachments (N)" count, the minicard paperclip badge, the slideshow, the cover, the board-background picker, My Attachments, the API list endpoints and the exporters - filters to live attachments with liveAttachments(); the publications keep sending the deleted ones so the card history can reach them. The card history records who deleted it and when, with the filename in the row. Restore - the table's selection + Restore, or the row's own Restore button, both through changeHistory.restore and applyAttachmentContent - clears the mark, so the card, its count and the minicard badge include the attachment again; the cover is never re-set. An attachment row previews with the existing attachmentViewer slideshow and downloads with the same link the gallery draws, and never offers cover or background. Uploads and renames are recorded through the existing history hooks. There is no per-attachment hard delete any more: api.attachment.delete, DELETE /api/attachment/delete/:id and removeBoardBackground soft-delete, permanentlyDeleteAttachmentFromFilesReport and the Files report's delete button are gone, Attachments.allow remove and onBeforeRemove refuse every client remove and log the attempt under Admin Panel / Problems. The one hard delete is Admin Panel / Problems / Delete enabled, board archived, board deleted from the archive: boardRemover now removes every attachment of the board, live and soft-deleted, with its file. One new i18n key, attachment-soft-delete-pop, replaces the "permanent, no undo" confirmation text; it is inserted in English into every locale at the same position for translation. Thanks to xet7 !
147 lines
6.2 KiB
JavaScript
147 lines
6.2 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { check } from 'meteor/check';
|
|
import Attachments from '/models/attachments';
|
|
import Activities from '/models/activities';
|
|
import Boards from '/models/boards';
|
|
import Cards from '/models/cards';
|
|
import ChangeHistory from '/models/changeHistory';
|
|
import { allowIsBoardMemberWithWriteAccess } from '/server/lib/utils';
|
|
import { canEditCardOrLinkedCard } from '/server/lib/linkedCardPermission';
|
|
import { deleteActivityUserId } from '/server/lib/attachmentActivityActor';
|
|
import { isRecordingSuppressed } from '/server/lib/historyRecordingScope';
|
|
import {
|
|
softDeleteAttachmentPlan,
|
|
restoreAttachmentPlan,
|
|
} from '/models/lib/attachmentSoftDelete';
|
|
|
|
// Attachment soft delete and restore (History.md §12). "Delete" on an
|
|
// attachment marks it - deletedAt / deletedBy / deleteBatchId, the same fields
|
|
// and helpers lists use - and keeps the file. The card, the minicard badge and
|
|
// every other card-facing read filter it out; the card history shows who
|
|
// deleted it and restores it. Nothing here ever calls Attachments.removeAsync:
|
|
// the one hard delete of attachments is the archived-board purge
|
|
// (server/models/boards.js boardRemover, §12.4).
|
|
|
|
// Who may delete or restore an attachment: whoever may edit the card it is on
|
|
// (the same check the upload allow rule uses, linked cards included), or for
|
|
// a board-level attachment such as a background, a member with write access.
|
|
export async function canSoftDeleteAttachment(userId, attachment) {
|
|
if (!userId || !attachment) return false;
|
|
const cardId = attachment.meta?.cardId;
|
|
if (cardId) {
|
|
const card = await Cards.findOneAsync(cardId);
|
|
if (card) return await canEditCardOrLinkedCard(userId, card);
|
|
}
|
|
const board = await Boards.findOneAsync(attachment.meta?.boardId);
|
|
return allowIsBoardMemberWithWriteAccess(userId, board);
|
|
}
|
|
|
|
const activityFor = (attachment, activityType, userId) => ({
|
|
userId,
|
|
type: 'card',
|
|
activityType,
|
|
attachmentId: attachment._id,
|
|
attachmentName: attachment.name,
|
|
boardId: attachment.meta?.boardId,
|
|
cardId: attachment.meta?.cardId,
|
|
listId: attachment.meta?.listId,
|
|
swimlaneId: attachment.meta?.swimlaneId,
|
|
});
|
|
|
|
// Soft-delete one attachment on behalf of `userId`. The caller has already
|
|
// checked permission. Idempotent: an attachment that is already deleted is
|
|
// left as it is and its batch id is returned.
|
|
export async function softDeleteAttachment({ userId, attachment }) {
|
|
const cardId = attachment.meta?.cardId;
|
|
const card = cardId ? await Cards.findOneAsync(cardId) : null;
|
|
const plan = softDeleteAttachmentPlan({ attachment, card, userId, at: new Date() });
|
|
if (!plan) return null;
|
|
if (plan.alreadyDeleted) return attachment.deleteBatchId || null;
|
|
|
|
// A deleted attachment cannot be the cover (§12.1); restore does not put it
|
|
// back, that is a choice about a live attachment.
|
|
if (plan.unsetCover) {
|
|
await Cards.updateAsync(card._id, { $unset: { coverId: '' } });
|
|
}
|
|
await Attachments.collection.updateAsync({ _id: attachment._id }, { $set: plan.set });
|
|
|
|
// The activity the hard delete used to write from the store strategy's
|
|
// onAfterRemove, with the same actor rule (#5504): the feed and the outgoing
|
|
// webhooks see the delete exactly as before.
|
|
if (cardId) {
|
|
try {
|
|
await Activities.insertAsync(
|
|
activityFor(attachment, 'deleteAttachment', deleteActivityUserId(userId, attachment.userId)),
|
|
);
|
|
} catch (error) {
|
|
console.error('Failed to insert deleteAttachment activity:', error);
|
|
}
|
|
}
|
|
// A history Restore runs this same function inside withoutRecording(): the
|
|
// restore writes its own rows, and a second one here would describe the same
|
|
// write twice (tests/historyRestoreRecordsOnce.test.cjs).
|
|
if (!isRecordingSuppressed()) await ChangeHistory.record(plan.historyRow);
|
|
return plan.batchId;
|
|
}
|
|
|
|
// Restore one soft-deleted attachment. Idempotent: a live one is left alone.
|
|
export async function restoreAttachment({ userId, attachment }) {
|
|
const plan = restoreAttachmentPlan({ attachment, userId });
|
|
if (!plan) return false;
|
|
if (plan.alreadyLive) return false;
|
|
await Attachments.collection.updateAsync({ _id: attachment._id }, plan.modifier);
|
|
if (attachment.meta?.cardId) {
|
|
try {
|
|
await Activities.insertAsync(activityFor(attachment, 'addAttachment', userId));
|
|
} catch (error) {
|
|
console.error('Failed to insert addAttachment activity:', error);
|
|
}
|
|
}
|
|
if (!isRecordingSuppressed()) await ChangeHistory.record(plan.historyRow);
|
|
return true;
|
|
}
|
|
|
|
async function loadAttachment(attachmentId) {
|
|
// 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.
|
|
return Attachments.collection.findOneAsync({ _id: attachmentId });
|
|
}
|
|
|
|
Meteor.methods({
|
|
// The ordinary "Delete" on a card's attachment (and the API's delete).
|
|
async 'attachments.softDelete'(attachmentId) {
|
|
check(attachmentId, String);
|
|
if (!this.userId) {
|
|
throw new Meteor.Error('not-authorized', 'Must be logged in');
|
|
}
|
|
const attachment = await loadAttachment(attachmentId);
|
|
if (!attachment) {
|
|
throw new Meteor.Error('attachment-not-found', 'Attachment not found');
|
|
}
|
|
if (!(await canSoftDeleteAttachment(this.userId, attachment))) {
|
|
throw new Meteor.Error('not-authorized', 'You do not have permission to delete this attachment');
|
|
}
|
|
const batchId = await softDeleteAttachment({ userId: this.userId, attachment });
|
|
return { deleted: true, batchId };
|
|
},
|
|
|
|
// The card history's Restore for an attachment row also lands here, through
|
|
// changeHistory.restore and applyAttachmentContent.
|
|
async 'attachments.restore'(attachmentId) {
|
|
check(attachmentId, String);
|
|
if (!this.userId) {
|
|
throw new Meteor.Error('not-authorized', 'Must be logged in');
|
|
}
|
|
const attachment = await loadAttachment(attachmentId);
|
|
if (!attachment) {
|
|
throw new Meteor.Error('attachment-not-found', 'Attachment not found');
|
|
}
|
|
if (!(await canSoftDeleteAttachment(this.userId, attachment))) {
|
|
throw new Meteor.Error('not-authorized', 'You do not have permission to restore this attachment');
|
|
}
|
|
const restored = await restoreAttachment({ userId: this.userId, attachment });
|
|
return { restored };
|
|
},
|
|
});
|