1
0
Fork 0

OStatus: Notifier for Friendica, better thread completion

This commit is contained in:
Michael Vogel 2015-06-12 00:39:56 +02:00
parent e02546880f
commit ad45ff7eb1
4 changed files with 82 additions and 32 deletions

View file

@ -1189,6 +1189,18 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
} }
} }
// If there is no guid then take the same guid that was taken before for the same plink
if ((trim($arr['guid']) == "") AND (trim($arr['plink']) != "") AND (trim($arr['network']) != "")) {
logger('item_store: checking for an existing guid for plink '.$arr['plink'], LOGGER_DEBUG);
$r = q("SELECT `guid` FROM `guid` WHERE `plink` = '%s' AND `network` = '%s' LIMIT 1",
dbesc(trim($arr['plink'])), dbesc(trim($arr['network'])));
if(count($r)) {
$arr['guid'] = $r[0]["guid"];
logger('item_store: found guid '.$arr['guid'].' for plink '.$arr['plink'], LOGGER_DEBUG);
}
}
// Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin. // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
// Deactivated, since the bbcode parser can handle with it - and it destroys posts with some smileys that contain "<" // Deactivated, since the bbcode parser can handle with it - and it destroys posts with some smileys that contain "<"
//if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false)) //if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))

View file

@ -301,26 +301,42 @@ function notifier_run(&$argv, &$argc){
$target_item['deny_cid'].$target_item['deny_gid']) == 0)) $target_item['deny_cid'].$target_item['deny_gid']) == 0))
$push_notify = true; $push_notify = true;
if ($parent['network'] == NETWORK_OSTATUS) { $thr_parent = q("SELECT `network` FROM `item` WHERE `uri` = '%s' AMD `uid` = %d",
logger('Parent is OStatus', LOGGER_DEBUG); dbesc($target_item["thr-parent"]), intval($target_item["uid"]));
// If the thread parent is OStatus then do some magic to distribute the messages.
// We have not only to look at the parent, since it could be a Friendica thread.
if (($thr_parent AND ($thr_parent['network'] == NETWORK_OSTATUS)) OR ($parent['network'] == NETWORK_OSTATUS)) {
logger('Parent is '.$parent['network'].'. Thread parent is '.$thr_parent['network'], LOGGER_DEBUG);
$push_notify = true; $push_notify = true;
// Send a salmon notification to every person we mentioned in the post // Send a salmon notification to every person we mentioned in the post
// To-Do: Send a Salmon to every Friendica user in that thread
$arr = explode(',',$target_item['tag']); $arr = explode(',',$target_item['tag']);
foreach($arr as $x) { foreach($arr as $x) {
logger('Checking tag '.$x, LOGGER_DEBUG); //logger('Checking tag '.$x, LOGGER_DEBUG);
$matches = null; $matches = null;
if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) { if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
$probed_contact = probe_url($matches[1]); $probed_contact = probe_url($matches[1]);
if ($probed_contact["notify"] != "") { if ($probed_contact["notify"] != "") {
logger('scrape data for slapper: '.print_r($probed_contact, true)); logger('Notify mentioned user '.$probed_contact["url"].': '.$probed_contact["notify"]);
$url_recipients[$probed_contact["notify"]] = $probed_contact["notify"]; $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
} }
} }
} }
// We notify Friendica users in the thread when it is an OStatus thread.
// Hopefully this transfers the messages to the other Friendica servers. (Untested)
if ($parent["network"] == NETWORK_OSTATUS) {
$r = q("SELECT `author-link` FROM `item` WHERE `parent` = %d", intval($target_item["parent"]));
foreach($r as $parent_item) {
$probed_contact = probe_url($parent_item["author-link"]);
if (($probed_contact["notify"] != "") AND ($probed_contact["network"] == NETWORK_DFRN)) {
logger('Notify Friendica user '.$probed_contact["url"].': '.$probed_contact["notify"]);
$url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
}
}
}
/* /*
// Check if the recipient isn't in your contact list, try to slap it // Check if the recipient isn't in your contact list, try to slap it
// Not sure if it is working or not. // Not sure if it is working or not.

View file

@ -61,9 +61,6 @@ function ostatus_fetchauthor($xpath, $context, $importer, &$contact) {
function ostatus_import($xml,$importer,&$contact, &$hub) { function ostatus_import($xml,$importer,&$contact, &$hub) {
// To-Do:
// Hub
$a = get_app(); $a = get_app();
logger("Import OStatus message", LOGGER_DEBUG); logger("Import OStatus message", LOGGER_DEBUG);
@ -287,8 +284,17 @@ function ostatus_import($xml,$importer,&$contact, &$hub) {
$orig_contact = $contact; $orig_contact = $contact;
$orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact); $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
$prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri); if (!intval(get_config('system','wall-to-wall_share'))) {
$item["body"] = $prefix.html2bbcode($orig_body)."[/share]"; $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
$item["body"] = $prefix.add_page_info_to_body(html2bbcode($orig_body))."[/share]";
} else {
$author["author-name"] = $orig_author["author-name"];
$author["author-link"] = $orig_author["author-link"];
$author["author-avatar"] = $orig_author["author-avatar"];
$item["body"] = add_page_info_to_body(html2bbcode($orig_body));
$item["uri"] = $orig_uri;
$item["created"] = $orig_created;
}
$item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue; $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
$item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue; $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;

View file

@ -1,5 +1,6 @@
<?php <?php
require_once("include/Contact.php"); require_once("include/Contact.php");
require_once("include/threads.php");
define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
define('OSTATUS_DEFAULT_POLL_TIMEFRAME', 1440); // given in minutes define('OSTATUS_DEFAULT_POLL_TIMEFRAME', 1440); // given in minutes
@ -93,7 +94,7 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
$r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `created`, `received`, `guid`) VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `created`, `received`, `guid`) VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s')",
intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION),
dbesc($message["created"]), dbesc($conversation_url), dbesc($message["created"]), dbesc($message["received"]), dbesc($message["guid"])); dbesc($message["created"]), dbesc($conversation_url), dbesc($message["created"]), dbesc($message["received"]), dbesc($message["guid"]));
logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid); logger('Storing conversation url '.$conversation_url.' for id '.$itemid);
} }
if ($only_add_conversation) if ($only_add_conversation)
@ -113,7 +114,7 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
$pageno = 1; $pageno = 1;
$items = array(); $items = array();
logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid); logger('fetching conversation url '.$conv.' for id '.$itemid.' and parent '.$parent["id"]);
do { do {
$conv_as = fetch_url($conv."?page=".$pageno); $conv_as = fetch_url($conv."?page=".$pageno);
@ -152,16 +153,21 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
if ($first_id == "") { if ($first_id == "") {
$first_id = $single_conv->id; $first_id = $single_conv->id;
// To-Do: $new_parents = q("SELECT `id`, `parent`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
// Only fetch a new parent if the new one doesn't have parents
// It can happen that OStatus servers have incomplete threads.
$new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
intval($message["uid"]), dbesc($first_id), intval($message["uid"]), dbesc($first_id),
dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN)); dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
if ($new_parents) { if ($new_parents) {
$parent = $new_parents[0]; // It can happen that OStatus servers have incomplete threads.
if ($parent["id"] != $message["parent"]) // Then keep the current parent
logger('Fetch new parent id '.$parent["id"].' for '.$itemid.' Old parent: '.$message["parent"]); if ($parent["id"] == $parent["parent"]) {
$parent = $new_parents[0];
if ($parent["id"] != $message["parent"])
logger('Fetch new parent id '.$parent["id"].' - Old parent: '.$message["parent"]);
} else {
$first_id = $parent["uri"];
//logger('Keep parent for '.$itemid.' - Old parent: '.$message["parent"]);
}
} else { } else {
$parent["id"] = 0; $parent["id"] = 0;
$parent["uri"] = $first_id; $parent["uri"] = $first_id;
@ -186,16 +192,28 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
if ($parent["id"] != 0) { if ($parent["id"] != 0) {
$existing_message = $message_exists[0]; $existing_message = $message_exists[0];
// Normally this shouldn't happen anymore, since we improved the way we fetch OStatus messages // We improved the way we fetch OStatus messages, this shouldn't happen very often now
if ($existing_message["parent"] != $parent["id"]) { if ($existing_message["parent"] != $parent["id"]) {
logger('updating id '.$existing_message["id"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG); logger('updating id '.$existing_message["id"].' with parent '.$existing_message["parent"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG);
// This is partly bad, since the entry in the thread table isn't updated // Update the parent id of the selected item
$r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d", $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `id` = %d",
intval($parent["id"]), intval($parent["id"]), dbesc($parent["uri"]), intval($existing_message["id"]));
dbesc($parent["uri"]),
dbesc($parent_uri), // Update the parent uri in the thread - but only if it points to itself
intval($existing_message["id"])); $r = q("UPDATE `item` SET `thr-parent` = '%s' WHERE `id` = %d AND `uri` = `thr-parent`",
dbesc($parent_uri), intval($existing_message["id"]));
// try to change all items of the same parent
$r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `parent` = %d",
intval($parent["id"]), dbesc($parent["uri"]), intval($existing_message["parent"]));
// Update the parent uri in the thread - but only if it points to itself
$r = q("UPDATE `item` SET `thr-parent` = '%s' WHERE (`parent` = %d) AND (`uri` = `thr-parent`)",
dbesc($parent["uri"]), intval($existing_message["parent"]));
// Now delete the thread
delete_thread($existing_message["parent"]);
} }
} }
continue; continue;
@ -275,21 +293,19 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
$newitem = item_store($arr); $newitem = item_store($arr);
logger('Stored new item '.$plink.' under id '.$newitem, LOGGER_DEBUG); logger('Stored new item '.$plink.' for parent '.$arr["parent"].' under id '.$newitem, LOGGER_DEBUG);
// Add the conversation entry (but don't fetch the whole conversation) // Add the conversation entry (but don't fetch the whole conversation)
complete_conversation($newitem, $conversation_url, true); complete_conversation($newitem, $conversation_url, true);
// If the newly created item is the top item then change the parent settings of the thread // If the newly created item is the top item then change the parent settings of the thread
// This shouldn't happen anymore. This could is supposed to be absolote. // This shouldn't happen anymore. This is supposed to be absolote.
if ($newitem AND ($arr["uri"] == $first_id)) { if ($newitem AND ($arr["uri"] == $first_id)) {
logger('setting new parent to id '.$newitem); logger('setting new parent to id '.$newitem);
$new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1", $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
intval($message["uid"]), intval($newitem)); intval($message["uid"]), intval($newitem));
if ($new_parents) { if ($new_parents)
$parent = $new_parents[0]; $parent = $new_parents[0];
logger('done changing parents to parent '.$newitem);
}
} }
} }
} }