From 82321114abb1339837d05ce119be45bef3c06595 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 1 Nov 2017 23:20:43 +0000 Subject: [PATCH 1/5] If a post is dedicated to a single forum then it will be rewritten to a forum post --- include/dfrn.php | 93 +++++++++++++++++++++++++++++++++++++++++++----- mod/item.php | 4 ++- object/Item.php | 4 ++- 3 files changed, 90 insertions(+), 11 deletions(-) diff --git a/include/dfrn.php b/include/dfrn.php index 55c51cb01..faa98c8e2 100644 --- a/include/dfrn.php +++ b/include/dfrn.php @@ -1987,6 +1987,81 @@ class dfrn { return true; } + /** + * @brief Check and possibly rewrite a post if it was a dedicated forum post + * + * @param array $item the new item record + * + * @return boolean Was the post be rewritten? + */ + private static function rewriteDedicatedForumPost($item) { + $fields = array('author-link', 'allow_cid', 'contact-id', 'private', 'wall', 'id', 'parent'); + $condition = array('uri' => $item['uri'], 'uid' => $item['uid']); + $existing = dba::select('item', $fields, $condition, array('limit' => 1)); + if (!dbm::is_result($existing)) { + return false; + } + + // Only rewrite if the former post was a private starting post + if (!$existing['wall'] || !$existing['private'] || ($existing['id'] != $existing['parent'])) { + return false; + } + + // Is the post only directed to a sigle forum + if ($existing['allow_cid'] != '<'.$item['contact-id'].'>') { + return false; + } + + $fields = array('id'); + $condition = array('uid' => $item['uid'], 'self' => true); + $self = dba::select('contact', $fields, $condition, array('limit' => 1)); + if (!dbm::is_result($self)) { + return false; + } + + // is the original item created by the "self" user. + if ($self['id'] != $existing['contact-id']) { + return false; + } + + $fields = array('forum', 'prv'); + $condition = array('id' => $item['contact-id']); + $contact = dba::select('contact', $fields, $condition, array('limit' => 1)); + if (!dbm::is_result($contact)) { + return false; + } + + // Is the post from a forum? + if (!$contact['forum'] && !$contact['prv']) { + return false; + } + + /// @todo There is an ugly downside of this whole rewrite process: These items loose the ability to be edited by the user. + logger('Item '.$item['uri'].' will be rewritten.', LOGGER_DEBUG); + + $item = store_conversation($item); + unset($fields['dsprsig']); + + // Rewrite to a public post if it comes from a public forum + if ($contact['forum']) { + $item['allow_cid'] = ''; + $item['allow_gid'] = ''; + $item['deny_cid'] = ''; + $item['deny_gid'] = ''; + $item['private'] = false; + } + + $item['wall'] = false; + + $item["owner-id"] = get_contact($item["owner-link"], 0); + + $condition = array('uri' => $item["uri"], 'uid' => $item["uid"]); + dba::update('item', $item, $condition); + + add_thread($existing['id']); + return true; + } + /** * @brief Updates an item * @@ -2006,15 +2081,15 @@ class dfrn { return false; } - $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` IN (0, %d)", - dbesc($item["title"]), - dbesc($item["body"]), - dbesc($item["tag"]), - dbesc(datetime_convert("UTC","UTC",$item["edited"])), - dbesc(datetime_convert()), - dbesc($item["uri"]), - intval($importer["importer_uid"]) - ); + if (!self::rewriteDedicatedForumPost($item)) { + $fields = array('title' => $item["title"], 'body' => $item["body"], + 'tag' => $item["tag"], 'changed' => datetime_convert(), + 'edited' => datetime_convert("UTC", "UTC", $item["edited"])); + + $condition = array("`uri` = ? AND `uid` IN (0, ?)", $item["uri"], $importer["importer_uid"]); + dba::update('item', $fields, $condition); + } + create_tags_from_itemuri($item["uri"], $importer["importer_uid"]); update_thread_uri($item["uri"], $importer["importer_uid"]); diff --git a/mod/item.php b/mod/item.php index 0bb0d17db..77f927223 100644 --- a/mod/item.php +++ b/mod/item.php @@ -575,6 +575,7 @@ function item_post(App $a) { $tagged = array(); $private_forum = false; + $only_to_forum = false; if (count($tags)) { foreach ($tags as $tag) { @@ -608,12 +609,13 @@ function item_post(App $a) { // When the forum is private or the forum is addressed with a "!" make the post private if (is_array($success['contact']) && ($success['contact']['prv'] || ($tag_type == '!'))) { $private_forum = true; + $only_to_forum = ($tag_type == '!'); $private_id = $success['contact']['id']; } } } - if ($private_forum && !$parent && !$private) { + if ($private_forum && !$parent && (!$private || $only_to_forum)) { // we tagged a private forum in a top level post and the message was public. // Restrict it. $private = 1; diff --git a/object/Item.php b/object/Item.php index 4f89ea804..c7a1bcabb 100644 --- a/object/Item.php +++ b/object/Item.php @@ -127,7 +127,9 @@ class Item extends BaseObject { ? t('Private Message') : false); $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false); - if (local_user() && link_compare($a->contact['url'],$item['author-link'])) { + + /// @todo The check for the contact-id is here to block editing of rewritten forum posts - see function dfrn::rewriteDedicatedForumPost + if (local_user() && link_compare($a->contact['url'],$item['author-link']) && ($a->contact['id'] == $item['contact-id'])) { if ($item["event-id"] != 0) { $edpost = array("events/event/".$item['event-id'], t("Edit")); } else { From 01af4435c8d768a3c045a2fcbf88032b74438554 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 2 Nov 2017 05:46:23 +0000 Subject: [PATCH 2/5] Now the public entry is created --- include/dfrn.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dfrn.php b/include/dfrn.php index faa98c8e2..52730fa2b 100644 --- a/include/dfrn.php +++ b/include/dfrn.php @@ -2058,7 +2058,7 @@ class dfrn { $condition = array('uri' => $item["uri"], 'uid' => $item["uid"]); dba::update('item', $item, $condition); - add_thread($existing['id']); + add_shadow_thread($existing['id']); return true; } From 5326b30ad19d1767092c2be14085f8b2a990d434 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 2 Nov 2017 07:48:41 +0000 Subject: [PATCH 3/5] Edits are now possible - but unsuccessful on the receiver side --- include/notifier.php | 19 +++++++++++++++++++ object/Item.php | 3 +-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/notifier.php b/include/notifier.php index e5ead46e3..17eb578d1 100644 --- a/include/notifier.php +++ b/include/notifier.php @@ -285,6 +285,25 @@ function notifier_run(&$argv, &$argc){ if ($parent['origin']) { $relay_to_owner = false; } + + // Special treatment for forum posts + if (($target_item['author-link'] != $target_item['owner-link']) && + ($owner['id'] != $target_item['contact-id']) && + ($target_item['uri'] === $target_item['parent-uri'])) { + + $fields = array('forum', 'prv'); + $condition = array('id' => $target_item['contact-id']); + $contact = dba::select('contact', $fields, $condition, array('limit' => 1)); + if (!dbm::is_result($contact)) { + // Should never happen + return false; + } + + // Is the post from a forum? + if ($contact['forum'] || $contact['prv']) { + $relay_to_owner = true; + } + } if ($relay_to_owner) { logger('notifier: followup '.$target_item["guid"], LOGGER_DEBUG); // local followup to remote post diff --git a/object/Item.php b/object/Item.php index c7a1bcabb..35421ac3e 100644 --- a/object/Item.php +++ b/object/Item.php @@ -128,8 +128,7 @@ class Item extends BaseObject { : false); $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false); - /// @todo The check for the contact-id is here to block editing of rewritten forum posts - see function dfrn::rewriteDedicatedForumPost - if (local_user() && link_compare($a->contact['url'],$item['author-link']) && ($a->contact['id'] == $item['contact-id'])) { + if (local_user() && link_compare($a->contact['url'],$item['author-link'])) { if ($item["event-id"] != 0) { $edpost = array("events/event/".$item['event-id'], t("Edit")); } else { From 82a3e0558c3da651287d2a50a14c17f870063165 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 2 Nov 2017 07:50:09 +0000 Subject: [PATCH 4/5] Just some empty line --- object/Item.php | 1 - 1 file changed, 1 deletion(-) diff --git a/object/Item.php b/object/Item.php index 35421ac3e..4f89ea804 100644 --- a/object/Item.php +++ b/object/Item.php @@ -127,7 +127,6 @@ class Item extends BaseObject { ? t('Private Message') : false); $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false); - if (local_user() && link_compare($a->contact['url'],$item['author-link'])) { if ($item["event-id"] != 0) { $edpost = array("events/event/".$item['event-id'], t("Edit")); From 6949ab25f7a3e4567e74e240664492b2fc02dace Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 2 Nov 2017 11:17:10 +0000 Subject: [PATCH 5/5] A single mistake --- include/dfrn.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dfrn.php b/include/dfrn.php index 52730fa2b..40149eec2 100644 --- a/include/dfrn.php +++ b/include/dfrn.php @@ -2007,7 +2007,7 @@ class dfrn { return false; } - // Is the post only directed to a sigle forum + // Is the post only directed to a single forum? if ($existing['allow_cid'] != '<'.$item['contact-id'].'>') { return false; }