From: Mikael Nordfeldth Date: Fri, 10 Jul 2015 11:44:47 +0000 (+0200) Subject: ShowstreamAction tidying up X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=9a92b58057;p=quix0rs-gnu-social.git ShowstreamAction tidying up Lots of these changes mean that we're requiring certain values to either by typed properly or return the expected value. If it doesn't there should be a fatal exception thrown which we can followup in the logs and won't go silently suppressed. --- diff --git a/lib/implugin.php b/lib/implugin.php index 2da4fa961a..87ca037160 100644 --- a/lib/implugin.php +++ b/lib/implugin.php @@ -563,14 +563,12 @@ abstract class ImPlugin extends Plugin return true; } - function onEndShowHeadElements($action) + function onEndShowHeadElements(Action $action) { - $aname = $action->trimmed('action'); - - if ($aname == 'shownotice') { + if ($action instanceof ShownoticeAction) { $user_im_prefs = new User_im_prefs(); - $user_im_prefs->user_id = $action->profile->id; + $user_im_prefs->user_id = $action->notice->getProfile()->getID(); $user_im_prefs->transport = $this->transport; if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->notice->uri) { @@ -580,13 +578,13 @@ abstract class ImPlugin extends Plugin 'content' => $id->toString())); } - } else if ($aname == 'showstream') { + } elseif ($action instanceof ShowstreamAction) { $user_im_prefs = new User_im_prefs(); - $user_im_prefs->user_id = $action->user->id; + $user_im_prefs->user_id = $action->getTarget()->getID(); $user_im_prefs->transport = $this->transport; - if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->profile->profileurl) { + if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->getTarget()->getUrl()) { $id = new Microid($this->microiduri($user_im_prefs->screenname), $action->selfUrl()); $action->element('meta', array('name' => 'microid', diff --git a/lib/profileaction.php b/lib/profileaction.php index d08446afae..5a5d526e42 100644 --- a/lib/profileaction.php +++ b/lib/profileaction.php @@ -72,6 +72,9 @@ abstract class ProfileAction extends ManagedAction public function getTarget() { + if (!$this->target instanceof Profile) { + throw new ServerException('No target profile in ProfileAction class'); + } return $this->target; } diff --git a/lib/profilelistitem.php b/lib/profilelistitem.php index e0b94ac76a..e21ff04ebe 100644 --- a/lib/profilelistitem.php +++ b/lib/profilelistitem.php @@ -32,18 +32,25 @@ if (!defined('GNUSOCIAL')) { exit(1); } class ProfileListItem extends Widget { /** Current profile. */ + protected $target = null; var $profile = null; /** Action object using us. */ var $action = null; - function __construct($profile, $action) + function __construct(Profile $target, HTMLOutputter $action) { parent::__construct($action); - $this->profile = $profile; + $this->target = $target; + $this->profile = $this->target; $this->action = $action; } + function getTarget() + { + return $this->target; + } + function show() { if (Event::handle('StartProfileListItem', array($this))) { diff --git a/plugins/GeoURL/GeoURLPlugin.php b/plugins/GeoURL/GeoURLPlugin.php index caad8fde11..a8e2546c4f 100644 --- a/plugins/GeoURL/GeoURLPlugin.php +++ b/plugins/GeoURL/GeoURLPlugin.php @@ -57,25 +57,27 @@ class GeoURLPlugin extends Plugin * * @return boolean event handler flag */ - function onEndShowHeadElements($action) + function onEndShowHeadElements(Action $action) { $name = $action->trimmed('action'); $location = null; - if ($name == 'showstream') { - $profile = $action->profile; - if (!empty($profile) && !empty($profile->lat) && !empty($profile->lon)) { + if ($action instanceof ShowstreamAction) { + $profile = $action->getTarget(); + if (!empty($profile->lat) && !empty($profile->lon)) { $location = $profile->lat . ', ' . $profile->lon; } - } else if ($name == 'shownotice') { - $notice = $action->profile; - if (!empty($notice) && !empty($notice->lat) && !empty($notice->lon)) { + } elseif ($action instanceof ShownoticeAction) { + // FIXME: getNotice in ShownoticeAction will do a new lookup, we should fix those classes + // so they can reliably just get a pre-stored notice object which was fetched in Shownotice prepare()... + $notice = $action->notice; + if ($notice instanceof Notice && !empty($notice->lat) && !empty($notice->lon)) { $location = $notice->lat . ', ' . $notice->lon; } } - if (!empty($location)) { + if (!is_null($location)) { $action->element('meta', array('name' => 'ICBM', 'content' => $location)); $action->element('meta', array('name' => 'DC.title', diff --git a/plugins/ModLog/ModLogPlugin.php b/plugins/ModLog/ModLogPlugin.php index 32c96be0e8..d1e01ca849 100644 --- a/plugins/ModLog/ModLogPlugin.php +++ b/plugins/ModLog/ModLogPlugin.php @@ -101,10 +101,10 @@ class ModLogPlugin extends Plugin $modlog->profile_id = $profile->id; - $cur = common_current_user(); + $scoped = Profile::current(); - if (!empty($cur)) { - $modlog->moderator_id = $cur->id; + if ($scoped instanceof Profile) { + $modlog->moderator_id = $scoped->getID(); } $modlog->role = $role; @@ -118,21 +118,22 @@ class ModLogPlugin extends Plugin function onEndShowSections(Action $action) { - if ($action->arg('action') != 'showstream') { + if (!$action instanceof ShowstreamAction) { + // early return for actions we're not interested in return true; } - $cur = common_current_user(); - - if (empty($cur) || !$cur->hasRight(self::VIEWMODLOG)) { + $scoped = $action->getScoped(); + if (!$scoped instanceof Profile || !$scoped->hasRight(self::VIEWMODLOG)) { + // only continue if we are allowed to VIEWMODLOG return true; } - $profile = $action->profile; + $profile = $action->getTarget(); $ml = new ModLog(); - $ml->profile_id = $profile->id; + $ml->profile_id = $profile->getID(); $ml->orderBy("created"); $cnt = $ml->find(); @@ -152,13 +153,13 @@ class ModLogPlugin extends Plugin $action->element('td', null, sprintf(($ml->is_grant) ? _('+%s') : _('-%s'), $ml->role)); $action->elementStart('td'); if ($ml->moderator_id) { - $mod = Profile::getKV('id', $ml->moderator_id); + $mod = Profile::getByID($ml->moderator_id); if (empty($mod)) { $action->text(_('[unknown]')); } else { - $action->element('a', array('href' => $mod->profileurl, - 'title' => $mod->fullname), - $mod->nickname); + $action->element('a', array('href' => $mod->getUrl(), + 'title' => $mod->getFullname()), + $mod->getNickname()); } } else { $action->text(_('[unknown]')); diff --git a/plugins/OStatus/OStatusPlugin.php b/plugins/OStatus/OStatusPlugin.php index 630031fdde..2fd60ad654 100644 --- a/plugins/OStatus/OStatusPlugin.php +++ b/plugins/OStatus/OStatusPlugin.php @@ -1063,12 +1063,16 @@ class OStatusPlugin extends Plugin function showEntityRemoteSubscribe($action, $target='ostatussub') { - $user = common_current_user(); - if ($user && ($user->id == $action->profile->id)) { + if (!$action->getScoped() instanceof Profile) { + // early return if we're not logged in + return true; + } + + if ($action->getScoped()->sameAs($action->getTarget())) { $action->elementStart('div', 'entity_actions'); $action->elementStart('p', array('id' => 'entity_remote_subscribe', 'class' => 'entity_subscribe')); - $action->element('a', array('href' => common_local_url($target), + $action->element('a', array('href' => common_local_url($action->getTarget()), 'class' => 'entity_remote_subscribe'), // TRANS: Link text for link to remote subscribe. _m('Remote')); @@ -1127,42 +1131,45 @@ class OStatusPlugin extends Plugin return true; } - function onStartProfileListItemActionElements($item, $profile=null) + // FIXME: This one can accept both an Action and a Widget. Confusing! Refactor to (HTMLOutputter $out, Profile $target)! + function onStartProfileListItemActionElements($item) { - if (!common_logged_in()) { - - $profileUser = User::getKV('id', $item->profile->id); - - if (!empty($profileUser)) { - - if ($item instanceof Action) { - $output = $item; - $profile = $item->profile; - } else { - $output = $item->out; - } + if (common_logged_in()) { + // only non-logged in users get to see the "remote subscribe" form + return true; + } elseif (!$item->getTarget()->isLocal()) { + // we can (for now) only provide remote subscribe forms for local users + return true; + } - // Add an OStatus subscribe - $output->elementStart('li', 'entity_subscribe'); - $url = common_local_url('ostatusinit', - array('nickname' => $profileUser->nickname)); - $output->element('a', array('href' => $url, - 'class' => 'entity_remote_subscribe'), - // TRANS: Link text for a user to subscribe to an OStatus user. - _m('Subscribe')); - $output->elementEnd('li'); - - $output->elementStart('li', 'entity_tag'); - $url = common_local_url('ostatustag', - array('nickname' => $profileUser->nickname)); - $output->element('a', array('href' => $url, - 'class' => 'entity_remote_tag'), - // TRANS: Link text for a user to list an OStatus user. - _m('List')); - $output->elementEnd('li'); - } + if ($item instanceof ProfileAction) { + $output = $item; + } elseif ($item instanceof Widget) { + $output = $item->out; + } else { + // Bad $item class, don't know how to use this for outputting! + throw new ServerException('Bad item type for onStartProfileListItemActionElements'); } + // Add an OStatus subscribe + $output->elementStart('li', 'entity_subscribe'); + $url = common_local_url('ostatusinit', + array('nickname' => $item->getTarget()->getNickname())); + $output->element('a', array('href' => $url, + 'class' => 'entity_remote_subscribe'), + // TRANS: Link text for a user to subscribe to an OStatus user. + _m('Subscribe')); + $output->elementEnd('li'); + + $output->elementStart('li', 'entity_tag'); + $url = common_local_url('ostatustag', + array('nickname' => $item->getTarget()->getNickname())); + $output->element('a', array('href' => $url, + 'class' => 'entity_remote_tag'), + // TRANS: Link text for a user to list an OStatus user. + _m('List')); + $output->elementEnd('li'); + return true; } diff --git a/plugins/OpenID/OpenIDPlugin.php b/plugins/OpenID/OpenIDPlugin.php index 710a34410a..2e9ada9806 100644 --- a/plugins/OpenID/OpenIDPlugin.php +++ b/plugins/OpenID/OpenIDPlugin.php @@ -390,11 +390,11 @@ class OpenIDPlugin extends Plugin $action->element('link', array('rel' => 'openid2.provider', 'href' => common_local_url('openidserver'))); $action->element('link', array('rel' => 'openid2.local_id', - 'href' => $action->profile->profileurl)); + 'href' => $action->getTarget()->getUrl())); $action->element('link', array('rel' => 'openid.server', 'href' => common_local_url('openidserver'))); $action->element('link', array('rel' => 'openid.delegate', - 'href' => $action->profile->profileurl)); + 'href' => $action->getTarget()->getUrl())); } if ($action instanceof SitestreamAction) { diff --git a/plugins/WebFinger/WebFingerPlugin.php b/plugins/WebFinger/WebFingerPlugin.php index 91dc9b53f7..c2a9c69d0c 100644 --- a/plugins/WebFinger/WebFingerPlugin.php +++ b/plugins/WebFinger/WebFingerPlugin.php @@ -144,7 +144,7 @@ class WebFingerPlugin extends Plugin public function onStartShowHTML($action) { if ($action instanceof ShowstreamAction) { - $acct = 'acct:'. $action->profile->nickname .'@'. common_config('site', 'server'); + $acct = 'acct:'. $action->getTarget()->getNickname() .'@'. common_config('site', 'server'); $url = common_local_url('webfinger') . '?resource='.$acct; foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {