Cleanup server fixme
authorChocobozzz <me@florianbigard.com>
Tue, 28 Jan 2020 13:45:17 +0000 (14:45 +0100)
committerChocobozzz <me@florianbigard.com>
Tue, 28 Jan 2020 13:45:17 +0000 (14:45 +0100)
14 files changed:
server/controllers/webfinger.ts
server/lib/activitypub/videos.ts
server/lib/job-queue/job-queue.ts
server/middlewares/validators/webfinger.ts
server/models/account/account-blocklist.ts
server/models/account/user-notification.ts
server/models/activitypub/actor.ts
server/models/server/server-blocklist.ts
server/models/video/video-caption.ts
server/models/video/video-channel.ts
server/models/video/video-playlist.ts
server/models/video/video.ts
server/typings/express.ts
shared/extra-utils/miscs/sql.ts

index fc9575160bf42e15a74ffc570eb9c2560732eb1d..77c8518808337d398ec0b9b37d6a1569669f0adf 100644 (file)
@@ -18,7 +18,7 @@ export {
 // ---------------------------------------------------------------------------
 
 function webfingerController (req: express.Request, res: express.Response) {
-  const actor = res.locals.actorFull
+  const actor = res.locals.actorUrl
 
   const json = {
     subject: req.query.resource,
@@ -32,5 +32,5 @@ function webfingerController (req: express.Request, res: express.Response) {
     ]
   }
 
-  return res.json(json).end()
+  return res.json(json)
 }
index ade93150f39453837dcebde1cc269d7e86497a61..7a9d5168bd994d2c585c26a1c1ae943df2bc9736 100644 (file)
@@ -639,7 +639,6 @@ async function videoActivityObjectToDBAttributes (videoChannel: MChannelId, vide
     createdAt: new Date(videoObject.published),
     publishedAt: new Date(videoObject.published),
     originallyPublishedAt: videoObject.originallyPublishedAt ? new Date(videoObject.originallyPublishedAt) : null,
-    // FIXME: updatedAt does not seems to be considered by Sequelize
     updatedAt: new Date(videoObject.updated),
     views: videoObject.views,
     likes: 0,
index 8bbf58f2bf08b0df46f810a7c2f3a6ca00f6f9ef..a1c623b2575d0997d4987f003f21278c73c05df6 100644 (file)
@@ -144,8 +144,7 @@ class JobQueue {
         continue
       }
 
-      // FIXME: Bull queue typings does not have getJobs method
-      const jobs = await (queue as any).getJobs(state, 0, start + count, asc)
+      const jobs = await queue.getJobs([ state ], 0, start + count, asc)
       results = results.concat(jobs)
     }
 
index d50e6527fe4ac7ab3d9764c6372e90b96df91163..5fe864f8b6a62233d0352be6c6a11e4fa41727d2 100644 (file)
@@ -18,15 +18,14 @@ const webfingerValidator = [
     const nameWithHost = getHostWithPort(req.query.resource.substr(5))
     const [ name ] = nameWithHost.split('@')
 
-    // FIXME: we don't need the full actor
-    const actor = await ActorModel.loadLocalByName(name)
+    const actor = await ActorModel.loadLocalUrlByName(name)
     if (!actor) {
       return res.status(404)
         .send({ error: 'Actor not found' })
         .end()
     }
 
-    res.locals.actorFull = actor
+    res.locals.actorUrl = actor
     return next()
   }
 ]
