]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/actions/ostatussub.php
Merge remote branch 'chat-interface-plugins/msn-plugin' into 1.0.x
[quix0rs-gnu-social.git] / plugins / OStatus / actions / ostatussub.php
index ffa88cb0884f335c06c8bf8bf254dc2fb22105b4..28714514f5ec402827bfd9cbe4df06fee5e26cc6 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  * 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
  * 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
  *
  *  showPreviewForm() - surrounding form for preview-and-confirm
- *    previewUser() - display profile for a remote user
- *    previewGroup() - display profile for a remote group
+ *    preview() - display profile for a remote user
  *
- *  successUser() - redirects to subscriptions page on subscribe
- *  successGroup() - redirects to groups page on join
+ *  success() - redirects to subscriptions page on subscribe
  */
 class OStatusSubAction extends Action
 {
@@ -55,8 +53,7 @@ class OStatusSubAction extends Action
         $this->elementStart('form', array('method' => 'post',
                                           'id' => 'form_ostatus_sub',
                                           'class' => 'form_settings',
-                                          'action' =>
-                                          common_local_url('ostatussub')));
+                                          'action' => $this->selfLink()));
 
         $this->hidden('token', common_session_token());
 
@@ -65,9 +62,9 @@ class OStatusSubAction extends Action
         $this->elementStart('ul', 'form_data');
         $this->elementStart('li');
         $this->input('profile',
-                     _m('Address or profile URL'),
+                     _m('Subscribe to'),
                      $this->profile_uri,
-                     _m('Enter the profile URL of a PubSubHubbub-enabled feed'));
+                     _m("OStatus user's address, like nickname@example.com or http://example.net/nickname"));
         $this->elementEnd('li');
         $this->elementEnd('ul');
 
@@ -87,75 +84,150 @@ class OStatusSubAction extends Action
      */
     function showPreviewForm()
     {
+        $ok = $this->preview();
+        if (!$ok) {
+            // @fixme maybe provide a cancel button or link back?
+            return;
+        }
+
+        $this->elementStart('div', 'entity_actions');
+        $this->elementStart('ul');
+        $this->elementStart('li', 'entity_subscribe');
         $this->elementStart('form', array('method' => 'post',
                                           'id' => 'form_ostatus_sub',
-                                          'class' => 'form_settings',
+                                          'class' => 'form_remote_authorize',
                                           'action' =>
-                                          common_local_url('ostatussub')));
-
+                                          $this->selfLink()));
+        $this->elementStart('fieldset');
         $this->hidden('token', common_session_token());
         $this->hidden('profile', $this->profile_uri);
-
-        $this->elementStart('fieldset', array('id' => 'settings_feeds'));
-
         if ($this->oprofile->isGroup()) {
-            $this->previewGroup();
-            $this->submit('subscribe', _m('Join'));
+            $this->submit('submit', _m('Join'), 'submit', null,
+                         _m('Join this group'));
         } else {
-            $this->previewUser();
-            $this->submit('subscribe', _m('Subscribe'));
+            $this->submit('submit', _m('Confirm'), 'submit', null,
+                         _m('Subscribe to this user'));
         }
-
-
         $this->elementEnd('fieldset');
-
         $this->elementEnd('form');
+        $this->elementEnd('li');
+        $this->elementEnd('ul');
+        $this->elementEnd('div');
     }
 
     /**
      * Show a preview for a remote user's profile
+     * @return boolean true if we're ok to try subscribing
      */
-    function previewUser()
+    function preview()
     {
         $oprofile = $this->oprofile;
         $profile = $oprofile->localProfile();
 
-        $this->text(sprintf(_m("Remote user %s"), $profile->nickname));
-        // ...
+        $cur = common_current_user();
+        if ($cur->isSubscribed($profile)) {
+            $this->element('div', array('class' => 'error'),
+                           _m("You are already subscribed to this user."));
+            $ok = false;
+        } else {
+            $ok = true;
+        }
+
+        $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
+        $avatarUrl = $avatar ? $avatar->displayUrl() : false;
+
+        $this->showEntity($profile,
+                          $profile->profileurl,
+                          $avatarUrl,
+                          $profile->bio);
+        return $ok;
     }
 
