Update pubsub to support lease_seconds

pull/394/head
Omar Roth 6 years ago
parent 8e884fe115
commit 7425700009

@ -0,0 +1,4 @@
#!/bin/sh
psql invidious -c "ALTER TABLE channels DROP COLUMN subscribed"
psql invidious -c "ALTER TABLE channels ADD COLUMN subscribed timestamptz"

@ -8,6 +8,7 @@ CREATE TABLE public.channels
author text, author text,
updated timestamp with time zone, updated timestamp with time zone,
deleted boolean, deleted boolean,
subscribed timestamp with time zone,
CONSTRAINT channels_id_key UNIQUE (id) CONSTRAINT channels_id_key UNIQUE (id)
); );

@ -2319,12 +2319,13 @@ end
# Add support for subscribing to channels via PubSubHubbub # Add support for subscribing to channels via PubSubHubbub
get "/feed/webhook" do |env| get "/feed/webhook/:token" do |env|
verify_token = env.params.url["token"]
mode = env.params.query["hub.mode"] mode = env.params.query["hub.mode"]
topic = env.params.query["hub.topic"] topic = env.params.query["hub.topic"]
challenge = env.params.query["hub.challenge"] challenge = env.params.query["hub.challenge"]
lease_seconds = env.params.query["hub.lease_seconds"] lease_seconds = env.params.query["hub.lease_seconds"]
verify_token = env.params.query["hub.verify_token"]
time, signature = verify_token.split(":") time, signature = verify_token.split(":")
@ -2337,12 +2338,12 @@ get "/feed/webhook" do |env|
end end
ucid = HTTP::Params.parse(URI.parse(topic).query.not_nil!)["channel_id"] ucid = HTTP::Params.parse(URI.parse(topic).query.not_nil!)["channel_id"]
PG_DB.exec("UPDATE channels SET subscribed = true WHERE id = $1", ucid) PG_DB.exec("UPDATE channels SET subscribed = $1 WHERE id = $2", Time.now, ucid)
halt env, status_code: 200, response: challenge halt env, status_code: 200, response: challenge
end end
post "/feed/webhook" do |env| post "/feed/webhook/:token" do |env|
body = env.request.body.not_nil!.gets_to_end body = env.request.body.not_nil!.gets_to_end
signature = env.request.headers["X-Hub-Signature"].lchop("sha1=") signature = env.request.headers["X-Hub-Signature"].lchop("sha1=")

@ -4,7 +4,7 @@ class InvidiousChannel
author: String, author: String,
updated: Time, updated: Time,
deleted: Bool, deleted: Bool,
subscribed: {type: Bool, default: false}, subscribed: Time?,
}) })
end end
@ -186,7 +186,7 @@ def fetch_channel(ucid, db, pull_all_videos = true, locale = nil)
db.exec("DELETE FROM channel_videos * WHERE NOT id = ANY ('{#{ids.map { |id| %("#{id}") }.join(",")}}') AND ucid = $1", ucid) db.exec("DELETE FROM channel_videos * WHERE NOT id = ANY ('{#{ids.map { |id| %("#{id}") }.join(",")}}') AND ucid = $1", ucid)
end end
channel = InvidiousChannel.new(ucid, author, Time.now, false, false) channel = InvidiousChannel.new(ucid, author, Time.now, false, nil)
return channel return channel
end end
@ -198,12 +198,12 @@ def subscribe_pubsub(ucid, key, config)
host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain) host_url = make_host_url(Kemal.config.ssl || config.https_only, config.domain)
body = { body = {
"hub.callback" => "#{host_url}/feed/webhook", "hub.callback" => "#{host_url}/feed/webhook/#{time}:#{OpenSSL::HMAC.hexdigest(:sha1, key, time)}",
"hub.topic" => "https://www.youtube.com/feeds/videos.xml?channel_id=#{ucid}", "hub.topic" => "https://www.youtube.com/feeds/videos.xml?channel_id=#{ucid}",
"hub.verify" => "async", "hub.verify" => "async",
"hub.mode" => "subscribe", "hub.mode" => "subscribe",
"hub.verify_token" => "#{time}:#{OpenSSL::HMAC.hexdigest(:sha1, key, time)}", "hub.lease_seconds" => "432000",
"hub.secret" => key.to_s, "hub.secret" => key.to_s,
} }
return client.post("/subscribe", form: body) return client.post("/subscribe", form: body)

@ -157,7 +157,7 @@ def subscribe_to_feeds(db, logger, key, config)
if config.use_pubsub_feeds if config.use_pubsub_feeds
spawn do spawn do
loop do loop do
db.query_all("SELECT id FROM channels WHERE subscribed = false") do |rs| db.query_all("SELECT id FROM channels WHERE CURRENT_TIMESTAMP - subscribed > '4 days'") do |rs|
ucid = rs.read(String) ucid = rs.read(String)
response = subscribe_pubsub(ucid, key, config) response = subscribe_pubsub(ucid, key, config)

Loading…
Cancel
Save