]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Meteor/MeteorPlugin.php
291c4e3a2a9c29d78fe5525a0e7bd15148759248
[quix0rs-gnu-social.git] / plugins / Meteor / MeteorPlugin.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Plugin to do "real time" updates using Comet/Bayeux
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  * Plugin to do realtime updates using Comet
36  *
37  * @category Plugin
38  * @package  Laconica
39  * @author   Evan Prodromou <evan@controlyourself.ca>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://laconi.ca/
42  */
43
44 class MeteorPlugin extends Plugin
45 {
46     var $webserver     = null;
47     var $webport       = null;
48     var $controlport   = null;
49     var $controlserver = null;
50     var $channelbase   = null;
51     var $_socket       = null;
52
53     function __construct($webserver=null, $webport=4670, $controlport=4671, $controlserver=null, $channelbase='')
54     {
55         global $config;
56
57         $this->webserver     = (empty($webserver)) ? $config['site']['server'] : $webserver;
58         $this->webport       = $webport;
59         $this->controlport   = $controlport;
60         $this->controlserver = (empty($controlserver)) ? $webserver : $controlserver;
61         $this->channelbase   = $channelbase;
62
63         parent::__construct();
64     }
65
66     function onEndShowScripts($action)
67     {
68         $timeline = null;
69
70         $this->log(LOG_DEBUG, 'got action ' . $action->trimmed('action'));
71
72         switch ($action->trimmed('action')) {
73          case 'public':
74             $timeline = '/timelines/public';
75             break;
76          case 'tag':
77             $tag = $action->trimmed('tag');
78             if (!empty($tag)) {
79                 $timeline = '/timelines/tag/'.$tag;
80             } else {
81                 return true;
82             }
83             break;
84          default:
85             return true;
86         }
87
88         $action->element('script', array('type' => 'text/javascript',
89                                          'src' => 'http://'.$this->webserver.(($this->webport == 80) ? '':':'.$this->webport).'/meteor.js'),
90                          ' ');
91
92         foreach (array('meteorupdater.js', 'json2.js') as $script) {
93             $action->element('script', array('type' => 'text/javascript',
94                                              'src' => common_path('plugins/Meteor/'.$script)),
95                          ' ');
96         }
97
98         $user = common_current_user();
99
100         if (!empty($user->id)) {
101             $user_id = $user->id;
102         } else {
103             $user_id = 0;
104         }
105
106         $replyurl = common_local_url('newnotice');
107         $favorurl = common_local_url('favor');
108         // FIXME: need to find a better way to pass this pattern in
109         $deleteurl = common_local_url('deletenotice',
110                                       array('notice' => '0000000000'));
111
112         $action->elementStart('script', array('type' => 'text/javascript'));
113         $action->raw("$(document).ready(function() { MeteorUpdater.init(\"$this->webserver\", $this->webport, \"{$this->channelbase}$timeline\", $user_id, \"$replyurl\", \"$favorurl\", \"$deleteurl\"); });");
114         $action->elementEnd('script');
115
116         return true;
117     }
118
119     function onEndNoticeSave($notice)
120     {
121         $this->log(LOG_INFO, "Called for save notice.");
122
123         $timelines = array();
124
125         // XXX: Add other timelines; this is just for the public one
126
127         if ($notice->is_local ||
128             ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
129             $timelines[] = '/timelines/public';
130         }
131
132         $tags = $this->getNoticeTags($notice);
133
134         if (!empty($tags)) {
135             foreach ($tags as $tag) {
136                 $timelines[] = '/timelines/tag/' . $tag;
137             }
138         }
139
140         if (count($timelines) > 0) {
141
142             $json = json_encode($this->noticeAsJson($notice));
143
144             $this->log(LOG_DEBUG, $json);
145
146             $this->_connect();
147
148             foreach ($timelines as $timeline) {
149                 $this->_addMessage($timeline, $json);
150             }
151
152             $this->_disconnect();
153         }
154
155         return true;
156     }
157
158     function _connect()
159     {
160         // May throw an exception.
161         $this->_socket = @stream_socket_client("tcp://{$this->controlserver}:{$this->controlport}",
162                                                $errno, $errstr, $timeout, $conflag);
163         if (!$this->_socket) {
164             throw new Exception("Couldn't connect to {$this->controlserver} on {$this->controlport}");
165         }
166     }
167
168     function _addMessage($channel, $message)
169     {
170         $cmd = "ADDMESSAGE {$this->channelbase}$channel $message\n";
171         $this->log(LOG_DEBUG, $cmd);
172         @fwrite($this->_socket, "COMMAND: $cmd");
173         $result = fgets($this->_socket);
174         $this->log(LOG_DEBUG, "RESULT: $result");
175         // TODO: parse and deal with result
176     }
177
178     function _disconnect()
179     {
180         @fclose($this->_socket);
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 }