Merge pull request #6726 from MrPetovan/task/6676-fix-implicit-mentions

Fix last batch of implicit mentions issues
This commit is contained in:
Michael Vogel 2019-02-24 15:40:11 +01:00 committed by GitHub
commit fa5525d084
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 547 additions and 264 deletions

View file

@ -425,7 +425,7 @@ class DBA
if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
// Question: Should we continue or stop the query here?
Logger::log('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), Logger::DEBUG);
Logger::warning('Query parameters mismatch.', ['query' => $sql, 'args' => $args, 'callstack' => System::callstack()]);
}
$sql = self::cleanQuery($sql);

View file

@ -2189,7 +2189,7 @@ class Contact extends BaseObject
{
$contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], ['id' => $cid]);
return self::magicLinkbyContact($contact, $url);
return self::magicLinkByContact($contact, $url);
}
/**
@ -2202,7 +2202,7 @@ class Contact extends BaseObject
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function magicLinkbyContact($contact, $url = '')
public static function magicLinkByContact($contact, $url = '')
{
if ((!local_user() && !remote_user()) || ($contact['network'] != Protocol::DFRN)) {
return $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url'];

View file

@ -1336,7 +1336,11 @@ class Item extends BaseObject
$expire_date = time() - ($expire_interval * 86400);
$created_date = strtotime($item['created']);
if ($created_date < $expire_date) {
Logger::log('item-store: item created ('.date('c', $created_date).') before expiration time ('.date('c', $expire_date).'). ignored. ' . print_r($item,true), Logger::DEBUG);
Logger::notice('Item created before expiration interval.', [
'created' => date('c', $created_date),
'expired' => date('c', $expire_date),
'$item' => $item
]);
return 0;
}
}
@ -1354,7 +1358,13 @@ class Item extends BaseObject
if (DBA::isResult($existing)) {
// We only log the entries with a different user id than 0. Otherwise we would have too many false positives
if ($uid != 0) {
Logger::log("Item with uri ".$item['uri']." already existed for user ".$uid." with id ".$existing["id"]." target network ".$existing["network"]." - new network: ".$item['network']);
Logger::notice('Item already existed for user', [
'uri' => $item['uri'],
'uid' => $uid,
'network' => $item['network'],
'existing_id' => $existing["id"],
'existing_network' => $existing["network"]
]);
}
return $existing["id"];
@ -1405,7 +1415,7 @@ class Item extends BaseObject
// When there is no content then we don't post it
if ($item['body'].$item['title'] == '') {
Logger::log('No body, no title.');
Logger::notice('No body, no title.');
return 0;
}
@ -1432,7 +1442,7 @@ class Item extends BaseObject
$item['author-id'] = defaults($item, 'author-id', Contact::getIdForURL($item["author-link"], 0, false, $default));
if (Contact::isBlocked($item["author-id"])) {
Logger::log('Contact '.$item["author-id"].' is blocked, item '.$item["uri"].' will not be stored');
Logger::notice('Author is blocked node-wide', ['author-link' => $item["author-link"], 'item-uri' => $item["uri"]]);
return 0;
}
@ -1442,22 +1452,27 @@ class Item extends BaseObject
$item['owner-id'] = defaults($item, 'owner-id', Contact::getIdForURL($item["owner-link"], 0, false, $default));
if (Contact::isBlocked($item["owner-id"])) {
Logger::log('Contact '.$item["owner-id"].' is blocked, item '.$item["uri"].' will not be stored');
Logger::notice('Owner is blocked node-wide', ['owner-link' => $item["owner-link"], 'item-uri' => $item["uri"]]);
return 0;
}
if ($item['network'] == Protocol::PHANTOM) {
Logger::log('Missing network. Called by: '.System::callstack(), Logger::DEBUG);
$item['network'] = Protocol::DFRN;
Logger::log("Set network to " . $item["network"] . " for " . $item["uri"], Logger::DEBUG);
Logger::notice('Missing network, setting to {network}.', [
'uri' => $item["uri"],
'network' => $item['network'],
'callstack' => System::callstack()
]);
}
// Checking if there is already an item with the same guid
Logger::log('Checking for an item for user '.$item['uid'].' on network '.$item['network'].' with the guid '.$item['guid'], Logger::DEBUG);
$condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']];
if (self::exists($condition)) {
Logger::log('found item with guid '.$item['guid'].' for user '.$item['uid'].' on network '.$item['network'], Logger::DEBUG);
Logger::notice('Found already existing item', [
'guid' => $item['guid'],
'uid' => $item['uid'],
'network' => $item['network']
]);
return 0;
}

View file

@ -1,37 +1,81 @@
<?php
/**
* @file src/Model/Term
* @file src/Model/Term.php
*/
namespace Friendica\Model;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Util\Strings;
/**
* Class Term
*
* This Model class handles term table interactions.
* This tables stores relevant terms related to posts, photos and searches, like hashtags, mentions and
* user-applied categories.
*
* @package Friendica\Model
*/
class Term
{
public static function tagTextFromItemId($itemid)
{
$tag_text = '';
$condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]];
$tags = DBA::select('term', [], $condition);
while ($tag = DBA::fetch($tags)) {
if ($tag_text != '') {
$tag_text .= ',';
}
const UNKNOWN = 0;
const HASHTAG = 1;
const MENTION = 2;
const CATEGORY = 3;
const PCATEGORY = 4;
const FILE = 5;
const SAVEDSEARCH = 6;
const CONVERSATION = 7;
/**
* An implicit mention is a mention in a comment body that is redundant with the threading information.
*/
const IMPLICIT_MENTION = 8;
/**
* An exclusive mention transfers the ownership of the post to the target account, usually a forum.
*/
const EXCLUSIVE_MENTION = 9;
if ($tag['type'] == 1) {
$tag_text .= '#';
} else {
$tag_text .= '@';
}
$tag_text .= '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
const TAG_CHARACTER = [
self::HASHTAG => '#',
self::MENTION => '@',
self::IMPLICIT_MENTION => '%',
self::EXCLUSIVE_MENTION => '!',
];
const OBJECT_TYPE_POST = 1;
const OBJECT_TYPE_PHOTO = 2;
/**
* Generates the legacy item.tag field comma-separated BBCode string from an item ID.
* Includes only hashtags, implicit and explicit mentions.
*
* @param int $item_id
* @return string
* @throws \Exception
*/
public static function tagTextFromItemId($item_id)
{
$tag_list = [];
$tags = self::tagArrayFromItemId($item_id, [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]);
foreach ($tags as $tag) {
$tag_list[] = self::TAG_CHARACTER[$tag['type']] . '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
}
return $tag_text;
return implode(',', $tag_list);
}
public static function tagArrayFromItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION])
/**
* Retrieves the terms from the provided type(s) associated with the provided item ID.
*
* @param int $item_id
* @param int|array $type
* @return array
* @throws \Exception
*/
public static function tagArrayFromItemId($item_id, $type = [self::HASHTAG, self::MENTION])
{
$condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type];
$condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type];
$tags = DBA::select('term', ['type', 'term', 'url'], $condition);
if (!DBA::isResult($tags)) {
return [];
@ -40,22 +84,39 @@ class Term
return DBA::toArray($tags);
}
public static function fileTextFromItemId($itemid)
/**
* Generates the legacy item.file field string from an item ID.
* Includes only file and category terms.
*
* @param int $item_id
* @return string
* @throws \Exception
*/
public static function fileTextFromItemId($item_id)
{
$file_text = '';
$condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]];
$tags = DBA::select('term', [], $condition);
while ($tag = DBA::fetch($tags)) {
if ($tag['type'] == TERM_CATEGORY) {
$tags = self::tagArrayFromItemId($item_id, [self::FILE, self::CATEGORY]);
foreach ($tags as $tag) {
if ($tag['type'] == self::CATEGORY) {
$file_text .= '<' . $tag['term'] . '>';
} else {
$file_text .= '[' . $tag['term'] . ']';
}
}
return $file_text;
}
public static function insertFromTagFieldByItemId($itemid, $tags)
/**
* Inserts new terms for the provided item ID based on the legacy item.tag field BBCode content.
* Deletes all previous tag terms for the same item ID.
* Sets both the item.mention and thread.mentions field flags if a mention concerning the item UID is found.
*
* @param int $item_id
* @param string $tag_str
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function insertFromTagFieldByItemId($item_id, $tag_str)
{
$profile_base = System::baseUrl();
$profile_data = parse_url($profile_base);
@ -64,32 +125,32 @@ class Term
$profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
$fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
$message = Item::selectFirst($fields, ['id' => $itemid]);
if (!DBA::isResult($message)) {
$item = Item::selectFirst($fields, ['id' => $item_id]);
if (!DBA::isResult($item)) {
return;
}
$message['tag'] = $tags;
$item['tag'] = $tag_str;
// Clean up all tags
self::deleteByItemId($itemid);
self::deleteByItemId($item_id);
if ($message['deleted']) {
if ($item['deleted']) {
return;
}
$taglist = explode(',', $message['tag']);
$taglist = explode(',', $item['tag']);
$tags_string = '';
foreach ($taglist as $tag) {
if ((substr(trim($tag), 0, 1) == '#') || (substr(trim($tag), 0, 1) == '@') || (substr(trim($tag), 0, 1) == '!')) {
if (Strings::startsWith($tag, self::TAG_CHARACTER)) {
$tags_string .= ' ' . trim($tag);
} else {
$tags_string .= ' #' . trim($tag);
}
}
$data = ' ' . $message['title'] . ' ' . $message['body'] . ' ' . $tags_string . ' ';
$data = ' ' . $item['title'] . ' ' . $item['body'] . ' ' . $tags_string . ' ';
// ignore anything in a code block
$data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
@ -103,11 +164,15 @@ class Term
}
}
$pattern = '/\W([\#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism';
$pattern = '/\W([\#@!%])\[url\=(.*?)\](.*?)\[\/url\]/ism';
if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
if (($match[1] == '@') || ($match[1] == '!')) {
if (in_array($match[1], [
self::TAG_CHARACTER[self::MENTION],
self::TAG_CHARACTER[self::IMPLICIT_MENTION],
self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]
])) {
$contact = Contact::getDetailsByURL($match[2], 0);
if (!empty($contact['addr'])) {
$match[3] = $contact['addr'];
@ -118,12 +183,12 @@ class Term
}
}
$tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
$tags[$match[2]] = $match[1] . trim($match[3], ',.:;[]/\"?!');
}
}
foreach ($tags as $tag => $link) {
if (substr(trim($tag), 0, 1) == '#') {
foreach ($tags as $link => $tag) {
if (self::isType($tag, self::HASHTAG)) {
// try to ignore #039 or #1 or anything like that
if (ctype_digit(substr(trim($tag), 1))) {
continue;
@ -134,11 +199,15 @@ class Term
continue;
}
$type = TERM_HASHTAG;
$type = self::HASHTAG;
$term = substr($tag, 1);
$link = '';
} elseif ((substr(trim($tag), 0, 1) == '@') || (substr(trim($tag), 0, 1) == '!')) {
$type = TERM_MENTION;
} elseif (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION)) {
if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION)) {
$type = self::MENTION;
} else {
$type = self::IMPLICIT_MENTION;
}
$contact = Contact::getDetailsByURL($link, 0);
if (!empty($contact['name'])) {
@ -147,43 +216,51 @@ class Term
$term = substr($tag, 1);
}
} else { // This shouldn't happen
$type = TERM_HASHTAG;
$type = self::HASHTAG;
$term = $tag;
$link = '';
Logger::notice('Unknown term type', ['tag' => $tag]);
}
if (DBA::exists('term', ['uid' => $message['uid'], 'otype' => TERM_OBJ_POST, 'oid' => $itemid, 'term' => $term])) {
if (DBA::exists('term', ['uid' => $item['uid'], 'otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'term' => $term, 'type' => $type])) {
continue;
}
if ($message['uid'] == 0) {
if ($item['uid'] == 0) {
$global = true;
DBA::update('term', ['global' => true], ['otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
DBA::update('term', ['global' => true], ['otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
} else {
$global = DBA::exists('term', ['uid' => 0, 'otype' => TERM_OBJ_POST, 'guid' => $message['guid']]);
$global = DBA::exists('term', ['uid' => 0, 'otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
}
DBA::insert('term', [
'uid' => $message['uid'],
'oid' => $itemid,
'otype' => TERM_OBJ_POST,
'uid' => $item['uid'],
'oid' => $item_id,
'otype' => self::OBJECT_TYPE_POST,
'type' => $type,
'term' => $term,
'url' => $link,
'guid' => $message['guid'],
'created' => $message['created'],
'received' => $message['received'],
'guid' => $item['guid'],
'created' => $item['created'],
'received' => $item['received'],
'global' => $global
]);
// Search for mentions
if (((substr($tag, 0, 1) == '@') || (substr($tag, 0, 1) == '!')) && (strpos($link, $profile_base_friendica) || strpos($link, $profile_base_diaspora))) {
$users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION)
&& (
strpos($link, $profile_base_friendica) !== false
|| strpos($link, $profile_base_diaspora) !== false
)
) {
$users_stmt = DBA::p("SELECT `uid` FROM `contact` WHERE self AND (`url` = ? OR `nurl` = ?)", $link, $link);
$users = DBA::toArray($users_stmt);
foreach ($users AS $user) {
if ($user['uid'] == $message['uid']) {
/// @todo This function is called frim Item::update - so we mustn't call that function here
DBA::update('item', ['mention' => true], ['id' => $itemid]);
DBA::update('thread', ['mention' => true], ['iid' => $message['parent']]);
if ($user['uid'] == $item['uid']) {
/// @todo This function is called from Item::update - so we mustn't call that function here
DBA::update('item', ['mention' => true], ['id' => $item_id]);
DBA::update('thread', ['mention' => true], ['iid' => $item['parent']]);
}
}
}
@ -191,20 +268,23 @@ class Term
}
/**
* @param integer $itemid item id
* Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
* Deletes all previous file terms for the same item ID.
*
* @param integer $item_id item id
* @param $files
* @return void
* @throws \Exception
*/
public static function insertFromFileFieldByItemId($itemid, $files)
public static function insertFromFileFieldByItemId($item_id, $files)
{
$message = Item::selectFirst(['uid', 'deleted'], ['id' => $itemid]);
$message = Item::selectFirst(['uid', 'deleted'], ['id' => $item_id]);
if (!DBA::isResult($message)) {
return;
}
// Clean up all tags
DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_FILE, TERM_CATEGORY]]);
DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [self::FILE, self::CATEGORY]]);
if ($message["deleted"]) {
return;
@ -216,9 +296,9 @@ class Term
foreach ($files[1] as $file) {
DBA::insert('term', [
'uid' => $message["uid"],
'oid' => $itemid,
'otype' => TERM_OBJ_POST,
'type' => TERM_FILE,
'oid' => $item_id,
'otype' => self::OBJECT_TYPE_POST,
'type' => self::FILE,
'term' => $file
]);
}
@ -228,9 +308,9 @@ class Term
foreach ($files[1] as $file) {
DBA::insert('term', [
'uid' => $message["uid"],
'oid' => $itemid,
'otype' => TERM_OBJ_POST,
'type' => TERM_CATEGORY,
'oid' => $item_id,
'otype' => self::OBJECT_TYPE_POST,
'type' => self::CATEGORY,
'term' => $file
]);
}
@ -252,6 +332,7 @@ class Term
'tags' => [],
'hashtags' => [],
'mentions' => [],
'implicit_mentions' => [],
];
$searchpath = System::baseUrl() . "/search?tag=";
@ -259,10 +340,9 @@ class Term
$taglist = DBA::select(
'term',
['type', 'term', 'url'],
["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
['otype' => self::OBJECT_TYPE_POST, 'oid' => $item['id'], 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]],
['order' => ['tid']]
);
while ($tag = DBA::fetch($taglist)) {
if ($tag['url'] == '') {
$tag['url'] = $searchpath . rawurlencode($tag['term']);
@ -270,25 +350,25 @@ class Term
$orig_tag = $tag['url'];
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$prefix = self::TAG_CHARACTER[$tag['type']];
switch($tag['type']) {
case self::HASHTAG:
if ($orig_tag != $tag['url']) {
$item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
}
$prefix = '';
if ($tag['type'] == TERM_HASHTAG) {
$tag['url'] = Contact::magicLinkByContact($author, $tag['url']);
if ($orig_tag != $tag['url']) {
$item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
}
$return['hashtags'][] = '#<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
$prefix = '#';
} elseif ($tag['type'] == TERM_MENTION) {
$tag['url'] = Contact::magicLink($tag['url']);
$return['mentions'][] = '@<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
$prefix = '@';
$return['hashtags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
$return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
break;
case self::MENTION:
$tag['url'] = Contact::magicLink($tag['url']);
$return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
$return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
break;
case self::IMPLICIT_MENTION:
$return['implicit_mentions'][] = $prefix . $tag['term'];
break;
}
$return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank">' . $tag['term'] . '</a>';
}
DBA::close($taglist);
@ -296,20 +376,38 @@ class Term
}
/**
* Delete all tags from an item
* Delete tags of the specific type(s) from an item
*
* @param int itemid - choose from which item the tags will be removed
* @param array $type
* @param int $item_id
* @param int|array $type
* @throws \Exception
*/
public static function deleteByItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION])
public static function deleteByItemId($item_id, $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION])
{
if (empty($itemid)) {
if (empty($item_id)) {
return;
}
// Clean up all tags
DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type]);
DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type]);
}
/**
* Check if the provided tag is of one of the provided term types.
*
* @param string $tag
* @param int ...$types
* @return bool
*/
public static function isType($tag, ...$types)
{
$tag_chars = [];
foreach ($types as $type) {
if (isset(self::TAG_CHARACTER[$type])) {
$tag_chars[] = self::TAG_CHARACTER[$type];
}
}
return Strings::startsWith($tag, $tag_chars);
}
}

