RSVPs seem to be created now, just gotta fix CancelrsvpAction

This commit is contained in:
Mikael Nordfeldth 2016-01-19 01:10:06 +01:00
parent 73992a1ed8
commit 84dda697d6
5 changed files with 98 additions and 256 deletions

View File

@ -173,38 +173,16 @@ class EventPlugin extends ActivityVerbHandlerPlugin
protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
{
if (count($act->objects) !== 1) {
// TRANS: Exception thrown when there are too many activity objects.
throw new Exception(_m('Too many activity objects.'));
}
$actobj = $act->objects[0];
switch (true) {
case ActivityUtils::compareVerbs($stored->getVerb(), [ActivityVerb::POST]):
return Happening::saveActivityObject($act, $stored);
break;
switch ($act->verb) {
case ActivityVerb::POST:
if (!ActivityUtils::compareTypes($actobj->type, array(Happening::OBJECT_TYPE))) {
// TRANS: Exception thrown when event plugin comes across a non-event type object.
throw new Exception(_m('Wrong type for object.'));
}
return Happening::saveActivityObject($actobj, $stored);
case ActivityUtils::compareVerbs($stored->getVerb(), [RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE]):
return RSVP::saveActivityObject($act, $stored);
break;
case RSVP::POSITIVE:
case RSVP::NEGATIVE:
case RSVP::POSSIBLE:
$happening = Happening::getKV('uri', $actobj->id);
if (empty($happening)) {
// FIXME: save the event
// TRANS: Exception thrown when trying to RSVP for an unknown event.
throw new Exception(_m('RSVP for unknown event.'));
}
$object = RSVP::saveNewFromNotice($stored, $happening, $act->verb);
// Our data model expects this
$stored->object_type = $act->verb;
return $object;
break;
default:
common_log(LOG_ERR, 'Unknown verb for events.');
return NULL;
}
return null;
}
function activityObjectFromNotice(Notice $stored)

View File

@ -28,11 +28,7 @@
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); }
/**
* RSVP for an event
@ -44,166 +40,53 @@ if (!defined('STATUSNET')) {
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
class NewrsvpAction extends Action
class NewrsvpAction extends FormAction
{
protected $user = null;
protected $event = null;
protected $verb = null;
protected $form = 'RSVP';
protected $event;
/**
* Returns the title of the action
*
* @return string Action title
*/
function title()
{
// TRANS: Title for RSVP ("please respond") action.
return _m('TITLE','New RSVP');
}
/**
* For initializing members of the class.
*
* @param array $argarray misc. arguments
*
* @return boolean true
*/
function prepare($argarray)
protected function doPreparation()
{
parent::prepare($argarray);
if ($this->boolean('ajax')) {
GNUsocial::setApi(true); // short error results!
}
$eventId = $this->trimmed('event');
if (empty($eventId)) {
// TRANS: Client exception thrown when referring to a non-existing event.
throw new ClientException(_m('No such event.'));
}
$this->event = Happening::getKV('id', $eventId);
if (empty($this->event)) {
// TRANS: Client exception thrown when referring to a non-existing event.
throw new ClientException(_m('No such event.'));
}
$this->user = common_current_user();
if (empty($this->user)) {
// TRANS: Client exception thrown when trying to RSVP ("please respond") while not logged in.
throw new ClientException(_m('You must be logged in to RSVP for an event.'));
}
common_debug(print_r($this->args, true));
switch (strtolower($this->trimmed('submitvalue'))) {
case 'yes':
$this->verb = RSVP::POSITIVE;
break;
case 'no':
$this->verb = RSVP::NEGATIVE;
break;
case 'maybe':
$this->verb = RSVP::POSSIBLE;
break;
default:
// TRANS: Client exception thrown when using an invalid value for RSVP ("please respond").
throw new ClientException(_m('Unknown submit value.'));
}
return true;
$this->formOpts['event'] = $this->event;
}
/**
* Handler method
*
* @param array $argarray is ignored since it's now passed in in prepare()
*
* @return void
*/
function handle($argarray=null)
protected function doPost()
{
parent::handle($argarray);
if ($this->isPost()) {
$this->newRSVP();
} else {
$this->showPage();
}
$verb = RSVP::verbFor(strtolower($this->trimmed('submitvalue')));
return;
}
$options = array('source' => 'web');
/**
* Add a new event
*
* @return void
*/
function newRSVP()
{
try {
$saved = RSVP::saveNew($this->user->getProfile(),
$this->event,
$this->verb);
} catch (ClientException $ce) {
$this->error = $ce->getMessage();
$this->showPage();
return;
}
$act = new Activity();
$act->id = UUID::gen();
$act->verb = $verb;
$act->time = time();
$act->title = _m('RSVP');
$act->actor = $this->scoped->asActivityObject();
$act->target = $this->event->getStored()->asActivityObject();
$act->objects = array(clone($act->target));
$act->content = RSVP::toHTML($this->scoped, $this->event, RSVP::codeFor($verb));
if ($this->boolean('ajax')) {
$rsvp = RSVP::fromNotice($saved);
$this->startHTML('text/xml;charset=utf-8');
$this->elementStart('head');
// TRANS: Page title after creating an event.
$this->element('title', null, _m('Event saved'));
$this->elementEnd('head');
$this->elementStart('body');
$cancel = new CancelRSVPForm($rsvp, $this);
$cancel->show();
$this->elementEnd('body');
$this->endHTML();
} else {
common_redirect($saved->getUrl(), 303);
}
}
$stored = Notice::saveActivity($act, $this->scoped, $options);
/**
* Show the event form
*
* @return void
*/
function showContent()
{
if (!empty($this->error)) {
$this->element('p', 'error', $this->error);
}
$form = new RSVPForm($this->event, $this);
$form->show();
return;
}
/**
* Return true if read only.
*
* MAY override
*
* @param array $args other arguments
*
* @return boolean is read only action?
*/
function isReadOnly($args)
{
if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
$_SERVER['REQUEST_METHOD'] == 'HEAD') {
return true;
} else {
return false;
}
return _m('Saved RSVP');
}
}

