use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
-use Friendica\Model\Post;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Diaspora;
if ($item['uid'] == $uid) {
self::markForDeletionById($item['id'], PRIORITY_HIGH);
} elseif ($item['uid'] != 0) {
- Logger::notice('Wrong ownership. Not deleting item', ['id' => $item['id']]);
+ Logger::warning('Wrong ownership. Not deleting item', ['id' => $item['id']]);
}
}
DBA::close($items);
*/
private static function contactId(array $item): int
{
- if (!empty($item['contact-id']) && DBA::exists('contact', ['self' => true, 'id' => $item['contact-id']])) {
- return $item['contact-id'];
- } elseif (($item['gravity'] == GRAVITY_PARENT) && !empty($item['uid']) && !empty($item['contact-id']) && Contact::isSharing($item['contact-id'], $item['uid'])) {
- return $item['contact-id'];
- } elseif (!empty($item['uid']) && !Contact::isSharing($item['author-id'], $item['uid'])) {
+ if ($item['uid'] == 0) {
return $item['author-id'];
- } elseif (!empty($item['contact-id'])) {
- return $item['contact-id'];
- } else {
- $contact_id = Contact::getIdForURL($item['author-link'], $item['uid']);
+ }
+
+ if ($item['origin']) {
+ $owner = User::getOwnerDataById($item['uid']);
+ return $owner['id'];
+ }
+
+ if (!empty($item['causer-id']) && Contact::isSharing($item['causer-id'], $item['uid'], true)) {
+ $cdata = Contact::getPublicAndUserContactID($item['causer-id'], $item['uid']);
+ if (!empty($cdata['user'])) {
+ return $cdata['user'];
+ }
+ }
+
+ if ($item['gravity'] == GRAVITY_PARENT) {
+ if (Contact::isSharingByURL($item['owner-link'], $item['uid'], true)) {
+ $contact_id = Contact::getIdForURL($item['owner-link'], $item['uid']);
+ } else {
+ $contact_id = Contact::getIdForURL($item['owner-link']);
+ }
if (!empty($contact_id)) {
return $contact_id;
}
}
- return $item['author-id'];
+
+ if (Contact::isSharingByURL($item['author-link'], $item['uid'], true)) {
+ $contact_id = Contact::getIdForURL($item['author-link'], $item['uid']);
+ } else {
+ $contact_id = Contact::getIdForURL($item['author-link']);
+ }
+
+ if (!empty($contact_id)) {
+ return $contact_id;
+ }
+
+ Logger::warning('contact-id could not be fetched, using self contact instead.', ['uid' => $item['uid'], 'item' => $item]);
+ $self = Contact::selectFirst(['id'], ['self' => true, 'uid' => $item['uid']]);
+ return $self['id'];
}
/**
if (!empty($item['uid'])) {
$owner = User::getOwnerDataById($item['uid'], false);
if (!$owner) {
- Logger::notice('Missing item user owner data', ['uid' => $item['uid']]);
+ Logger::warning('Missing item user owner data', ['uid' => $item['uid']]);
return false;
}
return 0;
}
+ /**
+ * Fetch the uri-id of the parent for the given uri-id
+ *
+ * @param integer $uriid
+ * @return integer
+ */
+ public static function getParent(int $uriid): int
+ {
+ $thread_parent = Post::selectFirstPost(['thr-parent-id', 'gravity'], ['uri-id' => $uriid]);
+ if (empty($thread_parent)) {
+ return 0;
+ }
+
+ if ($thread_parent['gravity'] == GRAVITY_PARENT) {
+ return $uriid;
+ }
+
+ return self::getParent($thread_parent['thr-parent-id']);
+ }
+
/**
* Fetch top-level parent data for the given item array
*
$item['gravity'] = self::getGravity($item);
- $item['language'] = self::getLanguage($item);
-
$default = ['url' => $item['author-link'], 'name' => $item['author-name'],
'photo' => $item['author-avatar'], 'network' => $item['network']];
$item['author-id'] = ($item['author-id'] ?? 0) ?: Contact::getIdForURL($item['author-link'], 0, null, $default);
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);
if (!empty($item['direction']) && in_array($item['direction'], [Conversation::PUSH, Conversation::RELAY]) &&
// Check for hashtags in the body and repair or add hashtag links
$item['body'] = self::setHashtags($item['body']);
+ $item['language'] = self::getLanguage($item);
+
$notify_type = Delivery::POST;
// Filling item related side tables
$post = Post::selectFirst(['id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
if (!empty($post['id'])) {
if (!empty($item['event-id'])) {
- $post = Post::selectFirst(['event-id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
- if (!empty($post['event-id'])) {
+ $event_post = Post::selectFirst(['event-id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
+ if (!empty($event_post['event-id'])) {
$event = DBA::selectFirst('event', ['edited', 'start', 'finish', 'summary', 'desc', 'location', 'nofinish', 'adjust'], ['id' => $item['event-id']]);
if (!empty($event)) {
// We aren't using "Event::store" here, since we don't want to trigger any further action
- $ret = DBA::update('event', $event, ['id' => $post['event-id']]);
- Logger::info('Event updated', ['uid' => $uid, 'source-event' => $item['event-id'], 'target-event' => $post['event-id'], 'ret' => $ret]);
+ $ret = DBA::update('event', $event, ['id' => $event_post['event-id']]);
+ Logger::info('Event updated', ['uid' => $uid, 'source-event' => $item['event-id'], 'target-event' => $event_post['event-id'], 'ret' => $ret]);
}
}
}
$item['origin'] = 0;
$item['wall'] = 0;
- if ($item['gravity'] == GRAVITY_PARENT) {
- $contact = Contact::getByURLForUser($item['owner-link'], $uid, false, ['id']);
- } else {
- $contact = Contact::getByURLForUser($item['author-link'], $uid, false, ['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)) {
- // Shouldn't happen even less
- Logger::warning('self contact could not be fetched', ['uid' => $uid, 'item' => $item]);
- return 0;
- }
- $item['contact-id'] = $self['id'];
- }
+ $item['contact-id'] = self::contactId($item);
$notify = false;
if ($item['gravity'] == GRAVITY_PARENT) {
return '';
}
+ $naked_body = self::getDominantLanguage($naked_body);
+
$availableLanguages = DI::l10n()->getAvailableLanguages();
// See https://github.com/friendica/friendica/issues/10511
// Persian is manually added to language detection until a persian translation is provided for the interface, at
return '';
}
+ /**
+ * Check if latin or non latin are dominant in the body and only return the dominant one
+ *
+ * @param string $body
+ * @return string
+ */
+ private static function getDominantLanguage(string $body): string
+ {
+ $latin = '';
+ $non_latin = '';
+ for ($i = 0; $i < mb_strlen($body); $i++) {
+ $character = mb_substr($body, $i, 1);
+ $ord = mb_ord($character);
+
+ // We add the most common characters to both strings.
+ if (($ord <= 64) || ($ord >= 91 && $ord <= 96) || ($ord >= 123 && $ord <= 191) || in_array($ord, [215, 247]) || ($ord >= 697 && $ord <= 735) || ($ord > 65535)) {
+ $latin .= $character;
+ $non_latin .= $character;
+ } elseif ($ord < 768) {
+ $latin .= $character;
+ } else {
+ $non_latin .= $character;
+ }
+ }
+ return (mb_strlen($latin) > mb_strlen($non_latin)) ? $latin : $non_latin;
+ }
+
public static function getLanguageMessage(array $item): string
{
$iso639 = new \Matriphe\ISO639\ISO639;
$item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
if (!DBA::isResult($item)) {
- Logger::notice('like: unknown item', ['id' => $item_id]);
+ Logger::warning('Post had not been fetched', ['id' => $item_id]);
return false;
}
}
// Retrieve the current logged in user's public contact
- $author_id = Contact::getIdForURL($owner['url']);
+ $author_id = Contact::getPublicIdByUserId($uid);
if (empty($author_id)) {
Logger::info('Empty public contact');
return false;
$activity = Activity::ANNOUNCE;
break;
default:
- Logger::notice('unknown verb', ['verb' => $verb, 'item' => $item_id]);
+ Logger::warning('unknown verb', ['verb' => $verb, 'item' => $item_id]);
return false;
}
$new_item = [
'guid' => System::createUUID(),
'uri' => self::newURI(),
- 'uid' => $item['uid'],
+ 'uid' => $uid,
'contact-id' => $owner['id'],
'wall' => $item['wall'],
'origin' => 1,
'unseen' => 1,
];
- $signed = Diaspora::createLikeSignature($uid, $new_item);
- if (!empty($signed)) {
- $new_item['diaspora_signed_text'] = json_encode($signed);
+ if (in_array($activity, [Activity::LIKE, Activity::DISLIKE])) {
+ $signed = Diaspora::createLikeSignature($uid, $new_item);
+ if (!empty($signed)) {
+ $new_item['diaspora_signed_text'] = json_encode($signed);
+ }
}
- $new_item_id = self::insert($new_item);
+ self::insert($new_item, true);
// If the parent item isn't visible then set it to visible
+ // @todo Check if this is still needed
if (!$item['visible']) {
self::update(['visible' => true], ['id' => $item['id']]);
}
-
- $new_item['id'] = $new_item_id;
-
- Hook::callAll('post_local_end', $new_item);
-
return true;
}