Happening.php 10 KB

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