Posts with subscribed tags will now be stored for the user

This commit is contained in:
Michael 2020-07-21 08:35:57 +00:00
parent 2889d59b83
commit ab8eef24c5
2 changed files with 85 additions and 17 deletions

View File

@ -1964,6 +1964,9 @@ class Item
check_user_notification($current_post); check_user_notification($current_post);
// Distribute items to users who subscribed to their tags
self::distributeByTags($item, $orig_item);
$transmit = $notify || ($item['visible'] && ($parent_origin || $item['origin'])); $transmit = $notify || ($item['visible'] && ($parent_origin || $item['origin']));
if ($transmit) { if ($transmit) {
@ -1983,6 +1986,26 @@ class Item
return $current_post; return $current_post;
} }
/**
* Distribute the given item to users who subscribed to their tags
*
* @param array $item Processed item
* @param array $original Original item
*/
private static function distributeByTags(array $item, array $original)
{
if (($item['uid'] != 0) || ($item['gravity'] != GRAVITY_PARENT) || !in_array($item['network'], Protocol::FEDERATED)) {
return;
}
$uids = Tag::getUIDListByURIId($item['uri-id']);
foreach ($uids as $uid) {
$original['uri-id'] = $item['uri-id'];
$stored = self::storeForUser($original, $uid);
Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
}
}
/** /**
* Insert a new item content entry * Insert a new item content entry
* *
@ -2079,13 +2102,6 @@ class Item
$origin = $item['origin']; $origin = $item['origin'];
unset($item['id']);
unset($item['parent']);
unset($item['mention']);
unset($item['wall']);
unset($item['origin']);
unset($item['starred']);
$users = []; $users = [];
/// @todo add a field "pcid" in the contact table that referrs to the public contact id. /// @todo add a field "pcid" in the contact table that referrs to the public contact id.
@ -2145,33 +2161,48 @@ class Item
if ($origin_uid == $uid) { if ($origin_uid == $uid) {
$item['diaspora_signed_text'] = $signed_text; $item['diaspora_signed_text'] = $signed_text;
} }
self::storeForUser($itemid, $item, $uid); self::storeForUser($item, $uid);
} }
} }
/** /**
* Store public items for the receivers * Store public items for the receivers
* *
* @param integer $itemid Item ID that should be added
* @param array $item The item entry that will be stored * @param array $item The item entry that will be stored
* @param integer $uid The user that will receive the item entry * @param integer $uid The user that will receive the item entry
* @return integer stored item id
* @throws \Exception * @throws \Exception
*/ */
private static function storeForUser($itemid, $item, $uid) private static function storeForUser(array $item, int $uid)
{ {
if (self::exists(['uri-id' => $item['uri-id'], 'uid' => $uid])) {
Logger::info('Item already exists', ['uri-id' => $item['uri-id'], 'uid' => $uid]);
return 0;
}
unset($item['id']);
unset($item['parent']);
unset($item['mention']);
unset($item['starred']);
$item['uid'] = $uid; $item['uid'] = $uid;
$item['origin'] = 0; $item['origin'] = 0;
$item['wall'] = 0; $item['wall'] = 0;
if ($item['uri'] == $item['parent-uri']) { if ($item['uri'] == $item['parent-uri']) {
$item['contact-id'] = Contact::getIdForURL($item['owner-link'], $uid); $contact = Contact::getByURLForUser($item['owner-link'], $uid, false, ['id']);
} else { } else {
$item['contact-id'] = Contact::getIdForURL($item['author-link'], $uid); $contact = Contact::getByURLForUser($item['author-link'], $uid, false, ['id']);
} }
if (empty($item['contact-id'])) { if (!empty($item['contact-id'])) {
$item['contact-id'] = $contact['id'];
} else {
// Shouldn't happen at all
$self = DBA::selectFirst('contact', ['id'], ['self' => true, 'uid' => $uid]); $self = DBA::selectFirst('contact', ['id'], ['self' => true, 'uid' => $uid]);
if (!DBA::isResult($self)) { if (!DBA::isResult($self)) {
return; // Shouldn't happen even less
return 0;
} }
$item['contact-id'] = $self['id']; $item['contact-id'] = $self['id'];
} }
@ -2189,10 +2220,11 @@ class Item
$distributed = self::insert($item, $notify, true); $distributed = self::insert($item, $notify, true);
if (!$distributed) { if (!$distributed) {
Logger::info("Distributed public item wasn't stored", ['id' => $itemid, 'user' => $uid]); Logger::info("Distributed public item wasn't stored", ['uri-id' => $item['uri-id'], 'user' => $uid]);
} else { } else {
Logger::info('Distributed public item was stored', ['id' => $itemid, 'user' => $uid, 'stored' => $distributed]); Logger::info('Distributed public item was stored', ['uri-id' => $item['uri-id'], 'user' => $uid, 'stored' => $distributed]);
} }
return $distributed;
} }
/** /**

View File

@ -536,5 +536,41 @@ class Tag
} }
return Strings::startsWithChars($tag, $tag_chars); return Strings::startsWithChars($tag, $tag_chars);
} }
/**
* Fetch user who subscribed to the given tag
*
* @param string $tag
* @return array User list
*/
private static function getUIDListByTag(string $tag)
{
$uids = [];
$searches = DBA::select('search', ['uid'], ['term' => $tag]);
while ($search = DBA::fetch($searches)) {
$uids[] = $search['uid'];
}
DBA::close($searches);
return $uids;
}
/**
* Fetch user who subscribed to the tags of the given item
*
* @param integer $uri_id
* @return array User list
*/
public static function getUIDListByURIId(int $uri_id)
{
$uids = [];
$tags = self::getByURIId($uri_id, [self::HASHTAG]);
foreach ($tags as $tag) {
$uids = array_merge($uids, self::getUIDListByTag(self::TAG_CHARACTER[self::HASHTAG] . $tag['name']));
}
return array_unique($uids);
}
} }