messagelistitem.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * GNUsocial implementation of Direct Messages
  18. *
  19. * @package GNUsocial
  20. * @author Mikael Nordfeldth <mmn@hethane.se>
  21. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * A single item in a message list
  28. *
  29. * @category Plugin
  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. abstract class MessageListItem extends Widget
  35. {
  36. protected $message;
  37. /**
  38. * Constructor.
  39. *
  40. * @param HTMLOutputter $out Output context
  41. * @param Notice $message Message to show
  42. */
  43. function __construct(HTMLOutputter $out, $message)
  44. {
  45. parent::__construct($out);
  46. $this->message = $message;
  47. }
  48. /**
  49. * Show the widget.
  50. *
  51. * @return void
  52. */
  53. function show()
  54. {
  55. $profile = $this->getMessageProfile();
  56. if (is_null($profile)) {
  57. // null most probably because there are no attention profiles and
  58. // the UI below isn't ready for that, yet.
  59. return;
  60. }
  61. $this->out->elementStart('li', ['class' => 'h-entry notice',
  62. 'id' => 'message-' . $this->message->getID()]);
  63. $this->out->elementStart('a', ['href' => $profile->getUrl(),
  64. 'class' => 'p-author']);
  65. $avatarUrl = $profile->avatarUrl(AVATAR_STREAM_SIZE);
  66. $this->out->element('img', ['src' => $avatarUrl,
  67. 'class' => 'avatar u-photo',
  68. 'width' => AVATAR_STREAM_SIZE,
  69. 'height' => AVATAR_STREAM_SIZE,
  70. 'alt' => $profile->getBestName()]);
  71. $this->out->element('span', ['class' => 'nickname fn'], $profile->getNickname());
  72. $this->out->elementEnd('a');
  73. // FIXME: URL, image, video, audio
  74. $this->out->elementStart('div', ['class' => 'e-content']);
  75. $this->out->raw($this->message->getRendered());
  76. $this->out->elementEnd('div');
  77. $messageurl = common_local_url('showmessage',
  78. ['message' => $this->message->getID()]);
  79. $this->out->elementStart('div', 'entry-metadata');
  80. $this->out->elementStart('a', ['rel' => 'bookmark',
  81. 'class' => 'timestamp',
  82. 'href' => $messageurl]);
  83. $dt = common_date_iso8601($this->message->getCreated());
  84. $this->out->element('time',
  85. ['class' => 'dt-published',
  86. 'datetime' => common_date_iso8601($this->message->getCreated()),
  87. // TRANS: Timestamp title (tooltip text) for NoticeListItem
  88. 'title' => common_exact_date($this->message->getCreated())],
  89. common_date_string($this->message->getCreated()));
  90. $this->out->elementEnd('a');
  91. if ($this->message->source) {
  92. $this->out->elementStart('span', 'source');
  93. // FIXME: bad i18n. Device should be a parameter (from %s).
  94. // TRANS: Followed by notice source (usually the client used to send the notice).
  95. $this->out->text(_('from'));
  96. $this->showSource($this->message->source);
  97. $this->out->elementEnd('span');
  98. }
  99. $this->out->elementEnd('div');
  100. $this->out->elementEnd('li');
  101. }
  102. /**
  103. * Dummy method. Serves no other purpose than to make strings available used
  104. * in self::showSource() through xgettext.
  105. *
  106. * @return void
  107. */
  108. function messageListItemDummyMessages()
  109. {
  110. // A dummy array with messages. These will get extracted by xgettext and
  111. // are used in self::showSource().
  112. $dummy_messages = [
  113. // TRANS: A possible notice source (web interface).
  114. _m('SOURCE','web'),
  115. // TRANS: A possible notice source (XMPP).
  116. _m('SOURCE','xmpp'),
  117. // TRANS: A possible notice source (e-mail).
  118. _m('SOURCE','mail'),
  119. // TRANS: A possible notice source (OpenMicroBlogging).
  120. _m('SOURCE','omb'),
  121. // TRANS: A possible notice source (Application Programming Interface).
  122. _m('SOURCE','api')
  123. ];
  124. }
  125. /**
  126. * Show the source of the message.
  127. *
  128. * Returns either the name (and link) of the API client that posted the notice,
  129. * or one of other other channels.
  130. *
  131. * @param string $source the source of the message
  132. *
  133. * @return void
  134. */
  135. function showSource(string $source)
  136. {
  137. $source_name = _m('SOURCE',$source);
  138. switch ($source) {
  139. case 'web':
  140. case 'xmpp':
  141. case 'mail':
  142. case 'omb':
  143. case 'api':
  144. $this->out->element('span', 'device', $source_name);
  145. break;
  146. default:
  147. $ns = Notice_source::getKV($source);
  148. if ($ns) {
  149. $this->out->elementStart('span', 'device');
  150. $this->out->element('a',
  151. ['href' => $ns->url,
  152. 'rel' => 'external'],
  153. $ns->name);
  154. $this->out->elementEnd('span');
  155. } else {
  156. $this->out->element('span', 'device', $source_name);
  157. }
  158. break;
  159. }
  160. return;
  161. }
  162. /**
  163. * Return the profile to show in the message item.
  164. *
  165. * Overridden in sub-classes to show sender, receiver, or whatever.
  166. *
  167. * @return Profile profile to show avatar and name of
  168. */
  169. abstract function getMessageProfile(): ?Profile;
  170. }