View File

@ -101,12 +101,23 @@ class Happening extends Managed_DataObject
);
}
public static function saveActivityObject(ActivityObject $actobj, Notice $stored)
public static function saveActivityObject(Activity $act, Notice $stored)
{
$other = Happening::getKV('uri', $actobj->id);
if ($other instanceof Happening) {
// TRANS: Client exception thrown when trying to create an event that already exists.
throw new ClientException(_m('Event already exists.'));
if (count($act->objects) !== 1) {
// TRANS: Exception thrown when there are too many activity objects.
throw new Exception(_m('Too many activity objects.'));
}
$actobj = $act->objects[0];
if (!ActivityUtils::compareTypes($actobj->type, [Happening::OBJECT_TYPE])) {
// TRANS: Exception thrown when event plugin comes across a non-event type object.
throw new Exception(_m('Wrong type for object.'));
}
try {
$other = Happening::getByKeys(['uri' => $actobj->id]);
throw AlreadyFulfilledException('Happening already exists.');
} catch (NoResultException $e) {
// alright, let's save this
}
$dtstart = null;

View File

@ -125,42 +125,18 @@ class RSVP extends Managed_DataObject
print "Resuming core schema upgrade...";
}
function saveNew($profile, $event, $verb, $options=array())
static function saveActivityObject(Activity $act, Notice $stored)
{
$eventNotice = $event->getStored();
$options = array_merge(array('source' => 'web'), $options);
$act = new Activity();
$act->type = ActivityObject::ACTIVITY;
$act->verb = $verb;
$act->time = $options['created'] ? strtotime($options['created']) : time();
$act->title = _m("RSVP");
$act->actor = $profile->asActivityObject();
$act->target = $eventNotice->asActivityObject();
$act->objects = array(clone($act->target));
$act->content = RSVP::toHTML($profile, $event, self::codeFor($verb));
$act->id = common_local_url('showrsvp', array('id' => UUID::gen()));
$act->link = $act->id;
$saved = Notice::saveActivity($act, $profile, $options);
return $saved;
}
function saveNewFromNotice($notice, $event, $verb)
{
$other = RSVP::getKV('uri', $notice->uri);
if (!empty($other)) {
// TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
throw new ClientException(_m('RSVP already exists.'));
$target = Notice::getByKeys(array('uri'=>$act->target->id));
if (!ActivityUtils::compareTypes($target->getObjectType(), [ Happening::OBJECT_TYPE ])) {
throw new ClientException('RSVP not aimed at a Happening');
}
$profile = $notice->getProfile();
// FIXME: Maybe we need some permission handling here, though I think it's taken care of in saveActivity?
try {
$other = RSVP::getByKeys( [ 'profile_id' => $profile->getID(),
'event_uri' => $event->getUri(),
$other = RSVP::getByKeys( [ 'profile_id' => $stored->getProfile()->getID(),
'event_uri' => $target->getUri(),
] );
// TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
throw new AlreadyFulfilledException(_m('RSVP already exists.'));
@ -169,32 +145,30 @@ class RSVP extends Managed_DataObject
}
$rsvp = new RSVP();
preg_match('/\/([^\/]+)\/*/', $notice->uri, $match);
$rsvp->id = $match[1] ? $match[1] : UUID::gen();
$rsvp->profile_id = $profile->id;
$rsvp->event_id = $event->id;
$rsvp->response = self::codeFor($verb);
$rsvp->created = $notice->created;
$rsvp->uri = $notice->uri;
$rsvp->id = UUID::gen(); // remove this
$rsvp->uri = $stored->getUri();
$rsvp->profile_id = $stored->getProfile()->getID();
$rsvp->event_uri = $target->getUri();
$rsvp->response = self::codeFor($stored->getVerb());
$rsvp->created = $stored->getCreated();
$rsvp->insert();
self::blow('rsvp:for-event:%s', $event->getUri());
self::blow('rsvp:for-event:%s', $target->getUri());
return $rsvp;
}
function codeFor($verb)
static function codeFor($verb)
{
switch ($verb) {
case RSVP::POSITIVE:
switch (true) {
case ActivityUtils::compareVerbs($verb, [RSVP::POSITIVE]):
return 'Y';
break;
case RSVP::NEGATIVE:
case ActivityUtils::compareVerbs($verb, [RSVP::NEGATIVE]):
return 'N';
break;
case RSVP::POSSIBLE:
case ActivityUtils::compareVerbs($verb, [RSVP::POSSIBLE]):
return '?';
break;
default:
@ -207,39 +181,41 @@ class RSVP extends Managed_DataObject
{
switch ($code) {
case 'Y':
case 'yes':
return RSVP::POSITIVE;
break;
case 'N':
case 'no':
return RSVP::NEGATIVE;
break;
case '?':
case 'maybe':
return RSVP::POSSIBLE;
break;
default:
// TRANS: Exception thrown when requesting an undefined code for RSVP.
throw new Exception(sprintf(_m('Unknown code "%s".'),$code));
throw new ClientException(sprintf(_m('Unknown code "%s".'), $code));
}
}
function getNotice()
public function getUri()
{
$notice = Notice::getKV('uri', $this->uri);
if (empty($notice)) {
// TRANS: Server exception thrown when requesting a non-exsting notice for an RSVP ("please respond").
// TRANS: %s is the RSVP with the missing notice.
throw new ServerException(sprintf(_m('RSVP %s does not correspond to a notice in the database.'),$this->id));
}
return $notice;
return $this->uri;
}
static function fromNotice(Notice $notice)
public function getEventUri()
{
$rsvp = new RSVP();
$rsvp->uri = $notice->getUri();
if (!$rsvp->find(true)) {
throw new NoResultException($rsvp);
}
return $rsvp;
return $this->event_uri;
}
static function getStored()
{
return Notice::getByKeys(['uri' => $this->getUri()]);
}
static function fromStored(Notice $stored)
{
return self::getByKeys(['uri' => $stored->getUri()]);
}
static function forEvent(Happening $event)
@ -285,24 +261,12 @@ class RSVP extends Managed_DataObject
function getProfile()
{
$profile = Profile::getKV('id', $this->profile_id);
if (empty($profile)) {
// TRANS: Exception thrown when requesting a non-existing profile.
// TRANS: %s is the ID of the non-existing profile.
throw new Exception(sprintf(_m('No profile with ID %s.'),$this->profile_id));
}
return $profile;
return Profile::getByID($this->profile_id);
}
function getEvent()
{
$event = Happening::getKV('uri', $this->event_uri);
if (empty($event)) {
// TRANS: Exception thrown when requesting a non-existing event.
// TRANS: %s is the ID of the non-existing event.
throw new Exception(sprintf(_m('No event with URI %s.'),$this->event_uri));
}
return $event;
return Happening::getByKeys(['uri' => $this->getEventUri()]);
}
function asHTML()
@ -319,7 +283,7 @@ class RSVP extends Managed_DataObject
$this->response);
}
static function toHTML($profile, $event, $response)
static function toHTML(Profile $profile, Happening $event, $response)
{
$fmt = null;
@ -359,7 +323,7 @@ class RSVP extends Managed_DataObject
}
return sprintf($fmt,
htmlspecialchars($profile->profileurl),
htmlspecialchars($profile->getUrl()),
htmlspecialchars($profile->getBestName()),
htmlspecialchars($eventUrl),
htmlspecialchars($eventTitle));
@ -405,9 +369,19 @@ class RSVP extends Managed_DataObject
$eventTitle);
}
function delete($useWhere=false)
public function delete($useWhere=false)
{
self::blow('rsvp:for-event:%s', $this->id);
return parent::delete($useWhere);
}
public function insert()
{
$result = parent::insert();
if ($result === false) {
common_log_db_error($this, 'INSERT', __FILE__);
throw new ServerException(_('Failed to insert '._ve(get_called_class()).' into database'));
}
return $result;
}
}

View File

@ -28,11 +28,7 @@
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); }
/**
* A form to RSVP for an event
@ -48,7 +44,7 @@ class RSVPForm extends Form
{
protected $event = null;
function __construct($event, $out=null)
function __construct(Happening $event, $out=null)
{
parent::__construct($out);
$this->event = $event;