TagProfileAction and OStatusPlugin now use less redundant code

This commit is contained in:
Mikael Nordfeldth 2015-03-08 20:10:20 +01:00
parent 32dacb9369
commit 55894f02c7
3 changed files with 16 additions and 81 deletions

View File

@ -19,7 +19,6 @@
if (!defined('GNUSOCIAL')) { exit(1); } if (!defined('GNUSOCIAL')) { exit(1); }
require_once INSTALLDIR . '/lib/settingsaction.php';
require_once INSTALLDIR . '/lib/peopletags.php'; require_once INSTALLDIR . '/lib/peopletags.php';
class TagprofileAction extends FormAction class TagprofileAction extends FormAction
@ -29,36 +28,32 @@ class TagprofileAction extends FormAction
protected $target = null; protected $target = null;
protected $form = 'TagProfile'; protected $form = 'TagProfile';
protected function prepare(array $args=array()) protected function doPreparation()
{ {
parent::prepare($args);
$id = $this->trimmed('id'); $id = $this->trimmed('id');
if (!$id) { $uri = $this->trimmed('uri');
$this->target = null; if (!empty($id)) {
} else {
$this->target = Profile::getKV('id', $id); $this->target = Profile::getKV('id', $id);
if (!$this->target instanceof Profile) { if (!$this->target instanceof Profile) {
// TRANS: Client error displayed when referring to non-existing profile ID. // TRANS: Client error displayed when referring to non-existing profile ID.
$this->clientError(_('No profile with that ID.')); $this->clientError(_('No profile with that ID.'));
} }
} elseif (!empty($uri)) {
$this->target = Profile::fromUri($uri);
} else {
// TRANS: Client error displayed when trying to tag a user but no ID or profile is provided.
$this->clientError(_('No profile identifier provided.'));
} }
if ($this->target instanceof Profile && !$this->scoped->canTag($this->target)) { if (!$this->scoped->canTag($this->target)) {
// TRANS: Client error displayed when trying to tag a user that cannot be tagged. // TRANS: Client error displayed when trying to tag a user that cannot be tagged.
$this->clientError(_('You cannot tag this user.')); $this->clientError(_('You cannot tag this user.'));
} }
return true; $this->formOpts = $this->target;
}
protected function handle() return true;
{
if (Event::handle('StartTagProfileAction', array($this, $this->target))) {
parent::handle();
Event::handle('EndTagProfileAction', array($this, $this->target));
}
} }
function title() function title()
@ -115,17 +110,8 @@ class TagprofileAction extends FormAction
} }
} }
protected function getForm() protected function doPost()
{ {
$class = $this->form.'Form';
$form = new $class($this, $this->target);
return $form;
}
protected function handlePost()
{
parent::handlePost(); // Does nothing for now
$tagstring = $this->trimmed('tags'); $tagstring = $this->trimmed('tags');
$token = $this->trimmed('token'); $token = $this->trimmed('token');
@ -144,22 +130,16 @@ class TagprofileAction extends FormAction
if (!common_valid_profile_tag($tag)) { if (!common_valid_profile_tag($tag)) {
// TRANS: Form validation error displayed if a given tag is invalid. // TRANS: Form validation error displayed if a given tag is invalid.
// TRANS: %s is the invalid tag. // TRANS: %s is the invalid tag.
$this->showForm(sprintf(_('Invalid tag: "%s".'), $tag)); throw new ClientException(sprintf(_('Invalid tag: "%s".'), $tag));
return;
} }
$tag_priv[$tag] = $private; $tag_priv[$tag] = $private;
} }
} }
try { $result = Profile_tag::setTags($this->scoped->getID(), $this->target->getID(), $tags, $tag_priv);
$result = Profile_tag::setTags($this->scoped->id, $this->target->id, $tags, $tag_priv); if (!$result) {
if (!$result) { throw new ServerException('The tags could not be saved.');
throw new Exception('The tags could not be saved.');
}
} catch (Exception $e) {
$this->showForm($e->getMessage());
return false;
} }
if ($this->boolean('ajax')) { if ($this->boolean('ajax')) {
@ -188,17 +168,4 @@ class TagprofileAction extends FormAction
Event::handle('EndSavePeopletags', array($this, $tagstring)); Event::handle('EndSavePeopletags', array($this, $tagstring));
} }
} }
function showPageNotice()
{
if ($this->error) {
$this->element('p', 'error', $this->error);
} else {
$this->elementStart('div', 'instructions');
$this->element('p', null,
// TRANS: Page notice.
_('Use this form to add your subscribers or subscriptions to lists.'));
$this->elementEnd('div');
}
}
} }

View File

@ -233,37 +233,6 @@ class OStatusPlugin extends Plugin
return true; return true;
} }
function onStartTagProfileAction($action, $profile)
{
$err = null;
$uri = $action->trimmed('uri');
if (!$profile && $uri) {
try {
if (Validate::email($uri)) {
$oprofile = Ostatus_profile::ensureWebfinger($uri);
} else if (Validate::uri($uri)) {
$oprofile = Ostatus_profile::ensureProfileURL($uri);
} else {
// TRANS: Exception in OStatus when invalid URI was entered.
throw new Exception(_m('Invalid URI.'));
}
// redirect to the new profile.
common_redirect(common_local_url('tagprofile', array('id' => $oprofile->profile_id)), 303);
} catch (Exception $e) {
// TRANS: Error message in OStatus plugin. Do not translate the domain names example.com
// TRANS: and example.net, as these are official standard domain names for use in examples.
$err = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
}
$action->showForm($err);
return false;
}
return true;
}
/* /*
* If the field being looked for is URI look for the profile * If the field being looked for is URI look for the profile
*/ */

View File

@ -2147,7 +2147,6 @@ class Ostatus_profile extends Managed_DataObject
throw new ServerException(sprintf(_m('Unrecognized URI protocol for profile: %1$s (%2$s).'), throw new ServerException(sprintf(_m('Unrecognized URI protocol for profile: %1$s (%2$s).'),
$protocol, $protocol,
$uri)); $uri));
break;
} }
} else { } else {
// TRANS: Server exception. %s is a URI. // TRANS: Server exception. %s is a URI.