diff options
author | Yorhel <git@yorhel.nl> | 2020-10-08 15:09:09 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2020-10-08 15:09:14 +0200 |
commit | 6d8c7104e6f01d3f1b8c048f2641940b4838a85e (patch) | |
tree | d42c9270e8a6746a85a728989a68935d754fe9c6 /lib | |
parent | 7eb7a3e073107bb5ad6ec20ae18f296bde33b4da (diff) |
notifications: Add subscription management UI + finalize DB migration
Seems to be working so far. I'll find the actual bugs in production.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/VNWeb/Discussions/Thread.pm | 2 | ||||
-rw-r--r-- | lib/VNWeb/HTML.pm | 37 | ||||
-rw-r--r-- | lib/VNWeb/User/Notifications.pm | 31 |
3 files changed, 67 insertions, 3 deletions
diff --git a/lib/VNWeb/Discussions/Thread.pm b/lib/VNWeb/Discussions/Thread.pm index 39de4b96..dd443d93 100644 --- a/lib/VNWeb/Discussions/Thread.pm +++ b/lib/VNWeb/Discussions/Thread.pm @@ -194,7 +194,7 @@ TUWF::get qr{/$RE{tid}(?:(?<sep>[\./])$RE{num})?}, sub { auth->notiRead($id, [ map $_->{num}, $posts->@* ]) if @$posts; - framework_ title => $t->{title}, $num ? (js => 1, pagevars => {sethash=>$num}) : (), sub { + framework_ title => $t->{title}, type => 't', dbobj => $t, $num ? (js => 1, pagevars => {sethash=>$num}) : (), sub { metabox_ $t; elm_ 'Discussions.Poll' => $POLL_OUT, { question => $t->{poll_question}, diff --git a/lib/VNWeb/HTML.pm b/lib/VNWeb/HTML.pm index 772f3ebc..d9098ffe 100644 --- a/lib/VNWeb/HTML.pm +++ b/lib/VNWeb/HTML.pm @@ -336,6 +336,38 @@ sub _footer_ { } +sub _maintabs_subscribe_ { + my($o, $id) = @_; + return if !auth || $id !~ /^[twvrpcsd]/; + + my $noti = + $id =~ /^t/ ? tuwf->dbVali('SELECT SUM(x) FROM ( + SELECT 1 FROM threads_posts tp, users u WHERE u.id =', \auth->uid, 'AND tp.uid =', \auth->uid, 'AND tp.tid =', \$id, ' AND u.notify_post + UNION SELECT 1+1 FROM threads_boards tb WHERE tb.tid =', \$id, 'AND tb.type = \'u\' AND tb.iid =', \auth->uid, ' + ) x(x)') + + : $id =~ /^w/ ? (auth->pref('notify_post') || auth->pref('notify_comment')) && tuwf->dbVali('SELECT SUM(x) FROM ( + SELECT 1 FROM reviews_posts wp, users u WHERE u.id =', \auth->uid, 'AND wp.uid =', \auth->uid, 'AND wp.id =', \$id, 'AND u.notify_post + UNION SELECT 1+1 FROM reviews w, users u WHERE u.id =', \auth->uid, 'AND w.uid =', \auth->uid, 'AND w.id =', \$id, 'AND u.notify_comment + ) x(x)') + + : auth->pref('notify_dbedit') && tuwf->dbVali('SELECT 1 FROM changes WHERE type = vndbid_type(', \$id, ')::dbentry_type AND itemid = vndbid_num(', \$id, ') AND requester =', \auth->uid); + + my $sub = tuwf->dbRowi('SELECT subnum, subreview FROM notification_subs WHERE uid =', \auth->uid, 'AND iid =', \$id); + + li_ id => 'subscribe', sub { + elm_ Subscribe => $VNWeb::User::Notifications::SUB, { + id => $id, + noti => $noti||0, + subnum => $sub->{subnum}, + subreview => $sub->{subreview}||0, + }, sub { + a_ href => '#', class => ($noti && (!defined $sub->{subnum} || $sub->{subnum})) || $sub->{subnum} || $sub->{subreview} ? 'active' : 'inactive', '🔔'; + }; + }; +} + + sub _maintabs_ { my $opt = shift; my($t, $o, $sel) = @{$opt}{qw/type dbobj tab/}; @@ -353,13 +385,13 @@ sub _maintabs_ { div_ class => 'maintabs right', sub { ul_ sub { - t '' => "/$id", $id; + t '' => "/$id", $id if $t ne 't'; t rg => "/$id/rg", 'relations' if $t =~ /[vp]/ && tuwf->dbVali('SELECT 1 FROM', $t eq 'v' ? 'vn_relations' : 'producers_relations', 'WHERE id =', \$o->{id}, 'LIMIT 1'); t releases => "/$id/releases", 'releases' if $t eq 'v'; - t edit => "/$id/edit", 'edit' if can_edit $t, $o; + t edit => "/$id/edit", 'edit' if $t ne 't' && can_edit $t, $o; t copy => "/$id/copy", 'copy' if $t =~ /[rc]/ && can_edit $t, $o; t tagmod => "/$id/tagmod", 'modify tags' if $t eq 'v' && auth->permTag && !$o->{entry_hidden}; @@ -381,6 +413,7 @@ sub _maintabs_ { }; t hist => "/$id/hist", 'history' if $t =~ /[uvrpcsd]/; + _maintabs_subscribe_ $o, $id; } } } diff --git a/lib/VNWeb/User/Notifications.pm b/lib/VNWeb/User/Notifications.pm index 3831402b..18ddb9f5 100644 --- a/lib/VNWeb/User/Notifications.pm +++ b/lib/VNWeb/User/Notifications.pm @@ -109,6 +109,10 @@ sub listing_ { } +# Redirect so that elm/Subscribe.elm can link to this page without knowing our uid. +TUWF::get qr{/u/notifies}, sub { auth ? tuwf->resRedirect('/u'.auth->uid.'/notifies') : tuwf->resNotFound }; + + TUWF::get qr{/$RE{uid}/notifies}, sub { my $id = tuwf->capture('id'); return tuwf->resNotFound if !auth || $id != auth->uid; @@ -200,9 +204,36 @@ TUWF::get qr{/$RE{uid}/notify/$RE{num}/(?<lid>[a-z0-9\.]+)}, sub { }; + # It's a bit annoying to add auth->notiRead() to each revision page, so do that in bulk with a simple hook. TUWF::hook before => sub { auth->notiRead($+{vndbid}, $+{rev}) if auth && tuwf->reqPath() =~ qr{^/(?<vndbid>[vrpcsd]$RE{num})\.(?<rev>$RE{num})$}; }; + + + +our $SUB = form_compile any => { + id => { vndbid => [qw|t w v r p c s d|] }, + subnum => { required => 0, jsonbool => 1 }, + subreview => { anybool => 1 }, + noti => { uint => 1 }, # Whether the user already gets 'subnum' notifications for this entry (see HTML.pm for possible values) +}; + +elm_api Subscribe => undef, $SUB, sub { + my($data) = @_; + + delete $data->{noti}; + $data->{subnum} = $data->{subnum}?1:0 if defined $data->{subnum}; # 'jsonbool' isn't understood by SQL + $data->{subreview} = 0 if $data->{id} !~ /^v/; + + my %where = (iid => delete $data->{id}, uid => auth->uid); + if(!defined $data->{subnum} && !$data->{subreview}) { + tuwf->dbExeci('DELETE FROM notification_subs WHERE', \%where); + } else { + tuwf->dbExeci('INSERT INTO notification_subs', {%where, %$data}, 'ON CONFLICT (iid,uid) DO UPDATE SET', $data); + } + elm_Success +}; + 1; |