newnotice.php 9.3 KB

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