index 6ebe325564290f18cbb54e43363f299e7de04db9..e2f66d733c514e444baa071b71e50e8da24a6664 100644 (file)
@@ -80,7 +80,7 @@ export class AccountBlocklistModel extends Model<AccountBlocklistModel> {
       attributes: [ 'accountId', 'id' ],
       where: {
         accountId: {
-          [Op.in]: accountIds // FIXME: sequelize ANY seems broken
+          [Op.in]: accountIds
         },
         targetAccountId
       },
index a05f30175cc7b21f9e12ce41d7f0973d3127c024..f649b8f95a29ca4b809650128461aadce9a5f5f2 100644 (file)
@@ -363,7 +363,7 @@ export class UserNotificationModel extends Model<UserNotificationModel> {
       where: {
         userId,
         id: {
-          [Op.in]: notificationIds // FIXME: sequelize ANY seems broken
+          [Op.in]: notificationIds
         }
       }
     }
index 007647ced187f20c46ddf850543e684d1f7eb7d7..d651a281aa02e11a4dd43468adaa0e3ba5588bfb 100644 (file)
@@ -43,7 +43,7 @@ import {
   MActorFull,
   MActorHost,
   MActorServer,
-  MActorSummaryFormattable,
+  MActorSummaryFormattable, MActorUrl,
   MActorWithInboxes
 } from '../../typings/models'
 import * as Bluebird from 'bluebird'
@@ -276,7 +276,8 @@ export class ActorModel extends Model<ActorModel> {
   })
   VideoChannel: VideoChannelModel
 
