import "pkg:/components/Services/Invidious/InvidiousService.bs" import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs" import "pkg:/components/Services/LoungeService/LoungeConstants.bs" import "pkg:/components/VideoQueue/Notifications/VideoQueueNotificationUtils.bs" namespace LoungeVideoQueue function SetPlaylist(videoQueueNode as object, invService as object, playlistData as object) HandleLoungePlaylist(videoQueueNode, invService, playlistData) end function function UpdatePlaylist(videoQueueNode as object, invService as object, playlistData as object) HandleLoungePlaylist(videoQueueNode, invService, playlistData) end function function HandleLoungePlaylist(videoQueueNode as object, invService as object, playlistData as object) as void videoIds = playlistData[FIELD_VIDEO_IDS] if StringUtils.IsNullOrEmpty(videoIds) videoQueueNode.clearQueue = true return end if videoIds = videoIds.Split(",") queueContent = videoQueueNode.content currentQueueNodes = queueContent.getChildren(-1, 0) queueNodesDict = {} for each node in currentQueueNodes queueNodesDict[node.id] = node end for instance = invalid newQueueNodes = [] for each vId in videoIds if queueNodesDict.DoesExist(vId) newQueueNodes.Push(queueNodesDict[vId]) else payload = CreateVideoPayload(vId) if instance = invalid instance = invService.GetInstance() end if contentNode = InvidiousContent.ToVideoContentNode(invalid, payload, instance) newQueueNodes.Push(contentNode) end if end for index = -1 currentIndex = playlistData[FIELD_CURRENT_INDEX] if IsString(currentIndex) currentIndex = CInt(Val(currentIndex)) index = currentIndex else index = videoQueueNode.index end if videoQueueNode.setQueueAndIndex = { videoNodes: newQueueNodes index: index } videoId = playlistData[FIELD_VIDEO_ID] timestamp = playlistData[FIELD_CURRENT_TIME] if not StringUtils.IsNullOrEmpty(videoId) if not StringUtils.IsNullOrEmpty(timestamp) timestamp = CInt(Val(timestamp)) if timestamp > 0 node = newQueueNodes[index] node.timestamp = timestamp end if end if videoQueueNode.play = true end if if StringUtils.IsNullOrEmpty(videoId) ' TODO:P1 - Handle the case where a video is removed from the queue ' with an index lower than the current index. In this case, the ' video queue index is not updated, and we end up highlighting the wrong ' video in the queue (the next video in the queue is highlighted instead of ' the video that was previously highlighted). This also leads to skipping ' a video that's supposed to play next. ' When a video is removed from the queue: ' - There's no videoId ' - There's no currentIndex ' - eventDetails is of the form {"eventType":"VIDEO_REMOVED","videoId":""} eventDetails = playlistData[FIELD_EVENT_DETAILS] if IsString(eventDetails) eventDetails = ParseJson(eventDetails) end if if eventDetails <> invalid and eventDetails["eventType"] = "VIDEO_ADDED" eventVideoId = eventDetails[FIELD_VIDEO_ID] if not StringUtils.IsNullOrEmpty(eventVideoId) preferences = videoQueueNode.preferences queueNotifications = preferences["misc.queue_notifications"] if queueNotifications payload = CreateVideoPayload(eventVideoId) if instance = invalid instance = invService.GetInstance() end if node = InvidiousContent.ToVideoContentNode(invalid, payload, instance) notifications = videoQueueNode.notifications hintVisible = videoQueueNode.player = invalid or not videoQueueNode.videoContainer.fullscreen VideoQueue.ShowNotifcation(notifications, node, hintVisible) end if end if end if end if end function function CreateVideoPayload(videoId as string) as object return { "type": "video" "videoId": videoId "videoThumbnails": [ { "quality": "maxres" "url": `https://i.ytimg.com/vi/${videoId}/maxresdefault.jpg` "width": 1280 "height": 720 } { "quality": "sddefault" "url": `https://i.ytimg.com/vi/${videoId}/sddefault.jpg` "width": 640 "height": 480 } { "quality": "high" "url": `https://i.ytimg.com/vi/${videoId}/hqdefault.jpg` "width": 480 "height": 360 } { "quality": "medium" "url": `https://i.ytimg.com/vi/${videoId}/mqdefault.jpg` "width": 320 "height": 180 } ] } end function end namespace