2023-06-01 12:43:27 +00:00
|
|
|
' needed for SignOut() and ServerInfo()
|
|
|
|
import "pkg:/source/utils/session.bs"
|
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
function get_token(user as string, password as string)
|
|
|
|
url = "Users/AuthenticateByName?format=json"
|
|
|
|
req = APIRequest(url)
|
2019-03-19 02:59:23 +00:00
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
json = postJson(req, FormatJson({ "Username": user, "Pw": password }))
|
2020-06-26 16:55:43 +00:00
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
if json = invalid then return invalid
|
2019-03-19 02:59:23 +00:00
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
userdata = CreateObject("roSGNode", "UserData")
|
|
|
|
userdata.json = json
|
2019-04-20 17:40:06 +00:00
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
userdata.callFunc("setActive")
|
|
|
|
userdata.callFunc("saveToRegistry")
|
|
|
|
return userdata
|
2019-03-19 02:59:23 +00:00
|
|
|
end function
|
|
|
|
|
2023-06-01 12:43:27 +00:00
|
|
|
function AboutMe(id = "" as string)
|
|
|
|
if id = ""
|
|
|
|
if m.global.session.user.id <> invalid
|
|
|
|
id = m.global.session.user.id
|
|
|
|
else
|
|
|
|
return invalid
|
|
|
|
end if
|
|
|
|
end if
|
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
url = Substitute("Users/{0}", id)
|
|
|
|
resp = APIRequest(url)
|
|
|
|
return getJson(resp)
|
2019-03-19 02:59:23 +00:00
|
|
|
end function
|
2019-03-19 04:17:50 +00:00
|
|
|
|
2021-12-30 01:00:13 +00:00
|
|
|
sub SignOut(deleteSavedEntry = true as boolean)
|
2023-09-14 15:52:32 +00:00
|
|
|
if m.global.session.user.id <> invalid and deleteSavedEntry = true
|
2021-07-09 20:08:32 +00:00
|
|
|
unset_user_setting("token")
|
2023-09-16 23:40:57 +00:00
|
|
|
unset_user_setting("username")
|
2021-07-09 20:08:32 +00:00
|
|
|
end if
|
|
|
|
unset_setting("active_user")
|
2023-06-01 12:43:27 +00:00
|
|
|
session.user.Logout()
|
2021-10-10 01:51:33 +00:00
|
|
|
m.global.sceneManager.currentUser = ""
|
|
|
|
group = m.global.sceneManager.callFunc("getActiveScene")
|
2021-10-09 19:02:34 +00:00
|
|
|
group.optionsAvailable = false
|
2021-06-26 10:18:09 +00:00
|
|
|
end sub
|
2019-04-20 17:54:24 +00:00
|
|
|
|
2019-04-22 19:08:10 +00:00
|
|
|
function AvailableUsers()
|
2021-07-09 20:08:32 +00:00
|
|
|
users = parseJson(get_setting("available_users", "[]"))
|
|
|
|
return users
|
2019-04-22 19:08:10 +00:00
|
|
|
end function
|
|
|
|
|
2019-04-20 17:54:24 +00:00
|
|
|
function ServerInfo()
|
2021-07-09 20:08:32 +00:00
|
|
|
url = "System/Info/Public"
|
|
|
|
req = APIRequest(url)
|
|
|
|
|
|
|
|
req.setMessagePort(CreateObject("roMessagePort"))
|
|
|
|
req.AsyncGetToString()
|
|
|
|
|
|
|
|
' wait 15 seconds for a server response
|
|
|
|
resp = wait(35000, req.GetMessagePort())
|
|
|
|
|
|
|
|
' handle unknown errors
|
|
|
|
if type(resp) <> "roUrlEvent"
|
|
|
|
return { "Error": true, "ErrorMessage": "Unknown" }
|
|
|
|
end if
|
|
|
|
|
|
|
|
' check for a location redirect header in the response
|
|
|
|
headers = resp.GetResponseHeaders()
|
|
|
|
if headers <> invalid and headers.location <> invalid
|
|
|
|
|
|
|
|
' only follow redirect if it the API Endpoint path is the same (/System/Info/Public)
|
|
|
|
' set the server to new location and try again
|
|
|
|
if right(headers.location, 19) = "/System/Info/Public"
|
|
|
|
set_setting("server", left(headers.location, len(headers.location) - 19))
|
2023-06-24 17:01:42 +00:00
|
|
|
isConnected = session.server.UpdateURL(left(headers.location, len(headers.location) - 19))
|
|
|
|
if isConnected
|
|
|
|
info = ServerInfo()
|
|
|
|
if info.Error
|
|
|
|
info.UpdatedUrl = left(headers.location, len(headers.location) - 19)
|
|
|
|
info.ErrorMessage = info.ErrorMessage + " (Note: Server redirected us to " + info.UpdatedUrl + ")"
|
|
|
|
end if
|
|
|
|
return info
|
2021-07-09 20:08:32 +00:00
|
|
|
end if
|
|
|
|
end if
|
2021-04-04 13:41:44 +00:00
|
|
|
end if
|
2021-07-09 20:08:32 +00:00
|
|
|
|
|
|
|
' handle any non 200 responses, returning the error code and message
|
|
|
|
if resp.GetResponseCode() <> 200
|
|
|
|
return { "Error": true, "ErrorCode": resp.GetResponseCode(), "ErrorMessage": resp.GetFailureReason() }
|
2021-04-04 13:41:44 +00:00
|
|
|
end if
|
|
|
|
|
2021-07-09 20:08:32 +00:00
|
|
|
' return the parsed response string
|
|
|
|
responseString = resp.GetString()
|
|
|
|
if responseString <> invalid and responseString <> ""
|
|
|
|
result = ParseJson(responseString)
|
|
|
|
if result <> invalid
|
|
|
|
result.Error = false
|
|
|
|
return result
|
|
|
|
end if
|
|
|
|
end if
|
|
|
|
|
|
|
|
' otherwise return error message
|
|
|
|
return { "Error": true, "ErrorMessage": "Does not appear to be a Jellyfin Server" }
|
2021-04-04 13:41:44 +00:00
|
|
|
|
2019-04-20 17:54:24 +00:00
|
|
|
end function
|
2020-03-21 21:22:26 +00:00
|
|
|
|
|
|
|
function GetPublicUsers()
|
2021-07-09 20:08:32 +00:00
|
|
|
url = "Users/Public"
|
|
|
|
resp = APIRequest(url)
|
|
|
|
return getJson(resp)
|
2020-03-21 21:22:26 +00:00
|
|
|
end function
|
2020-11-29 11:18:23 +00:00
|
|
|
|
|
|
|
' Load and parse Display Settings from server
|
|
|
|
sub LoadUserPreferences()
|
2023-06-01 12:43:27 +00:00
|
|
|
id = m.global.session.user.id
|
2021-07-09 20:08:32 +00:00
|
|
|
' 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
|
|
|
|
url = Substitute("DisplayPreferences/usersettings?userId={0}&client=emby", id)
|
|
|
|
resp = APIRequest(url)
|
|
|
|
jsonResponse = getJson(resp)
|
|
|
|
|
2022-07-12 23:15:59 +00:00
|
|
|
if jsonResponse <> invalid and jsonResponse.CustomPrefs <> invalid and jsonResponse.CustomPrefs["landing-livetv"] <> invalid
|
|
|
|
set_user_setting("display.livetv.landing", jsonResponse.CustomPrefs["landing-livetv"])
|
2021-07-09 20:08:32 +00:00
|
|
|
else
|
2022-07-12 23:15:59 +00:00
|
|
|
unset_user_setting("display.livetv.landing")
|
2021-07-09 20:08:32 +00:00
|
|
|
end if
|
2022-01-09 06:31:27 +00:00
|
|
|
end sub
|
|
|
|
|
2023-06-01 12:43:27 +00:00
|
|
|
sub LoadUserAbilities()
|
|
|
|
if m.global.session.user.Policy.EnableLiveTvManagement = true
|
2022-01-09 06:31:27 +00:00
|
|
|
set_user_setting("livetv.canrecord", "true")
|
2022-01-09 14:56:52 +00:00
|
|
|
else
|
2022-01-09 06:31:27 +00:00
|
|
|
set_user_setting("livetv.canrecord", "false")
|
|
|
|
end if
|
2023-06-01 12:43:27 +00:00
|
|
|
if m.global.session.user.Policy.EnableContentDeletion = true
|
2023-02-07 21:56:24 +00:00
|
|
|
set_user_setting("content.candelete", "true")
|
|
|
|
else
|
|
|
|
set_user_setting("content.candelete", "false")
|
|
|
|
end if
|
2022-01-09 06:31:27 +00:00
|
|
|
end sub
|
2022-05-29 20:00:38 +00:00
|
|
|
|
|
|
|
function initQuickConnect()
|
|
|
|
resp = APIRequest("QuickConnect/Initiate")
|
|
|
|
jsonResponse = getJson(resp)
|
|
|
|
if jsonResponse = invalid
|
|
|
|
return invalid
|
|
|
|
end if
|
|
|
|
|
|
|
|
if jsonResponse.Secret = invalid
|
|
|
|
return invalid
|
|
|
|
end if
|
|
|
|
|
|
|
|
return jsonResponse
|
|
|
|
end function
|
|
|
|
|
|
|
|
function checkQuickConnect(secret)
|
|
|
|
url = Substitute("QuickConnect/Connect?secret={0}", secret)
|
|
|
|
resp = APIRequest(url)
|
|
|
|
jsonResponse = getJson(resp)
|
|
|
|
if jsonResponse = invalid
|
|
|
|
return false
|
|
|
|
end if
|
|
|
|
|
|
|
|
if jsonResponse.Authenticated <> invalid and jsonResponse.Authenticated = true
|
|
|
|
return true
|
|
|
|
end if
|
|
|
|
|
|
|
|
return false
|
|
|
|
end function
|
|
|
|
|
|
|
|
function AuthenticateViaQuickConnect(secret)
|
|
|
|
params = {
|
|
|
|
secret: secret
|
|
|
|
}
|
|
|
|
req = APIRequest("Users/AuthenticateWithQuickConnect")
|
|
|
|
jsonResponse = postJson(req, FormatJson(params))
|
2023-06-01 12:43:27 +00:00
|
|
|
if jsonResponse <> invalid and jsonResponse.AccessToken <> invalid and jsonResponse.User <> invalid
|
2022-05-29 20:00:38 +00:00
|
|
|
userdata = CreateObject("roSGNode", "UserData")
|
|
|
|
userdata.json = jsonResponse
|
2023-06-01 12:43:27 +00:00
|
|
|
session.user.Update("id", jsonResponse.User.Id)
|
|
|
|
session.user.Update("authToken", jsonResponse.AccessToken)
|
2022-05-29 20:00:38 +00:00
|
|
|
userdata.callFunc("setActive")
|
|
|
|
userdata.callFunc("saveToRegistry")
|
|
|
|
return true
|
|
|
|
end if
|
|
|
|
|
|
|
|
return false
|
|
|
|
end function
|