Fusion social (spoilers, commentaires, listes partagées) + raccordement à la modération
- Signalement des commentaires et des listes partagées : copie du contenu, aperçu admin et suppression branchés dans report_content / admin_reports / admin_report_action. - Un compte bloqué ne peut plus commenter mes avis (déclencheur), et ses commentaires sont masqués côté client. - La migration défis retire à la fois tags et episodeLog des fiches publiques. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
This commit is contained in:
42 files changed
+3535
-35
No files matched your search
+2
-1
@@ -122,6 +122,7 @@ export function normalizeItem(raw: unknown): MediaItem | null {
|
||||
genres: safeStringList(r.genres, LIMITS.genres),
|
||||
platform: cleanText(r.platform, LIMITS.shortText),
|
||||
notes: cleanText(r.notes, LIMITS.notes),
|
||||
notesSpoiler: (r.notesSpoiler === true && !!cleanText(r.notes, LIMITS.notes)) || undefined,
|
||||
poster: safePosterUrl(r.poster),
|
||||
backdrop: remoteImage(r.backdrop),
|
||||
overview: cleanText(r.overview, LIMITS.overview),
|
||||
@@ -151,7 +152,7 @@ export function normalizeSettings(raw: unknown): Partial<Settings> | undefined {
|
||||
if (raw.themeMode === 'auto' || raw.themeMode === 'light' || raw.themeMode === 'dark' || raw.themeMode === 'night' || raw.themeMode === 'starfield') out.themeMode = raw.themeMode
|
||||
if (isPlainObject(raw.notifPrefs)) {
|
||||
const np: NotifPrefs = {}
|
||||
for (const k of ['episodes', 'follows', 'accepted', 'reactions'] as const) if (typeof raw.notifPrefs[k] === 'boolean') np[k] = raw.notifPrefs[k] as boolean
|
||||
for (const k of ['episodes', 'follows', 'accepted', 'reactions', 'comments', 'lists'] as const) if (typeof raw.notifPrefs[k] === 'boolean') np[k] = raw.notifPrefs[k] as boolean
|
||||
out.notifPrefs = np
|
||||
}
|
||||
const challenges = normalizeChallenges(raw.challenges)
|
||||
|
||||
@@ -1230,3 +1230,10 @@ export async function tmdbFindExternal(id: string, source: 'tvdb_id' | 'imdb_id'
|
||||
}
|
||||
|
||||
export { ANILIST, ANILIST_FIELDS, aniToResult, type AniMedia }
|
||||
/** Adresse de l'affiche TMDB d'un titre (listes partagées : la copie locale de l'affiche ne se partage pas). */
|
||||
export async function getTmdbPosterUrl(externalId: string, key: string): Promise<string | undefined> {
|
||||
if (!isSafeExternalId(externalId) || !externalId.startsWith('tmdb:')) return undefined
|
||||
const [, kind, id] = externalId.split(':') as ['tmdb', 'movie' | 'tv', string]
|
||||
const d = await tmdbFetch<{ poster_path?: string | null }>(`/${kind}/${id}`, key)
|
||||
return isSafeTmdbPath(d.poster_path) ? `${TMDB_IMG}/w342${d.poster_path}` : undefined
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import { t } from '../../i18n'
|
||||
import { cleanText, isPlainObject, safeIso } from '../security'
|
||||
import { cloudFetch } from './api'
|
||||
import { safeMediaUrl } from './social'
|
||||
|
||||
/**
|
||||
* Commentaires sous les avis. Lecture / écriture uniquement via les fonctions de la base
|
||||
* (get_review_comments, add_review_comment, delete_review_comment) qui vérifient les droits :
|
||||
* mêmes règles de visibilité que l'avis, anti-spam, suppression par l'auteur, l'auteur de l'avis ou un admin.
|
||||
*/
|
||||
|
||||
export const COMMENT_MAX = 500
|
||||
|
||||
export interface CommentAuthor {
|
||||
id: string
|
||||
username: string
|
||||
displayName: string
|
||||
avatarUrl?: string
|
||||
}
|
||||
|
||||
export interface ReviewComment {
|
||||
id: number
|
||||
body: string
|
||||
createdAt: string
|
||||
user: CommentAuthor
|
||||
mine: boolean
|
||||
canDelete: boolean
|
||||
}
|
||||
|
||||
export interface CommentThread {
|
||||
/** L'avis existe et je peux le voir */
|
||||
visible: boolean
|
||||
/** Je peux supprimer tous les commentaires (auteur de l'avis ou admin) */
|
||||
canModerate: boolean
|
||||
comments: ReviewComment[]
|
||||
}
|
||||
|
||||
/** La base n'a pas encore les commentaires (migration pas appliquée) : la fonctionnalité se masque. */
|
||||
export class CommentsUnavailable extends Error {}
|
||||
|
||||
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
||||
const USERNAME = /^[a-z0-9_]{3,20}$/
|
||||
|
||||
async function rpc(name: string, args: Record<string, unknown>): Promise<unknown> {
|
||||
const res = await cloudFetch(`/rest/v1/rpc/${name}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(args),
|
||||
})
|
||||
if (res.ok) return res.json()
|
||||
if (res.status === 404) throw new CommentsUnavailable(t('comments.unavailable'))
|
||||
const body = (await res.json().catch(() => ({}))) as { message?: string }
|
||||
const msg = typeof body.message === 'string' ? body.message : ''
|
||||
if (msg.includes('rate limit')) throw new Error(t('comments.err.rate'))
|
||||
if (msg.includes('review not found')) throw new Error(t('comments.err.gone'))
|
||||
if (msg.includes('invalid comment')) throw new Error(t('comments.err.invalid'))
|
||||
throw new Error(t('social.err.server', { status: res.status }))
|
||||
}
|
||||
|
||||
function toComment(v: unknown): ReviewComment | null {
|
||||
if (!isPlainObject(v) || typeof v.id !== 'number' || !isPlainObject(v.user)) return null
|
||||
const u = v.user
|
||||
const body = cleanText(v.body, COMMENT_MAX)
|
||||
const createdAt = safeIso(v.created_at)
|
||||
if (!body || !createdAt || typeof u.id !== 'string' || !UUID.test(u.id) || typeof u.username !== 'string' || !USERNAME.test(u.username)) return null
|
||||
return {
|
||||
id: v.id,
|
||||
body,
|
||||
createdAt,
|
||||
user: { id: u.id, username: u.username, displayName: cleanText(u.display_name, 40) ?? u.username, avatarUrl: safeMediaUrl(u.avatar_url) },
|
||||
mine: v.mine === true,
|
||||
canDelete: v.can_delete === true,
|
||||
}
|
||||
}
|
||||
|
||||
export async function getReviewComments(authorId: string, itemId: string): Promise<CommentThread> {
|
||||
if (!UUID.test(authorId)) return { visible: false, canModerate: false, comments: [] }
|
||||
const raw = await rpc('get_review_comments', { p_author: authorId, p_item: itemId })
|
||||
if (!isPlainObject(raw)) return { visible: false, canModerate: false, comments: [] }
|
||||
return {
|
||||
visible: raw.visible === true,
|
||||
canModerate: raw.can_moderate === true,
|
||||
comments: (Array.isArray(raw.comments) ? raw.comments.slice(0, 300) : []).map(toComment).filter((c): c is ReviewComment => c !== null),
|
||||
}
|
||||
}
|
||||
|
||||
export async function addReviewComment(authorId: string, itemId: string, body: string): Promise<ReviewComment> {
|
||||
const text = body.trim().slice(0, COMMENT_MAX)
|
||||
const c = toComment(await rpc('add_review_comment', { p_author: authorId, p_item: itemId, p_body: text }))
|
||||
if (!c) throw new Error(t('social.err.server', { status: 500 }))
|
||||
return c
|
||||
}
|
||||
|
||||
export async function deleteReviewComment(id: number): Promise<void> {
|
||||
await rpc('delete_review_comment', { p_id: id })
|
||||
}
|
||||
@@ -5,7 +5,15 @@ import { safeMediaUrl } from './social'
|
||||
|
||||
/** Notifications (cloche) : abonnements, demandes acceptées, réactions reçues. */
|
||||
|
||||
export type NotificationKind = 'follow_request' | 'follow_accepted' | 'new_follower' | 'reaction' | 'new_episode' | 'new_season'
|
||||
export type NotificationKind =
|
||||
| 'follow_request'
|
||||
| 'follow_accepted'
|
||||
| 'new_follower'
|
||||
| 'reaction'
|
||||
| 'new_episode'
|
||||
| 'new_season'
|
||||
| 'review_comment'
|
||||
| 'shared_list_invite'
|
||||
|
||||
export interface AppNotification {
|
||||
id: number
|
||||
@@ -19,8 +27,10 @@ export interface AppNotification {
|
||||
/** Nombre de nouveaux épisodes (épisodes) */
|
||||
ep_count: number | null
|
||||
actor: { username: string; display_name: string; avatar_url: string | null } | null
|
||||
/** Titre de la fiche concernée (réactions, épisodes) */
|
||||
/** Titre de la fiche concernée (réactions, commentaires, épisodes) ou nom de la liste partagée */
|
||||
title: string | null
|
||||
/** Invitation à une liste partagée : en attente, acceptée, ou plus valable (null) */
|
||||
invite_status: 'pending' | 'accepted' | null
|
||||
}
|
||||
|
||||
/** Épisode/saison à signaler (envoyé après la vérification des sorties). */
|
||||
@@ -54,7 +64,7 @@ async function rpc<T>(name: string, args: Record<string, unknown> = {}): Promise
|
||||
return res.json() as Promise<T>
|
||||
}
|
||||
|
||||
const KINDS: NotificationKind[] = ['follow_request', 'follow_accepted', 'new_follower', 'reaction', 'new_episode', 'new_season']
|
||||
const KINDS: NotificationKind[] = ['follow_request', 'follow_accepted', 'new_follower', 'reaction', 'new_episode', 'new_season', 'review_comment', 'shared_list_invite']
|
||||
const num = (v: unknown) => (typeof v === 'number' && Number.isFinite(v) ? v : null)
|
||||
|
||||
/** Réponse du serveur relue champ par champ (adresses d'images comprises), comme pour les profils. */
|
||||
@@ -75,6 +85,7 @@ function toNotification(v: unknown): AppNotification | null {
|
||||
ep_count: num(v.ep_count),
|
||||
actor: username ? { username, display_name: cleanText(a!.display_name, 40) ?? username, avatar_url: safeMediaUrl(a!.avatar_url) ?? null } : null,
|
||||
title: cleanText(v.title, 300) ?? null,
|
||||
invite_status: v.invite_status === 'pending' || v.invite_status === 'accepted' ? v.invite_status : null,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,10 @@ export interface TitleReview {
|
||||
updated_at: string
|
||||
rating: number | null
|
||||
notes: string
|
||||
/** L'auteur a signalé des spoilers : texte masqué jusqu'au tap */
|
||||
spoiler: boolean
|
||||
/** Nombre de commentaires sous l'avis (null : serveur sans les commentaires) */
|
||||
comments: number | null
|
||||
/** emoji -> nombre */
|
||||
reactions: Record<string, number>
|
||||
total: number
|
||||
@@ -76,6 +80,8 @@ function toReview(v: unknown): TitleReview | null {
|
||||
updated_at: safeIso(v.updated_at) ?? new Date(0).toISOString(),
|
||||
rating: typeof v.rating === 'number' && v.rating > 0 && v.rating <= 10 ? v.rating : null,
|
||||
notes,
|
||||
spoiler: v.spoiler === true,
|
||||
comments: typeof v.comments === 'number' && Number.isFinite(v.comments) ? Math.min(Math.max(0, Math.floor(v.comments)), 100000) : null,
|
||||
reactions,
|
||||
total: Object.values(reactions).reduce((a, b) => a + b, 0),
|
||||
mine: (REACTION_EMOJIS as readonly string[]).includes(v.mine as string) ? (v.mine as string) : null,
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
import { t } from '../../i18n'
|
||||
import type { MediaItem, MediaType } from '../../types'
|
||||
import { cleanText, isPlainObject, isSafeExternalId, remoteImage, safeIso } from '../security'
|
||||
import { cloudFetch } from './api'
|
||||
import { safeMediaUrl, type ProfileCard } from './social'
|
||||
|
||||
/**
|
||||
* Listes partagées / collaboratives (côté serveur, réservées aux membres).
|
||||
* Différentes des listes perso (privées, synchronisées avec la bibliothèque) : un titre y est un
|
||||
* simple instantané (référence TMDB/AniList, titre, affiche, type, année) ajouté par un membre.
|
||||
* Tout passe par les fonctions de la base, qui vérifient l'appartenance à chaque appel.
|
||||
*/
|
||||
|
||||
export interface MiniUser {
|
||||
id: string
|
||||
username: string
|
||||
displayName: string
|
||||
avatarUrl?: string
|
||||
}
|
||||
|
||||
export interface SharedListCard {
|
||||
id: string
|
||||
name: string
|
||||
isOwner: boolean
|
||||
owner: MiniUser | null
|
||||
count: number
|
||||
members: number
|
||||
posters: string[]
|
||||
/** Le titre demandé (p_external_id) est déjà dans la liste */
|
||||
has: boolean
|
||||
}
|
||||
|
||||
export interface SharedListInvite {
|
||||
id: string
|
||||
name: string
|
||||
owner: MiniUser | null
|
||||
members: number
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
export interface SharedListMember extends MiniUser {
|
||||
status: 'pending' | 'accepted'
|
||||
isOwner: boolean
|
||||
}
|
||||
|
||||
export interface SharedListItem {
|
||||
externalId: string
|
||||
title: string
|
||||
poster?: string
|
||||
type: MediaType
|
||||
year?: number
|
||||
addedAt: string
|
||||
addedBy: MiniUser | null
|
||||
}
|
||||
|
||||
export interface SharedList {
|
||||
id: string
|
||||
name: string
|
||||
isOwner: boolean
|
||||
members: SharedListMember[]
|
||||
items: SharedListItem[]
|
||||
}
|
||||
|
||||
/** Les tables / fonctions n'existent pas encore côté serveur : la fonctionnalité se masque. */
|
||||
export class SharedListsUnavailable extends Error {}
|
||||
|
||||
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
||||
const USERNAME = /^[a-z0-9_]{3,20}$/
|
||||
const TYPES = new Set<MediaType>(['film', 'serie', 'anime', 'kdrama', 'cdrama', 'autre'])
|
||||
export const SHARED_LIST_NAME_MAX = 80
|
||||
|
||||
async function rpc(name: string, args: Record<string, unknown> = {}): Promise<unknown> {
|
||||
const res = await cloudFetch(`/rest/v1/rpc/${name}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(args),
|
||||
})
|
||||
if (res.ok) return res.json()
|
||||
if (res.status === 404) throw new SharedListsUnavailable(t('shared.unavailable'))
|
||||
const body = (await res.json().catch(() => ({}))) as { message?: string }
|
||||
const msg = typeof body.message === 'string' ? body.message : ''
|
||||
if (msg.includes('rate limit')) throw new Error(t('shared.err.rate'))
|
||||
if (msg.includes('too many members')) throw new Error(t('shared.err.members'))
|
||||
if (msg.includes('too many lists')) throw new Error(t('shared.err.lists'))
|
||||
if (msg.includes('list full')) throw new Error(t('shared.err.full'))
|
||||
if (msg.includes('not connected')) throw new Error(t('shared.err.notConnected'))
|
||||
if (msg.includes('forbidden') || msg.includes('invite not found')) throw new Error(t('shared.err.forbidden'))
|
||||
throw new Error(t('social.err.server', { status: res.status }))
|
||||
}
|
||||
|
||||
function toUser(v: unknown): MiniUser | null {
|
||||
if (!isPlainObject(v) || typeof v.id !== 'string' || !UUID.test(v.id) || typeof v.username !== 'string' || !USERNAME.test(v.username)) return null
|
||||
return { id: v.id, username: v.username, displayName: cleanText(v.display_name, 40) ?? v.username, avatarUrl: safeMediaUrl(v.avatar_url) }
|
||||
}
|
||||
|
||||
const count = (v: unknown) => (typeof v === 'number' && Number.isFinite(v) && v > 0 ? Math.min(Math.floor(v), 100000) : 0)
|
||||
const listId = (v: unknown) => (typeof v === 'string' && UUID.test(v) ? v : null)
|
||||
|
||||
function toCard(v: unknown): SharedListCard | null {
|
||||
if (!isPlainObject(v)) return null
|
||||
const id = listId(v.id)
|
||||
const name = cleanText(v.name, SHARED_LIST_NAME_MAX)
|
||||
if (!id || !name) return null
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
isOwner: v.is_owner === true,
|
||||
owner: toUser(v.owner),
|
||||
count: count(v.count),
|
||||
members: count(v.members),
|
||||
posters: (Array.isArray(v.posters) ? v.posters.slice(0, 4) : []).map(remoteImage).filter((p): p is string => !!p),
|
||||
has: v.has === true,
|
||||
}
|
||||
}
|
||||
|
||||
function toInvite(v: unknown): SharedListInvite | null {
|
||||
if (!isPlainObject(v)) return null
|
||||
const id = listId(v.id)
|
||||
const name = cleanText(v.name, SHARED_LIST_NAME_MAX)
|
||||
if (!id || !name) return null
|
||||
return { id, name, owner: toUser(v.owner), members: count(v.members), createdAt: safeIso(v.created_at) ?? new Date(0).toISOString() }
|
||||
}
|
||||
|
||||
function toItem(v: unknown): SharedListItem | null {
|
||||
if (!isPlainObject(v) || !isSafeExternalId(v.external_id)) return null
|
||||
const title = cleanText(v.title, 300)
|
||||
if (!title) return null
|
||||
const year = typeof v.year === 'number' && Number.isInteger(v.year) && v.year >= 1870 && v.year <= 2200 ? v.year : undefined
|
||||
return {
|
||||
externalId: v.external_id,
|
||||
title,
|
||||
poster: remoteImage(v.poster),
|
||||
type: TYPES.has(v.type as MediaType) ? (v.type as MediaType) : 'autre',
|
||||
year,
|
||||
addedAt: safeIso(v.added_at) ?? new Date(0).toISOString(),
|
||||
addedBy: toUser(v.added_by),
|
||||
}
|
||||
}
|
||||
|
||||
function toMember(v: unknown): SharedListMember | null {
|
||||
const u = toUser(v)
|
||||
if (!u || !isPlainObject(v)) return null
|
||||
return { ...u, status: v.status === 'accepted' ? 'accepted' : 'pending', isOwner: v.is_owner === true }
|
||||
}
|
||||
|
||||
const list = <T>(v: unknown, map: (x: unknown) => T | null, max = 500): T[] => (Array.isArray(v) ? v.slice(0, max) : []).map(map).filter((x): x is T => x !== null)
|
||||
|
||||
// ─── Lecture ───
|
||||
|
||||
export async function getSharedLists(externalId?: string): Promise<{ lists: SharedListCard[]; invites: SharedListInvite[] }> {
|
||||
const raw = await rpc('get_shared_lists', { p_external_id: externalId && isSafeExternalId(externalId) ? externalId : null })
|
||||
if (!isPlainObject(raw)) return { lists: [], invites: [] }
|
||||
return { lists: list(raw.lists, toCard, 200), invites: list(raw.invites, toInvite, 100) }
|
||||
}
|
||||
|
||||
export async function getSharedList(id: string): Promise<SharedList | null> {
|
||||
if (!UUID.test(id)) return null
|
||||
const raw = await rpc('get_shared_list', { p_list: id })
|
||||
if (!isPlainObject(raw)) return null
|
||||
const name = cleanText(raw.name, SHARED_LIST_NAME_MAX)
|
||||
if (!name) return null
|
||||
return { id, name, isOwner: raw.is_owner === true, members: list(raw.members, toMember, 50), items: list(raw.items, toItem, 500) }
|
||||
}
|
||||
|
||||
/** Personnes invitables (abonnés / abonnements), filtrées par pseudo ou nom. */
|
||||
export async function inviteCandidates(id: string, q: string): Promise<ProfileCard[]> {
|
||||
if (!UUID.test(id)) return []
|
||||
const raw = await rpc('shared_list_candidates', { p_list: id, p_q: q.trim().slice(0, 40) })
|
||||
return list(raw, (v) => {
|
||||
const u = toUser(v)
|
||||
return u && isPlainObject(v) ? { ...u, isPrivate: v.is_private === true, relation: 'none' as const } : null
|
||||
}, 50)
|
||||
}
|
||||
|
||||
// ─── Écriture ───
|
||||
|
||||
export async function createSharedList(name: string): Promise<string> {
|
||||
const raw = await rpc('create_shared_list', { p_name: name.trim().slice(0, SHARED_LIST_NAME_MAX) })
|
||||
const id = isPlainObject(raw) ? listId(raw.id) : null
|
||||
if (!id) throw new Error(t('social.err.server', { status: 500 }))
|
||||
return id
|
||||
}
|
||||
|
||||
export const renameSharedList = (id: string, name: string) => rpc('rename_shared_list', { p_list: id, p_name: name.trim().slice(0, SHARED_LIST_NAME_MAX) })
|
||||
export const deleteSharedList = (id: string) => rpc('delete_shared_list', { p_list: id })
|
||||
export const inviteToSharedList = (id: string, userId: string) => rpc('invite_to_shared_list', { p_list: id, p_user: userId })
|
||||
export const respondToInvite = (id: string, accept: boolean) => rpc('respond_shared_list_invite', { p_list: id, p_accept: accept })
|
||||
export const leaveSharedList = (id: string) => rpc('leave_shared_list', { p_list: id })
|
||||
export const removeSharedListMember = (id: string, userId: string) => rpc('remove_shared_list_member', { p_list: id, p_user: userId })
|
||||
export const removeSharedListItem = (id: string, externalId: string) => rpc('remove_shared_list_item', { p_list: id, p_external_id: externalId })
|
||||
|
||||
/** Instantané minimal d'un titre (jamais d'image embarquée : seulement une adresse TMDB/AniList). */
|
||||
export interface TitleSnapshot {
|
||||
externalId: string
|
||||
title: string
|
||||
poster?: string
|
||||
type: MediaType
|
||||
year?: number
|
||||
}
|
||||
|
||||
export function snapshotOf(item: Pick<MediaItem, 'externalId' | 'title' | 'poster' | 'type' | 'year'>, posterFallback?: string): TitleSnapshot | null {
|
||||
if (!isSafeExternalId(item.externalId)) return null
|
||||
return { externalId: item.externalId, title: item.title, poster: remoteImage(item.poster) ?? remoteImage(posterFallback), type: item.type, year: item.year }
|
||||
}
|
||||
|
||||
export const addSharedListItem = (id: string, s: TitleSnapshot) =>
|
||||
rpc('add_shared_list_item', {
|
||||
p_list: id,
|
||||
p_item: { external_id: s.externalId, title: s.title.slice(0, 300), poster: s.poster ?? null, type: s.type, year: s.year ?? null },
|
||||
})
|
||||
Reference in new issue
Block a user