All files / src/components AdminReportsPanel.vue

96.72% Statements 59/61
98.14% Branches 53/54
87.5% Functions 14/16
96.49% Lines 55/57

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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262  36x 2x   2x 72x         3x                               2x                       35x           2x 2x                                               2x                     2x               2x                 2x         1x                       2x           2x                             1x                                                                         22x 22x 22x 22x 22x   22x       22x 22x 24x   22x 22x 22x     22x           3x 2x 2x         1x 1x 1x       3x       2x               2x 2x 3x 3x           3x 3x 3x   2x 3x 2x 2x   3x 1x     2x   1x   3x       22x                  
<template>
  <div class="admin-reports-panel">
    <p class="text-muted">{{ t('report.admin.description') }}</p>
 
    <ul class="nav nav-pills mb-3">
      <li v-for="option in VIEWS" :key="option" class="nav-item">
        <button
          type="button"
          class="nav-link py-1"
          :class="[{ active: view === option }, `reports-view-${option}`]"
          @click="switchView(option)"
        >
          {{ t(`report.admin.views.${option}`) }}
        </button>
      </li>
    </ul>
 
    <ErrorAlert v-if="list.error" :title="t('common.error')" :message="list.error" />
    <LoadingSpinner v-if="list.loading" />
 
    <template v-else-if="!list.error">
      <p v-if="list.items.length === 0" class="text-muted">
        {{ t(view === 'open' ? 'report.admin.empty' : 'report.admin.emptyClosed') }}
      </p>
 
      <div v-else class="table-responsive">
        <table class="table align-middle">
          <thead>
            <tr>
              <th>{{ t('report.admin.colDate') }}</th>
              <th>{{ t('report.admin.colContent') }}</th>
              <th>{{ t('report.admin.colReason') }}</th>
              <th>{{ t('report.admin.colReportedUser') }}</th>
              <th>{{ t('report.admin.colReporter') }}</th>
              <th>{{ t('report.admin.colDecision') }}</th>
            </tr>
          </thead>
          <tbody>
            <tr
              v-for="report in list.items"
              :key="report.id"
              class="report-row"
              :data-report-id="report.id"
            >
              <td class="text-nowrap align-top">{{ formatDateTime(report.createdAt) }}</td>
              <td class="report-content align-top">
                <AdminReportContent
                  :item="report"
                  :people="[report.reportedUser, report.reporter]"
                />
                <span
                  v-if="(report.targetReportCount ?? 1) > 1"
                  class="badge bg-dark mt-1 report-count"
                >
                  {{
                    t('report.admin.reportCount').replace(
                      '{count}',
                      String(report.targetReportCount)
                    )
                  }}
                </span>
                <div
                  v-if="report.previousDecision && report.status === 'open'"
                  class="small text-warning mt-1 report-reopened"
                >
                  <font-awesome-icon icon="fa-solid fa-rotate-left" class="me-1" />
                  {{ t('report.admin.reopened') }}
                </div>
              </td>
              <td class="align-top">
                <span
                  class="badge report-reason"
                  :class="report.reason === 'illegal' ? 'bg-danger' : 'bg-light text-dark border'"
                >
                  {{ t(`report.reasons.${report.reason}`) }}
                </span>
                <div v-if="report.details" class="small mt-1 report-details">
                  {{ report.details }}
                </div>
              </td>
              <td class="align-top">
                <AdminReportUserCell
                  :user="report.reportedUser"
                  :standing="report.reportedUserStanding"
                  :lifting="liftingUserId === report.reportedUser?.id"
                  @lift="liftSanctions"
                />
              </td>
              <td class="align-top">
                <AdminReportUserCell
                  :user="report.reporter"
                  :reporter-standing="report.reporterStanding"
                  :lifting="liftingUserId === report.reporter?.id"
                  @lift="liftSanctions"
                  @reporting="openReportingAction"
                />
              </td>
              <td class="align-top report-decision">
                <button
                  v-if="report.status === 'open'"
                  type="button"
                  class="btn btn-sm btn-outline-danger text-nowrap report-decide"
                  @click="deciding = report"
                >
                  <font-awesome-icon icon="fa-solid fa-gavel" class="me-1" />
                  {{ t('report.admin.decide') }}
                </button>
                <template v-else-if="report.decision">
                  <span
                    class="badge report-outcome"
                    :class="report.status === 'actioned' ? 'bg-danger' : 'bg-success'"
                  >
                    {{ t(`report.decision.outcomes.${report.decision.outcome}`) }}
                  </span>
                  <div class="small mt-1 report-decision-summary">
                    {{ decisionSummary(report) }}
                  </div>
                  <div v-if="report.decision.note" class="small text-muted report-decision-note">
                    {{ report.decision.note }}
                  </div>
                  <div class="small text-muted">
                    {{ formatDateTime(report.decision.decidedAt) }}
                  </div>
                </template>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
 
      <button
        v-if="list.nextCursor"
        type="button"
        class="btn btn-outline-secondary"
        :disabled="list.loadingMore"
        @click="list.loadMore()"
      >
        {{ t('admin.loadMore') }}
      </button>
    </template>
 
    <ReportDecisionModal :report="deciding" @close="deciding = null" @decided="onDecided" />
    <ReportingActionModal
      :user-id="reportingAction?.userId ?? null"
      :action="reportingAction?.action ?? null"
      @close="reportingAction = null"
      @done="onReportingAction"
    />
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-REPORT-004.1, UC-REPORT-005.1, UC-REPORT-008.1, UC-REPORT-010.1, UC-REPORT-011.1
import { computed, onMounted, ref } from 'vue'
import {
  useAdminStore,
  type AdminContentReport,
  type AdminReporterStanding,
  type ReportingAction
} from '@/stores/admin'
import { useToastStore } from '@/stores/toast'
import { getErrorMessage } from '@/utils/errors'
import ReportDecisionModal from '@/components/ReportDecisionModal.vue'
import ReportingActionModal from '@/components/ReportingActionModal.vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useDateFormat } from '@/composables/useDateFormat'
import { useReportDecisionSummary } from '@/composables/useReportDecisionSummary'
import LoadingSpinner from '@/components/LoadingSpinner.vue'
import ErrorAlert from '@/components/ErrorAlert.vue'
import AdminReportContent from '@/components/AdminReportContent.vue'
import AdminReportUserCell from '@/components/AdminReportUserCell.vue'
 
