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