Esempio n. 1
0
 /**
  * Create local ostatus_profile and profile/user_group entries for
  * the provided remote user or group.
  * This should never return null -- you will either get an object or
  * an exception will be thrown.
  *
  * @param ActivityObject $object
  * @param array $hints
  *
  * @return Ostatus_profile
  */
 protected static function createActivityObjectProfile(ActivityObject $object, array $hints = array())
 {
     $homeuri = $object->id;
     $discover = false;
     if (!$homeuri) {
         common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));
         // TRANS: Exception.
         throw new Exception(_m('No profile URI.'));
     }
     $user = User::getKV('uri', $homeuri);
     if ($user instanceof User) {
         // TRANS: Exception.
         throw new Exception(_m('Local user cannot be referenced as remote.'));
     }
     if (OStatusPlugin::localGroupFromUrl($homeuri)) {
         // TRANS: Exception.
         throw new Exception(_m('Local group cannot be referenced as remote.'));
     }
     $ptag = Profile_list::getKV('uri', $homeuri);
     if ($ptag instanceof Profile_list) {
         $local_user = User::getKV('id', $ptag->tagger);
         if ($local_user instanceof User) {
             // TRANS: Exception.
             throw new Exception(_m('Local list cannot be referenced as remote.'));
         }
     }
     if (array_key_exists('feedurl', $hints)) {
         $feeduri = $hints['feedurl'];
     } else {
         $discover = new FeedDiscovery();
         $feeduri = $discover->discoverFromURL($homeuri);
     }
     if (array_key_exists('salmon', $hints)) {
         $salmonuri = $hints['salmon'];
     } else {
         if (!$discover) {
             $discover = new FeedDiscovery();
             $discover->discoverFromFeedURL($hints['feedurl']);
         }
         // XXX: NS_REPLIES is deprecated anyway, so let's remove it in the future.
         $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON) ?: $discover->getAtomLink(Salmon::NS_REPLIES);
     }
     if (array_key_exists('hub', $hints)) {
         $huburi = $hints['hub'];
     } else {
         if (!$discover) {
             $discover = new FeedDiscovery();
             $discover->discoverFromFeedURL($hints['feedurl']);
         }
         $huburi = $discover->getHubLink();
     }
     if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
         // We can only deal with folks with a PuSH hub
         throw new FeedSubNoHubException();
     }
     $oprofile = new Ostatus_profile();
     $oprofile->uri = $homeuri;
     $oprofile->feeduri = $feeduri;
     $oprofile->salmonuri = $salmonuri;
     $oprofile->created = common_sql_now();
     $oprofile->modified = common_sql_now();
     if ($object->type == ActivityObject::PERSON) {
         $profile = new Profile();
         $profile->created = common_sql_now();
         self::updateProfile($profile, $object, $hints);
         $oprofile->profile_id = $profile->insert();
         if ($oprofile->profile_id === false) {
             // TRANS: Server exception.
             throw new ServerException(_m('Cannot save local profile.'));
         }
     } else {
         if ($object->type == ActivityObject::GROUP) {
             $profile = new Profile();
             $profile->query('BEGIN');
             $group = new User_group();
             $group->uri = $homeuri;
             $group->created = common_sql_now();
             self::updateGroup($group, $object, $hints);
             // TODO: We should do this directly in User_group->insert()!
             // currently it's duplicated in User_group->update()
             // AND User_group->register()!!!
             $fields = array('nickname' => 'nickname', 'fullname' => 'fullname', 'mainpage' => 'profileurl', 'homepage' => 'homepage', 'description' => 'bio', 'location' => 'location', 'created' => 'created', 'modified' => 'modified');
             foreach ($fields as $gf => $pf) {
                 $profile->{$pf} = $group->{$gf};
             }
             $profile_id = $profile->insert();
             if ($profile_id === false) {
                 $profile->query('ROLLBACK');
                 throw new ServerException(_('Profile insertion failed.'));
             }
             $group->profile_id = $profile_id;
             $oprofile->group_id = $group->insert();
             if ($oprofile->group_id === false) {
                 $profile->query('ROLLBACK');
                 // TRANS: Server exception.
                 throw new ServerException(_m('Cannot save local profile.'));
             }
             $profile->query('COMMIT');
         } else {
             if ($object->type == ActivityObject::_LIST) {
                 $ptag = new Profile_list();
                 $ptag->uri = $homeuri;
                 $ptag->created = common_sql_now();
                 self::updatePeopletag($ptag, $object, $hints);
                 $oprofile->peopletag_id = $ptag->insert();
                 if ($oprofile->peopletag_id === false) {
                     // TRANS: Server exception.
                     throw new ServerException(_m('Cannot save local list.'));
                 }
             }
         }
     }
     $ok = $oprofile->insert();
     if ($ok === false) {
         // TRANS: Server exception.
         throw new ServerException(_m('Cannot save OStatus profile.'));
     }
     $avatar = self::getActivityObjectAvatar($object, $hints);
     if ($avatar) {
         try {
             $oprofile->updateAvatar($avatar);
         } catch (Exception $ex) {
             // Profile is saved, but Avatar is messed up. We're
             // just going to continue.
             common_log(LOG_WARNING, "Exception saving OStatus profile avatar: " . $ex->getMessage());
         }
     }
     return $oprofile;
 }
