X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FOStatus%2Fclasses%2FOstatus_profile.php;h=821ebef3d5da21c7afc7047918580de9ddd4c8f4;hb=5304373b0b5c9905b30c85b565c23246d377467b;hp=4ba0a6c85d91f45e3eb7894b8a4fc2fc224ef136;hpb=e7c7fd39fc948f1169512916077185dd29973b60;p=quix0rs-gnu-social.git diff --git a/plugins/OStatus/classes/Ostatus_profile.php b/plugins/OStatus/classes/Ostatus_profile.php index 4ba0a6c85d..821ebef3d5 100644 --- a/plugins/OStatus/classes/Ostatus_profile.php +++ b/plugins/OStatus/classes/Ostatus_profile.php @@ -34,6 +34,7 @@ class Ostatus_profile extends Managed_DataObject public $profile_id; public $group_id; + public $peopletag_id; public $feeduri; public $salmonuri; @@ -60,6 +61,7 @@ class Ostatus_profile extends Managed_DataObject 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true), 'profile_id' => array('type' => 'integer'), 'group_id' => array('type' => 'integer'), + 'peopletag_id' => array('type' => 'integer'), 'feeduri' => array('type' => 'varchar', 'length' => 255), 'salmonuri' => array('type' => 'varchar', 'length' => 255), 'avatar' => array('type' => 'text'), @@ -70,11 +72,13 @@ class Ostatus_profile extends Managed_DataObject 'unique keys' => array( 'ostatus_profile_profile_id_idx' => array('profile_id'), 'ostatus_profile_group_id_idx' => array('group_id'), + 'ostatus_profile_peopletag_id_idx' => array('peopletag_id'), 'ostatus_profile_feeduri_idx' => array('feeduri'), ), 'foreign keys' => array( 'ostatus_profile_profile_id_fkey' => array('profile', array('profile_id' => 'id')), 'ostatus_profile_group_id_fkey' => array('user_group', array('group_id' => 'id')), + 'ostatus_profile_peopletag_id_fkey' => array('profile_list', array('peopletag_id' => 'id')), ), ); } @@ -103,6 +107,18 @@ class Ostatus_profile extends Managed_DataObject return null; } + /** + * Fetch the StatusNet-side peopletag for this feed + * @return Profile + */ + public function localPeopletag() + { + if ($this->peopletag_id) { + return Profile_list::staticGet('id', $this->peopletag_id); + } + return null; + } + /** * Returns an ActivityObject describing this remote user or group profile. * Can then be used to generate Atom chunks. @@ -113,6 +129,8 @@ class Ostatus_profile extends Managed_DataObject { if ($this->isGroup()) { return ActivityObject::fromGroup($this->localGroup()); + } else if ($this->isPeopletag()) { + return ActivityObject::fromPeopletag($this->localPeopletag()); } else { return ActivityObject::fromProfile($this->localProfile()); } @@ -134,6 +152,9 @@ class Ostatus_profile extends Managed_DataObject if ($this->isGroup()) { $noun = ActivityObject::fromGroup($this->localGroup()); return $noun->asString('activity:' . $element); + } else if ($this->isPeopletag()) { + $noun = ActivityObject::fromPeopletag($this->localPeopletag()); + return $noun->asString('activity:' . $element); } else { $noun = ActivityObject::fromProfile($this->localProfile()); return $noun->asString('activity:' . $element); @@ -145,16 +166,34 @@ class Ostatus_profile extends Managed_DataObject */ function isGroup() { - if ($this->profile_id && !$this->group_id) { + if ($this->profile_id || $this->peopletag_id && !$this->group_id) { return false; - } else if ($this->group_id && !$this->profile_id) { + } else if ($this->group_id && !$this->profile_id && !$this->peopletag_id) { return true; - } else if ($this->group_id && $this->profile_id) { - // TRANS: Server exception. - throw new ServerException(sprintf(_m('Invalid ostatus_profile state: both group and profile IDs set for %s.'),$this->uri)); + } else if ($this->group_id && ($this->profile_id || $this->peopletag_id)) { + // TRANS: Server exception. %s is a URI + throw new ServerException(_m("Invalid ostatus_profile state: two or more IDs set for %s", $this->uri)); } else { - // TRANS: Server exception. - throw new ServerException(sprintf(_m('Invalid ostatus_profile state: both group and profile IDs empty for %s.'),$this->uri)); + // TRANS: Server exception. %s is a URI + throw new ServerException(_m("Invalid ostatus_profile state: all IDs empty for %s", $this->uri)); + } + } + + /** + * @return boolean true if this is a remote peopletag + */ + function isPeopletag() + { + if ($this->profile_id || $this->group_id && !$this->peopletag_id) { + return false; + } else if ($this->peopletag_id && !$this->profile_id && !$this->group_id) { + return true; + } else if ($this->peopletag_id && ($this->profile_id || $this->group_id)) { + // TRANS: Server exception. %s is a URI + throw new ServerException(_m("Invalid ostatus_profile state: two or more IDs set for %s", $this->uri)); + } else { + // TRANS: Server exception. %s is a URI + throw new ServerException(_m("Invalid ostatus_profile state: all IDs empty for %s", $this->uri)); } } @@ -214,8 +253,15 @@ class Ostatus_profile extends Managed_DataObject if ($this->isGroup()) { $members = $this->localGroup()->getMembers(0, 1); $count = $members->N; + } else if ($this->isPeopletag()) { + $subscribers = $this->localPeopletag()->getSubscribers(0, 1); + $count = $subscribers->N; } else { - $count = $this->localProfile()->subscriberCount(); + $profile = $this->localProfile(); + $count = $profile->subscriberCount(); + if ($profile->hasLocalTags()) { + $count = 1; + } } common_log(LOG_INFO, __METHOD__ . " SUB COUNT BEFORE: $count"); @@ -235,7 +281,7 @@ class Ostatus_profile extends Managed_DataObject * @param string $verb Activity::SUBSCRIBE or Activity::JOIN * @param Object $object object of the action; must define asActivityNoun($tag) */ - public function notify($actor, $verb, $object=null) + public function notify($actor, $verb, $object=null, $target=null) { if (!($actor instanceof Profile)) { $type = gettype($actor); @@ -277,6 +323,9 @@ class Ostatus_profile extends Managed_DataObject $entry->raw($actor->asAtomAuthor()); $entry->raw($actor->asActivityActor()); $entry->raw($object->asActivityNoun('object')); + if ($target != null) { + $entry->raw($target->asActivityNoun('target')); + } $entry->elementEnd('entry'); $xml = $entry->getString(); @@ -293,6 +342,7 @@ class Ostatus_profile extends Managed_DataObject * an acceptable response from the remote site. * * @param mixed $entry XML string, Notice, or Activity + * @param Profile $actor * @return boolean success */ public function notifyActivity($entry, $actor) @@ -345,6 +395,8 @@ class Ostatus_profile extends Managed_DataObject { if ($this->isGroup()) { return $this->localGroup()->getBestName(); + } else if ($this->isPeopletag()) { + return $this->localPeopletag()->getBestName(); } else { return $this->localProfile()->getBestName(); } @@ -367,6 +419,7 @@ class Ostatus_profile extends Managed_DataObject } else if ($feed->localName == 'rss') { // @fixme check namespace $this->processRssFeed($feed, $source); } else { + // TRANS: Exception. throw new Exception(_m('Unknown feed format.')); } } @@ -390,6 +443,7 @@ class Ostatus_profile extends Managed_DataObject $channels = $rss->getElementsByTagName('channel'); if ($channels->length == 0) { + // TRANS: Exception. throw new Exception(_m('RSS feed without a channel.')); } else if ($channels->length > 1) { common_log(LOG_WARNING, __METHOD__ . ": more than one channel in an RSS feed"); @@ -417,7 +471,8 @@ class Ostatus_profile extends Managed_DataObject { $activity = new Activity($entry, $feed); - if (Event::handle('StartHandleFeedEntry', array($activity))) { + if (Event::handle('StartHandleFeedEntryWithProfile', array($activity, $this)) && + Event::handle('StartHandleFeedEntry', array($activity))) { // @todo process all activity objects switch ($activity->objects[0]->type) { @@ -439,6 +494,7 @@ class Ostatus_profile extends Managed_DataObject } Event::handle('EndHandleFeedEntry', array($activity)); + Event::handle('EndHandleFeedEntryWithProfile', array($activity, $this)); } } @@ -451,36 +507,10 @@ class Ostatus_profile extends Managed_DataObject */ public function processPost($activity, $method) { - if ($this->isGroup()) { - // A group feed will contain posts from multiple authors. - // @fixme validate these profiles in some way! - $oprofile = self::ensureActorProfile($activity); - if ($oprofile->isGroup()) { - // Groups can't post notices in StatusNet. - common_log(LOG_WARNING, "OStatus: skipping post with group listed as author: $oprofile->uri in feed from $this->uri"); - return false; - } - } else { - $actor = $activity->actor; - - if (empty($actor)) { - // OK here! assume the default - } else if ($actor->id == $this->uri || $actor->link == $this->uri) { - $this->updateFromActivityObject($actor); - } else if ($actor->id) { - // We have an ActivityStreams actor with an explicit ID that doesn't match the feed owner. - // This isn't what we expect from mainline OStatus person feeds! - // Group feeds go down another path, with different validation... - // Most likely this is a plain ol' blog feed of some kind which - // doesn't match our expectations. We'll take the entry, but ignore - // the info. - common_log(LOG_WARNING, "Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for {$this->uri}"); - } else { - // Plain without ActivityStreams actor info. - // We'll just ignore this info for now and save the update under the feed's identity. - } + $oprofile = $this->checkAuthorship($activity); - $oprofile = $this; + if (empty($oprofile)) { + return false; } // It's not always an ActivityObject::NOTE, but... let's just say it is. @@ -517,7 +547,7 @@ class Ostatus_profile extends Managed_DataObject $sourceContent = $note->title; } else { // @fixme fetch from $sourceUrl? - // TRANS: Client exception. %s is a source URL. + // TRANS: Client exception. %s is a source URI. throw new ClientException(sprintf(_m('No content for notice %s.'),$sourceUri)); } @@ -550,7 +580,9 @@ class Ostatus_profile extends Managed_DataObject // We mark up the attachment link specially for the HTML output // so we can fold-out the full version inline. - // TRANS: Shown when a notice is longer than supported and/or when attachments are present. + // @fixme I18N this tooltip will be saved with the site's default language + // TRANS: Shown when a notice is longer than supported and/or when attachments are present. At runtime + // TRANS: this will usually be replaced with localised text from StatusNet core messages. $showMoreText = _m('Show more'); $attachUrl = common_local_url('attachment', array('attachment' => $attachment->id)); @@ -569,6 +601,7 @@ class Ostatus_profile extends Managed_DataObject 'rendered' => $rendered, 'replies' => array(), 'groups' => array(), + 'peopletags' => array(), 'tags' => array(), 'urls' => array()); @@ -605,6 +638,10 @@ class Ostatus_profile extends Managed_DataObject } } + if ($this->isPeopletag()) { + $options['peopletags'][] = $this->localPeopletag(); + } + // Atom categories <-> hashtags foreach ($activity->categories as $cat) { if ($cat->term) { @@ -801,7 +838,7 @@ class Ostatus_profile extends Managed_DataObject return self::ensureFeedURL($feedurl, $hints); } - // TRANS: Exception. + // TRANS: Exception. %s is a URL. throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl)); } @@ -893,53 +930,19 @@ class Ostatus_profile extends Managed_DataObject * @return Ostatus_profile * @throws Exception */ + public static function ensureAtomFeed($feedEl, $hints) { - // Try to get a profile from the feed activity:subject - - $subject = ActivityUtils::child($feedEl, Activity::SUBJECT, Activity::SPEC); - - if (!empty($subject)) { - $subjObject = new ActivityObject($subject); - return self::ensureActivityObjectProfile($subjObject, $hints); - } - - // Otherwise, try the feed author - - $author = ActivityUtils::child($feedEl, Activity::AUTHOR, Activity::ATOM); + $author = ActivityUtils::getFeedAuthor($feedEl); - if (!empty($author)) { - $authorObject = new ActivityObject($author); - return self::ensureActivityObjectProfile($authorObject, $hints); + if (empty($author)) { + // XXX: make some educated guesses here + // TRANS: Feed sub exception. + throw new FeedSubException(_m('Can\'t find enough profile '. + 'information to make a feed.')); } - // Sheesh. Not a very nice feed! Let's try fingerpoken in the - // entries. - - $entries = $feedEl->getElementsByTagNameNS(Activity::ATOM, 'entry'); - - if (!empty($entries) && $entries->length > 0) { - - $entry = $entries->item(0); - - $actor = ActivityUtils::child($entry, Activity::ACTOR, Activity::SPEC); - - if (!empty($actor)) { - $actorObject = new ActivityObject($actor); - return self::ensureActivityObjectProfile($actorObject, $hints); - - } - - $author = ActivityUtils::child($entry, Activity::AUTHOR, Activity::ATOM); - - if (!empty($author)) { - $authorObject = new ActivityObject($author); - return self::ensureActivityObjectProfile($authorObject, $hints); - } - } - - // XXX: make some educated guesses here - throw new FeedSubException(_m('Can\'t find enough profile information to make a feed.')); + return self::ensureActivityObjectProfile($author, $hints); } /** @@ -998,6 +1001,7 @@ class Ostatus_profile extends Managed_DataObject return; } if (!common_valid_http_url($url)) { + // TRANS: Server exception. %s is a URL. throw new ServerException(sprintf(_m("Invalid avatar URL %s."), $url)); } @@ -1008,6 +1012,7 @@ class Ostatus_profile extends Managed_DataObject } if (!$self) { throw new ServerException(sprintf( + // TRANS: Server exception. %s is a URI. _m("Tried to update avatar for unsaved remote profile %s."), $this->uri)); } @@ -1017,6 +1022,7 @@ class Ostatus_profile extends Managed_DataObject $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar'); try { if (!copy($url, $temp_filename)) { + // TRANS: Server exception. %s is a URL. throw new ServerException(sprintf(_m("Unable to fetch avatar from %s."), $url)); } @@ -1124,7 +1130,8 @@ class Ostatus_profile extends Managed_DataObject return $url; } } - return common_path('plugins/OStatus/images/96px-Feed-icon.svg.png'); + + return Plugin::staticPath('OStatus', 'images/96px-Feed-icon.svg.png'); } /** @@ -1251,6 +1258,14 @@ class Ostatus_profile extends Managed_DataObject throw new Exception(_m('Local group can\'t be referenced as remote.')); } + $ptag = Profile_list::staticGet('uri', $homeuri); + if ($ptag) { + $local_user = User::staticGet('id', $ptag->tagger); + if (!empty($local_user)) { + throw new Exception("Local peopletag can't be referenced as remote."); + } + } + if (array_key_exists('feedurl', $hints)) { $feeduri = $hints['feedurl']; } else { @@ -1299,10 +1314,10 @@ class Ostatus_profile extends Managed_DataObject $oprofile->profile_id = $profile->insert(); if (!$oprofile->profile_id) { - // TRANS: Exception. + // TRANS: Server exception. throw new ServerException(_m('Can\'t save local profile.')); } - } else { + } else if ($object->type == ActivityObject::GROUP) { $group = new User_group(); $group->uri = $homeuri; $group->created = common_sql_now(); @@ -1310,15 +1325,25 @@ class Ostatus_profile extends Managed_DataObject $oprofile->group_id = $group->insert(); if (!$oprofile->group_id) { - // TRANS: Exception. + // TRANS: Server exception. throw new ServerException(_m('Can\'t 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) { + throw new ServerException("Can't save local peopletag"); + } } $ok = $oprofile->insert(); if (!$ok) { - // TRANS: Exception. + // TRANS: Server exception. throw new ServerException(_m('Can\'t save OStatus profile.')); } @@ -1347,12 +1372,16 @@ class Ostatus_profile extends Managed_DataObject if ($this->isGroup()) { $group = $this->localGroup(); self::updateGroup($group, $object, $hints); + } else if ($this->isPeopletag()) { + $ptag = $this->localPeopletag(); + self::updatePeopletag($ptag, $object, $hints); } else { $profile = $this->localProfile(); self::updateProfile($profile, $object, $hints); } + $avatar = self::getActivityObjectAvatar($object, $hints); - if ($avatar) { + if ($avatar && !isset($ptag)) { try { $this->updateAvatar($avatar); } catch (Exception $ex) { @@ -1365,7 +1394,17 @@ class Ostatus_profile extends Managed_DataObject { $orig = clone($profile); - $profile->nickname = self::getActivityObjectNickname($object, $hints); + // Existing nickname is better than nothing. + + if (!array_key_exists('nickname', $hints)) { + $hints['nickname'] = $profile->nickname; + } + + $nickname = self::getActivityObjectNickname($object, $hints); + + if (!empty($nickname)) { + $profile->nickname = $nickname; + } if (!empty($object->title)) { $profile->fullname = $object->title; @@ -1381,9 +1420,23 @@ class Ostatus_profile extends Managed_DataObject $profile->profileurl = $object->id; } - $profile->bio = self::getActivityObjectBio($object, $hints); - $profile->location = self::getActivityObjectLocation($object, $hints); - $profile->homepage = self::getActivityObjectHomepage($object, $hints); + $bio = self::getActivityObjectBio($object, $hints); + + if (!empty($bio)) { + $profile->bio = $bio; + } + + $location = self::getActivityObjectLocation($object, $hints); + + if (!empty($location)) { + $profile->location = $location; + } + + $homepage = self::getActivityObjectHomepage($object, $hints); + + if (!empty($homepage)) { + $profile->homepage = $homepage; + } if (!empty($object->geopoint)) { $location = ActivityContext::locationFromPoint($object->geopoint); @@ -1426,6 +1479,27 @@ class Ostatus_profile extends Managed_DataObject } } + protected static function updatePeopletag($tag, $object, $hints=array()) { + $orig = clone($tag); + + $tag->tag = $object->title; + + if (!empty($object->link)) { + $tag->mainpage = $object->link; + } else if (array_key_exists('profileurl', $hints)) { + $tag->mainpage = $hints['profileurl']; + } + + $tag->description = $object->summary; + $tagger = self::ensureActivityObjectProfile($object->owner); + $tag->tagger = $tagger->profile_id; + + if ($tag->id) { + common_log(LOG_DEBUG, "Updating OStatus peopletag $tag->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true)); + $tag->update($orig); + } + } + protected static function getActivityObjectHomepage($object, $hints=array()) { $homepage = null; @@ -1506,8 +1580,11 @@ class Ostatus_profile extends Managed_DataObject } // Try the profile url (like foo.example.com or example.com/user/foo) - - $profileUrl = ($object->link) ? $object->link : $hints['profileurl']; + if (!empty($object->link)) { + $profileUrl = $object->link; + } else if (!empty($hints['profileurl'])) { + $profileUrl = $hints['profileurl']; + } if (!empty($profileUrl)) { $nickname = self::nicknameFromURI($profileUrl); @@ -1538,9 +1615,11 @@ class Ostatus_profile extends Managed_DataObject protected static function nicknameFromURI($uri) { - preg_match('/(\w+):/', $uri, $matches); - - $protocol = $matches[1]; + if (preg_match('/(\w+):/', $uri, $matches)) { + $protocol = $matches[1]; + } else { + return null; + } switch ($protocol) { case 'acct': @@ -1757,6 +1836,7 @@ class Ostatus_profile extends Managed_DataObject if ($file_id === false) { common_log_db_error($file, "INSERT", __FILE__); + // TRANS: Server exception. throw new ServerException(_m('Could not store HTML content of long post as file.')); } @@ -1785,12 +1865,54 @@ class Ostatus_profile extends Managed_DataObject case 'mailto': $rest = $match[2]; $oprofile = Ostatus_profile::ensureWebfinger($rest); + break; default: - common_log("Unrecognized URI protocol for profile: $protocol ($uri)"); + throw new ServerException("Unrecognized URI protocol for profile: $protocol ($uri)"); break; } + } else { + throw new ServerException("No URI protocol for profile: ($uri)"); } } + + return $oprofile; + } + + function checkAuthorship($activity) + { + if ($this->isGroup() || $this->isPeopletag()) { + // A group or propletag feed will contain posts from multiple authors. + $oprofile = self::ensureActorProfile($activity); + if ($oprofile->isGroup() || $oprofile->isPeopletag()) { + // Groups can't post notices in StatusNet. + common_log(LOG_WARNING, + "OStatus: skipping post with group listed ". + "as author: $oprofile->uri in feed from $this->uri"); + return false; + } + } else { + $actor = $activity->actor; + + if (empty($actor)) { + // OK here! assume the default + } else if ($actor->id == $this->uri || $actor->link == $this->uri) { + $this->updateFromActivityObject($actor); + } else if ($actor->id) { + // We have an ActivityStreams actor with an explicit ID that doesn't match the feed owner. + // This isn't what we expect from mainline OStatus person feeds! + // Group feeds go down another path, with different validation... + // Most likely this is a plain ol' blog feed of some kind which + // doesn't match our expectations. We'll take the entry, but ignore + // the info. + common_log(LOG_WARNING, "Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for {$this->uri}"); + } else { + // Plain without ActivityStreams actor info. + // We'll just ignore this info for now and save the update under the feed's identity. + } + + $oprofile = $this; + } + return $oprofile; } }