Separate the UI paths for ostatussub and ostatusgroup. They'll redirect to each other transparently if they find you've put a remote entity of the other type.

This commit is contained in:
Brion Vibber 2010-03-03 13:40:26 -08:00
parent c04c8ae59a
commit a42d1116db
3 changed files with 224 additions and 103 deletions

View File

@ -51,8 +51,8 @@ class OStatusPlugin extends Plugin
array('action' => 'ostatusinit'), array('nickname' => '[A-Za-z0-9_-]+')); array('action' => 'ostatusinit'), array('nickname' => '[A-Za-z0-9_-]+'));
$m->connect('main/ostatussub', $m->connect('main/ostatussub',
array('action' => 'ostatussub')); array('action' => 'ostatussub'));
$m->connect('main/ostatussub', $m->connect('main/ostatusgroup',
array('action' => 'ostatussub'), array('feed' => '[A-Za-z0-9\.\/\:]+')); array('action' => 'ostatusgroup'));
// PuSH actions // PuSH actions
$m->connect('main/push/hub', array('action' => 'pushhub')); $m->connect('main/push/hub', array('action' => 'pushhub'));
@ -731,7 +731,7 @@ class OStatusPlugin extends Plugin
function onStartShowUserGroupsContent($action) function onStartShowUserGroupsContent($action)
{ {
$this->showEntityRemoteSubscribe($action); $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
return true; return true;
} }
@ -745,19 +745,19 @@ class OStatusPlugin extends Plugin
function onEndShowGroupsMiniList($action) function onEndShowGroupsMiniList($action)
{ {
$this->showEntityRemoteSubscribe($action); $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
return true; return true;
} }
function showEntityRemoteSubscribe($action) function showEntityRemoteSubscribe($action, $target='ostatussub')
{ {
$user = common_current_user(); $user = common_current_user();
if ($user && ($user->id == $action->profile->id)) { if ($user && ($user->id == $action->profile->id)) {
$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('ostatussub'), $action->element('a', array('href' => common_local_url($target),
'class' => 'entity_remote_subscribe') 'class' => 'entity_remote_subscribe')
, _m('Remote')); , _m('Remote'));
$action->elementEnd('p'); $action->elementEnd('p');

View File

@ -0,0 +1,181 @@
<?php
/*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2009-2010, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @package OStatusPlugin
* @maintainer Brion Vibber <brion@status.net>
*/
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
/**
* Key UI methods:
*
* showInputForm() - form asking for a remote profile account or URL
* We end up back here on errors
*
* showPreviewForm() - surrounding form for preview-and-confirm
* preview() - display profile for a remote group
*
* success() - redirects to groups page on join
*/
class OStatusGroupAction extends OStatusSubAction
{
protected $profile_uri; // provided acct: or URI of remote entity
protected $oprofile; // Ostatus_profile of remote entity, if valid
function validateRemoteProfile()
{
if (!$this->oprofile->isGroup()) {
// Send us to the user subscription form for conf
$target = common_local_url('ostatussub', array(), array('profile' => $this->profile_uri));
common_redirect($target, 303);
}
}
/**
* Show the initial form, when we haven't yet been given a valid
* remote profile.
*/
function showInputForm()
{
$user = common_current_user();
$profile = $user->getProfile();
$this->elementStart('form', array('method' => 'post',
'id' => 'form_ostatus_sub',
'class' => 'form_settings',
'action' => $this->selfLink()));
$this->hidden('token', common_session_token());
$this->elementStart('fieldset', array('id' => 'settings_feeds'));
$this->elementStart('ul', 'form_data');
$this->elementStart('li');
$this->input('profile',
_m('Group profile URL'),
$this->profile_uri,
_m('Enter the profile URL of a group on another StatusNet site'));
$this->elementEnd('li');
$this->elementEnd('ul');
$this->submit('validate', _m('Continue'));
$this->elementEnd('fieldset');
$this->elementEnd('form');
}
/**
* Show a preview for a remote group's profile
* @return boolean true if we're ok to try joining
*/
function preview()
{
$oprofile = $this->oprofile;
$group = $oprofile->localGroup();
$cur = common_current_user();
if ($cur->isMember($group)) {
$this->element('div', array('class' => 'error'),
_m("You are already a member of this group."));
$ok = false;
} else {
$ok = true;
}
$this->showEntity($group,
$group->getProfileUrl(),
$group->homepage_logo,
$group->description);
return $ok;
}
/**
* Redirect on successful remote group join
*/
function success()
{
$cur = common_current_user();
$url = common_local_url('usergroups', array('nickname' => $cur->nickname));
common_redirect($url, 303);
}
/**
* Attempt to finalize subscription.
* validateFeed must have been run first.
*
* Calls showForm on failure or success on success.
*/
function saveFeed()
{
$user = common_current_user();
$group = $this->oprofile->localGroup();
if ($user->isMember($group)) {
// TRANS: OStatus remote group subscription dialog error.
$this->showForm(_m('Already a member!'));
return;
}
if (Event::handle('StartJoinGroup', array($group, $user))) {
$ok = Group_member::join($this->oprofile->group_id, $user->id);
if ($ok) {
Event::handle('EndJoinGroup', array($group, $user));
$this->success();
} else {
// TRANS: OStatus remote group subscription dialog error.
$this->showForm(_m('Remote group join failed!'));
}
} else {
// TRANS: OStatus remote group subscription dialog error.
$this->showForm(_m('Remote group join aborted!'));
}
}
/**
* Title of the page
*
* @return string Title of the page
*/
function title()
{
// TRANS: Page title for OStatus remote group join form
return _m('Confirm joining remote group');
}
/**
* Instructions for use
*
* @return instructions for use
*/
function getInstructions()
{
return _m('You can subscribe to groups from other supported sites. Paste the group\'s profile URI below:');
}
function selfLink()
{
return common_local_url('ostatusgroup');
}
}

View File

@ -1,7 +1,7 @@
<?php <?php
/* /*
* StatusNet - the distributed open-source microblogging tool * StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2009, StatusNet, Inc. * Copyright (C) 2009-2010, StatusNet, Inc.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by * it under the terms of the GNU Affero General Public License as published by
@ -31,11 +31,9 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
* We end up back here on errors * We end up back here on errors
* *
* showPreviewForm() - surrounding form for preview-and-confirm * showPreviewForm() - surrounding form for preview-and-confirm
* previewUser() - display profile for a remote user * preview() - display profile for a remote user
* previewGroup() - display profile for a remote group
* *
* successUser() - redirects to subscriptions page on subscribe * success() - redirects to subscriptions page on subscribe
* successGroup() - redirects to groups page on join
*/ */
class OStatusSubAction extends Action class OStatusSubAction extends Action
{ {
@ -55,8 +53,7 @@ class OStatusSubAction extends Action
$this->elementStart('form', array('method' => 'post', $this->elementStart('form', array('method' => 'post',
'id' => 'form_ostatus_sub', 'id' => 'form_ostatus_sub',
'class' => 'form_settings', 'class' => 'form_settings',
'action' => 'action' => $this->selfLink()));
common_local_url('ostatussub')));
$this->hidden('token', common_session_token()); $this->hidden('token', common_session_token());
@ -87,11 +84,7 @@ class OStatusSubAction extends Action
*/ */
function showPreviewForm() function showPreviewForm()
{ {
if ($this->oprofile->isGroup()) { $ok = $this->preview();
$ok = $this->previewGroup();
} else {
$ok = $this->previewUser();
}
if (!$ok) { if (!$ok) {
// @fixme maybe provide a cancel button or link back? // @fixme maybe provide a cancel button or link back?
return; return;
@ -104,7 +97,7 @@ class OStatusSubAction extends Action
'id' => 'form_ostatus_sub', 'id' => 'form_ostatus_sub',
'class' => 'form_remote_authorize', 'class' => 'form_remote_authorize',
'action' => 'action' =>
common_local_url('ostatussub'))); $this->selfLink()));
$this->elementStart('fieldset'); $this->elementStart('fieldset');
$this->hidden('token', common_session_token()); $this->hidden('token', common_session_token());
$this->hidden('profile', $this->profile_uri); $this->hidden('profile', $this->profile_uri);
@ -126,7 +119,7 @@ class OStatusSubAction extends Action
* Show a preview for a remote user's profile * Show a preview for a remote user's profile
* @return boolean true if we're ok to try subscribing * @return boolean true if we're ok to try subscribing
*/ */
function previewUser() function preview()
{ {
$oprofile = $this->oprofile; $oprofile = $this->oprofile;
$profile = $oprofile->localProfile(); $profile = $oprofile->localProfile();
@ -150,32 +143,6 @@ class OStatusSubAction extends Action
return $ok; return $ok;
} }
/**
* Show a preview for a remote group's profile
* @return boolean true if we're ok to try joining
*/
function previewGroup()
{
$oprofile = $this->oprofile;
$group = $oprofile->localGroup();
$cur = common_current_user();
if ($cur->isMember($group)) {
$this->element('div', array('class' => 'error'),
_m("You are already a member of this group."));
$ok = false;
} else {
$ok = true;
}
$this->showEntity($group,
$group->getProfileUrl(),
$group->homepage_logo,
$group->description);
return $ok;
}
function showEntity($entity, $profile, $avatar, $note) function showEntity($entity, $profile, $avatar, $note)
{ {
$nickname = $entity->nickname; $nickname = $entity->nickname;
@ -254,23 +221,13 @@ class OStatusSubAction extends Action
/** /**
* Redirect on successful remote user subscription * Redirect on successful remote user subscription
*/ */
function successUser() function success()
{ {
$cur = common_current_user(); $cur = common_current_user();
$url = common_local_url('subscriptions', array('nickname' => $cur->nickname)); $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
common_redirect($url, 303); common_redirect($url, 303);
} }
/**
* Redirect on successful remote group join
*/
function successGroup()
{
$cur = common_current_user();
$url = common_local_url('usergroups', array('nickname' => $cur->nickname));
common_redirect($url, 303);
}
/** /**
* Pull data for a remote profile and check if it's valid. * Pull data for a remote profile and check if it's valid.
* Fills out error UI string in $this->error * Fills out error UI string in $this->error
@ -278,16 +235,9 @@ class OStatusSubAction extends Action
* *
* @return boolean * @return boolean
*/ */
function validateFeed() function pullRemoteProfile()
{ {
$profile_uri = trim($this->arg('profile')); $this->profile_uri = $this->trimmed('profile');
if ($profile_uri == '') {
$this->showForm(_m('Empty remote profile URL!'));
return;
}
$this->profile_uri = $profile_uri;
try { try {
if (Validate::email($this->profile_uri)) { if (Validate::email($this->profile_uri)) {
$this->oprofile = Ostatus_profile::ensureWebfinger($this->profile_uri); $this->oprofile = Ostatus_profile::ensureWebfinger($this->profile_uri);
@ -318,50 +268,36 @@ class OStatusSubAction extends Action
return false; return false;
} }
function validateRemoteProfile()
{
if ($this->oprofile->isGroup()) {
// Send us to the group subscription form for conf
$target = common_local_url('ostatusgroup', array(), array('profile' => $this->profile_uri));
common_redirect($target, 303);
}
}
/** /**
* Attempt to finalize subscription. * Attempt to finalize subscription.
* validateFeed must have been run first. * validateFeed must have been run first.
* *
* Calls showForm on failure or successUser/successGroup on success. * Calls showForm on failure or success on success.
*/ */
function saveFeed() function saveFeed()
{ {
// And subscribe the current user to the local profile // And subscribe the current user to the local profile
$user = common_current_user(); $user = common_current_user();
if ($this->oprofile->isGroup()) {
$group = $this->oprofile->localGroup();
if ($user->isMember($group)) {
// TRANS: OStatus remote group subscription dialog error.
$this->showForm(_m('Already a member!'));
return;
}
if (Event::handle('StartJoinGroup', array($group, $user))) {
$ok = Group_member::join($this->oprofile->group_id, $user->id);
if ($ok) {
Event::handle('EndJoinGroup', array($group, $user));
$this->successGroup();
} else {
// TRANS: OStatus remote group subscription dialog error.
$this->showForm(_m('Remote group join failed!'));
}
} else {
// TRANS: OStatus remote group subscription dialog error.
$this->showForm(_m('Remote group join aborted!'));
}
} else {
$local = $this->oprofile->localProfile(); $local = $this->oprofile->localProfile();
if ($user->isSubscribed($local)) { if ($user->isSubscribed($local)) {
// TRANS: OStatus remote subscription dialog error. // TRANS: OStatus remote subscription dialog error.
$this->showForm(_m('Already subscribed!')); $this->showForm(_m('Already subscribed!'));
} elseif ($this->oprofile->subscribeLocalToRemote($user)) { } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
$this->successUser(); $this->success();
} else { } else {
// TRANS: OStatus remote subscription dialog error. // TRANS: OStatus remote subscription dialog error.
$this->showForm(_m('Remote subscription failed!')); $this->showForm(_m('Remote subscription failed!'));
} }
} }
}
function prepare($args) function prepare($args)
{ {
@ -376,7 +312,9 @@ class OStatusSubAction extends Action
return false; return false;
} }
$this->profile_uri = $this->arg('profile'); if ($this->pullRemoteProfile()) {
$this->validateRemoteProfile();
}
return true; return true;
} }
@ -390,9 +328,6 @@ class OStatusSubAction extends Action
if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->handlePost(); $this->handlePost();
} else { } else {
if ($this->arg('profile')) {
$this->validateFeed();
}
$this->showForm(); $this->showForm();
} }
} }
@ -414,7 +349,7 @@ class OStatusSubAction extends Action
return; return;
} }
if ($this->validateFeed()) { if ($this->oprofile) {
if ($this->arg('submit')) { if ($this->arg('submit')) {
$this->saveFeed(); $this->saveFeed();
return; return;
@ -500,4 +435,9 @@ class OStatusSubAction extends Action
parent::showScripts(); parent::showScripts();
$this->autofocus('feedurl'); $this->autofocus('feedurl');
} }
function selfLink()
{
return common_local_url('ostatussub');
}
} }