Repository navigation
Show a Time's up screen when an access schedule ends #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
benklop
wants to merge
3
commits into
Moonfin-Client:master
Choose a base branch
from
benklop:feat/times-up-enforcement
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import "pkg:/source/api/baserequest.bs" | ||
| import "pkg:/source/utils/accessSchedule.bs" | ||
| import "pkg:/source/utils/misc.bs" | ||
|
|
||
| sub init() | ||
| m.top.functionName = "readSchedule" | ||
| end sub | ||
|
|
||
| sub readSchedule() | ||
| m.top.result = loadSchedule(m.top.userId) | ||
| end sub | ||
|
|
||
| function loadSchedule(userId as string) as object | ||
| failed = { ok: false, exempt: false, entries: [] } | ||
| if not isValidAndNotEmpty(userId) then return failed | ||
|
|
||
| response = requestResponse(APIRequest("Users/" + userId), "GET") | ||
| if response.status <> 200 or type(response.data) <> "roAssociativeArray" then return failed | ||
|
|
||
| policy = response.data.Policy | ||
| if type(policy) <> "roAssociativeArray" then return failed | ||
|
|
||
| exempt = policy.IsAdministrator = true | ||
| entries = accessSchedule.Read(policy) | ||
| refused = invalid | ||
| if not exempt and entries.Count() > 0 then refused = accessSchedule.RefusedBy(requestResponse(APIRequest("Users/Me"), "GET").status) | ||
| return { ok: true, exempt: exempt, entries: entries, refused: refused } | ||
| end function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- Reads the access schedule in a user's policy. GET /Users/{id} keeps answering outside the | ||
| schedule, and /Users/Me says whether the server is turning the user away. --> | ||
| <component name="AccessScheduleTask" extends="Task"> | ||
| <interface> | ||
| <field id="userId" type="string" /> | ||
| <!-- { ok, exempt, entries, refused }: ok is false when the policy couldn't be read, and refused | ||
| is invalid when the server wasn't asked or gave no answer --> | ||
| <field id="result" type="assocarray" alwaysNotify="true" /> | ||
| </interface> | ||
| </component> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import "pkg:/source/utils/accessSchedule.bs" | ||
| import "pkg:/source/utils/misc.bs" | ||
|
|
||
| ' How long to wait between looks when no opening or closing is nearer, for a user with a schedule | ||
| ' and for one without, and before asking again after a look that failed | ||
| const POLL_SECONDS = 60 | ||
| const IDLE_SECONDS = 900 | ||
| const RETRY_SECONDS = 15 | ||
| ' The least time between looks prompted by the server turning a request away, which can come in | ||
| ' bursts | ||
| const REFUSAL_SECONDS = 5 | ||
|
|
||
| sub init() | ||
| m.timer = m.top.findNode("timer") | ||
| m.timer.observeField("fire", "onTimer") | ||
| m.entries = [] | ||
| m.exempt = false | ||
| m.task = invalid | ||
| m.lookAgain = false | ||
| m.lastRefusalLook = 0 | ||
| m.clock = CreateObject("roTimespan") | ||
| m.top.blocked = false | ||
|
|
||
| if m.global.hasField("accessRefused") then m.global.observeField("accessRefused", "onRefused") | ||
| end sub | ||
|
|
||
| sub onUserChange() | ||
| m.timer.control = "stop" | ||
| ' A look still under way for the previous user must not answer for the next | ||
| cancelTask() | ||
| m.entries = [] | ||
| m.exempt = false | ||
| m.lookAgain = false | ||
| setBlocked(false, invalid) | ||
| if m.top.userId <> "" then look() | ||
| end sub | ||
|
|
||
| sub recheck() | ||
| if m.top.userId <> "" then look() | ||
| end sub | ||
|
|
||
| sub onRefused() | ||
| ' While blocked every request is refused, the server check included, and a look under way | ||
| ' already brings the newest answer | ||
| if m.top.userId = "" or m.top.blocked or m.task <> invalid then return | ||
| now = m.clock.TotalMilliseconds() | ||
| if m.lastRefusalLook <> 0 and now - m.lastRefusalLook < REFUSAL_SECONDS * 1000 then return | ||
| m.lastRefusalLook = now | ||
| look() | ||
| end sub | ||
|
|
||
| sub onTimer() | ||
| look() | ||
| end sub | ||
|
|
||
| sub cancelTask() | ||
| if m.task <> invalid | ||
| m.task.unobserveField("result") | ||
| m.task.control = "stop" | ||
| m.task = invalid | ||
| end if | ||
| end sub | ||
|
|
||
| ' Reads the schedule again. One look at a time: a request that arrives while one is under way | ||
| ' asks for another when it ends. | ||
| sub look() | ||
| m.timer.control = "stop" | ||
| if m.task <> invalid | ||
| m.lookAgain = true | ||
| return | ||
| end if | ||
| m.task = CreateObject("roSGNode", "AccessScheduleTask") | ||
| m.task.userId = m.top.userId | ||
| m.task.observeField("result", "onResult") | ||
| m.task.control = "run" | ||
| end sub | ||
|
|
||
| sub onResult(event as object) | ||
| result = event.getData() | ||
| m.task.unobserveField("result") | ||
| m.task = invalid | ||
|
|
||
| if result.ok | ||
| m.entries = result.entries | ||
| m.exempt = result.exempt | ||
| apply(result.refused) | ||
| else | ||
| ' A schedule that couldn't be read leaves the last answer in place | ||
| startTimer(RETRY_SECONDS) | ||
| end if | ||
|
|
||
| if m.lookAgain | ||
| m.lookAgain = false | ||
| look() | ||
| end if | ||
| end sub | ||
|
|
||
| sub apply(refused as dynamic) | ||
| now = CreateObject("roDateTime") | ||
| now.ToLocalTime() | ||
| day = now.GetDayOfWeek() | ||
| secondOfDay = now.GetHours() * 3600 + now.GetMinutes() * 60 + now.GetSeconds() | ||
|
|
||
| status = accessSchedule.Status(m.entries, refused, day, secondOfDay) | ||
| if m.exempt or not status.blocked | ||
| setBlocked(false, invalid) | ||
| else | ||
| setBlocked(true, status.next) | ||
| end if | ||
|
|
||
| ' An administrator has no schedule to enforce, so only a refused request looks again | ||
| if m.exempt then return | ||
| delay = POLL_SECONDS | ||
| if m.entries.Count() = 0 then delay = IDLE_SECONDS | ||
| boundary = accessSchedule.SecondsToBoundary(m.entries, day, secondOfDay) | ||
| if boundary <> invalid and boundary < delay then delay = boundary | ||
| startTimer(delay) | ||
| end sub | ||
|
|
||
| sub startTimer(seconds as integer) | ||
| if seconds < 1 then seconds = 1 | ||
| m.timer.duration = seconds | ||
| m.timer.control = "start" | ||
| end sub | ||
|
|
||
| ' Told only when the answer changes, so the app stops playback once for each time the user is | ||
| ' turned away. The next opening is kept current while blocked, since a changed schedule moves it. | ||
| sub setBlocked(blocked as boolean, upcoming as dynamic) | ||
| if blocked = m.top.blocked | ||
| if blocked then m.top.next = upcoming | ||
| return | ||
| end if | ||
| m.top.next = upcoming | ||
| m.top.blocked = blocked | ||
| m.top.accessStatus = blocked | ||
| end sub | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- Watches the signed in user's access schedule and says when it closes. Jellyfin turns a user away | ||
| outside their schedule, so nearly every request fails with it. For a user with a schedule the | ||
| server's answer decides. It looks again just before an opening or closing, when a request is | ||
| refused, and every minute for a user with a schedule or every 15 minutes for one without. --> | ||
| <component name="AccessScheduleWatcher" extends="Node"> | ||
| <interface> | ||
| <!-- The user to watch, or an empty string to stop --> | ||
| <field id="userId" type="string" onChange="onUserChange" /> | ||
| <field id="blocked" type="boolean" value="false" /> | ||
| <!-- When the user is next let in, as { seconds, dayOffset, secondOfDay, day }, while blocked --> | ||
| <field id="next" type="assocarray" /> | ||
| <!-- Told only when the answer changes --> | ||
| <field id="accessStatus" type="boolean" alwaysNotify="true" /> | ||
| <function name="recheck" /> | ||
| </interface> | ||
| <children> | ||
| <Timer id="timer" repeat="false" /> | ||
| </children> | ||
| </component> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import "pkg:/source/utils/themeApply.bs" | ||
|
|
||
| const CARD_WIDTH = 640 | ||
| const CARD_PAD = 32 | ||
| const GAP = 14 | ||
| const BUTTON_WIDTH = 260 | ||
| const BUTTON_HEIGHT = 64 | ||
|
|
||
| sub init() | ||
| m.top.optionsAvailable = false | ||
| ' The screen owns the whole display, so the nav bar is hidden | ||
| m.top.overhangVisible = false | ||
| m.card = m.top.findNode("card") | ||
| m.cardFill = m.top.findNode("cardFill") | ||
| m.cardBorder = m.top.findNode("cardBorder") | ||
| m.title = m.top.findNode("title") | ||
| m.message = m.top.findNode("message") | ||
| m.nextLabel = m.top.findNode("next") | ||
| m.switchUser = m.top.findNode("switchUser") | ||
|
|
||
| ' The theme replaces the labels' fonts, so the sizes are set after it | ||
| themeApply.ApplyToItem(m.top) | ||
| width = CARD_WIDTH - CARD_PAD * 2 | ||
| m.title.font.size = 48 | ||
| m.title.width = width | ||
| m.title.text = tr("Time's up") | ||
| m.message.font.size = 28 | ||
| m.message.width = width | ||
| m.message.text = tr("This account isn't available right now.") | ||
| m.nextLabel.font.size = 28 | ||
| m.nextLabel.width = width | ||
|
|
||
| m.switchUser.text = tr("Switch User") | ||
| m.switchUser.width = BUTTON_WIDTH | ||
| m.switchUser.height = BUTTON_HEIGHT | ||
| m.switchUser.observeField("buttonSelected", "onSwitchUser") | ||
|
|
||
| layoutCard() | ||
| end sub | ||
|
|
||
| sub OnScreenShown(_isReturning = false as boolean) | ||
| layoutCard() | ||
| m.switchUser.setFocus(true) | ||
| end sub | ||
|
|
||
| sub onNextChange() | ||
| m.nextLabel.text = describeNext(m.top.next) | ||
| layoutCard() | ||
| end sub | ||
|
|
||
| ' When the user can come back, in words. Empty when no entry opens soon. | ||
| function describeNext(opening as dynamic) as string | ||
| if type(opening) <> "roAssociativeArray" then return "" | ||
| time = timeOfDayText(opening.secondOfDay) | ||
| if opening.dayOffset = 0 then return Substitute(tr("You can use it again today at {0}."), time) | ||
| if opening.dayOffset = 1 then return Substitute(tr("You can use it again tomorrow at {0}."), time) | ||
| days = [tr("Sunday"), tr("Monday"), tr("Tuesday"), tr("Wednesday"), tr("Thursday"), tr("Friday"), tr("Saturday")] | ||
| ' A week out is the same weekday as today, which would read as today, so the date is named too | ||
| if opening.dayOffset >= 7 | ||
| months = [tr("Jan"), tr("Feb"), tr("Mar"), tr("Apr"), tr("May"), tr("Jun"), tr("Jul"), tr("Aug"), tr("Sep"), tr("Oct"), tr("Nov"), tr("Dec")] | ||
| date = CreateObject("roDateTime") | ||
| date.FromSeconds(date.AsSeconds() + opening.seconds) | ||
| date.ToLocalTime() | ||
| return Substitute(tr("You can use it again on {0}, {1} {2} at {3}."), days[opening.day], months[date.GetMonth() - 1], date.GetDayOfMonth().ToStr(), time) | ||
| end if | ||
| return Substitute(tr("You can use it again on {0} at {1}."), days[opening.day], time) | ||
| end function | ||
|
|
||
| ' Hours and minutes in the clock style the device is set to | ||
| function timeOfDayText(secondOfDay as integer) as string | ||
| hours = secondOfDay \ 3600 | ||
| minutes = (secondOfDay mod 3600) \ 60 | ||
| mm = minutes.ToStr() | ||
| if minutes < 10 then mm = "0" + mm | ||
| if CreateObject("roDeviceInfo").GetClockFormat() = "12h" | ||
| suffix = "AM" | ||
| if hours >= 12 then suffix = "PM" | ||
| shown = hours mod 12 | ||
| if shown = 0 then shown = 12 | ||
| return shown.ToStr() + ":" + mm + " " + suffix | ||
| end if | ||
| hh = hours.ToStr() | ||
| if hours < 10 then hh = "0" + hh | ||
| return hh + ":" + mm | ||
| end function | ||
|
|
||
| ' The line about the next opening only takes room while it has something to say | ||
| sub layoutCard() | ||
| y = CARD_PAD | ||
|
|
||
| m.title.translation = [CARD_PAD, y] | ||
| y += m.title.boundingRect().height + GAP | ||
|
|
||
| m.message.translation = [CARD_PAD, y] | ||
| y += m.message.boundingRect().height | ||
|
|
||
| m.nextLabel.visible = m.nextLabel.text <> "" | ||
| if m.nextLabel.visible | ||
| y += GAP | ||
| m.nextLabel.translation = [CARD_PAD, y] | ||
| y += m.nextLabel.boundingRect().height | ||
| end if | ||
|
|
||
| y += CARD_PAD | ||
| m.switchUser.translation = [(CARD_WIDTH - BUTTON_WIDTH) / 2, y] | ||
| y += BUTTON_HEIGHT + CARD_PAD | ||
|
|
||
| for each layer in [m.cardFill, m.cardBorder] | ||
| layer.width = CARD_WIDTH | ||
| layer.height = y | ||
| end for | ||
| m.card.translation = [(1920 - CARD_WIDTH) / 2, (1080 - y) / 2] | ||
| end sub | ||
|
|
||
| sub onSwitchUser() | ||
| m.top.switchUser = true | ||
| end sub | ||
|
|
||
| ' Nothing gets past the screen to the one behind it, and Back does not leave it | ||
| function onKeyEvent(key as string, press as boolean) as boolean | ||
| return true | ||
| end function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- Covers the app when the signed in user's access schedule has closed. Nothing behind it can take | ||
| focus, and the only way out is to pick another user or wait for the hours to open. --> | ||
| <component name="TimesUp" extends="JFScreen"> | ||
| <children> | ||
| <Rectangle width="1920" height="1080" color="#020814" /> | ||
| <Group id="card"> | ||
| <Poster id="cardFill" uri="pkg:/images/details/surface12.9.png" blendColor="#070E1E" opacity="0.9" /> | ||
| <Poster id="cardBorder" uri="pkg:/images/details/cardborder12.9.png" blendColor="#FFFFFF29" /> | ||
| <Label id="title" font="font:LargeBoldSystemFont" color="#FFFFFF" horizAlign="center" /> | ||
| <Label id="message" font="font:SmallSystemFont" color="#FFFFFFC2" wrap="true" horizAlign="center" /> | ||
| <Label id="next" font="font:SmallSystemFont" color="#FFFFFFC2" wrap="true" horizAlign="center" /> | ||
| <StandardButton | ||
| id="switchUser" | ||
| background="0x2B6AE5E6" | ||
| focusBackground="0x4783FFF2" | ||
| color="0xFFFFFFFF" | ||
| focusColor="0xFFFFFFFF" /> | ||
| </Group> | ||
| </children> | ||
| <interface> | ||
| <!-- { seconds, dayOffset, secondOfDay, day } from the watcher, or invalid when no entry opens soon --> | ||
| <field id="next" type="assocarray" onChange="onNextChange" /> | ||
| <field id="switchUser" type="boolean" alwaysNotify="true" /> | ||
| </interface> | ||
| </component> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: Moonfin-Client/Roku
Length of output: 4942
🏁 Script executed:
Repository: Moonfin-Client/Roku
Length of output: 42525
🏁 Script executed:
Repository: Moonfin-Client/Roku
Length of output: 33789
🌐 Web query:
Jellyfin source AccessSchedules DayOfWeek StartHour EndHour server local time UserPolicy💡 Result:
🏁 Script executed:
Repository: Moonfin-Client/Roku
Length of output: 22931
🌐 Web query:
site:github.com/jellyfin/jellyfin AccessSchedules IsParentalScheduleAllowed DateTime local time source💡 Result:
🏁 Script executed:
Repository: Moonfin-Client/Roku
Length of output: 12044
🌐 Web query:
Jellyfin v10.10 UserEntityExtensions.cs IsParentalScheduleAllowed DateTime.UtcNow ToLocalTime💡 Result:
🏁 Script executed:
Repository: Moonfin-Client/Roku
Length of output: 6017
🌐 Web query:
Jellyfin v10.9.0 raw UserEntityExtensions.cs AccessSchedules DateTime ToLocalTime IsParentalScheduleAllowed💡 Result:
🏁 Script executed:
Repository: Moonfin-Client/Roku
Length of output: 634
🏁 Script executed:
Repository: Moonfin-Client/Roku
Length of output: 4754
Evaluate the schedule in server-local time.
AccessScheduleWatcher.applycallsToLocalTime()on the Roku, then passes the Roku-local weekday and seconds to all three schedule functions. Jellyfin 10.11.10 callsDateTime.UtcNow.ToLocalTime()on the server before evaluatingAccessSchedules. For example, with a UTC server and a Roku in UTC−5, a Monday 09:00–17:00 schedule is already open at 05:00 Roku time, but the watcher can mark it blocked and report the wrong next opening. At 18:00 UTC, the inverse can leave the watcher unblocked after the server has closed access.Keep
accessScheduleinputs in server-local time. CorrectAccessScheduleWatcher.applyto use the server's current time or an authoritative server schedule result. Do not fix this by changing the helper's weekday or hour comparisons.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't possible until we have a way to actually retrieve server local time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benklop Thanks for clarifying. My recommendation assumed a server-local time source that the client does not have. I should not have presented this as a watcher-only fix.
The time-zone mismatch remains a limitation. Would you like a follow-up issue to track the missing server-time support?
✏️ Learnings added
You are interacting with an AI system.