From 57102c54c776a08eab79bf6cb0bc6473a8c7a557 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 31 Mar 2018 19:09:27 -0500 Subject: Add notifications --- src/helpers.cr | 20 ++++++++++++-------- src/invidious.cr | 17 +++++++++++++---- src/views/layout.ecr | 4 ++++ 3 files changed, 29 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/helpers.cr b/src/helpers.cr index 9bfa8b19..bca3954e 100644 --- a/src/helpers.cr +++ b/src/helpers.cr @@ -81,12 +81,11 @@ end class User add_mapping({ - id: String, - updated: Time, - notifications: Int32, - subscriptions: Array(String), - notifications_viewed: Time, - email: String, + id: String, + updated: Time, + notifications: Array(String), + subscriptions: Array(String), + email: String, }) end @@ -547,6 +546,11 @@ def fetch_channel(id, client, db) video_array = video.to_a args = arg_array(video_array) + db.exec("UPDATE users SET notifications = notifications || $1 \ + WHERE updated < $2 AND $3 = ANY(subscriptions) AND $1 <> ALL(notifications)", video_id, published, ucid) + + # UPDATE users SET notifications = notifications || ARRAY['Os9Rypn2rEQ'] WHERE updated < '2018-03-24 20:48:46' AND 'UCSc16oMxxlcJSb9SXkjwMjA' = ANY(subscriptions) AND 'Os9Rypn2rEQ' <> ALL (notifications); + # TODO: Update record on conflict db.exec("INSERT INTO channel_videos VALUES (#{args})\ ON CONFLICT (id) DO NOTHING", video_array) @@ -569,7 +573,7 @@ def get_user(sid, client, headers, db) args = arg_array(user_array) db.exec("INSERT INTO users VALUES (#{args}) \ - ON CONFLICT (email) DO UPDATE SET id = $1, updated = $2, subscriptions = $4", user_array) + ON CONFLICT (email) DO UPDATE SET id = $1, updated = $2, notifications = ARRAY[]::text[], subscriptions = $4", user_array) end else user = fetch_user(sid, client, headers) @@ -603,6 +607,6 @@ def fetch_user(sid, client, headers) email = "" end - user = User.new(sid, Time.now, 0, channels, Time.now, email) + user = User.new(sid, Time.now, [] of String, channels, email) return user end diff --git a/src/invidious.cr b/src/invidious.cr index f4d7f791..0250070b 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -186,6 +186,13 @@ end before_all do |env| if env.request.cookies.has_key?("SID") env.set "authorized", true + + sid = env.request.cookies["SID"].value + env.set "sid", sid + + notifications = PG_DB.query_one?("SELECT cardinality(notifications) FROM users WHERE id = $1", sid, as: Int32) + notifications ||= 0 + env.set "notifications", notifications else env.set "authorized", false end @@ -213,7 +220,7 @@ get "/watch" do |env| authorized = env.get? "authorized" if authorized - sid = env.request.cookies["SID"].value + sid = env.get("sid").as(String) subscriptions = PG_DB.query_one("SELECT subscriptions FROM users WHERE id = $1", sid, as: Array(String)) else @@ -580,7 +587,7 @@ get "/feed/subscriptions" do |env| headers = HTTP::Headers.new headers["Cookie"] = env.request.headers["Cookie"] - sid = env.request.cookies["SID"].value + sid = env.get("sid").as(String) client = get_client(youtube_pool) user = get_user(sid, client, headers, PG_DB) @@ -589,7 +596,9 @@ get "/feed/subscriptions" do |env| args = arg_array(user.subscriptions) offset = (page - 1) * max_results videos = PG_DB.query_all("SELECT * FROM channel_videos WHERE ucid IN (#{args})\ - ORDER BY published DESC LIMIT #{max_results} OFFSET #{offset}", user.subscriptions, as: ChannelVideo) + ORDER BY published DESC LIMIT #{max_results} OFFSET #{offset}", user.subscriptions, as: ChannelVideo) + + env.set "notifications", 0 templated "subscriptions" else @@ -723,7 +732,7 @@ get "/subscription_ajax" do |env| # Update user if client.post(post_url, headers, post_req).status_code == 200 - sid = env.request.cookies["SID"].value + sid = env.get("sid").as(String) case action when .starts_with? "action_create" diff --git a/src/views/layout.ecr b/src/views/layout.ecr index 2077d0f3..1c068350 100644 --- a/src/views/layout.ecr +++ b/src/views/layout.ecr @@ -29,7 +29,11 @@
-- cgit v1.2.3 16.3-alpine in /docker (#5301)dependabot[bot] Bumps crystallang/crystal from 1.16.2-alpine to 1.16.3-alpine. --- updated-dependencies: - dependency-name: crystallang/crystal dependency-version: 1.16.3-alpine dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2025-05-09Bump crystallang/crystal from 1.12.2-alpine to 1.16.2-alpine in /docker (#5290)dependabot[bot] Bumps crystallang/crystal from 1.12.2-alpine to 1.16.2-alpine. --- updated-dependencies: - dependency-name: crystallang/crystal dependency-version: 1.16.2-alpine dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2025-05-09Fix crystal version used in alpine 3.21Émilien (perso) 2025-05-09Bump crystal-lang/install-crystal from 1.8.0 to 1.8.2 (#5286)dependabot[bot] Bumps [crystal-lang/install-crystal](https://github.com/crystal-lang/install-crystal) from 1.8.0 to 1.8.2. - [Release notes](https://github.com/crystal-lang/install-crystal/releases) - [Commits](https://github.com/crystal-lang/install-crystal/compare/v1.8.0...v1.8.2) --- updated-dependencies: - dependency-name: crystal-lang/install-crystal dependency-version: 1.8.2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2025-05-09Bump docker/build-push-action from 5 to 6 (#5287)dependabot[bot] Bumps [docker/build-push-action](https://github.com/docker/build-push-action) from 5 to 6. - [Release notes](https://github.com/docker/build-push-action/releases) - [Commits](https://github.com/docker/build-push-action/compare/v5...v6) --- updated-dependencies: - dependency-name: docker/build-push-action dependency-version: '6' dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2025-05-09Bump alpine from 3.20 to 3.21 in /docker (#5288)dependabot[bot] Bumps alpine from 3.20 to 3.21. --- updated-dependencies: - dependency-name: alpine dependency-version: '3.21' dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2025-05-09Bump actions/cache from 3 to 4 (#5289)dependabot[bot] Bumps [actions/cache](https://github.com/actions/cache) from 3 to 4. - [Release notes](https://github.com/actions/cache/releases) - [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md) - [Commits](https://github.com/actions/cache/compare/v3...v4) --- updated-dependencies: - dependency-name: actions/cache dependency-version: '4' dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2025-05-09Bump actions/stale from 8 to 9 (#5291)dependabot[bot] Bumps [actions/stale](https://github.com/actions/stale) from 8 to 9. - [Release notes](https://github.com/actions/stale/releases) - [Changelog](https://github.com/actions/stale/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/stale/compare/v8...v9) --- updated-dependencies: - dependency-name: actions/stale dependency-version: '9' dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>