All files / src/services contentReports.ts

100% Statements 13/13
100% Branches 7/7
100% Functions 3/3
100% Lines 13/13

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                11x     11x                   6x 6x   2x       2x   2x                         2x 2x       2x     11x                                 2x         2x    
/**
 * Reporting a comment, a direct message conversation or a store pack to the
 * platform admins, who read the reports on the Administration page's Reports
 * tab, and appealing their decisions. See docs/architecture/MODERATION.md,
 * "Reports" and "Appeals".
 */
import api from '@/services/api'
 
export const REPORT_REASONS = ['spam', 'harassment', 'inappropriate', 'illegal', 'other'] as const
export type ReportReason = (typeof REPORT_REASONS)[number]
 
export const MAX_REPORT_DETAILS_LENGTH = 1000
 
/** What is reported: a comment lives on an item's thread or on a pack's. */
export type ReportTarget =
  | { type: 'comment'; commentId: string; entityId: string; itemId: string }
  | { type: 'comment'; commentId: string; packId: string }
  | { type: 'conversation'; conversationId: string }
  | { type: 'pack'; packId: string }
 
export function reportPath(target: ReportTarget): string {
  const id = encodeURIComponent
  switch (target.type) {
    case 'comment':
      return 'packId' in target
        ? `/v1/store/${id(target.packId)}/comments/${id(target.commentId)}/report`
        : `/v1/entities/${id(target.entityId)}/items/${id(target.itemId)}/comments/${id(target.commentId)}/report`
    case 'conversation':
      return `/v1/conversations/${id(target.conversationId)}/report`
    case 'pack':
      return `/v1/store/${id(target.packId)}/report`
  }
}
 
/**
 * Files the report. Resolves with `alreadyReported: true` when the caller had
 * reported the same thing before: the first report stands.
 */
export async function submitReport(
  target: ReportTarget,
  reason: ReportReason,
  details: string
): Promise<{ alreadyReported: boolean }> {
  const trimmed = details.trim()
  const response = await api.post<{ alreadyReported: boolean }>(reportPath(target), {
    reason,
    ...(trimmed && { details: trimmed })
  })
  return { alreadyReported: response.data.alreadyReported === true }
}
 
export const MAX_APPEAL_MESSAGE_LENGTH = 2000
 
/** The decision an appeal names, as its notification carries it */
export interface AppealRef {
  targetKey: string
  decisionId: string
}
 
/**
 * Appeals a platform admin's decision on reports (UC-REPORT-009.1): its
 * author, or a reporter of a dismissed report. Resolves with
 * `alreadyAppealed: true` when the caller had appealed it before.
 */
export async function submitAppeal(
  ref: AppealRef,
  message: string
): Promise<{ alreadyAppealed: boolean }> {
  const response = await api.post<{ alreadyAppealed: boolean }>('/v1/content-reports/appeals', {
    targetKey: ref.targetKey,
    decisionId: ref.decisionId,
    message: message.trim()
  })
  return { alreadyAppealed: response.data.alreadyAppealed === true }
}