Watch history (#232)

* [PoC] Watch history

* Render watch history on web app

* Changelog

* Log out user on insufficient access
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2023-12-20 09:39:42 -05:00
1 parent f581d568d9
commit a1aed7d253
10 files changed
+92 -13

No files matched your search

+1
View File
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Medium video quality in the settings (For most videos, this is 360p)
- Invidious watch history feed
### Fixed
@@ -1,4 +1,7 @@
import "pkg:/components/Dialog/DialogUtils.bs"
import "pkg:/components/Services/Invidious/InvidiousUnregisterTokenTask.bs"
import "pkg:/components/VideoFeed/PaginationType.bs"
import "pkg:/source/asyncTask/asyncTask.bs"
import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/CryptoUtils.bs"
import "pkg:/source/utils/Logging.bs"
@@ -22,7 +25,7 @@ namespace Invidious
const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions"
const AUTH_SCOPES = "GET:feed,GET:playlists*,POST:tokens/*,POST:history*,:subscriptions*"
const AUTH_SCOPES = "GET:feed,GET:playlists*,POST:tokens/*,:history*,:subscriptions*"
const AUTH_PLAYLISTS = "/api/v1/auth/playlists"
const AUTH_SUBSCRIPTIONS = "/api/v1/auth/subscriptions"
@@ -50,7 +53,8 @@ namespace Invidious
ChannelInfoHandler: m.ChannelInfoHandler,
ChannelVideosHandler: m.ChannelVideosHandler,
ChannelPlaylistsHandler: m.ChannelPlaylistsHandler,
ChannelRelatedChannelsHandler: m.ChannelRelatedChannelsHandler
ChannelRelatedChannelsHandler: m.ChannelRelatedChannelsHandler,
WatchHistoryHandler: m.WatchHistoryHandler
}
end function
@@ -403,6 +407,31 @@ namespace Invidious
}
end function
function WatchHistoryHandler(m as object, feedSource as object, response as object) as object
m.DeleteExpiredToken(response)
if response.StatusCode() <> 200
return invalid
end if
json = response.Json()
if json = invalid
return invalid
end if
videos = []
for each item in json
videos.push({
"type": "video",
"videoId": item
})
end for
return {
items: videos
}
end function
function DeleteExpiredToken(response as object)
if response.StatusCode() = 403
LogWarn(`Invidious instance responded with 403: ${response.Text()}`)
@@ -422,12 +451,18 @@ namespace Invidious
return invalid
end if
if not m.VerifyTokenScope(authToken.token)
' TODO:P1 automatically logging out the user is not a good experience
' We can at least show a message to the user on why they are logged out
' TODO:P2 right now we're probably in a UI thread, and we can't unregister the token
' So we're just going to ignore it for now
' m.UnregisterToken(authToken)
' Right now we're probably in a UI thread, and we can't unregister the token
' Without a Task
StartAsyncTask(InvidiousUnregisterTokenTask, {
invidious: m.node,
authToken: authToken
})
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN)
message = `It looks like you are logged in to Invidious, but your access token does not have the required permissions.
For that reason, we have logged you out. Please log in again.
This can happen when Playlet is updated with new features.
We apologize for the inconvenience.`
DialogUtils.ShowDialog(message, "User logged out")
return invalid
end if
return authToken
@@ -128,7 +128,11 @@ namespace InvidiousContent
function VideoSetThumbnail(node as object, videoItem as object, instance as dynamic, quality = "medium" as string) as boolean
videoThumbnails = videoItem.videoThumbnails
if videoThumbnails = invalid or videoThumbnails.Count() = 0
node.thumbnail = "pkg:/images/thumbnail-missing.jpg"
if StringUtils.IsNullOrEmpty(instance)
node.thumbnail = "pkg:/images/thumbnail-missing.jpg"
else
node.thumbnail = `${instance}/vi/${videoItem.videoId}/mqdefault.jpg`
end if
return false
end if
url = invalid
@@ -0,0 +1,12 @@
import "InvidiousService.bs"
@asynctask
function InvidiousUnregisterTokenTask(input as object) as object
invidiousNode = input.invidious
authToken = input.authToken
service = new Invidious.InvidiousService(invidiousNode)
service.UnregisterToken(authToken)
return invalid
end function
@@ -25,6 +25,7 @@ namespace Http
response = context.response
task = context.server.task
playQueue = task.playQueue
invidiousNode = task.invidious
payload = request.Json()
if payload = invalid
@@ -39,7 +40,10 @@ namespace Http
return true
end if
contentNode = InvidiousContent.ToRowCellContentNode(payload, invalid)
service = new Invidious.InvidiousService(invidiousNode)
instance = service.GetInstance()
contentNode = InvidiousContent.ToRowCellContentNode(payload, instance)
playQueue@.AddToQueue(contentNode)
queue = playQueue@.GetQueue()
@@ -53,6 +57,7 @@ namespace Http
response = context.response
task = context.server.task
playQueue = task.playQueue
invidiousNode = task.invidious
payload = request.Json()
if payload = invalid
@@ -71,7 +76,10 @@ namespace Http
payload.timestamp = payload.timestamp.toInt()
end if
contentNode = InvidiousContent.ToRowCellContentNode(payload, invalid)
service = new Invidious.InvidiousService(invidiousNode)
instance = service.GetInstance()
contentNode = InvidiousContent.ToRowCellContentNode(payload, instance)
playlistIndex = IsInt(payload.playlistIndex) ? payload.playlistIndex : -1
if contentNode.type = "playlist" and playlistIndex = -1
playlistIndex = 0
@@ -72,3 +72,10 @@
title: Playlists
apiType: Invidious
endpoint: auth_playlists
- title: Watch history
feedSources:
- id: inv_auth_history
title: Watch history
apiType: Invidious
endpoint: watch_history
@@ -202,3 +202,10 @@ channel_channels:
responseHandler: ChannelRelatedChannelsHandler
paginationType: Continuation
tryCount: 3
watch_history:
title: Watch history
url: /api/v1/auth/history
authenticated: true
paginationType: Pages
responseHandler: WatchHistoryHandler
@@ -86,6 +86,7 @@
<div class="flex flex-col items-center">
<div class="w-64">
<VideoThumbnail
bind:videoId
bind:title
bind:videoThumbnails
bind:liveNow
@@ -94,7 +95,7 @@
bind:invidiousInstance
/>
</div>
<div class="m-4">{title}</div>
<div class="m-4">{title || ""}</div>
</div>
<div class="flex flex-col">
<VideoStartAt
@@ -142,6 +142,7 @@
<button class="w-80 p-2" on:click={videoModal.show()}>
<div class="card card-compact bg-base-100 shadow-xl border border-neutral">
<VideoThumbnail
bind:videoId
bind:title
bind:videoThumbnails
bind:liveNow
@@ -150,7 +151,7 @@
bind:invidiousInstance
/>
<div class="card-body">
<h3 class="card-title text-base line-clamp-2 min-h-12">{title}</h3>
<h3 class="card-title text-base line-clamp-2 min-h-12">{title || ""}</h3>
<button
class="font-semibold link"
on:click={(e) => {
@@ -158,7 +159,7 @@
channelModal.show();
}}
>
{author}
{author || ""}
</button>
<div>{getViewCountDateText()}</div>
</div>
@@ -1,4 +1,5 @@
<script lang="ts">
export let videoId: string | undefined = undefined;
export let title: string | undefined = undefined;
export let videoThumbnails: any[] | undefined = undefined;
export let liveNow: boolean = undefined;
@@ -19,6 +20,8 @@
url = invidiousInstance + url;
}
thumbnailUrl = url;
} else if (invidiousInstance) {
thumbnailUrl = `${invidiousInstance}/vi/${videoId || "--"}/mqdefault.jpg`;
}
}