+ if ($item['gravity'] !== GRAVITY_PARENT) {
+ $toplevel_parent = self::getTopLevelParent($item);
+ if (empty($toplevel_parent)) {
+ return 0;
+ }
+
+ // If the thread originated from this node, we check the permission against the thread starter
+ $condition = ['uri' => $toplevel_parent['uri'], 'wall' => true];
+ $localTopLevelParent = self::selectFirst(['uid'], $condition);
+ if (!empty($localTopLevelParent['uid']) && !self::isAllowedByUser($item, $localTopLevelParent['uid'])) {
+ return 0;
+ }
+
+ $parent_id = $toplevel_parent['id'];
+ $item['parent-uri'] = $toplevel_parent['uri'];
+ $item['deleted'] = $toplevel_parent['deleted'];
+ $item['allow_cid'] = $toplevel_parent['allow_cid'];
+ $item['allow_gid'] = $toplevel_parent['allow_gid'];
+ $item['deny_cid'] = $toplevel_parent['deny_cid'];
+ $item['deny_gid'] = $toplevel_parent['deny_gid'];
+ $parent_origin = $toplevel_parent['origin'];
+
+ // Don't federate received participation messages
+ if ($item['verb'] != Activity::FOLLOW) {
+ $item['wall'] = $toplevel_parent['wall'];
+ } else {
+ $item['wall'] = false;
+ }
+
+ /*
+ * If the parent is private, force privacy for the entire conversation
+ * This differs from the above settings as it subtly allows comments from
+ * email correspondents to be private even if the overall thread is not.
+ */
+ if ($toplevel_parent['private']) {
+ $item['private'] = $toplevel_parent['private'];
+ }
+
+ /*
+ * Edge case. We host a public forum that was originally posted to privately.
+ * The original author commented, but as this is a comment, the permissions
+ * weren't fixed up so it will still show the comment as private unless we fix it here.
+ */
+ if ((intval($toplevel_parent['forum_mode']) == 1) && ($toplevel_parent['private'] != self::PUBLIC)) {
+ $item['private'] = self::PUBLIC;
+ }
+
+ // If its a post that originated here then tag the thread as "mention"
+ if ($item['origin'] && $item['uid']) {
+ DBA::update('thread', ['mention' => true], ['iid' => $parent_id]);
+ Logger::info('tagged thread as mention', ['parent' => $parent_id, 'uid' => $item['uid']]);
+ }
+
+ // Update the contact relations
+ Contact\Relation::store($toplevel_parent['author-id'], $item['author-id'], $item['created']);
+
+ unset($item['parent_origin']);
+ } else {
+ $parent_id = 0;
+ $parent_origin = $item['origin'];
+ }
+