From 74d249bc1346c7cfaac7ee49bebbebcf2a01f82a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 26 Feb 2021 16:26:27 +0100 Subject: Add ability to cleanup remote AP interactions --- shared/extra-utils/miscs/sql.ts | 39 ++++++++++++++++++++++++++++++++++++- shared/extra-utils/server/jobs.ts | 3 ++- shared/extra-utils/videos/videos.ts | 2 +- 3 files changed, 41 insertions(+), 3 deletions(-) (limited to 'shared/extra-utils') diff --git a/shared/extra-utils/miscs/sql.ts b/shared/extra-utils/miscs/sql.ts index e68812e1b..740f0c2d6 100644 --- a/shared/extra-utils/miscs/sql.ts +++ b/shared/extra-utils/miscs/sql.ts @@ -24,6 +24,25 @@ function getSequelize (internalServerNumber: number) { return seq } +function deleteAll (internalServerNumber: number, table: string) { + const seq = getSequelize(internalServerNumber) + + const options = { type: QueryTypes.DELETE } + + return seq.query(`DELETE FROM "${table}"`, options) +} + +async function getCount (internalServerNumber: number, table: string) { + const seq = getSequelize(internalServerNumber) + + const options = { type: QueryTypes.SELECT as QueryTypes.SELECT } + + const [ { total } ] = await seq.query<{ total: string }>(`SELECT COUNT(*) as total FROM "${table}"`, options) + if (total === null) return 0 + + return parseInt(total, 10) +} + function setActorField (internalServerNumber: number, to: string, field: string, value: string) { const seq = getSequelize(internalServerNumber) @@ -63,6 +82,20 @@ async function countVideoViewsOf (internalServerNumber: number, uuid: string) { return parseInt(total + '', 10) } +function selectQuery (internalServerNumber: number, query: string) { + const seq = getSequelize(internalServerNumber) + const options = { type: QueryTypes.SELECT as QueryTypes.SELECT } + + return seq.query(query, options) +} + +function updateQuery (internalServerNumber: number, query: string) { + const seq = getSequelize(internalServerNumber) + const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE } + + return seq.query(query, options) +} + async function closeAllSequelize (servers: ServerInfo[]) { for (const server of servers) { if (sequelizes[server.internalServerNumber]) { @@ -95,6 +128,10 @@ export { setActorField, countVideoViewsOf, setPluginVersion, + selectQuery, + deleteAll, + updateQuery, setActorFollowScores, - closeAllSequelize + closeAllSequelize, + getCount } diff --git a/shared/extra-utils/server/jobs.ts b/shared/extra-utils/server/jobs.ts index 97971f960..704929bd4 100644 --- a/shared/extra-utils/server/jobs.ts +++ b/shared/extra-utils/server/jobs.ts @@ -63,6 +63,7 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { else servers = serversArg as ServerInfo[] const states: JobState[] = [ 'waiting', 'active', 'delayed' ] + const repeatableJobs = [ 'videos-views', 'activitypub-cleaner' ] let pendingRequests: boolean function tasksBuilder () { @@ -79,7 +80,7 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { count: 10, sort: '-createdAt' }).then(res => res.body.data) - .then((jobs: Job[]) => jobs.filter(j => j.type !== 'videos-views')) + .then((jobs: Job[]) => jobs.filter(j => !repeatableJobs.includes(j.type))) .then(jobs => { if (jobs.length !== 0) { pendingRequests = true diff --git a/shared/extra-utils/videos/videos.ts b/shared/extra-utils/videos/videos.ts index 0b6a54046..67fe82d41 100644 --- a/shared/extra-utils/videos/videos.ts +++ b/shared/extra-utils/videos/videos.ts @@ -498,7 +498,7 @@ function updateVideo ( }) } -function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = HttpStatusCode.NO_CONTENT_204) { +function rateVideo (url: string, accessToken: string, id: number | string, rating: string, specialStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/videos/' + id + '/rate' return request(url) -- cgit v1.2.3