MessageModel.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * Model for a direct message
  28. *
  29. * @category Plugin
  30. * @package GNUsocial
  31. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class MessageModel
  35. {
  36. /**
  37. * Retrieve size-limit for messages content
  38. *
  39. * @return int size-limit
  40. */
  41. public static function maxContent(): int
  42. {
  43. $desclimit = common_config('message', 'contentlimit');
  44. // null => use global limit (distinct from 0!)
  45. if (is_null($desclimit) || !is_int($desclimit)) {
  46. $desclimit = common_config('site', 'textlimit');
  47. }
  48. return $desclimit;
  49. }
  50. /**
  51. * Is message-text too long?
  52. *
  53. * @param string $content message-text
  54. * @return bool true if too long, false otherwise
  55. */
  56. public static function contentTooLong(string $content): bool
  57. {
  58. $contentlimit = self::maxContent();
  59. return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
  60. }
  61. /**
  62. * Return data object of messages received by some user.
  63. *
  64. * @param User $to receiver
  65. * @param int|null $page page limiter
  66. * @return Notice data object with stream for messages
  67. */
  68. public static function inboxMessages(User $to, ?int $page = null)
  69. {
  70. $attention = new Attention();
  71. $attention->selectAdd('notice_id');
  72. $attention->whereAdd('profile_id = ' . $to->getID());
  73. $ids = $attention->find() ? $attention->fetchAll('notice_id') : [];
  74. $reply = new Reply();
  75. $reply->selectAdd('notice_id');
  76. $reply->whereAdd('profile_id = ' . $to->getID());
  77. if ($reply->find()) {
  78. $ids = array_unique(
  79. array_merge($ids, $reply->fetchAll('notice_id'))
  80. );
  81. } else if (empty($ids)) {
  82. return null;
  83. }
  84. $message = new Notice();
  85. $message->whereAdd('scope = ' . NOTICE::MESSAGE_SCOPE);
  86. $message->whereAddIn('id', $ids, 'int');
  87. $message->orderBy('created DESC, id DESC');
  88. if (!is_null($page) && $page >= 0) {
  89. $page = ($page == 0) ? 1 : $page;
  90. $message->limit(($page - 1) * MESSAGES_PER_PAGE,
  91. MESSAGES_PER_PAGE + 1);
  92. }
  93. return $message->find() ? $message : null;
  94. }
  95. /**
  96. * Return data object of messages sent by some user.
  97. *
  98. * @param User $from sender
  99. * @param int|null $page page limiter
  100. * @return Notice data object with stream for messages
  101. */
  102. public static function outboxMessages(User $from, ?int $page = null)
  103. {
  104. $message = new Notice();
  105. $message->profile_id = $from->getID();
  106. $message->whereAdd('scope = ' . NOTICE::MESSAGE_SCOPE);
  107. $message->orderBy('created DESC, id DESC');
  108. if (!is_null($page) && $page >= 0) {
  109. $page = ($page == 0) ? 1 : $page;
  110. $message->limit(($page - 1) * MESSAGES_PER_PAGE,
  111. MESSAGES_PER_PAGE + 1);
  112. }
  113. return $message->find() ? $message : null;
  114. }
  115. /**
  116. * Save a new message.
  117. *
  118. * @param Profile $from sender
  119. * @param string $content message-text
  120. * @param string $source message's source
  121. * @return Notice stored message
  122. */
  123. public static function saveNew(Profile $from, string $content, string $source = 'web'): Notice
  124. {
  125. return Notice::saveNew($from->getID(),
  126. $content,
  127. $source,
  128. ['distribute' => false, // using events to handle remote distribution
  129. 'scope' => NOTICE::MESSAGE_SCOPE]);
  130. }
  131. }