RSVP.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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('rsvp_event_uri_key' => array('happening', array('event_uri' => 'uri')),
  85. 'rsvp_profile_id__key' => array('profile', array('profile_id' => 'id'))),
  86. 'indexes' => array('rsvp_created_idx' => array('created')),
  87. );
  88. }
  89. static public function beforeSchemaUpdate()
  90. {
  91. $table = strtolower(get_called_class());
  92. $schema = Schema::get();
  93. $schemadef = $schema->getTableDef($table);
  94. // 2015-12-31 RSVPs refer to Happening by event_uri now, not event_id. Let's migrate!
  95. if (isset($schemadef['fields']['event_uri'])) {
  96. // We seem to have already migrated, good!
  97. return;
  98. }
  99. // this is a "normal" upgrade from StatusNet for example
  100. echo "\nFound old $table table, upgrading it to add 'event_uri' field...";
  101. $schemadef['fields']['event_uri'] = array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'Event URI');
  102. $schemadef['fields']['uri']['length'] = 191; // we likely don't have to discover too long keys here
  103. $schema->ensureTable($table, $schemadef);
  104. $rsvp = new RSVP();
  105. $rsvp->find();
  106. while ($rsvp->fetch()) {
  107. $event = Happening::getKV('id', $rsvp->event_id);
  108. if (!$event instanceof Happening) {
  109. $rsvp->delete();
  110. continue;
  111. }
  112. $orig = clone($rsvp);
  113. $rsvp->event_uri = $event->uri;
  114. $rsvp->updateWithKeys($orig);
  115. }
  116. print "DONE.\n";
  117. print "Resuming core schema upgrade...";
  118. }
  119. static function saveActivityObject(Activity $act, Notice $stored)
  120. {
  121. $target = Notice::getByKeys(array('uri'=>$act->target->id));
  122. if (!ActivityUtils::compareTypes($target->getObjectType(), [ Happening::OBJECT_TYPE ])) {
  123. throw new ClientException('RSVP not aimed at a Happening');
  124. }
  125. // FIXME: Maybe we need some permission handling here, though I think it's taken care of in saveActivity?
  126. try {
  127. $other = RSVP::getByKeys( [ 'profile_id' => $stored->getProfile()->getID(),
  128. 'event_uri' => $target->getUri(),
  129. ] );
  130. // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
  131. throw new AlreadyFulfilledException(_m('RSVP already exists.'));
  132. } catch (NoResultException $e) {
  133. // No previous RSVP, so go ahead and add.
  134. }
  135. $rsvp = new RSVP();
  136. $rsvp->id = UUID::gen(); // remove this
  137. $rsvp->uri = $stored->getUri();
  138. $rsvp->profile_id = $stored->getProfile()->getID();
  139. $rsvp->event_uri = $target->getUri();
  140. $rsvp->response = self::codeFor($stored->getVerb());
  141. $rsvp->created = $stored->getCreated();
  142. $rsvp->insert();
  143. self::blow('rsvp:for-event:%s', $target->getUri());
  144. return $rsvp;
  145. }
  146. static public function getObjectType()
  147. {
  148. return ActivityObject::ACTIVITY;
  149. }
  150. public function asActivityObject()
  151. {
  152. $happening = $this->getEvent();
  153. $actobj = new ActivityObject();
  154. $actobj->id = $this->getUri();
  155. $actobj->type = self::getObjectType();
  156. $actobj->title = $this->asString();
  157. $actobj->content = $this->asString();
  158. $actobj->target = array($happening->asActivityObject());
  159. return $actobj;
  160. }
  161. static function codeFor($verb)
  162. {
  163. switch (true) {
  164. case ActivityUtils::compareVerbs($verb, [RSVP::POSITIVE]):
  165. return 'Y';
  166. break;
  167. case ActivityUtils::compareVerbs($verb, [RSVP::NEGATIVE]):
  168. return 'N';
  169. break;
  170. case ActivityUtils::compareVerbs($verb, [RSVP::POSSIBLE]):
  171. return '?';
  172. break;
  173. default:
  174. // TRANS: Exception thrown when requesting an undefined verb for RSVP.
  175. throw new Exception(sprintf(_m('Unknown verb "%s".'),$verb));
  176. }
  177. }
  178. static function verbFor($code)
  179. {
  180. switch ($code) {
  181. case 'Y':
  182. case 'yes':
  183. return RSVP::POSITIVE;
  184. break;
  185. case 'N':
  186. case 'no':
  187. return RSVP::NEGATIVE;
  188. break;
  189. case '?':
  190. case 'maybe':
  191. return RSVP::POSSIBLE;
  192. break;
  193. default:
  194. // TRANS: Exception thrown when requesting an undefined code for RSVP.
  195. throw new ClientException(sprintf(_m('Unknown code "%s".'), $code));
  196. }
  197. }
  198. public function getUri()
  199. {
  200. return $this->uri;
  201. }
  202. public function getEventUri()
  203. {
  204. return $this->event_uri;
  205. }
  206. public function getStored()
  207. {
  208. return Notice::getByKeys(['uri' => $this->getUri()]);
  209. }
  210. static function fromStored(Notice $stored)
  211. {
  212. return self::getByKeys(['uri' => $stored->getUri()]);
  213. }
  214. static function byEventAndActor(Happening $event, Profile $actor)
  215. {
  216. return self::getByKeys(['event_uri' => $event->getUri(),
  217. 'profile_id' => $actor->getID()]);
  218. }
  219. static function forEvent(Happening $event)
  220. {
  221. $keypart = sprintf('rsvp:for-event:%s', $event->getUri());
  222. $idstr = self::cacheGet($keypart);
  223. if ($idstr !== false) {
  224. $ids = explode(',', $idstr);
  225. } else {
  226. $ids = array();
  227. $rsvp = new RSVP();
  228. $rsvp->selectAdd();
  229. $rsvp->selectAdd('id');
  230. $rsvp->event_uri = $event->getUri();
  231. if ($rsvp->find()) {
  232. while ($rsvp->fetch()) {
  233. $ids[] = $rsvp->id;
  234. }
  235. }
  236. self::cacheSet($keypart, implode(',', $ids));
  237. }
  238. $rsvps = array(RSVP::POSITIVE => array(),
  239. RSVP::NEGATIVE => array(),
  240. RSVP::POSSIBLE => array());
  241. foreach ($ids as $id) {
  242. $rsvp = RSVP::getKV('id', $id);
  243. if (!empty($rsvp)) {
  244. $verb = self::verbFor($rsvp->response);
  245. $rsvps[$verb][] = $rsvp;
  246. }
  247. }
  248. return $rsvps;
  249. }
  250. function getProfile()
  251. {
  252. return Profile::getByID($this->profile_id);
  253. }
  254. function getEvent()
  255. {
  256. return Happening::getByKeys(['uri' => $this->getEventUri()]);
  257. }
  258. function asHTML()
  259. {
  260. return self::toHTML($this->getProfile(),
  261. $this->getEvent(),
  262. $this->response);
  263. }
  264. function asString()
  265. {
  266. return self::toString($this->getProfile(),
  267. $this->getEvent(),
  268. $this->response);
  269. }
  270. static function toHTML(Profile $profile, Happening $event, $response)
  271. {
  272. $fmt = null;
  273. switch ($response) {
  274. case 'Y':
  275. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  276. // TRANS: %1$s is a profile URL, %2$s a profile name,
  277. // TRANS: %3$s is an event URL, %4$s an event title.
  278. $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> is attending <a href='%3\$s'>%4\$s</a>.</span>");
  279. break;
  280. case 'N':
  281. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  282. // TRANS: %1$s is a profile URL, %2$s a profile name,
  283. // TRANS: %3$s is an event URL, %4$s an event title.
  284. $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>");
  285. break;
  286. case '?':
  287. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  288. // TRANS: %1$s is a profile URL, %2$s a profile name,
  289. // TRANS: %3$s is an event URL, %4$s an event title.
  290. $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> might attend <a href='%3\$s'>%4\$s</a>.</span>");
  291. break;
  292. default:
  293. // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
  294. // TRANS: %s is the non-existing response code.
  295. throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
  296. }
  297. if (empty($event)) {
  298. $eventUrl = '#';
  299. // TRANS: Used as event title when not event title is available.
  300. // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
  301. $eventTitle = _m('an unknown event');
  302. } else {
  303. $eventUrl = $event->getStored()->getUrl();
  304. $eventTitle = $event->title;
  305. }
  306. return sprintf($fmt,
  307. htmlspecialchars($profile->getUrl()),
  308. htmlspecialchars($profile->getBestName()),
  309. htmlspecialchars($eventUrl),
  310. htmlspecialchars($eventTitle));
  311. }
  312. static function toString($profile, $event, $response)
  313. {
  314. $fmt = null;
  315. switch ($response) {
  316. case 'Y':
  317. // TRANS: Plain text version of an RSVP ("please respond") status for a user.
  318. // TRANS: %1$s is a profile name, %2$s is an event title.
  319. $fmt = _m('%1$s is attending %2$s.');
  320. break;
  321. case 'N':
  322. // TRANS: Plain text version of an RSVP ("please respond") status for a user.
  323. // TRANS: %1$s is a profile name, %2$s is an event title.
  324. $fmt = _m('%1$s is not attending %2$s.');
  325. break;
  326. case '?':
  327. // TRANS: Plain text version of an RSVP ("please respond") status for a user.
  328. // TRANS: %1$s is a profile name, %2$s is an event title.
  329. $fmt = _m('%1$s might attend %2$s.');
  330. break;
  331. default:
  332. // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
  333. // TRANS: %s is the non-existing response code.
  334. throw new Exception(sprintf(_m('Unknown response code %s.'),$response));
  335. break;
  336. }
  337. if (empty($event)) {
  338. // TRANS: Used as event title when not event title is available.
  339. // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
  340. $eventTitle = _m('an unknown event');
  341. } else {
  342. $eventTitle = $event->title;
  343. }
  344. return sprintf($fmt,
  345. $profile->getBestName(),
  346. $eventTitle);
  347. }
  348. public function delete($useWhere=false)
  349. {
  350. self::blow('rsvp:for-event:%s', $this->id);
  351. return parent::delete($useWhere);
  352. }
  353. public function insert()
  354. {
  355. $result = parent::insert();
  356. if ($result === false) {
  357. common_log_db_error($this, 'INSERT', __FILE__);
  358. throw new ServerException(_('Failed to insert '._ve(get_called_class()).' into database'));
  359. }
  360. return $result;
  361. }
  362. }