Files
wekan/models/checklistItems.js
T
Lauri Ojansivu 075e86b00e **Fixed new checklists (and checklist items) on a newly added card not being
visible until logout/login.** The `board` publication batched checklists,
checklist items, comments and attachments into board-level cursors filtered by
`cardId: { $in: cardIds }`, where `cardIds` was a one-time snapshot. In
`reywood:publish-composite` a child cursor only re-runs when its parent (the
board) document changes, so the snapshot never refreshed when a card was added
— a checklist on a card created after subscribing matched no published card and
only appeared on the next subscribe (logout/login). This was a regression from
the "Optimized board loading" change, which had replaced the original reactive
per-card child cursors with these batched snapshots. Fixed by **denormalizing a
`boardId` field onto `Checklists` and `ChecklistItems`** so they can be
published with a single board-level cursor filtered by `boardId` — one cursor
per collection (keeping the load optimization) that still reacts to checklists
on newly added cards, because a new checklist is created already carrying the
board's id. `boardId` is set on insert (server `before.insert` hooks, plus
explicitly in the Trello/WeKan board importers, which use `direct.insert` and
bypass hooks), re-derived when a checklist/item or its card moves to another
card (`before.update` on `cardId`) or the card moves to another board
(`Cards.after.update` cascade), and backfilled for existing data by an
idempotent startup migration. New `{ boardId: 1 }` indexes were added on both
collections. Comments and attachments instead remain reactive as per-card
children of the cards cursor. Assigned-only board members still only receive
checklists for cards assigned to them (the board-level cursor falls back to the
assigned cards' ids for those roles).

Thanks to ahlgrimma and Claude !

Fixes #6361
2026-06-06 04:02:48 +03:00

242 lines
7.2 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { ReactiveCache } from '/imports/reactiveCache';
import Activities from '/models/activities';
const { SimpleSchema } = require('/imports/simpleSchema');
const ChecklistItems = new Mongo.Collection('checklistItems');
/**
* An item in a checklist
*/
ChecklistItems.attachSchema(
new SimpleSchema({
title: {
/**
* the text of the item
*/
type: String,
},
sort: {
/**
* the sorting field of the item
*/
type: Number,
},
isFinished: {
/**
* Is the item checked?
*/
type: Boolean,
defaultValue: false,
},
checklistId: {
/**
* the checklist ID the item is attached to
*/
type: String,
},
cardId: {
/**
* the card ID the item is attached to
*/
type: String,
},
boardId: {
/**
* The ID of the board the item's card is on. Denormalized from the card
* so the board publication can publish checklist items with a single
* reactive cursor filtered by boardId. Kept in sync on insert, on the
* item / its checklist moving to another card, and when the card moves to
* another board.
*/
type: String,
optional: true,
},
createdAt: {
type: Date,
optional: true,
// eslint-disable-next-line consistent-return
autoValue() {
if (this.isInsert) {
return new Date();
} else if (this.isUpsert) {
return { $setOnInsert: new Date() };
} else {
this.unset();
}
},
},
modifiedAt: {
type: Date,
// eslint-disable-next-line consistent-return
autoValue() {
if (this.isInsert || this.isUpsert || this.isUpdate) {
return new Date();
} else {
this.unset();
}
},
},
}),
);
ChecklistItems.before.insert((userId, doc) => {
if (!doc.userId) {
doc.userId = userId;
}
});
ChecklistItems.helpers({
async setTitle(title) {
return await ChecklistItems.updateAsync(this._id, { $set: { title } });
},
async check() {
return await ChecklistItems.updateAsync(this._id, { $set: { isFinished: true } });
},
async uncheck() {
return await ChecklistItems.updateAsync(this._id, { $set: { isFinished: false } });
},
async toggleItem() {
return await ChecklistItems.updateAsync(this._id, { $set: { isFinished: !this.isFinished } });
},
async move(checklistId, sortIndex) {
const checklist = await ReactiveCache.getChecklist(checklistId);
const cardId = checklist.cardId;
return await ChecklistItems.updateAsync(this._id, {
$set: { cardId, checklistId, sort: sortIndex },
});
},
});
// Activities helper
export async function itemCreation(userId, doc) {
const card = await ReactiveCache.getCard(doc.cardId);
if (!card) {
console.warn('[itemCreation] Card not found for cardId:', doc.cardId, '— skipping activity insert.');
return;
}
const boardId = card.boardId;
await Activities.insertAsync({
userId,
activityType: 'addChecklistItem',
cardId: doc.cardId,
boardId,
checklistId: doc.checklistId,
checklistItemId: doc._id,
checklistItemName: doc.title,
listId: card.listId,
swimlaneId: card.swimlaneId,
});
}
export async function itemRemover(userId, doc) {
await Activities.removeAsync({
checklistItemId: doc._id,
});
}
export async function publishCheckActivity(userId, doc) {
const card = await ReactiveCache.getCard(doc.cardId);
if (!card) {
console.warn('[publishCheckActivity] Card not found for cardId:', doc.cardId, '— skipping activity insert.');
return;
}
const boardId = card.boardId;
let activityType;
if (doc.isFinished) {
activityType = 'checkedItem';
} else {
activityType = 'uncheckedItem';
}
const act = {
userId,
activityType,
cardId: doc.cardId,
boardId,
checklistId: doc.checklistId,
checklistItemId: doc._id,
checklistItemName: doc.title,
listId: card.listId,
swimlaneId: card.swimlaneId,
};
await Activities.insertAsync(act);
}
export async function publishChekListCompleted(userId, doc) {
const card = await ReactiveCache.getCard(doc.cardId);
if (!card) {
console.warn('[publishChekListCompleted] Card not found for cardId:', doc.cardId, '— skipping activity insert.');
return;
}
const boardId = card.boardId;
const checklistId = doc.checklistId;
const checkList = await ReactiveCache.getChecklist(checklistId);
if (!checkList) {
console.warn('[publishChekListCompleted] Checklist not found for checklistId:', checklistId, '— skipping activity insert.');
return;
}
const checklistItems = await ReactiveCache.getChecklistItems({ checklistId });
const isChecklistFinished = checkList.hideAllChecklistItems ||
(checklistItems.length > 0 && checklistItems.length === checklistItems.filter(i => i.isFinished).length);
if (isChecklistFinished) {
const act = {
userId,
activityType: 'completeChecklist',
cardId: doc.cardId,
boardId,
checklistId: doc.checklistId,
checklistName: checkList.title,
listId: card.listId,
swimlaneId: card.swimlaneId,
};
await Activities.insertAsync(act);
}
}
export async function publishChekListUncompleted(userId, doc) {
const card = await ReactiveCache.getCard(doc.cardId);
if (!card) {
console.warn('[publishChekListUncompleted] Card not found for cardId:', doc.cardId, '— skipping activity insert.');
return;
}
const boardId = card.boardId;
const checklistId = doc.checklistId;
const checkList = await ReactiveCache.getChecklist(checklistId);
if (!checkList) {
console.warn('[publishChekListUncompleted] Checklist not found for checklistId:', checklistId, '— skipping activity insert.');
return;
}
// BUGS in IFTTT Rules: https://github.com/wekan/wekan/issues/1972
// Currently in checklist all are set as uncompleted/not checked,
// IFTTT Rule does not move card to other list.
// If following line is negated/changed to:
// if(!isChecklistFinished){
// then unchecking of any checkbox will move card to other list,
// even when all checkboxes are not yet unchecked.
// What is correct code for only moving when all in list is unchecked?
// TIPS: Finding files, ignoring some directories with grep -v:
// cd wekan
// find . | xargs grep 'count' -sl | grep -v .meteor | grep -v node_modules | grep -v .build
// Maybe something related here?
// wekan/client/components/rules/triggers/checklistTriggers.js
const uncheckItems = await ReactiveCache.getChecklistItems({ checklistId });
const isChecklistFinished = checkList.hideAllChecklistItems ||
(uncheckItems.length > 0 && uncheckItems.length === uncheckItems.filter(i => i.isFinished).length);
if (isChecklistFinished) {
const act = {
userId,
activityType: 'uncompleteChecklist',
cardId: doc.cardId,
boardId,
checklistId: doc.checklistId,
checklistName: checkList.title,
listId: card.listId,
swimlaneId: card.swimlaneId,
};
await Activities.insertAsync(act);
}
}
export default ChecklistItems;