RSVP.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Data class for event RSVPs
  18. *
  19. * @category Data
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Data class for event RSVPs
  28. *
  29. * @category Event
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. *
  34. * @see Managed_DataObject
  35. */
  36. class RSVP extends Managed_DataObject
  37. {
  38. const POSITIVE = 'http://activitystrea.ms/schema/1.0/rsvp-yes';
  39. const POSSIBLE = 'http://activitystrea.ms/schema/1.0/rsvp-maybe';
  40. const NEGATIVE = 'http://activitystrea.ms/schema/1.0/rsvp-no';
  41. public $__table = 'rsvp'; // table name
  42. public $id; // varchar(36) UUID
  43. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  44. public $profile_id; // int
  45. public $event_uri; // varchar(191) not 255 because utf8mb4 takes more space
  46. public $response; // tinyint
  47. public $created; // datetime
  48. /**
  49. * The One True Thingy that must be defined and declared.
  50. */
  51. public static function schemaDef()
  52. {
  53. return array(
  54. 'description' => 'Plan to attend event',
  55. 'fields' => array(
  56. 'id' => array('type' => 'char',
  57. 'length' => 36,
  58. 'not null' => true,
  59. 'description' => 'UUID'),
  60. 'uri' => array('type' => 'varchar',
  61. 'length' => 191,
  62. 'not null' => true),
  63. 'profile_id' => array('type' => 'int'),
  64. 'event_uri' => array('type' => 'varchar',
  65. 'length' => 191,
  66. 'not null' => true,
  67. 'description' => 'Event URI'),
  68. 'response' => array('type' => 'char',
  69. 'length' => '1',
  70. 'description' => 'Y, N, or ? for three-state yes, no, maybe'),
  71. 'created' => array('type' => 'datetime',
  72. 'not null' => true),
  73. ),
  74. 'primary key' => array('id'),
  75. 'unique keys' => array(
  76. 'rsvp_uri_key' => array('uri'),
  77. 'rsvp_profile_id_event_uri_key' => array('profile_id', 'event_uri'),
  78. ),
  79. 'foreign keys' => array(
  80. 'rsvp_event_uri_fkey' => array('happening', array('event_uri' => 'uri')),
  81. 'rsvp_profile_id_fkey' => array('profile', array('profile_id' => 'id'))
  82. ),
  83. 'indexes' => array(
  84. 'rsvp_event_uri_idx' => array('event_uri'),
  85. 'rsvp_created_idx' => array('created'),
  86. ),
  87. );
  88. }
  89. public static 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. public 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([
  128. 'profile_id' => $stored->getProfile()->getID(),
  129. 'event_uri' => $target->getUri(),
  130. ]);
  131. // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
  132. throw new AlreadyFulfilledException(_m('RSVP already exists.'));
  133. } catch (NoResultException $e) {
  134. // No previous RSVP, so go ahead and add.
  135. }
  136. $rsvp = new RSVP();
  137. $rsvp->id = UUID::gen(); // remove this
  138. $rsvp->uri = $stored->getUri();
  139. $rsvp->profile_id = $stored->getProfile()->getID();
  140. $rsvp->event_uri = $target->getUri();
  141. $rsvp->response = self::codeFor($stored->getVerb());
  142. $rsvp->created = $stored->getCreated();
  143. $rsvp->insert();
  144. self::blow('rsvp:for-event:%s', $target->getUri());
  145. return $rsvp;
  146. }
  147. public static function getObjectType()
  148. {
  149. return ActivityObject::ACTIVITY;
  150. }
  151. public function asActivityObject()
  152. {
  153. $happening = $this->getEvent();
  154. $actobj = new ActivityObject();
  155. $actobj->id = $this->getUri();
  156. $actobj->type = self::getObjectType();
  157. $actobj->title = $this->asString();
  158. $actobj->content = $this->asString();
  159. $actobj->target = array($happening->asActivityObject());
  160. return $actobj;
  161. }
  162. public static function codeFor($verb)
  163. {
  164. switch (true) {
  165. case ActivityUtils::compareVerbs($verb, [RSVP::POSITIVE]):
  166. return 'Y';
  167. break;
  168. case ActivityUtils::compareVerbs($verb, [RSVP::NEGATIVE]):
  169. return 'N';
  170. break;
  171. case ActivityUtils::compareVerbs($verb, [RSVP::POSSIBLE]):
  172. return '?';
  173. break;
  174. default:
  175. // TRANS: Exception thrown when requesting an undefined verb for RSVP.
  176. throw new Exception(sprintf(_m('Unknown verb "%s".'), $verb));
  177. }
  178. }
  179. public static function verbFor($code)
  180. {
  181. switch ($code) {
  182. case 'Y':
  183. case 'yes':
  184. return RSVP::POSITIVE;
  185. break;
  186. case 'N':
  187. case 'no':
  188. return RSVP::NEGATIVE;
  189. break;
  190. case '?':
  191. case 'maybe':
  192. return RSVP::POSSIBLE;
  193. break;
  194. default:
  195. // TRANS: Exception thrown when requesting an undefined code for RSVP.
  196. throw new ClientException(sprintf(_m('Unknown code "%s".'), $code));
  197. }
  198. }
  199. public function getUri()
  200. {
  201. return $this->uri;
  202. }
  203. public function getEventUri()
  204. {
  205. return $this->event_uri;
  206. }
  207. public function getStored()
  208. {
  209. return Notice::getByKeys(['uri' => $this->getUri()]);
  210. }
  211. public static function fromStored(Notice $stored)
  212. {
  213. return self::getByKeys(['uri' => $stored->getUri()]);
  214. }
  215. public static function byEventAndActor(Happening $event, Profile $actor)
  216. {
  217. return self::getByKeys(['event_uri' => $event->getUri(),
  218. 'profile_id' => $actor->getID()]);
  219. }
  220. public static function forEvent(Happening $event)
  221. {
  222. $keypart = sprintf('rsvp:for-event:%s', $event->getUri());
  223. $idstr = self::cacheGet($keypart);
  224. if ($idstr !== false) {
  225. $ids = explode(',', $idstr);
  226. } else {
  227. $ids = array();
  228. $rsvp = new RSVP();
  229. $rsvp->selectAdd();
  230. $rsvp->selectAdd('id');
  231. $rsvp->event_uri = $event->getUri();
  232. if ($rsvp->find()) {
  233. while ($rsvp->fetch()) {
  234. $ids[] = $rsvp->id;
  235. }
  236. }
  237. self::cacheSet($keypart, implode(',', $ids));
  238. }
  239. $rsvps = array(RSVP::POSITIVE => array(),
  240. RSVP::NEGATIVE => array(),
  241. RSVP::POSSIBLE => array());
  242. foreach ($ids as $id) {
  243. $rsvp = RSVP::getKV('id', $id);
  244. if (!empty($rsvp)) {
  245. $verb = self::verbFor($rsvp->response);
  246. $rsvps[$verb][] = $rsvp;
  247. }
  248. }
  249. return $rsvps;
  250. }
  251. public function getProfile()
  252. {
  253. return Profile::getByID($this->profile_id);
  254. }
  255. public function getEvent()
  256. {
  257. return Happening::getByKeys(['uri' => $this->getEventUri()]);
  258. }
  259. public function asHTML()
  260. {
  261. return self::toHTML(
  262. $this->getProfile(),
  263. $this->getEvent(),
  264. $this->response
  265. );
  266. }
  267. public function asString()
  268. {
  269. return self::toString(
  270. $this->getProfile(),
  271. $this->getEvent(),
  272. $this->response
  273. );
  274. }
  275. public static function toHTML(Profile $profile, Happening $event, $response)
  276. {
  277. $fmt = null;
  278. switch ($response) {
  279. case 'Y':
  280. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  281. // TRANS: %1$s is a profile URL, %2$s a profile name,
  282. // TRANS: %3$s is an event URL, %4$s an event title.
  283. $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> is attending <a href='%3\$s'>%4\$s</a>.</span>");
  284. break;
  285. case 'N':
  286. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  287. // TRANS: %1$s is a profile URL, %2$s a profile name,
  288. // TRANS: %3$s is an event URL, %4$s an event title.
  289. $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>");
  290. break;
  291. case '?':
  292. // TRANS: HTML version of an RSVP ("please respond") status for a user.
  293. // TRANS: %1$s is a profile URL, %2$s a profile name,
  294. // TRANS: %3$s is an event URL, %4$s an event title.
  295. $fmt = _m("<span class='automatic event-rsvp'><a href='%1\$s'>%2\$s</a> might attend <a href='%3\$s'>%4\$s</a>.</span>");
  296. break;
  297. default:
  298. // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
  299. // TRANS: %s is the non-existing response code.
  300. throw new Exception(sprintf(_m('Unknown response code %s.'), $response));
  301. }
  302. if (empty($event)) {
  303. $eventUrl = '#';
  304. // TRANS: Used as event title when not event title is available.
  305. // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
  306. $eventTitle = _m('an unknown event');
  307. } else {
  308. $eventUrl = $event->getStored()->getUrl();
  309. $eventTitle = $event->title;
  310. }
  311. return sprintf(
  312. $fmt,
  313. htmlspecialchars($profile->getUrl()),
  314. htmlspecialchars($profile->getBestName()),
  315. htmlspecialchars($eventUrl),
  316. htmlspecialchars($eventTitle)
  317. );
  318. }
  319. public static function toString($profile, $event, $response)
  320. {
  321. $fmt = null;
  322. switch ($response) {
  323. case 'Y':
  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 attending %2$s.');
  327. break;
  328. case 'N':
  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 is not attending %2$s.');
  332. break;
  333. case '?':
  334. // TRANS: Plain text version of an RSVP ("please respond") status for a user.
  335. // TRANS: %1$s is a profile name, %2$s is an event title.
  336. $fmt = _m('%1$s might attend %2$s.');
  337. break;
  338. default:
  339. // TRANS: Exception thrown when requesting a user's RSVP status for a non-existing response code.
  340. // TRANS: %s is the non-existing response code.
  341. throw new Exception(sprintf(_m('Unknown response code %s.'), $response));
  342. break;
  343. }
  344. if (empty($event)) {
  345. // TRANS: Used as event title when not event title is available.
  346. // TRANS: Used as: Username [is [not ] attending|might attend] an unknown event.
  347. $eventTitle = _m('an unknown event');
  348. } else {
  349. $eventTitle = $event->title;
  350. }
  351. return sprintf(
  352. $fmt,
  353. $profile->getBestName(),
  354. $eventTitle
  355. );
  356. }
  357. public function delete($useWhere=false)
  358. {
  359. self::blow('rsvp:for-event:%s', $this->id);
  360. return parent::delete($useWhere);
  361. }
  362. public function insert()
  363. {
  364. $result = parent::insert();
  365. if ($result === false) {
  366. common_log_db_error($this, 'INSERT', __FILE__);
  367. throw new ServerException(_('Failed to insert '._ve(get_called_class()).' into database'));
  368. }
  369. return $result;
  370. }
  371. }