aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-12 17:53:50 +0100
committerChocobozzz <me@florianbigard.com>2017-12-13 16:50:33 +0100
commit3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch)
treee5ca358287fca6ecacce83defcf23af1e8e9f419 /server/helpers
parentc893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff)
downloadPeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip
Move models to typescript-sequelize
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/activitypub.ts10
-rw-r--r--server/helpers/custom-validators/accounts.ts13
-rw-r--r--server/helpers/custom-validators/activitypub/account.ts2
-rw-r--r--server/helpers/custom-validators/activitypub/activity.ts4
-rw-r--r--server/helpers/custom-validators/activitypub/misc.ts4
-rw-r--r--server/helpers/custom-validators/index.ts9
-rw-r--r--server/helpers/custom-validators/video-channels.ts10
-rw-r--r--server/helpers/custom-validators/videos.ts19
-rw-r--r--server/helpers/custom-validators/webfinger.ts8
-rw-r--r--server/helpers/ffmpeg-utils.ts2
-rw-r--r--server/helpers/index.ts1
-rw-r--r--server/helpers/logger.ts2
-rw-r--r--server/helpers/peertube-crypto.ts6
-rw-r--r--server/helpers/utils.ts17
-rw-r--r--server/helpers/webfinger.ts4
15 files changed, 51 insertions, 60 deletions
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts
index 1ea6422ca..43907b596 100644
--- a/server/helpers/activitypub.ts
+++ b/server/helpers/activitypub.ts
@@ -1,8 +1,8 @@
1import { Activity } from '../../shared/models/activitypub/activity' 1import { ResultList } from '../../shared/models'
2import { ResultList } from '../../shared/models/result-list.model' 2import { Activity } from '../../shared/models/activitypub'
3import { AccountInstance } from '../models/account/account-interface' 3import { ACTIVITY_PUB } from '../initializers'
4import { AccountModel } from '../models/account/account'
4import { signObject } from './peertube-crypto' 5import { signObject } from './peertube-crypto'
5import { ACTIVITY_PUB } from '../initializers/constants'
6 6
7function activityPubContextify <T> (data: T) { 7function activityPubContextify <T> (data: T) {
8 return Object.assign(data,{ 8 return Object.assign(data,{
@@ -71,7 +71,7 @@ function activityPubCollectionPagination (url: string, page: any, result: Result
71 return orderedCollectionPagination 71 return orderedCollectionPagination
72} 72}
73 73
74function buildSignedActivity (byAccount: AccountInstance, data: Object) { 74function buildSignedActivity (byAccount: AccountModel, data: Object) {
75 const activity = activityPubContextify(data) 75 const activity = activityPubContextify(data)
76 76
77 return signObject(byAccount, activity) as Promise<Activity> 77 return signObject(byAccount, activity) as Promise<Activity>
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts
index e3c477414..8dc5d1f0d 100644
--- a/server/helpers/custom-validators/accounts.ts
+++ b/server/helpers/custom-validators/accounts.ts
@@ -2,8 +2,7 @@ import * as Bluebird from 'bluebird'
2import { Response } from 'express' 2import { Response } from 'express'
3import 'express-validator' 3import 'express-validator'
4import * as validator from 'validator' 4import * as validator from 'validator'
5import { database as db } from '../../initializers' 5import { AccountModel } from '../../models/account/account'
6import { AccountInstance } from '../../models'
7import { isUserUsernameValid } from './users' 6import { isUserUsernameValid } from './users'
8 7
9function isAccountNameValid (value: string) { 8function isAccountNameValid (value: string) {
@@ -11,24 +10,24 @@ function isAccountNameValid (value: string) {
11} 10}
12 11
13function isAccountIdExist (id: number | string, res: Response) { 12function isAccountIdExist (id: number | string, res: Response) {
14 let promise: Bluebird<AccountInstance> 13 let promise: Bluebird<AccountModel>
15 14
16 if (validator.isInt('' + id)) { 15 if (validator.isInt('' + id)) {
17 promise = db.Account.load(+id) 16 promise = AccountModel.load(+id)
18 } else { // UUID 17 } else { // UUID
19 promise = db.Account.loadByUUID('' + id) 18 promise = AccountModel.loadByUUID('' + id)
20 } 19 }
21 20
22 return isAccountExist(promise, res) 21 return isAccountExist(promise, res)
23} 22}
24 23
25function isLocalAccountNameExist (name: string, res: Response) { 24function isLocalAccountNameExist (name: string, res: Response) {
26 const promise = db.Account.loadLocalByName(name) 25 const promise = AccountModel.loadLocalByName(name)
27 26
28 return isAccountExist(promise, res) 27 return isAccountExist(promise, res)
29} 28}
30 29
31async function isAccountExist (p: Bluebird<AccountInstance>, res: Response) { 30async function isAccountExist (p: Bluebird<AccountModel>, res: Response) {
32 const account = await p 31 const account = await p
33 32
34 if (!account) { 33 if (!account) {
diff --git a/server/helpers/custom-validators/activitypub/account.ts b/server/helpers/custom-validators/activitypub/account.ts
index cab39a654..10bf81e8a 100644
--- a/server/helpers/custom-validators/activitypub/account.ts
+++ b/server/helpers/custom-validators/activitypub/account.ts
@@ -1,5 +1,5 @@
1import * as validator from 'validator' 1import * as validator from 'validator'
2import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' 2import { CONSTRAINTS_FIELDS } from '../../../initializers'
3import { isAccountNameValid } from '../accounts' 3import { isAccountNameValid } from '../accounts'
4import { exists, isUUIDValid } from '../misc' 4import { exists, isUUIDValid } from '../misc'
5import { isActivityPubUrlValid, isBaseActivityValid } from './misc' 5import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
diff --git a/server/helpers/custom-validators/activitypub/activity.ts b/server/helpers/custom-validators/activitypub/activity.ts
index 3a0e8197c..043e3c55e 100644
--- a/server/helpers/custom-validators/activitypub/activity.ts
+++ b/server/helpers/custom-validators/activitypub/activity.ts
@@ -1,8 +1,9 @@
1import * as validator from 'validator' 1import * as validator from 'validator'
2import { Activity, ActivityType } from '../../../../shared/models/activitypub/activity' 2import { Activity, ActivityType } from '../../../../shared/models/activitypub'
3import { isAccountAcceptActivityValid, isAccountDeleteActivityValid, isAccountFollowActivityValid } from './account' 3import { isAccountAcceptActivityValid, isAccountDeleteActivityValid, isAccountFollowActivityValid } from './account'
4import { isAnnounceActivityValid } from './announce' 4import { isAnnounceActivityValid } from './announce'
5import { isActivityPubUrlValid } from './misc' 5import { isActivityPubUrlValid } from './misc'
6import { isDislikeActivityValid, isLikeActivityValid } from './rate'
6import { isUndoActivityValid } from './undo' 7import { isUndoActivityValid } from './undo'
7import { isVideoChannelCreateActivityValid, isVideoChannelDeleteActivityValid, isVideoChannelUpdateActivityValid } from './video-channels' 8import { isVideoChannelCreateActivityValid, isVideoChannelDeleteActivityValid, isVideoChannelUpdateActivityValid } from './video-channels'
8import { 9import {
@@ -12,7 +13,6 @@ import {
12 isVideoTorrentUpdateActivityValid 13 isVideoTorrentUpdateActivityValid
13} from './videos' 14} from './videos'
14import { isViewActivityValid } from './view' 15import { isViewActivityValid } from './view'
15import { isDislikeActivityValid, isLikeActivityValid } from './rate'
16 16
17function isRootActivityValid (activity: any) { 17function isRootActivityValid (activity: any) {
18 return Array.isArray(activity['@context']) && 18 return Array.isArray(activity['@context']) &&
diff --git a/server/helpers/custom-validators/activitypub/misc.ts b/server/helpers/custom-validators/activitypub/misc.ts
index 1bbfd0fc4..65f5ca809 100644
--- a/server/helpers/custom-validators/activitypub/misc.ts
+++ b/server/helpers/custom-validators/activitypub/misc.ts
@@ -1,7 +1,7 @@
1import * as validator from 'validator' 1import * as validator from 'validator'
2import { exists } from '../misc' 2import { CONSTRAINTS_FIELDS } from '../../../initializers'
3import { isTestInstance } from '../../core-utils' 3import { isTestInstance } from '../../core-utils'
4import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' 4import { exists } from '../misc'
5 5
6function isActivityPubUrlValid (url: string) { 6function isActivityPubUrlValid (url: string) {
7 const isURLOptions = { 7 const isURLOptions = {
diff --git a/server/helpers/custom-validators/index.ts b/server/helpers/custom-validators/index.ts
deleted file mode 100644
index d3b2f5393..000000000
--- a/server/helpers/custom-validators/index.ts
+++ /dev/null
@@ -1,9 +0,0 @@
1export * from './activitypub'
2export * from './misc'
3export * from './servers'
4export * from './servers'
5export * from './users'
6export * from './accounts'
7export * from './video-channels'
8export * from './videos'
9export * from './webfinger'
diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts
index 3de9f041b..6bc96bf51 100644
--- a/server/helpers/custom-validators/video-channels.ts
+++ b/server/helpers/custom-validators/video-channels.ts
@@ -2,8 +2,8 @@ import * as express from 'express'
2import 'express-validator' 2import 'express-validator'
3import 'multer' 3import 'multer'
4import * as validator from 'validator' 4import * as validator from 'validator'
5import { CONSTRAINTS_FIELDS, database as db } from '../../initializers' 5import { CONSTRAINTS_FIELDS } from '../../initializers'
6import { VideoChannelInstance } from '../../models' 6import { VideoChannelModel } from '../../models/video/video-channel'
7import { exists } from './misc' 7import { exists } from './misc'
8 8
9const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS 9const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
@@ -17,11 +17,11 @@ function isVideoChannelNameValid (value: string) {
17} 17}
18 18
19async function isVideoChannelExist (id: string, res: express.Response) { 19async function isVideoChannelExist (id: string, res: express.Response) {
20 let videoChannel: VideoChannelInstance 20 let videoChannel: VideoChannelModel
21 if (validator.isInt(id)) { 21 if (validator.isInt(id)) {
22 videoChannel = await db.VideoChannel.loadAndPopulateAccount(+id) 22 videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
23 } else { // UUID 23 } else { // UUID
24 videoChannel = await db.VideoChannel.loadByUUIDAndPopulateAccount(id) 24 videoChannel = await VideoChannelModel.loadByUUIDAndPopulateAccount(id)
25 } 25 }
26 26
27 if (!videoChannel) { 27 if (!videoChannel) {
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index 37fa8b08a..ee9d0ed19 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -4,10 +4,15 @@ import { values } from 'lodash'
4import 'multer' 4import 'multer'
5import * as validator from 'validator' 5import * as validator from 'validator'
6import { VideoRateType } from '../../../shared' 6import { VideoRateType } from '../../../shared'
7import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers' 7import {
8import { VIDEO_PRIVACIES } from '../../initializers/constants' 8 CONSTRAINTS_FIELDS,
9import { database as db } from '../../initializers/database' 9 VIDEO_CATEGORIES,
10import { VideoInstance } from '../../models/video/video-interface' 10 VIDEO_LANGUAGES,
11 VIDEO_LICENCES,
12 VIDEO_PRIVACIES,
13 VIDEO_RATE_TYPES
14} from '../../initializers'
15import { VideoModel } from '../../models/video/video'
11import { exists, isArray } from './misc' 16import { exists, isArray } from './misc'
12 17
13const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS 18const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
@@ -100,12 +105,12 @@ function isVideoFileSizeValid (value: string) {
100} 105}
101 106
102async function isVideoExist (id: string, res: Response) { 107async function isVideoExist (id: string, res: Response) {
103 let video: VideoInstance 108 let video: VideoModel
104 109
105 if (validator.isInt(id)) { 110 if (validator.isInt(id)) {
106 video = await db.Video.loadAndPopulateAccountAndServerAndTags(+id) 111 video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
107 } else { // UUID 112 } else { // UUID
108 video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(id) 113 video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
109 } 114 }
110 115
111 if (!video) { 116 if (!video) {
diff --git a/server/helpers/custom-validators/webfinger.ts b/server/helpers/custom-validators/webfinger.ts
index e93115d81..38f6b938d 100644
--- a/server/helpers/custom-validators/webfinger.ts
+++ b/server/helpers/custom-validators/webfinger.ts
@@ -1,6 +1,4 @@
1import 'express-validator' 1import { CONFIG } from '../../initializers'
2import 'multer'
3import { CONFIG } from '../../initializers/constants'
4import { exists } from './misc' 2import { exists } from './misc'
5 3
6function isWebfingerResourceValid (value: string) { 4function isWebfingerResourceValid (value: string) {
@@ -13,9 +11,7 @@ function isWebfingerResourceValid (value: string) {
13 11
14 const host = accountParts[1] 12 const host = accountParts[1]
15 13
16 if (host !== CONFIG.WEBSERVER.HOST) return false 14 return host === CONFIG.WEBSERVER.HOST
17
18 return true
19} 15}
20 16
21// --------------------------------------------------------------------------- 17// ---------------------------------------------------------------------------
diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts
index 8ad205961..c2581f460 100644
--- a/server/helpers/ffmpeg-utils.ts
+++ b/server/helpers/ffmpeg-utils.ts
@@ -1,5 +1,5 @@
1import * as ffmpeg from 'fluent-ffmpeg' 1import * as ffmpeg from 'fluent-ffmpeg'
2import { VideoResolution } from '../../shared/models/videos/video-resolution.enum' 2import { VideoResolution } from '../../shared/models/videos'
3import { CONFIG } from '../initializers' 3import { CONFIG } from '../initializers'
4 4
5function getVideoFileHeight (path: string) { 5function getVideoFileHeight (path: string) {
diff --git a/server/helpers/index.ts b/server/helpers/index.ts
index 2c7ac3954..d96bc48e9 100644
--- a/server/helpers/index.ts
+++ b/server/helpers/index.ts
@@ -1,7 +1,6 @@
1export * from './activitypub' 1export * from './activitypub'
2export * from './core-utils' 2export * from './core-utils'
3export * from './logger' 3export * from './logger'
4export * from './custom-validators'
5export * from './ffmpeg-utils' 4export * from './ffmpeg-utils'
6export * from './database-utils' 5export * from './database-utils'
7export * from './peertube-crypto' 6export * from './peertube-crypto'
diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts
index 8d809d16d..2676133db 100644
--- a/server/helpers/logger.ts
+++ b/server/helpers/logger.ts
@@ -2,7 +2,7 @@
2import * as mkdirp from 'mkdirp' 2import * as mkdirp from 'mkdirp'
3import * as path from 'path' 3import * as path from 'path'
4import * as winston from 'winston' 4import * as winston from 'winston'
5import { CONFIG } from '../initializers/constants' 5import { CONFIG } from '../initializers'
6 6
7const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT 7const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
8 8
diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.ts
index 74e4cc703..c4c735cb8 100644
--- a/server/helpers/peertube-crypto.ts
+++ b/server/helpers/peertube-crypto.ts
@@ -1,5 +1,5 @@
1import { BCRYPT_SALT_SIZE, PRIVATE_RSA_KEY_SIZE } from '../initializers' 1import { BCRYPT_SALT_SIZE, PRIVATE_RSA_KEY_SIZE } from '../initializers'
2import { AccountInstance } from '../models/account/account-interface' 2import { AccountModel } from '../models/account/account'
3import { bcryptComparePromise, bcryptGenSaltPromise, bcryptHashPromise, createPrivateKey, getPublicKey } from './core-utils' 3import { bcryptComparePromise, bcryptGenSaltPromise, bcryptHashPromise, createPrivateKey, getPublicKey } from './core-utils'
4import { jsig } from './custom-jsonld-signature' 4import { jsig } from './custom-jsonld-signature'
5import { logger } from './logger' 5import { logger } from './logger'
@@ -13,7 +13,7 @@ async function createPrivateAndPublicKeys () {
13 return { privateKey: key, publicKey } 13 return { privateKey: key, publicKey }
14} 14}
15 15
16function isSignatureVerified (fromAccount: AccountInstance, signedDocument: object) { 16function isSignatureVerified (fromAccount: AccountModel, signedDocument: object) {
17 const publicKeyObject = { 17 const publicKeyObject = {
18 '@context': jsig.SECURITY_CONTEXT_URL, 18 '@context': jsig.SECURITY_CONTEXT_URL,
19 '@id': fromAccount.url, 19 '@id': fromAccount.url,
@@ -40,7 +40,7 @@ function isSignatureVerified (fromAccount: AccountInstance, signedDocument: obje
40 }) 40 })
41} 41}
42 42
43function signObject (byAccount: AccountInstance, data: any) { 43function signObject (byAccount: AccountModel, data: any) {
44 const options = { 44 const options = {
45 privateKeyPem: byAccount.privateKey, 45 privateKeyPem: byAccount.privateKey,
46 creator: byAccount.url 46 creator: byAccount.url
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index 3464341e6..cb5e536b8 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -1,9 +1,10 @@
1import * as express from 'express' 1import * as express from 'express'
2import * as Sequelize from 'sequelize' 2import { Model } from 'sequelize-typescript'
3import { ResultList } from '../../shared' 3import { ResultList } from '../../shared'
4import { VideoResolution } from '../../shared/models/videos/video-resolution.enum' 4import { VideoResolution } from '../../shared/models/videos'
5import { CONFIG, database as db } from '../initializers' 5import { CONFIG } from '../initializers'
6import { AccountInstance } from '../models/account/account-interface' 6import { AccountModel } from '../models/account/account'
7import { UserModel } from '../models/account/user'
7import { pseudoRandomBytesPromise } from './core-utils' 8import { pseudoRandomBytesPromise } from './core-utils'
8import { logger } from './logger' 9import { logger } from './logger'
9 10
@@ -46,7 +47,7 @@ async function isSignupAllowed () {
46 return true 47 return true
47 } 48 }
48 49
49 const totalUsers = await db.User.countTotal() 50 const totalUsers = await UserModel.countTotal()
50 51
51 return totalUsers < CONFIG.SIGNUP.LIMIT 52 return totalUsers < CONFIG.SIGNUP.LIMIT
52} 53}
@@ -72,17 +73,17 @@ function computeResolutionsToTranscode (videoFileHeight: number) {
72 return resolutionsEnabled 73 return resolutionsEnabled
73} 74}
74 75
75function resetSequelizeInstance (instance: Sequelize.Instance<any>, savedFields: object) { 76function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
76 Object.keys(savedFields).forEach(key => { 77 Object.keys(savedFields).forEach(key => {
77 const value = savedFields[key] 78 const value = savedFields[key]
78 instance.set(key, value) 79 instance.set(key, value)
79 }) 80 })
80} 81}
81 82
82let serverAccount: AccountInstance 83let serverAccount: AccountModel
83async function getServerAccount () { 84async function getServerAccount () {
84 if (serverAccount === undefined) { 85 if (serverAccount === undefined) {
85 serverAccount = await db.Account.loadApplication() 86 serverAccount = await AccountModel.loadApplication()
86 } 87 }
87 88
88 if (!serverAccount) { 89 if (!serverAccount) {
diff --git a/server/helpers/webfinger.ts b/server/helpers/webfinger.ts
index ab2888981..d98068cd7 100644
--- a/server/helpers/webfinger.ts
+++ b/server/helpers/webfinger.ts
@@ -1,8 +1,8 @@
1import * as WebFinger from 'webfinger.js' 1import * as WebFinger from 'webfinger.js'
2import { WebFingerData } from '../../shared' 2import { WebFingerData } from '../../shared'
3import { fetchRemoteAccount } from '../lib/activitypub/account' 3import { fetchRemoteAccount } from '../lib/activitypub'
4import { isTestInstance } from './core-utils' 4import { isTestInstance } from './core-utils'
5import { isActivityPubUrlValid } from './custom-validators' 5import { isActivityPubUrlValid } from './custom-validators/activitypub'
6 6
7const webfinger = new WebFinger({ 7const webfinger = new WebFinger({
8 webfist_fallback: false, 8 webfist_fallback: false,