-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.pm
executable file
·294 lines (217 loc) · 7.35 KB
/
messages.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package messages;
use strict;
use Data::Dumper;
use Apache2::RequestRec;
use Apache2::Const qw(OK REDIRECT);
use HTTP::Date;
use lib "lib";
use Profiles;
use template2;
use profmanager;
use Users;
use mail;
use CGI;
use Alerts;
use sphere;
my $cache = new Cache;
sub handler :method {
my $class = shift;
my $r = shift;
$r->content_type('text/html');
$r->headers_out->set('Expires', HTTP::Date::time2str(time - 60));
my $P = Profiles->new(request => $r, cache => $cache);
unless (ref $P) { return 0; }
#warn "MESSAGES PID: $$";
my $self = {
req => $r,
user => $P->{user},
cache => $P->{cache},
dbh => $P->{dbh},
util => util->new(dbh => $P->{dbh}, cache => $P->{cache}),
query => CGI->new($r),
};
bless $self, $class;
$self->{command} = $P->{command};
if (!$P->verify($P->{user})) {
$r->headers_out->set(Location => "/");
return REDIRECT;
}
#warn "COMMAND $self->{command}";
if ($self->{command} eq "" || $self->{command} eq "/inbox") {
return $self->showInbox();
} elsif ($self->{command} eq "/sendMessage") {
return $self->sendMessage();
} elsif ($self->{command} eq "/sent") {
return $self->messageSentLandingPage();
} elsif ($self->{command} eq "/delete") {
return $self->deleteMessages();
}
return showInbox($self);
}
sub showInbox {
my $P = shift;
my %ids;
my $offset = $P->{query}->param('offset') || 0;
# get all the recent messages
my %conversations;
my $sql = qq|SELECT fromId,toId,date FROM messages WHERE (toId=$P->{user}{user}{id} OR fromId=$P->{user}{user}{id}) AND (hide NOT LIKE '%_$P->{user}{user}{id}_%' OR hide IS NULL) ORDER BY date DESC|;
my $sth = $P->{dbh}->prepare($sql);
$sth->execute;
while (my $message = $sth->fetchrow_hashref) {
my $cid;
if ($message->{toId} eq $P->{user}{user}{id}) {
$cid = $message->{fromId};
} else {
$cid = $message->{toId};
}
if ($conversations{$cid}) {
if ($message->{date} gt $conversations{$cid}) {
$conversations{$cid} = $message->{date};
}
} else {
$conversations{$cid} = $message->{date};
}
}
my $isRead = $P->{dbh}->prepare("SELECT COUNT(1) FROM messages WHERE (toId=$P->{user}{user}{id} AND fromId=?) AND isRead=0");
my $lastMessageRead = $P->{dbh}->prepare("SELECT toId,isRead FROM messages WHERE (fromId=$P->{user}{user}{id} AND toId=?) OR (fromId=? AND toId=$P->{user}{user}{id}) ORDER BY date DESC LIMIT 1");
my $i = 0;
foreach my $uid (sort {$conversations{$b} cmp $conversations{$a}} keys %conversations) {
my $User = Users->new(dbh => $P->{dbh}, cache => $P->{cache}, userId => $uid) or next;
next unless ++$i > $offset;
if ($i < $offset+11) {
$User->{profile}{lastDate} = $conversations{$uid};
$isRead->execute($uid);
$User->{profile}{isRead} = $isRead->fetchrow;
$lastMessageRead->execute($uid,$uid);
if ($lastMessageRead->rows) {
my @r = $lastMessageRead->fetchrow;
if ($r[0] != $uid) {
$User->{profile}{toIsRead} = 0;
} else {
$User->{profile}{toIsRead}= $r[1] == 1 ? 2 : 1;
}
} else {
$User->{profile}{toIsRead} = 0;
}
push(@{ $P->{user}{recent} },{message => $User->profile });
}
}
$P->{user}{message}{total} = $i;
if ($i > $offset+10) {
$P->{user}{inbox}{next} = $offset + 10;
}
if ($offset >= 10) {
$P->{user}{inbox}{prev} = $offset - 10;
}
$isRead->finish;
$lastMessageRead->finish;
print processTemplate($P->{user},"messages.inbox.html");
return 0;
}
sub sendMessage {
my ($P) = @_;
# insert the new message into the database. Then, send the recipient an email.
my $to = $P->{query}->param('to');
my $from = $P->{query}->param('from');
my $text = $P->{query}->param('message');
#warn Dumper($P->{query});
# check to see if this needs to debit a point
my $sql = "SELECT count(1) FROM messages WHERE (toId=$to AND fromId=$from) OR (toId=$from and fromId=$to);";
my $sth = $P->{dbh}->prepare($sql);
$sth->execute;
my $count = $sth->fetchrow;
$sth->finish;
if ($count < 1 && $to != 9656) {
if ($P->{user}{user}{points} < 1) {
$P->ErrorOut("You can't send a message without any points.");
return 0;
}
}
# check blocklist
my $U = Users->new(dbh => $P->{dbh}, cache => $P->{cache}, userId => $to);
if ($U) {
my $blocks = $U->getBlocklist($P->{user}{user}{id});
if (ref $blocks eq 'ARRAY') {
$P->{user}{blocklist} = {
map {$_ => 1} @$blocks
};
}
if ($P->{user}{blocklist}{message}) {
$P->{req}->headers_out->set(Location => "/profiles/".$U->{profile}{linkhandle});
return REDIRECT;
}
}
$text = $P->{dbh}->quote($text);
$sql = "INSERT INTO messages SET hide='',toId=$to,fromId=$from,date=NOW(),text=$text;";
$sth = $P->{dbh}->prepare($sql);
$sth->execute || warn("Failed inserting message into database.");
$sth->finish;
# unhide any previously hidden messages between these people
$P->{dbh}->do("UPDATE messages SET hide=null WHERE (toid=$to and fromid=$from) OR (toid=$from AND fromid=$to)");
# notes to the zombie are free
if ($count == 0 && $to != 9656) {
$P->{dbh}->do("UPDATE users SET points=points - 1 WHERE id=$P->{user}{user}{id};");
$P->{dbh}->do("INSERT INTO spent (userId,date,partner) VALUES ($P->{user}{user}{id},NOW(),'$P->{user}{global}{cobrand}')");
$P->{user}{user}{points}--;
}
my $A = Alerts->new(dbh => $P->{dbh}, cache => $P->{cache}, userId => $to);
if ($A->checkSub('newmessage')) {
my $U = Users->new(dbh => $P->{dbh}, cache => $P->{cache}, userId => $to);
if ($U) {
my %data;
$data{user} = $U->profile;
$data{sender} = $P->{user}{user};
$data{message} = {text => $text};
$A->send('newmessage',\%data);
}
}
$P->{req}->headers_out->set(Location => "/messages.csm/sent?to=$to");
return (REDIRECT);
}
sub messageSentLandingPage {
my ($P) = @_;
my $to = $P->{query}->param('to');
$P->{user}{page}{showpoints} = 1;
my $User = Users->new(dbh => $P->{dbh}, cache => $P->{cache}, userId => $to);
$P->{user}{profile} = $User->profile;
if (0 && sphere::getSuperSphere($P)) {
# randomize a little
my $i;
for ($i = @{$P->{user}{superResults}}; --$i; ) {
my $j = int rand ($i+1);
@{$P->{user}{superResults}}[$i,$j] = @{$P->{user}{superResults}}[$j,$i];
}
} else {
my $sql = "SELECT profileId FROM thumb WHERE type='U' AND profileId != $P->{user}{user}{id} ORDER BY RAND() LIMIT 15";
my $sth = $P->{dbh}->prepare($sql);
$sth->execute;
my $count =0;
while (my $id = $sth->fetchrow) {
my $User = Users->new(dbh => $P->{dbh}, cache => $P->{cache}, userId => $id);
next unless $User;
push(@{$P->{user}{superResults}},{user => $User->profile});
$count++;
last if $count == 5;
}
$sth->finish;
}
print processTemplate($P->{user},"messages.sent.html");
return 0;
}
sub deleteMessages {
my ($P) = @_;
my $otheruser = $P->{query}->param('userId');
my $sth = $P->{dbh}->prepare("SELECT hide FROM messages WHERE (toId=? AND fromId=$P->{user}{user}{id}) OR (toId=$P->{user}{user}{id} AND fromId=?)");
$sth->execute($otheruser,$otheruser);
my $hide = $sth->fetchrow;
$hide .= '_'.$P->{user}{user}{id}.'_';
#warn "UPDATING HIDE: $hide";
$P->{dbh}->do("UPDATE messages SET hide = ?, isread = 1 WHERE (toId=? AND fromId=$P->{user}{user}{id}) OR (toId=$P->{user}{user}{id} AND fromId=?)",undef,$hide,$otheruser,$otheruser);
$P->{req}->headers_out->set(Location => '/messages.csm/inbox');
return REDIRECT;
}
sub ErrorOut {
my $self = shift;
$self->{user}{page}{message} = +shift;
Profiles::ErrorOut($self->{user});
}