Merge pull request #3422 from annando/1705-diaspora-comments-2

Follow up for PR 3421 - some improvements
This commit is contained in:
Tobias Diekershoff 2017-05-03 07:29:54 +02:00 committed by GitHub
commit b6277924cf

View file

@ -333,6 +333,23 @@ class Diaspora {
return false; return false;
} }
if (!self::valid_posting($msg, $fields)) {
logger("Invalid posting");
return false;
}
// Is it a an action (comment, like, ...) for our own post?
if (isset($fields->parent_guid)) {
$guid = notags(unxmlify($fields->parent_guid));
$importer = self::importer_for_guid($guid);
if (is_array($importer)) {
logger("delivering to origin: ".$importer["name"]);
$message_id = self::dispatch($importer, $msg);
return $message_id;
}
}
// Now distribute it to the followers // Now distribute it to the followers
$r = q("SELECT `user`.* FROM `user` WHERE `user`.`uid` IN $r = q("SELECT `user`.* FROM `user` WHERE `user`.`uid` IN
(SELECT `contact`.`uid` FROM `contact` WHERE `contact`.`network` = '%s' AND `contact`.`addr` = '%s') (SELECT `contact`.`uid` FROM `contact` WHERE `contact`.`network` = '%s' AND `contact`.`addr` = '%s')
@ -346,9 +363,10 @@ class Diaspora {
logger("delivering to: ".$rr["username"]); logger("delivering to: ".$rr["username"]);
self::dispatch($rr, $msg); self::dispatch($rr, $msg);
} }
} elseif (!Config::get('system', 'relay_subscribe', false)) {
logger("Unwanted message from ".$sender." send by ".$_SERVER["REMOTE_ADDR"]." with ".$_SERVER["HTTP_USER_AGENT"].": ".print_r($msg, true), LOGGER_DEBUG);
} else { } else {
// Use a dummy importer to import the data for the public copy // Use a dummy importer to import the data for the public copy
// or for comments from unknown people
$importer = array("uid" => 0, "page-flags" => PAGE_FREELOVE); $importer = array("uid" => 0, "page-flags" => PAGE_FREELOVE);
$message_id = self::dispatch($importer, $msg); $message_id = self::dispatch($importer, $msg);
} }
@ -377,11 +395,6 @@ class Diaspora {
$type = $fields->getName(); $type = $fields->getName();
$social_relay = Config::get('system', 'relay_subscribe', false);
if (!$social_relay AND ($type == 'message')) {
logger("Unwanted message from ".$sender." send by ".$_SERVER["REMOTE_ADDR"]." with ".$_SERVER["HTTP_USER_AGENT"].": ".print_r($msg, true), LOGGER_DEBUG);
}
logger("Received message type ".$type." from ".$sender." for user ".$importer["uid"], LOGGER_DEBUG); logger("Received message type ".$type." from ".$sender." for user ".$importer["uid"], LOGGER_DEBUG);
switch ($type) { switch ($type) {
@ -828,17 +841,20 @@ class Diaspora {
logger("defining user ".$contact["nick"]." as friend"); logger("defining user ".$contact["nick"]." as friend");
} }
if (($contact["blocked"]) || ($contact["readonly"]) || ($contact["archive"])) // We don't seem to like that person
if ($contact["blocked"] || $contact["readonly"] || $contact["archive"]) {
return false; return false;
if ($contact["rel"] == CONTACT_IS_SHARING || $contact["rel"] == CONTACT_IS_FRIEND) // We are following this person? Then it is okay
} elseif (($contact["rel"] == CONTACT_IS_SHARING) || ($contact["rel"] == CONTACT_IS_FRIEND)) {
return true; return true;
if ($contact["rel"] == CONTACT_IS_FOLLOWER) // Is it a post to a community? That's good
if (($importer["page-flags"] == PAGE_COMMUNITY) OR $is_comment) } elseif (($contact["rel"] == CONTACT_IS_FOLLOWER) && ($importer["page-flags"] == PAGE_COMMUNITY)) {
return true; return true;
}
// Messages for the global users are always accepted // Messages for the global users and comments are always accepted
if ($importer["uid"] == 0) if (($importer["uid"] == 0) || $is_comment) {
return true; return true;
}
return false; return false;
} }
@ -856,8 +872,13 @@ class Diaspora {
$contact = self::contact_by_handle($importer["uid"], $handle); $contact = self::contact_by_handle($importer["uid"], $handle);
if (!$contact) { if (!$contact) {
logger("A Contact for handle ".$handle." and user ".$importer["uid"]." was not found"); logger("A Contact for handle ".$handle." and user ".$importer["uid"]." was not found");
// If a contact isn't found, we accept it anyway if it is a comment
if ($is_comment) {
return $importer;
} else {
return false; return false;
} }
}
if (!self::post_allow($importer, $contact, $is_comment)) { if (!self::post_allow($importer, $contact, $is_comment)) {
logger("The handle: ".$handle." is not allowed to post to user ".$importer["uid"]); logger("The handle: ".$handle." is not allowed to post to user ".$importer["uid"]);
@ -1111,9 +1132,9 @@ class Diaspora {
$cid = $r[0]["id"]; $cid = $r[0]["id"];
$network = $r[0]["network"]; $network = $r[0]["network"];
// We are receiving content from a user that is about to be terminated // We are receiving content from a user that possibly is about to be terminated
// This means the user is vital, so we remove a possible termination date. // This means the user is vital, so we remove a possible termination date.
unmark_for_death($contact); unmark_for_death($r[0]);
} else { } else {
$cid = $contact["id"]; $cid = $contact["id"];
$network = NETWORK_DIASPORA; $network = NETWORK_DIASPORA;
@ -1228,24 +1249,23 @@ class Diaspora {
} }
/** /**
* @brief Find the best importer for a comment * @brief Find the best importer for a comment, like, ...
* *
* @param array $importer Array of the importer user
* @param string $guid The guid of the item * @param string $guid The guid of the item
* *
* @return array the importer that fits the best * @return array|boolean the origin owner of that post - or false
*/ */
private static function importer_for_comment($importer, $guid) { private static function importer_for_guid($guid) {
$item = dba::fetch_first("SELECT `uid` FROM `item` WHERE `origin` AND `guid` = ? LIMIT 1", $guid); $item = dba::fetch_first("SELECT `uid` FROM `item` WHERE `origin` AND `guid` = ? LIMIT 1", $guid);
if (dbm::is_result($item)) { if (dbm::is_result($item)) {
logger("Found user ".$item['uid']." as owner of item ".$guid, LOGGER_DEBUG); logger("Found user ".$item['uid']." as owner of item ".$guid, LOGGER_DEBUG);
$contact = dba::fetch_first("SELECT * FROM `contact` WHERE `self` AND `uid` = ?", $item['uid']); $contact = dba::fetch_first("SELECT * FROM `contact` WHERE `self` AND `uid` = ?", $item['uid']);
if (dbm::is_result($contact)) { if (dbm::is_result($contact)) {
$importer = $contact; return $contact;
} }
} }
return $importer; return false;
} }
/** /**
@ -1277,11 +1297,6 @@ class Diaspora {
$thr_uri = ""; $thr_uri = "";
} }
// Find the best importer when there was no importer found
if ($importer["uid"] == 0) {
$importer = self::importer_for_comment($importer, $parent_guid);
}
$contact = self::allowed_contact_by_handle($importer, $sender, true); $contact = self::allowed_contact_by_handle($importer, $sender, true);
if (!$contact) { if (!$contact) {
return false; return false;