| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class RealtimePlugin extends Plugin
- {
- protected $showurl = null;
-
- function onInitializePlugin()
- {
-
- $this->showurl = common_local_url('shownotice',
- array('notice' => '0000000000'));
- return true;
- }
- function onCheckSchema()
- {
- $schema = Schema::get();
- $schema->ensureTable('realtime_channel', Realtime_channel::schemaDef());
- return true;
- }
-
- public function onRouterInitialized(URLMapper $m)
- {
- $m->connect('main/channel/:channelkey/keepalive',
- array('action' => 'keepalivechannel'),
- array('channelkey' => '[a-z0-9]{32}'));
- $m->connect('main/channel/:channelkey/close',
- array('action' => 'closechannel'),
- array('channelkey' => '[a-z0-9]{32}'));
- return true;
- }
- function onEndShowScripts($action)
- {
- $channel = $this->_getChannel($action);
- if (empty($channel)) {
- return true;
- }
- $timeline = $this->_pathToChannel(array($channel->channel_key));
-
-
- if (empty($timeline)) {
- return true;
- }
- $base = $action->selfUrl();
- if (mb_strstr($base, '?')) {
- $url = $base . '&realtime=1';
- } else {
- $url = $base . '?realtime=1';
- }
- $scripts = $this->_getScripts();
- foreach ($scripts as $script) {
- $action->script($script);
- }
- $user = common_current_user();
- if (!empty($user->id)) {
- $user_id = $user->id;
- } else {
- $user_id = 0;
- }
- if ($action->boolean('realtime')) {
- $realtimeUI = ' RealtimeUpdate.initPopupWindow();';
- }
- else {
- $pluginPath = common_path('plugins/Realtime/');
- $keepalive = common_local_url('keepalivechannel', array('channelkey' => $channel->channel_key));
- $close = common_local_url('closechannel', array('channelkey' => $channel->channel_key));
- $realtimeUI = ' RealtimeUpdate.initActions('.json_encode($url).', '.json_encode($timeline).', '.json_encode($pluginPath).', '.json_encode($keepalive).', '.json_encode($close).'); ';
- }
- $script = ' $(document).ready(function() { '.
- $realtimeUI.
- $this->_updateInitialize($timeline, $user_id).
- '}); ';
- $action->inlineScript($script);
- return true;
- }
- public function onEndShowStylesheets(Action $action)
- {
- $urlpath = self::staticPath(str_replace('Plugin','',__CLASS__),
- 'css/realtimeupdate.css');
- $action->cssLink($urlpath, null, 'screen, projection, tv');
- return true;
- }
- public function onHandleQueuedNotice(Notice $notice)
- {
- $paths = array();
-
- try {
- $profile = $notice->getProfile();
- } catch (Exception $e) {
- $this->log(LOG_ERR, $e->getMessage());
- return true;
- }
- try {
- $user = $profile->getUser();
- $paths[] = array('showstream', $user->nickname, null);
- } catch (NoSuchUserException $e) {
-
- $user = null;
- }
-
- $is_local = intval($notice->is_local);
- if ($is_local === Notice::LOCAL_PUBLIC ||
- ($is_local === Notice::REMOTE && !common_config('public', 'localonly'))) {
- $paths[] = array('public', null, null);
- }
-
- $tags = $this->getNoticeTags($notice);
- if (!empty($tags)) {
- foreach ($tags as $tag) {
- $paths[] = array('tag', $tag, null);
- }
- }
-
-
- $ni = $notice->whoGets();
- foreach (array_keys($ni) as $user_id) {
- $user = User::getKV('id', $user_id);
- $paths[] = array('all', $user->nickname, null);
- }
-
- $reply = new Reply();
- $reply->notice_id = $notice->id;
- if ($reply->find()) {
- while ($reply->fetch()) {
- $user = User::getKV('id', $reply->profile_id);
- if (!empty($user)) {
- $paths[] = array('replies', $user->nickname, null);
- }
- }
- }
-
-
- $gi = new Group_inbox();
- $gi->notice_id = $notice->id;
- if ($gi->find()) {
- while ($gi->fetch()) {
- $ug = User_group::getKV('id', $gi->group_id);
- $paths[] = array('showgroup', $ug->nickname, null);
- }
- }
- if (count($paths) > 0) {
- $json = $this->noticeAsJson($notice);
- $this->_connect();
-
-
- foreach ($paths as $path) {
- list($action, $arg1, $arg2) = $path;
- $channels = Realtime_channel::getAllChannels($action, $arg1, $arg2);
- $this->log(LOG_INFO, sprintf(_("%d candidate channels for notice %d"),
- count($channels),
- $notice->id));
- foreach ($channels as $channel) {
-
-
- if (is_null($channel->user_id)) {
- $profile = null;
- } else {
- $profile = Profile::getKV('id', $channel->user_id);
- }
- if ($notice->inScope($profile)) {
- $this->log(LOG_INFO,
- sprintf(_("Delivering notice %d to channel (%s, %s, %s) for user '%s'"),
- $notice->id,
- $channel->action,
- $channel->arg1,
- $channel->arg2,
- ($profile) ? ($profile->nickname) : "<public>"));
- $timeline = $this->_pathToChannel(array($channel->channel_key));
- $this->_publish($timeline, $json);
- }
- }
- }
- $this->_disconnect();
- }
- return true;
- }
- function onStartShowBody($action)
- {
- $realtime = $action->boolean('realtime');
- if (!$realtime) {
- return true;
- }
- $action->elementStart('body',
- (common_current_user()) ? array('id' => $action->trimmed('action'),
- 'class' => 'user_in realtime-popup')
- : array('id' => $action->trimmed('action'),
- 'class'=> 'realtime-popup'));
-
-
- $action->elementStart('address');
- if (common_config('singleuser', 'enabled')) {
- $user = User::singleUser();
- $url = common_local_url('showstream', array('nickname' => $user->nickname));
- } else {
- $url = common_local_url('public');
- }
- $action->element('a', array('class' => 'url',
- 'href' => $url),
- '');
- $action->elementEnd('address');
- $action->showContentBlock();
- $action->showScripts();
- $action->elementEnd('body');
- return false;
- }
- function noticeAsJson($notice)
- {
-
-
-
-
- $act = new ApiAction('/dev/null');
- $arr = $act->twitterStatusArray($notice, true);
- $arr['url'] = $notice->getUrl(true);
- $arr['html'] = htmlspecialchars($notice->getRendered());
- $arr['source'] = htmlspecialchars($arr['source']);
- $arr['conversation_url'] = $notice->getConversationUrl();
- $profile = $notice->getProfile();
- $arr['user']['profile_url'] = $profile->profileurl;
-
- if (!empty($notice->repeat_of)) {
- $original = Notice::getKV('id', $notice->repeat_of);
- if ($original instanceof Notice) {
- $arr['retweeted_status']['url'] = $original->getUrl(true);
- $arr['retweeted_status']['html'] = htmlspecialchars($original->getRendered());
- $arr['retweeted_status']['source'] = htmlspecialchars($original->source);
- $originalProfile = $original->getProfile();
- $arr['retweeted_status']['user']['profile_url'] = $originalProfile->profileurl;
- $arr['retweeted_status']['conversation_url'] = $original->getConversationUrl();
- }
- unset($original);
- }
- return $arr;
- }
- function getNoticeTags($notice)
- {
- $tags = null;
- $nt = new Notice_tag();
- $nt->notice_id = $notice->id;
- if ($nt->find()) {
- $tags = array();
- while ($nt->fetch()) {
- $tags[] = $nt->tag;
- }
- }
- $nt->free();
- $nt = null;
- return $tags;
- }
- function _getScripts()
- {
- $urlpath = self::staticPath(str_replace('Plugin','',__CLASS__),
- 'js/realtimeupdate.js');
- return array($urlpath);
- }
-
- function onEndScriptMessages($action, &$messages)
- {
-
- $messages['realtime_play'] = _m('BUTTON', 'Play');
-
- $messages['realtime_play_tooltip'] = _m('TOOLTIP', 'Play');
-
- $messages['realtime_pause'] = _m('BUTTON', 'Pause');
-
- $messages['realtime_pause_tooltip'] = _m('TOOLTIP', 'Pause');
-
- $messages['realtime_popup'] = _m('BUTTON', 'Pop up');
-
- $messages['realtime_popup_tooltip'] = _m('TOOLTIP', 'Pop up in a window');
- return true;
- }
- function _updateInitialize($timeline, $user_id)
- {
- return "RealtimeUpdate.init($user_id, \"$this->showurl\"); ";
- }
- function _connect()
- {
- }
- function _publish($timeline, $json)
- {
- }
- function _disconnect()
- {
- }
- function _pathToChannel($path)
- {
- return '';
- }
- function _getTimeline($action)
- {
- $channel = $this->_getChannel($action);
- if (empty($channel)) {
- return null;
- }
- return $this->_pathToChannel(array($channel->channel_key));
- }
- function _getChannel($action)
- {
- $timeline = null;
- $arg1 = null;
- $arg2 = null;
- $action_name = $action->trimmed('action');
-
-
-
- switch ($action_name) {
- case 'public':
-
- break;
- case 'tag':
- $tag = $action->trimmed('tag');
- if (!empty($tag)) {
- $arg1 = $tag;
- } else {
- $this->log(LOG_NOTICE, "Unexpected 'tag' action without tag argument");
- return null;
- }
- break;
- case 'showstream':
- case 'all':
- case 'replies':
- case 'showgroup':
- $nickname = common_canonical_nickname($action->trimmed('nickname'));
- if (!empty($nickname)) {
- $arg1 = $nickname;
- } else {
- $this->log(LOG_NOTICE, "Unexpected $action_name action without nickname argument.");
- return null;
- }
- break;
- default:
- return null;
- }
- $user = common_current_user();
- $user_id = (!empty($user)) ? $user->id : null;
- $channel = Realtime_channel::getChannel($user_id,
- $action_name,
- $arg1,
- $arg2);
- return $channel;
- }
- function onStartReadWriteTables(&$alwaysRW, &$rwdb)
- {
- $alwaysRW[] = 'realtime_channel';
- return true;
- }
- }
|