Add 'since' to '/api/v1/auth/notifications'

This commit is contained in:
Omar Roth 2019-04-28 18:14:16 -05:00
parent ef309bd8d0
commit 54d250bde4
3 changed files with 69 additions and 25 deletions

View file

@ -3428,31 +3428,15 @@ get "/api/v1/popular" do |env|
env.response.content_type = "application/json"
videos = JSON.build do |json|
JSON.build do |json|
json.array do
popular_videos.each do |video|
json.object do
json.field "title", video.title
json.field "videoId", video.id
json.field "videoThumbnails" do
generate_thumbnails(json, video.id, config, Kemal.config)
end
json.field "lengthSeconds", video.length_seconds
json.field "author", video.author
json.field "authorId", video.ucid
json.field "authorUrl", "/channel/#{video.ucid}"
json.field "published", video.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
video.to_json(locale, config, Kemal.config, json)
end
end
end
end
videos
end
get "/api/v1/top" do |env|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
@ -3573,9 +3557,9 @@ get "/api/v1/channels/:ucid" do |env|
metadata.each do |item|
case item.content
when .includes? "views"
total_views = item.content.delete("views •,").to_i64
total_views = item.content.gsub(/\D/, "").to_i64
when .includes? "subscribers"
sub_count = item.content.delete("subscribers").delete(",").to_i64
sub_count = item.content.delete("subscribers").gsub(/\D/, "").to_i64
when .includes? "Joined"
joined = Time.parse(item.content.lchop("Joined "), "%b %-d, %Y", Time::Location.local)
end
@ -3788,7 +3772,7 @@ end
next error_message
end
response = JSON.build do |json|
JSON.build do |json|
json.array do
videos.each do |video|
json.object do
@ -3816,8 +3800,6 @@ end
end
end
end
response
end
end
@ -4295,10 +4277,42 @@ get "/api/v1/auth/notifications" do |env|
topics = env.params.query["topics"]?.try &.split(",").uniq.first(1000)
topics ||= [] of String
since = env.params.query["since"]?.try &.to_i?
begin
id = 0
spawn do
if since
topics.try &.each do |topic|
case topic
when .match(/UC[A-Za-z0-9_-]{22}/)
PG_DB.query_all("SELECT * FROM channel_videos WHERE ucid = $1 AND published > $2 ORDER BY published DESC LIMIT 15",
topic, Time.unix(since.not_nil!), as: ChannelVideo).each do |video|
response = JSON.parse(video.to_json(locale, config, Kemal.config))
if fields_text = env.params.query["fields"]?
begin
JSONFilter.filter(response, fields_text)
rescue ex
env.response.status_code = 400
response = {"error" => ex.message}
end
end
env.response.puts "id: #{id}"
env.response.puts "data: #{response.to_json}"
env.response.puts
env.response.flush
id += 1
end
else
# TODO
end
end
end
PG.connect_listen(PG_URL, "notifications") do |event|
notification = JSON.parse(event.payload)
topic = notification["topic"].as_s

View file

@ -9,6 +9,34 @@ struct InvidiousChannel
end
struct ChannelVideo
def to_json(locale, config, kemal_config, json : JSON::Builder)
json.object do
json.field "title", self.title
json.field "videoId", self.id
json.field "videoThumbnails" do
generate_thumbnails(json, self.id, config, Kemal.config)
end
json.field "lengthSeconds", self.length_seconds
json.field "author", self.author
json.field "authorId", self.ucid
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))
end
end
def to_json(locale, config, kemal_config, json : JSON::Builder | Nil = nil)
if json
to_json(locale, config, kemal_config, json)
else
JSON.build do |json|
to_json(locale, config, kemal_config, json)
end
end
end
db_mapping({
id: String,
title: String,

View file

@ -262,8 +262,10 @@ struct Video
generate_storyboards(json, self.storyboards, config, kemal_config)
end
json.field "description", html_to_content(self.description).last
json.field "descriptionHtml", html_to_content(self.description).first
description_html, description = html_to_content(self.description)
json.field "description", description
json.field "descriptionHtml", description_html
json.field "published", self.published.to_unix
json.field "publishedText", translate(locale, "`x` ago", recode_date(self.published, locale))
json.field "keywords", self.keywords