Working on some RSVP code stuff

This commit is contained in:
Mikael Nordfeldth 2016-01-21 02:10:34 +01:00
parent 5999171c11
commit f74d2d555c
10 changed files with 126 additions and 66 deletions

View File

@ -282,6 +282,11 @@ class Notice extends Managed_DataObject
return ActivityUtils::resolveUri($this->verb, $make_relative); return ActivityUtils::resolveUri($this->verb, $make_relative);
} }
public function isVerb(array $verbs)
{
return ActivityUtils::compareVerbs($this->getVerb(), $verbs);
}
/* /*
* Get the original representation URL of this notice. * Get the original representation URL of this notice.
* *
@ -314,6 +319,15 @@ class Notice extends Managed_DataObject
return ActivityUtils::resolveUri($this->object_type, $canonical); return ActivityUtils::resolveUri($this->object_type, $canonical);
} }
public function isObjectType(array $types)
{
try {
return ActivityUtils::compareTypes($this->getObjectType(), $types);
} catch (NoObjectTypeException $e) {
return false;
}
}
public static function getByUri($uri) public static function getByUri($uri)
{ {
$notice = new Notice(); $notice = new Notice();

View File

@ -108,6 +108,7 @@ abstract class ActivityHandlerPlugin extends Plugin
} }
function isMyType($type) { function isMyType($type) {
// Third argument to compareTypes is true, to allow for notices with empty object_type for example (verb-only)
return count($this->types())===0 || ActivityUtils::compareTypes($type, $this->types()); return count($this->types())===0 || ActivityUtils::compareTypes($type, $this->types());
} }

View File

@ -350,7 +350,7 @@ class ActivityUtils
static function compareTypes($type, $objects) static function compareTypes($type, $objects)
{ {
$type = self::resolveUri($type); $type = self::resolveUri($type, false);
foreach ((array)$objects as $object) { foreach ((array)$objects as $object) {
if ($type === self::resolveUri($object)) { if ($type === self::resolveUri($object)) {
return true; return true;
@ -367,6 +367,7 @@ class ActivityUtils
static function resolveUri($uri, $make_relative=false) static function resolveUri($uri, $make_relative=false)
{ {
if (empty($uri)) { if (empty($uri)) {
common_debug(_ve(debug_backtrace()));
throw new ServerException('No URI to resolve in ActivityUtils::resolveUri'); throw new ServerException('No URI to resolve in ActivityUtils::resolveUri');
} }

View File

@ -78,7 +78,12 @@ class Deleted_notice extends Managed_DataObject
$act->actor = $actor->asActivityObject(); $act->actor = $actor->asActivityObject();
$act->target = new ActivityObject(); // We don't save the notice object, as it's supposed to be removed! $act->target = new ActivityObject(); // We don't save the notice object, as it's supposed to be removed!
$act->target->id = $notice->getUri(); $act->target->id = $notice->getUri();
try {
$act->target->type = $notice->getObjectType(); $act->target->type = $notice->getObjectType();
} catch (NoObjectTypeException $e) {
// This could be for example an RSVP which is a verb and carries no object-type
$act->target->type = null;
}
$act->objects = array(clone($act->target)); $act->objects = array(clone($act->target));
$url = $notice->getUrl(); $url = $notice->getUrl();

View File

@ -129,6 +129,13 @@ class EventPlugin extends ActivityVerbHandlerPlugin
RSVP::POSSIBLE); RSVP::POSSIBLE);
} }
function isMyNotice(Notice $notice) {
if (!empty($notice->object_type)) {
return parent::isMyNotice($notice);
}
return $this->isMyVerb($notice->verb);
}
public function newFormAction() { public function newFormAction() {
// such as 'newbookmark' or 'newevent' route // such as 'newbookmark' or 'newevent' route
return 'new'.$this->tag(); return 'new'.$this->tag();
@ -191,57 +198,22 @@ class EventPlugin extends ActivityVerbHandlerPlugin
$happening = null; $happening = null;
switch (true) { switch (true) {
case ActivityUtils::compareVerbs($stored->verb, array(ActivityVerb::POST)) && case $stored->isVerb([ActivityVerb::POST]) && $stored->isObjectType([Happening::OBJECT_TYPE]):
ActivityUtils::compareTypes($stored->object_type, array(Happening::OBJECT_TYPE)): $obj = Happening::fromStored($stored)->asActivityObject();
$happening = Happening::fromStored($stored);
break; break;
// FIXME: Why are these object_type?? // isObjectType here is because we had the verb stored in object_type previously for unknown reasons
case ActivityUtils::compareTypes($stored->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)): case $stored->isObjectType([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
$rsvp = RSVP::fromNotice($stored); case $stored->isVerb([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
$happening = $rsvp->getEvent(); $obj = RSVP::fromStored($stored)->asActivityObject();
break; break;
default: default:
// TRANS: Exception thrown when event plugin comes across a unknown object type. // TRANS: Exception thrown when event plugin comes across a unknown object type.
throw new Exception(_m('Unknown object type.')); throw new Exception(_m('Unknown object type.'));
} }
$obj = new ActivityObject();
$obj->id = $happening->getUri();
$obj->type = Happening::OBJECT_TYPE;
$obj->title = $happening->title;
$obj->summary = $happening->description;
$obj->link = $happening->getStored()->getUrl();
$obj->extra[] = array('dtstart',
array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
common_date_iso8601($happening->start_time));
$obj->extra[] = array('dtend',
array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
common_date_iso8601($happening->end_time));
$obj->extra[] = array('location', false, $happening->location);
$obj->extra[] = array('url', false, $happening->url);
return $obj; return $obj;
} }
/**
* Change the verb on RSVP notices
*
* @param Notice $notice
*
* @return ActivityObject
*/
protected function extendActivity(Notice $stored, Activity $act, Profile $scoped=null) {
switch (true) {
// FIXME: Why are these object_type??
case ActivityUtils::compareTypes($stored->object_type, array(RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE)):
$act->verb = $stored->object_type;
break;
}
return true;
}
/** /**
* Form for our app * Form for our app
* *
@ -253,33 +225,29 @@ class EventPlugin extends ActivityVerbHandlerPlugin
return new EventForm($out); return new EventForm($out);
} }
/** function deleteRelated(Notice $stored)
* When a notice is deleted, clean up related tables.
*
* @param Notice $notice
*/
function deleteRelated(Notice $notice)
{ {
switch ($notice->object_type) { switch (true) {
case Happening::OBJECT_TYPE: case $stored->isObjectType([Happening::OBJECT_TYPE]):
common_log(LOG_DEBUG, "Deleting event from notice..."); common_log(LOG_DEBUG, "Deleting event from notice...");
try { try {
$happening = Happening::fromStored($notice); $happening = Happening::fromStored($stored);
$happening->delete(); $happening->delete();
} catch (NoResultException $e) { } catch (NoResultException $e) {
// already gone // already gone
} }
break; break;
case RSVP::POSITIVE: // isObjectType here is because we had the verb stored in object_type previously for unknown reasons
case RSVP::NEGATIVE: case $stored->isObjectType([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
case RSVP::POSSIBLE: case $stored->isVerb([RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
common_log(LOG_DEBUG, "Deleting rsvp from notice..."); common_log(LOG_DEBUG, "Deleting rsvp from notice...");
$rsvp = RSVP::fromNotice($notice); try {
common_log(LOG_DEBUG, "to delete: $rsvp->id"); $rsvp = RSVP::fromStored($stored);
$rsvp->delete(); $rsvp->delete();
} catch (NoResultException $e) {
// already gone
}
break; break;
default:
common_log(LOG_DEBUG, "Not deleting related, wtf...");
} }
} }
@ -442,9 +410,9 @@ class EventPlugin extends ActivityVerbHandlerPlugin
protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null) protected function showRSVP(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
{ {
$rsvp = RSVP::fromNotice($stored); try {
$rsvp = RSVP::fromStored($stored);
if (empty($rsvp)) { } catch (NoResultException $e) {
// TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond"). // TRANS: Content for a deleted RSVP list item (RSVP stands for "please respond").
$out->element('p', null, _m('Deleted.')); $out->element('p', null, _m('Deleted.'));
return; return;

View File

@ -54,7 +54,12 @@ class RsvpAction extends FormAction
protected function doPreparation() protected function doPreparation()
{ {
if ($this->trimmed('notice')) {
$stored = Notice::getByID($this->trimmed('notice'));
$this->event = Happening::fromStored($stored);
} else {
$this->event = Happening::getByKeys(['uri'=>$this->trimmed('event')]); $this->event = Happening::getByKeys(['uri'=>$this->trimmed('event')]);
}
$this->formOpts['event'] = $this->event; $this->formOpts['event'] = $this->event;
} }
@ -86,6 +91,9 @@ class RsvpAction extends FormAction
$act->objects = array(clone($act->target)); $act->objects = array(clone($act->target));
$act->content = RSVP::toHTML($this->scoped, $this->event, RSVP::codeFor($verb)); $act->content = RSVP::toHTML($this->scoped, $this->event, RSVP::codeFor($verb));
$act->context = new ActivityContext();
$act->context->replyToID = $this->event->getUri();
$stored = Notice::saveActivity($act, $this->scoped, $options); $stored = Notice::saveActivity($act, $this->scoped, $options);
return _m('Saved RSVP'); return _m('Saved RSVP');

View File

@ -211,6 +211,9 @@ class Happening extends Managed_DataObject
static function fromStored(Notice $stored) static function fromStored(Notice $stored)
{ {
if (!ActivityUtils::compareTypes($stored->getObjectType(), [self::OBJECT_TYPE])) {
throw new ServerException('Notice is not of type '.self::OBJECT_TYPE);
}
return self::getByKeys(array('uri'=>$stored->getUri())); return self::getByKeys(array('uri'=>$stored->getUri()));
} }
@ -224,4 +227,43 @@ class Happening extends Managed_DataObject
return RSVP::pkeyGet(array('profile_id' => $profile->getID(), return RSVP::pkeyGet(array('profile_id' => $profile->getID(),
'event_uri' => $this->getUri())); 'event_uri' => $this->getUri()));
} }
static public function getObjectType()
{
return self::OBJECT_TYPE;
}
public function asActivityObject()
{
$actobj = new ActivityObject();
$actobj->id = $this->getUri();
$actobj->type = self::getObjectType();
$actobj->title = $this->title;
$actobj->summary = $this->description;
$actobj->extra[] = array('dtstart',
array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
common_date_iso8601($this->start_time));
$actobj->extra[] = array('dtend',
array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
common_date_iso8601($this->end_time));
$actobj->extra[] = array('location',
array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
$this->location);
$actobj->extra[] = array('url',
array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
$this->getUrl());
/* We don't use these ourselves, but we add them to be nice RSS/XML citizens */
$actobj->extra[] = array('startdate',
array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
common_date_iso8601($this->start_time));
$actobj->extra[] = array('enddate',
array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
common_date_iso8601($this->end_time));
$actobj->extra[] = array('location',
array('xmlns' => 'http://purl.org/rss/1.0/modules/event/'),
$this->location);
return $actobj;
}
} }

View File

@ -128,6 +128,7 @@ class RSVP extends Managed_DataObject
static function saveActivityObject(Activity $act, Notice $stored) static function saveActivityObject(Activity $act, Notice $stored)
{ {
$target = Notice::getByKeys(array('uri'=>$act->target->id)); $target = Notice::getByKeys(array('uri'=>$act->target->id));
common_debug(_ve('TARGET: '.$target));
if (!ActivityUtils::compareTypes($target->getObjectType(), [ Happening::OBJECT_TYPE ])) { if (!ActivityUtils::compareTypes($target->getObjectType(), [ Happening::OBJECT_TYPE ])) {
throw new ClientException('RSVP not aimed at a Happening'); throw new ClientException('RSVP not aimed at a Happening');
} }
@ -159,6 +160,25 @@ class RSVP extends Managed_DataObject
return $rsvp; return $rsvp;
} }
static public function getObjectType()
{
return ActivityObject::ACTIVITY;
}
public function asActivityObject()
{
$happening = $this->getEvent();
$actobj = new ActivityObject();
$actobj->id = $rsvp->getUri();
$actobj->type = self::getObjectType();
$actobj->title = $this->asString();
$actobj->content = $this->asString();
$actobj->target = array($happening->asActivityObject());
return $actobj;
}
static function codeFor($verb) static function codeFor($verb)
{ {
switch (true) { switch (true) {

View File

@ -97,7 +97,8 @@ class RSVPForm extends Form
// TRANS: Field label on form to RSVP ("please respond") for an event. // TRANS: Field label on form to RSVP ("please respond") for an event.
$this->out->text(_m('RSVP:')); $this->out->text(_m('RSVP:'));
$this->out->hidden('event-id', $this->event->getUri(), 'event'); $this->out->hidden('notice', $this->event->getStored()->getID(), 'event');
$this->out->hidden('event', $this->event->getUri(), 'event'); // not used
$this->out->hidden('rsvp', ''); $this->out->hidden('rsvp', '');
$this->out->elementEnd('fieldset'); $this->out->elementEnd('fieldset');

View File

@ -330,7 +330,7 @@ class Fave extends Managed_DataObject
static public function getObjectType() static public function getObjectType()
{ {
return 'activity'; return ActivityObject::ACTIVITY;
} }
public function asActivityObject(Profile $scoped=null) public function asActivityObject(Profile $scoped=null)