]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Comet/CometPlugin.php
Merge branch '0.8.x' of git@gitorious.org:laconica/dev into 0.8.x
[quix0rs-gnu-social.git] / plugins / Comet / CometPlugin.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 CometPlugin extends Plugin
45 {
46     var $server = null;
47
48     function __construct($server=null, $username=null, $password=null)
49     {
50         $this->server   = $server;
51         $this->username = $username;
52         $this->password = $password;
53
54         parent::__construct();
55     }
56
57     function onEndShowScripts($action)
58     {
59         $timeline = null;
60
61         $this->log(LOG_DEBUG, 'got action ' . $action->trimmed('action'));
62
63         switch ($action->trimmed('action')) {
64          case 'public':
65             $timeline = '/timelines/public';
66             break;
67          case 'tag':
68             $tag = $action->trimmed('tag');
69             if (!empty($tag)) {
70                 $timeline = '/timelines/tag/'.$tag;
71             } else {
72                 return true;
73             }
74             break;
75          default:
76             return true;
77         }
78
79         $scripts = array('jquery.comet.js', 'json2.js', 'updatetimeline.js');
80
81         foreach ($scripts as $script) {
82             $action->element('script', array('type' => 'text/javascript',
83                                              'src' => common_path('plugins/Comet/'.$script)),
84                          ' ');
85         }
86
87         $user = common_current_user();
88
89         if (!empty($user->id)) {
90             $user_id = $user->id;
91         } else {
92             $user_id = 0;
93         }
94
95         $replyurl = common_local_url('newnotice');
96         $favorurl = common_local_url('favor');
97         // FIXME: need to find a better way to pass this pattern in
98         $deleteurl = common_local_url('deletenotice',
99                                       array('notice' => '0000000000'));
100
101         $action->elementStart('script', array('type' => 'text/javascript'));
102         $action->raw("$(document).ready(function() { updater.init(\"$this->server\", \"$timeline\", $user_id, \"$replyurl\", \"$favorurl\", \"$deleteurl\"); });");
103         $action->elementEnd('script');
104
105         return true;
106     }
107
108     function onEndNoticeSave($notice)
109     {
110         $this->log(LOG_INFO, "Called for save notice.");
111
112         $timelines = array();
113
114         // XXX: Add other timelines; this is just for the public one
115
116         if ($notice->is_local ||
117             ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
118             $timelines[] = '/timelines/public';
119         }
120
121         $tags = $this->getNoticeTags($notice);
122
123         if (!empty($tags)) {
124             foreach ($tags as $tag) {
125                 $timelines[] = '/timelines/tag/' . $tag;
126             }
127         }
128
129         if (count($timelines) > 0) {
130             // Require this, since we need it
131             require_once(INSTALLDIR.'/plugins/Comet/bayeux.class.inc.php');
132
133             $json = $this->noticeAsJson($notice);
134
135             // Bayeux? Comet? Huh? These terms confuse me
136             $bay = new Bayeux($this->server, $this->user, $this->password);
137
138             foreach ($timelines as $timeline) {
139                 $this->log(LOG_INFO, "Posting notice $notice->id to '$timeline'.");
140                 $bay->publish($timeline, $json);
141             }
142
143             $bay = NULL;
144         }
145
146         return true;
147     }
148
149     function noticeAsJson($notice)
150     {
151         // FIXME: this code should be abstracted to a neutral third
152         // party, like Notice::asJson(). I'm not sure of the ethics
153         // of refactoring from within a plugin, so I'm just abusing
154         // the TwitterApiAction method. Don't do this unless you're me!
155
156         require_once(INSTALLDIR.'/lib/twitterapi.php');
157
158         $act = new TwitterApiAction('/dev/null');
159
160         $arr = $act->twitter_status_array($notice, true);
161         $arr['url'] = $notice->bestUrl();
162         $arr['html'] = htmlspecialchars($notice->rendered);
163         $arr['source'] = htmlspecialchars($arr['source']);
164
165         if (!empty($notice->reply_to)) {
166             $reply_to = Notice::staticGet('id', $notice->reply_to);
167             if (!empty($reply_to)) {
168                 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
169             }
170             $reply_to = null;
171         }
172
173         $profile = $notice->getProfile();
174         $arr['user']['profile_url'] = $profile->profileurl;
175
176         return $arr;
177     }
178
179     function getNoticeTags($notice)
180     {
181         $tags = null;
182
183         $nt = new Notice_tag();
184         $nt->notice_id = $notice->id;
185
186         if ($nt->find()) {
187             $tags = array();
188             while ($nt->fetch()) {
189                 $tags[] = $nt->tag;
190             }
191         }
192
193         $nt->free();
194         $nt = null;
195
196         return $tags;
197     }
198
199     // Push this up to Plugin
200
201     function log($level, $msg)
202     {
203         common_log($level, get_class($this) . ': '.$msg);
204     }
205 }