Member/Ecology revamp.
This commit is contained in:
parent
fc7ec52574
commit
59d6e97787
31 changed files with 1763 additions and 1010 deletions
|
|
@ -36,21 +36,22 @@ export async function fetchAllDocuments() {
|
|||
}
|
||||
|
||||
const documents = []
|
||||
let path = '/documents.list'
|
||||
let offset = 0
|
||||
const limit = 25
|
||||
|
||||
while (path) {
|
||||
const response = await fetch(`${OUTLINE_API_BASE}${path}`, {
|
||||
while (true) {
|
||||
const response = await fetch(`${OUTLINE_API_BASE}/documents.list`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${apiKey}`
|
||||
},
|
||||
body: JSON.stringify({ limit: 25 })
|
||||
body: JSON.stringify({ offset, limit })
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text()
|
||||
console.error(`[outline] POST ${path} ${response.status} ${errorText}`)
|
||||
console.error(`[outline] POST /documents.list offset=${offset} ${response.status} ${errorText}`)
|
||||
throw createError({
|
||||
statusCode: response.status,
|
||||
statusMessage: 'Outline API error'
|
||||
|
|
@ -58,10 +59,46 @@ export async function fetchAllDocuments() {
|
|||
}
|
||||
|
||||
const data = await response.json()
|
||||
documents.push(...(data.data || []))
|
||||
const page = data.data || []
|
||||
documents.push(...page)
|
||||
|
||||
path = data.pagination?.nextPath || null
|
||||
if (page.length < limit) break
|
||||
offset += limit
|
||||
}
|
||||
|
||||
return documents
|
||||
}
|
||||
|
||||
export async function fetchCollections() {
|
||||
const config = useRuntimeConfig()
|
||||
const apiKey = config.outlineApiKey
|
||||
|
||||
if (!apiKey) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Outline API key not configured'
|
||||
})
|
||||
}
|
||||
|
||||
const response = await fetch(`${OUTLINE_API_BASE}/collections.list`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${apiKey}`
|
||||
},
|
||||
body: JSON.stringify({ limit: 100 })
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text()
|
||||
console.error(`[outline] POST /collections.list ${response.status} ${errorText}`)
|
||||
throw createError({
|
||||
statusCode: response.status,
|
||||
statusMessage: 'Outline API error'
|
||||
})
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
const collections = data.data || []
|
||||
return new Map(collections.map(c => [c.id, c.name]))
|
||||
}
|
||||
|
|
|
|||
72
server/utils/syncWikiArticles.js
Normal file
72
server/utils/syncWikiArticles.js
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import WikiArticle from '../models/wikiArticle.js'
|
||||
import { connectDB } from './mongoose.js'
|
||||
import { fetchAllDocuments, fetchCollections, extractSummary } from './outline.js'
|
||||
|
||||
export async function syncWikiArticles() {
|
||||
const [documents, collectionMap] = await Promise.all([
|
||||
fetchAllDocuments(),
|
||||
fetchCollections()
|
||||
])
|
||||
|
||||
await connectDB()
|
||||
|
||||
const fetchedOutlineIds = new Set(documents.map((doc) => doc.id))
|
||||
|
||||
const existing = await WikiArticle.find({}, 'outlineId publishedAt')
|
||||
const existingByOutlineId = new Map(
|
||||
existing.map((a) => [a.outlineId, a])
|
||||
)
|
||||
|
||||
let created = 0
|
||||
let updated = 0
|
||||
let deleted = 0
|
||||
let errors = 0
|
||||
|
||||
for (const doc of documents) {
|
||||
try {
|
||||
// Only $set fields from Outline — tags are never touched
|
||||
const articleData = {
|
||||
title: doc.title,
|
||||
collection: collectionMap.get(doc.collectionId) || null,
|
||||
url: doc.url?.startsWith('/') ? `https://wiki.ghostguild.org${doc.url}` : doc.url,
|
||||
summary: extractSummary(doc.text),
|
||||
publishedAt: doc.publishedAt ? new Date(doc.publishedAt) : new Date(doc.createdAt),
|
||||
permission: doc.permission || null,
|
||||
lastSyncedAt: new Date(),
|
||||
outlineUpdatedAt: doc.updatedAt ? new Date(doc.updatedAt) : null
|
||||
}
|
||||
|
||||
const result = await WikiArticle.findOneAndUpdate(
|
||||
{ outlineId: doc.id },
|
||||
{ $set: articleData },
|
||||
{ upsert: true, new: true, rawResult: true }
|
||||
)
|
||||
|
||||
if (result.lastErrorObject?.updatedExisting) {
|
||||
updated++
|
||||
} else {
|
||||
created++
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[wiki-sync] Error upserting doc ${doc.id}:`, err)
|
||||
errors++
|
||||
}
|
||||
}
|
||||
|
||||
for (const [outlineId, article] of existingByOutlineId) {
|
||||
if (!fetchedOutlineIds.has(outlineId) && article.publishedAt !== null) {
|
||||
try {
|
||||
await WikiArticle.findOneAndUpdate(
|
||||
{ outlineId },
|
||||
{ $set: { publishedAt: null, lastSyncedAt: new Date() } }
|
||||
)
|
||||
deleted++
|
||||
} catch (err) {
|
||||
console.error(`[wiki-sync] Error soft-deleting ${outlineId}:`, err)
|
||||
errors++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { created, updated, deleted, errors }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue