mirror of
https://github.com/wekan/wekan.git
synced 2026-11-20 09:45:04 -05:00
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 !
156 lines
7.6 KiB
JavaScript
156 lines
7.6 KiB
JavaScript
// Board Settings / Bots (models/lib/botRuns.js, docs/Features/Rules/Bots.md):
|
|
// handing a card to a bot, and what its runner reports back.
|
|
//
|
|
// A bot has no rights in WeKan of its own:
|
|
// - WeKan sends it a dispatch through the ordinary outgoing-webhook queue
|
|
// (server/notifications/webhookQueue.js: the SSRF guard, signing with the
|
|
// bot's token, retries, Sandstorm's Powerbox), and nothing else;
|
|
// - its runner changes the board only through WeKan's REST API, with its
|
|
// own WeKan user's token and that user's board role; the one route here,
|
|
// the run report, needs write access to the board like every card write.
|
|
// Handing a card over is a board admin's choice: the Bots view, or a Rule a
|
|
// board admin made.
|
|
import { Meteor } from 'meteor/meteor';
|
|
import { check, Match } from 'meteor/check';
|
|
import { Random } from 'meteor/random';
|
|
import { WebApp } from 'meteor/webapp';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import Integrations from '/models/integrations';
|
|
import CardComments from '/models/cardComments';
|
|
import BotRuns from '/models/botRuns';
|
|
import { Authentication } from '/server/authentication';
|
|
import { sendJsonResult } from '/server/apiMiddleware';
|
|
import { publicErrorData } from '/server/lib/apiResponseHelpers';
|
|
import { allowIsBoardAdmin } from '/server/lib/utils';
|
|
import { canCommentInList } from '/server/lib/listPermissionGate';
|
|
import { ensureIndex } from '/server/lib/mongoStartup';
|
|
|
|
const runs = require('/models/lib/botRuns');
|
|
|
|
function outbox() {
|
|
return require('/server/notifications/webhookQueue').webhookOutbox;
|
|
}
|
|
|
|
async function enqueue(bot, eventId, body) {
|
|
await outbox().enqueue({ integrationId: bot._id, eventId, groupKey: eventId, body, deliverAt: Date.now() });
|
|
}
|
|
|
|
/**
|
|
* Hand a card to a bot of its own board: a run, and a dispatch to the bot.
|
|
* A card already with that bot keeps its run. Returns the run id.
|
|
*/
|
|
export async function dispatchToBot({ integrationId, cardId, userId, source }) {
|
|
const card = await ReactiveCache.getCard(cardId);
|
|
if (!card) throw new Meteor.Error('not-found');
|
|
const bot = await Integrations.findOneAsync({ _id: integrationId, boardId: card.boardId });
|
|
if (!runs.isBot(bot)) throw new Meteor.Error('bot-not-found');
|
|
const active = await BotRuns.findOneAsync({ cardId, integrationId, status: { $in: runs.ACTIVE } });
|
|
if (active) return active._id;
|
|
const board = await ReactiveCache.getBoard(card.boardId);
|
|
const list = await ReactiveCache.getList(card.listId);
|
|
const comments = await CardComments.find({ cardId }, { sort: { createdAt: 1 }, limit: 20 }).fetchAsync();
|
|
const now = new Date();
|
|
const run = { _id: Random.id(), boardId: card.boardId, cardId, integrationId, status: 'queued', source: source || 'button',
|
|
createdBy: userId || null, createdAt: now, updatedAt: now, events: [{ text: 'dispatched', status: 'queued', at: now }],
|
|
options: [], question: null };
|
|
await BotRuns.insertAsync(run);
|
|
await enqueue(bot, run._id, runs.dispatchBody({ run, bot, board, card, list, comments, baseUrl: Meteor.absoluteUrl('').replace(/\/$/, '') }));
|
|
return run._id;
|
|
}
|
|
|
|
async function runForAdmin(userId, runId) {
|
|
const run = await BotRuns.findOneAsync(runId);
|
|
if (!run) throw new Meteor.Error('not-found');
|
|
const board = await ReactiveCache.getBoard(run.boardId);
|
|
if (!userId || !allowIsBoardAdmin(userId, board)) throw new Meteor.Error('not-authorized');
|
|
const bot = await Integrations.findOneAsync({ _id: run.integrationId, boardId: run.boardId });
|
|
return { run, board, bot };
|
|
}
|
|
|
|
// A reply or an answer is a comment on the card - written by the person, under
|
|
// their own right to comment - and the bot is told.
|
|
async function commentAndTell(userId, runId, kind, text, payload) {
|
|
const { run, board, bot } = await runForAdmin(userId, runId);
|
|
const card = await ReactiveCache.getCard(run.cardId);
|
|
if (!card || !(await canCommentInList(userId, board, card.listId))) throw new Meteor.Error('not-authorized');
|
|
const commentId = await CardComments.insertAsync({ text, boardId: card.boardId, cardId: card._id, userId });
|
|
if (runs.isBot(bot)) await enqueue(bot, `${run._id}:${kind}:${commentId}`, runs.followUpBody(kind, run, { ...payload, text, commentId }));
|
|
if (run.status === 'awaiting_input') await BotRuns.updateAsync(run._id, { $set: { status: 'running', question: null, options: [], updatedAt: new Date() } });
|
|
return commentId;
|
|
}
|
|
|
|
Meteor.methods({
|
|
async botDispatch(cardId, integrationId) {
|
|
check(cardId, String);
|
|
check(integrationId, String);
|
|
const card = await ReactiveCache.getCard(cardId);
|
|
const board = card && await ReactiveCache.getBoard(card.boardId);
|
|
if (!this.userId || !allowIsBoardAdmin(this.userId, board)) throw new Meteor.Error('not-authorized');
|
|
return dispatchToBot({ integrationId, cardId, userId: this.userId, source: 'button' });
|
|
},
|
|
async botAnswer(runId, value) {
|
|
check(runId, String);
|
|
check(value, String);
|
|
const run = await BotRuns.findOneAsync(runId);
|
|
const option = run && (run.options || []).find(o => o.value === value);
|
|
if (!option) throw new Meteor.Error('bot-option-not-found');
|
|
return commentAndTell(this.userId, runId, 'answer', `↳ ${option.label}`, { value });
|
|
},
|
|
async botReply(runId, text) {
|
|
check(runId, String);
|
|
check(text, String);
|
|
const body = text.trim().slice(0, 65536);
|
|
if (!body) throw new Meteor.Error('bot-reply-empty');
|
|
return commentAndTell(this.userId, runId, 'reply', body, {});
|
|
},
|
|
async botStop(runId) {
|
|
check(runId, String);
|
|
const { run, bot } = await runForAdmin(this.userId, runId);
|
|
if (runs.FINAL.includes(run.status)) return run.status;
|
|
const now = new Date();
|
|
await BotRuns.updateAsync(run._id, { $set: { status: 'stopped', finishedAt: now, updatedAt: now },
|
|
$push: { events: { $each: [{ text: 'stopped', status: 'stopped', at: now }], $slice: -runs.MAX_EVENTS } } });
|
|
if (runs.isBot(bot)) await enqueue(bot, `${run._id}:stop`, runs.followUpBody('stop', run));
|
|
return 'stopped';
|
|
},
|
|
});
|
|
|
|
// The bots' runs of a board, for its admins' Bots view.
|
|
Meteor.publish('botRuns', async function botRunsPublication(boardId) {
|
|
check(boardId, String);
|
|
const board = await ReactiveCache.getBoard(boardId);
|
|
if (!this.userId || !allowIsBoardAdmin(this.userId, board)) return this.ready();
|
|
return BotRuns.find({ boardId }, { sort: { updatedAt: -1 }, limit: 300 });
|
|
});
|
|
|
|
// The runner reports: POST /api/boards/:boardId/cards/:cardId/bot-runs/:runId
|
|
// { status, text, tool, question, options, tokens, costUsd }, with a WeKan
|
|
// API token whose user may write this board - the same check as every card
|
|
// write of the REST API.
|
|
WebApp.handlers.post('/api/boards/:boardId/cards/:cardId/bot-runs/:runId', async function botRunReport(req, res) {
|
|
try {
|
|
const { boardId, cardId, runId } = req.params;
|
|
await Authentication.checkBoardWriteAccess(req.userId, boardId);
|
|
const run = await BotRuns.findOneAsync({ _id: runId, boardId, cardId });
|
|
if (!run) {
|
|
sendJsonResult(res, { code: 404, data: { error: 'Run not found' } });
|
|
return;
|
|
}
|
|
const result = runs.applyRunUpdate(run, runs.cleanRunUpdate(req.body || {}));
|
|
if (result.error) {
|
|
sendJsonResult(res, { code: 400, data: { error: result.error } });
|
|
return;
|
|
}
|
|
const { _id, ...fields } = result.run;
|
|
await BotRuns.updateAsync({ _id: runId, boardId, cardId }, { $set: { ...fields, reportedBy: req.userId } });
|
|
sendJsonResult(res, { code: 200, data: { _id: runId, status: result.run.status } });
|
|
} catch (error) {
|
|
sendJsonResult(res, publicErrorData(error));
|
|
}
|
|
});
|
|
|
|
Meteor.startup(async () => {
|
|
await ensureIndex(BotRuns, { boardId: 1, updatedAt: -1 });
|
|
await ensureIndex(BotRuns, { cardId: 1, integrationId: 1, status: 1 });
|
|
});
|