-    /**
-     * Show a preview for a remote group's profile
-     */
-    function previewGroup()
+    function showEntity($entity, $profile, $avatar, $note)
     {
-        $oprofile = $this->oprofile;
-        $group = $oprofile->localGroup();
+        $nickname = $entity->nickname;
+        $fullname = $entity->fullname;
+        $homepage = $entity->homepage;
+        $location = $entity->location;
+
+        if (!$avatar) {
+            $avatar = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
+        }
+
+        $this->elementStart('div', 'entity_profile vcard');
+        $this->elementStart('dl', 'entity_depiction');
+        $this->element('dt', null, _('Photo'));
+        $this->elementStart('dd');
+        $this->element('img', array('src' => $avatar,
+                                    'class' => 'photo avatar',
+                                    'width' => AVATAR_PROFILE_SIZE,
+                                    'height' => AVATAR_PROFILE_SIZE,
+                                    'alt' => $nickname));
+        $this->elementEnd('dd');
+        $this->elementEnd('dl');
+
+        $this->elementStart('dl', 'entity_nickname');
+        $this->element('dt', null, _('Nickname'));
+        $this->elementStart('dd');
+        $hasFN = ($fullname !== '') ? 'nickname' : 'fn nickname';
+        $this->elementStart('a', array('href' => $profile,
+                                       'class' => 'url '.$hasFN));
+        $this->raw($nickname);
+        $this->elementEnd('a');
+        $this->elementEnd('dd');
+        $this->elementEnd('dl');
+
+        if (!is_null($fullname)) {
+            $this->elementStart('dl', 'entity_fn');
+            $this->elementStart('dd');
+            $this->elementStart('span', 'fn');
+            $this->raw($fullname);
+            $this->elementEnd('span');
+            $this->elementEnd('dd');
+            $this->elementEnd('dl');
+        }
+        if (!is_null($location)) {
+            $this->elementStart('dl', 'entity_location');
+            $this->element('dt', null, _('Location'));
+            $this->elementStart('dd', 'label');
+            $this->raw($location);
+            $this->elementEnd('dd');
+            $this->elementEnd('dl');
+        }
 
-        $this->text(sprintf(_m("Remote group %s"), $group->nickname));
-        // ..
+        if (!is_null($homepage)) {
+            $this->elementStart('dl', 'entity_url');
+            $this->element('dt', null, _('URL'));
+            $this->elementStart('dd');
+            $this->elementStart('a', array('href' => $homepage,
+                                                'class' => 'url'));
+            $this->raw($homepage);
+            $this->elementEnd('a');
+            $this->elementEnd('dd');
+            $this->elementEnd('dl');
+        }
+
+        if (!is_null($note)) {
+            $this->elementStart('dl', 'entity_note');
+            $this->element('dt', null, _('Note'));
+            $this->elementStart('dd', 'note');
+            $this->raw($note);
+            $this->elementEnd('dd');
+            $this->elementEnd('dl');
+        }
+        $this->elementEnd('div');
     }
 
     /**
      * Redirect on successful remote user subscription
      */
