mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
format and time zone. #6586 was reopened for what the first fix left, and every point of it was a symptom of one thing: the PDF export of a card had grown separately from the Excel export of the same card. "Assignee, Labels, due,... these titels should be in the user set language" - the labels were hard-coded English, and not even consistent with each other: the card export wrote "Due: " while the board export wrote "due" with no colon. Both exports now build every label from the same i18n keys, in the language the request carries, through one field() helper that writes every "Label: value" - so the two cannot drift apart again. Every key carries its English text as the fallback, so a missing translation shows the word and never the key. "I think all those other things we set in a card should be also present" - both exports now carry title, labels, creator, assignees, members, board, swimlane, list, card number, requested by, assigned by, the six dates, spent time, description, custom fields, checklists, subtasks, comments, attachments, voting and Planning Poker, in that order. Custom fields, voting and poker are new on BOTH sides; they are appended to ALL_FIELDS rather than inserted, so a saved ?fields= link still asks for what it always asked for, and they are checkboxes in the export popup like the rest. "the time is not in the user set timezone (-2h wrong for Europe/Berlin)" - it was worse than -2h: the PDF printed UTC and the Excel export printed whatever zone the server was started in. Dates are stored in UTC and a profile carries no zone, so the export links now send the browser's IANA zone and the date format the OPENED CARD is showing - which for a logged-out reader lives in localStorage, where no server-side lookup reaches it. Both exports format through formatDateByUserPreference, which gained an optional zone; without one it still renders in the process zone, which is what every client-side caller wants. A server-built export given no zone renders UTC and says UTC. Verified: 12:00Z prints 14:00 for Europe/Berlin, 00:00 on the 15th for Pacific/Auckland, and an unknown zone falls back instead of failing. "would it make sense to support markdown formated text in description" - a description is now cut into runs and drawn in the four base-14 Courier faces, so **bold**, *italic* and ***both*** are bold, italic and both. Consecutive Tj operators continue at the current text position, so a font switch needs no glyph measuring. Strikethrough and inline code keep their words and lose their markers - a Type1 base-14 font has no strike and there is no fifth face for code - and an underscore inside a word stays an underscore. tests/cardExportParity.test.cjs pins the shared field list, keys, formatter and request params; tests/pdfExport.test.cjs gains the runs, the wrapping and the four declared faces. Two of its guards moved rather than being deleted: they pinned the hard-coded English "Labels: " and "due", which is the behaviour that deliberately changed, and they now pin that both come from the same keys. Thanks to Heart1010 and xet7 ! Fixes #6586,
172 lines
6.8 KiB
JavaScript
172 lines
6.8 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
||
import { ReactiveCache } from '/imports/reactiveCache';
|
||
import { TAPi18n } from '/imports/i18n';
|
||
import { runOnServer } from './runOnServer';
|
||
import ImpersonatedUsers from '/models/impersonatedUsers';
|
||
|
||
runOnServer(function() {
|
||
const { ExporterExcelCard, ALL_FIELDS } = require('./server/ExporterExcelCard');
|
||
const { WebApp } = require('meteor/webapp');
|
||
const { safeRoute } = require('/server/apiMiddleware');
|
||
const { Authentication } = require('/server/authentication');
|
||
|
||
/**
|
||
* @operation exportExcelCard
|
||
* @tag Cards
|
||
*
|
||
* @summary Export a single card to Excel (.xlsx), formatted for DIN A4 Portrait printing.
|
||
*
|
||
* @description If user is already logged-in, pass loginToken as param
|
||
* "authToken": '/api/boards/:boardId/lists/:listId/cards/:cardId/exportExcel?authToken=:token'
|
||
*
|
||
* Optional query param "fields" is a comma-separated list of sections to include.
|
||
* Valid values: people, board-info, dates, description, checklists, subtasks, comments
|
||
* Omitting "fields" includes all sections.
|
||
*
|
||
* @param {string} boardId the ID of the board
|
||
* @param {string} listId the ID of the list
|
||
* @param {string} cardId the ID of the card to export
|
||
* @param {string} authToken the loginToken
|
||
* @param {string} fields comma-separated list of sections to include
|
||
*/
|
||
WebApp.handlers.get(
|
||
'/api/boards/:boardId/lists/:listId/cards/:cardId/exportExcel',
|
||
safeRoute(async function (req, res) {
|
||
const boardId = req.params.boardId;
|
||
const paramListId = req.params.listId;
|
||
const paramCardId = req.params.cardId;
|
||
let user = null;
|
||
let impersonateDone = false;
|
||
let adminId = null;
|
||
|
||
const board = await ReactiveCache.getBoard(boardId);
|
||
if (!board) {
|
||
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||
res.end('Board not found');
|
||
return;
|
||
}
|
||
|
||
// GHSA-6p5m-f9p2-wqm5: the card has to BELONG to the board being
|
||
// authorised. Every check below is about `board` - isPublic() on one
|
||
// branch, canExport() on the other - and both were deciding access to one
|
||
// object while the export read a different one, named by a path parameter
|
||
// the caller also controls. Bound here, at the routing layer, as well as in
|
||
// the exporter's own query: the two identifiers arrive together, so this is
|
||
// where their relationship is cheapest to state, and it holds for the
|
||
// public branch too, which skips authentication entirely.
|
||
//
|
||
// 404, not 403: whether a given card id exists at all is not something an
|
||
// unauthorised caller should learn from the difference.
|
||
const containedCard = await ReactiveCache.getCard({
|
||
_id: paramCardId,
|
||
boardId,
|
||
listId: paramListId,
|
||
});
|
||
if (!containedCard) {
|
||
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||
res.end('Card not found');
|
||
return;
|
||
}
|
||
|
||
// Public boards skip authentication
|
||
if (board.isPublic()) {
|
||
const fieldsParam = req.query.fields;
|
||
const fields = fieldsParam
|
||
? fieldsParam.split(',').map(f => f.trim()).filter(f => ALL_FIELDS.includes(f))
|
||
: null;
|
||
const exporter = new ExporterExcelCard(boardId, paramListId, paramCardId, 'en', fields);
|
||
await exporter.build(res);
|
||
return;
|
||
}
|
||
|
||
// Authenticate for private boards
|
||
const loginToken = req.query.authToken;
|
||
if (loginToken) {
|
||
if (loginToken.length > 10000) {
|
||
res.writeHead(400, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||
res.end('Invalid token');
|
||
return;
|
||
}
|
||
|
||
const hashToken = Accounts._hashLoginToken(loginToken);
|
||
user = await ReactiveCache.getUser({
|
||
'services.resume.loginTokens.hashedToken': hashToken,
|
||
});
|
||
if (!user) {
|
||
res.writeHead(401, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||
res.end('Invalid token');
|
||
return;
|
||
}
|
||
adminId = user._id.toString();
|
||
impersonateDone = await ReactiveCache.getImpersonatedUser({ adminId });
|
||
} else if (!Meteor.settings.public.sandstorm) {
|
||
try {
|
||
await Authentication.checkUserId(req.userId);
|
||
} catch (error) {
|
||
res.writeHead(error.statusCode || 403, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||
res.end('Unauthorized');
|
||
return;
|
||
}
|
||
user = await ReactiveCache.getUser({
|
||
_id: req.userId,
|
||
isAdmin: true,
|
||
});
|
||
}
|
||
|
||
// Determine language: prefer the active UI language sent by the client
|
||
// (?lang=fi), then the profile setting, then English. The client-side
|
||
// param is necessary because WeKan may use the browser locale without
|
||
// ever writing it to profile.language.
|
||
let userLanguage =
|
||
req.query.lang ||
|
||
(user && user.profile && user.profile.language) ||
|
||
'en';
|
||
|
||
// Ensure the chosen language bundle is loaded into i18next.
|
||
try {
|
||
await TAPi18n.loadLanguage(userLanguage);
|
||
} catch (_) {
|
||
// Unknown / unsupported language – fall back to English silently.
|
||
userLanguage = 'en';
|
||
}
|
||
|
||
// #6586: the date format the OPENED CARD is showing, sent by the export
|
||
// link - for a reader who is not logged in it lives in localStorage, which
|
||
// this lookup cannot reach. The profile is the fallback, and only the
|
||
// three formats formatDateByUserPreference understands are accepted.
|
||
const DATE_FORMATS = ['YYYY-MM-DD', 'DD-MM-YYYY', 'MM-DD-YYYY'];
|
||
const requestedFormat = req.query && req.query.dateFormat;
|
||
const dateFormat = DATE_FORMATS.includes(requestedFormat)
|
||
? requestedFormat
|
||
: ((user && user.profile && user.profile.dateFormat) || 'YYYY-MM-DD');
|
||
|
||
// #6586: the reader's IANA zone, sent by the export link. Without it the
|
||
// dates come out in the server's zone, which is the "-2h wrong for
|
||
// Europe/Berlin" the PDF export was reported for and this export shared.
|
||
const timezone = (req.query && typeof req.query.tz === 'string' && req.query.tz.length <= 64)
|
||
? req.query.tz
|
||
: '';
|
||
|
||
// Parse optional ?fields=people,dates,... query param
|
||
const fieldsParam = req.query.fields;
|
||
const fields = fieldsParam
|
||
? fieldsParam.split(',').map(f => f.trim()).filter(f => ALL_FIELDS.includes(f))
|
||
: null;
|
||
|
||
const exporter = new ExporterExcelCard(boardId, paramListId, paramCardId, userLanguage, fields, dateFormat, timezone);
|
||
if ((await exporter.canExport(user))) {
|
||
if (impersonateDone) {
|
||
await ImpersonatedUsers.insertAsync({
|
||
adminId,
|
||
boardId,
|
||
reason: 'exportExcelCard',
|
||
});
|
||
}
|
||
await exporter.build(res);
|
||
} else {
|
||
res.end(TAPi18n.__('user-can-not-export-card-to-excel'));
|
||
}
|
||
}),
|
||
);
|
||
});
|