From 921c34aa650463de75e3fa0d72b52975a0ea0646 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Wed, 10 Oct 2018 16:10:58 -0500 Subject: [PATCH] Create materialized views for Google accounts --- src/invidious/users.cr | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/invidious/users.cr b/src/invidious/users.cr index 608ca4d7..4116e732 100644 --- a/src/invidious/users.cr +++ b/src/invidious/users.cr @@ -119,6 +119,15 @@ def get_user(sid, client, headers, db, refresh = true) db.exec("INSERT INTO users VALUES (#{args}) \ ON CONFLICT (email) DO UPDATE SET id = users.id || $1, updated = $2, subscriptions = $4", user_array) + + begin + view_name = "subscriptions_#{sha256(user.email)[0..7]}" + PG_DB.exec("CREATE MATERIALIZED VIEW #{view_name} AS \ + SELECT * FROM channel_videos WHERE \ + ucid = ANY ((SELECT subscriptions FROM users WHERE email = '#{user.email}')::text[]) \ + ORDER BY published DESC;") + rescue ex + end end else user = fetch_user(sid, client, headers, db) @@ -129,6 +138,15 @@ def get_user(sid, client, headers, db, refresh = true) db.exec("INSERT INTO users VALUES (#{args}) \ ON CONFLICT (email) DO UPDATE SET id = users.id || $1, updated = $2, subscriptions = $4", user_array) + + begin + view_name = "subscriptions_#{sha256(user.email)[0..7]}" + PG_DB.exec("CREATE MATERIALIZED VIEW #{view_name} AS \ + SELECT * FROM channel_videos WHERE \ + ucid = ANY ((SELECT subscriptions FROM users WHERE email = '#{user.email}')::text[]) \ + ORDER BY published DESC;") + rescue ex + end end return user