]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/RealtimePlugin.php
pass proper args for realtime recipe methods
[quix0rs-gnu-social.git] / plugins / Realtime / RealtimePlugin.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!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  Laconica
42  * @author   Evan Prodromou <evan@controlyourself.ca>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://laconi.ca/
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         $timeline = null;
65
66         switch ($action->trimmed('action')) {
67          case 'public':
68             $timeline = '/timelines/public';
69             break;
70          case 'tag':
71             $tag = $action->trimmed('tag');
72             if (!empty($tag)) {
73                 $timeline = '/timelines/tag/'.$tag;
74             } else {
75                 return true;
76             }
77             break;
78          default:
79             return true;
80         }
81
82         $scripts = $this->_getScripts();
83
84         foreach ($scripts as $script) {
85             $action->element('script', array('type' => 'text/javascript',
86                                              'src' => $script),
87                          ' ');
88         }
89
90         $user = common_current_user();
91
92         if (!empty($user->id)) {
93             $user_id = $user->id;
94         } else {
95             $user_id = 0;
96         }
97
98         $action->elementStart('script', array('type' => 'text/javascript'));
99         $action->raw("$(document).ready(function() { ");
100         $action->raw($this->_updateInitialize($timeline, $user_id));
101         $action->raw(" });");
102         $action->elementEnd('script');
103
104         return true;
105     }
106
107     function onEndNoticeSave($notice)
108     {
109         $timelines = array();
110
111         // XXX: Add other timelines; this is just for the public one
112
113         if ($notice->is_local ||
114             ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
115             $timelines[] = '/timelines/public';
116         }
117
118         $tags = $this->getNoticeTags($notice);
119
120         if (!empty($tags)) {
121             foreach ($tags as $tag) {
122                 $timelines[] = '/timelines/tag/' . $tag;
123             }
124         }
125
126         if (count($timelines) > 0) {
127
128             $json = $this->noticeAsJson($notice);
129
130             $this->_connect();
131
132             foreach ($timelines as $timeline) {
133                 $this->_publish($timeline, $json);
134             }
135
136             $this->_disconnect();
137         }
138
139         return true;
140     }
141
142     function noticeAsJson($notice)
143     {
144         // FIXME: this code should be abstracted to a neutral third
145         // party, like Notice::asJson(). I'm not sure of the ethics
146         // of refactoring from within a plugin, so I'm just abusing
147         // the TwitterApiAction method. Don't do this unless you're me!
148
149         require_once(INSTALLDIR.'/lib/twitterapi.php');
150
151         $act = new TwitterApiAction('/dev/null');
152
153         $arr = $act->twitter_status_array($notice, true);
154         $arr['url'] = $notice->bestUrl();
155         $arr['html'] = htmlspecialchars($notice->rendered);
156         $arr['source'] = htmlspecialchars($arr['source']);
157
158         if (!empty($notice->reply_to)) {
159             $reply_to = Notice::staticGet('id', $notice->reply_to);
160             if (!empty($reply_to)) {
161                 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
162             }
163             $reply_to = null;
164         }
165
166         $profile = $notice->getProfile();
167         $arr['user']['profile_url'] = $profile->profileurl;
168
169         return $arr;
170     }
171
172     function getNoticeTags($notice)
173     {
174         $tags = null;
175
176         $nt = new Notice_tag();
177         $nt->notice_id = $notice->id;
178
179         if ($nt->find()) {
180             $tags = array();
181             while ($nt->fetch()) {
182                 $tags[] = $nt->tag;
183             }
184         }
185
186         $nt->free();
187         $nt = null;
188
189         return $tags;
190     }
191
192     // Push this up to Plugin
193
194     function log($level, $msg)
195     {
196         common_log($level, get_class($this) . ': '.$msg);
197     }
198
199     function _getScripts()
200     {
201         return array(common_local_path('plugins/Realtime/realtimeupdater.js'),
202                      common_local_path('plugins/Realtime/json2.js'));
203     }
204
205     function _updateInitialize($timeline, $user_id)
206     {
207         return '; ';
208     }
209
210     function _connect()
211     {
212     }
213
214     function _publish($timeline, $json)
215     {
216     }
217
218     function _disconnect()
219     {
220     }
221 }