From 4fc2b2584bab156fefbfcb1eaea4eb376b89b623 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Thu, 31 Dec 2015 19:23:05 +0100 Subject: [PATCH] RSVPs refer to Happening (event) by URI instead of ID now --- plugins/Event/EventPlugin.php | 6 ++ plugins/Event/actions/cancelrsvp.php | 2 +- plugins/Event/classes/Happening.php | 2 +- plugins/Event/classes/RSVP.php | 101 +++++++++++++---------- plugins/Event/lib/eventsnoticestream.php | 47 ++++++++--- 5 files changed, 101 insertions(+), 57 deletions(-) diff --git a/plugins/Event/EventPlugin.php b/plugins/Event/EventPlugin.php index 0c520ddb7a..5c4abc41d0 100644 --- a/plugins/Event/EventPlugin.php +++ b/plugins/Event/EventPlugin.php @@ -67,6 +67,12 @@ class EventPlugin extends MicroAppPlugin return true; } + public function onBeforePluginCheckSchema() + { + RSVP::beforeSchemaUpdate(); + return true; + } + /** * Map URLs to actions * diff --git a/plugins/Event/actions/cancelrsvp.php b/plugins/Event/actions/cancelrsvp.php index 83f6a73ae0..662a1de0b3 100644 --- a/plugins/Event/actions/cancelrsvp.php +++ b/plugins/Event/actions/cancelrsvp.php @@ -89,7 +89,7 @@ class CancelrsvpAction extends Action throw new ClientException(_m('No such RSVP.')); } - $this->event = Happening::getKV('id', $this->rsvp->event_id); + $this->event = Happening::getKV('uri', $this->rsvp->event_uri); if (empty($this->event)) { // TRANS: Client exception thrown when referring to a non-existing event. diff --git a/plugins/Event/classes/Happening.php b/plugins/Event/classes/Happening.php index 06d5b99ab7..9ef288eca4 100644 --- a/plugins/Event/classes/Happening.php +++ b/plugins/Event/classes/Happening.php @@ -223,6 +223,6 @@ class Happening extends Managed_DataObject function getRSVP($profile) { return RSVP::pkeyGet(array('profile_id' => $profile->getID(), - 'event_id' => $this->id)); + 'event_uri' => $this->getUri())); } } diff --git a/plugins/Event/classes/RSVP.php b/plugins/Event/classes/RSVP.php index 3e0efc689a..520347eeb6 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 @@ -52,24 +50,10 @@ class RSVP extends Managed_DataObject public $id; // varchar(36) UUID 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. */ @@ -86,10 +70,10 @@ class RSVP extends Managed_DataObject '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,14 +83,47 @@ 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')), ); } + static public function beforeSchemaUpdate() + { + $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; + } + + // this is a "normal" upgrade from StatusNet for example + echo "\nFound old $table table, upgrading it to add 'event_uri' field..."; + + $schemadef['fields']['event_uri'] = array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'Event URI'); + $schema->ensureTable($table, $schemadef); + + $rsvp = new RSVP(); + $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..."; + } + function saveNew($profile, $event, $verb, $options=array()) { if (array_key_exists('uri', $options)) { @@ -117,19 +134,21 @@ class RSVP extends Managed_DataObject } } - $other = RSVP::pkeyGet(array('profile_id' => $profile->id, - 'event_id' => $event->id)); - - if (!empty($other)) { + 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 ClientException(_m('RSVP already exists.')); + throw new AlreadyFulfilledException(_m('RSVP already exists.')); + } catch (NoResultException $e) { + // No previous RSVP, so go ahead and add. } $rsvp = new RSVP(); $rsvp->id = UUID::gen(); - $rsvp->profile_id = $profile->id; - $rsvp->event_id = $event->id; + $rsvp->profile_id = $profile->getID(); + $rsvp->event_uri = $event->getUri(); $rsvp->response = self::codeFor($verb); if (array_key_exists('created', $options)) { @@ -147,7 +166,7 @@ class RSVP extends Managed_DataObject $rsvp->insert(); - self::blow('rsvp:for-event:%s', $event->id); + self::blow('rsvp:for-event:%s', $event->getUri()); // XXX: come up with something sexier @@ -165,10 +184,10 @@ class RSVP extends Managed_DataObject $eventNotice = $event->getNotice(); if (!empty($eventNotice)) { - $options['reply_to'] = $eventNotice->id; + $options['reply_to'] = $eventNotice->getID(); } - $saved = Notice::saveNew($profile->id, + $saved = Notice::saveNew($profile->getID(), $content, array_key_exists('source', $options) ? $options['source'] : 'web', @@ -236,7 +255,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 +269,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 +307,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); } diff --git a/plugins/Event/lib/eventsnoticestream.php b/plugins/Event/lib/eventsnoticestream.php index 26e18e229f..c0d91060be 100644 --- a/plugins/Event/lib/eventsnoticestream.php +++ b/plugins/Event/lib/eventsnoticestream.php @@ -2,14 +2,6 @@ class RawEventsNoticeStream extends NoticeStream { - protected $target; - protected $own; - - function __construct(Profile $target) - { - $this->target = $target; - } - function getNoticeIds($offset, $limit, $since_id, $max_id) { $notice = new Notice(); @@ -17,7 +9,6 @@ class RawEventsNoticeStream extends NoticeStream $qry = 'SELECT notice.* FROM notice '; $qry .= 'INNER JOIN happening ON happening.uri = notice.uri '; - $qry .= 'WHERE happening.profile_id = ' . $this->target->getID() . ' '; $qry .= 'AND notice.is_local != ' . Notice::GATEWAY . ' '; if ($since_id != 0) { @@ -29,7 +20,7 @@ class RawEventsNoticeStream extends NoticeStream } // NOTE: we sort by event time, not by notice time! - $qry .= 'ORDER BY created DESC '; + $qry .= 'ORDER BY happening.created DESC '; if (!is_null($offset)) { $qry .= "LIMIT $limit OFFSET $offset"; } @@ -48,9 +39,13 @@ class RawEventsNoticeStream extends NoticeStream class EventsNoticeStream extends ScopingNoticeStream { - function __construct(Profile $target, Profile $scoped=null) + // possible values of RSVP in our database + protected $rsvp = ['Y', 'N', '?']; + protected $target = null; + + function __construct(Profile $target, Profile $scoped=null, array $rsvp=array()) { - $stream = new RawEventsNoticeStream($target); + $stream = new RawEventsNoticeStream(); if ($target->sameAs($scoped)) { $key = 'happening:ids_for_user_own:'.$target->getID(); @@ -58,6 +53,34 @@ class EventsNoticeStream extends ScopingNoticeStream $key = 'happening:ids_for_user:'.$target->getID(); } + // Match RSVP against our possible values, given in the class variable + // and if no RSVPs are given is empty, assume we want all events, even + // without RSVPs from this profile. + $this->rsvp = array_intersect($this->rsvp, $rsvp); + $this->target = $target; + parent::__construct(new CachingNoticeStream($stream, $key), $scoped); } + + function filter($notice) + { + if (!parent::filter($notice)) { + // if not in our scope, return false + return false; + } + + if (empty($this->rsvp)) { + // Don't filter on RSVP (for only events with RSVP if no responses + // are given (give ['Y', 'N', '?'] for only RSVP'd events!). + return true; + } + + $rsvp = new RSVP(); + $rsvp->profile_id = $this->target->getID(); + $rsvp->event_uri = $notice->getUri(); + $rsvp->whereAddIn('response', $this->rsvp, $rsvp->columnType('response')); + + // filter out if no RSVP match was found + return $rsvp->N > 0; + } } -- 2.39.5