use Friendica\Core\System;
use Friendica\Core\Config;
use Friendica\Network\Probe;
+use Friendica\Util\Lock;
require_once 'include/Contact.php';
require_once 'include/threads.php';
}
}
}
-
$author["contact-id"] = $contact["id"];
- if ($author["author-link"] != "") {
+ $found = false;
+
+ if ($aliaslink != '') {
+ $condition = array("`uid` = ? AND `alias` = ? AND `network` != ?",
+ $importer["uid"], $aliaslink, NETWORK_STATUSNET);
+ $r = dba::select('contact', array(), $condition, array('limit' => 1));
+
+ if (dbm::is_result($r)) {
+ $found = true;
+ if ($r['blocked']) {
+ $r['id'] = -1;
+ }
+ $contact = $r;
+ $author["contact-id"] = $r["id"];
+ }
+ }
+
+ if (!$found && ($author["author-link"] != "")) {
if ($aliaslink == "") {
$aliaslink = $author["author-link"];
}
- $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` IN ('%s', '%s') AND `network` != '%s'",
- intval($importer["uid"]), dbesc(normalise_link($author["author-link"])),
- dbesc(normalise_link($aliaslink)), dbesc(NETWORK_STATUSNET));
+ $condition = array("`uid` = ? AND `nurl` IN (?, ?) AND `network` != ?", $importer["uid"],
+ normalise_link($author["author-link"]), normalise_link($aliaslink), NETWORK_STATUSNET);
+ $r = dba::select('contact', array(), $condition, array('limit' => 1));
if (dbm::is_result($r)) {
- $contact = $r[0];
- $author["contact-id"] = $r[0]["id"];
- $author["author-link"] = $r[0]["url"];
+ $found = true;
+ if ($r['blocked']) {
+ $r['id'] = -1;
+ }
+ $contact = $r;
+ $author["contact-id"] = $r["id"];
}
- } elseif ($addr != "") {
- // Should not happen
- $contact = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `addr` = ? AND `network` != ?",
+ }
+
+ if (!$found && ($addr != "")) {
+ $condition = array("`uid` = ? AND `addr` = ? AND `network` != ?",
$importer["uid"], $addr, NETWORK_STATUSNET);
+ $r = dba::select('contact', array(), $condition, array('limit' => 1));
- if (dbm::is_result($contact)) {
- $author["contact-id"] = $contact["id"];
- $author["author-link"] = $contact["url"];
+ if (dbm::is_result($r)) {
+ $found = true;
+ if ($r['blocked']) {
+ $r['id'] = -1;
+ }
+ $contact = $r;
+ $author["contact-id"] = $r["id"];
}
}
$author["owner-avatar"] = $author["author-avatar"];
// Only update the contacts if it is an OStatus contact
- if ($r && !$onlyfetch && ($contact["network"] == NETWORK_OSTATUS)) {
+ if ($r && ($r['id'] > 0) && !$onlyfetch && ($contact["network"] == NETWORK_OSTATUS)) {
+
+ // This contact is vital, so we awake it from the dead
+ unmark_for_death($contact);
// Update contact data
+ $current = $contact;
+ unset($current['name-date']);
+
// This query doesn't seem to work
// $value = $xpath->query("atom:link[@rel='salmon']", $context)->item(0)->nodeValue;
// if ($value != "")
// if ($value != "")
// $contact["poll"] = $value;
+ $contact['url'] = $author["author-link"];
+ $contact['nurl'] = normalise_link($contact['url']);
+
$value = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
if ($value != "")
$contact["alias"] = $value;
if ($value != "")
$contact["location"] = $value;
- if (($contact["name"] != $r[0]["name"]) || ($contact["nick"] != $r[0]["nick"]) || ($contact["about"] != $r[0]["about"]) ||
- ($contact["alias"] != $r[0]["alias"]) || ($contact["location"] != $r[0]["location"])) {
+ $contact['name-date'] = datetime_convert();
- logger("Update contact data for contact ".$contact["id"], LOGGER_DEBUG);
+ dba::update('contact', $contact, array('id' => $contact["id"]), $current);
- q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `alias` = '%s', `about` = '%s', `location` = '%s', `name-date` = '%s' WHERE `id` = %d",
- dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["alias"]),
- dbesc($contact["about"]), dbesc($contact["location"]),
- dbesc(datetime_convert()), intval($contact["id"]));
- }
-
- if (isset($author["author-avatar"]) && ($author["author-avatar"] != $r[0]['avatar'])) {
+ if (!empty($author["author-avatar"]) && ($author["author-avatar"] != $current['avatar'])) {
logger("Update profile picture for contact ".$contact["id"], LOGGER_DEBUG);
-
update_contact_avatar($author["author-avatar"], $importer["uid"], $contact["id"]);
}
// Ensure that we are having this contact (with uid=0)
- $cid = get_contact($author["author-link"], 0);
+ $cid = get_contact($aliaslink, 0);
if ($cid) {
+ $fields = array('url', 'nurl', 'name', 'nick', 'alias', 'about', 'location');
+ $old_contact = dba::select('contact', $fields, array('id' => $cid), array('limit' => 1));
+
// Update it with the current values
- q("UPDATE `contact` SET `url` = '%s', `name` = '%s', `nick` = '%s', `alias` = '%s',
- `about` = '%s', `location` = '%s',
- `success_update` = '%s', `last-update` = '%s'
- WHERE `id` = %d",
- dbesc($author["author-link"]), dbesc($contact["name"]), dbesc($contact["nick"]),
- dbesc($contact["alias"]), dbesc($contact["about"]), dbesc($contact["location"]),
- dbesc(datetime_convert()), dbesc(datetime_convert()), intval($cid));
+ $fields = array('url' => $author["author-link"], 'name' => $contact["name"],
+ 'nurl' => normalise_link($author["author-link"]),
+ 'nick' => $contact["nick"], 'alias' => $contact["alias"],
+ 'about' => $contact["about"], 'location' => $contact["location"],
+ 'success_update' => datetime_convert(), 'last-update' => datetime_convert());
+
+ dba::update('contact', $fields, array('id' => $cid), $old_contact);
// Update the avatar
update_contact_avatar($author["author-avatar"], 0, $cid);
}
if ($entries->length == 1) {
+ // We reformat the XML to make it better readable
$doc2 = new DOMDocument();
$doc2->loadXML($xml);
$doc2->preserveWhiteSpace = false;
$header["protocol"] = PROTOCOL_OSTATUS_SALMON;
$header["source"] = $xml2;
+ } elseif (!$initialize) {
+ return false;
}
// Fetch the first author
$entrylist[] = $entry;
}
- if (!$initialize && (count($entrylist) > 1)) {
- return false;
- }
-
foreach (array_reverse($entrylist) AS $entry) {
// fetch the author
$authorelement = $xpath->query('/atom:entry/atom:author', $entry);
+
+ if ($authorelement->length == 0) {
+ $authorelement = $xpath->query('atom:author', $entry);
+ }
+
if ($authorelement->length > 0) {
$author = self::fetchauthor($xpath, $entry, $importer, $contact, $stored);
}
$item = array_merge($header, $author);
+ $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
+
$item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
- /// Delete a message
- if ($item["verb"] == "qvitter-delete-notice" || $item["verb"] == ACTIVITY_DELETE) {
- // ignore "Delete" messages (by now)
- logger("Ignore delete message ".print_r($item, true));
+ // Delete a message
+ if (in_array($item["verb"], array('qvitter-delete-notice', ACTIVITY_DELETE, 'delete'))) {
+ self::deleteNotice($item);
+ continue;
+ }
+
+ if (in_array($item["verb"], array(NAMESPACE_OSTATUS."/unfavorite", ACTIVITY_UNFAVORITE))) {
+ // Ignore "Unfavorite" message
+ logger("Ignore unfavorite message ".print_r($item, true), LOGGER_DEBUG);
+ continue;
+ }
+
+ // Deletions come with the same uri, so we check for duplicates after processing deletions
+ if (dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item["uri"]))) {
+ logger('Post with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.', LOGGER_DEBUG);
continue;
+ } else {
+ logger('Processing post with URI '.$item["uri"].' for user '.$importer["uid"].'.', LOGGER_DEBUG);
}
if ($item["verb"] == ACTIVITY_JOIN) {
// ignore "Join" messages
- logger("Ignore join message ".print_r($item, true));
+ logger("Ignore join message ".print_r($item, true), LOGGER_DEBUG);
+ continue;
+ }
+
+ if ($item["verb"] == "http://mastodon.social/schema/1.0/block") {
+ // ignore mastodon "block" messages
+ logger("Ignore block message ".print_r($item, true), LOGGER_DEBUG);
continue;
}
continue;
}
- if ($item["verb"] == NAMESPACE_OSTATUS."/unfavorite") {
- // Ignore "Unfavorite" message
- logger("Ignore unfavorite message ".print_r($item, true));
- continue;
- }
-
if ($item["verb"] == ACTIVITY_FAVORITE) {
$orig_uri = $xpath->query("activity:object/atom:id", $entry)->item(0)->nodeValue;
logger("Favorite ".$orig_uri." ".print_r($item, true));
// http://activitystrea.ms/schema/1.0/rsvp-yes
if (!in_array($item["verb"], array(ACTIVITY_POST, ACTIVITY_LIKE, ACTIVITY_SHARE))) {
- logger("Unhandled verb ".$item["verb"]." ".print_r($item, true));
+ logger("Unhandled verb ".$item["verb"]." ".print_r($item, true), LOGGER_DEBUG);
}
self::processPost($xpath, $entry, $item, $importer);
if ($initialize && (count(self::$itemlist) > 0)) {
- // We will import it everytime, when it is started by our contacts
- $valid = !empty(self::$itemlist[0]['contact-id']);
- if (!$valid) {
- // If not, then it depends on this setting
- $valid = !Config::get('system','ostatus_full_threads');
- }
-
- if ($valid) {
- // But we will only import complete threads
- $valid = self::$itemlist[0]['uri'] == self::$itemlist[0]['parent-uri'];
- }
-
- if ($valid) {
- // Never post a thread when the only interaction by our contact was a like
- $valid = false;
- $verbs = array(ACTIVITY_POST, ACTIVITY_SHARE);
- foreach (self::$itemlist AS $item) {
- if (!empty($item['contact-id']) && in_array($item['verb'], $verbs)) {
- $valid = true;
+ if (self::$itemlist[0]['uri'] == self::$itemlist[0]['parent-uri']) {
+ // We will import it everytime, when it is started by our contacts
+ $valid = !empty(self::$itemlist[0]['contact-id']);
+ if (!$valid) {
+ // If not, then it depends on this setting
+ $valid = !Config::get('system','ostatus_full_threads');
+ }
+ if ($valid) {
+ // Never post a thread when the only interaction by our contact was a like
+ $valid = false;
+ $verbs = array(ACTIVITY_POST, ACTIVITY_SHARE);
+ foreach (self::$itemlist AS $item) {
+ if (!empty($item['contact-id']) && in_array($item['verb'], $verbs)) {
+ $valid = true;
+ }
}
}
+ } else {
+ // But we will only import complete threads
+ $valid = dba::exists('item', array('uid' => $importer["uid"], 'uri' => self::$itemlist[0]['parent-uri']));
}
if ($valid) {
$found = dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item["uri"]));
if ($found) {
logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already exists.", LOGGER_DEBUG);
+ } elseif ($item['contact-id'] < 0) {
+ logger("Item with uri ".$item["uri"]." is from a blocked contact.", LOGGER_DEBUG);
} else {
- $ret = item_store($item);
- logger('Item was stored with return value '.$ret);
+ // We are having duplicated entries. Hopefully this solves it.
+ if (Lock::set('ostatus_process_item_store')) {
+ $ret = item_store($item);
+ Lock::remove('ostatus_process_item_store');
+ logger("Item with uri ".$item["uri"]." for user ".$importer["uid"].' stored. Return value: '.$ret);
+ } else {
+ $ret = item_store($item);
+ logger("We couldn't lock - but tried to store the item anyway. Return value is ".$ret);
+ }
}
}
}
self::$itemlist = array();
}
+ logger('Processing done for post with URI '.$item["uri"].' for user '.$importer["uid"].'.', LOGGER_DEBUG);
}
return true;
}
+ private static function deleteNotice($item) {
+
+ $condition = array('uid' => $item['uid'], 'author-link' => $item['author-link'], 'uri' => $item['uri']);
+ $deleted = dba::select('item', array('id', 'parent-uri'), $condition, array('limit' => 1));
+ if (!dbm::is_result($deleted)) {
+ logger('Item from '.$item['author-link'].' with uri '.$item['uri'].' for user '.$item['uid']." wasn't found. We don't delete it. ");
+ return;
+ }
+
+ // Currently we don't have a central deletion function that we could use in this case. The function "item_drop" doesn't work for that case
+ dba::update('item', array('deleted' => true, 'title' => '', 'body' => '',
+ 'edited' => datetime_convert(), 'changed' => datetime_convert()),
+ array('id' => $deleted["id"]));
+
+ delete_thread($deleted["id"], $deleted["parent-uri"]);
+
+ logger('Deleted item with uri '.$item['uri'].' for user '.$item['uid']);
+ }
+
/**
* @brief Processes the XML for a post
*
* @param array $importer user record of the importing user
*/
private static function processPost($xpath, $entry, &$item, $importer) {
- $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
$item["body"] = html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue);
$item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
if (($item["object-type"] == ACTIVITY_OBJ_BOOKMARK) || ($item["object-type"] == ACTIVITY_OBJ_EVENT)) {
$item["body"] = html2bbcode($clear_text) . '[spoiler]' . $item["body"] . '[/spoiler]';
}
+ if (($self != '') && empty($item['protocol'])) {
+ self::fetchSelf($self, $item);
+ }
+
if (!empty($item["conversation-href"])) {
self::fetchConversation($item['conversation-href'], $item['conversation-uri']);
}
if (isset($item["parent-uri"]) && ($related != '')) {
- self::FetchRelated($related, $item["parent-uri"], $importer);
+ if (!dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item['parent-uri']))) {
+ self::fetchRelated($related, $item["parent-uri"], $importer);
+ } else {
+ logger('Reply with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.', LOGGER_DEBUG);
+ }
+
$item["type"] = 'remote-comment';
$item["gravity"] = GRAVITY_COMMENT;
} else {
$item["parent-uri"] = $item["uri"];
}
- if (($item['author-link'] != '') && !empty($header["protocol"])) {
+ if (($item['author-link'] != '') && !empty($item['protocol'])) {
$item = store_conversation($item);
}
self::$conv_list[$conversation] = true;
- $conversation_data = z_fetch_url($conversation);
+ $conversation_data = z_fetch_url($conversation, false, $redirects, array('accept_content' => 'application/atom+xml, text/html'));
if (!$conversation_data['success']) {
return;
$conv_data['source'] = $doc2->saveXML();
+ $condition = array('item-uri' => $conv_data['uri'],'protocol' => PROTOCOL_OSTATUS_FEED);
+ if (dba::exists('conversation', $condition)) {
+ logger('Delete deprecated entry for URI '.$conv_data['uri'], LOGGER_DEBUG);
+ dba::delete('conversation', array('item-uri' => $conv_data['uri']));
+ }
+
logger('Store conversation data for uri '.$conv_data['uri'], LOGGER_DEBUG);
store_conversation($conv_data);
}
}
+ /**
+ * @brief Fetch the own post so that it can be stored later
+ * @param array $item The item array
+ *
+ * We want to store the original data for later processing.
+ * This function is meant for cases where we process a feed with multiple entries.
+ * In that case we need to fetch the single posts here.
+ *
+ * @param string $self The link to the self item
+ */
+ private static function fetchSelf($self, &$item) {
+ $condition = array('`item-uri` = ? AND `protocol` IN (?, ?)', $self, PROTOCOL_DFRN, PROTOCOL_OSTATUS_SALMON);
+ if (dba::exists('conversation', $condition)) {
+ logger('Conversation '.$item['uri'].' is already stored.', LOGGER_DEBUG);
+ return;
+ }
+
+ $self_data = z_fetch_url($self);
+
+ if (!$self_data['success']) {
+ return;
+ }
+
+ // We reformat the XML to make it better readable
+ $doc = new DOMDocument();
+ $doc->loadXML($self_data['body']);
+ $doc->preserveWhiteSpace = false;
+ $doc->formatOutput = true;
+ $xml = $doc->saveXML();
+
+ $item["protocol"] = PROTOCOL_OSTATUS_SALMON;
+ $item["source"] = $xml;
+
+ logger('Conversation '.$item['uri'].' is now fetched.', LOGGER_DEBUG);
+ }
+
/**
* @brief Fetch related posts and processes them
*
}
$stored = false;
- $related_data = z_fetch_url($related);
+ $related_data = z_fetch_url($related, false, $redirects, array('accept_content' => 'application/atom+xml, text/html'));
if (!$related_data['success']) {
return;