From 64464f23aed3b7b3331db686c85926c72f0ef7e5 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Thu, 30 May 2019 15:09:39 -0500 Subject: [PATCH] Add 'views' to channel_videos --- config/migrate-scripts/migrate-db-52cb239.sh | 3 +++ config/sql/channel_videos.sql | 1 + src/invidious.cr | 5 +++-- src/invidious/channels.cr | 18 +++++++++++++----- src/invidious/views/components/item.ecr | 4 ++-- 5 files changed, 22 insertions(+), 9 deletions(-) create mode 100755 config/migrate-scripts/migrate-db-52cb239.sh diff --git a/config/migrate-scripts/migrate-db-52cb239.sh b/config/migrate-scripts/migrate-db-52cb239.sh new file mode 100755 index 00000000..db8efeab --- /dev/null +++ b/config/migrate-scripts/migrate-db-52cb239.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +psql invidious kemal -c "ALTER TABLE channel_videos ADD COLUMN views bigint;" diff --git a/config/sql/channel_videos.sql b/config/sql/channel_videos.sql index ba354adf..cec57cd4 100644 --- a/config/sql/channel_videos.sql +++ b/config/sql/channel_videos.sql @@ -13,6 +13,7 @@ CREATE TABLE public.channel_videos length_seconds integer, live_now boolean, premiere_timestamp timestamp with time zone, + views bigint, CONSTRAINT channel_videos_id_key UNIQUE (id) ); diff --git a/src/invidious.cr b/src/invidious.cr index f487b0e9..dea32a39 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -2857,6 +2857,7 @@ post "/feed/webhook/:token" do |env| length_seconds: video.length_seconds, live_now: video.live_now, premiere_timestamp: video.premiere_timestamp, + views: video.views, ) users = PG_DB.query_all("UPDATE users SET notifications = notifications || $1 \ @@ -2866,10 +2867,10 @@ post "/feed/webhook/:token" do |env| video_array = video.to_a args = arg_array(video_array) - PG_DB.exec("INSERT INTO channel_videos VALUES (#{args}) \ + PG_DB.exec("INSERT INTO channel_videos (id, title, published, updated, ucid, author, length_seconds, live_now, premiere_timestamp) VALUES (#{args}) \ ON CONFLICT (id) DO UPDATE SET title = $2, published = $3, \ updated = $4, ucid = $5, author = $6, length_seconds = $7, \ - live_now = $8, premiere_timestamp = $9", video_array) + live_now = $8, premiere_timestamp = $9, views = $10", video_array) users.each do |user| payload = { diff --git a/src/invidious/channels.cr b/src/invidious/channels.cr index c8de9543..d0eb7dd3 100644 --- a/src/invidious/channels.cr +++ b/src/invidious/channels.cr @@ -24,6 +24,8 @@ struct ChannelVideo json.field "authorUrl", "/channel/#{self.ucid}" json.field "published", self.published.to_unix json.field "publishedText", translate(locale, "`x` ago", recode_date(self.published, locale)) + + json.field "viewCount", self.views end end @@ -47,6 +49,7 @@ struct ChannelVideo length_seconds: {type: Int32, default: 0}, live_now: {type: Bool, default: false}, premiere_timestamp: {type: Time?, default: nil}, + views: {type: Int64?, default: nil}, }) end @@ -155,6 +158,8 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil) updated = Time.parse_rfc3339(entry.xpath_node("updated").not_nil!.content) author = entry.xpath_node("author/name").not_nil!.content ucid = entry.xpath_node("channelid").not_nil!.content + views = entry.xpath_node("group/community/statistics").try &.["views"]?.try &.to_i64? + views ||= 0_i64 channel_video = videos.select { |video| video.id == video_id }[0]? @@ -175,7 +180,8 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil) author: author, length_seconds: length_seconds, live_now: live_now, - premiere_timestamp: premiere_timestamp + premiere_timestamp: premiere_timestamp, + views: views, ) users = db.query_all("UPDATE users SET notifications = notifications || $1 \ @@ -190,7 +196,7 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil) db.exec("INSERT INTO channel_videos VALUES (#{args}) \ ON CONFLICT (id) DO UPDATE SET title = $2, published = $3, \ updated = $4, ucid = $5, author = $6, length_seconds = $7, \ - live_now = $8", video_array) + live_now = $8, views = $10", video_array) users.each do |user| payload = { @@ -236,7 +242,8 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil) author: video.author, length_seconds: video.length_seconds, live_now: video.live_now, - premiere_timestamp: video.premiere_timestamp + premiere_timestamp: video.premiere_timestamp, + views: video.views ) } videos.each do |video| @@ -254,8 +261,9 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil) # We don't update the 'premire_timestamp' here because channel pages don't include them db.exec("INSERT INTO channel_videos VALUES (#{args}) \ - ON CONFLICT (id) DO UPDATE SET title = $2, updated = $4, \ - ucid = $5, author = $6, length_seconds = $7, live_now = $8", video_array) + ON CONFLICT (id) DO UPDATE SET title = $2, published = $3, \ + updated = $4, ucid = $5, author = $6, length_seconds = $7, \ + live_now = $8, views = $10", video_array) # Update all users affected by insert users.each do |user| diff --git a/src/invidious/views/components/item.ecr b/src/invidious/views/components/item.ecr index 5ec406cb..dc2acac9 100644 --- a/src/invidious/views/components/item.ecr +++ b/src/invidious/views/components/item.ecr @@ -81,7 +81,7 @@ <% end %>
- <%= item.responds_to?(:views) ? translate(locale, "`x` views", number_to_short_text(item.views)) : "" %> + <%= item.responds_to?(:views) && item.views ? translate(locale, "`x` views", number_to_short_text(item.views || 0)) : "" %>
<% else %> @@ -130,7 +130,7 @@ <% end %>
- <%= item.responds_to?(:views) ? translate(locale, "`x` views", number_to_short_text(item.views)) : "" %> + <%= item.responds_to?(:views) && item.views ? translate(locale, "`x` views", number_to_short_text(item.views || 0)) : "" %>
<% end %>