Add notifications

This commit is contained in:
Omar Roth 2018-03-31 19:09:27 -05:00
parent 823f679cef
commit 57102c54c7
4 changed files with 30 additions and 14 deletions

View file

@ -6,9 +6,8 @@ CREATE TABLE public.users
( (
id text COLLATE pg_catalog."default" NOT NULL, id text COLLATE pg_catalog."default" NOT NULL,
updated timestamp with time zone, updated timestamp with time zone,
notifications integer, notifications text[] COLLATE pg_catalog."default",
subscriptions text[] COLLATE pg_catalog."default", subscriptions text[] COLLATE pg_catalog."default",
notifications_viewed timestamp with time zone,
email text COLLATE pg_catalog."default" NOT NULL, email text COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT users_email_key UNIQUE (email), CONSTRAINT users_email_key UNIQUE (email),
CONSTRAINT users_id_key UNIQUE (id) CONSTRAINT users_id_key UNIQUE (id)

View file

@ -83,9 +83,8 @@ class User
add_mapping({ add_mapping({
id: String, id: String,
updated: Time, updated: Time,
notifications: Int32, notifications: Array(String),
subscriptions: Array(String), subscriptions: Array(String),
notifications_viewed: Time,
email: String, email: String,
}) })
end end
@ -547,6 +546,11 @@ def fetch_channel(id, client, db)
video_array = video.to_a video_array = video.to_a
args = arg_array(video_array) 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 # TODO: Update record on conflict
db.exec("INSERT INTO channel_videos VALUES (#{args})\ db.exec("INSERT INTO channel_videos VALUES (#{args})\
ON CONFLICT (id) DO NOTHING", video_array) ON CONFLICT (id) DO NOTHING", video_array)
@ -569,7 +573,7 @@ def get_user(sid, client, headers, db)
args = arg_array(user_array) args = arg_array(user_array)
db.exec("INSERT INTO users VALUES (#{args}) \ 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 end
else else
user = fetch_user(sid, client, headers) user = fetch_user(sid, client, headers)
@ -603,6 +607,6 @@ def fetch_user(sid, client, headers)
email = "" email = ""
end end
user = User.new(sid, Time.now, 0, channels, Time.now, email) user = User.new(sid, Time.now, [] of String, channels, email)
return user return user
end end

View file

@ -186,6 +186,13 @@ end
before_all do |env| before_all do |env|
if env.request.cookies.has_key?("SID") if env.request.cookies.has_key?("SID")
env.set "authorized", true 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 else
env.set "authorized", false env.set "authorized", false
end end
@ -213,7 +220,7 @@ get "/watch" do |env|
authorized = env.get? "authorized" authorized = env.get? "authorized"
if 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)) subscriptions = PG_DB.query_one("SELECT subscriptions FROM users WHERE id = $1", sid, as: Array(String))
else else
@ -580,7 +587,7 @@ get "/feed/subscriptions" do |env|
headers = HTTP::Headers.new headers = HTTP::Headers.new
headers["Cookie"] = env.request.headers["Cookie"] headers["Cookie"] = env.request.headers["Cookie"]
sid = env.request.cookies["SID"].value sid = env.get("sid").as(String)
client = get_client(youtube_pool) client = get_client(youtube_pool)
user = get_user(sid, client, headers, PG_DB) user = get_user(sid, client, headers, PG_DB)
@ -591,6 +598,8 @@ get "/feed/subscriptions" do |env|
videos = PG_DB.query_all("SELECT * FROM channel_videos WHERE ucid IN (#{args})\ 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" templated "subscriptions"
else else
env.redirect "/" env.redirect "/"
@ -723,7 +732,7 @@ get "/subscription_ajax" do |env|
# Update user # Update user
if client.post(post_url, headers, post_req).status_code == 200 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 case action
when .starts_with? "action_create" when .starts_with? "action_create"

View file

@ -29,7 +29,11 @@
<div class="pure-g"> <div class="pure-g">
<div class="pure-u-1 pure-u-md-1-3"> <div class="pure-u-1 pure-u-md-1-3">
<a href="/feed/subscriptions" class="pure-menu-heading"> <a href="/feed/subscriptions" class="pure-menu-heading">
<% if env.get("notifications").as(Int32) > 0 %>
<center><i class="fas fa-bell"></i></center>
<% else %>
<center><i class="far fa-bell"></i></center> <center><i class="far fa-bell"></i></center>
<% end %>
</a> </a>
</div> </div>
<div class="pure-u-1 pure-u-md-2-3"> <div class="pure-u-1 pure-u-md-2-3">