]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/server-commands/miscs/sql-command.ts
35cc2253f7d09ae2cff85637d9f13212fee29a16
[github/Chocobozzz/PeerTube.git] / shared / server-commands / miscs / sql-command.ts
1 import { QueryTypes, Sequelize } from 'sequelize'
2 import { forceNumber } from '@shared/core-utils'
3 import { AbstractCommand } from '../shared'
4
5 export class SQLCommand extends AbstractCommand {
6 private sequelize: Sequelize
7
8 deleteAll (table: string) {
9 const seq = this.getSequelize()
10
11 const options = { type: QueryTypes.DELETE }
12
13 return seq.query(`DELETE FROM "${table}"`, options)
14 }
15
16 async getVideoShareCount () {
17 const [ { total } ] = await this.selectQuery<{ total: string }>(`SELECT COUNT(*) as total FROM "videoShare"`)
18 if (total === null) return 0
19
20 return parseInt(total, 10)
21 }
22
23 async getInternalFileUrl (fileId: number) {
24 return this.selectQuery<{ fileUrl: string }>(`SELECT "fileUrl" FROM "videoFile" WHERE id = :fileId`, { fileId })
25 .then(rows => rows[0].fileUrl)
26 }
27
28 setActorField (to: string, field: string, value: string) {
29 return this.updateQuery(`UPDATE actor SET ${this.escapeColumnName(field)} = :value WHERE url = :to`, { value, to })
30 }
31
32 setVideoField (uuid: string, field: string, value: string) {
33 return this.updateQuery(`UPDATE video SET ${this.escapeColumnName(field)} = :value WHERE uuid = :uuid`, { value, uuid })
34 }
35
36 setPlaylistField (uuid: string, field: string, value: string) {
37 return this.updateQuery(`UPDATE "videoPlaylist" SET ${this.escapeColumnName(field)} = :value WHERE uuid = :uuid`, { value, uuid })
38 }
39
40 async countVideoViewsOf (uuid: string) {
41 const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' +
42 `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = :uuid`
43
44 const [ { total } ] = await this.selectQuery<{ total: number }>(query, { uuid })
45 if (!total) return 0
46
47 return forceNumber(total)
48 }
49
50 getActorImage (filename: string) {
51 return this.selectQuery<{ width: number, height: number }>(`SELECT * FROM "actorImage" WHERE filename = :filename`, { filename })
52 .then(rows => rows[0])
53 }
54
55 // ---------------------------------------------------------------------------
56
57 setPluginVersion (pluginName: string, newVersion: string) {
58 return this.setPluginField(pluginName, 'version', newVersion)
59 }
60
61 setPluginLatestVersion (pluginName: string, newVersion: string) {
62 return this.setPluginField(pluginName, 'latestVersion', newVersion)
63 }
64
65 setPluginField (pluginName: string, field: string, value: string) {
66 return this.updateQuery(
67 `UPDATE "plugin" SET ${this.escapeColumnName(field)} = :value WHERE "name" = :pluginName`,
68 { pluginName, value }
69 )
70 }
71
72 // ---------------------------------------------------------------------------
73
74 selectQuery <T extends object> (query: string, replacements: { [id: string]: string | number } = {}) {
75 const seq = this.getSequelize()
76 const options = {
77 type: QueryTypes.SELECT as QueryTypes.SELECT,
78 replacements
79 }
80
81 return seq.query<T>(query, options)
82 }
83
84 updateQuery (query: string, replacements: { [id: string]: string | number } = {}) {
85 const seq = this.getSequelize()
86 const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE, replacements }
87
88 return seq.query(query, options)
89 }
90
91 // ---------------------------------------------------------------------------
92
93 async getPlaylistInfohash (playlistId: number) {
94 const query = 'SELECT "p2pMediaLoaderInfohashes" FROM "videoStreamingPlaylist" WHERE id = :playlistId'
95
96 const result = await this.selectQuery<{ p2pMediaLoaderInfohashes: string }>(query, { playlistId })
97 if (!result || result.length === 0) return []
98
99 return result[0].p2pMediaLoaderInfohashes
100 }
101
102 // ---------------------------------------------------------------------------
103
104 setActorFollowScores (newScore: number) {
105 return this.updateQuery(`UPDATE "actorFollow" SET "score" = :newScore`, { newScore })
106 }
107
108 setTokenField (accessToken: string, field: string, value: string) {
109 return this.updateQuery(
110 `UPDATE "oAuthToken" SET ${this.escapeColumnName(field)} = :value WHERE "accessToken" = :accessToken`,
111 { value, accessToken }
112 )
113 }
114
115 async cleanup () {
116 if (!this.sequelize) return
117
118 await this.sequelize.close()
119 this.sequelize = undefined
120 }
121
122 private getSequelize () {
123 if (this.sequelize) return this.sequelize
124
125 const dbname = 'peertube_test' + this.server.internalServerNumber
126 const username = 'peertube'
127 const password = 'peertube'
128 const host = '127.0.0.1'
129 const port = 5432
130
131 this.sequelize = new Sequelize(dbname, username, password, {
132 dialect: 'postgres',
133 host,
134 port,
135 logging: false
136 })
137
138 return this.sequelize
139 }
140
141 private escapeColumnName (columnName: string) {
142 return this.getSequelize().escape(columnName)
143 .replace(/^'/, '"')
144 .replace(/'$/, '"')
145 }
146 }