]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailSummary/lib/useremailsummaryhandler.php
Debugging log fix.
[quix0rs-gnu-social.git] / plugins / EmailSummary / lib / useremailsummaryhandler.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  *
5  * Handler for queue items of type 'usersum', sends an email summaries
6  * to a particular user.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * @category  Sample
22  * @package   StatusNet
23  * @author    Evan Prodromou <evan@status.net>
24  * @copyright 2010 StatusNet, Inc.
25  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
26  * @link      http://status.net/
27  */
28
29 if (!defined('GNUSOCIAL')) { exit(1); }
30
31 /**
32  * Handler for queue items of type 'usersum', sends an email summaries
33  * to a particular user.
34  *
35  * @category  Email
36  * @package   StatusNet
37  * @author    Evan Prodromou <evan@status.net>
38  * @copyright 2010 StatusNet, Inc.
39  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
40  * @link      http://status.net/
41  */
42 class UserEmailSummaryHandler extends QueueHandler
43 {
44     // Maximum number of notices to include by default. This is probably too much.
45     const MAX_NOTICES = 200;
46
47     /**
48      * Return transport keyword which identifies items this queue handler
49      * services; must be defined for all subclasses.
50      *
51      * Must be 8 characters or less to fit in the queue_item database.
52      * ex "email", "jabber", "sms", "irc", ...
53      *
54      * @return string
55      */
56     function transport()
57     {
58         return 'usersum';
59     }
60
61     /**
62      * Send a summary email to the user
63      *
64      * @param mixed $object
65      * @return boolean true on success, false on failure
66      */
67     function handle($user_id)
68     {
69         // Skip if they've asked not to get summaries
70
71         $ess = Email_summary_status::getKV('user_id', $user_id);
72
73         if (!empty($ess) && !$ess->send_summary) {
74             common_log(LOG_INFO, sprintf('Not sending email summary for user %s by request.', $user_id));
75             return true;
76         }
77
78         $since_id = null;
79
80         if (!empty($ess)) {
81             $since_id = $ess->last_summary_id;
82         }
83
84         $user = User::getKV('id', $user_id);
85
86         if (empty($user)) {
87             common_log(LOG_INFO, sprintf('Not sending email summary for user %s; no such user.', $user_id));
88             return true;
89         }
90
91         if (empty($user->email)) {
92             common_log(LOG_INFO, sprintf('Not sending email summary for user %s; no email address.', $user_id));
93             return true;
94         }
95
96         try {
97             $profile = $user->getProfile();
98         } catch (UserNoProfileException $e) {
99             common_log(LOG_WARNING, sprintf('Not sending email summary for user %s; no profile.', $user_id));
100             return true;
101         }
102
103         // An InboxNoticeStream for a certain user, scoped to its own view
104         $stream = new InboxNoticeStream($profile, $profile);
105
106         $notice = $stream->getNotices(0, self::MAX_NOTICES, $since_id);
107
108         if (empty($notice) || $notice->N == 0) {
109             common_log(LOG_WARNING, sprintf('Not sending email summary for user %s; no notices.', $user_id));
110             return true;
111         }
112
113         // XXX: This is risky fingerpoken in der objektvars, but I didn't feel like
114         // figuring out a better way. -ESP
115
116         $new_top = null;
117
118         if ($notice->fetch()) {
119             $new_top = $notice->id;
120         }
121
122         // TRANS: Subject for e-mail.
123         $subject = sprintf(_m('Your latest updates from %s'), common_config('site', 'name'));
124
125         $out = new XMLStringer(true);
126
127         $out->elementStart('html');
128         $out->elementStart('head');
129         $out->element('title', null, $subject);
130         $out->elementEnd('head');
131         $out->elementStart('body');
132         $out->elementStart('div', array('width' => '100%',
133                                         'style' => 'background-color: #ffffff; border: 4px solid #4c609a; padding: 10px;'));
134
135         $out->elementStart('div', array('style' => 'color: #ffffff; background-color: #4c609a; font-weight: bold; margin-bottom: 10px; padding: 4px;'));
136         // TRANS: Text in e-mail summary.
137         // TRANS: %1$s is the StatusNet sitename, %2$s is the recipient's profile name.
138         $out->raw(sprintf(_m('Recent updates from %1$s for %2$s:'),
139                           common_config('site', 'name'),
140                           $profile->getBestName()));
141         $out->elementEnd('div');
142
143         $out->elementStart('table', array('width' => '550px',
144                                           'style' => 'border: none; border-collapse: collapse;', 'cellpadding' => '6'));
145
146         do {
147             $profile = Profile::getKV('id', $notice->profile_id);
148
149             if (empty($profile)) {
150                 continue;
151             }
152
153             $avatarUrl = $profile->avatarUrl(AVATAR_STREAM_SIZE);
154
155             $out->elementStart('tr');
156             $out->elementStart('td', array('width' => AVATAR_STREAM_SIZE,
157                                            'height' => AVATAR_STREAM_SIZE,
158                                            'align' => 'left',
159                                            'valign' => 'top',
160                                            'style' => 'border-bottom: 1px dotted #C5CEE3; padding: 10px 6px 10px 6px;'));
161             $out->element('img', array('src' => $avatarUrl,
162                                        'width' => AVATAR_STREAM_SIZE,
163                                        'height' => AVATAR_STREAM_SIZE,
164                                        'alt' => $profile->getBestName()));
165             $out->elementEnd('td');
166             $out->elementStart('td', array('align' => 'left',
167                                            'valign' => 'top',
168                                            'style' => 'border-bottom: 1px dotted #C5CEE3; padding: 10px 6px 10px 6px;'));
169             $out->element('a', array('href' => $profile->profileurl),
170                           $profile->nickname);
171             $out->text(' ');
172             $out->raw($notice->getRendered());
173             $out->elementStart('div', array('style' => 'font-size: 0.8em; padding-top: 4px;'));
174             $noticeurl = $notice->getLocalUrl();
175             // above should always return an URL
176             assert(!empty($noticeurl));
177             $out->elementStart('a', array('rel' => 'bookmark',
178                                           'href' => $noticeurl));
179             $dt = common_date_iso8601($notice->created);
180             $out->element('abbr', array('style' => 'border-bottom: none;',
181                                         'title' => $dt),
182                           common_date_string($notice->created));
183             $out->elementEnd('a');
184             $out->element('a', array('href' => $notice->getConversationUrl()),
185                           // TRANS: Link text for link to conversation view.
186                           _m('in context'));
187             $out->elementEnd('div');
188             $out->elementEnd('td');
189             $out->elementEnd('tr');
190         } while ($notice->fetch());
191
192         $out->elementEnd('table');
193
194         // TRANS: Link text for link to e-mail settings.
195         // TRANS: %1$s is a link to the e-mail settings, %2$s is the StatusNet sitename.
196         $out->raw("<p>" . sprintf(_m('<a href="%1$s">change your email settings for %2$s</a>'),
197                           common_local_url('emailsettings'),
198                           common_config('site', 'name'))."</p>");
199
200         $out->elementEnd('div');
201         $out->elementEnd('body');
202         $out->elementEnd('html');
203
204         $body = $out->getString();
205
206         // FIXME: do something for people who don't like HTML email
207
208         mail_to_user($user,
209                      $subject,
210                      $body,
211                      array('Content-Type' => 'text/html; charset=utf-8',
212                            'Mime-Version' => '1.0'));
213
214         if (empty($ess)) {
215             $ess = new Email_summary_status();
216
217             $ess->user_id         = $user_id;
218             $ess->created         = common_sql_now();
219             $ess->last_summary_id = $new_top;
220             $ess->modified        = common_sql_now();
221
222             $ess->insert();
223         } else {
224             $orig = clone($ess);
225
226             $ess->last_summary_id = $new_top;
227             $ess->modified        = common_sql_now();
228
229             $ess->update($orig);
230         }
231
232         return true;
233     }
234 }