|
| 1 | +import { audit } from '../helpers/audit.ts' |
| 2 | +import { actorFrom, mayOnPage, unlockedFor } from './pages.ts' |
| 3 | +import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify' |
| 4 | +import type { RatingMode } from '../models/pageRatings.ts' |
| 5 | +import type { Page } from '../models/pages.ts' |
| 6 | + |
| 7 | +const pageParams = { |
| 8 | + type: 'object', |
| 9 | + properties: { |
| 10 | + siteId: { type: 'string', format: 'uuid' }, |
| 11 | + pageId: { type: 'string', format: 'uuid' } |
| 12 | + }, |
| 13 | + required: ['siteId', 'pageId'] |
| 14 | +} |
| 15 | + |
| 16 | +const ratingResponse = { |
| 17 | + description: 'How the page is rated now, and what the caller gave it', |
| 18 | + type: 'object', |
| 19 | + properties: { |
| 20 | + ok: { type: 'boolean' }, |
| 21 | + rating: { $ref: 'PageRatingSummary#' }, |
| 22 | + value: { |
| 23 | + type: 'integer', |
| 24 | + description: 'The caller’s own rating, 0 once withdrawn.' |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * The page being rated and the scale it is rated on, or a refusal. |
| 31 | + * |
| 32 | + * Rating belongs to an account — one opinion per person needs a person to hang it on — so an |
| 33 | + * anonymous request never gets as far as the page. Beyond that it is the same gate as reading: a page |
| 34 | + * somebody may not read is answered as though it were not there. Both switches have to be on, the |
| 35 | + * site's ratings mode and the page's own `allowRatings`. |
| 36 | + */ |
| 37 | +async function loadRateable( |
| 38 | + req: FastifyRequest<{ Params: { siteId: string; pageId: string } }>, |
| 39 | + reply: FastifyReply |
| 40 | +): Promise<{ userId: string; mode: RatingMode; page: Page } | null> { |
| 41 | + const actor = actorFrom(req) |
| 42 | + if (!actor) { |
| 43 | + reply.unauthorized('Rating a page requires a logged in user.') |
| 44 | + return null |
| 45 | + } |
| 46 | + const mode = WIKI.models.pageRatings.modeFor(req.params.siteId) |
| 47 | + if (!mode) { |
| 48 | + reply.forbidden('Ratings are turned off for this site.') |
| 49 | + return null |
| 50 | + } |
| 51 | + const page = await WIKI.models.pages.getPage({ |
| 52 | + siteId: req.params.siteId, |
| 53 | + id: req.params.pageId, |
| 54 | + unlocked: (id: string) => unlockedFor(req, id) |
| 55 | + }) |
| 56 | + if (!page || !mayOnPage(req, 'read:pages', page)) { |
| 57 | + reply.notFound('This page does not exist.') |
| 58 | + return null |
| 59 | + } |
| 60 | + if (!page.allowRatings) { |
| 61 | + reply.forbidden('Ratings are turned off for this page.') |
| 62 | + return null |
| 63 | + } |
| 64 | + return { userId: actor.id, mode, page } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Page Ratings API Routes |
| 69 | + * |
| 70 | + * A reader's thumbs or stars for a page. How a page is rated so far comes with the page itself |
| 71 | + * (`rating`, and the reader's own under `viewer.rating`), so there is no read route here. |
| 72 | + * |
| 73 | + * No route-level `permissions` on either: the test is `read:pages` on the page, which a group's page |
| 74 | + * rules decide and the hook in `index.ts` cannot see. |
| 75 | + */ |
| 76 | +async function routes(app: FastifyInstance) { |
| 77 | + /** |
| 78 | + * RATE A PAGE |
| 79 | + */ |
| 80 | + app.put<{ Params: { siteId: string; pageId: string }; Body: { value: number } }>( |
| 81 | + '/sites/:siteId/pages/:pageId/rating', |
| 82 | + { |
| 83 | + schema: { |
| 84 | + summary: 'Rate a page', |
| 85 | + description: |
| 86 | + 'Records the caller’s rating of the page, replacing any they gave it before. The scale is the site’s ratings mode: `1` or `-1` for thumbs, `1` to `5` for stars.\n\nNeeds a logged in user who may read the page, with ratings on for both the site and the page.', |
| 87 | + tags: ['Pages'], |
| 88 | + params: pageParams, |
| 89 | + body: { |
| 90 | + type: 'object', |
| 91 | + required: ['value'], |
| 92 | + properties: { |
| 93 | + value: { type: 'integer', minimum: -1, maximum: 5 } |
| 94 | + } |
| 95 | + }, |
| 96 | + response: { 200: ratingResponse } |
| 97 | + } |
| 98 | + }, |
| 99 | + async (req, reply) => { |
| 100 | + const target = await loadRateable(req, reply) |
| 101 | + if (!target) { |
| 102 | + return reply |
| 103 | + } |
| 104 | + const { userId, mode, page } = target |
| 105 | + if (!WIKI.models.pageRatings.isValid(mode, req.body.value)) { |
| 106 | + return reply.badRequest( |
| 107 | + mode === 'thumbs' |
| 108 | + ? 'A thumbs rating is 1 or -1.' |
| 109 | + : 'A star rating is a whole number from 1 to 5.' |
| 110 | + ) |
| 111 | + } |
| 112 | + const totals = await WIKI.models.pageRatings.rate({ |
| 113 | + pageId: page.id, |
| 114 | + userId, |
| 115 | + mode, |
| 116 | + value: req.body.value |
| 117 | + }) |
| 118 | + |
| 119 | + await audit(req, 'page', 'ratePage', { |
| 120 | + pageId: page.id, |
| 121 | + siteId: req.params.siteId, |
| 122 | + locale: page.locale, |
| 123 | + path: page.path, |
| 124 | + mode, |
| 125 | + value: req.body.value |
| 126 | + }) |
| 127 | + |
| 128 | + return { |
| 129 | + ok: true, |
| 130 | + rating: WIKI.models.pageRatings.summaryFromCache(totals, mode), |
| 131 | + value: req.body.value |
| 132 | + } |
| 133 | + } |
| 134 | + ) |
| 135 | + |
| 136 | + /** |
| 137 | + * WITHDRAW A RATING |
| 138 | + */ |
| 139 | + app.delete<{ Params: { siteId: string; pageId: string } }>( |
| 140 | + '/sites/:siteId/pages/:pageId/rating', |
| 141 | + { |
| 142 | + schema: { |
| 143 | + summary: 'Withdraw a rating', |
| 144 | + description: |
| 145 | + 'Forgets the caller’s rating of the page. A page they had not rated answers the same way, since the outcome asked for already holds.', |
| 146 | + tags: ['Pages'], |
| 147 | + params: pageParams, |
| 148 | + response: { 200: ratingResponse } |
| 149 | + } |
| 150 | + }, |
| 151 | + async (req, reply) => { |
| 152 | + const target = await loadRateable(req, reply) |
| 153 | + if (!target) { |
| 154 | + return reply |
| 155 | + } |
| 156 | + const { userId, mode, page } = target |
| 157 | + const totals = await WIKI.models.pageRatings.unrate({ pageId: page.id, userId }) |
| 158 | + |
| 159 | + await audit(req, 'page', 'unratePage', { |
| 160 | + pageId: page.id, |
| 161 | + siteId: req.params.siteId, |
| 162 | + locale: page.locale, |
| 163 | + path: page.path |
| 164 | + }) |
| 165 | + |
| 166 | + return { |
| 167 | + ok: true, |
| 168 | + rating: WIKI.models.pageRatings.summaryFromCache(totals, mode), |
| 169 | + value: 0 |
| 170 | + } |
| 171 | + } |
| 172 | + ) |
| 173 | +} |
| 174 | + |
| 175 | +export default routes |
0 commit comments