Change the owner of a reshared item

This commit is contained in:
Michael 2020-08-13 06:09:26 +00:00
parent 8ca92d2022
commit 1ab81266b9
2 changed files with 33 additions and 45 deletions

View File

@ -708,48 +708,17 @@ function conversation(App $a, array $items, $mode, $update, $preview = false, $o
*/ */
function conversation_fetch_comments($thread_items, $pinned) { function conversation_fetch_comments($thread_items, $pinned) {
$comments = []; $comments = [];
$parentlines = [];
$lineno = 0;
$actor = [];
$received = '';
$owner = '';
while ($row = Item::fetch($thread_items)) { while ($row = Item::fetch($thread_items)) {
if (($row['verb'] == Activity::ANNOUNCE) && ($row['author-link'] == $owner)) {
continue;
}
if (($row['verb'] == Activity::ANNOUNCE) && !empty($row['contact-uid']) && ($row['received'] > $received) && ($row['thr-parent'] == $row['parent-uri'])) {
$actor = ['link' => $row['author-link'], 'avatar' => $row['author-avatar'], 'name' => $row['author-name']];
$received = $row['received'];
}
if ((($row['gravity'] == GRAVITY_PARENT) && !$row['origin'] && !in_array($row['network'], [Protocol::DIASPORA])) &&
(empty($row['contact-uid']) || !in_array($row['network'], Protocol::NATIVE_SUPPORT))) {
$parentlines[] = $lineno;
}
if (($row['gravity'] == GRAVITY_PARENT) && ($row['author-link'] != $row['owner-link'])) {
$owner = $row['owner-link'];
}
if ($row['gravity'] == GRAVITY_PARENT) { if ($row['gravity'] == GRAVITY_PARENT) {
$row['pinned'] = $pinned; $row['pinned'] = $pinned;
} }
$comments[] = $row; $comments[] = $row;
$lineno++;
} }
DBA::close($thread_items); DBA::close($thread_items);
if (!empty($actor)) {
foreach ($parentlines as $line) {
$comments[$line]['owner-link'] = $actor['link'];
$comments[$line]['owner-avatar'] = $actor['avatar'];
$comments[$line]['owner-name'] = $actor['name'];
}
}
return $comments; return $comments;
} }

View File

@ -1800,7 +1800,9 @@ class Item
// It is mainly used in the "post_local" hook. // It is mainly used in the "post_local" hook.
unset($item['api_source']); unset($item['api_source']);
self::transformToForumPost($item); if ($item['verb'] == Activity::ANNOUNCE) {
self::setOwnerforResharedItem($item);
}
// Check for hashtags in the body and repair or add hashtag links // Check for hashtags in the body and repair or add hashtag links
$item['body'] = self::setHashtags($item['body']); $item['body'] = self::setHashtags($item['body']);
@ -1992,34 +1994,51 @@ class Item
} }
/** /**
* Convert items to forum posts * Change the owner of a parent item if it had been shared by a forum or a user contact
* *
* (public) forum posts in the new format consist of the regular post by the author * (public) forum posts in the new format consist of the regular post by the author
* followed by an announce message sent from the forum account. * followed by an announce message sent from the forum account.
* This means we have to look out for an announce message send by a forum account. * Changing the owner helps in grouping forum posts.
*
* For other reshared posts this function changes the owner of that post
* if the author is not a user contact but the post had been shared by one.
* This helps in detecting why this post appeared in the timeline.
* *
* @param array $item * @param array $item
* @return void * @return void
*/ */
private static function transformToForumPost(array $item) private static function setOwnerforResharedItem(array $item)
{ {
if ($item["verb"] != Activity::ANNOUNCE) { $parent = self::selectFirst(['id', 'owner-id', 'author-id', 'author-link'], ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
// No announce message, so don't do anything if (!DBA::isResult($parent)) {
Logger::error('Parent not found', ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
return; return;
} }
$pcontact = Contact::selectFirst(['nurl'], ['id' => $item['author-id'], 'contact-type' => Contact::TYPE_COMMUNITY]); $author = Contact::selectFirst(['url', 'contact-type'], ['id' => $item['author-id']]);
if (empty($pcontact['nurl'])) { if (!DBA::isResult($author)) {
// The announce message wasn't created by a forum account, so we don't need to continue Logger::error('Author not found', ['id' => $item['author-id']]);
return; return;
} }
$contact = Contact::selectFirst(['id'], ['nurl' => $pcontact['nurl'], 'uid' => $item['uid']]); if (($author['contact-type'] != Contact::TYPE_COMMUNITY) && (($parent['owner-id'] != $parent['author-id']) || ($item['uid'] == 0))) {
if (!empty($contact['id'])) { logger::info('The resharer is no forum and the parent is reshared or not a personal post: quit', ['resharer' => $item['author-id'], 'owner' => $parent['owner-id'], 'author' => $parent['author-id'], 'uid' => $item['uid']]);
$condition = ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]; return;
Item::update(['owner-id' => $item['author-id'], 'contact-id' => $contact['id']], $condition);
Logger::info('Convert message into a forum message', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $contact['id']]);
} }
if (Contact::getIdForURL($parent['author-link'], $item['uid'])) {
logger::info('The parent author is a user contact: quit', ['author' => $parent['author-link'], 'uid' => $item['uid']]);
return;
}
$cid = Contact::getIdForURL($author['url'], $item['uid']);
if (empty($cid)) {
logger::info('The resharer is not a user contact: quit', ['resharer' => $author['url'], 'uid' => $item['uid']]);
return;
}
Item::update(['owner-id' => $item['author-id'], 'contact-id' => $cid], ['id' => $parent['id']]);
Logger::info('Change owner of the parent', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $cid]);
} }
/** /**