pisha-server 0.0.0-stage → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +40 -2
- package/accounts/accounts.js +754 -0
- package/accounts/index.js +1 -0
- package/accounts/passwords.js +41 -0
- package/accounts/totp.js +54 -0
- package/boards/boards.js +1087 -0
- package/boards/github.js +212 -0
- package/boards/index.js +1 -0
- package/boards/sync.js +980 -0
- package/companies.js +294 -0
- package/company-export.js +101 -0
- package/company.js +82 -0
- package/dashboard/dashboard.js +408 -0
- package/dashboard/index.js +1 -0
- package/data-upgrade.js +69 -0
- package/install/linux.sh +181 -0
- package/install/macos.sh +121 -0
- package/install/windows.bat +54 -0
- package/machines/index.js +3 -0
- package/machines/machines.js +910 -0
- package/messaging/inbox.js +252 -0
- package/messaging/index.js +3 -0
- package/messaging/messaging.js +551 -0
- package/messaging/plans.js +67 -0
- package/observation/index.js +1 -0
- package/observation/observation.js +231 -0
- package/organization/index.js +1 -0
- package/organization/organization.js +218 -0
- package/package.json +55 -4
- package/permissions/index.js +1 -0
- package/permissions/permissions.js +211 -0
- package/pisha-host.mjs +3303 -0
- package/pisha-host.mjs.sig +1 -0
- package/platform/acme.js +207 -0
- package/platform/basics.js +14 -0
- package/platform/config.js +154 -0
- package/platform/control.js +93 -0
- package/platform/credentials.js +151 -0
- package/platform/database.js +251 -0
- package/platform/errors.js +13 -0
- package/platform/files.js +50 -0
- package/platform/http.js +207 -0
- package/platform/index.js +58 -0
- package/platform/limits.js +122 -0
- package/platform/listen.js +183 -0
- package/platform/roles.js +6 -0
- package/platform/stream.js +19 -0
- package/platform/text.js +83 -0
- package/platform/ws.js +186 -0
- package/routes.js +405 -0
- package/server.js +65 -0
- package/sessions/index.js +1 -0
- package/sessions/sessions.js +533 -0
- package/ui/LICENSE-affectui.txt +26 -0
- package/ui/addmachine.js +151 -0
- package/ui/api.js +20 -0
- package/ui/app.js +143 -0
- package/ui/attention.js +78 -0
- package/ui/back.js +168 -0
- package/ui/boards.js +958 -0
- package/ui/chat.js +388 -0
- package/ui/content.js +157 -0
- package/ui/dialogs.js +135 -0
- package/ui/doing.js +112 -0
- package/ui/dom.js +6 -0
- package/ui/ds-base.css +146 -0
- package/ui/ds-components.css +229 -0
- package/ui/fonts/LICENSE-space-grotesk.txt +93 -0
- package/ui/fonts/space-grotesk-latin-700-normal.woff2 +0 -0
- package/ui/fonts/space-grotesk-latin-ext-700-normal.woff2 +0 -0
- package/ui/format.js +25 -0
- package/ui/i18n.js +2145 -0
- package/ui/icons/apple-touch-icon.png +0 -0
- package/ui/icons/icon-192.png +0 -0
- package/ui/icons/icon-512.png +0 -0
- package/ui/icons/maskable-512.png +0 -0
- package/ui/icons.js +41 -0
- package/ui/inbox.js +535 -0
- package/ui/index.html +105 -0
- package/ui/machines.js +261 -0
- package/ui/manifest.webmanifest +16 -0
- package/ui/mosaic.js +189 -0
- package/ui/nav.js +142 -0
- package/ui/newteam.js +223 -0
- package/ui/org.js +669 -0
- package/ui/overlays.css +126 -0
- package/ui/palette.js +180 -0
- package/ui/people.js +371 -0
- package/ui/permissions.js +202 -0
- package/ui/qr.js +158 -0
- package/ui/router.js +173 -0
- package/ui/routes.js +47 -0
- package/ui/screen-boards.css +241 -0
- package/ui/screen-inbox.css +201 -0
- package/ui/screen-machines.css +76 -0
- package/ui/screen-mosaic.css +26 -0
- package/ui/screen-newteam.css +90 -0
- package/ui/screen-org.css +119 -0
- package/ui/screen-session.css +99 -0
- package/ui/screen-signin.css +28 -0
- package/ui/screen-team.css +80 -0
- package/ui/screen-teams.css +41 -0
- package/ui/screen-usage.css +60 -0
- package/ui/session.js +42 -0
- package/ui/shell.css +122 -0
- package/ui/signin.js +120 -0
- package/ui/squads.js +371 -0
- package/ui/state.js +59 -0
- package/ui/stream.js +269 -0
- package/ui/styles.css +343 -0
- package/ui/team.js +115 -0
- package/ui/teams.js +58 -0
- package/ui/terminal.js +261 -0
- package/ui/theme.js +32 -0
- package/ui/timeline.js +163 -0
- package/ui/topbar.js +211 -0
- package/ui/usage.js +175 -0
- package/ui/voice-parse.js +98 -0
- package/ui/voice.js +320 -0
- package/usage/index.js +1 -0
- package/usage/usage.js +127 -0
- package/vendor/InterVariable.woff2 +0 -0
- package/vendor/LICENSE-inter +92 -0
- package/vendor/LICENSE-xterm +21 -0
- package/vendor/README.md +8 -0
- package/vendor/addon-fit.js +2 -0
- package/vendor/xterm.css +218 -0
- package/vendor/xterm.js +2 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { now, cleanText, bad, ApiError } from '../platform/index.js'
|
|
2
|
+
|
|
3
|
+
// What a coordinator says is for the person: a decision only they can take, or
|
|
4
|
+
// something they will want to know when they are back. Pisha doesn't read it
|
|
5
|
+
// or know what it is about: it keeps it where they can see it, until they say.
|
|
6
|
+
const MAX_TEXT = 2000
|
|
7
|
+
const MAX_OPTION = 200
|
|
8
|
+
const MAX_OPTIONS = 6
|
|
9
|
+
const MAX_REF = 500
|
|
10
|
+
const MAX_OPEN = 500 // what waits, sent whole (more is a squad gone wrong)
|
|
11
|
+
const LIVE_DONE = 50 // and the last ones done, with it: what a session's timeline shows
|
|
12
|
+
const PAGE_MAX = 100 // done ones asked for at a time
|
|
13
|
+
const MAX_READ_ALL = 500
|
|
14
|
+
const MAX_SEARCH = 100
|
|
15
|
+
const PER_HOUR = 30
|
|
16
|
+
const EDITS_PER_HOUR = 60
|
|
17
|
+
const MAX_ANSWER = 2000
|
|
18
|
+
|
|
19
|
+
// Lines that start "1) …", "2. …": a list. Without options, a question like
|
|
20
|
+
// that is several decisions in one, or one whose choices aren't in options.
|
|
21
|
+
const NUMBERED_RE = /^\s*\d{1,2}\s*[.)]\s+\S/gm
|
|
22
|
+
// Where each item of a list starts: "1." or "1)" at the start of a line, or
|
|
23
|
+
// "1)" after a space when it is written on one line ("… 1) ¿x? 2) ¿y?").
|
|
24
|
+
// (Not "1." mid-line: "at 19:30. Is that ok?" is no list.)
|
|
25
|
+
const ITEM_START_RE = /(?:^[ \t]*\d{1,2}[ \t]*[.)]|(?<=\s)\d{1,2}\))[ \t]+(?=\S)/gm
|
|
26
|
+
// How many items of a list ask something of their own: two or more are two
|
|
27
|
+
// or more decisions, whatever options they come with.
|
|
28
|
+
function askingItems(text) {
|
|
29
|
+
const starts = [...text.matchAll(ITEM_START_RE)]
|
|
30
|
+
if (starts.length < 2) return 0
|
|
31
|
+
return starts.filter((m, i) => text.slice(m.index, starts[i + 1]?.index ?? text.length).includes('?')).length
|
|
32
|
+
}
|
|
33
|
+
// The option a recommendation names: its number, or its words (followed, if
|
|
34
|
+
// it wants, by why: "Keep the column: nothing is lost"). -1: none of them.
|
|
35
|
+
export function recommendedIndex(options, recommended) {
|
|
36
|
+
const r = String(recommended ?? '').trim().toLowerCase()
|
|
37
|
+
if (!r || !options.length) return -1
|
|
38
|
+
if (/^\d{1,2}$/.test(r)) return Number(r) >= 1 && Number(r) <= options.length ? Number(r) - 1 : -1
|
|
39
|
+
return options.findIndex(o => { const t = o.trim().toLowerCase(); return r === t || (r.startsWith(t) && /^[\s:.,;—–-]/.test(r.slice(t.length))) })
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createInbox(platform, { sessions }) {
|
|
43
|
+
const { db, broadcast, allow, rateLimit, spendStorage, ensureRoom, quota, inTx } = platform
|
|
44
|
+
const { roleOf } = sessions
|
|
45
|
+
|
|
46
|
+
const view = r => ({
|
|
47
|
+
id: r.id, session: r.session, kind: r.kind, text: r.text, options: r.options ? JSON.parse(r.options) : [],
|
|
48
|
+
recommended: r.recommended, ref: r.ref, created_at: r.created_at, resolved_at: r.resolved_at,
|
|
49
|
+
version: r.version, updated_at: r.updated_at, answer: r.answer, answered_by: r.answered_by, closed_reason: r.closed_reason, dismissed_by: r.dismissed_by,
|
|
50
|
+
})
|
|
51
|
+
// What is open, and the last ones done: sent to every dashboard on each
|
|
52
|
+
// change. The rest of what is done is asked for a page at a time (done()).
|
|
53
|
+
function list() {
|
|
54
|
+
return [
|
|
55
|
+
...db.prepare('SELECT * FROM inbox WHERE resolved_at IS NULL ORDER BY id DESC LIMIT ?').all(MAX_OPEN),
|
|
56
|
+
...db.prepare('SELECT * FROM inbox WHERE resolved_at IS NOT NULL ORDER BY resolved_at DESC, id DESC LIMIT ?').all(LIVE_DONE),
|
|
57
|
+
].map(view)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// What is done, newest first, a page at a time. The cursor is where the last
|
|
61
|
+
// page ended ("resolved_at:id"), not a page number: what is closed meanwhile
|
|
62
|
+
// goes on top without moving what comes next. Optionally only one team's
|
|
63
|
+
// (its coordinator wrote them all), or those whose text or answer has some words.
|
|
64
|
+
const CURSOR_RE = /^(\d{1,16}):(\d{1,16})$/
|
|
65
|
+
function done({ before = null, limit = 50, team = null, q = null } = {}) {
|
|
66
|
+
const n = Math.max(1, Math.min(Number(limit) || 50, PAGE_MAX))
|
|
67
|
+
const where = ['resolved_at IS NOT NULL'], args = []
|
|
68
|
+
if (before != null && before !== '') {
|
|
69
|
+
const m = CURSOR_RE.exec(String(before))
|
|
70
|
+
if (!m) throw bad('before is the next_before of the page before')
|
|
71
|
+
where.push('(resolved_at < ? OR (resolved_at = ? AND id < ?))'); args.push(Number(m[1]), Number(m[1]), Number(m[2]))
|
|
72
|
+
}
|
|
73
|
+
if (team) { where.push('session = ?'); args.push(String(team)) }
|
|
74
|
+
const words = String(q ?? '').slice(0, MAX_SEARCH).trim()
|
|
75
|
+
if (words) {
|
|
76
|
+
const like = `%${words.replace(/[\\%_]/g, c => `\\${c}`)}%`
|
|
77
|
+
where.push("(text LIKE ? ESCAPE '\\' OR answer LIKE ? ESCAPE '\\' OR closed_reason LIKE ? ESCAPE '\\')"); args.push(like, like, like)
|
|
78
|
+
}
|
|
79
|
+
const rows = db.prepare(`SELECT * FROM inbox WHERE ${where.join(' AND ')} ORDER BY resolved_at DESC, id DESC LIMIT ?`).all(...args, n + 1)
|
|
80
|
+
const more = rows.length > n
|
|
81
|
+
const page = rows.slice(0, n)
|
|
82
|
+
const last = page.at(-1)
|
|
83
|
+
return { items: page.map(view), next_before: more && last ? `${last.resolved_at}:${last.id}` : null }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const short = (value, max, what) => {
|
|
87
|
+
const text = cleanText(String(value ?? '')).trim()
|
|
88
|
+
if (text.length > max) throw bad(`${what} is too long (max ${max} chars)`)
|
|
89
|
+
return text
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Each question belongs to the registration of its coordinator that wrote it,
|
|
93
|
+
// not to the name: a name deleted and registered again by someone else
|
|
94
|
+
// neither changes them nor gets the user's answers.
|
|
95
|
+
const epochOf = name => db.prepare('SELECT epoch FROM identities WHERE name = ?').get(name)?.epoch ?? null
|
|
96
|
+
const ownedNow = row => row.epoch != null && row.epoch === epochOf(row.session)
|
|
97
|
+
const GONE = 'its coordinator no longer exists'
|
|
98
|
+
// Open questions whose coordinator is gone (deleted, or registered again): closed.
|
|
99
|
+
function closeOrphans(name = null) {
|
|
100
|
+
const changes = db.prepare(`UPDATE inbox SET resolved_at = ?, closed_reason = ? WHERE resolved_at IS NULL ${name ? 'AND session = ?' : ''}
|
|
101
|
+
AND (epoch IS NULL OR epoch IS NOT (SELECT epoch FROM identities WHERE name = inbox.session))`).run(now(), GONE, ...(name ? [name] : [])).changes
|
|
102
|
+
if (changes) broadcast('inbox', list())
|
|
103
|
+
return changes
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Only who leads a squad, and has been approved to: nobody else speaks for it.
|
|
107
|
+
function checkWriter(name) {
|
|
108
|
+
const row = db.prepare('SELECT role, needs_approval FROM identities WHERE name = ?').get(name)
|
|
109
|
+
if (roleOf(name) !== 'coordinator' || row?.needs_approval) throw new ApiError(403, 'Forbidden: only an approved coordinator writes to the user\'s inbox')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// What a question says, checked the same way when it is written and changed.
|
|
113
|
+
function fields(p, kind) {
|
|
114
|
+
if (String(p.text ?? '').length > MAX_TEXT * 2) throw bad(`text is too long (max ${MAX_TEXT} chars)`)
|
|
115
|
+
const text = short(p.text, MAX_TEXT, 'text')
|
|
116
|
+
if (!text) throw bad('text is required')
|
|
117
|
+
const options = kind === 'ask' ? (Array.isArray(p.options) ? p.options : []).map(o => short(o, MAX_OPTION, 'an option')).filter(Boolean) : []
|
|
118
|
+
if (options.length > MAX_OPTIONS) throw bad(`at most ${MAX_OPTIONS} options`)
|
|
119
|
+
const numbered = text.match(NUMBERED_RE)?.length ?? 0
|
|
120
|
+
if (kind === 'ask' && !options.length && numbered >= 2) {
|
|
121
|
+
throw bad(`NOT SAVED: this reads like ${numbered} decisions in one (a numbered list). Ask once per decision, each with its own options and recommended, so the user can answer each on its own; put two together only when the answer to one changes the other. If they are the choices of a single decision, put them in options.`)
|
|
122
|
+
}
|
|
123
|
+
const asking = askingItems(text)
|
|
124
|
+
if (kind === 'ask' && asking >= 2) {
|
|
125
|
+
throw bad(`NOT SAVED: this asks ${asking} things in one (a numbered list of questions). Call ask_user once per decision, each with its own options and recommended, so the user can answer each on its own; put two together only when the answer to one changes the other.`)
|
|
126
|
+
}
|
|
127
|
+
let recommended = kind === 'ask' ? short(p.recommended, MAX_OPTION, 'recommended') || null : null
|
|
128
|
+
if (recommended && options.length) {
|
|
129
|
+
const n = recommendedIndex(options, recommended)
|
|
130
|
+
if (n < 0) throw bad('NOT SAVED: recommended must name one of the options: its exact words (or its number), followed, if you want, by ": why". Put any other advice in the text.')
|
|
131
|
+
// (Its number, written as the option itself: what the user reads.)
|
|
132
|
+
if (/^\d{1,2}$/.test(recommended.trim())) recommended = options[n]
|
|
133
|
+
}
|
|
134
|
+
const ref = short(p.ref, MAX_REF, 'ref') || null
|
|
135
|
+
return { text, options, recommended, ref }
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function add(name, kind, p) {
|
|
139
|
+
rateLimit(name)
|
|
140
|
+
checkWriter(name)
|
|
141
|
+
ensureRoom()
|
|
142
|
+
const f = fields(p, kind)
|
|
143
|
+
if (!allow('inbox', name, PER_HOUR, 3600_000)) throw new ApiError(429, `Too Many Requests: at most ${PER_HOUR} an hour`)
|
|
144
|
+
spendStorage('text', name, f.text.length + f.options.join('').length)
|
|
145
|
+
const info = db.prepare('INSERT INTO inbox (session, kind, text, options, recommended, ref, created_at, epoch) VALUES (?, ?, ?, ?, ?, ?, ?, ?)')
|
|
146
|
+
.run(name, kind, f.text, f.options.length ? JSON.stringify(f.options) : null, f.recommended, f.ref, now(), epochOf(name))
|
|
147
|
+
broadcast('inbox', list())
|
|
148
|
+
return { id: Number(info.lastInsertRowid) }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Its coordinator learned something that changes it: the user should never
|
|
152
|
+
// decide on what it no longer thinks. Or it no longer applies: closed, with why.
|
|
153
|
+
function edit(name, p) {
|
|
154
|
+
rateLimit(name)
|
|
155
|
+
checkWriter(name)
|
|
156
|
+
const id = Number(p.id)
|
|
157
|
+
const row = Number.isSafeInteger(id) ? db.prepare('SELECT * FROM inbox WHERE id = ? AND session = ?').get(id, name) : null
|
|
158
|
+
if (!row || !ownedNow(row)) throw bad(`nothing of yours in the user's inbox with id ${p.id}`)
|
|
159
|
+
if (row.resolved_at) throw bad(`#${id} is no longer open (${row.answer != null ? 'the user answered it' : row.dismissed_by ? 'the user dismissed it without answering: don\'t wait for an answer' : 'it was closed'}): if you still need something, ask again`)
|
|
160
|
+
if (!allow('inbox-edit', name, EDITS_PER_HOUR, 3600_000)) throw new ApiError(429, `Too Many Requests: at most ${EDITS_PER_HOUR} changes an hour`)
|
|
161
|
+
if (p.close != null) {
|
|
162
|
+
const reason = short(p.close, MAX_OPTION, 'close')
|
|
163
|
+
if (!reason) throw bad('close needs the reason it no longer applies')
|
|
164
|
+
db.prepare('UPDATE inbox SET resolved_at = ?, closed_reason = ? WHERE id = ?').run(now(), reason, id)
|
|
165
|
+
broadcast('inbox', list())
|
|
166
|
+
return { id, closed: true }
|
|
167
|
+
}
|
|
168
|
+
const old = view(row)
|
|
169
|
+
const f = fields({
|
|
170
|
+
text: p.text ?? old.text,
|
|
171
|
+
options: p.options ?? old.options,
|
|
172
|
+
recommended: p.recommended ?? old.recommended,
|
|
173
|
+
ref: p.ref ?? old.ref,
|
|
174
|
+
}, row.kind)
|
|
175
|
+
spendStorage('text', name, f.text.length + f.options.join('').length)
|
|
176
|
+
db.prepare('UPDATE inbox SET text = ?, options = ?, recommended = ?, ref = ?, version = version + 1, updated_at = ? WHERE id = ?')
|
|
177
|
+
.run(f.text, f.options.length ? JSON.stringify(f.options) : null, f.recommended, f.ref, now(), id)
|
|
178
|
+
broadcast('inbox', list())
|
|
179
|
+
return { id, version: row.version + 1 }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Everything to know the user has read, at once: only what they had in front
|
|
183
|
+
// of them (ids), not what arrived meanwhile.
|
|
184
|
+
function resolveTells(ids) {
|
|
185
|
+
if (!Array.isArray(ids) || !ids.length) throw bad('ids is required')
|
|
186
|
+
if (ids.length > MAX_READ_ALL) throw bad(`at most ${MAX_READ_ALL} at a time`)
|
|
187
|
+
const at = now()
|
|
188
|
+
const one = db.prepare("UPDATE inbox SET resolved_at = ? WHERE id = ? AND kind = 'tell' AND resolved_at IS NULL")
|
|
189
|
+
let changed = 0
|
|
190
|
+
inTx(() => { for (const id of ids) if (Number.isSafeInteger(id)) changed += one.run(at, id).changes })
|
|
191
|
+
if (changed) broadcast('inbox', list())
|
|
192
|
+
return { resolved: changed }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function resolve(id) {
|
|
196
|
+
if (!Number.isSafeInteger(id)) throw bad('id is required')
|
|
197
|
+
if (!db.prepare('UPDATE inbox SET resolved_at = ? WHERE id = ? AND resolved_at IS NULL').run(now(), id).changes) throw bad('nothing open with that id')
|
|
198
|
+
broadcast('inbox', list())
|
|
199
|
+
return { id, resolved: true }
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// The user puts a question away without answering it: closed, and the
|
|
203
|
+
// dashboard tells its coordinator not to wait for an answer.
|
|
204
|
+
function dismiss(id, by) {
|
|
205
|
+
const row = Number.isSafeInteger(id) ? db.prepare('SELECT * FROM inbox WHERE id = ?').get(id) : null
|
|
206
|
+
if (!row || row.kind !== 'ask') throw bad('no question with that id')
|
|
207
|
+
if (!row.resolved_at && !ownedNow(row)) { closeOrphans(row.session); throw new ApiError(409, `Conflict: ${GONE}: it was closed`, { parameters: { reason: 'closed' } }) }
|
|
208
|
+
if (!db.prepare('UPDATE inbox SET resolved_at = ?, dismissed_by = ? WHERE id = ? AND resolved_at IS NULL').run(now(), by, id).changes) {
|
|
209
|
+
throw new ApiError(409, 'Conflict: it is no longer open', { parameters: { reason: 'closed' } })
|
|
210
|
+
}
|
|
211
|
+
broadcast('inbox', list())
|
|
212
|
+
return { id, session: row.session, text: row.text }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// The user answers a question from the dashboard, or replies to something
|
|
216
|
+
// they were told ("update the branches with main"), as they read it: if its
|
|
217
|
+
// coordinator changed it since, they read it again first. Taken before it is
|
|
218
|
+
// typed in the coordinator's terminal (nothing changes it meanwhile), and
|
|
219
|
+
// given back if that fails.
|
|
220
|
+
function takeAnswer(p, by) {
|
|
221
|
+
const id = Number(p.id)
|
|
222
|
+
const row = Number.isSafeInteger(id) ? db.prepare('SELECT * FROM inbox WHERE id = ?').get(id) : null
|
|
223
|
+
if (!row) throw bad('nothing in the inbox with that id')
|
|
224
|
+
if (row.resolved_at) throw new ApiError(409, 'Conflict: it is no longer open', { parameters: { reason: 'closed' } })
|
|
225
|
+
if (!ownedNow(row)) { closeOrphans(row.session); throw new ApiError(409, `Conflict: ${GONE}: it was closed`, { parameters: { reason: 'closed' } }) }
|
|
226
|
+
if (Number(p.version) !== row.version) throw new ApiError(409, 'Conflict: its coordinator changed it after you read it: read it again before answering', { parameters: { reason: 'changed', version: row.version } })
|
|
227
|
+
const answer = short(p.answer, MAX_ANSWER, 'the answer')
|
|
228
|
+
if (!answer) throw bad('the answer is empty')
|
|
229
|
+
if (!db.prepare('UPDATE inbox SET resolved_at = ?, answer = ?, answered_by = ? WHERE id = ? AND resolved_at IS NULL AND version = ?').run(now(), answer, by, id, row.version).changes) {
|
|
230
|
+
throw new ApiError(409, 'Conflict: it changed while answering: read it again', { parameters: { reason: 'changed' } })
|
|
231
|
+
}
|
|
232
|
+
broadcast('inbox', list())
|
|
233
|
+
return { id, kind: row.kind, session: row.session, text: row.text, answer }
|
|
234
|
+
}
|
|
235
|
+
function giveBack(id) {
|
|
236
|
+
db.prepare('UPDATE inbox SET resolved_at = NULL, answer = NULL, answered_by = NULL WHERE id = ?').run(id)
|
|
237
|
+
broadcast('inbox', list())
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function cleanupInbox() {
|
|
241
|
+
closeOrphans()
|
|
242
|
+
// (What is done is kept as long as the company says: Settings › Company.)
|
|
243
|
+
db.prepare('DELETE FROM inbox WHERE resolved_at < ?').run(now() - quota('inbox_days', 90) * 24 * 3600_000)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const inboxMethods = {
|
|
247
|
+
askUser: (name, p) => add(name, 'ask', p),
|
|
248
|
+
tellUser: (name, p) => add(name, 'tell', { text: p.text, ref: p.ref }),
|
|
249
|
+
editInbox: (name, p) => edit(name, p),
|
|
250
|
+
}
|
|
251
|
+
return { inboxMethods, list, done, resolve, resolveTells, dismiss, takeAnswer, giveBack, cleanupInbox, closeOrphans }
|
|
252
|
+
}
|