From e14f2f275035439b286a8c887be9544c65620209 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 6 Oct 2018 18:19:47 -0500 Subject: [PATCH] Prevent duplicate subscriptions when importing user data --- src/invidious.cr | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 2e141c29..b78e8f72 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -1116,7 +1116,7 @@ post "/data_control" do |env| body["subscriptions"].as_a.each do |ucid| ucid = ucid.as_s if !user.subscriptions.includes? ucid - PG_DB.exec("UPDATE users SET subscriptions = array_append(subscriptions,$1) WHERE id = $2", ucid, user.id) + PG_DB.exec("UPDATE users SET subscriptions = array_append(subscriptions,$1) WHERE email = $2", ucid, user.email) begin client = make_client(YT_URL) @@ -1131,6 +1131,7 @@ post "/data_control" do |env| id = id.as_s if !user.watched.includes? id PG_DB.exec("UPDATE users SET watched = array_append(watched,$1) WHERE email = $2", id, user.email) + user.watched << id end end @@ -1141,11 +1142,12 @@ post "/data_control" do |env| ucid = channel["xmlUrl"].match(/UC[a-zA-Z0-9_-]{22}/).not_nil![0] if !user.subscriptions.includes? ucid - PG_DB.exec("UPDATE users SET subscriptions = array_append(subscriptions,$1) WHERE email = $2", ucid, user.email) - begin client = make_client(YT_URL) get_channel(ucid, client, PG_DB, false, false) + + PG_DB.exec("UPDATE users SET subscriptions = array_append(subscriptions,$1) WHERE email = $2", ucid, user.email) + user.subscriptions << ucid rescue ex next end @@ -1156,11 +1158,12 @@ post "/data_control" do |env| ucid = md["channel_id"] if !user.subscriptions.includes? ucid - PG_DB.exec("UPDATE users SET subscriptions = array_append(subscriptions,$1) WHERE email = $2", ucid, user.email) - begin client = make_client(YT_URL) get_channel(ucid, client, PG_DB, false, false) + + PG_DB.exec("UPDATE users SET subscriptions = array_append(subscriptions,$1) WHERE email = $2", ucid, user.email) + user.subscriptions << ucid rescue ex next end @@ -1192,19 +1195,22 @@ post "/data_control" do |env| db = entry.io.gets_to_end db.scan(/youtube\.com\/watch\?v\=(?[a-zA-Z0-9_-]{11})/) do |md| - if !user.watched.includes? md["id"] - PG_DB.exec("UPDATE users SET watched = array_append(watched,$1) WHERE email = $2", md["id"], user.email) + id = md["id"] + if !user.watched.includes? id + PG_DB.exec("UPDATE users SET watched = array_append(watched,$1) WHERE email = $2", id, user.email) + user.watched << id end end db.scan(/youtube\.com\/channel\/(?[a-zA-Z0-9_-]{22})/) do |md| ucid = md["ucid"] if !user.subscriptions.includes? ucid - PG_DB.exec("UPDATE users SET subscriptions = array_append(subscriptions,$1) WHERE email = $2", ucid, user.email) - begin client = make_client(YT_URL) get_channel(ucid, client, PG_DB, false, false) + + PG_DB.exec("UPDATE users SET subscriptions = array_append(subscriptions,$1) WHERE email = $2", ucid, user.email) + user.subscriptions << ucid rescue ex next end