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.
This commit is contained in:
Mikael Nordfeldth 2015-07-10 13:44:47 +02:00
parent f8877e015b
commit 9a92b58057
8 changed files with 88 additions and 70 deletions

View File

@ -563,14 +563,12 @@ abstract class ImPlugin extends Plugin
return true; return true;
} }
function onEndShowHeadElements($action) function onEndShowHeadElements(Action $action)
{ {
$aname = $action->trimmed('action'); if ($action instanceof ShownoticeAction) {
if ($aname == 'shownotice') {
$user_im_prefs = new User_im_prefs(); $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; $user_im_prefs->transport = $this->transport;
if ($user_im_prefs->find() && $user_im_prefs->fetch() && $user_im_prefs->microid && $action->notice->uri) { 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())); 'content' => $id->toString()));
} }
} else if ($aname == 'showstream') { } elseif ($action instanceof ShowstreamAction) {
$user_im_prefs = new User_im_prefs(); $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; $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), $id = new Microid($this->microiduri($user_im_prefs->screenname),
$action->selfUrl()); $action->selfUrl());
$action->element('meta', array('name' => 'microid', $action->element('meta', array('name' => 'microid',

View File

@ -72,6 +72,9 @@ abstract class ProfileAction extends ManagedAction
public function getTarget() public function getTarget()
{ {
if (!$this->target instanceof Profile) {
throw new ServerException('No target profile in ProfileAction class');
}
return $this->target; return $this->target;
} }

View File

@ -32,18 +32,25 @@ if (!defined('GNUSOCIAL')) { exit(1); }
class ProfileListItem extends Widget class ProfileListItem extends Widget
{ {
/** Current profile. */ /** Current profile. */
protected $target = null;
var $profile = null; var $profile = null;
/** Action object using us. */ /** Action object using us. */
var $action = null; var $action = null;
function __construct($profile, $action) function __construct(Profile $target, HTMLOutputter $action)
{ {
parent::__construct($action); parent::__construct($action);
$this->profile = $profile; $this->target = $target;
$this->profile = $this->target;
$this->action = $action; $this->action = $action;
} }
function getTarget()
{
return $this->target;
}
function show() function show()
{ {
if (Event::handle('StartProfileListItem', array($this))) { if (Event::handle('StartProfileListItem', array($this))) {

View File

@ -57,25 +57,27 @@ class GeoURLPlugin extends Plugin
* *
* @return boolean event handler flag * @return boolean event handler flag
*/ */
function onEndShowHeadElements($action) function onEndShowHeadElements(Action $action)
{ {
$name = $action->trimmed('action'); $name = $action->trimmed('action');
$location = null; $location = null;
if ($name == 'showstream') { if ($action instanceof ShowstreamAction) {
$profile = $action->profile; $profile = $action->getTarget();
if (!empty($profile) && !empty($profile->lat) && !empty($profile->lon)) { if (!empty($profile->lat) && !empty($profile->lon)) {
$location = $profile->lat . ', ' . $profile->lon; $location = $profile->lat . ', ' . $profile->lon;
} }
} else if ($name == 'shownotice') { } elseif ($action instanceof ShownoticeAction) {
$notice = $action->profile; // FIXME: getNotice in ShownoticeAction will do a new lookup, we should fix those classes
if (!empty($notice) && !empty($notice->lat) && !empty($notice->lon)) { // 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; $location = $notice->lat . ', ' . $notice->lon;
} }
} }
if (!empty($location)) { if (!is_null($location)) {
$action->element('meta', array('name' => 'ICBM', $action->element('meta', array('name' => 'ICBM',
'content' => $location)); 'content' => $location));
$action->element('meta', array('name' => 'DC.title', $action->element('meta', array('name' => 'DC.title',

View File

@ -101,10 +101,10 @@ class ModLogPlugin extends Plugin
$modlog->profile_id = $profile->id; $modlog->profile_id = $profile->id;
$cur = common_current_user(); $scoped = Profile::current();
if (!empty($cur)) { if ($scoped instanceof Profile) {
$modlog->moderator_id = $cur->id; $modlog->moderator_id = $scoped->getID();
} }
$modlog->role = $role; $modlog->role = $role;
@ -118,21 +118,22 @@ class ModLogPlugin extends Plugin
function onEndShowSections(Action $action) function onEndShowSections(Action $action)
{ {
if ($action->arg('action') != 'showstream') { if (!$action instanceof ShowstreamAction) {
// early return for actions we're not interested in
return true; return true;
} }
$cur = common_current_user(); $scoped = $action->getScoped();
if (!$scoped instanceof Profile || !$scoped->hasRight(self::VIEWMODLOG)) {
if (empty($cur) || !$cur->hasRight(self::VIEWMODLOG)) { // only continue if we are allowed to VIEWMODLOG
return true; return true;
} }
$profile = $action->profile; $profile = $action->getTarget();
$ml = new ModLog(); $ml = new ModLog();
$ml->profile_id = $profile->id; $ml->profile_id = $profile->getID();
$ml->orderBy("created"); $ml->orderBy("created");
$cnt = $ml->find(); $cnt = $ml->find();
@ -152,13 +153,13 @@ class ModLogPlugin extends Plugin
$action->element('td', null, sprintf(($ml->is_grant) ? _('+%s') : _('-%s'), $ml->role)); $action->element('td', null, sprintf(($ml->is_grant) ? _('+%s') : _('-%s'), $ml->role));
$action->elementStart('td'); $action->elementStart('td');
if ($ml->moderator_id) { if ($ml->moderator_id) {
$mod = Profile::getKV('id', $ml->moderator_id); $mod = Profile::getByID($ml->moderator_id);
if (empty($mod)) { if (empty($mod)) {
$action->text(_('[unknown]')); $action->text(_('[unknown]'));
} else { } else {
$action->element('a', array('href' => $mod->profileurl, $action->element('a', array('href' => $mod->getUrl(),
'title' => $mod->fullname), 'title' => $mod->getFullname()),
$mod->nickname); $mod->getNickname());
} }
} else { } else {
$action->text(_('[unknown]')); $action->text(_('[unknown]'));

View File

@ -1063,12 +1063,16 @@ class OStatusPlugin extends Plugin
function showEntityRemoteSubscribe($action, $target='ostatussub') function showEntityRemoteSubscribe($action, $target='ostatussub')
{ {
$user = common_current_user(); if (!$action->getScoped() instanceof Profile) {
if ($user && ($user->id == $action->profile->id)) { // early return if we're not logged in
return true;
}
if ($action->getScoped()->sameAs($action->getTarget())) {
$action->elementStart('div', 'entity_actions'); $action->elementStart('div', 'entity_actions');
$action->elementStart('p', array('id' => 'entity_remote_subscribe', $action->elementStart('p', array('id' => 'entity_remote_subscribe',
'class' => 'entity_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'), 'class' => 'entity_remote_subscribe'),
// TRANS: Link text for link to remote subscribe. // TRANS: Link text for link to remote subscribe.
_m('Remote')); _m('Remote'));
@ -1127,25 +1131,30 @@ class OStatusPlugin extends Plugin
return true; 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()) { 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;
}
$profileUser = User::getKV('id', $item->profile->id); if ($item instanceof ProfileAction) {
if (!empty($profileUser)) {
if ($item instanceof Action) {
$output = $item; $output = $item;
$profile = $item->profile; } elseif ($item instanceof Widget) {
} else {
$output = $item->out; $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 // Add an OStatus subscribe
$output->elementStart('li', 'entity_subscribe'); $output->elementStart('li', 'entity_subscribe');
$url = common_local_url('ostatusinit', $url = common_local_url('ostatusinit',
array('nickname' => $profileUser->nickname)); array('nickname' => $item->getTarget()->getNickname()));
$output->element('a', array('href' => $url, $output->element('a', array('href' => $url,
'class' => 'entity_remote_subscribe'), 'class' => 'entity_remote_subscribe'),
// TRANS: Link text for a user to subscribe to an OStatus user. // TRANS: Link text for a user to subscribe to an OStatus user.
@ -1154,14 +1163,12 @@ class OStatusPlugin extends Plugin
$output->elementStart('li', 'entity_tag'); $output->elementStart('li', 'entity_tag');
$url = common_local_url('ostatustag', $url = common_local_url('ostatustag',
array('nickname' => $profileUser->nickname)); array('nickname' => $item->getTarget()->getNickname()));
$output->element('a', array('href' => $url, $output->element('a', array('href' => $url,
'class' => 'entity_remote_tag'), 'class' => 'entity_remote_tag'),
// TRANS: Link text for a user to list an OStatus user. // TRANS: Link text for a user to list an OStatus user.
_m('List')); _m('List'));
$output->elementEnd('li'); $output->elementEnd('li');
}
}
return true; return true;
} }

View File

@ -390,11 +390,11 @@ class OpenIDPlugin extends Plugin
$action->element('link', array('rel' => 'openid2.provider', $action->element('link', array('rel' => 'openid2.provider',
'href' => common_local_url('openidserver'))); 'href' => common_local_url('openidserver')));
$action->element('link', array('rel' => 'openid2.local_id', $action->element('link', array('rel' => 'openid2.local_id',
'href' => $action->profile->profileurl)); 'href' => $action->getTarget()->getUrl()));
$action->element('link', array('rel' => 'openid.server', $action->element('link', array('rel' => 'openid.server',
'href' => common_local_url('openidserver'))); 'href' => common_local_url('openidserver')));
$action->element('link', array('rel' => 'openid.delegate', $action->element('link', array('rel' => 'openid.delegate',
'href' => $action->profile->profileurl)); 'href' => $action->getTarget()->getUrl()));
} }
if ($action instanceof SitestreamAction) { if ($action instanceof SitestreamAction) {

View File

@ -144,7 +144,7 @@ class WebFingerPlugin extends Plugin
public function onStartShowHTML($action) public function onStartShowHTML($action)
{ {
if ($action instanceof ShowstreamAction) { 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; $url = common_local_url('webfinger') . '?resource='.$acct;
foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) { foreach (array(Discovery::JRD_MIMETYPE, Discovery::XRD_MIMETYPE) as $type) {