]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/subscribe.php
Twitter integration - support for new foreign_link table and prefs now save/update
[quix0rs-gnu-social.git] / actions / subscribe.php
index bf97975768d53e59e9e07b343e3baf3f959edeb2..b93c06f120f0f50b084584acaa8d5688606275c0 100644 (file)
@@ -20,6 +20,7 @@
 if (!defined('LACONICA')) { exit(1); }
 
 class SubscribeAction extends Action {
+       
        function handle($args) {
                parent::handle($args);
 
@@ -35,6 +36,15 @@ class SubscribeAction extends Action {
                        return;
                }
 
+               # CSRF protection
+
+               $token = $this->trimmed('token');
+               
+               if (!$token || $token != common_session_token()) {
+                       $this->client_error(_('There was a problem with your session token. Try again, please.'));
+                       return;
+               }
+
                $other_nickname = $this->arg('subscribeto');
 
                $other = User::staticGet('nickname', $other_nickname);
@@ -49,19 +59,21 @@ class SubscribeAction extends Action {
                        return;
                }
 
-               $sub = new Subscription();
-               $sub->subscriber = $user->id;
-               $sub->subscribed = $other->id;
-
-               $sub->created = DB_DataObject_Cast::dateTime(); # current time
-
-               if (!$sub->insert()) {
-                       common_server_error(_('Couldn\'t create subscription.'));
+               if (!$user->subscribeTo($other)) {
+                       $this->server_error(_('Could not subscribe.'));
                        return;
                }
 
                $this->notify($other, $user);
 
+               if ($other->autosubscribe && !$other->isSubscribed($user)) {
+                       if (!$other->subscribeTo($user)) {
+                               $this->server_error(_('Could not subscribe other to you.'));
+                               return;
+                       }
+                       $this->notify($user, $other);
+               }
+               
                common_redirect(common_local_url('subscriptions', array('nickname' =>
                                                                                                                                $user->nickname)));
        }
@@ -69,31 +81,11 @@ class SubscribeAction extends Action {
        function notify($listenee, $listener) {
                # XXX: add other notifications (Jabber, SMS) here
                # XXX: queue this and handle it offline
+               # XXX: Whatever happens, do it in Twitter-like API, too
                $this->notify_email($listenee, $listener);
        }
 
        function notify_email($listenee, $listener) {
-               if ($listenee->email) {
-                       global $config;
-                       $profile = $listenee->getProfile();
-                       $other = $listener->getProfile();
-                       $name = $profile->getBestName();
-                       $long_name = ($other->fullname) ? ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
-                       $recipients = $listenee->email;
-                       $headers['From'] = mail_notify_from();
-                       $headers['To'] = $name . ' <' . $listenee->email . '>';
-                       $headers['Subject'] = sprintf(_('%1$s is now listening to your notices on %2$s.'), $name, $config['site']['name']);
-
-                       $body  = sprintf(_('%1$s is now listening to your notices on %2$s.'), $long_name, $config['site']['name']);
-                       $body .= "\n\n";
-                       $body .= "\t".$other->profileurl;
-                       $body .= "\n\n";
-                       $body .= _('Faithfully yours, ');
-                       $body .= "\n";
-                       $body .= $config['site']['name'];
-                       $body .= "\n";
-
-                       mail_send($recipients, $headers, $body);
-               }
+               mail_subscribe_notify($listenee, $listener);
        }
 }
\ No newline at end of file