3 * StatusNet - the distributed open-source microblogging tool
5 * Handler for queue items of type 'usersum', sends an email summaries
6 * to a particular user.
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.
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.
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/>.
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/
29 if (!defined('STATUSNET')) {
34 * Handler for queue items of type 'usersum', sends an email summaries
35 * to a particular user.
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/
44 class UserEmailSummaryHandler extends QueueHandler
46 // Maximum number of notices to include by default. This is probably too much.
47 const MAX_NOTICES = 200;
50 * Return transport keyword which identifies items this queue handler
51 * services; must be defined for all subclasses.
53 * Must be 8 characters or less to fit in the queue_item database.
54 * ex "email", "jabber", "sms", "irc", ...
64 * Send a summary email to the user
66 * @param mixed $object
67 * @return boolean true on success, false on failure
69 function handle($user_id)
71 // Skip if they've asked not to get summaries
73 $ess = Email_summary_status::staticGet('user_id', $user_id);
75 if (!empty($ess) && !$ess->send_summary) {
76 common_log(LOG_INFO, sprintf('Not sending email summary for user %s by request.', $user_id));
83 $since_id = $ess->last_summary_id;
86 $user = User::staticGet('id', $user_id);
89 common_log(LOG_INFO, sprintf('Not sending email summary for user %s; no such user.', $user_id));
93 if (empty($user->email)) {
94 common_log(LOG_INFO, sprintf('Not sending email summary for user %s; no email address.', $user_id));
98 $profile = $user->getProfile();
100 if (empty($profile)) {
101 common_log(LOG_WARNING, sprintf('Not sending email summary for user %s; no profile.', $user_id));
105 $notice = $user->ownFriendsTimeline(0, self::MAX_NOTICES, $since_id);
107 if (empty($notice) || $notice->N == 0) {
108 common_log(LOG_WARNING, sprintf('Not sending email summary for user %s; no notices.', $user_id));
112 // XXX: This is risky fingerpoken in der objektvars, but I didn't feel like
113 // figuring out a better way. -ESP
117 if ($notice instanceof ArrayWrapper) {
118 $new_top = $notice->_items[0]->id;
121 $out = new XMLStringer();
123 $out->elementStart('div', array('width' => '100%',
124 'style' => 'background-color: #ffffff; border: 4px solid #4c609a; padding: 10px;'));
126 $out->elementStart('div', array('style' => 'color: #ffffff; background-color: #4c609a; font-weight: bold; margin-bottom: 10px; padding: 4px;'));
127 // TRANS: Text in e-mail summary.
128 // TRANS: %1$s is the StatusNet sitename, %2$s is the recipient's profile name.
129 $out->raw(sprintf(_m('Recent updates from %1$s for %2$s:'),
130 common_config('site', 'name'),
131 $profile->getBestName()));
132 $out->elementEnd('div');
134 $out->elementStart('table', array('width' => '550px',
135 'style' => 'border: none; border-collapse: collapse;', 'cellpadding' => '6'));
137 while ($notice->fetch()) {
138 $profile = Profile::staticGet('id', $notice->profile_id);
140 if (empty($profile)) {
144 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
146 $out->elementStart('tr');
147 $out->elementStart('td', array('width' => AVATAR_STREAM_SIZE,
148 'height' => AVATAR_STREAM_SIZE,
151 'style' => 'border-bottom: 1px dotted #C5CEE3; padding: 10px 6px 10px 6px;'));
152 $out->element('img', array('src' => ($avatar) ?
153 $avatar->displayUrl() :
154 Avatar::defaultImage(AVATAR_STREAM_SIZE),
155 'width' => AVATAR_STREAM_SIZE,
156 'height' => AVATAR_STREAM_SIZE,
157 'alt' => $profile->getBestName()));
158 $out->elementEnd('td');
159 $out->elementStart('td', array('align' => 'left',
161 'style' => 'border-bottom: 1px dotted #C5CEE3; padding: 10px 6px 10px 6px;'));
162 $out->element('a', array('href' => $profile->profileurl),
165 $out->raw($notice->rendered);
166 $out->elementStart('div', array('style' => 'font-size: 0.8em; padding-top: 4px;'));
167 $noticeurl = $notice->bestUrl();
168 // above should always return an URL
169 assert(!empty($noticeurl));
170 $out->elementStart('a', array('rel' => 'bookmark',
171 'href' => $noticeurl));
172 $dt = common_date_iso8601($notice->created);
173 $out->element('abbr', array('style' => 'border-bottom: none;',
175 common_date_string($notice->created));
176 $out->elementEnd('a');
177 if ($notice->hasConversation()) {
178 $conv = Conversation::staticGet('id', $notice->conversation);
179 $convurl = $conv->uri;
180 if (!empty($convurl)) {
183 array('href' => $convurl.'#notice-'.$notice->id),
184 // TRANS: Link text for link to conversation view.
188 $out->elementEnd('div');
189 $out->elementEnd('td');
190 $out->elementEnd('tr');
193 $out->elementEnd('table');
195 // TRANS: Link text for link to e-mail settings.
196 // TRANS: %1$s is a link to the e-mail settings, %2$s is the StatusNet sitename.
197 $out->raw("<p>" . sprintf(_m('<a href="%1$s">change your email settings for %2$s</a>'),
198 common_local_url('emailsettings'),
199 common_config('site', 'name'))."</p>");
201 $out->elementEnd('div');
203 $body = $out->getString();
205 // FIXME: do something for people who don't like HTML email
207 // TRANS: Subject for e-mail.
208 mail_to_user($user, _m('Updates from your network'), $body,
209 array('Content-Type' => 'text/html; charset=UTF-8'));
212 $ess = new Email_summary_status();
214 $ess->user_id = $user_id;
215 $ess->created = common_sql_now();
216 $ess->last_summary_id = $new_top;
217 $ess->modified = common_sql_now();
223 $ess->last_summary_id = $new_top;
224 $ess->modified = common_sql_now();