X-Git-Url: https://git.mxchange.org/?p=quix0rs-gnu-social.git;a=blobdiff_plain;f=plugins%2FEvent%2Fclasses%2FRSVP.php;h=0c39022b140433ccfba4a2cba5b940386a0f8bb4;hp=c9504bf601cfafac25576f444a9939215207010a;hb=d6b28c64830f632bb2f4b6f3c9369b9e56ad217a;hpb=77d780a59e45701839f77a7d15996c90a07b2bd5 diff --git a/plugins/Event/classes/RSVP.php b/plugins/Event/classes/RSVP.php index c9504bf601..0c39022b14 100644 --- a/plugins/Event/classes/RSVP.php +++ b/plugins/Event/classes/RSVP.php @@ -27,9 +27,7 @@ * along with this program. If not, see . */ -if (!defined('STATUSNET')) { - exit(1); -} +if (!defined('GNUSOCIAL')) { exit(1); } /** * Data class for event RSVPs @@ -50,26 +48,12 @@ class RSVP extends Managed_DataObject public $__table = 'rsvp'; // table name public $id; // varchar(36) UUID - public $uri; // varchar(255) + public $uri; // varchar(191) not 255 because utf8mb4 takes more space public $profile_id; // int - public $event_id; // varchar(36) UUID + public $event_uri; // varchar(191) not 255 because utf8mb4 takes more space public $response; // tinyint public $created; // datetime - /** - * Add the compound profile_id/event_id index to our cache keys - * since the DB_DataObject stuff doesn't understand compound keys - * except for the primary. - * - * @return array - */ - function _allCacheKeys() { - $keys = parent::_allCacheKeys(); - $keys[] = self::multicacheKey('RSVP', array('profile_id' => $this->profile_id, - 'event_id' => $this->event_id)); - return $keys; - } - /** * The One True Thingy that must be defined and declared. */ @@ -83,13 +67,13 @@ class RSVP extends Managed_DataObject 'not null' => true, 'description' => 'UUID'), 'uri' => array('type' => 'varchar', - 'length' => 255, + 'length' => 191, 'not null' => true), 'profile_id' => array('type' => 'int'), - 'event_id' => array('type' => 'char', - 'length' => 36, + 'event_uri' => array('type' => 'varchar', + 'length' => 191, 'not null' => true, - 'description' => 'UUID'), + 'description' => 'Event URI'), 'response' => array('type' => 'char', 'length' => '1', 'description' => 'Y, N, or ? for three-state yes, no, maybe'), @@ -99,82 +83,105 @@ class RSVP extends Managed_DataObject 'primary key' => array('id'), 'unique keys' => array( 'rsvp_uri_key' => array('uri'), - 'rsvp_profile_event_key' => array('profile_id', 'event_id'), + 'rsvp_profile_event_key' => array('profile_id', 'event_uri'), ), - 'foreign keys' => array('rsvp_event_id_key' => array('event', array('event_id' => 'id')), + 'foreign keys' => array('rsvp_event_uri_key' => array('happening', array('event_uri' => 'uri')), 'rsvp_profile_id__key' => array('profile', array('profile_id' => 'id'))), 'indexes' => array('rsvp_created_idx' => array('created')), ); } - function saveNew(Profile $profile, $event, $verb, array $options = array()) + static public function beforeSchemaUpdate() { - if (array_key_exists('uri', $options)) { - $other = RSVP::getKV('uri', $options['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.')); - } + $table = strtolower(get_called_class()); + $schema = Schema::get(); + $schemadef = $schema->getTableDef($table); + + // 2015-12-31 RSVPs refer to Happening by event_uri now, not event_id. Let's migrate! + if (isset($schemadef['fields']['event_uri'])) { + // We seem to have already migrated, good! + return; } - $other = RSVP::pkeyGet(array('profile_id' => $profile->id, - 'event_id' => $event->id)); + // this is a "normal" upgrade from StatusNet for example + echo "\nFound old $table table, upgrading it to add 'event_uri' field..."; - if (!empty($other)) { - // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond"). - throw new ClientException(_m('RSVP already exists.')); - } + $schemadef['fields']['event_uri'] = array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'Event URI'); + $schema->ensureTable($table, $schemadef); $rsvp = new RSVP(); - - $rsvp->id = UUID::gen(); - $rsvp->profile_id = $profile->id; - $rsvp->event_id = $event->id; - $rsvp->response = self::codeFor($verb); - - if (array_key_exists('created', $options)) { - $rsvp->created = $options['created']; - } else { - $rsvp->created = common_sql_now(); + $rsvp->find(); + while ($rsvp->fetch()) { + $event = Happening::getKV('id', $rsvp->event_id); + if (!$event instanceof Happening) { + continue; + } + $orig = clone($rsvp); + $rsvp->event_uri = $event->uri; + $rsvp->updateWithKeys($orig); } + print "DONE.\n"; + print "Resuming core schema upgrade..."; + } - if (array_key_exists('uri', $options)) { - $rsvp->uri = $options['uri']; - } else { - $rsvp->uri = common_local_url('showrsvp', - array('id' => $rsvp->id)); - } + function saveNew(Profile $profile, $event, $verb, array $options = array()) + { + $eventNotice = $event->getNotice(); + $options = array_merge(array('source' => 'web'), $options); - $rsvp->insert(); + $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)); - self::blow('rsvp:for-event:%s', $event->id); + $act->id = common_local_url('showrsvp', array('id' => UUID::gen())); + $act->link = $act->id; - // XXX: come up with something sexier + $saved = Notice::saveActivity($act, $profile, $options); - $content = $rsvp->asString(); + return $saved; + } - $rendered = $rsvp->asHTML(); + 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.')); + } - $options = array_merge(array('object_type' => $verb), - $options); + $profile = $notice->getProfile(); - if (!array_key_exists('uri', $options)) { - $options['uri'] = $rsvp->uri; + try { + $other = RSVP::getByKeys( [ 'profile_id' => $profile->getID(), + 'event_uri' => $event->getUri(), + ] ); + // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond"). + throw new AlreadyFulfilledException(_m('RSVP already exists.')); + } catch (NoResultException $e) { + // No previous RSVP, so go ahead and add. } - $eventNotice = $event->getNotice(); + $rsvp = new RSVP(); - if (!empty($eventNotice)) { - $options['reply_to'] = $eventNotice->id; - } + 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; - $saved = Notice::saveNew($profile->id, - $content, - array_key_exists('source', $options) ? - $options['source'] : 'web', - $options); + $rsvp->insert(); - return $saved; + self::blow('rsvp:for-event:%s', $event->getUri()); + + return $rsvp; } function codeFor($verb) @@ -236,7 +243,7 @@ class RSVP extends Managed_DataObject static function forEvent(Happening $event) { - $keypart = sprintf('rsvp:for-event:%s', $event->id); + $keypart = sprintf('rsvp:for-event:%s', $event->getUri()); $idstr = self::cacheGet($keypart); @@ -250,7 +257,7 @@ class RSVP extends Managed_DataObject $rsvp->selectAdd(); $rsvp->selectAdd('id'); - $rsvp->event_id = $event->id; + $rsvp->event_uri = $event->getUri(); if ($rsvp->find()) { while ($rsvp->fetch()) { @@ -288,30 +295,26 @@ class RSVP extends Managed_DataObject function getEvent() { - $event = Happening::getKV('id', $this->event_id); + $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 ID %s.'),$this->event_id)); + throw new Exception(sprintf(_m('No event with URI %s.'),$this->event_uri)); } return $event; } function asHTML() { - $event = Happening::getKV('id', $this->event_id); - return self::toHTML($this->getProfile(), - $event, + $this->getEvent(), $this->response); } function asString() { - $event = Happening::getKV('id', $this->event_id); - return self::toString($this->getProfile(), - $event, + $this->getEvent(), $this->response); }