O

ouhammmourachid / feedback-board

Public

Public feedback board for your product: users post ideas and bugs, upvote once each, and comment. You set the status (planned, in progress, done) from the dashboard.

  • SaaS starter
  • 0 deploys
  • 0 forks

Feedback Board: feature requests, votes and comments

Public feedback board for your product: users post ideas and bugs, upvote once each, and comment. You set the status (planned, in progress, done) from the dashboard.

What you get

  • One vote per user per post, enforced by a unique index
  • Vote counts kept on the post, so you can sort by -vote_count
  • Statuses: open, planned, in progress, done, declined (admin only)
  • Anyone can read; signed-in users post, vote and comment
  • Authors can edit their post while you haven't changed its status

Collections

posts

FieldTypeNotes
title (required)text
detailstext
category (required)selectfeature, bug, improvement
statusselectSet by an admin
author (required)relation → usersSet by a hook to the signed-in user
vote_countnumberKept by the server
ActionWho can do it
List / viewanyone
Createsigned-in users
Updateauthor = @request.auth.id && status = 'open'
Deleteauthor = @request.auth.id && status = 'open'

votes

FieldTypeNotes
post (required)relation → posts
user (required)relation → usersSet by a hook to the signed-in user
ActionWho can do it
List / viewuser = @request.auth.id
Createsigned-in users
Updatesuperusers only
Deleteuser = @request.auth.id

comments

FieldTypeNotes
post (required)relation → posts
author (required)relation → usersSet by a hook to the signed-in user
body (required)text
ActionWho can do it
List / viewanyone
Createsigned-in users
Updateauthor = @request.auth.id
Deleteauthor = @request.auth.id

Hooks

All hooks are in pb_hooks/main.pb.js and commented.

Quick start

import PocketBase from "pocketbase"
const pb = new PocketBase("https://YOUR-INSTANCE.pocketshub.com")

const top = await pb.collection("posts").getList(1, 20, { sort: "-vote_count", filter: "status != 'declined'" })

const post = await pb.collection("posts").create({ title: "Dark mode", category: "feature" })
await pb.collection("votes").create({ post: post.id })            // vote
await pb.collection("comments").create({ post: post.id, body: "+1, please!" })

// Which posts has the signed-in user voted on?
const myVotes = await pb.collection("votes").getFullList()

Notes

  • To remove a vote, delete the user's vote record. The count updates by itself.

License

MIT

posts
  • id
  • title
  • details
  • categorysingle
  • statussingle
  • authorsingle
  • vote_count
  • created
  • updated
votes
  • id
  • postsingle
  • usersingle
  • created
  • updated
comments
  • id
  • postsingle
  • authorsingle
  • body
  • created
  • updated

3 collections, 2 relations. Hover a collection to trace its relations

main.pb.js90 lines
/// <reference path="../pb_data/types.d.ts" />

// ---------------------------------------------------------------------------
// Posts (feature requests, bug reports)
// ---------------------------------------------------------------------------

// New posts start "open" with no votes, authored by the signed-in user.
onRecordCreateRequest((e) => {
  if (!e.hasSuperuserAuth()) {
    if (!e.auth) throw new UnauthorizedError("Sign in to post feedback.")
    e.record.set("author", e.auth.id)
    e.record.set("status", "open")
  }
  e.record.set("vote_count", 0)
  e.next()
}, "posts")

// Authors can edit title, details and category. Status is set by an admin (superuser).
onRecordUpdateRequest((e) => {
  if (!e.hasSuperuserAuth()) {
    const original = e.record.original()
    for (const field of ["author", "status", "vote_count"]) {
      if (original.getString(field) !== e.record.getString(field)) {
        throw new ForbiddenError(`"${field}" can't be changed here.`)
      }
    }
  }
  e.next()
}, "posts")

// ---------------------------------------------------------------------------
// Votes: one per user per post (unique index), counted onto the post
// ---------------------------------------------------------------------------

onRecordCreateRequest((e) => {
  if (!e.hasSuperuserAuth()) {
    if (!e.auth) throw new UnauthorizedError("Sign in to vote.")
    e.record.set("user", e.auth.id)
  }
  e.next()
}, "votes")

onRecordAfterCreateSuccess((e) => {
  const postId = e.record.getString("post")
  try {
    const post = e.app.findRecordById("posts", postId)
    post.set("vote_count", e.app.countRecords("votes", $dbx.hashExp({ post: postId })))
    e.app.save(post)
  } catch (err) {
    e.app.logger().warn("Vote recount failed", "post", postId, "error", String(err))
  }
  e.next()
}, "votes")

onRecordAfterDeleteSuccess((e) => {
  const postId = e.record.getString("post")
  try {
    const post = e.app.findRecordById("posts", postId)
    post.set("vote_count", e.app.countRecords("votes", $dbx.hashExp({ post: postId })))
    e.app.save(post)
  } catch (_) {
    // The post itself was deleted (votes cascade). Nothing to recount.
  }
  e.next()
}, "votes")

// ---------------------------------------------------------------------------
// Comments
// ---------------------------------------------------------------------------

onRecordCreateRequest((e) => {
  if (!e.hasSuperuserAuth()) {
    if (!e.auth) throw new UnauthorizedError("Sign in to comment.")
    e.record.set("author", e.auth.id)
  }
  e.next()
}, "comments")

onRecordUpdateRequest((e) => {
  if (!e.hasSuperuserAuth()) {
    const original = e.record.original()
    if (
      original.getString("author") !== e.record.getString("author") ||
      original.getString("post") !== e.record.getString("post")
    ) {
      throw new ForbiddenError("Only the text of a comment can be edited.")
    }
  }
  e.next()
}, "comments")