From: Mikael Nordfeldth Date: Sun, 3 Jan 2016 12:08:34 +0000 (+0100) Subject: Merge branch 'nightly' into singpolyma/gnu-social-events-saveObjectFromActivity X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=95d415257a43d5d4338ce22089463c1820b6bf8b;p=quix0rs-gnu-social.git Merge branch 'nightly' into singpolyma/gnu-social-events-saveObjectFromActivity Conflicts: plugins/Event/EventPlugin.php plugins/Event/classes/RSVP.php I just fixed 'em with magic! --- 95d415257a43d5d4338ce22089463c1820b6bf8b diff --cc plugins/Event/EventPlugin.php index 33f08d69c9,5c4abc41d0..01e6da902c --- a/plugins/Event/EventPlugin.php +++ b/plugins/Event/EventPlugin.php @@@ -112,14 -125,7 +125,11 @@@ class EventPlugin extends MicroAppPlugi } function types() { -- return array(Happening::OBJECT_TYPE, - RSVP::POSITIVE, - RSVP::NEGATIVE, - RSVP::POSSIBLE); ++ return array(Happening::OBJECT_TYPE); + } + + function verbs() { + return array(ActivityVerb::POST, RSVP::POSITIVE, RSVP::NEGATIVE, RSVP::POSSIBLE); @@@ -149,17 -155,53 +159,51 @@@ throw new Exception(_m('Wrong type for object.')); } + $dtstart = $happeningObj->element->getElementsByTagName('dtstart'); + if($dtstart->length == 0) { + // TRANS: Exception thrown when has no start date + throw new Exception(_m('No start date for event.')); + } + + $dtend = $happeningObj->element->getElementsByTagName('dtend'); + if($dtend->length == 0) { + // TRANS: Exception thrown when has no end date + throw new Exception(_m('No end date for event.')); + } + + // convert RFC3339 dates delivered in Activity Stream to MySQL DATETIME date format + $start_time = new DateTime($dtstart->item(0)->nodeValue); + $start_time->setTimezone(new DateTimeZone('UTC')); + $start_time = $start_time->format('Y-m-d H:i:s'); + $end_time = new DateTime($dtend->item(0)->nodeValue); + $end_time->setTimezone(new DateTimeZone('UTC')); + $end_time = $end_time->format('Y-m-d H:i:s'); + + // location is optional + $location = null; + $location_object = $happeningObj->element->getElementsByTagName('location'); + if($location_object->length > 0) { + $location = $location_object->item(0)->nodeValue; + } + + // url is optional + $url = null; + $url_object = $happeningObj->element->getElementsByTagName('url'); + if($url_object->length > 0) { + $url = $url_object->item(0)->nodeValue; + } + - $notice = null; - switch ($activity->verb) { case ActivityVerb::POST: - // FIXME: get startTime, endTime, location and URL - return Happening::saveNew($actor, - $start_time, - $end_time, - $happeningObj->title, - null, - $happeningObj->summary, - null, - $options); + // FIXME: get startTime, endTime, location and URL + $notice = Happening::saveNew($actor, + $start_time, + $end_time, + $happeningObj->title, + $location, + $happeningObj->summary, + $url, + $options); break; case RSVP::POSITIVE: case RSVP::NEGATIVE: diff --cc plugins/Event/classes/RSVP.php index cc9dbfd6c9,520347eeb6..5c74bfee4f --- a/plugins/Event/classes/RSVP.php +++ b/plugins/Event/classes/RSVP.php @@@ -107,45 -91,57 +91,80 @@@ class RSVP extends Managed_DataObjec ); } + 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)) { - $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.')); - } + $eventNotice = $event->getNotice(); + $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.')); } + $profile = $notice->getProfile(); + - $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(); @@@ -160,9 -166,34 +179,9 @@@ $rsvp->insert(); - self::blow('rsvp:for-event:%s', $event->id); + self::blow('rsvp:for-event:%s', $event->getUri()); - // XXX: come up with something sexier - - $content = $rsvp->asString(); - - $rendered = $rsvp->asHTML(); - - $options = array_merge(array('object_type' => $verb), - $options); - - if (!array_key_exists('uri', $options)) { - $options['uri'] = $rsvp->uri; - } - - $eventNotice = $event->getNotice(); - - if (!empty($eventNotice)) { - $options['reply_to'] = $eventNotice->getID(); - } - - $saved = Notice::saveNew($profile->getID(), - $content, - array_key_exists('source', $options) ? - $options['source'] : 'web', - $options); - - return $saved; + return $rsvp; } function codeFor($verb)