]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailSummary/useremailsummaryhandler.php
set height and width of avatar td in email summary
[quix0rs-gnu-social.git] / plugins / EmailSummary / 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
45 class UserEmailSummaryHandler extends QueueHandler
46 {
47     // Maximum number of notices to include by default. This is probably too much.
48     
49     const MAX_NOTICES = 200;
50     
51     /**
52      * Return transport keyword which identifies items this queue handler
53      * services; must be defined for all subclasses.
54      *
55      * Must be 8 characters or less to fit in the queue_item database.
56      * ex "email", "jabber", "sms", "irc", ...
57      *
58      * @return string
59      */
60     
61     function transport()
62     {
63         return 'sitesum';
64     }
65
66     /**
67      * Send a summary email to the user
68      * 
69      * @param mixed $object
70      * @return boolean true on success, false on failure
71      */
72     
73     function handle($user_id)
74     {
75         // Skip if they've asked not to get summaries
76
77         $ess = Email_summary_status::staticGet('user_id', $user_id);
78         
79         if (!empty($ess) && !$ess->send_summary) {
80             common_log(LOG_INFO, sprintf('Not sending email summary for user %s by request.', $user_id));
81             return true;
82         }
83
84         $since_id = null;
85         
86         if (!empty($ess)) {
87             $since_id = $ess->last_summary_id;
88         }
89           
90         $user = User::staticGet('id', $user_id);
91
92         if (empty($user)) {
93             common_log(LOG_INFO, sprintf('Not sending email summary for user %s; no such user.', $user_id));
94             return true;
95         }
96         
97         if (empty($user->email)) {
98             common_log(LOG_INFO, sprintf('Not sending email summary for user %s; no email address.', $user_id));
99             return true;
100         }
101         
102         $profile = $user->getProfile();
103         
104         if (empty($profile)) {
105             common_log(LOG_WARNING, sprintf('Not sending email summary for user %s; no profile.', $user_id));
106             return true;
107         }
108         
109         $notice = $user->ownFriendsTimeline(0, self::MAX_NOTICES, $since_id);
110
111         if (empty($notice) || $notice->N == 0) {
112             common_log(LOG_WARNING, sprintf('Not sending email summary for user %s; no notices.', $user_id));
113             return true;
114         }
115
116         // XXX: This is risky fingerpoken in der objektvars, but I didn't feel like
117         // figuring out a better way. -ESP
118
119         $new_top = null;
120         
121         if ($notice instanceof ArrayWrapper) {
122             $new_top = $notice->_items[0]->id;
123         }
124         
125         $out = new XMLStringer();
126
127         $out->raw(sprintf(_('<p>Recent updates from %1s for %2s:</p>'),
128                           common_config('site', 'name'),
129                           $profile->getBestName()));
130         
131
132         $out->elementStart('table', array('width' => '100%', 'style' => 'border: none'));
133         
134         while ($notice->fetch()) {
135             
136             $profile = Profile::staticGet('id', $notice->profile_id);
137             
138             if (empty($profile)) {
139                 continue;
140             }
141             
142             $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
143
144             $out->elementStart('tr');
145             $out->elementStart('td', array('width' => AVATAR_STREAM_SIZE,
146                                            'height' => AVATAR_STREAM_SIZE));
147             $out->element('img', array('src' => ($avatar) ?
148                                        $avatar->displayUrl() :
149                                        Avatar::defaultImage($avatar_size),
150                                        'class' => 'avatar photo',
151                                        'width' => $avatar_size,
152                                        'height' => $avatar_size,
153                                        'alt' => $profile->getBestName()));
154             $out->elementEnd('td');
155             $out->elementStart('td');
156             $out->element('a', array('href' => $profile->profileurl),
157                           $profile->nickname);
158             $out->text(' ');
159             $out->raw($notice->rendered);
160             $out->element('br'); // yeah, you know it. I just wrote a <br> in the middle of my table layout.
161             $noticeurl = $notice->bestUrl();
162             // above should always return an URL
163             assert(!empty($noticeurl));
164             $out->elementStart('a', array('rel' => 'bookmark',
165                                           'class' => 'timestamp',
166                                           'href' => $noticeurl));
167             $dt = common_date_iso8601($notice->created);
168             $out->element('abbr', array('class' => 'published',
169                                         'title' => $dt),
170                           common_date_string($notice->created));
171             $out->elementEnd('a');
172             if ($notice->hasConversation()) {
173                 $conv = Conversation::staticGet('id', $notice->conversation);
174                 $convurl = $conv->uri;
175                 if (!empty($convurl)) {
176                                   $out->text(' ');
177                                   $out->element('a',
178                                                 array('href' => $convurl.'#notice-'.$notice->id,
179                                                       'class' => 'response'),
180                                                 _('in context'));
181                 }
182             }
183             $out->elementEnd('td');
184             $out->elementEnd('tr');
185         }
186         
187         $out->elementEnd('table');
188         
189         $out->raw(sprintf(_('<p><a href="%1s">change your email settings for %2s</a></p>'),
190                           common_local_url('emailsettings'),
191                           common_config('site', 'name')));
192
193         $body = $out->getString();
194         
195         // FIXME: do something for people who don't like HTML email
196         
197         mail_to_user($user, _('Updates from your network'), $body,
198                      array('Content-Type' => 'text/html; charset=UTF-8'));
199
200         if (empty($ess)) {
201             
202             $ess = new Email_summary_status();
203             
204             $ess->user_id         = $user_id;
205             $ess->created         = common_sql_now();
206             $ess->last_summary_id = $new_top;
207             $ess->modified        = common_sql_now();
208
209             $ess->insert();
210             
211         } else {
212             
213             $orig = clone($ess);
214             
215             $ess->last_summary_id = $new_top;
216             $ess->modified        = common_sql_now();
217
218             $ess->update();
219         }
220         
221         return true;
222     }
223 }