MessageModel.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // get the messages
  71. $message = new Notice();
  72. $message->selectAdd();
  73. $message->selectAdd('notice.*');
  74. // fetch all notice IDs related to the user $to
  75. $message->joinAdd(['id', 'attention:notice_id']);
  76. $message->whereAdd('notice.scope = ' . Notice::MESSAGE_SCOPE);
  77. $message->whereAdd('attention.profile_id = ' . $to->getID());
  78. $message->orderBy('notice.created DESC, notice.id DESC');
  79. if (!is_null($page) && $page >= 0) {
  80. $page = ($page == 0) ? 1 : $page;
  81. $message->limit(($page - 1) * MESSAGES_PER_PAGE,
  82. MESSAGES_PER_PAGE + 1);
  83. }
  84. return $message->find() ? $message : null;
  85. }
  86. /**
  87. * Return data object of messages sent by some user.
  88. *
  89. * @param User $from sender
  90. * @param int|null $page page limiter
  91. * @return Notice data object with stream for messages
  92. */
  93. public static function outboxMessages(User $from, ?int $page = null)
  94. {
  95. $message = new Notice();
  96. $message->profile_id = $from->getID();
  97. $message->whereAdd('scope = ' . NOTICE::MESSAGE_SCOPE);
  98. $message->orderBy('created DESC, id DESC');
  99. if (!is_null($page) && $page >= 0) {
  100. $page = ($page == 0) ? 1 : $page;
  101. $message->limit(($page - 1) * MESSAGES_PER_PAGE,
  102. MESSAGES_PER_PAGE + 1);
  103. }
  104. return $message->find() ? $message : null;
  105. }
  106. /**
  107. * Save a new message.
  108. *
  109. * @param Profile $from sender
  110. * @param string $content message-text
  111. * @param string $source message's source
  112. * @return Notice stored message
  113. */
  114. public static function saveNew(Profile $from, string $content, string $source = 'web'): Notice
  115. {
  116. return Notice::saveNew($from->getID(),
  117. $content,
  118. $source,
  119. ['distribute' => false, // using events to handle remote distribution
  120. 'scope' => NOTICE::MESSAGE_SCOPE]);
  121. }
  122. }