-    function successUser()
+    function success()
     {
         $cur = common_current_user();
         $url = common_local_url('subscriptions', array('nickname' => $cur->nickname));
         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.
      * Fills out error UI string in $this->error
@@ -163,75 +235,75 @@ class OStatusSubAction extends Action
      *
      * @return boolean
      */
-    function validateFeed()
+    function pullRemoteProfile()
     {
-        $profile_uri = trim($this->arg('profile'));
-
-        if ($profile_uri == '') {
-            $this->showForm(_m('Empty remote profile URL!'));
-            return;
-        }
-        $this->profile_uri = $profile_uri;
-
-        // @fixme validate, normalize bla bla
+        $this->profile_uri = $this->trimmed('profile');
         try {
-            $oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
-            $this->oprofile = $oprofile;
+            if (Validate::email($this->profile_uri)) {
+                $this->oprofile = Ostatus_profile::ensureWebfinger($this->profile_uri);
+            } else if (Validate::uri($this->profile_uri)) {
+                $this->oprofile = Ostatus_profile::ensureProfileURL($this->profile_uri);
+            } else {
+                $this->error = _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");
+                common_debug('Invalid address format.', __FILE__);
+                return false;
+            }
             return true;
         } catch (FeedSubBadURLException $e) {
-            $this->error = _m('Invalid URL or could not reach server.');
+            $this->error = _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");
+            common_debug('Invalid URL or could not reach server.', __FILE__);
         } catch (FeedSubBadResponseException $e) {
-            $this->error = _m('Cannot read feed; server returned error.');
+            $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
+            common_debug('Cannot read feed; server returned error.', __FILE__);
         } catch (FeedSubEmptyException $e) {
-            $this->error = _m('Cannot read feed; server returned an empty page.');
+            $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
+            common_debug('Cannot read feed; server returned an empty page.', __FILE__);
         } catch (FeedSubBadHTMLException $e) {
-            $this->error = _m('Bad HTML, could not find feed link.');
+            $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
+            common_debug('Bad HTML, could not find feed link.', __FILE__);
         } catch (FeedSubNoFeedException $e) {
-            $this->error = _m('Could not find a feed linked from this URL.');
+            $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
+            common_debug('Could not find a feed linked from this URL.', __FILE__);
         } catch (FeedSubUnrecognizedTypeException $e) {
-            $this->error = _m('Not a recognized feed type.');
-        } catch (FeedSubException $e) {
+            $this->error = _m("Sorry, we could not reach that feed. Please try that OStatus address again later.");
+            common_debug('Not a recognized feed type.', __FILE__);
+        } catch (Exception $e) {
             // Any new ones we forgot about
-            $this->error = sprintf(_m('Bad feed URL: %s %s'), get_class($e), $e->getMessage());
+            $this->error = _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");
+            common_debug(sprintf('Bad feed URL: %s %s', get_class($e), $e->getMessage()), __FILE__);
         }
 
         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.
      * 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()
     {
         // And subscribe the current user to the local profile
         $user = common_current_user();
-
-        if (!$this->oprofile->subscribe()) {
-            $this->showForm(_m("Failed to set up server-to-server subscription."));
-            return;
-        }
-
-        if ($this->oprofile->isGroup()) {
-            $group = $this->oprofile->localGroup();
-            if ($user->isMember($group)) {
-                $this->showForm(_m('Already a member!'));
-            } elseif (Group_member::join($this->oprofile->group_id, $user->id)) {
-                $this->successGroup();
-            } else {
-                $this->showForm(_m('Remote group join failed!'));
-            }
+        $local = $this->oprofile->localProfile();
+        if ($user->isSubscribed($local)) {
+            // TRANS: OStatus remote subscription dialog error.
+            $this->showForm(_m('Already subscribed!'));
+        } elseif (Subscription::start($user, $local)) {
+            $this->success();
         } else {
-            $local = $this->oprofile->localProfile();
-            if ($user->isSubscribed($local)) {
-                $this->showForm(_m('Already subscribed!'));
-            } elseif ($this->oprofile->subscribeLocalToRemote($user)) {
-                $this->successUser();
-            } else {
-                $this->showForm(_m('Remote subscription failed!'));
-            }
+            // TRANS: OStatus remote subscription dialog error.
+            $this->showForm(_m('Remote subscription failed!'));
         }
     }
 
@@ -248,8 +320,9 @@ class OStatusSubAction extends Action
             return false;
         }
 
-        $this->profile_uri = $this->arg('profile');
-
+        if ($this->pullRemoteProfile()) {
+            $this->validateRemoteProfile();
+        }
         return true;
     }
 
@@ -262,14 +335,10 @@ class OStatusSubAction extends Action
         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
             $this->handlePost();
         } else {
-            if ($this->arg('profile')) {
-                $this->validateFeed();
-            }
             $this->showForm();
         }
     }
 
-
     /**
      * Handle posts to this form
      *
@@ -286,8 +355,8 @@ class OStatusSubAction extends Action
             return;
         }
 
-        if ($this->validateFeed()) {
-            if ($this->arg('subscribe')) {
+        if ($this->oprofile) {
+            if ($this->arg('submit')) {
                 $this->saveFeed();
                 return;
             }
@@ -327,7 +396,8 @@ class OStatusSubAction extends Action
 
     function title()
     {
-        return _m('Authorize subscription');
+        // TRANS: Page title for OStatus remote subscription form
+        return _m('Confirm');
     }
 
     /**
@@ -343,7 +413,7 @@ class OStatusSubAction extends Action
 
     function showPageNotice()
     {
-        if ($this->error) {
+        if (!empty($this->error)) {
             $this->element('p', 'error', $this->error);
         }
     }
@@ -371,4 +441,23 @@ class OStatusSubAction extends Action
         parent::showScripts();
         $this->autofocus('feedurl');
     }
+
+    function selfLink()
+    {
+        return common_local_url('ostatussub');
+    }
+
+    /**
+     * Disable the send-notice form at the top of the page.
+     * This is really just a hack for the broken CSS in the Cloudy theme,
+     * I think; copying from other non-notice-navigation pages that do this
+     * as well. There will be plenty of others also broken.
+     *
+     * @fixme fix the cloudy theme
+     * @fixme do this in a more general way
+     */
+    function showNoticeForm() {
+        // nop
+    }
+
 }