Files
FabledCurator/frontend/src/stores/sources.js
T
bvandeusenandClaude Opus 5.5 1efece0f21
CI and images / extension-version (push) Successful in 3s
CI and images / lint (push) Successful in 3s
CI and images / extension-test (push) Successful in 20s
CI and images / frontend-build (push) Successful in 21s
CI and images / backend-lint-and-test (push) Successful in 33s
CI and images / integration (push) Successful in 2m39s
CI and images / sign-extension (push) Successful in 3s
CI and images / build-agent (push) Successful in 6s
CI and images / build-web (push) Successful in 1m43s
CI and images / smoke-web (push) Successful in 54s
CI and images / promote (push) Successful in 2s
feat: a Discord source can remove the posts it took from people outside its poster list (#4486)
"Remove posts from other posters" on a Discord source with an "Only posts by"
list previews what goes, per poster, then deletes it behind a typed token:

- posts whose record names a poster not on the list (by id, username or
  display name); posts that record no poster are left alone and counted;
- images found only on those posts; one also on a kept post stays, and a
  synthetic drop's link never keeps one;
- their attachments, and every drop that absorbed one of them, so the grouping
  sweep regroups what remains.

Preview and apply share one predicate, and a parity test holds them to it.
The post record now also saves the poster's display name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 08:34:06 -04:00

197 lines
6.9 KiB
JavaScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'
export const useSourcesStore = defineStore('sources', () => {
const api = useApi()
// keyed cache: null => all sources, number => artist_id filtered
const byArtist = ref(new Map())
const { loading, error, run } = useAsyncAction()
const scheduleStatus = ref(null)
const allSources = computed(() => byArtist.value.get(null) ?? [])
async function loadAll() {
await run(async () => {
const body = await api.get('/api/sources')
byArtist.value.set(null, body)
})
}
async function loadForArtist(artistId) {
await run(async () => {
const body = await api.get('/api/sources', { params: { artist_id: artistId } })
byArtist.value.set(artistId, body)
})
}
function _invalidate(artistId) {
byArtist.value.delete(null)
if (artistId != null) byArtist.value.delete(artistId)
}
// Patch a single updated source into every cached array IN PLACE, rather than
// invalidating + refetching the whole list. The full refetch was what locked
// the Subscriptions UI and collapsed the expanded row after every inline
// action (check / backfill / recover / toggle) — operator-flagged 2026-06-07.
// Map.set on a Vue-reactive ref(Map) triggers re-render; we hand each cache a
// NEW array so the table diffs just the one changed row.
function _patchSource(updated) {
if (!updated || updated.id == null) return
for (const [key, arr] of byArtist.value) {
const i = arr.findIndex((s) => s.id === updated.id)
if (i === -1) continue
const next = arr.slice()
next[i] = { ...arr[i], ...updated }
byArtist.value.set(key, next)
}
}
function _dropSource(id) {
for (const [key, arr] of byArtist.value) {
if (arr.some((s) => s.id === id)) {
byArtist.value.set(key, arr.filter((s) => s.id !== id))
}
}
}
async function create(payload) {
const body = await api.post('/api/sources', { body: payload })
_invalidate(payload.artist_id)
return body
}
async function update(id, patch, artistIdHint = null) {
const body = await api.patch(`/api/sources/${id}`, { body: patch })
_patchSource(body)
return body
}
async function remove(id, artistIdHint = null) {
await api.delete(`/api/sources/${id}`)
_dropSource(id)
}
async function findOrCreateArtist(name) {
const body = await api.post('/api/artists', { body: { name } })
return { artist: { id: body.id, name: body.name, slug: body.slug }, created: body.created }
}
async function autocompleteArtist(query, limit = 20) {
if (!query || !query.trim()) return []
return await api.get('/api/artists/autocomplete', { params: { q: query, limit } })
}
// #130: move a source (+ its content) to another artist. Files don't move
// (slug is immutable); the backend re-attributes source/posts/images and
// deletes the old artist if it's left empty.
async function reassign(id, targetArtistId) {
return await api.post(`/api/sources/${id}/reassign`, {
body: { target_artist_id: targetArtistId },
})
}
async function loadScheduleStatus() {
scheduleStatus.value = await api.get('/api/sources/schedule-status')
return scheduleStatus.value
}
// FC-3c: trigger a download for one source. Returns {download_event_id,status}.
const checkingIds = ref(new Set())
// force=true bypasses the platform-rate-limit cooldown gate. Single-
// source RETRY clicks pass it (operator-explicit override, useful for
// rapid auth-fix testing); bulk RETRY ALL / MaintenanceMenu retries
// leave it off so the cooldown does its preventive job.
async function checkNow(id, { force = false } = {}) {
checkingIds.value = new Set(checkingIds.value).add(id)
try {
const url = force
? `/api/sources/${id}/check?force=true`
: `/api/sources/${id}/check`
return await api.post(url)
} finally {
const next = new Set(checkingIds.value)
next.delete(id)
checkingIds.value = next
}
}
// Plan #693: start/stop a run-until-done backfill. 'start' walks the full
// post history in time-boxed chunks until it reaches the bottom (then the
// source shows backfill_state 'complete'); 'stop' cancels back to tick mode.
async function startBackfill(id, artistIdHint = null) {
const body = await api.post(`/api/sources/${id}/backfill`, { body: { action: 'start' } })
_patchSource(body)
return body
}
async function stopBackfill(id, artistIdHint = null) {
const body = await api.post(`/api/sources/${id}/backfill`, { body: { action: 'stop' } })
_patchSource(body)
return body
}
// Plan #697: arm a recovery walk — a backfill that bypasses the Patreon
// seen-ledger to re-fetch dropped-and-deleted near-dups and re-evaluate them
// under the current pHash threshold. Stop with stopBackfill (shared lifecycle).
async function recoverSource(id, artistIdHint = null) {
const body = await api.post(`/api/sources/${id}/backfill`, { body: { action: 'recover' } })
_patchSource(body)
return body
}
// #830: arm a recapture walk (native platforms) — re-grab every post's body +
// external links and localize on-disk inline images, WITHOUT re-downloading
// media. Shares the backfill lifecycle/badge; stop via stopBackfill.
async function recaptureSource(id, artistIdHint = null) {
const body = await api.post(`/api/sources/${id}/backfill`, { body: { action: 'recapture' } })
_patchSource(body)
return body
}
// #4486: a Discord source's posts by people outside its poster list.
// The preview touches nothing and hands back the token the remove must echo.
async function previewOtherPosters(id) {
return api.get(`/api/sources/${id}/discord/other-posters`)
}
async function removeOtherPosters(id, confirm) {
return api.post(`/api/sources/${id}/discord/other-posters/remove`, { body: { confirm } })
}
function sourcesByArtistGrouped() {
// returns [{artist: {id,name,slug}, sources: [...]}, ...]
const arr = byArtist.value.get(null) ?? []
const groups = new Map()
for (const s of arr) {
const key = s.artist_id
if (!groups.has(key)) {
groups.set(key, {
artist: { id: s.artist_id, name: s.artist_name, slug: s.artist_slug },
sources: [],
})
}
groups.get(key).sources.push(s)
}
return Array.from(groups.values()).sort(
(a, b) => a.artist.name.localeCompare(b.artist.name)
)
}
return {
byArtist, loading, error, scheduleStatus,
allSources,
checkingIds,
loadAll, loadForArtist,
create, update, remove,
checkNow,
startBackfill,
stopBackfill,
recoverSource,
recaptureSource,
previewOtherPosters, removeOtherPosters,
findOrCreateArtist, autocompleteArtist, reassign,
loadScheduleStatus,
sourcesByArtistGrouped,
}
})