Skip to content

Commit

Permalink
Add support for notifications on keywords, closes #142
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Feb 20, 2017
1 parent f5d0a79 commit 9a31ef7
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 21 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Revision history for perl distribution Convos

0.99_27 Not Released
- Fix going to connection dialog after connection save
- Add support for notifications on keywords #142

0.99_26 2017-02-20T23:11:01+0100
- Fix scrolling of sidebars and main menu
Expand Down
2 changes: 2 additions & 0 deletions assets/js/core/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
this.currentPage = "";
this.dialogs = [];
this.email = "";
this.highlightKeywords = [];
this.notifications = [];
this.unread = 0;
this._keepAliveTid = setInterval(this._keepAlive(), 20000);
Expand Down Expand Up @@ -92,6 +93,7 @@
data.connections.forEach(function(c) { self.ensureConnection(c); });
data.dialogs.forEach(function(d) { d.reset = true; self.ensureDialog(d); });
self.email = data.email;
self.highlightKeywords = data.highlight_keywords || [];
self.notifications = data.notifications.reverse();
self.unread = data.unread || 0;
self.currentPage = "convos-chat";
Expand Down
16 changes: 10 additions & 6 deletions assets/vue/convos-profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<md-input :value.sync="password" type="password" placeholder="At least six characters" cols="s6">Password</md-input>
<md-input :value.sync="passwordAgain" type="password" cols="s6">Repeat password</md-input>
</div>
<div class="row">
<md-input :value.sync="highlightKeywords" placeholder="cats, superheroes, ..." cols="s12">Notification keywords</md-input>
</div>
<div class="row">
<div class="col s12">
<input v-model="sortDialogsByRead" type="checkbox" class="filled-in" id="form_sort_by">
Expand Down Expand Up @@ -43,6 +46,7 @@ module.exports = {
data: function() {
return {
errors: [],
highlightKeywords: "",
password: "",
passwordAgain: "",
notifications: Notification.permission,
Expand All @@ -57,6 +61,7 @@ module.exports = {
methods: {
save: function() {
var self = this;
var highlightKeywords = this.highlightKeywords.split(/[,\s]+/).filter(function(k) { return k.length; });
if (this.password != this.passwordAgain)
return this.errors = [{message: "Passwords does not match"}];
Expand All @@ -65,16 +70,15 @@ module.exports = {
if (!this.notifications)
this.enableNotifications(false);
if (this.password) {
Convos.api.updateUser({body: {password: this.password}}, function(err, res) {
if (!err) this.password = "";
self.errors = err || [];
});
}
Convos.api.updateUser({body: {highlight_keywords: highlightKeywords, password: this.password}}, function(err, res) {
if (!err) this.password = "";
self.errors = err || [];
});
}
},
ready: function() {
this.sortDialogsByRead = this.settings.sortDialogsBy == "lastRead";
this.highlightKeywords = this.user.highlightKeywords.join(", ");
}
};
</script>
3 changes: 2 additions & 1 deletion lib/Convos/Controller/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ sub update {
$self->delay(
sub {
my ($delay) = @_;
$user->set_password($json->{password}) if $json->{password};
$user->highlight_keywords($json->{highlight_keywords}) if $json->{highlight_keywords};
$user->set_password($json->{password}) if $json->{password};
$user->save($delay->begin);
},
sub {
Expand Down
2 changes: 1 addition & 1 deletion lib/Convos/Core/Connection/Irc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ sub _event_privmsg {
$target ||= $self->messages;
$from ||= $self->id;
$highlight = grep { $msg->{params}[1] =~ /\b\Q$_\E\b/i } $self->_irc->nick,
@{$self->url->query->every_param('highlight')};
@{$self->user->highlight_keywords};

$target->last_active(Mojo::Date->new->to_datetime);

Expand Down
6 changes: 4 additions & 2 deletions lib/Convos/Core/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use constant BCRYPT_BASE_SETTINGS => do {

sub core { shift->{core} or die 'core is required in constructor' }
sub email { shift->{email} or die 'email is required in constructor' }
has highlight_keywords => sub { +[] };
sub password { shift->{password} ||= '' }
has unread => sub {0};

Expand All @@ -38,7 +39,7 @@ has_many connections => 'Convos::Core::Connection' => sub {

sub get {
my ($self, $args, $cb) = @_;
my $res = $self->TO_JSON;
my $res = $self->TO_JSON;

Mojo::IOLoop->delay(
sub {
Expand Down Expand Up @@ -144,7 +145,8 @@ sub TO_JSON {
$self->{registered} ||= Mojo::Date->new->to_datetime;
my $json = {map { ($_, $self->{$_} // '') } qw(email password registered)};
delete $json->{password} unless $persist;
$json->{unread} = $self->unread;
$json->{highlight_keywords} = $self->highlight_keywords;
$json->{unread} = $self->unread;
$json;
}

Expand Down
5 changes: 5 additions & 0 deletions public/convos-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,11 @@
"email": { "type": "string", "description": "Unique email identifying a user in Convos" },
"registered": { "type": "string", "format": "date-time", "description": "Example: 2015-09-06T10:47:31Z" },
"unread": { "type": "integer", "description": "Number of unread notifications" },
"highlight_keywords": {
"description": "Extra keywords to highlight on",
"type": "array",
"items": { "type": "string" }
},
"connections": {
"type": "array",
"items": { "$ref": "#/definitions/Connection" }
Expand Down
7 changes: 4 additions & 3 deletions t/user.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ ok -e $settings_file, 'created storage file';
is_deeply(
$user->TO_JSON,
{
email => 'jhthorsen@cpan.org',
registered => Mojo::Date->new($main::time)->to_datetime,
unread => 0
email => 'jhthorsen@cpan.org',
highlight_keywords => [],
registered => Mojo::Date->new($main::time)->to_datetime,
unread => 0
},
'TO_JSON'
);
Expand Down
14 changes: 14 additions & 0 deletions t/web-irc-highlight.t
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,18 @@ is @{$t->tx->res->json->{notifications}}, $n + 1, 'only one new notification';
is int(grep { $_->{highlight} } @events), 2, 'marked as highlight';
is int(grep { !$_->{highlight} } @events), 2, 'not marked as highlight';

$user->highlight_keywords(['normal', 'yikes']);
$connection->_event_privmsg(
{
event => 'privmsg',
prefix => 'batgirl!batgirl@i.love.debian.org',
params => ['#convos', 'Or what about a normal message in a channel?'],
}
);

$stop_at_n_events = 5;
$ws->ua->ioloop->start;
$t->get_ok('/api/notifications')->status_is(200);
is @{$t->tx->res->json->{notifications}}, $n + 2, 'notification on custom keyword';

done_testing;
35 changes: 27 additions & 8 deletions t/web-user.t
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,38 @@ my $registered = $t->tx->res->json->{registered};

$t->post_ok('/api/user', json => {})->status_is(200);

$t->get_ok('/api/user')->status_is(200)
->json_is('', {email => 'superman@example.com', registered => $registered, unread => 0});
$t->get_ok('/api/user')->status_is(200)->json_is(
'',
{
email => 'superman@example.com',
highlight_keywords => [],
registered => $registered,
unread => 0
}
);

$t->post_ok('/api/user', json => {highlight_keywords => ['foo']})->status_is(200);
$t->get_ok('/api/user')->status_is(200)->json_is(
'',
{
email => 'superman@example.com',
highlight_keywords => ['foo'],
registered => $registered,
unread => 0
}
);

$t->app->core->get_user('superman@example.com')->unread(4);
$t->get_ok('/api/user?connections=true&dialogs=true&notifications=true')->status_is(200)->json_is(
'',
{
connections => [],
dialogs => [],
notifications => [],
email => 'superman@example.com',
registered => $registered,
unread => 4,
connections => [],
dialogs => [],
highlight_keywords => ['foo'],
notifications => [],
email => 'superman@example.com',
registered => $registered,
unread => 4,
}
);

Expand Down

0 comments on commit 9a31ef7

Please sign in to comment.