From cd1f3cde00612a4798fc0c42fd5daa37ff393964 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 30 Jan 2016 01:20:43 +0100 Subject: [PATCH] DFRN Deletions should now work too --- include/import-dfrn.php | 143 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 131 insertions(+), 12 deletions(-) diff --git a/include/import-dfrn.php b/include/import-dfrn.php index 3265eb6ff..fd5b71cad 100644 --- a/include/import-dfrn.php +++ b/include/import-dfrn.php @@ -1,13 +1,9 @@ attributes AS $attributes) { + if ($attributes->name == "ref") + $uri = $attributes->textContent; + if ($attributes->name == "when") + $when = $attributes->textContent; + } + if ($when) + $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s'); + else + $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s'); + + if (!$uri OR !is_array($contact)) + return false; + + $r = q("SELECT `item`.*, `contact`.`self` FROM `item` INNER JOIN `contact` on `item`.`contact-id` = `contact`.`id` + WHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1", + dbesc($uri), + intval($importer["uid"]), + intval($contact["id"]) + ); + if(count($r)) { + $item = $r[0]; + + if(!$item["deleted"]) + logger('deleting item '.$item["id"].' uri='.$item['uri'], LOGGER_DEBUG); + + if($item["object-type"] === ACTIVITY_OBJ_EVENT) { + logger("Deleting event ".$item["event-id"], LOGGER_DEBUG); + event_delete($item["event-id"]); + } + + if(($item["verb"] === ACTIVITY_TAG) && ($item["object-type"] === ACTIVITY_OBJ_TAGTERM)) { + $xo = parse_xml_string($item["object"],false); + $xt = parse_xml_string($item["target"],false); + if($xt->type === ACTIVITY_OBJ_NOTE) { + $i = q("SELECT `id`, `contact-id`, `tag` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", + dbesc($xt->id), + intval($importer["importer_uid"]) + ); + if(count($i)) { + + // For tags, the owner cannot remove the tag on the author's copy of the post. + + $owner_remove = (($item['contact-id'] == $i[0]['contact-id']) ? true: false); + $author_remove = (($item['origin'] && $item['self']) ? true : false); + $author_copy = (($item['origin']) ? true : false); + + if($owner_remove && $author_copy) + continue; + if($author_remove || $owner_remove) { + $tags = explode(',',$i[0]['tag']); + $newtags = array(); + if(count($tags)) { + foreach($tags as $tag) + if(trim($tag) !== trim($xo->body)) + $newtags[] = trim($tag); + } + q("UPDATE `item` SET `tag` = '%s' WHERE `id` = %d", + dbesc(implode(',',$newtags)), + intval($i[0]['id']) + ); + create_tags_from_item($i[0]['id']); + } + } + } + } + + if($item['uri'] == $item['parent-uri']) { + $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', + `body` = '', `title` = '' + WHERE `parent-uri` = '%s' AND `uid` = %d", + dbesc($when), + dbesc(datetime_convert()), + dbesc($item['uri']), + intval($importer['uid']) + ); + create_tags_from_itemuri($item['uri'], $importer['uid']); + create_files_from_itemuri($item['uri'], $importer['uid']); + update_thread_uri($item['uri'], $importer['uid']); + } else { + $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', + `body` = '', `title` = '' + WHERE `uri` = '%s' AND `uid` = %d", + dbesc($when), + dbesc(datetime_convert()), + dbesc($uri), + intval($importer['uid']) + ); + create_tags_from_itemuri($uri, $importer['uid']); + create_files_from_itemuri($uri, $importer['uid']); + if($item['last-child']) { + // ensure that last-child is set in case the comment that had it just got wiped. + q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ", + dbesc(datetime_convert()), + dbesc($item['parent-uri']), + intval($item['uid']) + ); + // who is the last child now? + $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `moderated` = 0 AND `uid` = %d + ORDER BY `created` DESC LIMIT 1", + dbesc($item['parent-uri']), + intval($importer['uid']) + ); + if(count($r)) { + q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d", + intval($r[0]['id']) + ); + } + } + } + } } - function import($xml,$importer) { + function import($xml,$importer, &$contact) { $a = get_app(); @@ -697,8 +810,10 @@ $onlyfetch = true; // Test $xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS); $xpath->registerNamespace('statusnet', NAMESPACE_STATUSNET); - $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `self`", intval($importer["uid"])); - $contact = $r[0]; + if (!$contact) { + $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `self`", intval($importer["uid"])); + $contact = $r[0]; + } $header = array(); $header["uid"] = $importer["uid"]; @@ -734,6 +849,10 @@ $onlyfetch = true; // Test foreach ($relocations AS $relocation) self::process_relocation($xpath, $relocation, $importer); + $deletions = $xpath->query('/atom:feed/at:deleted-entry'); + foreach ($deletions AS $deletion) + self::process_deletion($header, $xpath, $deletion, $importer, $contact); + $entries = $xpath->query('/atom:feed/atom:entry'); foreach ($entries AS $entry) self::process_entry($header, $xpath, $entry, $importer, $contact);