newnotice.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. * Handler for posting new notices
  18. *
  19. * @category Personal
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Zach Copley <zach@status.net>
  23. * @author Sarven Capadisli <csarven@status.net>
  24. * @copyright 2008-2009 StatusNet, Inc.
  25. * @copyright 2013 Free Software Foundation, Inc http://www.fsf.org
  26. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  27. */
  28. defined('GNUSOCIAL') || die();
  29. /**
  30. * Action for posting new notices
  31. *
  32. * @category Personal
  33. * @package GNUsocial
  34. * @author Evan Prodromou <evan@status.net>
  35. * @author Zach Copley <zach@status.net>
  36. * @author Sarven Capadisli <csarven@status.net>
  37. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  38. */
  39. class NewnoticeAction extends FormAction
  40. {
  41. public $stored;
  42. protected $form = 'Notice';
  43. protected $inreplyto = null;
  44. /**
  45. * Title of the page
  46. *
  47. * Note that this usually doesn't get called unless something went wrong
  48. *
  49. * @return string page title
  50. */
  51. public function title()
  52. {
  53. if ($this->getInfo() && $this->stored instanceof Notice) {
  54. // TRANS: Page title after sending a notice.
  55. return _('Notice posted');
  56. }
  57. if ($this->int('inreplyto')) {
  58. return _m('TITLE', 'New reply');
  59. }
  60. // TRANS: Page title for sending a new notice.
  61. return _m('TITLE', 'New notice');
  62. }
  63. protected function doPreparation()
  64. {
  65. foreach (['inreplyto'] as $opt) {
  66. if ($this->trimmed($opt)) {
  67. $this->formOpts[$opt] = $this->trimmed($opt);
  68. }
  69. }
  70. if ($this->int('inreplyto')) {
  71. // Throws exception if the inreplyto Notice is given but not found.
  72. $this->inreplyto = Notice::getByID($this->int('inreplyto'));
  73. }
  74. // Backwards compatibility for "share this" widget things.
  75. // If no 'content', use 'status_textarea'
  76. $this->formOpts['content'] = $this->trimmed('content') ?: $this->trimmed('status_textarea');
  77. }
  78. /**
  79. * This doPost saves a new notice, based on arguments
  80. *
  81. * If successful, will show the notice, or return an Ajax-y result.
  82. * If not, it will show an error message -- possibly Ajax-y.
  83. *
  84. * Also, if the notice input looks like a command, it will run the
  85. * command and show the results -- again, possibly ajaxy.
  86. *
  87. * @return void
  88. */
  89. protected function doPost()
  90. {
  91. assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
  92. $user = $this->scoped->getUser();
  93. $content = $this->formOpts['content'];
  94. $options = array('source' => 'web');
  95. Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
  96. $inter = new CommandInterpreter();
  97. $cmd = $inter->handle_command($user, $content);
  98. if ($cmd) {
  99. if (GNUsocial::isAjax()) {
  100. $cmd->execute(new AjaxWebChannel($this));
  101. } else {
  102. $cmd->execute(new WebChannel($this));
  103. }
  104. return;
  105. }
  106. $act = new Activity();
  107. $act->verb = ActivityVerb::POST;
  108. $act->time = time();
  109. $act->actor = $this->scoped->asActivityObject();
  110. $upload = null;
  111. try {
  112. // throws exception on failure
  113. $upload = MediaFile::fromUpload('attach', $this->scoped);
  114. if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options))) {
  115. $content .= ($content==='' ? '' : ' ') . $upload->shortUrl();
  116. }
  117. Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options));
  118. // We could check content length here if the URL was added, but I'll just let it slide for now...
  119. $act->enclosures[] = $upload->getEnclosure();
  120. } catch (NoUploadedMediaException $e) {
  121. // simply no attached media to the new notice
  122. if (empty($content)) {
  123. // TRANS: Client error displayed trying to send a notice without content.
  124. throw new ClientException(_m('No content!'));
  125. }
  126. }
  127. // Reject notice if it is too long (without the HTML)
  128. // This is done after MediaFile::fromUpload etc. just to act the same as the ApiStatusesUpdateAction
  129. if (Notice::contentTooLong($content)) {
  130. // TRANS: Client error displayed when the parameter "status" is missing.
  131. // TRANS: %d is the maximum number of character for a notice.
  132. throw new ClientException(sprintf(
  133. _m('That\'s too long. Maximum notice size is %d character.',
  134. 'That\'s too long. Maximum notice size is %d characters.',
  135. Notice::maxContent()),
  136. Notice::maxContent()
  137. ));
  138. }
  139. $act->context = new ActivityContext();
  140. if ($this->inreplyto instanceof Notice) {
  141. $act->context->replyToID = $this->inreplyto->getUri();
  142. $act->context->replyToUrl = $this->inreplyto->getUrl(true); // maybe we don't have to send true here to force a URL?
  143. }
  144. if ($this->scoped->shareLocation()) {
  145. // use browser data if checked; otherwise profile data
  146. if ($this->arg('notice_data-geo')) {
  147. $locOptions = Notice::locationOptions(
  148. $this->trimmed('lat'),
  149. $this->trimmed('lon'),
  150. $this->trimmed('location_id'),
  151. $this->trimmed('location_ns'),
  152. $this->scoped
  153. );
  154. } else {
  155. $locOptions = Notice::locationOptions(
  156. null,
  157. null,
  158. null,
  159. null,
  160. $this->scoped
  161. );
  162. }
  163. $act->context->location = Location::fromOptions($locOptions);
  164. }
  165. $content = $this->scoped->shortenLinks($content);
  166. // FIXME: Make sure NoticeTitle plugin gets a change to add the title to our activityobject!
  167. if (Event::handle('StartNoticeSaveWeb', array($this, $this->scoped, &$content, &$options))) {
  168. // FIXME: We should be able to get the attentions from common_render_content!
  169. // and maybe even directly save whether they're local or not!
  170. $act->context->attention = common_get_attentions($content, $this->scoped, $this->inreplyto);
  171. // $options gets filled with possible scoping settings
  172. ToSelector::fillActivity($this, $act, $options);
  173. $actobj = new ActivityObject();
  174. $actobj->type = ActivityObject::NOTE;
  175. $actobj->content = common_render_content($content, $this->scoped, $this->inreplyto);
  176. // Finally add the activity object to our activity
  177. $act->objects[] = $actobj;
  178. $this->stored = Notice::saveActivity($act, $this->scoped, $options);
  179. if ($upload instanceof MediaFile) {
  180. $upload->attachToNotice($this->stored);
  181. }
  182. Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
  183. }
  184. Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content, &$options));
  185. if (!GNUsocial::isAjax()) {
  186. $url = common_local_url('shownotice', array('notice' => $this->stored->id));
  187. common_redirect($url, 303);
  188. }
  189. return _m('Saved the notice!');
  190. }
  191. protected function showContent()
  192. {
  193. if ($this->getInfo() && $this->stored instanceof Notice) {
  194. $this->showNotice($this->stored);
  195. } elseif (!$this->getError()) {
  196. if (!GNUsocial::isAjax() && $this->inreplyto instanceof Notice) {
  197. $this->showNotice($this->inreplyto);
  198. }
  199. parent::showContent();
  200. }
  201. }
  202. /**
  203. * Output a notice
  204. *
  205. * Used to generate the notice code for Ajax results.
  206. *
  207. * @param Notice $notice Notice that was saved
  208. *
  209. * @return void
  210. */
  211. public function showNotice(Notice $notice)
  212. {
  213. $nli = new NoticeListItem($notice, $this);
  214. $nli->show();
  215. }
  216. public function showNoticeForm()
  217. {
  218. // pass
  219. }
  220. }