Happening.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 happenings
  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 happenings
  28. *
  29. * There's already an Event class in lib/event.php, so we couldn't
  30. * call this an Event without causing a hole in space-time.
  31. *
  32. * "Happening" seemed good enough.
  33. *
  34. * @category Event
  35. * @package GNUsocial
  36. * @author Evan Prodromou <evan@status.net>
  37. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  38. *
  39. * @see Managed_DataObject
  40. */
  41. class Happening extends Managed_DataObject
  42. {
  43. const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/event';
  44. public $__table = 'happening'; // table name
  45. public $id; // varchar(36) UUID
  46. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  47. public $profile_id; // int
  48. public $start_time; // datetime
  49. public $end_time; // datetime
  50. public $title; // varchar(191) not 255 because utf8mb4 takes more space
  51. public $location; // varchar(191) not 255 because utf8mb4 takes more space
  52. public $url; // varchar(191) not 255 because utf8mb4 takes more space
  53. public $description; // text
  54. public $created; // datetime
  55. /**
  56. * The One True Thingy that must be defined and declared.
  57. */
  58. public static function schemaDef()
  59. {
  60. return array(
  61. 'description' => 'A real-world happening',
  62. 'fields' => array(
  63. 'id' => array('type' => 'char',
  64. 'length' => 36,
  65. 'not null' => true,
  66. 'description' => 'UUID'),
  67. 'uri' => array('type' => 'varchar',
  68. 'length' => 191,
  69. 'not null' => true),
  70. 'profile_id' => array('type' => 'int', 'not null' => true),
  71. 'start_time' => array('type' => 'datetime', 'not null' => true),
  72. 'end_time' => array('type' => 'datetime', 'not null' => true),
  73. 'title' => array('type' => 'varchar',
  74. 'length' => 191,
  75. 'not null' => true),
  76. 'location' => array('type' => 'varchar',
  77. 'length' => 191),
  78. 'url' => array('type' => 'varchar',
  79. 'length' => 191),
  80. 'description' => array('type' => 'text'),
  81. 'created' => array('type' => 'datetime',
  82. 'not null' => true),
  83. ),
  84. 'primary key' => array('id'),
  85. 'unique keys' => array(
  86. 'happening_uri_key' => array('uri'),
  87. ),
  88. 'foreign keys' => array(
  89. 'happening_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  90. 'happening_uri_fkey' => array('notice', array('uri' => 'uri'))
  91. ),
  92. 'indexes' => array(
  93. 'happening_profile_id_idx' => array('profile_id'),
  94. 'happening_created_idx' => array('created'),
  95. 'happening_start_time_end_time_idx' => array('start_time', 'end_time'),
  96. ),
  97. );
  98. }
  99. public static function saveActivityObject(Activity $act, Notice $stored)
  100. {
  101. if (count($act->objects) !== 1) {
  102. // TRANS: Exception thrown when there are too many activity objects.
  103. throw new Exception(_m('Too many activity objects.'));
  104. }
  105. $actobj = $act->objects[0];
  106. if (!ActivityUtils::compareTypes($actobj->type, [Happening::OBJECT_TYPE])) {
  107. // TRANS: Exception thrown when event plugin comes across a non-event type object.
  108. throw new Exception(_m('Wrong type for object.'));
  109. }
  110. try {
  111. $other = Happening::getByKeys(['uri' => $actobj->id]);
  112. throw AlreadyFulfilledException('Happening already exists.');
  113. } catch (NoResultException $e) {
  114. // alright, let's save this
  115. }
  116. $dtstart = null;
  117. $dtend = null;
  118. $location = null;
  119. $url = null;
  120. foreach ($actobj->extra as $extra) {
  121. switch ($extra[0]) {
  122. case 'dtstart':
  123. $dtstart = $extra[2];
  124. break;
  125. case 'dtend':
  126. $dtend = $extra[2];
  127. break;
  128. case 'location':
  129. // location is optional
  130. $location = $extra[2];
  131. break;
  132. case 'url':
  133. // url is optional
  134. $url = $extra[2];
  135. }
  136. }
  137. if (empty($dtstart)) {
  138. // TRANS: Exception thrown when has no start date
  139. throw new Exception(_m('No start date for event.'));
  140. }
  141. if (empty($dtend)) {
  142. // TRANS: Exception thrown when has no end date
  143. throw new Exception(_m('No end date for event.'));
  144. }
  145. // convert RFC3339 dates delivered in Activity Stream to MySQL DATETIME date format
  146. $start_time = new DateTime($dtstart);
  147. $start_time->setTimezone(new DateTimeZone('UTC'));
  148. $start_time = $start_time->format('Y-m-d H:i:s');
  149. $end_time = new DateTime($dtend);
  150. $end_time->setTimezone(new DateTimeZone('UTC'));
  151. $end_time = $end_time->format('Y-m-d H:i:s');
  152. $ev = new Happening();
  153. $ev->id = UUID::gen();
  154. $ev->uri = $actobj->id;
  155. $ev->profile_id = $stored->getProfile()->getID();
  156. $ev->start_time = $start_time;
  157. $ev->end_time = $end_time;
  158. $ev->title = $actobj->title;
  159. $ev->location = $location;
  160. $ev->description = $stored->getContent();
  161. $ev->url = $url;
  162. $ev->created = $stored->getCreated();
  163. $ev->insert();
  164. return $ev;
  165. }
  166. public function insert()
  167. {
  168. $result = parent::insert();
  169. if ($result === false) {
  170. common_log_db_error($this, 'INSERT', __FILE__);
  171. throw new ServerException(_('Failed to insert '._ve(get_called_class()).' into database'));
  172. }
  173. return $result;
  174. }
  175. /**
  176. * Returns the profile's canonical url, not necessarily a uri/unique id
  177. *
  178. * @return string $url
  179. */
  180. public function getUrl()
  181. {
  182. if (empty($this->url) ||
  183. !filter_var($this->url, FILTER_VALIDATE_URL)) {
  184. throw new InvalidUrlException($this->url);
  185. }
  186. return $this->url;
  187. }
  188. public function getUri()
  189. {
  190. return $this->uri;
  191. }
  192. public function getStored()
  193. {
  194. return Notice::getByKeys(array('uri'=>$this->getUri()));
  195. }
  196. public static function fromStored(Notice $stored)
  197. {
  198. if (!ActivityUtils::compareTypes($stored->getObjectType(), [self::OBJECT_TYPE])) {
  199. throw new ServerException('Notice is not of type '.self::OBJECT_TYPE);
  200. }
  201. return self::getByKeys(array('uri'=>$stored->getUri()));
  202. }
  203. public function getRSVPs()
  204. {
  205. return RSVP::forEvent($this);
  206. }
  207. public function getRSVP($profile)
  208. {
  209. return RSVP::pkeyGet([
  210. 'profile_id' => $profile->getID(),
  211. 'event_uri' => $this->getUri(),
  212. ]);
  213. }
  214. public static function getObjectType()
  215. {
  216. return self::OBJECT_TYPE;
  217. }
  218. public function asActivityObject()
  219. {
  220. $actobj = new ActivityObject();
  221. $actobj->id = $this->getUri();
  222. $actobj->type = self::getObjectType();
  223. $actobj->title = $this->title;
  224. $actobj->summary = $this->description;
  225. $actobj->extra[] = array('dtstart',
  226. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  227. common_date_iso8601($this->start_time));
  228. $actobj->extra[] = array('dtend',
  229. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  230. common_date_iso8601($this->end_time));
  231. $actobj->extra[] = array('location',
  232. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  233. $this->location);
  234. try {
  235. $actobj->extra[] = array('url',
  236. array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
  237. $this->getUrl());
  238. } catch (InvalidUrlException $e) {
  239. // oh well, no URL for you!
  240. }
  241. /* We don't use these ourselves, but we add them to be nice RSS/XML citizens */
  242. $actobj->extra[] = array('startdate',
  243. array('xmlns' => 'http://purl.org/rss/1.0/plugins/event/'),
  244. common_date_iso8601($this->start_time));
  245. $actobj->extra[] = array('enddate',
  246. array('xmlns' => 'http://purl.org/rss/1.0/plugins/event/'),
  247. common_date_iso8601($this->end_time));
  248. $actobj->extra[] = array('location',
  249. array('xmlns' => 'http://purl.org/rss/1.0/plugins/event/'),
  250. $this->location);
  251. return $actobj;
  252. }
  253. }