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