RSVP.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. /**
  3. * Data class for event RSVPs
  4. *
  5. * PHP version 5
  6. *
  7. * @category Data
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  11. * @link http://status.net/
  12. *
  13. * StatusNet - the distributed open-source microblogging tool
  14. * Copyright (C) 2011, StatusNet, Inc.
  15. *
  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.
  20. *
  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.
  25. *
  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/>.
  28. */
  29. if (!defined('GNUSOCIAL')) { exit(1); }
  30. /**
  31. * Data class for event RSVPs
  32. *
  33. * @category Event
  34. * @package StatusNet
  35. * @author Evan Prodromou <evan@status.net>
  36. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  37. * @link http://status.net/
  38. *
  39. * @see Managed_DataObject
  40. */
  41. class RSVP extends Managed_DataObject
  42. {
  43. const POSITIVE = 'http://activitystrea.ms/schema/1.0/rsvp-yes';
  44. const POSSIBLE = 'http://activitystrea.ms/schema/1.0/rsvp-maybe';
  45. const NEGATIVE = 'http://activitystrea.ms/schema/1.0/rsvp-no';
  46. public $__table = 'rsvp'; // table name
  47. public $id; // varchar(36) UUID
  48. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  49. public $profile_id; // int
  50. public $event_uri; // varchar(191) not 255 because utf8mb4 takes more space
  51. public $response; // tinyint
  52. public $created; // datetime
  53. /**
  54. * The One True Thingy that must be defined and declared.
  55. */
  56. public static function schemaDef()
  57. {
  58. return array(
  59. 'description' => 'Plan to attend event',
  60. 'fields' => array(
  61. 'id' => array('type' => 'char',
  62. 'length' => 36,
  63. 'not null' => true,
  64. 'description' => 'UUID'),
  65. 'uri' => array('type' => 'varchar',
  66. 'length' => 191,
  67. 'not null' => true),
  68. 'profile_id' => array('type' => 'int'),
  69. 'event_uri' => array('type' => 'varchar',
  70. 'length' => 191,
  71. 'not null' => true,
  72. 'description' => 'Event URI'),
  73. 'response' => array('type' => 'char',
  74. 'length' => '1',
  75. 'description' => 'Y, N, or ? for three-state yes, no, maybe'),
  76. 'created' => array('type' => 'datetime',
  77. 'not null' => true),
  78. ),
  79. 'primary key' => array('id'),
  80. 'unique keys' => array(
  81. 'rsvp_uri_key' => array('uri'),
  82. 'rsvp_profile_event_key' => array('profile_id', 'event_uri'),
  83. ),
  84. 'foreign keys' => array(
  85. 'rsvp_event_uri_fkey' => array('happening', array('event_uri' => 'uri')),
  86. 'rsvp_profile_id_fkey' => array('profile', array('profile_id' => 'id'))
  87. ),
  88. 'indexes' => array('rsvp_created_idx' => array('created')),
  89. );
  90. }
  91. static public function beforeSchemaUpdate()
  92. {
  93. $table = strtolower(get_called_class());
  94. $schema = Schema::get();
  95. $schemadef = $schema->getTableDef($table);
  96. // 2015-12-31 RSVPs refer to Happening by event_uri now, not event_id. Let's migrate!
  97. if (isset($schemadef['fields']['event_uri'])) {
  98. // We seem to have already migrated, good!
  99. return;
  100. }
  101. // this is a "normal" upgrade from StatusNet for example
  102. echo "\nFound old $table table, upgrading it to add 'event_uri' field...";
  103. $schemadef['fields']['event_uri'] = array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'Event URI');
  104. $schemadef['fields']['uri']['length'] = 191; // we likely don't have to discover too long keys here
  105. $schema->ensureTable($table, $schemadef);
  106. $rsvp = new RSVP();
  107. $rsvp->find();
  108. while ($rsvp->fetch()) {
  109. $event = Happening::getKV('id', $rsvp->event_id);
  110. if (!$event instanceof Happening) {
  111. $rsvp->delete();
  112. continue;
  113. }
  114. $orig = clone($rsvp);
  115. $rsvp->event_uri = $event->uri;
  116. $rsvp->updateWithKeys($orig);
  117. }
  118. print "DONE.\n";
  119. print "Resuming core schema upgrade...";
  120. }
  121. static function saveActivityObject(Activity $act, Notice $stored)
  122. {
  123. $target = Notice::getByKeys(array('uri'=>$act->target->id));
  124. if (!ActivityUtils::compareTypes($target->getObjectType(), [ Happening::OBJECT_TYPE ])) {
  125. throw new ClientException('RSVP not aimed at a Happening');
  126. }
  127. // FIXME: Maybe we need some permission handling here, though I think it's taken care of in saveActivity?
  128. try {
  129. $other = RSVP::getByKeys( [ 'profile_id' => $stored->getProfile()->getID(),
  130. 'event_uri' => $target->getUri(),
  131. ] );
  132. // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
  133. throw new AlreadyFulfilledException(_m('RSVP already exists.'));
  134. } catch (NoResultException $e) {
  135. // No previous RSVP, so go ahead and add.
  136. }
  137. $rsvp = new RSVP();
  138. $rsvp->id = UUID::gen(); // remove this
  139. $rsvp->uri = $stored->getUri();
  140. $rsvp->profile_id = $stored->getProfile()->getID();
  141. $rsvp->event_uri = $target->getUri();
  142. $rsvp->response = self::codeFor($stored->getVerb());
  143. $rsvp->created = $stored->getCreated();
  144. $rsvp->insert();
  145. self::blow('rsvp:for-event:%s', $target->getUri());
  146. return $rsvp;
  147. }
  148. static public function getObjectType()
  149. {
  150. return ActivityObject::ACTIVITY;
  151. }
  152. public function asActivityObject()
  153. {
  154. $happening = $this->getEvent();
  155. $actobj = new ActivityObject();
  156. $actobj->id = $this->getUri();
  157. $actobj->type = self::getObjectType();
  158. $actobj->title = $this->asString();
  159. $actobj->content = $this->asString();
  160. $actobj->target = array($happening->asActivityObject());
  161. return $actobj;
  162. }
  163. static function codeFor($verb)
  164. {
  165. switch (true) {
  166. case ActivityUtils::compareVerbs($verb, [RSVP::POSITIVE]):
  167. return 'Y';
  168. break;
  169. case ActivityUtils::compareVerbs($verb, [RSVP::NEGATIVE]):
  170. return 'N';
  171. break;
  172. case ActivityUtils::compareVerbs($verb, [RSVP::POSSIBLE]):
  173. return '?';
  174. break;
  175. default:
  176. // TRANS: Exception thrown when requesting an undefined verb for RSVP.
  177. throw new Exception(sprintf(_m('Unknown verb "%s".'),$verb));
  178. }
  179. }
  180. static function verbFor($code)
  181. {
  182. switch ($code) {
  183. case 'Y':
  184. case 'yes':
  185. return RSVP::POSITIVE;
  186. break;
  187. case 'N':
  188. case 'no':
  189. return RSVP::NEGATIVE;
  190. break;
  191. case '?':
  192. case 'maybe':
  193. return RSVP::POSSIBLE;
  194. break;
  195. default:
  196. // TRANS: Exception thrown when requesting an undefined code for RSVP.
  197. throw new ClientException(sprintf(_m('Unknown code "%s".'), $code));
  198. }
  199. }
  200. public function getUri()
  201. {
  202. return $this->uri;
  203. }
  204. public function getEventUri()
  205. {
  206. return $this->event_uri;
  207. }
  208. public function getStored()
  209. {
  210. return Notice::getByKeys(['uri' => $this->getUri()]);
  211. }
  212. static function fromStored(Notice $stored)
  213. {
  214. return self::getByKeys(['uri' => $stored->getUri()]);
  215. }
  216. static function byEventAndActor(Happening $event, Profile $actor)
  217. {
  218. return self::getByKeys(['event_uri' => $event->getUri(),
  219. 'profile_id' => $actor->getID()]);
  220. }
  221. static function forEvent(Happening $event)
  222. {
  223. $keypart = sprintf('rsvp:for-event:%s', $event->getUri());
  224. $idstr = self::cacheGet($keypart);
  225. if ($idstr !== false) {
  226. $ids = explode(',', $idstr);
  227. } else {
  228. $ids = array();
  229. $rsvp = new RSVP();
  230. $rsvp->selectAdd();
  231. $rsvp->selectAdd('id');
  232. $rsvp->event_uri = $event->getUri();
  233. if ($rsvp->find()) {
  234. while ($rsvp->fetch()) {
  235. $ids[] = $rsvp->id;
  236. }
  237. }
  238. self::cacheSet($keypart, implode(',', $ids));
  239. }
  240. $rsvps = array(RSVP::POSITIVE => array(),
  241. RSVP::NEGATIVE => array(),
  242. RSVP::POSSIBLE => array());
  243. foreach ($ids as $id) {
  244. $rsvp = RSVP::getKV('id', $id);
  245. if (!empty($rsvp)) {
  246. $verb = self::verbFor($rsvp->response);
  247. $rsvps[$verb][] = $rsvp;
  248. }
  249. }
  250. return $rsvps;
  251. }
  252. function getProfile()
  253. {
  254. return Profile::getByID($this->profile_id);
  255. }
  256. function getEvent()
  257. {
  258. return Happening::getByKeys(['uri' => $this->getEventUri()]);
  259. }
  260. function asHTML()
  261. {
  262. return self::toHTML($this->getProfile(),
  263. $this->getEvent(),
  264. $this->response);
  265. }
  266. function asString()
  267. {
  268. return self::toString($this->getProfile(),
  269. $this->getEvent(),
  270. $this->response);
  271. }
  272. static function toHTML(Profile $profile, Happening $event, $response)
  273. {
  274. $fmt = null;
  275. switch ($response) {
  276. case 'Y':
  277. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  278. // TRANS: %1$s is a profile URL, %2$s a profile name,
  279. // TRANS: %3$s is an event URL, %4$s an event title.
  280. $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> is attending <a href='%3\$s'>%4\$s</a>.</span>");
  281. break;
  282. case 'N':
  283. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  284. // TRANS: %1$s is a profile URL, %2$s a profile name,
  285. // TRANS: %3$s is an event URL, %4$s an event title.
  286. $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>");
  287. break;
  288. case '?':
  289. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  290. // TRANS: %1$s is a profile URL, %2$s a profile name,
  291. // TRANS: %3$s is an event URL, %4$s an event title.
  292. $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> might attend <a href='%3\$s'>%4\$s</a>.</span>");
  293. break;
  294. default:
  295. // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
  296. // TRANS: %s is the non-existing response code.
  297. throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
  298. }
  299. if (empty($event)) {
  300. $eventUrl = '#';
  301. // TRANS: Used as event title when not event title is available.
  302. // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
  303. $eventTitle = _m('an unknown event');
  304. } else {
  305. $eventUrl = $event->getStored()->getUrl();
  306. $eventTitle = $event->title;
  307. }
  308. return sprintf($fmt,
  309. htmlspecialchars($profile->getUrl()),
  310. htmlspecialchars($profile->getBestName()),
  311. htmlspecialchars($eventUrl),
  312. htmlspecialchars($eventTitle));
  313. }
  314. static function toString($profile, $event, $response)
  315. {
  316. $fmt = null;
  317. switch ($response) {
  318. case 'Y':
  319. // TRANS: Plain text version of an RSVP ("please respond") status for a user.
  320. // TRANS: %1$s is a profile name, %2$s is an event title.
  321. $fmt = _m('%1$s is attending %2$s.');
  322. break;
  323. case 'N':
  324. // TRANS: Plain text version of an RSVP ("please respond") status for a user.
  325. // TRANS: %1$s is a profile name, %2$s is an event title.
  326. $fmt = _m('%1$s is not attending %2$s.');
  327. break;
  328. case '?':
  329. // TRANS: Plain text version of an RSVP ("please respond") status for a user.
  330. // TRANS: %1$s is a profile name, %2$s is an event title.
  331. $fmt = _m('%1$s might attend %2$s.');
  332. break;
  333. default:
  334. // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
  335. // TRANS: %s is the non-existing response code.
  336. throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
  337. break;
  338. }
  339. if (empty($event)) {
  340. // TRANS: Used as event title when not event title is available.
  341. // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
  342. $eventTitle = _m('an unknown event');
  343. } else {
  344. $eventTitle = $event->title;
  345. }
  346. return sprintf($fmt,
  347. $profile->getBestName(),
  348. $eventTitle);
  349. }
  350. public function delete($useWhere=false)
  351. {
  352. self::blow('rsvp:for-event:%s', $this->id);
  353. return parent::delete($useWhere);
  354. }
  355. public function insert()
  356. {
  357. $result = parent::insert();
  358. if ($result === false) {
  359. common_log_db_error($this, 'INSERT', __FILE__);
  360. throw new ServerException(_('Failed to insert '._ve(get_called_class()).' into database'));
  361. }
  362. return $result;
  363. }
  364. }