use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
+use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Model\Post\Category;
use Friendica\Protocol\Activity;
const PRIVATE = 1;
const UNLISTED = 2;
+ const TABLES = ['item', 'user-item', 'item-content', 'post-delivery-data', 'diaspora-interaction'];
+
private static $legacy_mode = null;
+ private static function getItemFields()
+ {
+ $definition = DBStructure::definition('', false);
+
+ $postfields = [];
+ foreach (self::TABLES as $table) {
+ $postfields[$table] = array_keys($definition[$table]['fields']);
+ }
+
+ return $postfields;
+ }
+
public static function isLegacyMode()
{
if (is_null(self::$legacy_mode)) {
}
// Update the contact relations
- if ($item['author-id'] != $parent['author-id']) {
- DBA::update('contact-relation', ['last-interaction' => $item['created']], ['cid' => $parent['author-id'], 'relation-cid' => $item['author-id']], true);
- }
+ ContactRelation::store($parent['author-id'], $item['author-id'], $item['created']);
}
return $item;
public static function insert($item, $notify = false, $dontcache = false)
{
+ $structure = self::getItemFields();
+
$orig_item = $item;
$priority = PRIORITY_HIGH;
'photo' => $item['owner-avatar'], 'network' => $item['network']];
$item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, null, $default);
+ // Ensure that there is an avatar cache
+ Contact::checkAvatarCache($item['author-id']);
+ Contact::checkAvatarCache($item['owner-id']);
+
// The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes
$item["contact-id"] = self::contactId($item);
Tag::storeFromBody($item['uri-id'], $body);
}
+ // Remove all fields that aren't part of the item table
+ foreach ($item as $field => $value) {
+ if (!in_array($field, $structure['item'])) {
+ unset($item[$field]);
+ }
+ }
+
$ret = DBA::insert('item', $item);
// When the item was successfully stored we fetch the ID of the item.
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']));
if ($transmit) {
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'];
+ $original['gravity'] = $item['gravity'];
+ $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
*
$origin = $item['origin'];
- unset($item['id']);
- unset($item['parent']);
- unset($item['mention']);
- unset($item['wall']);
- unset($item['origin']);
- unset($item['starred']);
-
$users = [];
/// @todo add a field "pcid" in the contact table that referrs to the public contact id.
if ($origin_uid == $uid) {
$item['diaspora_signed_text'] = $signed_text;
}
- self::storeForUser($itemid, $item, $uid);
+ self::storeForUser($item, $uid);
}
}
/**
* 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 integer $uid The user that will receive the item entry
+ * @return integer stored item id
* @throws \Exception
*/
- private static function storeForUser($itemid, $item, $uid)
+ public 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['origin'] = 0;
$item['wall'] = 0;
- if ($item['uri'] == $item['parent-uri']) {
- $item['contact-id'] = Contact::getIdForURL($item['owner-link'], $uid);
+
+ if ($item['gravity'] == GRAVITY_PARENT) {
+ $contact = Contact::getByURLForUser($item['owner-link'], $uid, false, ['id']);
} 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($contact['id'])) {
+ $item['contact-id'] = $contact['id'];
+ } else {
+ // Shouldn't happen at all
+ Logger::warning('contact-id could not be fetched', ['uid' => $uid, 'item' => $item]);
$self = DBA::selectFirst('contact', ['id'], ['self' => true, 'uid' => $uid]);
if (!DBA::isResult($self)) {
- return;
+ // Shouldn't happen even less
+ Logger::warning('self contact could not be fetched', ['uid' => $uid, 'item' => $item]);
+ return 0;
}
$item['contact-id'] = $self['id'];
}
/// @todo Handling of "event-id"
$notify = false;
- if ($item['uri'] == $item['parent-uri']) {
+ if ($item['gravity'] == GRAVITY_PARENT) {
$contact = DBA::selectFirst('contact', [], ['id' => $item['contact-id'], 'self' => false]);
if (DBA::isResult($contact)) {
$notify = self::isRemoteSelf($contact, $item);
$distributed = self::insert($item, $notify, true);
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 {
- 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;
}
/**
return false;
}
+ if (!Item::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) {
+ $parent_item = self::selectFirst(self::ITEM_FIELDLIST, ['uri-id' => $item['parent-uri-id'], 'uid' => 0]);
+ if (!empty($parent_item) && ($parent_item['private'] != self::PRIVATE)) {
+ $stored = self::storeForUser($parent_item, $uid);
+ Logger::info('Public item stored for user', ['uri-id' => $parent_item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
+ }
+ }
+
// Retrieves the local post owner
$owner_self_contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]);
if (!DBA::isResult($owner_self_contact)) {
*
* @return integer item id
*/
- public static function fetchByLink($uri, $uid = 0)
+ public static function fetchByLink(string $uri, int $uid = 0)
{
$item_id = self::searchByLink($uri, $uid);
if (!empty($item_id)) {
*
* @return array item array with data from the original item
*/
- public static function addShareDataFromOriginal($item)
+ public static function addShareDataFromOriginal(array $item)
{
$shared = self::getShareArray($item);
if (empty($shared)) {
}
// Otherwhise try to find (and possibly fetch) the item via the link. This should work for Diaspora and ActivityPub posts
- $id = self::fetchByLink($shared['link'], $uid);
+ $id = self::fetchByLink($shared['link'] ?? '', $uid);
if (empty($id)) {
- Logger::info('Original item not found', ['url' => $shared['link'], 'callstack' => System::callstack()]);
+ Logger::info('Original item not found', ['url' => $shared['link'] ?? '', 'callstack' => System::callstack()]);
return $item;
}