Optimize list my playlists SQL query
authorChocobozzz <me@florianbigard.com>
Thu, 9 Jan 2020 08:26:59 +0000 (09:26 +0100)
committerChocobozzz <me@florianbigard.com>
Thu, 9 Jan 2020 08:27:21 +0000 (09:27 +0100)
client/src/app/shared/video-playlist/video-playlist.service.ts
server/controllers/api/accounts.ts
server/models/video/video-playlist.ts

index 1ec9315efddb41de0e4bb4fd1a8cfb5870d81080..078bcc5d78bdcdce6ff42c60125197f15639cda1 100644 (file)
@@ -42,6 +42,7 @@ export class VideoPlaylistService {
   private videoExistsCache: { [ id: number ]: VideoExistInPlaylist[] } = {}
 
   private myAccountPlaylistCache: ResultList<CachedPlaylist> = undefined
+  private myAccountPlaylistCacheRunning = false
   private myAccountPlaylistCacheSubject = new Subject<ResultList<CachedPlaylist>>()
 
   constructor (
@@ -78,12 +79,20 @@ export class VideoPlaylistService {
   }
 
   listMyPlaylistWithCache (user: AuthUser, search?: string) {
-    if (!search && this.myAccountPlaylistCache) return of(this.myAccountPlaylistCache)
+    if (!search) {
+      if (this.myAccountPlaylistCacheRunning) return
+      if (this.myAccountPlaylistCache) return of(this.myAccountPlaylistCache)
+    }
+
+    this.myAccountPlaylistCacheRunning = true
 
     return this.listAccountPlaylists(user.account, undefined, '-updatedAt', search)
                .pipe(
                  tap(result => {
-                   if (!search) this.myAccountPlaylistCache = result
+                   if (!search) {
+                     this.myAccountPlaylistCacheRunning = false
+                     this.myAccountPlaylistCache = result
+                   }
                  })
                )
   }
index 00148ff555a7dd0282ff30fb7fcbf14c77edc8ef..05740318efe046d40f26f0bda41dca059b747cf7 100644 (file)
@@ -133,9 +133,9 @@ async function listAccountPlaylists (req: express.Request, res: express.Response
   const serverActor = await getServerActor()
 
   // Allow users to see their private/unlisted video playlists
-  let privateAndUnlisted = false
+  let listMyPlaylists = false
   if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
-    privateAndUnlisted = true
+    listMyPlaylists = true
   }
 
   const resultList = await VideoPlaylistModel.listForApi({
@@ -145,7 +145,7 @@ async function listAccountPlaylists (req: express.Request, res: express.Response
     count: req.query.count,
     sort: req.query.sort,
     accountId: res.locals.account.id,
-    privateAndUnlisted,
+    listMyPlaylists,
     type: req.query.playlistType
   })
 
index 71a5802499ea1007461d2420f9faad6e95cefd9e..bcdda36e5cd589f0943a798cd3272dbff17c2d89 100644 (file)
@@ -68,7 +68,7 @@ type AvailableForListOptions = {
   type?: VideoPlaylistType
   accountId?: number
   videoChannelId?: number
-  privateAndUnlisted?: boolean,
+  listMyPlaylists?: boolean,
   search?: string
 }
 
@@ -124,27 +124,31 @@ type AvailableForListOptions = {
     ]
   },
   [ ScopeNames.AVAILABLE_FOR_LIST ]: (options: AvailableForListOptions) => {
-    // Only list local playlists OR playlists that are on an instance followed by actorId
-    const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
-    const whereActor = {
-      [ Op.or ]: [
-        {
-          serverId: null
-        },
-        {
-          serverId: {
-            [ Op.in ]: literal(inQueryInstanceFollow)
-          }
-        }
-      ]
-    }
+
+    let whereActor: WhereOptions = {}
 
     const whereAnd: WhereOptions[] = []
 
-    if (options.privateAndUnlisted !== true) {
+    if (options.listMyPlaylists !== true) {
       whereAnd.push({
         privacy: VideoPlaylistPrivacy.PUBLIC
       })
+
+      // Only list local playlists OR playlists that are on an instance followed by actorId
+      const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
+
+      whereActor = {
+        [ Op.or ]: [
+          {
+            serverId: null
+          },
+          {
+            serverId: {
+              [ Op.in ]: literal(inQueryInstanceFollow)
+            }
+          }
+        ]
+      }
     }
 
     if (options.accountId) {
@@ -301,7 +305,7 @@ export class VideoPlaylistModel extends Model<VideoPlaylistModel> {
     type?: VideoPlaylistType,
     accountId?: number,
     videoChannelId?: number,
-    privateAndUnlisted?: boolean,
+    listMyPlaylists?: boolean,
     search?: string
   }) {
     const query = {
@@ -319,7 +323,7 @@ export class VideoPlaylistModel extends Model<VideoPlaylistModel> {
             followerActorId: options.followerActorId,
             accountId: options.accountId,
             videoChannelId: options.videoChannelId,
-            privateAndUnlisted: options.privateAndUnlisted,
+            listMyPlaylists: options.listMyPlaylists,
             search: options.search
           } as AvailableForListOptions
         ]