All files / src/stores moderation.ts

100% Statements 47/47
100% Branches 19/19
100% Functions 7/7
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180                                                                                                                                        7x               7x 201x 201x 201x 201x 201x 201x 201x 201x   201x     13x 13x 13x 13x     11x 11x   3x 3x   13x         10x 10x 10x       6x 5x 5x 5x           4x     6x   5x         17x             10x 10x 10x       4x 4x 4x 3x             4x   6x 6x   10x       201x                                
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
import type { EntityField } from '@/stores/entities'
import type { FormFieldPlacement } from '@/composables/useEntityFieldLayout'
 
export type ModerationStatus = 'pending' | 'approved' | 'rejected'
export type ModerationVote = 'approve' | 'reject'
 
// The item as its entity's default form shows it: only the fields that form
// displays, so a reviewer sees what a card would show and nothing more.
export interface ModerationItemCard {
  fields: EntityField[]
  layout: FormFieldPlacement[]
  values: Record<string, unknown>
}
 
// The store pack a comment is about, at the version it was written on
export interface ModerationPack {
  id: string
  name: string | Record<string, string>
  description: string | Record<string, string>
  version: number
  entities: { name: string | Record<string, string>; fieldCount: number }[]
}
 
// What a reviewer sees of a moderated content — for a comment: the comment,
// the one it replies to, and what it is about: the entity and item of an
// `item-comment`, or the pack of a `pack-comment`. Never its author.
export interface ModerationView {
  comment: { id: string; body: string; createdAt: string }
  parent: { id: string; body: string | null } | null
  entity?: { id: string; name: string | Record<string, string> }
  item?: { id: string; label: string | null; card?: ModerationItemCard }
  pack?: ModerationPack
}
 
export interface ModerationTask {
  taskId: string
  subjectType: string
  createdAt: string
  view: ModerationView
}
 
export interface ModerationQuota {
  required: number
  have: number
}
 
export interface ModerationStats {
  reviewPoints: number
  reputationScore: number
  votesCast: number
  votesAgreed: number
  quota: ModerationQuota
}
 
export interface ModerationSubmission {
  taskId: string
  subjectType: string
  status: ModerationStatus
  createdAt: string
  decidedAt: string | null
  view: ModerationView
}
 
// Refusals that mean "this content is no longer yours to vote on": the task is
// dropped from the local list instead of being offered again.
const STALE_TASK_ERRORS = [
  'TASK_DECIDED',
  'ALREADY_VOTED',
  'NOT_REVIEWABLE',
  'NOT_FOUND',
  'OWN_CONTENT'
]
 
export const useModerationStore = defineStore('moderation', () => {
  const tasks = ref<ModerationTask[]>([])
  const quota = ref<ModerationQuota | null>(null)
  const stats = ref<ModerationStats | null>(null)
  const submissions = ref<ModerationSubmission[]>([])
  const submissionsNextToken = ref<string | null>(null)
  const loadingTasks = ref(false)
  const loadingSubmissions = ref(false)
  const error = ref<string | null>(null)
  // The task being voted on, so its buttons can be locked
  const voting = ref<string | null>(null)
 
  async function fetchTasks() {
    loadingTasks.value = true
    error.value = null
    try {
      const response = await api.get<{ tasks: ModerationTask[]; quota: ModerationQuota }>(
        '/v1/moderation/tasks'
      )
      tasks.value = response.data.tasks
      quota.value = response.data.quota
    } catch (err: any) {
      error.value = err.response?.data?.error || 'Failed to load contents to review'
      throw err
    } finally {
      loadingTasks.value = false
    }
  }
 
  async function fetchStats() {
    const response = await api.get<ModerationStats>('/v1/moderation/me')
    stats.value = response.data
    quota.value = response.data.quota
  }
 
  async function fetchSubmissions(more = false) {
    if (more && !submissionsNextToken.value) return
    loadingSubmissions.value = true
    try {
      const response = await api.get<{
        submissions: ModerationSubmission[]
        nextToken: string | null
      }>('/v1/moderation/submissions', {
        params: more && submissionsNextToken.value ? { nextToken: submissionsNextToken.value } : {}
      })
      submissions.value = more
        ? [...submissions.value, ...response.data.submissions]
        : response.data.submissions
      submissionsNextToken.value = response.data.nextToken
    } finally {
      loadingSubmissions.value = false
    }
  }
 
  function dropTask(taskId: string) {
    tasks.value = tasks.value.filter((task) => task.taskId !== taskId)
  }
 
  // Votes on a task. Voting earns a review point (the quota to publish), so the
  // local counters follow without a refetch. Returns the task's status after
  // the vote: `pending` while the community is still deciding.
  async function vote(taskId: string, choice: ModerationVote): Promise<ModerationStatus> {
    voting.value = taskId
    try {
      const response = await api.post<{ taskId: string; status: ModerationStatus }>(
        `/v1/moderation/tasks/${taskId}/vote`,
        { vote: choice }
      )
      dropTask(taskId)
      if (quota.value) quota.value = { ...quota.value, have: quota.value.have + 1 }
      if (stats.value) {
        stats.value = {
          ...stats.value,
          reviewPoints: stats.value.reviewPoints + 1,
          votesCast: stats.value.votesCast + 1,
          quota: { ...stats.value.quota, have: stats.value.quota.have + 1 }
        }
      }
      return response.data.status
    } catch (err: any) {
      if (STALE_TASK_ERRORS.includes(err.response?.data?.error)) dropTask(taskId)
      throw err
    } finally {
      voting.value = null
    }
  }
 
  return {
    tasks,
    quota,
    stats,
    submissions,
    submissionsNextToken,
    loadingTasks,
    loadingSubmissions,
    error,
    voting,
    fetchTasks,
    fetchStats,
    fetchSubmissions,
    vote
  }
})