-  private static cache: { [ id: string ]: any } = {}
+  private static localNameCache: { [ id: string ]: any } = {}
+  private static localUrlCache: { [ id: string ]: any } = {}
 
   static load (id: number): Bluebird<MActor> {
     return ActorModel.unscoped().findByPk(id)
@@ -345,8 +346,8 @@ export class ActorModel extends Model<ActorModel> {
 
   static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> {
     // The server actor never change, so we can easily cache it
-    if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.cache[preferredUsername]) {
-      return Bluebird.resolve(ActorModel.cache[preferredUsername])
+    if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localNameCache[preferredUsername]) {
+      return Bluebird.resolve(ActorModel.localNameCache[preferredUsername])
     }
 
     const query = {
@@ -361,7 +362,33 @@ export class ActorModel extends Model<ActorModel> {
                      .findOne(query)
                      .then(actor => {
                        if (preferredUsername === SERVER_ACTOR_NAME) {
-                         ActorModel.cache[ preferredUsername ] = actor
+                         ActorModel.localNameCache[ preferredUsername ] = actor
+                       }
+
+                       return actor
+                     })
+  }
+
+  static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> {
+    // The server actor never change, so we can easily cache it
+    if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localUrlCache[preferredUsername]) {
+      return Bluebird.resolve(ActorModel.localUrlCache[preferredUsername])
+    }
+
+    const query = {
+      attributes: [ 'url' ],
+      where: {
+        preferredUsername,
+        serverId: null
+      },
+      transaction
+    }
+
+    return ActorModel.unscoped()
+                     .findOne(query)
+                     .then(actor => {
+                       if (preferredUsername === SERVER_ACTOR_NAME) {
+                         ActorModel.localUrlCache[ preferredUsername ] = actor
                        }
 
                        return actor
index b88df4fd52700d072082f4c1b4037e7d39b1258c..883ae47ab0c84e61d7d65299b3e1f386ba79d358 100644 (file)
@@ -81,7 +81,7 @@ export class ServerBlocklistModel extends Model<ServerBlocklistModel> {
       attributes: [ 'accountId', 'id' ],
       where: {
         accountId: {
-          [Op.in]: accountIds // FIXME: sequelize ANY seems broken
+          [Op.in]: accountIds
         },
         targetServerId
       },
index eeb2a4afdc7cfcb351a759cef9b24cd486decaff..6335d44e4f088be495d231aadd2a33302deb1a38 100644 (file)
@@ -120,7 +120,7 @@ export class VideoCaptionModel extends Model<VideoCaptionModel> {
       language
     }
 
-    return (VideoCaptionModel.upsert<VideoCaptionModel>(values, { transaction, returning: true }) as any) // FIXME: typings
+    return VideoCaptionModel.upsert(values, { transaction, returning: true })
       .then(([ caption ]) => caption)
   }
 
index e10adcb3aca3817d78aa19cfcc0d584fff81b15d..4e98e7ba3f064333176f8a30c2e87c98365c7518 100644 (file)
@@ -43,18 +43,6 @@ import {
   MChannelSummaryFormattable
 } from '../../typings/models/video'
 
-// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
-const indexes: ModelIndexesOptions[] = [
-  buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
-
-  {
-    fields: [ 'accountId' ]
-  },
-  {
-    fields: [ 'actorId' ]
-  }
-]
-
 export enum ScopeNames {
   FOR_API = 'FOR_API',
   WITH_ACCOUNT = 'WITH_ACCOUNT',
@@ -176,7 +164,16 @@ export type SummaryOptions = {
 }))
 @Table({
   tableName: 'videoChannel',
-  indexes
+  indexes: [
+    buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
+
+    {
+      fields: [ 'accountId' ]
+    },
+    {
+      fields: [ 'actorId' ]
+    }
+  ]
 })
 export class VideoChannelModel extends Model<VideoChannelModel> {
 
index bcdda36e5cd589f0943a798cd3272dbff17c2d89..ba1fc23eac3e952e9cda963b541db6e22e4df0fa 100644 (file)
@@ -369,7 +369,7 @@ export class VideoPlaylistModel extends Model<VideoPlaylistModel> {
           model: VideoPlaylistElementModel.unscoped(),
           where: {
             videoId: {
-              [Op.in]: videoIds // FIXME: sequelize ANY seems broken
+              [Op.in]: videoIds
             }
           },
           required: true
index eacffe1864156d84d84e0cc2ee817c892cd354ae..20e1f1c4aa97c043243001a231b75d23df473f17 100644 (file)
@@ -1,18 +1,7 @@
 import * as Bluebird from 'bluebird'
 import { maxBy, minBy } from 'lodash'
 import { join } from 'path'
-import {
-  CountOptions,
-  FindOptions,
-  IncludeOptions,
-  ModelIndexesOptions,
-  Op,
-  QueryTypes,
-  ScopeOptions,
-  Sequelize,
-  Transaction,
-  WhereOptions
-} from 'sequelize'
+import { CountOptions, FindOptions, IncludeOptions, Op, QueryTypes, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize'
 import {
   AllowNull,
   BeforeDestroy,
@@ -136,8 +125,7 @@ import {
   MVideoThumbnailBlacklist,
   MVideoWithAllFiles,
   MVideoWithFile,
-  MVideoWithRights,
-  MStreamingPlaylistFiles
+  MVideoWithRights
 } from '../../typings/models'
 import { MVideoFile, MVideoFileStreamingPlaylistVideo } from '../../typings/models/video/video-file'
 import { MThumbnail } from '../../typings/models/video/thumbnail'
@@ -145,74 +133,6 @@ import { VideoFile } from '@shared/models/videos/video-file.model'
 import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
 import validator from 'validator'
 
-// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
-const indexes: (ModelIndexesOptions & { where?: WhereOptions })[] = [
-  buildTrigramSearchIndex('video_name_trigram', 'name'),
-
-  { fields: [ 'createdAt' ] },
-  {
-    fields: [
-      { name: 'publishedAt', order: 'DESC' },
-      { name: 'id', order: 'ASC' }
-    ]
-  },
-  { fields: [ 'duration' ] },
-  { fields: [ 'views' ] },
-  { fields: [ 'channelId' ] },
-  {
-    fields: [ 'originallyPublishedAt' ],
-    where: {
-      originallyPublishedAt: {
-        [Op.ne]: null
-      }
-    }
-  },
-  {
-    fields: [ 'category' ], // We don't care videos with an unknown category
-    where: {
-      category: {
-        [Op.ne]: null
-      }
-    }
-  },
-  {
-    fields: [ 'licence' ], // We don't care videos with an unknown licence
-    where: {
-      licence: {
-        [Op.ne]: null
-      }
-    }
-  },
-  {
-    fields: [ 'language' ], // We don't care videos with an unknown language
-    where: {
-      language: {
-        [Op.ne]: null
-      }
-    }
-  },
-  {
-    fields: [ 'nsfw' ], // Most of the videos are not NSFW
-    where: {
-      nsfw: true
-    }
-  },
-  {
-    fields: [ 'remote' ], // Only index local videos
-    where: {
-      remote: false
-    }
-  },
-  {
-    fields: [ 'uuid' ],
-    unique: true
-  },
-  {
-    fields: [ 'url' ],
-    unique: true
-  }
-]
-
 export enum ScopeNames {
   AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS',
   FOR_API = 'FOR_API',
@@ -292,7 +212,7 @@ export type AvailableForListIDsOptions = {
     if (options.ids) {
       query.where = {
         id: {
-          [ Op.in ]: options.ids // FIXME: sequelize ANY seems broken
+          [ Op.in ]: options.ids
         }
       }
     }
@@ -760,7 +680,72 @@ export type AvailableForListIDsOptions = {
 }))
 @Table({
   tableName: 'video',
-  indexes
+  indexes: [
+    buildTrigramSearchIndex('video_name_trigram', 'name'),
+
+    { fields: [ 'createdAt' ] },
+    {
+      fields: [
+        { name: 'publishedAt', order: 'DESC' },
+        { name: 'id', order: 'ASC' }
+      ]
+    },
+    { fields: [ 'duration' ] },
+    { fields: [ 'views' ] },
+    { fields: [ 'channelId' ] },
+    {
+      fields: [ 'originallyPublishedAt' ],
+      where: {
+        originallyPublishedAt: {
+          [Op.ne]: null
+        }
+      }
+    },
+    {
+      fields: [ 'category' ], // We don't care videos with an unknown category
+      where: {
+        category: {
+          [Op.ne]: null
+        }
+      }
+    },
+    {
+      fields: [ 'licence' ], // We don't care videos with an unknown licence
+      where: {
+        licence: {
+          [Op.ne]: null
+        }
+      }
+    },
+    {
+      fields: [ 'language' ], // We don't care videos with an unknown language
+      where: {
+        language: {
+          [Op.ne]: null
+        }
+      }
+    },
+    {
+      fields: [ 'nsfw' ], // Most of the videos are not NSFW
+      where: {
+        nsfw: true
+      }
+    },
+    {
+      fields: [ 'remote' ], // Only index local videos
+      where: {
+        remote: false
+      }
+    },
+    {
+      fields: [ 'uuid' ],
+      unique: true
+    },
+    {
+      fields: [ 'url' ],
+      unique: true
+    }
+  ]
 })
 export class VideoModel extends Model<VideoModel> {
 
index 3cc7c7632809caa7295ac00f65d9d57379e70252..43a9b2c99026839df00ea0235cc22a4131c8ae03 100644 (file)
@@ -21,7 +21,7 @@ import {
 } from './models'
 import { MVideoPlaylistFull, MVideoPlaylistFullSummary } from './models/video/video-playlist'
 import { MVideoImportDefault } from '@server/typings/models/video/video-import'
-import { MAccountBlocklist, MStreamingPlaylist, MVideoFile } from '@server/typings/models'
+import { MAccountBlocklist, MActorUrl, MStreamingPlaylist, MVideoFile } from '@server/typings/models'
 import { MVideoPlaylistElement, MVideoPlaylistElementVideoUrlPlaylistPrivacy } from '@server/typings/models/video/video-playlist-element'
 import { MAccountVideoRateAccountVideo } from '@server/typings/models/video/video-rate'
 import { MVideoChangeOwnershipFull } from './models/video/video-change-ownership'
@@ -74,6 +74,7 @@ declare module 'express' {
 
       account?: MAccountDefault
 
+      actorUrl?: MActorUrl
       actorFull?: MActorFull
 
       user?: MUserDefault
index 167649c6d6dad76585adb183483c3836f6163bb0..42599c20e8e3701018e54e8236190f6d1d8ab893 100644 (file)
@@ -59,7 +59,6 @@ async function countVideoViewsOf (internalServerNumber: number, uuid: string) {
 
   if (!total) return 0
 
-  // FIXME: check if we really need parseInt
   return parseInt(total + '', 10)
 }