]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/RealtimePlugin.php
Merge branch '0.8.x' of git@gitorious.org:statusnet/mainline into 0.8.x
[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     function onInitializePlugin()
54     {
55         $this->replyurl = common_local_url('newnotice');
56         $this->favorurl = common_local_url('favor');
57         // FIXME: need to find a better way to pass this pattern in
58         $this->deleteurl = common_local_url('deletenotice',
59                                             array('notice' => '0000000000'));
60     }
61
62     function onEndShowScripts($action)
63     {
64         $path = null;
65
66         $a = $action->trimmed('action');
67
68         switch ($a) {
69             case 'public': case 'all': case 'replies': case 'showstream':
70                 $path = array($a);
71                 break;
72             case 'tag':
73                 $tag = $action->trimmed('tag');
74                 if (!empty($tag)) {
75                     $path = array('tag', $tag);
76                 } else {
77                     return true;
78                 }
79                 break;
80              default:
81                 return true;
82         }
83
84         $timeline = $this->_pathToChannel($path);
85
86         $scripts = $this->_getScripts();
87
88         foreach ($scripts as $script) {
89             $action->script($script);
90         }
91
92         $user = common_current_user();
93
94         if (!empty($user->id)) {
95             $user_id = $user->id;
96         } else {
97             $user_id = 0;
98         }
99
100         $action->script('plugins/Realtime/jquery.getUrlParam.js');
101
102         $action->elementStart('script', array('type' => 'text/javascript'));
103         $action->raw('
104         <!--
105         $(document).ready(function() {
106             ' . $this->_updateInitialize($timeline, $user_id) . '
107         });
108         -->
109         ');
110         $action->elementEnd('script');
111
112         return true;
113     }
114
115     function onEndNoticeSave($notice)
116     {
117         $paths = array();
118
119         // TODO: Replies timeline
120
121         if ($notice->is_local ||
122             ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
123             foreach (array('public', 'all', 'replies', 'showstream') as $a) {
124                 $paths[] = array($a);
125             }
126         }
127
128         $tags = $this->getNoticeTags($notice);
129
130         if (!empty($tags)) {
131             foreach ($tags as $tag) {
132                 $paths[] = array('tag', $tag);
133             }
134         }
135
136         if (count($paths) > 0) {
137
138             $json = $this->noticeAsJson($notice);
139
140             $this->_connect();
141
142             foreach ($paths as $path) {
143                 $timeline = $this->_pathToChannel($path);
144                 $this->_publish($timeline, $json);
145             }
146
147             $this->_disconnect();
148         }
149
150         return true;
151     }
152
153     function noticeAsJson($notice)
154     {
155         // FIXME: this code should be abstracted to a neutral third
156         // party, like Notice::asJson(). I'm not sure of the ethics
157         // of refactoring from within a plugin, so I'm just abusing
158         // the TwitterApiAction method. Don't do this unless you're me!
159
160         require_once(INSTALLDIR.'/lib/twitterapi.php');
161
162         $act = new TwitterApiAction('/dev/null');
163
164         $arr = $act->twitter_status_array($notice, true);
165         $arr['url'] = $notice->bestUrl();
166         $arr['html'] = htmlspecialchars($notice->rendered);
167         $arr['source'] = htmlspecialchars($arr['source']);
168
169         if (!empty($notice->reply_to)) {
170             $reply_to = Notice::staticGet('id', $notice->reply_to);
171             if (!empty($reply_to)) {
172                 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
173             }
174             $reply_to = null;
175         }
176
177         $profile = $notice->getProfile();
178         $arr['user']['profile_url'] = $profile->profileurl;
179
180         return $arr;
181     }
182
183     function getNoticeTags($notice)
184     {
185         $tags = null;
186
187         $nt = new Notice_tag();
188         $nt->notice_id = $notice->id;
189
190         if ($nt->find()) {
191             $tags = array();
192             while ($nt->fetch()) {
193                 $tags[] = $nt->tag;
194             }
195         }
196
197         $nt->free();
198         $nt = null;
199
200         return $tags;
201     }
202
203     // Push this up to Plugin
204
205     function log($level, $msg)
206     {
207         common_log($level, get_class($this) . ': '.$msg);
208     }
209
210     function _getScripts()
211     {
212         return array('plugins/Realtime/realtimeupdate.js',
213                      'plugins/Realtime/json2.js');
214     }
215
216     function _updateInitialize($timeline, $user_id)
217     {
218         return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
219     }
220
221     function _connect()
222     {
223     }
224
225     function _publish($timeline, $json)
226     {
227     }
228
229     function _disconnect()
230     {
231     }
232
233     function _pathToChannel($path)
234     {
235         return '';
236     }
237 }