Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect the postopts values for scheduled posts #10581

Merged
merged 2 commits into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions mod/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,11 @@ function item_post(App $a) {
$o = conversation($a, [array_merge($contact_record, $datarray)], 'search', false, true);

System::jsonExit(['preview' => $o]);
} elseif (!empty($_REQUEST['scheduled_at'])) {
}

Hook::callAll('post_local',$datarray);

if (!empty($_REQUEST['scheduled_at'])) {
$scheduled_at = DateTimeFormat::convert($_REQUEST['scheduled_at'], 'UTC', $a->getTimezone());
if ($scheduled_at > DateTimeFormat::utcNow()) {
unset($datarray['created']);
Expand All @@ -692,16 +696,12 @@ function item_post(App $a) {
unset($datarray['edit']);
unset($datarray['self']);
unset($datarray['api_source']);
Post\Delayed::add($datarray['uri'], $datarray, PRIORITY_HIGH, false, $scheduled_at);

Post\Delayed::add($datarray['uri'], $datarray, PRIORITY_HIGH, Post\Delayed::PREPARED_NO_HOOK, $scheduled_at);
item_post_return(DI::baseUrl(), $api_source, $return_path);
}
}

$datarray['uri-id'] = ItemURI::getIdByURI($datarray['uri']);

Hook::callAll('post_local',$datarray);

if (!empty($datarray['cancel'])) {
Logger::info('mod_item: post cancelled by addon.');
if ($return_path) {
Expand All @@ -716,6 +716,8 @@ function item_post(App $a) {
System::jsonExit($json);
}

$datarray['uri-id'] = ItemURI::getIdByURI($datarray['uri']);

if ($orig_post) {
// Fill the cache field
// This could be done in Item::update as well - but we have to check for the existance of some fields.
Expand Down
26 changes: 12 additions & 14 deletions src/Model/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ private static function getGravity(array $item)
return GRAVITY_UNKNOWN; // Should not happen
}

public static function insert($item, $notify = false, $dontcache = false)
public static function insert(array $item, bool $notify = false, bool $post_local = true)
{
$orig_item = $item;

Expand Down Expand Up @@ -931,7 +931,7 @@ public static function insert($item, $notify = false, $dontcache = false)
$item["private"] = self::PRIVATE;
}

if ($notify) {
if ($notify && $post_local) {
$item['edit'] = false;
$item['parent'] = $parent_id;

Expand All @@ -953,7 +953,7 @@ public static function insert($item, $notify = false, $dontcache = false)
unset($_SESSION['authenticated']);
unset($_SESSION['uid']);
}
} else {
} elseif (!$notify) {
Hook::callAll('post_remote', $item);
}

Expand Down Expand Up @@ -1141,15 +1141,13 @@ public static function insert($item, $notify = false, $dontcache = false)
return 0;
}

if (!$dontcache) {
if ($notify) {
if (!\Friendica\Content\Feature::isEnabled($posted_item['uid'], 'explicit_mentions') && ($posted_item['gravity'] == GRAVITY_COMMENT)) {
Tag::createImplicitMentions($posted_item['uri-id'], $posted_item['thr-parent-id']);
}
Hook::callAll('post_local_end', $posted_item);
} else {
Hook::callAll('post_remote_end', $posted_item);
if ($notify) {
if (!\Friendica\Content\Feature::isEnabled($posted_item['uid'], 'explicit_mentions') && ($posted_item['gravity'] == GRAVITY_COMMENT)) {
Tag::createImplicitMentions($posted_item['uri-id'], $posted_item['thr-parent-id']);
}
Hook::callAll('post_local_end', $posted_item);
} else {
Hook::callAll('post_remote_end', $posted_item);
}

if ($posted_item['gravity'] === GRAVITY_PARENT) {
Expand Down Expand Up @@ -1465,7 +1463,7 @@ private static function storeForUser(array $item, int $uid)
}
}

$distributed = self::insert($item, $notify, true);
$distributed = self::insert($item, $notify);

if (!$distributed) {
Logger::info("Distributed item wasn't stored", ['uri-id' => $item['uri-id'], 'user' => $uid]);
Expand Down Expand Up @@ -1534,7 +1532,7 @@ private static function addShadow($itemid)
$item['contact-id'] = $item['author-id'];
}

$public_shadow = self::insert($item, false, true);
$public_shadow = self::insert($item);

Logger::info('Stored public shadow', ['thread' => $itemid, 'id' => $public_shadow]);
}
Expand Down Expand Up @@ -1593,7 +1591,7 @@ private static function addShadowPost($itemid)
unset($item['post-reason']);
$item['contact-id'] = Contact::getIdForURL($item['author-link']);

$public_shadow = self::insert($item, false, true);
$public_shadow = self::insert($item);

Logger::info('Stored public shadow', ['uri-id' => $item['uri-id'], 'id' => $public_shadow]);

Expand Down
54 changes: 36 additions & 18 deletions src/Model/Post/Delayed.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,36 @@

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain the meaning of these three values as it isn’t intuitive just from the names.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A boolean parameter has been converted to an integer parameter. Since there can be outstanding jobs in the workerqueue during the update we have to assure that it is interpreted correctly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant in the code, please. 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

/**
* The content is posted like a manual post. Means some processing of body will be done.
* Also it is posted with default permissions and default connector settings.
* This is used for mirrored connector posts.
*/
const UNPREPARED = 1;
/**
* 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');
Expand All @@ -63,7 +80,7 @@ public static function add(string $uri, array $item, int $notify = 0, bool $unpr

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' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $preparation_mode, $uri);
if (!$wid) {
return 0;
}
Expand Down Expand Up @@ -168,21 +185,21 @@ public static function getParametersForid(int $id)
/**
* 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) {
if ($preparation_mode == self::UNPREPARED) {
$_SESSION['authenticated'] = true;
$_SESSION['uid'] = $item['uid'];

Expand All @@ -209,7 +226,8 @@ public static function publish(array $item, int $notify = 0, array $taglist = []

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']]);

Expand Down
2 changes: 1 addition & 1 deletion src/Module/Api/Mastodon/Statuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public static function post(array $parameters = [])
if (!empty($request['scheduled_at'])) {
$item['guid'] = Item::guid($item, true);
$item['uri'] = Item::newURI($item['uid'], $item['guid']);
$id = Post\Delayed::add($item['uri'], $item, PRIORITY_HIGH, false, $request['scheduled_at']);
$id = Post\Delayed::add($item['uri'], $item, PRIORITY_HIGH, Post\Delayed::PREPARED, $request['scheduled_at']);
if (empty($id)) {
DI::mstdnError()->InternalError();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ public static function import($xml, array $importer = [], array $contact = [])
}
$publish_at = date(DateTimeFormat::MYSQL, $publish_time);

if (Post\Delayed::add($posting['item']['uri'], $posting['item'], $posting['notify'], false, $publish_at, $posting['taglist'], $posting['attachments'])) {
if (Post\Delayed::add($posting['item']['uri'], $posting['item'], $posting['notify'], Post\Delayed::PREPARED, $publish_at, $posting['taglist'], $posting['attachments'])) {
DI::pConfig()->set($item['uid'], 'system', 'last_publish', $publish_time);
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/Worker/DelayedPublish.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ class DelayedPublish
/**
* Publish a post, used for delayed postings
*
* @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 void
*/
public static function execute(array $item, int $notify = 0, array $taglist = [], array $attachments = [], bool $unprepared = false, string $uri = '')
public static function execute(array $item, int $notify = 0, array $taglist = [], array $attachments = [], int $preparation_mode = Post\Delayed::PREPARED, string $uri = '')
{
$id = Post\Delayed::publish($item, $notify, $taglist, $attachments, $unprepared, $uri);
Logger::notice('Post published', ['id' => $id, 'uid' => $item['uid'], 'notify' => $notify, 'unprepared' => $unprepared]);
$id = Post\Delayed::publish($item, $notify, $taglist, $attachments, $preparation_mode, $uri);
Logger::notice('Post published', ['id' => $id, 'uid' => $item['uid'], 'notify' => $notify, 'unprepared' => $preparation_mode]);
}
}