const adminStore = useAdminStore()
const { t } = useUILanguage()
const { formatDateTime } = useDateFormat()
const { decisionSummary: summarize } = useReportDecisionSummary()
const toastStore = useToastStore()
 
const VIEWS = ['open', 'closed'] as const
type View = (typeof VIEWS)[number]
 
// Open reports wait for a decision; closed ones keep it
const view = ref<View>('open')
const list = computed(() =>
  view.value === 'open' ? adminStore.contentReports : adminStore.closedContentReports
)
const deciding = ref<AdminContentReport | null>(null)
const liftingUserId = ref<string | null>(null)
const reportingAction = ref<{ userId: string; action: ReportingAction } | null>(null)
 
/** Every report of both lists, to update a user's rows wherever they are */
const allReports = () => [
  ...adminStore.contentReports.items,
  ...adminStore.closedContentReports.items
]
 
function switchView(next: View) {
  if (view.value === next) return
  view.value = next
  list.value.load()
}
 
// The decision closed every open report of the target: both lists changed
function onDecided() {
  deciding.value = null
  adminStore.contentReports.load()
  adminStore.closedContentReports.load()
}
 
function decisionSummary(report: AdminContentReport): string {
  return summarize(report.decision!)
}
 
function openReportingAction(userId: string, action: ReportingAction) {
  reportingAction.value = { userId, action }
}
 
// Every report of that reporter shows the warning or the suspension
function onReportingAction(
  userId: string,
  standing: Pick<AdminReporterStanding, 'reportingWarnedAt' | 'reportingSuspendedUntil'>
) {
  reportingAction.value = null
  for (const report of allReports()) {
    Eif (report.reporter?.id === userId && report.reporterStanding) {
      Object.assign(report.reporterStanding, standing)
    }
  }
}
 
async function liftSanctions(userId: string) {
  liftingUserId.value = userId
  try {
    await adminStore.liftUserSanctions(userId)
    // Every row of that user, in both lists, shows them free again
    for (const report of allReports()) {
      if (report.reportedUser?.id === userId && report.reportedUserStanding) {
        report.reportedUserStanding.restrictedUntil = null
        report.reportedUserStanding.bannedAt = null
      }
      if (report.reporter?.id === userId && report.reporterStanding) {
        report.reporterStanding.reportingSuspendedUntil = null
      }
    }
    toastStore.success(t('report.admin.liftSuccess'))
  } catch (err) {
    toastStore.error(getErrorMessage(err, t('report.admin.liftError'), t))
  } finally {
    liftingUserId.value = null
  }
}
 
onMounted(() => list.value.load())
</script>
 
<style scoped>
.report-details {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
</style>