-
Notifications
You must be signed in to change notification settings - Fork 84
/
Conversation.pm
149 lines (92 loc) · 3.28 KB
/
Conversation.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package Convos::Core::Conversation;
use Mojo::Base -base;
use Convos::Util qw($CHANNEL_RE logf);
use Mojo::Date;
has frozen => '';
has info => sub { +{} };
has name => sub { Carp::confess('name required in constructor') };
has password => '';
has notifications => 0;
has topic => '';
has unread => 0;
sub connection { shift->{connection} or Carp::confess('connection required in constructor') }
sub id { my $from = $_[1] || $_[0]; lc($from->{id} // $from->{name}) }
sub is_private { shift->name =~ /^$CHANNEL_RE/ ? 0 : 1 }
sub inc_notifications { $_[0]->{notifications}++; $_[0] }
sub inc_unread { $_[0]->{unread}++; $_[0] }
sub messages_p {
my ($self, $query) = @_;
return $self->connection->user->core->backend->messages_p($self, $query);
}
# back compat - will be removed in future version
sub _calculate_unread_p {
my $self = shift;
return unless my $last_read = delete $self->{last_read};
return $self->messages_p({after => $last_read, limit => 61})
->then(sub { $self->unread(int @{shift->{messages}}) });
}
sub TO_JSON {
my ($self, $persist) = @_;
my %json = map { ($_, $self->$_) } qw(frozen name notifications topic unread);
$json{connection_id} = $self->connection->id;
$json{conversation_id} = $self->id;
$json{info} = $self->info;
$json{last_read} = $self->{last_read} if $self->{last_read}; # back compat
$json{password} = $self->password if $persist;
return \%json;
}
1;
=encoding utf8
=head1 NAME
Convos::Core::Conversation - A convos conversation base class
=head1 DESCRIPTION
L<Convos::Core::Conversation> represents a conversation (conversation) with one or
more participants.
=head1 ATTRIBUTES
=head2 connection
Holds a L<Convos::Core::Connection> object.
=head2 frozen
$str = $conversation->frozen;
Will be set to a description if the conversation is "frozen", which means you are
no longer part of it.
=head2 id
$str = $conversation->id;
$str = $class->id(\%attr);
Returns a unique identifier for a conversation.
=head2 info
$hash_ref = $conversation->info;
$conversation = $conversation->info({away => 1});
Extra information about the conversation. Might come from a "WHOIS" reply.
=head2 name
$str = $conversation->name;
The name of this conversation.
=head2 password
$str = $conversation->password;
The password used to join this conversation.
=head2 topic
$str = $conversation->topic;
The topic (subject) of the conversation.
=head2 unread
$int = $conversation->unread;
Holds the number of unread messages.
=head2 notifications
$int = $conversation->notifications;
Holds the number of unread notifications.
=head1 METHODS
=head2 inc_unread
$conversation = $conversation->inc_unread;
Used to increase the L</unread> count.
=head2 inc_notifications
$p = $conversation->inc_notifications;
Used to increate the unread L</notifications> count.
=head2 is_private
$bool = $conversation->is_private;
Returns true if you are only talking to a single user and no other
participants can join the conversation.
=head2 messages_p
$p = $conversation->messages_p(\%query)->then(sub { my $messages = shift; });
Will fetch messages from persistent backend.
See also L<Convos::Core::Backend/messages>.
=head1 SEE ALSO
L<Convos::Core>.
=cut