View file

@ -2,9 +2,12 @@
namespace Friendica\Module;
use Friendica\Content\Text\HTML;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Model;
use Friendica\Protocol\ActivityPub\Processor;
use Friendica\Protocol\Diaspora;
/**
* @author Hypolite Petovan <mrpetovan@gmail.com>
@ -27,20 +30,27 @@ class Itemsource extends \Friendica\BaseModule
$source = '';
$item_uri = '';
$item_id = '';
$terms = [];
if (!empty($guid)) {
$item = Model\Item::selectFirst([], ['guid' => $guid]);
$item = Model\Item::selectFirst(['id', 'guid', 'uri'], ['guid' => $guid]);
$conversation = Model\Conversation::getByItemUri($item['uri']);
$guid = $item['guid'];
$item_id = $item['id'];
$item_uri = $item['uri'];
$source = $conversation['source'];
$terms = Model\Term::tagArrayFromItemId($item['id'], [Model\Term::HASHTAG, Model\Term::MENTION, Model\Term::IMPLICIT_MENTION]);
}
$tpl = Renderer::getMarkupTemplate('debug/itemsource.tpl');
$o = Renderer::replaceMacros($tpl, [
'$guid' => ['guid', L10n::t('Item Guid'), defaults($_REQUEST, 'guid', ''), ''],
'$guid' => ['guid', L10n::t('Item Guid'), $guid, ''],
'$source' => $source,
'$item_uri' => $item_uri
'$item_uri' => $item_uri,
'$item_id' => $item_id,
'$terms' => $terms,
]);
return $o;

View file

@ -83,7 +83,7 @@ class Post extends BaseObject
$author = ['uid' => 0, 'id' => $this->getDataValue('author-id'),
'network' => $this->getDataValue('author-network'),
'url' => $this->getDataValue('author-link')];
$this->redirect_url = Contact::magicLinkbyContact($author);
$this->redirect_url = Contact::magicLinkByContact($author);
if (!$this->isToplevel()) {
$this->threaded = true;
}
@ -224,7 +224,7 @@ class Post extends BaseObject
'network' => $item['author-network'], 'url' => $item['author-link']];
if (local_user() || remote_user()) {
$profile_link = Contact::magicLinkbyContact($author);
$profile_link = Contact::magicLinkByContact($author);
} else {
$profile_link = $item['author-link'];
}
@ -366,6 +366,7 @@ class Post extends BaseObject
'tags' => $tags['tags'],
'hashtags' => $tags['hashtags'],
'mentions' => $tags['mentions'],
'implicit_mentions' => $tags['implicit_mentions'],
'txt_cats' => L10n::t('Categories:'),
'txt_folders' => L10n::t('Filed under:'),
'has_cats' => ((count($categories)) ? 'true' : ''),
@ -804,7 +805,7 @@ class Post extends BaseObject
$text = '';
}
$terms = Term::tagArrayFromItemId($this->getId(), TERM_MENTION);
$terms = Term::tagArrayFromItemId($this->getId(), [Term::MENTION, Term::IMPLICIT_MENTION]);
foreach ($terms as $term) {
$profile = Contact::getDetailsByURL($term['url']);
@ -948,7 +949,7 @@ class Post extends BaseObject
$owner = ['uid' => 0, 'id' => $this->getDataValue('owner-id'),
'network' => $this->getDataValue('owner-network'),
'url' => $this->getDataValue('owner-link')];
$this->owner_url = Contact::magicLinkbyContact($owner);
$this->owner_url = Contact::magicLinkByContact($owner);
}
}
}

View file

@ -65,7 +65,7 @@ class Processor
* @param array $implicit_mentions List of profile URLs to skip
* @return string with tags
*/
private static function constructTagString($tags, $sensitive, array $implicit_mentions)
private static function constructTagString(array $tags, $sensitive)
{
if (empty($tags)) {
return '';
@ -73,7 +73,7 @@ class Processor
$tag_text = '';
foreach ($tags as $tag) {
if (in_array(defaults($tag, 'type', ''), ['Mention', 'Hashtag']) && !in_array($tag['href'], $implicit_mentions)) {
if (in_array(defaults($tag, 'type', ''), ['Mention', 'Hashtag'])) {
if (!empty($tag_text)) {
$tag_text .= ',';
}
@ -129,7 +129,7 @@ class Processor
*/
public static function updateItem($activity)
{
$item = Item::selectFirst(['uri', 'parent-uri', 'gravity'], ['uri' => $activity['id']]);
$item = Item::selectFirst(['uri', 'thr-parent', 'gravity'], ['uri' => $activity['id']]);
if (!DBA::isResult($item)) {
Logger::warning('Unknown item', ['uri' => $activity['id']]);
return;
@ -144,20 +144,20 @@ class Processor
$content = self::replaceEmojis($content, $activity['emojis']);
$content = self::convertMentions($content);
$implicit_mentions = [];
if (($item['parent-uri'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
$parent = Item::selectFirst(['id', 'author-link', 'alias'], ['uri' => $item['parent-uri']]);
if (($item['thr-parent'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
$parent = Item::selectFirst(['id', 'author-link', 'alias'], ['uri' => $item['thr-parent']]);
if (!DBA::isResult($parent)) {
Logger::warning('Unknown parent item.', ['uri' => $item['parent-uri']]);
Logger::warning('Unknown parent item.', ['uri' => $item['thr-parent']]);
return;
}
$implicit_mentions = self::getImplicitMentionList($parent);
$content = self::removeImplicitMentionsFromBody($content, $implicit_mentions);
$potential_implicit_mentions = self::getImplicitMentionList($parent);
$content = self::removeImplicitMentionsFromBody($content, $potential_implicit_mentions);
$activity['tags'] = self::convertImplicitMentionsInTags($activity['tags'], $potential_implicit_mentions);
}
$item['body'] = $content;
$item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive'], $implicit_mentions);
$item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive']);
Item::update($item, ['uri' => $activity['id']]);
}
@ -173,7 +173,7 @@ class Processor
{
$item = [];
$item['verb'] = ACTIVITY_POST;
$item['parent-uri'] = $activity['reply-to-id'];
$item['thr-parent'] = $activity['reply-to-id'];
if ($activity['reply-to-id'] == $activity['id']) {
$item['gravity'] = GRAVITY_PARENT;
@ -220,7 +220,7 @@ class Processor
{
$item = [];
$item['verb'] = $verb;
$item['parent-uri'] = $activity['object_id'];
$item['thr-parent'] = $activity['object_id'];
$item['gravity'] = GRAVITY_ACTIVITY;
$item['object-type'] = ACTIVITY_OBJ_NOTE;
@ -275,8 +275,8 @@ class Processor
{
/// @todo What to do with $activity['context']?
if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['parent-uri']])) {
Logger::log('Parent ' . $item['parent-uri'] . ' not found, message will be discarded.', Logger::DEBUG);
if (($item['gravity'] != GRAVITY_PARENT) && !Item::exists(['uri' => $item['thr-parent']])) {
Logger::info('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]);
return;
}
@ -289,7 +289,7 @@ class Processor
$item['owner-link'] = $activity['actor'];
$item['owner-id'] = Contact::getIdForURL($activity['actor'], 0, true);
} else {
Logger::log('Ignoring actor because of thread completion.', Logger::DEBUG);
Logger::info('Ignoring actor because of thread completion.');
$item['owner-link'] = $item['author-link'];
$item['owner-id'] = $item['author-id'];
}
@ -299,21 +299,20 @@ class Processor
$content = self::replaceEmojis($content, $activity['emojis']);
$content = self::convertMentions($content);
$implicit_mentions = [];
if (($item['parent-uri'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
if (($item['thr-parent'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
$item_private = !in_array(0, $activity['item_receiver']);
$parent = Item::selectFirst(['id', 'private', 'author-link', 'alias'], ['uri' => $item['parent-uri']]);
$parent = Item::selectFirst(['id', 'private', 'author-link', 'alias'], ['uri' => $item['thr-parent']]);
if (!DBA::isResult($parent)) {
return;
}
if ($item_private && !$parent['private']) {
Logger::log('Item ' . $item['uri'] . ' is private but the parent ' . $item['parent-uri'] . ' is not. So we drop it.');
Logger::warning('Item is private but the parent is not. Dropping.', ['item-uri' => $item['uri'], 'thr-parent' => $item['thr-parent']]);
return;
}
$implicit_mentions = self::getImplicitMentionList($parent);
$content = self::removeImplicitMentionsFromBody($content, $implicit_mentions);
$potential_implicit_mentions = self::getImplicitMentionList($parent);
$content = self::removeImplicitMentionsFromBody($content, $potential_implicit_mentions);
$activity['tags'] = self::convertImplicitMentionsInTags($activity['tags'], $potential_implicit_mentions);
}
$item['created'] = $activity['published'];
@ -333,7 +332,7 @@ class Processor
$item['coord'] = $item['latitude'] . ' ' . $item['longitude'];
}
$item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive'], $implicit_mentions);
$item['tag'] = self::constructTagString($activity['tags'], $activity['sensitive']);
$item['app'] = $activity['generator'];
$item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
@ -358,7 +357,11 @@ class Processor
}
$item_id = Item::insert($item);
Logger::log('Storing for user ' . $item['uid'] . ': ' . $item_id);
if ($item_id) {
Logger::info('Item insertion successful', ['user' => $item['uid'], 'item_id' => $item_id]);
} else {
Logger::notice('Item insertion aborted', ['user' => $item['uid']]);
}
if ($item['uid'] == 0) {
$stored = $item_id;
@ -658,10 +661,18 @@ class Processor
*/
private static function getImplicitMentionList(array $parent)
{
$parent_terms = Term::tagArrayFromItemId($parent['id'], [TERM_MENTION]);
if (Config::get('system', 'disable_implicit_mentions')) {
return [];
}
$parent_terms = Term::tagArrayFromItemId($parent['id'], [Term::MENTION, Term::IMPLICIT_MENTION]);
$parent_author = Contact::getDetailsByURL($parent['author-link'], 0);
$implicit_mentions = [
$parent['author-link']
$parent_author['url'],
$parent_author['nurl'],
$parent_author['alias'],
];
if ($parent['alias']) {
@ -684,12 +695,12 @@ class Processor
* Strips from the body prepended implicit mentions
*
* @param string $body
* @param array $implicit_mentions List of profile URLs
* @param array $potential_mentions
* @return string
*/
private static function removeImplicitMentionsFromBody($body, array $implicit_mentions)
private static function removeImplicitMentionsFromBody($body, array $potential_mentions)
{
if (Config::get('system', 'disable_mentions_removal')) {
if (Config::get('system', 'disable_implicit_mentions')) {
return $body;
}
@ -697,7 +708,7 @@ class Processor
// Extract one prepended mention at a time from the body
while(preg_match('#^(@\[url=([^\]]+)].*?\[\/url]\s)(.*)#mis', $body, $matches)) {
if (!in_array($matches[2], $implicit_mentions) ) {
if (!in_array($matches[2], $potential_mentions) ) {
$kept_mentions[] = $matches[1];
}
@ -709,4 +720,50 @@ class Processor
return implode('', $kept_mentions);
}
private static function convertImplicitMentionsInTags($activity_tags, array $potential_mentions)
{
if (Config::get('system', 'disable_implicit_mentions')) {
return $activity_tags;
}
foreach ($activity_tags as $index => $tag) {
if (in_array($tag['href'], $potential_mentions)) {
$activity_tags[$index]['name'] = preg_replace(
'/' . preg_quote(Term::TAG_CHARACTER[Term::MENTION], '/') . '/',
Term::TAG_CHARACTER[Term::IMPLICIT_MENTION],
$activity_tags[$index]['name'],
1
);
}
}
return $activity_tags;
}
public static function testImplicitMentions($item, $source)
{
$parent = Item::selectFirst(['id', 'guid', 'author-link', 'alias'], ['uri' => $item['thr-parent']]);
$implicit_mentions = self::getImplicitMentionList($parent);
var_dump($implicit_mentions);
$object = json_decode($source, true)['object'];
var_dump($object);
$content = HTML::toBBCode($object['content']);
$content = self::convertMentions($content);
$activity = [
'tags' => $object['tag'],
'content' => $content
];
var_dump($activity);
$activity['content'] = Processor::removeImplicitMentionsFromBody($activity['content'], $implicit_mentions);
$activity['tags'] = Processor::convertImplicitMentionsInTags($activity['tags'], $implicit_mentions);
return $activity;
}
}

View file

@ -62,16 +62,16 @@ class Receiver
{
$http_signer = HTTPSignature::getSigner($body, $header);
if (empty($http_signer)) {
Logger::log('Invalid HTTP signature, message will be discarded.', Logger::DEBUG);
Logger::warning('Invalid HTTP signature, message will be discarded.');
return;
} else {
Logger::log('HTTP signature is signed by ' . $http_signer, Logger::DEBUG);
Logger::info('Valid HTTP signature', ['signer' => $http_signer]);
}
$activity = json_decode($body, true);
if (empty($activity)) {
Logger::log('Invalid body.', Logger::DEBUG);
Logger::warning('Invalid body.');
return;
}
@ -79,7 +79,7 @@ class Receiver
$actor = JsonLD::fetchElement($ldactivity, 'as:actor');
Logger::log('Message for user ' . $uid . ' is from actor ' . $actor, Logger::DEBUG);
Logger::info('Message for user ' . $uid . ' is from actor ' . $actor);
if (LDSignature::isSigned($activity)) {
$ld_signer = LDSignature::getSigner($activity);

View file

@ -343,7 +343,7 @@ class Transmitter
$actor_profile = APContact::getByURL($item['author-link']);
}
$terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
$terms = Term::tagArrayFromItemId($item['id'], [Term::MENTION, Term::IMPLICIT_MENTION]);
if (!$item['private']) {
$data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
@ -807,12 +807,12 @@ class Transmitter
{
$tags = [];
$terms = Term::tagArrayFromItemId($item['id']);
$terms = Term::tagArrayFromItemId($item['id'], [Term::HASHTAG, Term::MENTION, Term::IMPLICIT_MENTION]);
foreach ($terms as $term) {
if ($term['type'] == TERM_HASHTAG) {
if ($term['type'] == Term::HASHTAG) {
$url = System::baseUrl() . '/search?tag=' . urlencode($term['term']);
$tags[] = ['type' => 'Hashtag', 'href' => $url, 'name' => '#' . $term['term']];
} elseif ($term['type'] == TERM_MENTION) {
} elseif ($term['type'] == Term::MENTION || $term['type'] == Term::IMPLICIT_MENTION) {
$contact = Contact::getDetailsByURL($term['url']);
if (!empty($contact['addr'])) {
$mention = '@' . $contact['addr'];
@ -1439,6 +1439,10 @@ class Transmitter
private static function prependMentions($body, array $permission_block)
{
if (Config::get('system', 'disable_implicit_mentions')) {
return $body;
}
$mentions = [];
foreach ($permission_block['to'] as $profile_url) {

View file

@ -1546,7 +1546,7 @@ class DFRN
$author["network"] = $contact_old["network"];
} else {
if (!$onlyfetch) {
Logger::log("Contact ".$author["link"]." wasn't found for user ".$importer["importer_uid"]." XML: ".$xml, Logger::DEBUG);
Logger::debug("Contact ".$author["link"]." wasn't found for user ".$importer["importer_uid"]." XML: ".$xml);
}
$author["contact-unknown"] = true;

View file

@ -942,7 +942,7 @@ class Diaspora
$person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
if (DBA::isResult($person)) {
Logger::log("In cache " . print_r($person, true), Logger::DEBUG);
Logger::debug("In cache " . print_r($person, true));
// update record occasionally so it doesn't get stale
$d = strtotime($person["updated"]." +00:00");
@ -3675,7 +3675,7 @@ class Diaspora
&& !strstr($body, $profile['addr'])
&& !strstr($body, $profile_url)
) {
$body = '@[url=' . $profile_url . ']' . $profile['nick'] . '[/url] ' . $body;
$body = '@[url=' . $profile_url . ']' . $profile['name'] . '[/url] ' . $body;
}
return $body;
@ -3776,7 +3776,7 @@ class Diaspora
* @param array $item The item that will be exported
* @param array $owner the array of the item owner
*
* @return array The data for a comment
* @return array|false The data for a comment
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
private static function constructComment(array $item, array $owner)
@ -3788,30 +3788,40 @@ class Diaspora
return $result;
}
$parent = Item::selectFirst(['guid', 'author-link'], ['id' => $item["parent"], 'parent' => $item["parent"]]);
if (!DBA::isResult($parent)) {
$toplevel_item = Item::selectFirst(['guid', 'author-link'], ['id' => $item["parent"], 'parent' => $item["parent"]]);
if (!DBA::isResult($toplevel_item)) {
Logger::error('Missing parent conversation item', ['parent' => $item["parent"]]);
return false;
}
$thread_parent_item = $toplevel_item;
if ($item['thr-parent'] != $item['parent-uri']) {
$thread_parent_item = Item::selectFirst(['guid', 'author-link'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]);
}
$body = $item["body"];
if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
$body = self::prependParentAuthorMention($body, $parent['author-link']);
if ((empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions'))
&& !Config::get('system', 'disable_implicit_mentions')
) {
$body = self::prependParentAuthorMention($body, $thread_parent_item['author-link']);
}
$text = html_entity_decode(BBCode::toMarkdown($body));
$created = DateTimeFormat::utc($item["created"], DateTimeFormat::ATOM);
$comment = ["author" => self::myHandle($owner),
"guid" => $item["guid"],
"created_at" => $created,
"parent_guid" => $parent["guid"],
"text" => $text,
"author_signature" => ""];
$comment = [
"author" => self::myHandle($owner),
"guid" => $item["guid"],
"created_at" => $created,
"parent_guid" => $toplevel_item["guid"],
"text" => $text,
"author_signature" => ""
];
// Send the thread parent guid only if it is a threaded comment
if ($item['thr-parent'] != $item['parent-uri']) {
$comment['thread_parent_guid'] = self::getGuidFromUri($item['thr-parent'], $item['uid']);
$comment['thread_parent_guid'] = $thread_parent_item['guid'];
}
Cache::set($cachekey, $comment, Cache::QUARTER_HOUR);

View file

@ -331,4 +331,19 @@ class Strings
return $uri;
}
/**
* Check if the trimmed provided string is starting with one of the provided characters
*
* @param string $string
* @param array $chars
* @return bool
*/
public static function startsWith($string, array $chars)
{
$return = in_array(substr(trim($string), 0, 1), $chars);
return $return;
}
}

View file

@ -6,6 +6,7 @@ namespace Friendica\Util;
use Friendica\Core\Logger;
use DOMXPath;
use Friendica\Core\System;
use SimpleXMLElement;
/**
@ -422,10 +423,11 @@ class XML
$x = @simplexml_load_string($s);
if (!$x) {
Logger::log('libxml: parse: error: ' . $s, Logger::DATA);
Logger::error('Error(s) while parsing XML string.', ['callstack' => System::callstack()]);
foreach (libxml_get_errors() as $err) {
Logger::log('libxml: parse: ' . $err->code." at ".$err->line.":".$err->column." : ".$err->message, Logger::DATA);
Logger::info('libxml error', ['code' => $err->code, 'position' => $err->line . ":" . $err->column, 'message' => $err->message]);
}
Logger::debug('Erroring XML string', ['xml' => $s]);
libxml_clear_errors();
}
return $x;