Favorites are now being stored from activities

This commit is contained in:
Mikael Nordfeldth
2014-07-02 00:22:27 +02:00
parent 2e6a32c7be
commit c44146d6f8
7 changed files with 98 additions and 117 deletions

View File

@@ -147,39 +147,31 @@ class FavoritePlugin extends ActivityHandlerPlugin
'format' => '(xml|json)'));
}
public function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
{
}
// FIXME: Set this to abstract public when all plugins have migrated!
public function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
// FIXME: Set this to abstract public in lib/activityhandlerplugin.php ddwhen all plugins have migrated!
protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
{
assert($this->isMyActivity($act));
$actor = $stored->getProfile();
$uris = array();
if (count($act->objects) != 1) {
// TRANS: Exception thrown when a favor activity has anything but 1 object
throw new ClientException(_('Favoring activites can only be done one at a time'));
// If empty, we should've created it ourselves on our node.
if (!isset($options['created'])) {
$options['created'] = !empty($act->time) ? common_sql_date($act->time) : common_sql_now();
}
if (!isset($options['uri'])) {
$options['uri'] = !empty($act->id) ? $act->id : $act->selfLink;
}
$obj = $act->objects[0];
$type = isset($obj->type) ? ActivityUtils::resolveUri($obj->type, true) : ActivityObject::NOTE;
$uris = $obj->getIdentifiers();
// We must have an objects[0] here because in isMyActivity we require the count to be == 1
$actobj = $act->objects[0];
try {
$local = ActivityUtils::findLocalObject($uris, $type);
} catch (Exception $e) {
// TODO: if it's not available locally, we should import the favorited object!
common_debug('Could not find favored notice locally: '.var_export($uris,true));
$object = Fave::saveActivityObject($actobj, $stored);
} catch (ServerException $e) {
// Probably that the favored notice doesn't exist in our local database
// but may also be some missing profile or so, which we could catch in a
// more explicit catch-statement.
return null;
}
if (!empty($act->time)) {
// This should reasonably mean that it was created on our instance.
$options['created'] = common_sql_date($act->time);
}
$options['uri'] = !empty($act->id) ? $act->id : $act->selfLink;
$object = Fave::saveNew($actor, $local, $type, $options);
common_debug(get_called_class().' returning '.get_class($object).' object with uri: '.$object->uri);
return $object;
}

View File

@@ -79,7 +79,8 @@ class Fave extends Managed_DataObject
}
// exception throwing takeover!
public function insert() {
public function insert()
{
if (!parent::insert()) {
throw new ServerException(sprintf(_m('Could not store new object of type %s'), get_called_class()));
}
@@ -169,7 +170,8 @@ class Fave extends Managed_DataObject
return $act;
}
static function existsForProfile($notice, Profile $scoped) {
static function existsForProfile($notice, Profile $scoped)
{
$fave = self::pkeyGet(array('user_id'=>$scoped->id, 'notice_id'=>$notice->id));
return ($fave instanceof Fave);
@@ -270,7 +272,8 @@ class Fave extends Managed_DataObject
// Remember that we want the _activity_ notice here, not faves applied
// to the supplied Notice (as with byNotice)!
static public function fromStored(Notice $stored) {
static public function fromStored(Notice $stored)
{
$class = get_called_class();
$object = new $class;
$object->uri = $stored->uri;
@@ -280,16 +283,18 @@ class Fave extends Managed_DataObject
return $object;
}
static public function verbToTitle($verb) {
static public function verbToTitle($verb)
{
return ucfirst($verb);
}
static public function object_type()
static public function getObjectType()
{
return 'activity';
}
public function asActivityObject(Profile $scoped=null) {
public function asActivityObject(Profile $scoped=null)
{
$actobj = new ActivityObject();
$actobj->id = $this->getUri();
$actobj->type = ActivityUtils::resolveUri(ActivityObject::ACTIVITY);
@@ -301,35 +306,51 @@ class Fave extends Managed_DataObject
return $actobj;
}
static public function parseActivityObject(ActivityObject $actobj, Notice $stored) {
static public function parseActivityObject(ActivityObject $actobj, Notice $stored)
{
// The ActivityObject we get here is the _favored_ notice (kind of what we're "in-reply-to")
// The Notice we get is the _activity_ stored in our Notice table
$type = isset($actobj->type) ? ActivityUtils::resolveUri($actobj->type, true) : ActivityObject::NOTE;
$local = ActivityUtils::findLocalObject($actobj->getIdentifiers(), $type);
if (!$local instanceof Notice) {
// $local always returns something, but this was not what we expected. Something is wrong.
throw new Exception('Something other than a Notice was returned from findLocalObject');
}
$actor = $stored->getProfile();
$object = new Fave();
$object->user_id = $actor->id;
$object->notice_id = $stored->id;
$object->object_uri = $stored->uri;
$object->user_id = $stored->getProfile()->id;
$object->notice_id = $local->id;
$object->uri = $stored->uri;
$object->created = $stored->created;
$object->modified = $stored->modified;
return $object;
}
static function saveActivityObject(ActivityObject $actobj, Notice $stored) {
static function saveActivityObject(ActivityObject $actobj, Notice $stored)
{
$object = self::parseActivityObject($actobj, $stored);
$object->insert(); // exception throwing!
return $object;
}
public function getTarget() {
public function getTarget()
{
// throws exception on failure
return ActivityUtils::findLocalObject(array($this->uri), $this->type);
}
public function getTargetObject() {
public function getTargetObject()
{
return $this->getTarget()->asActivityObject();
}
protected $_stored = array();
public function getStored() {
public function getStored()
{
if (!isset($this->_stored[$this->uri])) {
$stored = new Notice();
$stored->uri = $this->uri;
@@ -341,7 +362,8 @@ class Fave extends Managed_DataObject
return $this->_stored[$this->uri];
}
public function getActor() {
public function getActor()
{
$profile = new Profile();
$profile->id = $this->user_id;
if (!$profile->find(true)) {
@@ -350,7 +372,8 @@ class Fave extends Managed_DataObject
return $profile;
}
public function getActorObject() {
public function getActorObject()
{
return $this->getActor()->asActivityObject();
}
@@ -371,7 +394,7 @@ class Fave extends Managed_DataObject
}
return TagURI::mint(strtolower(get_called_class()).':%d:%s:%d:%s',
$actor->id,
ActivityUtils::resolveUri(self::object_type(), true),
ActivityUtils::resolveUri(self::getObjectType(), true),
$target->id,
common_date_iso8601($created));
}