]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Post/Delayed.php
Merge pull request #12298 from annando/api-suggestions
[friendica.git] / src / Model / Post / Delayed.php
index dd26869d1c0b66229c62fd3139cf9fd18b42b163..6e92465fac58a8ce1697d742a4fdd179fd18236b 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -33,19 +33,30 @@ use Friendica\Util\DateTimeFormat;
 
 class Delayed
 {
+       /**
+        * The content of the post is posted as is. Connector settings are using the default settings.
+        * This is used for automated scheduled posts via feeds or from the API.
+        */
+       const PREPARED = 0;
+       /**
+        * Like PREPARED, but additionally the connector settings can differ.
+        * This is used when manually publishing scheduled posts.
+        */
+       const PREPARED_NO_HOOK = 2;
+
        /**
         * Insert a new delayed post
         *
-        * @param string  $uri
-        * @param array   $item
-        * @param integer $notify
-        * @param bool    $unprepared
-        * @param string  $delayed
-        * @param array   $taglist
-        * @param array   $attachments
-        * @return int    ID of the created delayed post entry
+        * @param string $uri
+        * @param array  $item
+        * @param int    $notify
+        * @param int    $preparation_mode
+        * @param string $delayed
+        * @param array  $taglist
+        * @param array  $attachments
+        * @return int   ID of the created delayed post entry
         */
-       public static function add(string $uri, array $item, int $notify = 0, bool $unprepared = false, string $delayed = '', array $taglist = [], array $attachments = [])
+       public static function add(string $uri, array $item, int $notify = 0, int $preparation_mode = self::PREPARED, string $delayed = '', array $taglist = [], array $attachments = [])
        {
                if (empty($item['uid']) || self::exists($uri, $item['uid'])) {
                        Logger::notice('No uid or already found');
@@ -58,19 +69,16 @@ class Delayed
                        $last_publish = DI::pConfig()->get($item['uid'], 'system', 'last_publish', 0, true);
                        $next_publish = max($last_publish + (60 * $min_posting), time());
                        $delayed = date(DateTimeFormat::MYSQL, $next_publish);
-               } else {
-                       $next_publish = strtotime($delayed);
+                       DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish);
                }
 
                Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]);
 
-               $wid = Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $unprepared, $uri);
+               $wid = Worker::add(['priority' => Worker::PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $preparation_mode, $uri);
                if (!$wid) {
                        return 0;
                }
 
-               DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish);
-
                $delayed_post = [
                        'uri'     => $uri,
                        'uid'     => $item['uid'],
@@ -171,50 +179,23 @@ class Delayed
        /**
         * Publish a delayed post
         *
-        * @param array $item
-        * @param integer $notify
-        * @param array $taglist
-        * @param array $attachments
-        * @param bool  $unprepared
+        * @param array  $item
+        * @param int    $notify
+        * @param array  $taglist
+        * @param array  $attachments
+        * @param int    $preparation_mode
         * @param string $uri
         * @return bool
         */
-       public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], bool $unprepared = false, string $uri = '')
+       public static function publish(array $item, int $notify = 0, array $taglist = [], array $attachments = [], int $preparation_mode = self::PREPARED, string $uri = '')
        {
                if (!empty($attachments)) {
                        $item['attachments'] = $attachments;
                }
 
-               if ($unprepared) {
-                       $_SESSION['authenticated'] = true;
-                       $_SESSION['uid'] = $item['uid'];
-
-                       $_REQUEST = $item;
-                       $_REQUEST['api_source'] = true;
-                       $_REQUEST['profile_uid'] = $item['uid'];
-                       $_REQUEST['title'] = $item['title'] ?? '';
-
-                       if (!empty($item['app'])) {
-                               $_REQUEST['source'] = $item['app'];
-                       }
-
-                       require_once 'mod/item.php';
-                       $id = item_post(DI::app());
-
-                       if (empty($uri) && !empty($item['extid'])) {
-                               $uri = $item['extid'];
-                       }
-
-                       Logger::notice('Unprepared post stored', ['id' => $id, 'uid' => $item['uid'], 'uri' => $uri]);
-                       if (self::exists($uri, $item['uid'])) {
-                               self::delete($uri, $item['uid']);
-                       }
-
-                       return $id;
-               }
-               $id = Item::insert($item, $notify);
+               $id = Item::insert($item, $notify, $preparation_mode == self::PREPARED);
 
-               Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id']]);
+               Logger::notice('Post stored', ['id' => $id, 'uid' => $item['uid'], 'cid' => $item['contact-id'] ?? 'N/A']);
 
                if (empty($uri) && !empty($item['uri'])) {
                        $uri = $item['uri'];