]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/RealtimePlugin.php
Do realtime popup with PHP instead of Javascript
[quix0rs-gnu-social.git] / plugins / Realtime / RealtimePlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Superclass for plugins that do "real time" updates of timelines using Ajax
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * Superclass for plugin to do realtime updates
36  *
37  * Based on experience with the Comet and Meteor plugins,
38  * this superclass extracts out some of the common functionality
39  *
40  * @category Plugin
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46
47 class RealtimePlugin extends Plugin
48 {
49     protected $replyurl = null;
50     protected $favorurl = null;
51     protected $deleteurl = null;
52
53     /**
54      * When it's time to initialize the plugin, calculate and
55      * pass the URLs we need.
56      */
57
58     function onInitializePlugin()
59     {
60         $this->replyurl = common_local_url('newnotice');
61         $this->favorurl = common_local_url('favor');
62         // FIXME: need to find a better way to pass this pattern in
63         $this->deleteurl = common_local_url('deletenotice',
64                                             array('notice' => '0000000000'));
65         return true;
66     }
67
68     function onEndShowScripts($action)
69     {
70         $timeline = $this->_getTimeline($action);
71
72         // If there's not a timeline on this page,
73         // just return true
74
75         if (empty($timeline)) {
76             return true;
77         }
78
79         $scripts = $this->_getScripts();
80
81         foreach ($scripts as $script) {
82             $action->script($script);
83         }
84
85         $user = common_current_user();
86
87         if (!empty($user->id)) {
88             $user_id = $user->id;
89         } else {
90             $user_id = 0;
91         }
92
93         $action->elementStart('script', array('type' => 'text/javascript'));
94
95         $script = ' $(document).ready(function() { '.
96           $this->_updateInitialize($timeline, $user_id).
97           '}); ';
98
99         $action->raw($script);
100
101         $action->elementEnd('script');
102
103         return true;
104     }
105
106     function onEndNoticeSave($notice)
107     {
108         $paths = array();
109
110         // XXX: Add other timelines; this is just for the public one
111
112         if ($notice->is_local ||
113             ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
114             $paths[] = array('public');
115         }
116
117         $tags = $this->getNoticeTags($notice);
118
119         if (!empty($tags)) {
120             foreach ($tags as $tag) {
121                 $paths[] = array('tag', $tag);
122             }
123         }
124
125         if (count($paths) > 0) {
126
127             $json = $this->noticeAsJson($notice);
128
129             $this->_connect();
130
131             foreach ($paths as $path) {
132                 $timeline = $this->_pathToChannel($path);
133                 $this->_publish($timeline, $json);
134             }
135
136             $this->_disconnect();
137         }
138
139         return true;
140     }
141
142     function onStartShowPageNotice($action)
143     {
144         $timeline = $this->_getTimeline($action);
145         if (!empty($timeline)) {
146             $base = $action->selfUrl();
147             if (mb_strstr($url, '?')) {
148                 $url = $base . '&realtime=1';
149             } else {
150                 $url = $base . '?realtime=1';
151             }
152             $title = $action->title();
153             $code = "window.open('$url', '$title', 'toolbar=no,resizable=yes,scrollbars=yes,status=yes,height=640,width=575');";
154             $action->element('a', array('href' => $base,
155                                         'onclick' => $code,
156                                         'id' => 'realtime_timeline',
157                                         'title' => _('Pop up')),
158                              'Pop up');
159
160         }
161         return true;
162     }
163
164     function onStartShowBody($action)
165     {
166         $realtime = $action->boolean('realtime');
167         if (!$realtime) {
168             return true;
169         }
170
171         $action->elementStart('body',
172                               (common_current_user()) ? array('id' => $action->trimmed('action'),
173                                                               'class' => 'user_in')
174                               : array('id' => $action->trimmed('action')));
175         if (common_logged_in()) {
176             $action->showNoticeForm();
177         }
178         $action->showContent();
179         $action->elementEnd('body');
180         return false; // No default processing
181     }
182
183     function noticeAsJson($notice)
184     {
185         // FIXME: this code should be abstracted to a neutral third
186         // party, like Notice::asJson(). I'm not sure of the ethics
187         // of refactoring from within a plugin, so I'm just abusing
188         // the TwitterApiAction method. Don't do this unless you're me!
189
190         require_once(INSTALLDIR.'/lib/twitterapi.php');
191
192         $act = new TwitterApiAction('/dev/null');
193
194         $arr = $act->twitter_status_array($notice, true);
195         $arr['url'] = $notice->bestUrl();
196         $arr['html'] = htmlspecialchars($notice->rendered);
197         $arr['source'] = htmlspecialchars($arr['source']);
198
199         if (!empty($notice->reply_to)) {
200             $reply_to = Notice::staticGet('id', $notice->reply_to);
201             if (!empty($reply_to)) {
202                 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
203             }
204             $reply_to = null;
205         }
206
207         $profile = $notice->getProfile();
208         $arr['user']['profile_url'] = $profile->profileurl;
209
210         return $arr;
211     }
212
213     function getNoticeTags($notice)
214     {
215         $tags = null;
216
217         $nt = new Notice_tag();
218         $nt->notice_id = $notice->id;
219
220         if ($nt->find()) {
221             $tags = array();
222             while ($nt->fetch()) {
223                 $tags[] = $nt->tag;
224             }
225         }
226
227         $nt->free();
228         $nt = null;
229
230         return $tags;
231     }
232
233     // Push this up to Plugin
234
235     function log($level, $msg)
236     {
237         common_log($level, get_class($this) . ': '.$msg);
238     }
239
240     function _getScripts()
241     {
242         return array('plugins/Realtime/realtimeupdate.js',
243                      'plugins/Realtime/json2.js');
244     }
245
246     function _updateInitialize($timeline, $user_id)
247     {
248         return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
249     }
250
251     function _connect()
252     {
253     }
254
255     function _publish($timeline, $json)
256     {
257     }
258
259     function _disconnect()
260     {
261     }
262
263     function _pathToChannel($path)
264     {
265         return '';
266     }
267
268     function _getTimeline($action)
269     {
270         $path = null;
271         $timeline = null;
272
273         switch ($action->trimmed('action')) {
274          case 'public':
275             $path = array('public');
276             break;
277          case 'tag':
278             $tag = $action->trimmed('tag');
279             if (!empty($tag)) {
280                 $path = array('tag', $tag);
281             }
282             break;
283          default:
284             break;
285         }
286
287         if (!is_null($path)) {
288             $timeline = $this->_pathToChannel($path);
289         }
290
291         return $timeline;
292     }
293 }