Conversations should work now too.

This commit is contained in:
Michael Vogel 2016-03-04 23:28:43 +01:00
parent 2edf4548dc
commit ef3620c191

View File

@ -22,7 +22,7 @@ class xml {
if (!is_object($xml)) { if (!is_object($xml)) {
foreach($array as $key => $value) { foreach($array as $key => $value) {
$root = new SimpleXMLElement('<'.$key.'/>'); $root = new SimpleXMLElement("<".$key."/>");
array_to_xml($value, $root); array_to_xml($value, $root);
$dom = dom_import_simplexml($root)->ownerDocument; $dom = dom_import_simplexml($root)->ownerDocument;
@ -82,7 +82,7 @@ class diaspora {
dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DIASPORA),
dbesc($msg["author"]) dbesc($msg["author"])
); );
if(count($r)) { if($r) {
foreach($r as $rr) { foreach($r as $rr) {
logger("delivering to: ".$rr["username"]); logger("delivering to: ".$rr["username"]);
self::dispatch($rr,$msg); self::dispatch($rr,$msg);
@ -125,7 +125,7 @@ class diaspora {
case "conversation": case "conversation":
//return true; //return true;
return self::import_conversation($importer, $fields); return self::import_conversation($importer, $msg, $fields);
case "like": // Done case "like": // Done
return true; return true;
@ -320,7 +320,7 @@ class diaspora {
dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DIASPORA),
dbesc($handle) dbesc($handle)
); );
if (count($r)) { if ($r) {
$person = $r[0]; $person = $r[0];
logger("In cache ".print_r($r,true), LOGGER_DEBUG); logger("In cache ".print_r($r,true), LOGGER_DEBUG);
@ -336,7 +336,7 @@ class diaspora {
// Note that Friendica contacts will return a "Diaspora person" // Note that Friendica contacts will return a "Diaspora person"
// if Diaspora connectivity is enabled on their server // if Diaspora connectivity is enabled on their server
if (count($r) AND ($r["network"] === NETWORK_DIASPORA)) { if ($r AND ($r["network"] === NETWORK_DIASPORA)) {
self::add_fcontact($r, $update); self::add_fcontact($r, $update);
$person = $r; $person = $r;
} }
@ -415,17 +415,17 @@ class diaspora {
dbesc($handle) dbesc($handle)
); );
if ($r AND count($r)) if ($r)
return $r[0]; return $r[0];
$handle_parts = explode("@", $handle); $handle_parts = explode("@", $handle);
$nurl_sql = '%%://' . $handle_parts[1] . '%%/profile/' . $handle_parts[0]; $nurl_sql = "%%://".$handle_parts[1]."%%/profile/".$handle_parts[0];
$r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `nurl` LIKE '%s' LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `nurl` LIKE '%s' LIMIT 1",
dbesc(NETWORK_DFRN), dbesc(NETWORK_DFRN),
intval($uid), intval($uid),
dbesc($nurl_sql) dbesc($nurl_sql)
); );
if($r AND count($r)) if($r)
return $r[0]; return $r[0];
return false; return false;
@ -481,7 +481,7 @@ class diaspora {
dbesc($guid) dbesc($guid)
); );
if(count($r)) { if($r) {
logger("message ".$guid." already exists for user ".$uid); logger("message ".$guid." already exists for user ".$uid);
return false; return false;
} }
@ -566,7 +566,7 @@ class diaspora {
FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1", FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
intval($uid), dbesc($guid)); intval($uid), dbesc($guid));
if(!count($r)) { if(!$r) {
$result = self::store_by_guid($guid, $contact["url"], $uid); $result = self::store_by_guid($guid, $contact["url"], $uid);
if (!$result) { if (!$result) {
@ -585,7 +585,7 @@ class diaspora {
} }
} }
if (!count($r)) { if (!$r) {
logger("parent item not found: parent: ".$guid." item: ".$guid); logger("parent item not found: parent: ".$guid." item: ".$guid);
return false; return false;
} else } else
@ -722,62 +722,10 @@ class diaspora {
return $message_id; return $message_id;
} }
private function import_conversation($importer, $data) { private function import_conversation_message($importer, $contact, $data, $msg, $mesg) {
// @todo $guid = notags(unxmlify($data->guid));
print_r($data); $subject = notags(unxmlify($data->subject));
die(); $author = notags(unxmlify($data->author));
/*
$guid = notags(unxmlify($xml->guid));
$subject = notags(unxmlify($xml->subject));
$diaspora_handle = notags(unxmlify($xml->diaspora_handle));
$participant_handles = notags(unxmlify($xml->participant_handles));
$created_at = datetime_convert('UTC','UTC',notags(unxmlify($xml->created_at)));
$parent_uri = $diaspora_handle . ':' . $guid;
$messages = $xml->message;
if(! count($messages)) {
logger('empty conversation');
return;
}
$contact = self::get_allowed_contact_by_handle($importer, $sender, true)
if (!$contact)
return false;
$conversation = null;
$c = q("select * from conv where uid = %d and guid = '%s' limit 1",
intval($importer["uid"]),
dbesc($guid)
);
if(count($c))
$conversation = $c[0];
else {
$r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
intval($importer["uid"]),
dbesc($guid),
dbesc($diaspora_handle),
dbesc(datetime_convert('UTC','UTC',$created_at)),
dbesc(datetime_convert()),
dbesc($subject),
dbesc($participant_handles)
);
if($r)
$c = q("select * from conv where uid = %d and guid = '%s' limit 1",
intval($importer["uid"]),
dbesc($guid)
);
if(count($c))
$conversation = $c[0];
}
if(! $conversation) {
logger('diaspora_conversation: unable to create conversation.');
return;
}
foreach($messages as $mesg) {
$reply = 0; $reply = 0;
@ -786,63 +734,64 @@ class diaspora {
$msg_parent_author_signature = notags(unxmlify($mesg->parent_author_signature)); $msg_parent_author_signature = notags(unxmlify($mesg->parent_author_signature));
$msg_author_signature = notags(unxmlify($mesg->author_signature)); $msg_author_signature = notags(unxmlify($mesg->author_signature));
$msg_text = unxmlify($mesg->text); $msg_text = unxmlify($mesg->text);
$msg_created_at = datetime_convert('UTC','UTC',notags(unxmlify($mesg->created_at))); $msg_created_at = datetime_convert("UTC", "UTC", notags(unxmlify($mesg->created_at)));
$msg_diaspora_handle = notags(unxmlify($mesg->diaspora_handle)); $msg_author = notags(unxmlify($mesg->diaspora_handle));
$msg_conversation_guid = notags(unxmlify($mesg->conversation_guid)); $msg_conversation_guid = notags(unxmlify($mesg->conversation_guid));
if($msg_conversation_guid != $guid) { if($msg_conversation_guid != $guid) {
logger('diaspora_conversation: message conversation guid does not belong to the current conversation. ' . $xml); logger("message conversation guid does not belong to the current conversation.");
continue; return false;
} }
$body = diaspora2bb($msg_text); $body = diaspora2bb($msg_text);
$message_id = $msg_diaspora_handle . ':' . $msg_guid; $message_uri = $msg_author.":".$msg_guid;
$author_signed_data = $msg_guid . ';' . $msg_parent_guid . ';' . $msg_text . ';' . unxmlify($mesg->created_at) . ';' . $msg_diaspora_handle . ';' . $msg_conversation_guid; $author_signed_data = $msg_guid.";".$msg_parent_guid.";".$msg_text.";".unxmlify($mesg->created_at).";".$msg_author.";".$msg_conversation_guid;
$author_signature = base64_decode($msg_author_signature); $author_signature = base64_decode($msg_author_signature);
if(strcasecmp($msg_diaspora_handle,$msg["author"]) == 0) { if(strcasecmp($msg_author,$msg["author"]) == 0) {
$person = $contact; $person = $contact;
$key = $msg["key"]; $key = $msg["key"];
} } else {
else { $person = self::get_person_by_handle($msg_author);
$person = find_diaspora_person_by_handle($msg_diaspora_handle);
if(is_array($person) && x($person,'pubkey')) if (is_array($person) && x($person, "pubkey"))
$key = $person["pubkey"]; $key = $person["pubkey"];
else { else {
logger('diaspora_conversation: unable to find author details'); logger("unable to find author details");
continue; return false;
} }
} }
if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) { if (!rsa_verify($author_signed_data, $author_signature, $key, "sha256")) {
logger('diaspora_conversation: verification failed.'); logger("verification failed.");
continue; return false;
} }
if($msg_parent_author_signature) { if($msg_parent_author_signature) {
$owner_signed_data = $msg_guid . ';' . $msg_parent_guid . ';' . $msg_text . ';' . unxmlify($mesg->created_at) . ';' . $msg_diaspora_handle . ';' . $msg_conversation_guid; $owner_signed_data = $msg_guid.";".$msg_parent_guid.";".$msg_text.";".unxmlify($mesg->created_at).";".$msg_author.";".$msg_conversation_guid;
$parent_author_signature = base64_decode($msg_parent_author_signature); $parent_author_signature = base64_decode($msg_parent_author_signature);
$key = $msg["key"]; $key = $msg["key"];
if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) { if (!rsa_verify($owner_signed_data, $parent_author_signature, $key, "sha256")) {
logger('diaspora_conversation: owner verification failed.'); logger("owner verification failed.");
continue; return false;
} }
} }
$r = q("select id from mail where `uri` = '%s' limit 1", $r = q("SELECT `id` FROM `mail` WHERE `uri` = '%s' LIMIT 1",
dbesc($message_id) dbesc($message_uri)
); );
if(count($r)) { if($r) {
logger('diaspora_conversation: duplicate message already delivered.', LOGGER_DEBUG); logger("duplicate message already delivered.", LOGGER_DEBUG);
continue; return false;
} }
q("insert into mail ( `uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`) values ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')", q("INSERT INTO `mail` (`uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`)
VALUES (%d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
intval($importer["uid"]), intval($importer["uid"]),
dbesc($msg_guid), dbesc($msg_guid),
intval($conversation["id"]), intval($conversation["id"]),
@ -854,32 +803,86 @@ class diaspora {
dbesc($body), dbesc($body),
0, 0,
0, 0,
dbesc($message_id), dbesc($message_uri),
dbesc($parent_uri), dbesc($author.":".$guid),
dbesc($msg_created_at) dbesc($msg_created_at)
); );
q("update conv set updated = '%s' where id = %d", q("UPDATE `conv` SET `updated` = '%s' WHERE `id` = %d",
dbesc(datetime_convert()), dbesc(datetime_convert()),
intval($conversation["id"]) intval($conversation["id"])
); );
notification(array( notification(array(
'type' => NOTIFY_MAIL, "type" => NOTIFY_MAIL,
'notify_flags' => $importer["notify-flags"], "notify_flags" => $importer["notify-flags"],
'language' => $importer["language"], "language" => $importer["language"],
'to_name' => $importer["username"], "to_name" => $importer["username"],
'to_email' => $importer["email"], "to_email" => $importer["email"],
'uid' =>$importer["uid"], "uid" =>$importer["uid"],
'item' => array('subject' => $subject, 'body' => $body), "item" => array("subject" => $subject, "body" => $body),
'source_name' => $person["name"], "source_name" => $person["name"],
'source_link' => $person["url"], "source_link" => $person["url"],
'source_photo' => $person["thumb"], "source_photo" => $person["thumb"],
'verb' => ACTIVITY_POST, "verb" => ACTIVITY_POST,
'otype' => 'mail' "otype" => "mail"
)); ));
} }
*/
private function import_conversation($importer, $msg, $data) {
$guid = notags(unxmlify($data->guid));
$subject = notags(unxmlify($data->subject));
$created_at = datetime_convert("UTC", "UTC", notags(unxmlify($data->created_at)));
$author = notags(unxmlify($data->author));
$participants = notags(unxmlify($data->participants));
$messages = $data->message;
if (!count($messages)) {
logger("empty conversation");
return false;
}
$contact = self::get_allowed_contact_by_handle($importer, $msg["author"], true);
if (!$contact)
return false;
$conversation = null;
$c = q("SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
intval($importer["uid"]),
dbesc($guid)
);
if($c)
$conversation = $c[0];
else {
$r = q("INSERT INTO `conv` (`uid`, `guid`, `creator`, `created`, `updated`, `subject`, `recips`)
VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s')",
intval($importer["uid"]),
dbesc($guid),
dbesc($author),
dbesc(datetime_convert("UTC", "UTC", $created_at)),
dbesc(datetime_convert()),
dbesc($subject),
dbesc($participants)
);
if($r)
$c = q("SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
intval($importer["uid"]),
dbesc($guid)
);
if($c)
$conversation = $c[0];
}
if (!$conversation) {
logger("unable to create conversation.");
return;
}
foreach($messages as $mesg)
self::import_conversation_message($importer, $contact, $data, $msg, $mesg);
return true; return true;
} }
@ -1007,8 +1010,6 @@ EOT;
$author = notags(unxmlify($data->author)); $author = notags(unxmlify($data->author));
$conversation_guid = notags(unxmlify($data->conversation_guid)); $conversation_guid = notags(unxmlify($data->conversation_guid));
$parent_uri = $author.":".$parent_guid;
$contact = self::get_allowed_contact_by_handle($importer, $author, true); $contact = self::get_allowed_contact_by_handle($importer, $author, true);
if (!$contact) if (!$contact)
return false; return false;
@ -1019,7 +1020,7 @@ EOT;
intval($importer["uid"]), intval($importer["uid"]),
dbesc($conversation_guid) dbesc($conversation_guid)
); );
if(count($c)) if($c)
$conversation = $c[0]; $conversation = $c[0];
else { else {
logger("conversation not available."); logger("conversation not available.");
@ -1029,7 +1030,7 @@ EOT;
$reply = 0; $reply = 0;
$body = diaspora2bb($text); $body = diaspora2bb($text);
$message_id = $author.":".$guid; $message_uri = $author.":".$guid;
$person = self::get_person_by_handle($author); $person = self::get_person_by_handle($author);
if (!$person) { if (!$person) {
@ -1038,10 +1039,10 @@ EOT;
} }
$r = q("SELECT `id` FROM `mail` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", $r = q("SELECT `id` FROM `mail` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
dbesc($message_id), dbesc($message_uri),
intval($importer["uid"]) intval($importer["uid"])
); );
if(count($r)) { if($r) {
logger("duplicate message already delivered.", LOGGER_DEBUG); logger("duplicate message already delivered.", LOGGER_DEBUG);
return false; return false;
} }
@ -1059,8 +1060,8 @@ EOT;
dbesc($body), dbesc($body),
0, 0,
1, 1,
dbesc($message_id), dbesc($message_uri),
dbesc($parent_uri), dbesc($author.":".$parent_guid),
dbesc($created_at) dbesc($created_at)
); );
@ -1174,8 +1175,8 @@ EOT;
// @todo // @todo
print_r($data); print_r($data);
/* /*
$author = unxmlify($xml->author); $author = unxmlify($data->author);
$recipient = unxmlify($xml->recipient); $recipient = unxmlify($data->recipient);
if (!$author || !$recipient) if (!$author || !$recipient)
return; return;
@ -1200,7 +1201,7 @@ EOT;
intval($importer["uid"]) intval($importer["uid"])
); );
if((count($r)) && (!$r[0]["hide-friends"]) && (!$contact["hidden"]) && intval(get_pconfig($importer["uid"],'system','post_newfriend'))) { if($r && !$r[0]["hide-friends"] && !$contact["hidden"] && intval(get_pconfig($importer["uid"],'system','post_newfriend'))) {
$self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1", $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
intval($importer["uid"]) intval($importer["uid"])
@ -1208,7 +1209,7 @@ EOT;
// they are not CONTACT_IS_FOLLOWER anymore but that's what we have in the array // they are not CONTACT_IS_FOLLOWER anymore but that's what we have in the array
if(count($self) && $contact["rel"] == CONTACT_IS_FOLLOWER) { if($self && $contact["rel"] == CONTACT_IS_FOLLOWER) {
$arr = array(); $arr = array();
$arr["uri"] = $arr["parent-uri"] = item_new_uri(App::get_hostname(), $importer["uid"]); $arr["uri"] = $arr["parent-uri"] = item_new_uri(App::get_hostname(), $importer["uid"]);
@ -1224,17 +1225,16 @@ EOT;
$arr["verb"] = ACTIVITY_FRIEND; $arr["verb"] = ACTIVITY_FRIEND;
$arr["object-type"] = ACTIVITY_OBJ_PERSON; $arr["object-type"] = ACTIVITY_OBJ_PERSON;
$A = '[url=' . $self[0]["url"] . "]' . $self[0]["name"] . '[/url]'; $A = '[url='.$self[0]["url"] . "]'.$self[0]["name"] .'[/url]';
$B = '[url=' . $contact["url"] . "]' . $contact["name"] . '[/url]'; $B = '[url='.$contact["url"] . "]'.$contact["name"] .'[/url]';
$BPhoto = '[url=' . $contact["url"] . "]' . '[img]' . $contact["thumb"] . '[/img][/url]'; $BPhoto = '[url='.$contact["url"] . "]'.'[img]'.$contact["thumb"] .'[/img][/url]';
$arr["body"] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto; $arr["body"] = sprintf( t('%1$s is now friends with %2$s'), $A, $B)."\n\n\n".$Bphoto;
$arr["object"] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $contact["name"] . '</title>' $arr["object"] = '<object><type>'. ACTIVITY_OBJ_PERSON .'</type><title>'.$contact["name"] .'</title>'
. '<id>' . $contact["url"] . '/' . $contact["name"] . '</id>'; .'<id>'.$contact["url"] .'/'.$contact["name"] .'</id>';
$arr["object"] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $contact["url"] . '" />' . "\n") $arr["object"] .= '<link>'. xmlify('<link rel="alternate" type="text/html" href="'.$contact["url"] .'" />'. "\n");
; $arr["object"] .= xmlify('<link rel="photo" type="image/jpeg" href="'.$contact["thumb"] .'" />'. "\n");
$arr["object"] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $contact["thumb"] . '" />' . "\n"); $arr["object"] .= '</link></object>'. "\n";
$arr["object"] .= '</link></object>' . "\n";
$arr["last-child"] = 1; $arr["last-child"] = 1;
$arr["allow_cid"] = $user[0]["allow_cid"]; $arr["allow_cid"] = $user[0]["allow_cid"];
@ -1256,12 +1256,12 @@ EOT;
$ret = self::get_person_by_handle($author); $ret = self::get_person_by_handle($author);
if((! count($ret)) || ($ret["network"] != NETWORK_DIASPORA)) { if (!$ret || ($ret["network"] != NETWORK_DIASPORA)) {
logger('diaspora_request: Cannot resolve diaspora handle ' . $author . ' for ' . $recipient); logger('Cannot resolve diaspora handle '.$author .' for '.$recipient);
return; return;
} }
$batch = (($ret["batch"]) ? $ret["batch"] : implode('/', array_slice(explode('/',$ret["url"]),0,3)) . '/receive/public'); $batch = (($ret["batch"]) ? $ret["batch"] : implode('/', array_slice(explode('/',$ret["url"]),0,3)) .'/receive/public');
@ -1286,10 +1286,10 @@ EOT;
// find the contact record we just created // find the contact record we just created
$contact_record = diaspora_get_contact_by_handle($importer["uid"],$author); $contact_record = self::get_contact_by_handle($importer["uid"],$author);
if(! $contact_record) { if(! $contact_record) {
logger('diaspora_request: unable to locate newly created contact record.'); logger('unable to locate newly created contact record.');
return; return;
} }
@ -1360,7 +1360,7 @@ EOT;
FROM `item` WHERE `guid` = '%s' AND `visible` AND NOT `deleted` AND `body` != '' LIMIT 1", FROM `item` WHERE `guid` = '%s' AND `visible` AND NOT `deleted` AND `body` != '' LIMIT 1",
dbesc($guid)); dbesc($guid));
if(count($r)) { if($r) {
logger("reshared message ".$guid." already exists on system."); logger("reshared message ".$guid." already exists on system.");
// Maybe it is already a reshared item? // Maybe it is already a reshared item?
@ -1371,23 +1371,23 @@ EOT;
return $r[0]; return $r[0];
} }
if (!count($r)) { if (!$r) {
$server = 'https://'.substr($orig_author,strpos($orig_author,'@')+1); $server = "https://".substr($orig_author, strpos($orig_author, "@") + 1);
logger("1st try: reshared message ".$guid." will be fetched from original server: ".$server); logger("1st try: reshared message ".$guid." will be fetched from original server: ".$server);
$item_id = self::store_by_guid($guid, $server); $item_id = self::store_by_guid($guid, $server);
if (!$item_id) { if (!$item_id) {
$server = 'https://'.substr($author,strpos($author,'@')+1); $server = "https://".substr($author, strpos($author, "@") + 1);
logger("2nd try: reshared message ".$guid." will be fetched from sharer's server: ".$server); logger("2nd try: reshared message ".$guid." will be fetched from sharer's server: ".$server);
$item = self::store_by_guid($guid, $server); $item = self::store_by_guid($guid, $server);
} }
if (!$item_id) { if (!$item_id) {
$server = 'http://'.substr($orig_author,strpos($orig_author,'@')+1); $server = "http://".substr($orig_author, strpos($orig_author, "@") + 1);
logger("3rd try: reshared message ".$guid." will be fetched from original server: ".$server); logger("3rd try: reshared message ".$guid." will be fetched from original server: ".$server);
$item = self::store_by_guid($guid, $server); $item = self::store_by_guid($guid, $server);
} }
if (!$item_id) { if (!$item_id) {
$server = 'http://'.substr($author,strpos($author,'@')+1); $server = "http://".substr($author, strpos($author, "@") + 1);
logger("4th try: reshared message ".$guid." will be fetched from sharer's server: ".$server); logger("4th try: reshared message ".$guid." will be fetched from sharer's server: ".$server);
$item = self::store_by_guid($guid, $server); $item = self::store_by_guid($guid, $server);
} }