From a78dcca4f7722e20e799095635b9457c66db244e Mon Sep 17 00:00:00 2001 From: 1hitsong <3330318+1hitsong@users.noreply.github.com> Date: Fri, 27 Oct 2023 10:51:09 -0400 Subject: [PATCH 1/4] Calculate device id on login and use for API calls Fixes #1417 --- components/GetPlaybackInfoTask.brs | 2 +- source/VideoPlayer.brs | 3 ++- source/api/sdk.bs | 2 +- source/utils/globals.brs | 1 + source/utils/session.bs | 7 +++++++ 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/components/GetPlaybackInfoTask.brs b/components/GetPlaybackInfoTask.brs index 0cd6374d..14702874 100644 --- a/components/GetPlaybackInfoTask.brs +++ b/components/GetPlaybackInfoTask.brs @@ -35,7 +35,7 @@ end function ' Returns an array of playback info to be displayed during playback. ' In the future, with a custom playback info view, we can return an associated array. sub getPlaybackInfoTask() - sessions = api.sessions.Get() + sessions = api.sessions.Get({ "deviceId": m.global.device.serverDeviceName }) m.playbackInfo = ItemPostPlaybackInfo(m.top.videoID) diff --git a/source/VideoPlayer.brs b/source/VideoPlayer.brs index d8615ec7..f992b1cd 100644 --- a/source/VideoPlayer.brs +++ b/source/VideoPlayer.brs @@ -468,7 +468,8 @@ end sub ' Returns an array of playback info to be displayed during playback. ' In the future, with a custom playback info view, we can return an associated array. function GetPlaybackInfo() - sessions = api.sessions.Get() + sessions = api.sessions.Get({ "deviceId": m.global.device.serverDeviceName }) + if isValid(sessions) and sessions.Count() > 0 return GetTranscodingStats(sessions[0]) end if diff --git a/source/api/sdk.bs b/source/api/sdk.bs index 51bca783..45be12d8 100644 --- a/source/api/sdk.bs +++ b/source/api/sdk.bs @@ -1374,7 +1374,7 @@ namespace api end function ' Gets a list of sessions. - function Get(params = { "deviceId": m.global.device.id } as object) + function Get(params = {} as object) req = APIRequest("/sessions", params) return getJson(req) end function diff --git a/source/utils/globals.brs b/source/utils/globals.brs index 538ce30d..5006641c 100644 --- a/source/utils/globals.brs +++ b/source/utils/globals.brs @@ -107,6 +107,7 @@ sub SaveDeviceToGlobal() uuid: deviceInfo.GetRandomUUID(), name: displayName, friendlyName: filteredFriendly, + serverDeviceName: "", model: deviceInfo.GetModel(), modelType: deviceInfo.GetModelType(), modelDetails: deviceInfo.GetModelDetails(), diff --git a/source/utils/session.bs b/source/utils/session.bs index 9b5665a7..d8e6dfc9 100644 --- a/source/utils/session.bs +++ b/source/utils/session.bs @@ -177,6 +177,13 @@ namespace session ' Load and parse Display Settings from server sub LoadUserPreferences() + ' Save device id so we don't calculate it every time we need it + if isValid(m.global.session.user) and isValid(m.global.session.user.friendlyName) + m.global.device.serverDeviceName = m.global.device.id + m.global.session.user.friendlyName + else + m.global.device.serverDeviceName = m.global.device.id + end if + id = m.global.session.user.id ' Currently using client "emby", which is what website uses so we get same Display prefs as web. ' May want to change to specific Roku display settings From f3c04377c159e67a55bdde118c55ea5ed0d7f863 Mon Sep 17 00:00:00 2001 From: 1hitsong <3330318+1hitsong@users.noreply.github.com> Date: Fri, 27 Oct 2023 10:53:33 -0400 Subject: [PATCH 2/4] Use device severDeviceName in base request --- source/api/baserequest.brs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/source/api/baserequest.brs b/source/api/baserequest.brs index 10346133..d68fcba4 100644 --- a/source/api/baserequest.brs +++ b/source/api/baserequest.brs @@ -205,11 +205,7 @@ function authRequest(request as object) as object auth = auth + ", UserId=" + QUOTE + m.global.session.user.id + QUOTE end if - if m.global.session.user <> invalid and m.global.session.user.friendlyName <> invalid - auth = auth + ", DeviceId=" + QUOTE + m.global.device.id + m.global.session.user.friendlyName + QUOTE - else - auth = auth + ", DeviceId=" + QUOTE + m.global.device.id + QUOTE - end if + auth = auth + ", DeviceId=" + QUOTE + m.global.device.serverDeviceName + QUOTE if m.global.session.user.authToken <> invalid auth = auth + ", Token=" + QUOTE + m.global.session.user.authToken + QUOTE From 16ccfe29308aaad1ccb42f1b42d37ae007e33512 Mon Sep 17 00:00:00 2001 From: 1hitsong <3330318+1hitsong@users.noreply.github.com> Date: Sat, 28 Oct 2023 08:13:31 -0400 Subject: [PATCH 3/4] Code cleanup based on review --- source/ShowScenes.brs | 4 ---- source/api/sdk.bs | 2 +- source/utils/globals.brs | 2 +- source/utils/session.bs | 16 +++++++++++++--- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/source/ShowScenes.brs b/source/ShowScenes.brs index b236aad3..28d38b84 100644 --- a/source/ShowScenes.brs +++ b/source/ShowScenes.brs @@ -119,7 +119,6 @@ function LoginFlow() else print "Success! Auth token is still valid" session.user.Login(currentUser, true) - session.user.LoadUserPreferences() LoadUserAbilities() return true end if @@ -132,7 +131,6 @@ function LoginFlow() if isValid(userData) print "login success!" session.user.Login(userData, true) - session.user.LoadUserPreferences() LoadUserAbilities() return true else @@ -175,7 +173,6 @@ function LoginFlow() if isValid(userData) print "login success!" session.user.Login(userData, true) - session.user.LoadUserPreferences() LoadUserAbilities() return true else @@ -201,7 +198,6 @@ function LoginFlow() goto start_login end if - session.user.LoadUserPreferences() LoadUserAbilities() m.global.sceneManager.callFunc("clearScenes") diff --git a/source/api/sdk.bs b/source/api/sdk.bs index 45be12d8..38ef4929 100644 --- a/source/api/sdk.bs +++ b/source/api/sdk.bs @@ -1374,7 +1374,7 @@ namespace api end function ' Gets a list of sessions. - function Get(params = {} as object) + function Get(params = { "deviceId": m.global.device.serverDeviceName } as object) req = APIRequest("/sessions", params) return getJson(req) end function diff --git a/source/utils/globals.brs b/source/utils/globals.brs index 5006641c..2c3dd2a7 100644 --- a/source/utils/globals.brs +++ b/source/utils/globals.brs @@ -107,7 +107,7 @@ sub SaveDeviceToGlobal() uuid: deviceInfo.GetRandomUUID(), name: displayName, friendlyName: filteredFriendly, - serverDeviceName: "", + serverDeviceName: deviceInfo.getChannelClientID(), model: deviceInfo.GetModel(), modelType: deviceInfo.GetModelType(), modelDetails: deviceInfo.GetModelDetails(), diff --git a/source/utils/session.bs b/source/utils/session.bs index d8e6dfc9..62b20731 100644 --- a/source/utils/session.bs +++ b/source/utils/session.bs @@ -173,16 +173,23 @@ namespace session set_user_setting("token", tmpSession.user.authToken) set_user_setting("username", tmpSession.user.name) end if + + session.user.LoadUserPreferences() end sub - ' Load and parse Display Settings from server - sub LoadUserPreferences() - ' Save device id so we don't calculate it every time we need it + ' Sets the global service device name value used by the API + sub SetServerDeviceName() if isValid(m.global.session.user) and isValid(m.global.session.user.friendlyName) m.global.device.serverDeviceName = m.global.device.id + m.global.session.user.friendlyName else m.global.device.serverDeviceName = m.global.device.id end if + end sub + + ' Load and parse Display Settings from server + sub LoadUserPreferences() + ' Save device id so we don't calculate it every time we need it + session.user.SetServerDeviceName() id = m.global.session.user.id ' Currently using client "emby", which is what website uses so we get same Display prefs as web. @@ -344,6 +351,9 @@ namespace session session.user.settings.Save(item, get_setting(item)) end if end for + + ' Reset server device name state + session.user.SetServerDeviceName() end sub ' Saves the user setting to the global session. From 0216cee4b211835b110ea5f61636a4286c3e52ae Mon Sep 17 00:00:00 2001 From: 1hitsong <3330318+1hitsong@users.noreply.github.com> Date: Sat, 28 Oct 2023 09:53:08 -0400 Subject: [PATCH 4/4] Update source/utils/session.bs Co-authored-by: Charles Ewert --- source/utils/session.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/utils/session.bs b/source/utils/session.bs index 62b20731..56548aa5 100644 --- a/source/utils/session.bs +++ b/source/utils/session.bs @@ -177,7 +177,7 @@ namespace session session.user.LoadUserPreferences() end sub - ' Sets the global service device name value used by the API + ' Sets the global server device name value used by the API sub SetServerDeviceName() if isValid(m.global.session.user) and isValid(m.global.session.user.friendlyName) m.global.device.serverDeviceName = m.global.device.id + m.global.session.user.friendlyName