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 ### Added
- Medium video quality in the settings (For most videos, this is 360p) - Medium video quality in the settings (For most videos, this is 360p)
- Invidious watch history feed
### Fixed ### 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:/components/VideoFeed/PaginationType.bs"
import "pkg:/source/asyncTask/asyncTask.bs"
import "pkg:/source/services/HttpClient.bs" import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/CryptoUtils.bs" import "pkg:/source/utils/CryptoUtils.bs"
import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Logging.bs"
@@ -22,7 +25,7 @@ namespace Invidious
const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions" 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_PLAYLISTS = "/api/v1/auth/playlists"
const AUTH_SUBSCRIPTIONS = "/api/v1/auth/subscriptions" const AUTH_SUBSCRIPTIONS = "/api/v1/auth/subscriptions"
@@ -50,7 +53,8 @@ namespace Invidious
ChannelInfoHandler: m.ChannelInfoHandler, ChannelInfoHandler: m.ChannelInfoHandler,
ChannelVideosHandler: m.ChannelVideosHandler, ChannelVideosHandler: m.ChannelVideosHandler,
ChannelPlaylistsHandler: m.ChannelPlaylistsHandler, ChannelPlaylistsHandler: m.ChannelPlaylistsHandler,
ChannelRelatedChannelsHandler: m.ChannelRelatedChannelsHandler ChannelRelatedChannelsHandler: m.ChannelRelatedChannelsHandler,
WatchHistoryHandler: m.WatchHistoryHandler
} }
end function end function
@@ -403,6 +407,31 @@ namespace Invidious
} }
end function 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) function DeleteExpiredToken(response as object)
if response.StatusCode() = 403 if response.StatusCode() = 403
LogWarn(`Invidious instance responded with 403: ${response.Text()}`) LogWarn(`Invidious instance responded with 403: ${response.Text()}`)
@@ -422,12 +451,18 @@ namespace Invidious
return invalid return invalid
end if end if
if not m.VerifyTokenScope(authToken.token) if not m.VerifyTokenScope(authToken.token)
' TODO:P1 automatically logging out the user is not a good experience ' Right now we're probably in a UI thread, and we can't unregister the token
' We can at least show a message to the user on why they are logged out ' Without a Task
' TODO:P2 right now we're probably in a UI thread, and we can't unregister the token StartAsyncTask(InvidiousUnregisterTokenTask, {
' So we're just going to ignore it for now invidious: m.node,
' m.UnregisterToken(authToken) authToken: authToken
})
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN) 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 return invalid
end if end if
return authToken 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 function VideoSetThumbnail(node as object, videoItem as object, instance as dynamic, quality = "medium" as string) as boolean
videoThumbnails = videoItem.videoThumbnails videoThumbnails = videoItem.videoThumbnails
if videoThumbnails = invalid or videoThumbnails.Count() = 0 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 return false
end if end if
url = invalid 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 response = context.response
task = context.server.task task = context.server.task
playQueue = task.playQueue playQueue = task.playQueue
invidiousNode = task.invidious
payload = request.Json() payload = request.Json()
if payload = invalid if payload = invalid
@@ -39,7 +40,10 @@ namespace Http
return true return true
end if end if
contentNode = InvidiousContent.ToRowCellContentNode(payload, invalid) service = new Invidious.InvidiousService(invidiousNode)
instance = service.GetInstance()
contentNode = InvidiousContent.ToRowCellContentNode(payload, instance)
playQueue@.AddToQueue(contentNode) playQueue@.AddToQueue(contentNode)
queue = playQueue@.GetQueue() queue = playQueue@.GetQueue()
@@ -53,6 +57,7 @@ namespace Http
response = context.response response = context.response
task = context.server.task task = context.server.task
playQueue = task.playQueue playQueue = task.playQueue
invidiousNode = task.invidious
payload = request.Json() payload = request.Json()
if payload = invalid if payload = invalid
@@ -71,7 +76,10 @@ namespace Http
payload.timestamp = payload.timestamp.toInt() payload.timestamp = payload.timestamp.toInt()
end if 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 playlistIndex = IsInt(payload.playlistIndex) ? payload.playlistIndex : -1
if contentNode.type = "playlist" and playlistIndex = -1 if contentNode.type = "playlist" and playlistIndex = -1
playlistIndex = 0 playlistIndex = 0
@@ -72,3 +72,10 @@
title: Playlists title: Playlists
apiType: Invidious apiType: Invidious
endpoint: auth_playlists 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 responseHandler: ChannelRelatedChannelsHandler
paginationType: Continuation paginationType: Continuation
tryCount: 3 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="flex flex-col items-center">
<div class="w-64"> <div class="w-64">
<VideoThumbnail <VideoThumbnail
bind:videoId
bind:title bind:title
bind:videoThumbnails bind:videoThumbnails
bind:liveNow bind:liveNow
@@ -94,7 +95,7 @@
bind:invidiousInstance bind:invidiousInstance
/> />
</div> </div>
<div class="m-4">{title}</div> <div class="m-4">{title || ""}</div>
</div> </div>
<div class="flex flex-col"> <div class="flex flex-col">
<VideoStartAt <VideoStartAt
@@ -142,6 +142,7 @@
<button class="w-80 p-2" on:click={videoModal.show()}> <button class="w-80 p-2" on:click={videoModal.show()}>
<div class="card card-compact bg-base-100 shadow-xl border border-neutral"> <div class="card card-compact bg-base-100 shadow-xl border border-neutral">
<VideoThumbnail <VideoThumbnail
bind:videoId
bind:title bind:title
bind:videoThumbnails bind:videoThumbnails
bind:liveNow bind:liveNow
@@ -150,7 +151,7 @@
bind:invidiousInstance bind:invidiousInstance
/> />
<div class="card-body"> <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 <button
class="font-semibold link" class="font-semibold link"
on:click={(e) => { on:click={(e) => {
@@ -158,7 +159,7 @@
channelModal.show(); channelModal.show();
}} }}
> >
{author} {author || ""}
</button> </button>
<div>{getViewCountDateText()}</div> <div>{getViewCountDateText()}</div>
</div> </div>
@@ -1,4 +1,5 @@
<script lang="ts"> <script lang="ts">
export let videoId: string | undefined = undefined;
export let title: string | undefined = undefined; export let title: string | undefined = undefined;
export let videoThumbnails: any[] | undefined = undefined; export let videoThumbnails: any[] | undefined = undefined;
export let liveNow: boolean = undefined; export let liveNow: boolean = undefined;
@@ -19,6 +20,8 @@
url = invidiousInstance + url; url = invidiousInstance + url;
} }
thumbnailUrl = url; thumbnailUrl = url;
} else if (invidiousInstance) {
thumbnailUrl = `${invidiousInstance}/vi/${videoId || "--"}/mqdefault.jpg`;
} }
} }