From 503c6f440abc8f5924c38c4bd63591cb6cefacec Mon Sep 17 00:00:00 2001
From: Chocobozzz <me@florianbigard.com>
Date: Mon, 15 Jul 2019 15:41:56 +0200
Subject: [PATCH] Add peertube plugin index website models

---
 server/helpers/custom-validators/plugins.ts           |  7 +++----
 server/initializers/constants.ts                      |  2 +-
 server/middlewares/validators/config.ts               |  5 +++--
 server/middlewares/validators/users.ts                |  5 +++--
 server/models/account/user.ts                         |  4 ++--
 shared/models/plugins/peertube-plugin-index.model.ts  | 11 +++++++++++
 .../plugins/peertube-plugin-latest-version.model.ts   |  5 +++++
 shared/models/plugins/peertube-plugin-list.model.ts   | 10 ++++++++++
 8 files changed, 38 insertions(+), 11 deletions(-)
 create mode 100644 shared/models/plugins/peertube-plugin-index.model.ts
 create mode 100644 shared/models/plugins/peertube-plugin-latest-version.model.ts
 create mode 100644 shared/models/plugins/peertube-plugin-list.model.ts

diff --git a/server/helpers/custom-validators/plugins.ts b/server/helpers/custom-validators/plugins.ts
index 064af9ead8..e0a6f98a7a 100644
--- a/server/helpers/custom-validators/plugins.ts
+++ b/server/helpers/custom-validators/plugins.ts
@@ -4,7 +4,6 @@ import { PluginType } from '../../../shared/models/plugins/plugin.type'
 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
 import { PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model'
 import { isUrlValid } from './activitypub/misc'
-import { isThemeRegistered } from '../../lib/plugins/theme-utils'
 
 const PLUGINS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.PLUGINS
 
@@ -66,8 +65,8 @@ function isCSSPathsValid (css: any[]) {
   return isArray(css) && css.every(c => isSafePath(c))
 }
 
-function isThemeValid (name: string) {
-  return isPluginNameValid(name) && isThemeRegistered(name)
+function isThemeNameValid (name: string) {
+  return isPluginNameValid(name)
 }
 
 function isPackageJSONValid (packageJSON: PluginPackageJson, pluginType: PluginType) {
@@ -91,7 +90,7 @@ function isLibraryCodeValid (library: any) {
 export {
   isPluginTypeValid,
   isPackageJSONValid,
-  isThemeValid,
+  isThemeNameValid,
   isPluginHomepage,
   isPluginVersionValid,
   isPluginNameValid,
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index 4163fe49da..2d487a263d 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -171,7 +171,7 @@ const SCHEDULER_INTERVALS_MS = {
 
 // ---------------------------------------------------------------------------
 
-let CONSTRAINTS_FIELDS = {
+const CONSTRAINTS_FIELDS = {
   USERS: {
     NAME: { min: 1, max: 120 }, // Length
     DESCRIPTION: { min: 3, max: 1000 }, // Length
diff --git a/server/middlewares/validators/config.ts b/server/middlewares/validators/config.ts
index 31b1319146..9c43da1654 100644
--- a/server/middlewares/validators/config.ts
+++ b/server/middlewares/validators/config.ts
@@ -5,7 +5,8 @@ import { logger } from '../../helpers/logger'
 import { CustomConfig } from '../../../shared/models/server/custom-config.model'
 import { Emailer } from '../../lib/emailer'
 import { areValidationErrors } from './utils'
-import { isThemeValid } from '../../helpers/custom-validators/plugins'
+import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
+import { isThemeRegistered } from '../../lib/plugins/theme-utils'
 
 const customConfigUpdateValidator = [
   body('instance.name').exists().withMessage('Should have a valid instance name'),
@@ -48,7 +49,7 @@ const customConfigUpdateValidator = [
   body('followers.instance.enabled').isBoolean().withMessage('Should have a valid followers of instance boolean'),
   body('followers.instance.manualApproval').isBoolean().withMessage('Should have a valid manual approval boolean'),
 
-  body('theme.default').custom(isThemeValid).withMessage('Should have a valid theme'),
+  body('theme.default').custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking customConfigUpdateValidator parameters', { parameters: req.body })
diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts
index df7f77b840..a507afc5bf 100644
--- a/server/middlewares/validators/users.ts
+++ b/server/middlewares/validators/users.ts
@@ -28,7 +28,8 @@ import { ActorModel } from '../../models/activitypub/actor'
 import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor'
 import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels'
 import { UserRegister } from '../../../shared/models/users/user-register.model'
-import { isThemeValid } from '../../helpers/custom-validators/plugins'
+import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
+import { isThemeRegistered } from '../../lib/plugins/theme-utils'
 
 const usersAddValidator = [
   body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
@@ -207,7 +208,7 @@ const usersUpdateMeValidator = [
     .custom(isUserVideosHistoryEnabledValid).withMessage('Should have a valid videos history enabled attribute'),
   body('theme')
     .optional()
-    .custom(isThemeValid).withMessage('Should have a valid theme'),
+    .custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') })
diff --git a/server/models/account/user.ts b/server/models/account/user.ts
index 6f0b0e00f4..0041bf5770 100644
--- a/server/models/account/user.ts
+++ b/server/models/account/user.ts
@@ -52,7 +52,7 @@ import { ActorModel } from '../activitypub/actor'
 import { ActorFollowModel } from '../activitypub/actor-follow'
 import { VideoImportModel } from '../video/video-import'
 import { UserAdminFlag } from '../../../shared/models/users/user-flag.model'
-import { isThemeValid } from '../../helpers/custom-validators/plugins'
+import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
 import { getThemeOrDefault } from '../../lib/plugins/theme-utils'
 
 enum ScopeNames {
@@ -191,7 +191,7 @@ export class UserModel extends Model<UserModel> {
 
   @AllowNull(false)
   @Default(DEFAULT_THEME_NAME)
-  @Is('UserTheme', value => throwIfNotValid(value, isThemeValid, 'theme'))
+  @Is('UserTheme', value => throwIfNotValid(value, isThemeNameValid, 'theme'))
   @Column
   theme: string
 
diff --git a/shared/models/plugins/peertube-plugin-index.model.ts b/shared/models/plugins/peertube-plugin-index.model.ts
new file mode 100644
index 0000000000..2957a338d9
--- /dev/null
+++ b/shared/models/plugins/peertube-plugin-index.model.ts
@@ -0,0 +1,11 @@
+export interface PeerTubePluginIndex {
+  npmName: string
+  description: string
+  homepage: string
+  createdAt: Date
+  updatedAt: Date
+
+  popularity: number
+
+  latestVersion: string
+}
diff --git a/shared/models/plugins/peertube-plugin-latest-version.model.ts b/shared/models/plugins/peertube-plugin-latest-version.model.ts
new file mode 100644
index 0000000000..36dd3af54c
--- /dev/null
+++ b/shared/models/plugins/peertube-plugin-latest-version.model.ts
@@ -0,0 +1,5 @@
+export interface PeertubePluginLatestVersion {
+  currentPeerTubeEngine?: string,
+
+  npmNames: string[]
+}
diff --git a/shared/models/plugins/peertube-plugin-list.model.ts b/shared/models/plugins/peertube-plugin-list.model.ts
new file mode 100644
index 0000000000..5f0ecce685
--- /dev/null
+++ b/shared/models/plugins/peertube-plugin-list.model.ts
@@ -0,0 +1,10 @@
+import { PluginType } from './plugin.type'
+
+export interface PeertubePluginList {
+  start: number
+  count: number
+  sort: string
+  pluginType?: PluginType
+  currentPeerTubeEngine?: string
+  search?: string
+}
-- 
GitLab