style: 代码格式优化

This commit is contained in:
digua
2026-04-06 21:04:05 +08:00
committed by digua
parent bfc96a723d
commit 3189de3a6e
34 changed files with 218 additions and 261 deletions
+3 -1
View File
@@ -64,7 +64,9 @@ export function generateId(): string {
return `ds_${crypto.randomBytes(6).toString('hex')}`
}
export function addDataSource(partial: Omit<DataSource, 'id' | 'createdAt' | 'lastPullAt' | 'lastStatus' | 'lastError' | 'lastNewMessages'>): DataSource {
export function addDataSource(
partial: Omit<DataSource, 'id' | 'createdAt' | 'lastPullAt' | 'lastStatus' | 'lastError' | 'lastNewMessages'>
): DataSource {
const sources = loadDataSources()
const ds: DataSource = {
...partial,
+13 -7
View File
@@ -23,7 +23,9 @@ function getTempFilePath(ext: string): string {
function cleanupTempFile(filePath: string): void {
try {
if (fs.existsSync(filePath)) fs.unlinkSync(filePath)
} catch { /* ignore */ }
} catch {
/* ignore */
}
}
function notifySessionListChanged(): void {
@@ -33,7 +35,9 @@ function notifySessionListChanged(): void {
for (const win of wins) {
win.webContents.send('api:importCompleted')
}
} catch { /* ignore */ }
} catch {
/* ignore */
}
}
function notifyPullResult(dsId: string, status: 'success' | 'error', detail: string): void {
@@ -43,7 +47,9 @@ function notifyPullResult(dsId: string, status: 'success' | 'error', detail: str
for (const win of wins) {
win.webContents.send('api:pullResult', { dsId, status, detail })
}
} catch { /* ignore */ }
} catch {
/* ignore */
}
}
/**
@@ -51,9 +57,7 @@ function notifyPullResult(dsId: string, status: 'success' | 'error', detail: str
*/
async function fetchToTempFile(ds: DataSource): Promise<string> {
return new Promise<string>((resolve, reject) => {
const url = ds.url.includes('?')
? `${ds.url}&since=${ds.lastPullAt}`
: `${ds.url}?since=${ds.lastPullAt}`
const url = ds.url.includes('?') ? `${ds.url}&since=${ds.lastPullAt}` : `${ds.url}?since=${ds.lastPullAt}`
const request = net.request(url)
@@ -140,7 +144,9 @@ async function executePull(ds: DataSource): Promise<void> {
if (result.success) {
try {
await worker.generateIncrementalSessions(ds.targetSessionId)
} catch { /* ignore */ }
} catch {
/* ignore */
}
}
} else {
result = await worker.streamImport(tempFile)
+7 -17
View File
@@ -62,11 +62,7 @@ export function getImportingStatus(): boolean {
return isImporting
}
async function handleImport(
request: FastifyRequest,
reply: FastifyReply,
sessionId?: string
): Promise<void> {
async function handleImport(request: FastifyRequest, reply: FastifyReply, sessionId?: string): Promise<void> {
if (isImporting) {
const err = importInProgress()
reply.code(err.statusCode).send(errorResponse(err))
@@ -174,12 +170,9 @@ async function handleImport(
export function registerImportRoutes(server: FastifyInstance): void {
// JSONL mode: skip fastify's default body parsing, use request.raw stream directly
server.addContentTypeParser(
'application/x-ndjson',
(_request, _payload, done) => {
done(null, undefined)
}
)
server.addContentTypeParser('application/x-ndjson', (_request, _payload, done) => {
done(null, undefined)
})
// POST /api/v1/import — Import to new session
server.post('/api/v1/import', async (request, reply) => {
@@ -187,10 +180,7 @@ export function registerImportRoutes(server: FastifyInstance): void {
})
// POST /api/v1/sessions/:id/import — Incremental import to existing session
server.post<{ Params: { id: string } }>(
'/api/v1/sessions/:id/import',
async (request, reply) => {
await handleImport(request, reply, request.params.id)
}
)
server.post<{ Params: { id: string } }>('/api/v1/sessions/:id/import', async (request, reply) => {
await handleImport(request, reply, request.params.id)
})
}
+30 -42
View File
@@ -57,24 +57,15 @@ export function registerSessionRoutes(server: FastifyInstance): void {
const keywords = keyword ? [keyword] : []
const senderIdNum = senderId ? parseInt(senderId, 10) : undefined
const result = await worker.searchMessages(
id,
keywords,
hasFilter ? filter : undefined,
limit,
offset,
senderIdNum
)
const result = await worker.searchMessages(id, keywords, hasFilter ? filter : undefined, limit, offset, senderIdNum)
return successResponse(
{
messages: result.messages,
total: result.total,
page,
limit,
totalPages: Math.ceil(result.total / limit),
}
)
return successResponse({
messages: result.messages,
total: result.total,
page,
limit,
totalPages: Math.ceil(result.total / limit),
})
})
// GET /api/v1/sessions/:id/members — Member list
@@ -117,33 +108,30 @@ export function registerSessionRoutes(server: FastifyInstance): void {
})
// POST /api/v1/sessions/:id/sql — Execute SQL (read-only)
server.post<{ Params: { id: string }; Body: { sql: string } }>(
'/api/v1/sessions/:id/sql',
async (request, reply) => {
const { id } = request.params
await ensureSession(id)
server.post<{ Params: { id: string }; Body: { sql: string } }>('/api/v1/sessions/:id/sql', async (request, reply) => {
const { id } = request.params
await ensureSession(id)
const { sql } = request.body || {}
if (!sql || typeof sql !== 'string') {
const err = sqlExecutionError('Missing sql parameter')
return reply.code(err.statusCode).send(errorResponse(err))
}
try {
const result = await worker.executeRawSQL(id, sql)
return successResponse(result)
} catch (err: any) {
const message = err.message || 'SQL execution error'
if (message.includes('SELECT') || message.includes('只读') || message.includes('readonly')) {
const apiErr = new ApiError('SQL_READONLY_VIOLATION' as any, message)
apiErr.statusCode = 400
return reply.code(400).send(errorResponse(apiErr))
}
const apiErr = sqlExecutionError(message)
return reply.code(apiErr.statusCode).send(errorResponse(apiErr))
}
const { sql } = request.body || {}
if (!sql || typeof sql !== 'string') {
const err = sqlExecutionError('Missing sql parameter')
return reply.code(err.statusCode).send(errorResponse(err))
}
)
try {
const result = await worker.executeRawSQL(id, sql)
return successResponse(result)
} catch (err: any) {
const message = err.message || 'SQL execution error'
if (message.includes('SELECT') || message.includes('只读') || message.includes('readonly')) {
const apiErr = new ApiError('SQL_READONLY_VIOLATION' as any, message)
apiErr.statusCode = 400
return reply.code(400).send(errorResponse(apiErr))
}
const apiErr = sqlExecutionError(message)
return reply.code(apiErr.statusCode).send(errorResponse(apiErr))
}
})
// GET /api/v1/sessions/:id/export — Export ChatLab Format JSON
server.get<{ Params: { id: string } }>('/api/v1/sessions/:id/export', async (request, reply) => {
+4 -1
View File
@@ -46,7 +46,10 @@ export function registerSystemRoutes(server: FastifyInstance): void {
required: ['name', 'platform', 'type'],
properties: {
name: { type: 'string' },
platform: { type: 'string', enum: ['qq', 'wechat', 'telegram', 'discord', 'line', 'whatsapp', 'instagram', 'unknown'] },
platform: {
type: 'string',
enum: ['qq', 'wechat', 'telegram', 'discord', 'line', 'whatsapp', 'instagram', 'unknown'],
},
type: { type: 'string', enum: ['group', 'private'] },
groupId: { type: 'string' },
},