summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2020-10-13 16:58:00 +0200
committerYorhel <git@yorhel.nl>2020-10-13 16:58:00 +0200
commit7430acd68bf99585e461ef4fad4b40be45466d70 (patch)
tree81ace6e00ba0d5f636502f6126abb42304a42eca
parenta25ef5a7313b95873d6db50f658331026b965bc7 (diff)
notifications: Add subscribing for (un)applications of traits
-rw-r--r--elm/Subscribe.elm10
-rw-r--r--lib/VNDB/Util/LayoutHTML.pm1
-rw-r--r--lib/VNWeb/HTML.pm28
-rw-r--r--lib/VNWeb/User/Notifications.pm6
-rw-r--r--sql/func.sql13
-rw-r--r--sql/schema.sql5
-rw-r--r--util/updates/2020-10-13-notifications-subapply.sql3
7 files changed, 48 insertions, 18 deletions
diff --git a/elm/Subscribe.elm b/elm/Subscribe.elm
index 8631daac..ca70a675 100644
--- a/elm/Subscribe.elm
+++ b/elm/Subscribe.elm
@@ -29,6 +29,7 @@ type Msg
= Opened Bool
| SubNum Bool Bool
| SubReview Bool
+ | SubApply Bool
| Submitted GApi.Response
@@ -41,6 +42,7 @@ update msg model =
Opened b -> ({ model | opened = b }, Cmd.none)
SubNum v b -> save { dat | subnum = if b then Just v else Nothing }
SubReview b -> save { dat | subreview = b }
+ SubApply b -> save { dat | subapply = b }
Submitted e -> ({ model | state = if e == GApi.Success then Api.Normal else Api.Error e }, Cmd.none)
@@ -52,7 +54,9 @@ view model =
msg txt = p [] [ text txt, text " These can be disabled globally in your ", a [ href "/u/notifies" ] [ text "notification settings" ], text "." ]
in
div []
- [ a [ href "#", onClickD (Opened (not model.opened)), class (if (dat.noti > 0 && dat.subnum /= Just False) || dat.subnum == Just True || dat.subreview then "active" else "inactive") ] [ text "🔔" ]
+ [ a [ href "#", onClickD (Opened (not model.opened))
+ , class (if (dat.noti > 0 && dat.subnum /= Just False) || dat.subnum == Just True || dat.subreview || dat.subapply then "active" else "inactive")
+ ] [ text "🔔" ]
, if not model.opened then text ""
else div [] [ div []
[ h4 []
@@ -76,7 +80,7 @@ view model =
"w" -> text " Disable notifications only for this review."
_ -> text " Disable edit notifications only for this entry."
]
- , label []
+ , if t == "i" then text "" else label []
[ inputCheck "" (dat.subnum == Just True) (SubNum True)
, case t of
"t" -> text " Enable notifications for new replies"
@@ -86,6 +90,8 @@ view model =
]
, if t /= "v" then text "" else
label [] [ inputCheck "" dat.subreview SubReview, text " Enable notifications for new reviews." ]
+ , if t /= "i" then text "" else
+ label [] [ inputCheck "" dat.subapply SubApply, text " Enable notifications when this trait is applied or removed from a character." ]
, case model.state of
Api.Error e -> b [ class "standout" ] [ br [] [], text (Api.showResponse e) ]
_ -> text ""
diff --git a/lib/VNDB/Util/LayoutHTML.pm b/lib/VNDB/Util/LayoutHTML.pm
index a18542f8..7d070f94 100644
--- a/lib/VNDB/Util/LayoutHTML.pm
+++ b/lib/VNDB/Util/LayoutHTML.pm
@@ -35,6 +35,7 @@ sub htmlFooter { # %options => { pref_code => 1 }
noscript id => 'pref_code', title => $self->authGetCode('/xml/prefs.xml'), ''
if $o{pref_code} && $self->authInfo->{id};
script type => 'text/javascript', src => $self->{url_static}.'/f/vndb.js?'.$self->{version}, '';
+ VNWeb::HTML::_scripts_({});
end 'body';
end 'html';
}
diff --git a/lib/VNWeb/HTML.pm b/lib/VNWeb/HTML.pm
index d9098ffe..3cc13494 100644
--- a/lib/VNWeb/HTML.pm
+++ b/lib/VNWeb/HTML.pm
@@ -338,7 +338,7 @@ sub _footer_ {
sub _maintabs_subscribe_ {
my($o, $id) = @_;
- return if !auth || $id !~ /^[twvrpcsd]/;
+ return if !auth || $id !~ /^[twvrpcsdi]/;
my $noti =
$id =~ /^t/ ? tuwf->dbVali('SELECT SUM(x) FROM (
@@ -351,9 +351,10 @@ sub _maintabs_subscribe_ {
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);
+ : $id =~ /^[vrpcsd]/ && 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);
+ my $sub = tuwf->dbRowi('SELECT subnum, subreview, subapply FROM notification_subs WHERE uid =', \auth->uid, 'AND iid =', \$id);
li_ id => 'subscribe', sub {
elm_ Subscribe => $VNWeb::User::Notifications::SUB, {
@@ -361,8 +362,9 @@ sub _maintabs_subscribe_ {
noti => $noti||0,
subnum => $sub->{subnum},
subreview => $sub->{subreview}||0,
+ subapply => $sub->{subapply}||0,
}, sub {
- a_ href => '#', class => ($noti && (!defined $sub->{subnum} || $sub->{subnum})) || $sub->{subnum} || $sub->{subreview} ? 'active' : 'inactive', '🔔';
+ a_ href => '#', class => ($noti && (!defined $sub->{subnum} || $sub->{subnum})) || $sub->{subnum} || $sub->{subreview} || $sub->{subapply} ? 'active' : 'inactive', '🔔';
};
};
}
@@ -459,6 +461,17 @@ sub _hidden_msg_ {
}
+sub _scripts_ {
+ my($o) = @_;
+ script_ type => 'application/json', id => 'pagevars', sub {
+ # Escaping rules for a JSON <script> context are kinda weird, but more efficient than regular xml_escape().
+ lit_(JSON::XS->new->canonical->encode(tuwf->req->{pagevars}) =~ s{</}{<\\/}rg =~ s/<!--/<\\u0021--/rg);
+ } if keys tuwf->req->{pagevars}->%*;
+ script_ type => 'application/javascript', src => config->{url_static}.'/f/elm.js?'.config->{version}, '' if tuwf->req->{pagevars}{elm};
+ script_ type => 'application/javascript', src => config->{url_static}.'/f/plain.js?'.config->{version}, '' if $o->{js} || tuwf->req->{pagevars}{elm};
+}
+
+
# Options:
# title => $title
# index => 1/0, default 0
@@ -489,12 +502,7 @@ sub framework_ {
$cont->() unless $o{hiddenmsg} && _hidden_msg_ \%o;
div_ id => 'footer', \&_footer_;
};
- script_ type => 'application/json', id => 'pagevars', sub {
- # Escaping rules for a JSON <script> context are kinda weird, but more efficient than regular xml_escape().
- lit_(JSON::XS->new->canonical->encode(tuwf->req->{pagevars}) =~ s{</}{<\\/}rg =~ s/<!--/<\\u0021--/rg);
- } if keys tuwf->req->{pagevars}->%*;
- script_ type => 'application/javascript', src => config->{url_static}.'/f/elm.js?'.config->{version}, '' if tuwf->req->{pagevars}{elm};
- script_ type => 'application/javascript', src => config->{url_static}.'/f/plain.js?'.config->{version}, '' if $o{js} || tuwf->req->{pagevars}{elm};
+ _scripts_ \%o;
}
}
}
diff --git a/lib/VNWeb/User/Notifications.pm b/lib/VNWeb/User/Notifications.pm
index 18ddb9f5..aa97b064 100644
--- a/lib/VNWeb/User/Notifications.pm
+++ b/lib/VNWeb/User/Notifications.pm
@@ -13,6 +13,7 @@ my %ntypes = (
subpost => 'Reply to a thread you subscribed to',
subedit => 'Entry you subscribed to has been edited',
subreview => 'New review for a VN you subscribed to',
+ subapply => 'Trait you subscribed to has been (un)applied',
);
@@ -214,9 +215,10 @@ TUWF::hook before => sub {
our $SUB = form_compile any => {
- id => { vndbid => [qw|t w v r p c s d|] },
+ id => { vndbid => [qw|t w v r p c s d i|] },
subnum => { required => 0, jsonbool => 1 },
subreview => { anybool => 1 },
+ subapply => { anybool => 1 },
noti => { uint => 1 }, # Whether the user already gets 'subnum' notifications for this entry (see HTML.pm for possible values)
};
@@ -228,7 +230,7 @@ elm_api Subscribe => undef, $SUB, sub {
$data->{subreview} = 0 if $data->{id} !~ /^v/;
my %where = (iid => delete $data->{id}, uid => auth->uid);
- if(!defined $data->{subnum} && !$data->{subreview}) {
+ if(!defined $data->{subnum} && !$data->{subreview} && !$data->{subapply}) {
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);
diff --git a/sql/func.sql b/sql/func.sql
index ec309237..30e9be4d 100644
--- a/sql/func.sql
+++ b/sql/func.sql
@@ -544,8 +544,6 @@ $$ LANGUAGE plpgsql;
-- 'iid' and 'num' identify the item that has been created.
-- 'uid' indicates who created the item, providing an easy method of not creating a notification for that user.
-- (can technically be fetched with a DB lookup, too)
---
--- TODO: Don't create a notification if the user still has an unread notification on the same item, but lower 'num'?
CREATE OR REPLACE FUNCTION notify(iid vndbid, num integer, uid integer) RETURNS TABLE (uid integer, ntype notification_ntype[], iid vndbid, num int) AS $$
SELECT uid, array_agg(ntype), $1, $2
FROM (
@@ -648,6 +646,17 @@ CREATE OR REPLACE FUNCTION notify(iid vndbid, num integer, uid integer) RETURNS
FROM reviews w, notification_subs ns
WHERE w.id = $1 AND vndbid_type($1) = 'w' AND $2 IS NULL AND ns.iid = vndbid('v', w.vid) AND ns.subreview
+ -- subapply
+ UNION
+ SELECT 'subapply', uid
+ FROM notification_subs
+ WHERE subapply AND vndbid_type($1) = 'c' AND $2 IS NOT NULL
+ AND iid IN(
+ WITH new(tid) AS (SELECT vndbid('i', tid) FROM chars_traits_hist WHERE chid = (SELECT id FROM changes WHERE type = 'c' AND itemid = vndbid_num($1) AND rev = $2)),
+ old(tid) AS (SELECT vndbid('i', tid) FROM chars_traits_hist WHERE chid = (SELECT id FROM changes WHERE type = 'c' AND itemid = vndbid_num($1) AND $2 > 1 AND rev = $2-1))
+ (SELECT tid FROM old EXCEPT SELECT tid FROM new) UNION (SELECT tid FROM new EXCEPT SELECT tid FROM old)
+ )
+
) AS noti(ntype, uid)
WHERE uid <> $3
AND uid <> 1 -- No announcements for Multi
diff --git a/sql/schema.sql b/sql/schema.sql
index 2b213986..934a3de9 100644
--- a/sql/schema.sql
+++ b/sql/schema.sql
@@ -57,7 +57,7 @@ CREATE TYPE edit_rettype AS (itemid integer, chid integer, rev integer);
CREATE TYPE gender AS ENUM ('unknown', 'm', 'f', 'b');
CREATE TYPE language AS ENUM ('ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'eo', 'es', 'fi', 'fr', 'gd', 'he', 'hr', 'hu', 'id', 'it', 'ja', 'ko', 'mk', 'ms', 'lt', 'lv', 'nl', 'no', 'pl', 'pt-pt', 'pt-br', 'ro', 'ru', 'sk', 'sl', 'sv', 'ta', 'th', 'tr', 'uk', 'vi', 'zh');
CREATE TYPE medium AS ENUM ('cd', 'dvd', 'gdr', 'blr', 'flp', 'mrt', 'mem', 'umd', 'nod', 'in', 'otc');
-CREATE TYPE notification_ntype AS ENUM ('pm', 'dbdel', 'listdel', 'dbedit', 'announce', 'post', 'comment', 'subpost', 'subedit', 'subreview');
+CREATE TYPE notification_ntype AS ENUM ('pm', 'dbdel', 'listdel', 'dbedit', 'announce', 'post', 'comment', 'subpost', 'subedit', 'subreview', 'subapply');
CREATE TYPE platform AS ENUM ('win', 'dos', 'lin', 'mac', 'ios', 'and', 'dvd', 'bdp', 'fmt', 'gba', 'gbc', 'msx', 'nds', 'nes', 'p88', 'p98', 'pce', 'pcf', 'psp', 'ps1', 'ps2', 'ps3', 'ps4', 'psv', 'drc', 'sat', 'sfc', 'swi', 'wii', 'wiu', 'n3d', 'x68', 'xb1', 'xb3', 'xbo', 'web', 'oth');
CREATE TYPE producer_type AS ENUM ('co', 'in', 'ng');
CREATE TYPE producer_relation AS ENUM ('old', 'new', 'sub', 'par', 'imp', 'ipa', 'spa', 'ori');
@@ -255,7 +255,8 @@ CREATE TABLE notification_subs (
-- true = Default behavior + get subedit/subpost notifications for this entry.
-- false = Disable all affected ntypes for this entry.
subnum boolean,
- subreview boolean NOT NULL DEFAULT false,
+ subreview boolean NOT NULL DEFAULT false, -- VNs
+ subapply boolean NOT NULL DEFAULT false, -- Traits
PRIMARY KEY(iid,uid)
);
diff --git a/util/updates/2020-10-13-notifications-subapply.sql b/util/updates/2020-10-13-notifications-subapply.sql
new file mode 100644
index 00000000..e20ad2a6
--- /dev/null
+++ b/util/updates/2020-10-13-notifications-subapply.sql
@@ -0,0 +1,3 @@
+ALTER TYPE notification_ntype ADD VALUE 'subapply' AFTER 'subreview';
+ALTER TABLE notification_subs ADD COLUMN subapply boolean NOT NULL DEFAULT false;
+\i sql/func.sql