@@ -24,12 +24,25 @@ sub messages {
$self;
}

sub calculate_unread {
my ($self, $cb) = @_;
$self->messages(
{after => $self->last_read},
sub {
my ($self, $err, $messages) = @_;
$self->{unread} = $messages ? @$messages : 0;
$self->$cb($err);
}
);
}

sub TO_JSON {
my ($self, $persist) = @_;
my %json = map { ($_, $self->$_) } qw(frozen is_private name last_read topic);
$json{connection_id} = $self->connection->id;
$json{dialog_id} = $self->id;
$json{password} = $self->password if $persist;
$json{unread} = $self->{unread} || 0;
return \%json;
}

@@ -108,6 +121,14 @@ Will fetch messages from persistent backend.
See also L<Convos::Core::Backend/messages>.
=head2 calculate_unread
$self = $self->calculate_unread(sub { my ($self, $err) = @_; });
Used to find the number of unread messages after L</last_read>.
EXPERIMENTAL!
=head1 AUTHOR
Jan Henning Thorsen - C<jhthorsen@cpan.org>
@@ -539,13 +539,14 @@
}
},
"Dialog": {
"required": ["connection_id", "dialog_id", "last_read", "name"],
"required": ["connection_id", "dialog_id", "last_read", "name", "unread"],
"properties": {
"connection_id": { "type": "string", "description": "Unique identifier for the connection this dialog is part of" },
"dialog_id": { "type": "string", "description": "Unique identifier for the dialog" },
"last_read": { "type": "string", "format": "date-time" },
"name": { "type": "string", "description": "Name of the room or person" },
"topic": { "type": "string", "description": "The subjec/topic for this room" }
"topic": { "type": "string", "description": "The subjec/topic for this room" },
"unread": { "type": "integer", "description": "Number of unread messages" }
}
},
"Error": {
@@ -24,8 +24,14 @@ $t->get_ok('/api/connection/irc-localhost/dialog/%23convos/participants')->statu
->json_has('/participants/0/mode')->json_has('/participants/0/name');

my $last_read = Mojo::Date->new(1471623058)->to_datetime;
$user->connection({name => 'localhost', protocol => 'irc'})
->dialog({name => '#Convos', frozen => ''})->last_read($last_read);
my $connection = $user->connection({name => 'localhost', protocol => 'irc'});
$connection->_irc->emit(
irc_privmsg => {
prefix => 'Supergirl!super.girl@i.love.debian.org',
params => ['#Convos', 'not a superdupersuperman?']
}
);
$connection->dialog({name => '#Convos', frozen => ''})->last_read($last_read);
$t->get_ok('/api/dialogs')->status_is(200)->json_is(
'/dialogs' => [
{
@@ -36,6 +42,7 @@ $t->get_ok('/api/dialogs')->status_is(200)->json_is(
name => '#Convos',
last_read => '2016-08-19T16:10:58Z',
topic => '',
unread => 1,
},
]
);
@@ -74,6 +81,7 @@ $t->get_ok('/api/user?connections=true&dialogs=true')->status_is(200)->json_is(
name => '#Convos',
last_read => '2016-08-19T16:10:58Z',
topic => '',
unread => 1,
},
{
connection_id => 'irc-example',
@@ -83,6 +91,7 @@ $t->get_ok('/api/user?connections=true&dialogs=true')->status_is(200)->json_is(
last_read => '2016-08-19T16:10:58Z',
name => '#superheroes',
topic => '',
unread => 0,
}
],
'user dialogs'