Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.7 KiB

6 months ago
9 months ago
10 months ago
10 months ago
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2010-2021, the Friendica project
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Friendica\Model\Post;
  22. use Friendica\Core\Logger;
  23. use Friendica\Database\DBA;
  24. use Friendica\Core\Worker;
  25. use Friendica\Database\Database;
  26. use Friendica\DI;
  27. use Friendica\Model\Item;
  28. use Friendica\Model\Post;
  29. use Friendica\Model\Tag;
  30. use Friendica\Util\DateTimeFormat;
  31. class Delayed
  32. {
  33. /**
  34. * Insert a new delayed post
  35. *
  36. * @param string $uri
  37. * @param array $item
  38. * @param integer $notify
  39. * @param bool $unprepared
  40. * @param string $delayed
  41. * @param array $taglist
  42. * @param array $attachments
  43. * @return bool insert success
  44. */
  45. public static function add(string $uri, array $item, int $notify = 0, bool $unprepared = false, string $delayed = '', array $taglist = [], array $attachments = [])
  46. {
  47. if (empty($item['uid']) || self::exists($uri, $item['uid'])) {
  48. Logger::notice('No uid or already found');
  49. return false;
  50. }
  51. if (empty($delayed)) {
  52. $min_posting = DI::config()->get('system', 'minimum_posting_interval', 0);
  53. $last_publish = DI::pConfig()->get($item['uid'], 'system', 'last_publish', 0, true);
  54. $next_publish = max($last_publish + (60 * $min_posting), time());
  55. $delayed = date(DateTimeFormat::MYSQL, $next_publish);
  56. } else {
  57. $next_publish = strtotime($delayed);
  58. }
  59. Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]);
  60. if (!Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $unprepared, $uri)) {
  61. return false;
  62. }
  63. DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish);
  64. return DBA::insert('delayed-post', ['uri' => $uri, 'uid' => $item['uid'], 'delayed' => $delayed], Database::INSERT_IGNORE);
  65. }
  66. /**
  67. * Delete a delayed post
  68. *
  69. * @param string $uri
  70. * @param int $uid
  71. *
  72. * @return bool delete success
  73. */
  74. private static function delete(string $uri, int $uid)
  75. {
  76. return DBA::delete('delayed-post', ['uri' => $uri, 'uid' => $uid]);
  77. }
  78. /**
  79. * Check if an entry exists
  80. *
  81. * @param string $uri
  82. * @param int $uid
  83. *
  84. * @return bool "true" if an entry with that URI exists
  85. */
  86. public static function exists(string $uri, int $uid)
  87. {
  88. return DBA::exists('delayed-post', ['uri' => $uri, 'uid' => $uid]);
  89. }
  90. /**
  91. * Publish a delayed post
  92. *
  93. * @param array $item
  94. * @param integer $notify
  95. * @param array $taglist
  96. * @param array $attachments
  97. * @param bool $unprepared
  98. * @param string $uri
  99. * @return bool
  100. */
  101. public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], bool $unprepared = false, string $uri = '')
  102. {
  103. if (!empty($attachments)) {
  104. $item['attachments'] = $attachments;
  105. }
  106. if ($unprepared) {
  107. $_SESSION['authenticated'] = true;
  108. $_SESSION['uid'] = $item['uid'];
  109. $_REQUEST = $item;
  110. $_REQUEST['api_source'] = true;
  111. $_REQUEST['profile_uid'] = $item['uid'];
  112. $_REQUEST['title'] = $item['title'] ?? '';
  113. if (!empty($item['app'])) {
  114. $_REQUEST['source'] = $item['app'];
  115. }
  116. require_once 'mod/item.php';
  117. $id = item_post(DI::app());
  118. if (empty($uri) && !empty($item['extid'])) {
  119. $uri = $item['extid'];
  120. }
  121. Logger::notice('Unprepared post stored', ['id' => $id, 'uid' => $item['uid'], 'uri' => $uri]);
  122. if (self::exists($uri, $item['uid'])) {
  123. self::delete($uri, $item['uid']);
  124. }
  125. return $id;
  126. }
  127. $id = Item::insert($item, $notify);
  128. Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]);
  129. if (empty($uri) && !empty($item['uri'])) {
  130. $uri = $item['uri'];
  131. }
  132. if (!empty($uri) && self::exists($uri, $item['uid'])) {
  133. self::delete($uri, $item['uid']);
  134. }
  135. if (!empty($id) && (!empty($taglist) || !empty($attachments))) {
  136. $feeditem = Post::selectFirst(['uri-id'], ['id' => $id]);
  137. foreach ($taglist as $tag) {
  138. Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
  139. }
  140. }
  141. return $id;
  142. }
  143. }