From 250860d92c166b06d4af5d713ec2640a5ad4e701 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Mon, 22 Apr 2019 10:40:29 -0500 Subject: [PATCH] Add '/api/v1/auth/subscriptions' --- src/invidious.cr | 67 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 3780a2f0..ac9b1320 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -1712,7 +1712,14 @@ get "/subscription_manager" do |env| format = env.params.query["format"]? format ||= "rss" - subscriptions = PG_DB.query_all("SELECT * FROM channels WHERE id = ANY('{#{user.subscriptions.join(",")}}')", as: InvidiousChannel) + if user.subscriptions.empty? + values = "'{}'" + else + values = "VALUES #{user.subscriptions.map { |id| %(('#{id}')) }.join(",")}" + end + + subscriptions = PG_DB.query_all("SELECT * FROM channels WHERE id = ANY(#{values})", as: InvidiousChannel) + subscriptions.sort_by! { |channel| channel.author.downcase } if action_takeout @@ -4263,20 +4270,54 @@ end # ... # end -# TODO -# get "/api/v1/auth/subscriptions" do |env| -# ... -# end +get "/api/v1/auth/subscriptions" do |env| + env.response.content_type = "application/json" + user = env.get("user").as(User) -# TODO -# post "/api/v1/auth/subscriptions/:ucid" do |env| -# ... -# end + if user.subscriptions.empty? + values = "'{}'" + else + values = "VALUES #{user.subscriptions.map { |id| %(('#{id}')) }.join(",")}" + end -# TODO -# delete "/api/v1/auth/subscriptions/:ucid" do |env| -# ... -# end + subscriptions = PG_DB.query_all("SELECT * FROM channels WHERE id = ANY(#{values})", as: InvidiousChannel) + + JSON.build do |json| + json.array do + subscriptions.each do |subscription| + json.object do + json.field "author", subscription.author + json.field "authorId", subscription.id + end + end + end + end +end + +post "/api/v1/auth/subscriptions/:ucid" do |env| + env.response.content_type = "application/json" + user = env.get("user").as(User) + + ucid = env.params.url["ucid"] + + if !user.subscriptions.includes? ucid + get_channel(ucid, PG_DB, false, false) + PG_DB.exec("UPDATE users SET subscriptions = array_append(subscriptions,$1) WHERE email = $2", ucid, user.email) + end + + env.response.status_code = 204 +end + +delete "/api/v1/auth/subscriptions/:ucid" do |env| + env.response.content_type = "application/json" + user = env.get("user").as(User) + + ucid = env.params.url["ucid"] + + PG_DB.exec("UPDATE users SET subscriptions = array_remove(subscriptions,$1) WHERE email = $2", ucid, user.email) + + env.response.status_code = 204 +end get "/api/v1/auth/tokens" do |env| env.response.content_type = "application/json"