2019-04-18 05:28:17 -04:00
|
|
|
import { QueryTypes, Sequelize } from 'sequelize'
|
2019-04-26 02:50:52 -04:00
|
|
|
import { ServerInfo } from '../server/servers'
|
2018-10-23 05:38:48 -04:00
|
|
|
|
2020-01-31 10:56:52 -05:00
|
|
|
const sequelizes: { [ id: number ]: Sequelize } = {}
|
2019-02-21 09:44:12 -05:00
|
|
|
|
2019-04-26 02:50:52 -04:00
|
|
|
function getSequelize (internalServerNumber: number) {
|
|
|
|
if (sequelizes[internalServerNumber]) return sequelizes[internalServerNumber]
|
2019-02-21 09:44:12 -05:00
|
|
|
|
2019-04-26 02:50:52 -04:00
|
|
|
const dbname = 'peertube_test' + internalServerNumber
|
2018-10-23 05:38:48 -04:00
|
|
|
const username = 'peertube'
|
|
|
|
const password = 'peertube'
|
2020-08-24 10:35:01 -04:00
|
|
|
const host = 'localhost'
|
2018-10-23 05:38:48 -04:00
|
|
|
const port = 5432
|
|
|
|
|
2019-02-21 09:44:12 -05:00
|
|
|
const seq = new Sequelize(dbname, username, password, {
|
2018-10-23 05:38:48 -04:00
|
|
|
dialect: 'postgres',
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
logging: false
|
|
|
|
})
|
2019-02-21 09:44:12 -05:00
|
|
|
|
2019-04-26 02:50:52 -04:00
|
|
|
sequelizes[internalServerNumber] = seq
|
2019-02-21 09:44:12 -05:00
|
|
|
|
|
|
|
return seq
|
2018-10-23 05:38:48 -04:00
|
|
|
}
|
|
|
|
|
2021-02-26 10:26:27 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-04-26 02:50:52 -04:00
|
|
|
function setActorField (internalServerNumber: number, to: string, field: string, value: string) {
|
|
|
|
const seq = getSequelize(internalServerNumber)
|
2018-10-23 05:38:48 -04:00
|
|
|
|
2019-04-18 05:28:17 -04:00
|
|
|
const options = { type: QueryTypes.UPDATE }
|
2018-10-23 05:38:48 -04:00
|
|
|
|
|
|
|
return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
|
|
|
|
}
|
|
|
|
|
2019-04-26 02:50:52 -04:00
|
|
|
function setVideoField (internalServerNumber: number, uuid: string, field: string, value: string) {
|
|
|
|
const seq = getSequelize(internalServerNumber)
|
2018-11-14 10:32:12 -05:00
|
|
|
|
2019-04-18 05:28:17 -04:00
|
|
|
const options = { type: QueryTypes.UPDATE }
|
2018-11-14 10:32:12 -05:00
|
|
|
|
|
|
|
return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
|
|
|
|
}
|
|
|
|
|
2019-04-26 02:50:52 -04:00
|
|
|
function setPlaylistField (internalServerNumber: number, uuid: string, field: string, value: string) {
|
|
|
|
const seq = getSequelize(internalServerNumber)
|
2019-03-19 09:13:53 -04:00
|
|
|
|
2019-04-18 05:28:17 -04:00
|
|
|
const options = { type: QueryTypes.UPDATE }
|
2019-03-19 09:13:53 -04:00
|
|
|
|
|
|
|
return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
|
|
|
|
}
|
|
|
|
|
2019-04-26 02:50:52 -04:00
|
|
|
async function countVideoViewsOf (internalServerNumber: number, uuid: string) {
|
|
|
|
const seq = getSequelize(internalServerNumber)
|
2019-04-11 11:33:36 -04:00
|
|
|
|
|
|
|
// tslint:disable
|
2020-01-31 10:56:52 -05:00
|
|
|
const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' +
|
|
|
|
`INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`
|
2019-04-11 11:33:36 -04:00
|
|
|
|
2019-04-18 05:28:17 -04:00
|
|
|
const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
|
|
|
|
const [ { total } ] = await seq.query<{ total: number }>(query, options)
|
2019-04-11 11:33:36 -04:00
|
|
|
|
|
|
|
if (!total) return 0
|
|
|
|
|
2019-04-18 05:28:17 -04:00
|
|
|
return parseInt(total + '', 10)
|
2019-04-11 11:33:36 -04:00
|
|
|
}
|
|
|
|
|
2021-04-08 05:23:45 -04:00
|
|
|
function getActorImage (internalServerNumber: number, filename: string) {
|
|
|
|
return selectQuery(internalServerNumber, `SELECT * FROM "actorImage" WHERE filename = '${filename}'`)
|
|
|
|
.then(rows => rows[0])
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:26:27 -05:00
|
|
|
function selectQuery (internalServerNumber: number, query: string) {
|
|
|
|
const seq = getSequelize(internalServerNumber)
|
|
|
|
const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
|
|
|
|
|
|
|
|
return seq.query<any>(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)
|
|
|
|
}
|
|
|
|
|
2019-04-26 02:50:52 -04:00
|
|
|
async function closeAllSequelize (servers: ServerInfo[]) {
|
|
|
|
for (const server of servers) {
|
2020-01-31 10:56:52 -05:00
|
|
|
if (sequelizes[server.internalServerNumber]) {
|
|
|
|
await sequelizes[server.internalServerNumber].close()
|
|
|
|
// eslint-disable-next-line
|
|
|
|
delete sequelizes[server.internalServerNumber]
|
2019-02-21 09:44:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 10:54:52 -05:00
|
|
|
function setPluginField (internalServerNumber: number, pluginName: string, field: string, value: string) {
|
2019-07-19 08:36:04 -04:00
|
|
|
const seq = getSequelize(internalServerNumber)
|
|
|
|
|
|
|
|
const options = { type: QueryTypes.UPDATE }
|
|
|
|
|
2021-03-11 10:54:52 -05:00
|
|
|
return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
function setPluginVersion (internalServerNumber: number, pluginName: string, newVersion: string) {
|
|
|
|
return setPluginField(internalServerNumber, pluginName, 'version', newVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
function setPluginLatestVersion (internalServerNumber: number, pluginName: string, newVersion: string) {
|
|
|
|
return setPluginField(internalServerNumber, pluginName, 'latestVersion', newVersion)
|
2019-07-19 08:36:04 -04:00
|
|
|
}
|
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
function setActorFollowScores (internalServerNumber: number, newScore: number) {
|
|
|
|
const seq = getSequelize(internalServerNumber)
|
|
|
|
|
|
|
|
const options = { type: QueryTypes.UPDATE }
|
|
|
|
|
|
|
|
return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options)
|
|
|
|
}
|
|
|
|
|
2021-03-12 09:20:46 -05:00
|
|
|
function setTokenField (internalServerNumber: number, accessToken: string, field: string, value: string) {
|
|
|
|
const seq = getSequelize(internalServerNumber)
|
|
|
|
|
|
|
|
const options = { type: QueryTypes.UPDATE }
|
|
|
|
|
|
|
|
return seq.query(`UPDATE "oAuthToken" SET "${field}" = '${value}' WHERE "accessToken" = '${accessToken}'`, options)
|
|
|
|
}
|
|
|
|
|
2018-10-23 05:38:48 -04:00
|
|
|
export {
|
2018-11-14 10:32:12 -05:00
|
|
|
setVideoField,
|
2019-03-19 09:13:53 -04:00
|
|
|
setPlaylistField,
|
2019-02-21 09:44:12 -05:00
|
|
|
setActorField,
|
2019-04-11 11:33:36 -04:00
|
|
|
countVideoViewsOf,
|
2019-07-19 08:36:04 -04:00
|
|
|
setPluginVersion,
|
2021-03-11 10:54:52 -05:00
|
|
|
setPluginLatestVersion,
|
2021-02-26 10:26:27 -05:00
|
|
|
selectQuery,
|
2021-04-08 05:23:45 -04:00
|
|
|
getActorImage,
|
2021-02-26 10:26:27 -05:00
|
|
|
deleteAll,
|
2021-03-12 09:20:46 -05:00
|
|
|
setTokenField,
|
2021-02-26 10:26:27 -05:00
|
|
|
updateQuery,
|
2019-08-06 11:19:53 -04:00
|
|
|
setActorFollowScores,
|
2021-02-26 10:26:27 -05:00
|
|
|
closeAllSequelize,
|
|
|
|
getCount
|
2018-10-23 05:38:48 -04:00
|
|
|
}
|