]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/server/plugin.ts
WIP plugins: list installed plugins in client
[github/Chocobozzz/PeerTube.git] / server / models / server / plugin.ts
CommitLineData
ad91e700
C
1import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2import { getSort, throwIfNotValid } from '../utils'
345da516
C
3import {
4 isPluginDescriptionValid,
5 isPluginNameValid,
6 isPluginTypeValid,
7 isPluginVersionValid
8} from '../../helpers/custom-validators/plugins'
ad91e700
C
9import { PluginType } from '../../../shared/models/plugins/plugin.type'
10import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
11import { FindAndCountOptions } from 'sequelize'
12
13@DefaultScope(() => ({
14 attributes: {
15 exclude: [ 'storage' ]
16 }
17}))
345da516
C
18
19@Table({
20 tableName: 'plugin',
21 indexes: [
22 {
23 fields: [ 'name' ],
24 unique: true
25 }
26 ]
27})
28export class PluginModel extends Model<PluginModel> {
29
30 @AllowNull(false)
31 @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name'))
32 @Column
33 name: string
34
35 @AllowNull(false)
36 @Is('PluginType', value => throwIfNotValid(value, isPluginTypeValid, 'type'))
37 @Column
38 type: number
39
40 @AllowNull(false)
41 @Is('PluginVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
42 @Column
43 version: string
44
45 @AllowNull(false)
46 @Column
47 enabled: boolean
48
49 @AllowNull(false)
50 @Column
51 uninstalled: boolean
52
53 @AllowNull(false)
345da516
C
54 @Column
55 peertubeEngine: string
56
57 @AllowNull(true)
58 @Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description'))
59 @Column
60 description: string
61
62 @AllowNull(true)
63 @Column(DataType.JSONB)
64 settings: any
65
66 @AllowNull(true)
67 @Column(DataType.JSONB)
68 storage: any
69
70 @CreatedAt
71 createdAt: Date
72
73 @UpdatedAt
74 updatedAt: Date
75
76 static listEnabledPluginsAndThemes () {
77 const query = {
78 where: {
79 enabled: true,
80 uninstalled: false
81 }
82 }
83
84 return PluginModel.findAll(query)
85 }
86
2c053942
C
87 static load (pluginName: string) {
88 const query = {
89 where: {
90 name: pluginName
91 }
92 }
93
94 return PluginModel.findOne(query)
95 }
96
ad91e700
C
97 static getSetting (pluginName: string, settingName: string) {
98 const query = {
99 attributes: [ 'settings' ],
100 where: {
101 name: pluginName
102 }
103 }
104
105 return PluginModel.findOne(query)
106 .then(p => p.settings)
107 .then(settings => {
108 if (!settings) return undefined
109
110 return settings[settingName]
111 })
112 }
113
114 static setSetting (pluginName: string, settingName: string, settingValue: string) {
f023a19c
C
115 const query = {
116 where: {
117 name: pluginName
118 }
119 }
120
ad91e700
C
121 const toSave = {
122 [`settings.${settingName}`]: settingValue
123 }
124
125 return PluginModel.update(toSave, query)
126 .then(() => undefined)
127 }
128
129 static listForApi (options: {
130 type?: PluginType,
131 uninstalled?: boolean,
132 start: number,
133 count: number,
134 sort: string
135 }) {
136 const query: FindAndCountOptions = {
137 offset: options.start,
138 limit: options.count,
139 order: getSort(options.sort),
140 where: {}
141 }
142
143 if (options.type) query.where['type'] = options.type
144 if (options.uninstalled) query.where['uninstalled'] = options.uninstalled
145
146 return PluginModel
147 .findAndCountAll(query)
148 .then(({ rows, count }) => {
149 return { total: count, data: rows }
150 })
151 }
152
153 toFormattedJSON (): PeerTubePlugin {
154 return {
155 name: this.name,
156 type: this.type,
157 version: this.version,
158 enabled: this.enabled,
159 uninstalled: this.uninstalled,
160 peertubeEngine: this.peertubeEngine,
161 description: this.description,
162 settings: this.settings,
163 createdAt: this.createdAt,
164 updatedAt: this.updatedAt
165 }
f023a19c
C
166 }
167
345da516 168}