Add History API (#3654)

This commit is contained in:
Samantaz Fox 2023-03-07 20:28:43 +01:00
commit 35ac26bd61
No known key found for this signature in database
GPG key ID: F42821059186176E
2 changed files with 64 additions and 0 deletions

View file

@ -54,6 +54,65 @@ module Invidious::Routes::API::V1::Authenticated
env.response.status_code = 204 env.response.status_code = 204
end end
def self.get_history(env)
env.response.content_type = "application/json"
user = env.get("user").as(User)
page = env.params.query["page"]?.try &.to_i?.try &.clamp(0, Int32::MAX)
page ||= 1
max_results = env.params.query["max_results"]?.try &.to_i?.try &.clamp(0, MAX_ITEMS_PER_PAGE)
max_results ||= user.preferences.max_results
max_results ||= CONFIG.default_user_preferences.max_results
start_index = (page - 1) * max_results
if user.watched[start_index]?
watched = user.watched.reverse[start_index, max_results]
end
watched ||= [] of String
return watched.to_json
end
def self.mark_watched(env)
user = env.get("user").as(User)
if !user.preferences.watch_history
return error_json(409, "Watch history is disabled in preferences.")
end
id = env.params.url["id"]
if !id.match(/^[a-zA-Z0-9_-]{11}$/)
return error_json(400, "Invalid video id.")
end
Invidious::Database::Users.mark_watched(user, id)
env.response.status_code = 204
end
def self.mark_unwatched(env)
user = env.get("user").as(User)
if !user.preferences.watch_history
return error_json(409, "Watch history is disabled in preferences.")
end
id = env.params.url["id"]
if !id.match(/^[a-zA-Z0-9_-]{11}$/)
return error_json(400, "Invalid video id.")
end
Invidious::Database::Users.mark_unwatched(user, id)
env.response.status_code = 204
end
def self.clear_history(env)
user = env.get("user").as(User)
Invidious::Database::Users.clear_watch_history(user)
env.response.status_code = 204
end
def self.feed(env) def self.feed(env)
env.response.content_type = "application/json" env.response.content_type = "application/json"

View file

@ -257,6 +257,11 @@ module Invidious::Routing
get "/api/v1/auth/export/invidious", {{namespace}}::Authenticated, :export_invidious get "/api/v1/auth/export/invidious", {{namespace}}::Authenticated, :export_invidious
post "/api/v1/auth/import/invidious", {{namespace}}::Authenticated, :import_invidious post "/api/v1/auth/import/invidious", {{namespace}}::Authenticated, :import_invidious
get "/api/v1/auth/history", {{namespace}}::Authenticated, :get_history
post "/api/v1/auth/history/:id", {{namespace}}::Authenticated, :mark_watched
delete "/api/v1/auth/history/:id", {{namespace}}::Authenticated, :mark_unwatched
delete "/api/v1/auth/history", {{namespace}}::Authenticated, :clear_history
get "/api/v1/auth/feed", {{namespace}}::Authenticated, :feed get "/api/v1/auth/feed", {{namespace}}::Authenticated, :feed
get "/api/v1/auth/subscriptions", {{namespace}}::Authenticated, :get_subscriptions get "/api/v1/auth/subscriptions", {{namespace}}::Authenticated, :get_subscriptions