3 * Data class for event RSVPs
9 * @author Evan Prodromou <evan@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2011, StatusNet, Inc.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Affero General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Affero General Public License for more details.
26 * You should have received a copy of the GNU Affero General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 if (!defined('GNUSOCIAL')) { exit(1); }
33 * Data class for event RSVPs
37 * @author Evan Prodromou <evan@status.net>
38 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
39 * @link http://status.net/
41 * @see Managed_DataObject
43 class RSVP extends Managed_DataObject
45 const POSITIVE = 'http://activitystrea.ms/schema/1.0/rsvp-yes';
46 const POSSIBLE = 'http://activitystrea.ms/schema/1.0/rsvp-maybe';
47 const NEGATIVE = 'http://activitystrea.ms/schema/1.0/rsvp-no';
49 public $__table = 'rsvp'; // table name
50 public $id; // varchar(36) UUID
51 public $uri; // varchar(191) not 255 because utf8mb4 takes more space
52 public $profile_id; // int
53 public $event_uri; // varchar(191) not 255 because utf8mb4 takes more space
54 public $response; // tinyint
55 public $created; // datetime
58 * The One True Thingy that must be defined and declared.
60 public static function schemaDef()
63 'description' => 'Plan to attend event',
65 'id' => array('type' => 'char',
68 'description' => 'UUID'),
69 'uri' => array('type' => 'varchar',
72 'profile_id' => array('type' => 'int'),
73 'event_uri' => array('type' => 'varchar',
76 'description' => 'Event URI'),
77 'response' => array('type' => 'char',
79 'description' => 'Y, N, or ? for three-state yes, no, maybe'),
80 'created' => array('type' => 'datetime',
83 'primary key' => array('id'),
84 'unique keys' => array(
85 'rsvp_uri_key' => array('uri'),
86 'rsvp_profile_event_key' => array('profile_id', 'event_uri'),
88 'foreign keys' => array('rsvp_event_uri_key' => array('happening', array('event_uri' => 'uri')),
89 'rsvp_profile_id__key' => array('profile', array('profile_id' => 'id'))),
90 'indexes' => array('rsvp_created_idx' => array('created')),
94 static public function beforeSchemaUpdate()
96 $table = strtolower(get_called_class());
97 $schema = Schema::get();
98 $schemadef = $schema->getTableDef($table);
100 // 2015-12-31 RSVPs refer to Happening by event_uri now, not event_id. Let's migrate!
101 if (isset($schemadef['fields']['event_uri'])) {
102 // We seem to have already migrated, good!
106 // this is a "normal" upgrade from StatusNet for example
107 echo "\nFound old $table table, upgrading it to add 'event_uri' field...";
109 $schemadef['fields']['event_uri'] = array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'Event URI');
110 $schema->ensureTable($table, $schemadef);
114 while ($rsvp->fetch()) {
115 $event = Happening::getKV('id', $rsvp->event_id);
116 if (!$event instanceof Happening) {
119 $orig = clone($rsvp);
120 $rsvp->event_uri = $event->uri;
121 $rsvp->updateWithKeys($orig);
124 print "Resuming core schema upgrade...";
127 function saveNew(Profile $profile, $event, $verb, array $options = array())
129 $eventNotice = $event->getNotice();
130 $options = array_merge(array('source' => 'web'), $options);
132 $act = new Activity();
133 $act->type = ActivityObject::ACTIVITY;
135 $act->time = $options['created'] ? strtotime($options['created']) : time();
136 $act->title = _m("RSVP");
137 $act->actor = $profile->asActivityObject();
138 $act->target = $eventNotice->asActivityObject();
139 $act->objects = array(clone($act->target));
140 $act->content = RSVP::toHTML($profile, $event, self::codeFor($verb));
142 $act->id = common_local_url('showrsvp', array('id' => UUID::gen()));
143 $act->link = $act->id;
145 $saved = Notice::saveActivity($act, $profile, $options);
150 function saveNewFromNotice($notice, $event, $verb)
152 $other = RSVP::getKV('uri', $notice->uri);
153 if (!empty($other)) {
154 // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
155 throw new ClientException(_m('RSVP already exists.'));
158 $profile = $notice->getProfile();
161 $other = RSVP::getByKeys( [ 'profile_id' => $profile->getID(),
162 'event_uri' => $event->getUri(),
164 // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
165 throw new AlreadyFulfilledException(_m('RSVP already exists.'));
166 } catch (NoResultException $e) {
167 // No previous RSVP, so go ahead and add.
172 preg_match('/\/([^\/]+)\/*/', $notice->uri, $match);
173 $rsvp->id = $match[1] ? $match[1] : UUID::gen();
174 $rsvp->profile_id = $profile->id;
175 $rsvp->event_id = $event->id;
176 $rsvp->response = self::codeFor($verb);
177 $rsvp->created = $notice->created;
178 $rsvp->uri = $notice->uri;
182 self::blow('rsvp:for-event:%s', $event->getUri());
187 function codeFor($verb)
200 // TRANS: Exception thrown when requesting an undefined verb for RSVP.
201 throw new Exception(sprintf(_m('Unknown verb "%s".'),$verb));
205 static function verbFor($code)
209 return RSVP::POSITIVE;
212 return RSVP::NEGATIVE;
215 return RSVP::POSSIBLE;
218 // TRANS: Exception thrown when requesting an undefined code for RSVP.
219 throw new Exception(sprintf(_m('Unknown code "%s".'),$code));
225 $notice = Notice::getKV('uri', $this->uri);
226 if (empty($notice)) {
227 // TRANS: Server exception thrown when requesting a non-exsting notice for an RSVP ("please respond").
228 // TRANS: %s is the RSVP with the missing notice.
229 throw new ServerException(sprintf(_m('RSVP %s does not correspond to a notice in the database.'),$this->id));
234 static function fromNotice(Notice $notice)
237 $rsvp->uri = $notice->uri;
238 if (!$rsvp->find(true)) {
239 throw new NoResultException($rsvp);
244 static function forEvent(Happening $event)
246 $keypart = sprintf('rsvp:for-event:%s', $event->getUri());
248 $idstr = self::cacheGet($keypart);
250 if ($idstr !== false) {
251 $ids = explode(',', $idstr);
258 $rsvp->selectAdd('id');
260 $rsvp->event_uri = $event->getUri();
263 while ($rsvp->fetch()) {
267 self::cacheSet($keypart, implode(',', $ids));
270 $rsvps = array(RSVP::POSITIVE => array(),
271 RSVP::NEGATIVE => array(),
272 RSVP::POSSIBLE => array());
274 foreach ($ids as $id) {
275 $rsvp = RSVP::getKV('id', $id);
277 $verb = self::verbFor($rsvp->response);
278 $rsvps[$verb][] = $rsvp;
285 function getProfile()
287 $profile = Profile::getKV('id', $this->profile_id);
288 if (empty($profile)) {
289 // TRANS: Exception thrown when requesting a non-existing profile.
290 // TRANS: %s is the ID of the non-existing profile.
291 throw new Exception(sprintf(_m('No profile with ID %s.'),$this->profile_id));
298 $event = Happening::getKV('uri', $this->event_uri);
300 // TRANS: Exception thrown when requesting a non-existing event.
301 // TRANS: %s is the ID of the non-existing event.
302 throw new Exception(sprintf(_m('No event with URI %s.'),$this->event_uri));
309 return self::toHTML($this->getProfile(),
316 return self::toString($this->getProfile(),
321 static function toHTML(Profile $profile, Event $event, $response)
327 // TRANS: HTML version of an RSVP ("please respond") status for a user.
328 // TRANS: %1$s is a profile URL, %2$s a profile name,
329 // TRANS: %3$s is an event URL, %4$s an event title.
330 $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> is attending <a href='%3\$s'>%4\$s</a>.</span>");
333 // TRANS: HTML version of an RSVP ("please respond") status for a user.
334 // TRANS: %1$s is a profile URL, %2$s a profile name,
335 // TRANS: %3$s is an event URL, %4$s an event title.
336 $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> is not attending <a href='%3\$s'>%4\$s</a>.</span>");
339 // TRANS: HTML version of an RSVP ("please respond") status for a user.
340 // TRANS: %1$s is a profile URL, %2$s a profile name,
341 // TRANS: %3$s is an event URL, %4$s an event title.
342 $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> might attend <a href='%3\$s'>%4\$s</a>.</span>");
345 // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
346 // TRANS: %s is the non-existing response code.
347 throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
352 // TRANS: Used as event title when not event title is available.
353 // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
354 $eventTitle = _m('an unknown event');
356 $notice = $event->getNotice();
357 $eventUrl = $notice->getUrl();
358 $eventTitle = $event->title;
362 htmlspecialchars($profile->profileurl),
363 htmlspecialchars($profile->getBestName()),
364 htmlspecialchars($eventUrl),
365 htmlspecialchars($eventTitle));
368 static function toString($profile, $event, $response)
374 // TRANS: Plain text version of an RSVP ("please respond") status for a user.
375 // TRANS: %1$s is a profile name, %2$s is an event title.
376 $fmt = _m('%1$s is attending %2$s.');
379 // TRANS: Plain text version of an RSVP ("please respond") status for a user.
380 // TRANS: %1$s is a profile name, %2$s is an event title.
381 $fmt = _m('%1$s is not attending %2$s.');
384 // TRANS: Plain text version of an RSVP ("please respond") status for a user.
385 // TRANS: %1$s is a profile name, %2$s is an event title.
386 $fmt = _m('%1$s might attend %2$s.');
389 // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
390 // TRANS: %s is the non-existing response code.
391 throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
396 // TRANS: Used as event title when not event title is available.
397 // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
398 $eventTitle = _m('an unknown event');
400 $notice = $event->getNotice();
401 $eventTitle = $event->title;
405 $profile->getBestName(),
409 function delete($useWhere=false)
411 self::blow('rsvp:for-event:%s', $this->id);
412 return parent::delete($useWhere);