Esempio n. 2
0
 /**
  * Create local ostatus_profile and profile/user_group entries for
  * the provided remote user or group.
  * This should never return null -- you will either get an object or
  * an exception will be thrown.
  *
  * @param ActivityObject $object
  * @param array $hints
  *
  * @return Ostatus_profile
  */
 protected static function createActivityObjectProfile($object, $hints = array())
 {
     $homeuri = $object->id;
     $discover = false;
     if (!$homeuri) {
         common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));
         // TRANS: Exception.
         throw new Exception(_m('No profile URI.'));
     }
     $user = User::staticGet('uri', $homeuri);
     if ($user) {
         // TRANS: Exception.
         throw new Exception(_m('Local user cannot be referenced as remote.'));
     }
     if (OStatusPlugin::localGroupFromUrl($homeuri)) {
         // TRANS: Exception.
         throw new Exception(_m('Local group cannot be referenced as remote.'));
     }
     $ptag = Profile_list::staticGet('uri', $homeuri);
     if ($ptag) {
         $local_user = User::staticGet('id', $ptag->tagger);
         if (!empty($local_user)) {
             // TRANS: Exception.
             throw new Exception(_m('Local list cannot be referenced as remote.'));
         }
     }
     if (array_key_exists('feedurl', $hints)) {
         $feeduri = $hints['feedurl'];
     } else {
         $discover = new FeedDiscovery();
         $feeduri = $discover->discoverFromURL($homeuri);
     }
     if (array_key_exists('salmon', $hints)) {
         $salmonuri = $hints['salmon'];
     } else {
         if (!$discover) {
             $discover = new FeedDiscovery();
             $discover->discoverFromFeedURL($hints['feedurl']);
         }
         $salmonuri = $discover->getAtomLink(Salmon::NS_REPLIES);
     }
     if (array_key_exists('hub', $hints)) {
         $huburi = $hints['hub'];
     } else {
         if (!$discover) {
             $discover = new FeedDiscovery();
             $discover->discoverFromFeedURL($hints['feedurl']);
         }
         $huburi = $discover->getHubLink();
     }
     if (!$huburi && !common_config('feedsub', 'fallback_hub')) {
         // We can only deal with folks with a PuSH hub
         throw new FeedSubNoHubException();
     }
     $oprofile = new Ostatus_profile();
     $oprofile->uri = $homeuri;
     $oprofile->feeduri = $feeduri;
     $oprofile->salmonuri = $salmonuri;
     $oprofile->created = common_sql_now();
     $oprofile->modified = common_sql_now();
     if ($object->type == ActivityObject::PERSON) {
         $profile = new Profile();
         $profile->created = common_sql_now();
         self::updateProfile($profile, $object, $hints);
         $oprofile->profile_id = $profile->insert();
         if (!$oprofile->profile_id) {
             // TRANS: Server exception.
             throw new ServerException(_m('Cannot save local profile.'));
         }
     } else {
         if ($object->type == ActivityObject::GROUP) {
             $group = new User_group();
             $group->uri = $homeuri;
             $group->created = common_sql_now();
             self::updateGroup($group, $object, $hints);
             $oprofile->group_id = $group->insert();
             if (!$oprofile->group_id) {
                 // TRANS: Server exception.
                 throw new ServerException(_m('Cannot save local profile.'));
             }
         } else {
             if ($object->type == ActivityObject::_LIST) {
                 $ptag = new Profile_list();
                 $ptag->uri = $homeuri;
                 $ptag->created = common_sql_now();
                 self::updatePeopletag($ptag, $object, $hints);
                 $oprofile->peopletag_id = $ptag->insert();
                 if (!$oprofile->peopletag_id) {
                     // TRANS: Server exception.
                     throw new ServerException(_m('Cannot save local list.'));
                 }
             }
         }
     }
     $ok = $oprofile->insert();
     if (!$ok) {
         // TRANS: Server exception.
         throw new ServerException(_m('Cannot save OStatus profile.'));
     }
     $avatar = self::getActivityObjectAvatar($object, $hints);
     if ($avatar) {
         try {
             $oprofile->updateAvatar($avatar);
         } catch (Exception $ex) {
             // Profile is saved, but Avatar is messed up. We're
             // just going to continue.
             common_log(LOG_WARNING, "Exception saving OStatus profile avatar: " . $ex->getMessage());
         }
     }
     return $oprofile;
 }