tickets
All repositories: gitoria
6.4 KB
// lib/comments.hl — COMMENTS AFTER THEY ARE WRITTEN: edit, delete, like (mission 016, layouts#2 — the ticket page draws the// comments with the shared Comments block, components/comments/). Statics only, the server realm.// A comment itself is a history event (lib/events.hl, kind 'comment', APPEND ONLY: its first text stays there). What// happened to it afterwards is kept here:// changesTable pk @id index @event APPEND ONLY: { event (events @id of the comment), ticket (tickets @id), kind ('edit' |// 'delete'), user (users @id), text (edit: the NEW text; delete: ''), created }// likesTable pk @id index @event !key { event, ticket, user (users @id), key (event + '/' + user), created }// one row per user and comment; liking again takes it back (the row goes)// THE COMMENT AS SHOWN: the event's text, replaced by the newest edit; its EARLIER VERSIONS are the event's text and every edit// but the newest, oldest first (the page folds them out under "edited"). A DELETED comment keeps its place in the history as one// line "commented (deleted <when>)" — so a state change that answers it still reads in order — but its text is gone from pages,// pushes, the JSON API and the Markdown view (the stored texts stay: append only).// WHO MAY: edit / delete = the comment's author (its `user`) while a member of the project; like = any member. A comment from// before the login (no `user`) cannot be changed by anybody. lib/tickets.hl changeComment finds the ticket and the event and calls// these with the records; the faces (components/ticket.hl) and the API (lib/api.hl) call that.// Nothing here goes to the event stream for programs (lib/stream.hl): edits, deletions and likes are no history events.import { MPackDB } from 'hl:mpackdb'import { now } from 'hl:time'import { storageDir, countOf, clean, localStamp, oldestFirst } from './util.hl'import { isMember, userOk, authorMissing } from './projects.hl'import { dropFiles } from './attachments.hl'static changesTable = new MPackDB(file = storageDir + '/comment-changes.db', primaryKey = '@id', indexes = ['@event'])static likesTable = new MPackDB(file = storageDir + '/comment-likes.db', primaryKey = '@id', indexes = ['@event', '!key'])// ---- reading -------------------------------------------------------------------------------------------// what became of the comment event `e` (a record): { text (as shown now), edited, editedMs, earlier ([{ when, createdMs,// text }], oldest first), deleted, deletedMs, likes (a number), likedBy (users @ids) }static commentStateOf = (e) => {changes = oldestFirst(rowsOf(changesTable.find('event', e.id)), 'created')let text = e.text != null ? e.text : ''let at = e.createdearlier = []let editedMs = 0let deletedMs = 0for (c of changes) {if (c.kind == 'edit' && deletedMs == 0) {earlier.push({ when = localStamp(at) createdMs = at text = text })text = c.textat = c.creatededitedMs = c.created}if (c.kind == 'delete' && deletedMs == 0) { deletedMs = c.created }}likers = likersOf(e.id)return { text = deletedMs > 0 ? '' : text edited = editedMs > 0 && deletedMs == 0 editedMs = editedMs earlier = deletedMs > 0 ? [] : earlier deleted = deletedMs > 0 deletedMs = deletedMs likes = deletedMs > 0 ? 0 : likers.length likedBy = deletedMs > 0 ? [] : likers }}static rowsOf = (found) => {out = []if (countOf(found) == 0) { return out }for (r of found) { out.push(r) }return out}// ---- who may -------------------------------------------------------------------------------------------// `e` = the comment event, `t` = its ticket (records). Answers null (fine) or { error, field, forbidden? }.static mayChange = (e, t, user, what) => {if (!userOk(user)) { return authorMissing }if (e.kind != 'comment') { return { error = 'that history entry is not a comment' field = 'comment' } }if (commentStateOf(e).deleted) { return { error = 'that comment was deleted' field = 'comment' } }if (!isMember(t.project, user)) { return { error = 'only a member of the project can ' + what + ' a comment' field = '' forbidden = true } }return null}static mayChangeOwn = (e, t, user, what) => {bad = mayChange(e, t, user, what)if (bad != null) { return bad }if (e.user == null || e.user != user.id) { return { error = 'only its author can ' + what + ' a comment' field = '' forbidden = true } }return null}// ---- writing: each answers null (done) or { error, field, forbidden? } ---------------------------------------// EDIT: a new text (Markdown, not empty, not the same)static editComment = (e, t, user, text) => {bad = mayChangeOwn(e, t, user, 'edit')if (bad != null) { return bad }body = clean(text)if (body == '') { return { error = 'a comment needs text' field = 'text' } }if (body == commentStateOf(e).text) { return { error = 'nothing changed' field = 'text' } }id = changesTable.put({ event = e.id ticket = t.id kind = 'edit' user = user.id text = body created = now() })if (id == null) { return { error = 'could not store the edit: ' + changesTable.lastError() field = '' } }return null}// DELETE: the comment leaves the pages and the API; its line stays in the history. Its attachments (tickets#27) are dropped: their// bytes leave storage, the address answers 404 (an EDIT keeps them — it changes the text only)static deleteComment = (e, t, user) => {bad = mayChangeOwn(e, t, user, 'delete')if (bad != null) { return bad }id = changesTable.put({ event = e.id ticket = t.id kind = 'delete' user = user.id text = '' created = now() })if (id == null) { return { error = 'could not store the deletion: ' + changesTable.lastError() field = '' } }dropFiles(e.id)return null}// LIKE: one per user; a second like takes it back. Answers null or the refusal; `liked` = likedBy after itstatic likeComment = (e, t, user) => {bad = mayChange(e, t, user, 'like')if (bad != null) { return bad }key = e.id + '/' + user.idfound = rowsOf(likesTable.find('key', key))if (found.length > 0) {for (l of found) { likesTable.delete(l.id) }return null}id = likesTable.put({ event = e.id ticket = t.id user = user.id key = key created = now() })if (id == null) { return { error = 'could not store the like: ' + likesTable.lastError() field = '' } }return null}// who likes a comment (users @ids; the page works out "liked" for the user looking)static likersOf = (eventId) => {out = []for (l of rowsOf(likesTable.find('event', eventId))) { out.push(l.user) }return out}
Branches
Latest commits
- 1206404fREADME attachments, report 018, LOG/STATUSmre
- 3d9e7f3ctests: m017 gate fixes (list count, waits before delete clicks); gate 373/0mre
- dd41d5bcre-sync components 15be2ac; mission 018mre
- 420ad1bbtickets (mission 017 WIP, tickets#27): comment attachments — lib/attachments.hl (storage/attachments, limits, type by content, members-only GET /api/attachments/:id, delete drops, edit keeps), multipart POST …/comments (lib/multipart.hl), Respond block in FilesUpload (blocks verbatim from components w27c 1f8d79d), gate m017 checks written (not yet run); report, LOG, STATUSmre
- a7a1380dmission brief 017mre
- f83d9c0dtickets (mission 016): report reports/016-comments-block.md, LOG, STATUS, README gate countmre
- eef80f55tickets (mission 016): comments through the shared Comments block (layouts#2) — edit (author, earlier versions kept), delete (author, asks; leaves one line in the history), like (members, toggle) on the server (lib/comments.hl, append-only tables), faces + API …/comments/:comment/{edit,delete,like}, push commentChanged; every run of comments is a block in place in the history; Markdown fields on the Hybriel MdEditor (shared/md-editor.js retired: it took over the block's <md-editor>); blocks copied verbatim from components.hybriel 9db927d (tools/sync-components.sh); gates browser 351/0 (old 326/0), events 52/0 (old 52/0)mre
- eb7aeeb9mission brief 016mre
- e4c34759paths: projects moved into /media/STORAGE/projects/WorldAPI/mre
- 26efb23freport w6-events (events test)mre
- c6bfab04mission 015: re-vendor Hybriel 0f396652 (static build), reportmre
- b7ec5981mission brief 015mre
- 5dc68731docker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
- 8a460b16mission 014: Apollo's 'stream opened right after the start gets no live events' is not a tickets bug — events.mjs checks early streamsmre
- 6309ca81mission brief 014mre
- c1161469mission 013: GitHub pipe tables on ticket pages (lib/markdown.hl + View + CSS) and re-vendor hybriel master a832f73bmre
- a30b5d12mission brief (w13)mre
- b50a6f93w9 (tickets): re-vendor hybriel 9f912f3b (#146 emit in a lib static, #147) — the 12 live-push emits in lib/api.hl compile as they are; cold start (#148): tests wait TICKETS_COLD_MS for every start, deploy.sh gates browser + events and waits up to 120 s for the URL, container /tmp tmpfs + hybriel-cache volume; events-client stops at EVENTS_MAX inside a chunk; connect.mjs ports by env. bin/hybriel sha256 7421907868ac07d8a7d08296b083c6f8b20407d0e099ce4bebbf7ae053d9060a. Gates (8720–8739): browser 315/0 (+ all-cold 315/0), events 48/0 cold, connect 60/0, short-id 19/0; real-data copy old 0e8a41c vs new: 2,425 reads + 72 writes identical after masking ?v hashes, offline at, route order; event stream on the copy delivers. Not deployed.mre
- 43be6578w9: re-vendor hybriel master edfd400a; event stream imports sseStart/sseSend/sseAlive from hl:http1 (hybriel#144), useTransport gone; tools/import.hl plain declarations. BLOCKED: hybriel-native refuses `emit client` in a static (lib/api.hl) — the server does not compile on edfd400a, gates fail at start; scratch copy without those emits: events 48/0, gate 254 ok up to the first live pushmre
- 0e8a41caw6 (ticket #26): event stream for programs — GET /api/events (Server-Sent Events)mre