Merge pull request #1351 from annando/1501-central-item-storage

Diaspora: Storing of original posts of reshared posts
This commit is contained in:
Tobias Diekershoff 2015-02-03 20:38:25 +01:00
commit f39e2803f8
6 changed files with 239 additions and 26 deletions

View file

@ -1405,11 +1405,11 @@ if(! function_exists('get_max_import_size')) {
if(! function_exists('profile_load')) { if(! function_exists('profile_load')) {
function profile_load(&$a, $nickname, $profile = 0, $profiledata = array()) { function profile_load(&$a, $nickname, $profile = 0, $profiledata = array()) {
$user = q("select uid from user where nickname = '%s' limit 1", $user = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
dbesc($nickname) dbesc($nickname)
); );
if(! ($user && count($user))) { if(!$user && count($user) && !count($profiledata)) {
logger('profile error: ' . $a->query_string, LOGGER_DEBUG); logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
notice( t('Requested account is not available.') . EOL ); notice( t('Requested account is not available.') . EOL );
$a->error = 404; $a->error = 404;
@ -1440,7 +1440,7 @@ if(! function_exists('profile_load')) {
intval($profile_int) intval($profile_int)
); );
} }
if((! $r) && (! count($r))) { if((!$r) && (!count($r))) {
$r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile` $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid` INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` = 1 and `contact`.`self` = 1 LIMIT 1", WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` = 1 and `contact`.`self` = 1 LIMIT 1",
@ -1448,7 +1448,7 @@ if(! function_exists('profile_load')) {
); );
} }
if(($r === false) || (! count($r))) { if(($r === false) || (!count($r)) && !count($profiledata)) {
logger('profile error: ' . $a->query_string, LOGGER_DEBUG); logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
notice( t('Requested profile is not available.') . EOL ); notice( t('Requested profile is not available.') . EOL );
$a->error = 404; $a->error = 404;
@ -1457,7 +1457,7 @@ if(! function_exists('profile_load')) {
// fetch user tags if this isn't the default profile // fetch user tags if this isn't the default profile
if(! $r[0]['is-default']) { if(!$r[0]['is-default']) {
$x = q("select `pub_keywords` from `profile` where uid = %d and `is-default` = 1 limit 1", $x = q("select `pub_keywords` from `profile` where uid = %d and `is-default` = 1 limit 1",
intval($r[0]['profile_uid']) intval($r[0]['profile_uid'])
); );

View file

@ -15,7 +15,7 @@ function user_remove($uid) {
call_hooks('remove_user',$r[0]); call_hooks('remove_user',$r[0]);
// save username (actually the nickname as it is guaranteed // save username (actually the nickname as it is guaranteed
// unique), so it cannot be re-registered in the future. // unique), so it cannot be re-registered in the future.
q("insert into userd ( username ) values ( '%s' )", q("insert into userd ( username ) values ( '%s' )",
@ -191,7 +191,7 @@ if(! function_exists('contact_photo_menu')){
function contact_photo_menu($contact) { function contact_photo_menu($contact) {
$a = get_app(); $a = get_app();
$contact_url=""; $contact_url="";
$pm_url=""; $pm_url="";
$status_link=""; $status_link="";
@ -222,24 +222,24 @@ function contact_photo_menu($contact) {
$contact_url = $a->get_baseurl() . '/contacts/' . $contact['id']; $contact_url = $a->get_baseurl() . '/contacts/' . $contact['id'];
$posts_link = $a->get_baseurl() . '/network/0?nets=all&cid=' . $contact['id']; $posts_link = $a->get_baseurl() . '/network/0?nets=all&cid=' . $contact['id'];
$contact_drop_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/drop?confirm=1'; $contact_drop_link = $a->get_baseurl() . "/contacts/" . $contact['id'] . '/drop?confirm=1';
$menu = Array( $menu = Array(
'poke' => array(t("Poke"), $poke_link), 'poke' => array(t("Poke"), $poke_link),
'status' => array(t("View Status"), $status_link), 'status' => array(t("View Status"), $status_link),
'profile' => array(t("View Profile"), $profile_link), 'profile' => array(t("View Profile"), $profile_link),
'photos' => array(t("View Photos"), $photos_link), 'photos' => array(t("View Photos"), $photos_link),
'network' => array(t("Network Posts"), $posts_link), 'network' => array(t("Network Posts"), $posts_link),
'edit' => array(t("Edit Contact"), $contact_url), 'edit' => array(t("Edit Contact"), $contact_url),
'drop' => array(t("Drop Contact"), $contact_drop_link), 'drop' => array(t("Drop Contact"), $contact_drop_link),
'pm' => array(t("Send PM"), $pm_url), 'pm' => array(t("Send PM"), $pm_url),
); );
$args = array('contact' => $contact, 'menu' => &$menu); $args = array('contact' => $contact, 'menu' => &$menu);
call_hooks('contact_photo_menu', $args); call_hooks('contact_photo_menu', $args);
/* $o = ""; /* $o = "";
foreach($menu as $k=>$v){ foreach($menu as $k=>$v){
if ($v!="") { if ($v!="") {
@ -293,3 +293,107 @@ function contacts_not_grouped($uid,$start = 0,$count = 0) {
return $r; return $r;
} }
function get_contact($url, $uid = 0) {
require_once("include/Scrape.php");
$data = array();
$contactid = 0;
// is it an address in the format user@server.tld?
if (!strstr($url, "http") OR strstr($url, "@")) {
$data = probe_url($url);
$url = $data["url"];
if ($url == "")
return 0;
}
$contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
dbesc(normalise_link($url)),
intval($uid));
if (!$contact)
$contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d",
dbesc($url),
dbesc(normalise_link($url)),
intval($uid));
if ($contact) {
$contactid = $contact[0]["id"];
// Update the contact every 7 days
$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -7 days'));
//$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
if (!$update_photo)
return($contactid);
}
if (!count($data))
$data = probe_url($url);
// Does this address belongs to a valid network?
if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
return 0;
// tempory programming. Can be deleted after 2015-02-07
if (($data["alias"] == "") AND (normalise_link($data["url"]) != normalise_link($url)))
$data["alias"] = normalise_link($url);
if ($contactid == 0) {
q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
`name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
`batch`, `request`, `confirm`, `poco`,
`writable`, `blocked`, `readonly`, `pending`)
VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', 1, 0, 0, 0)",
intval($uid),
dbesc(datetime_convert()),
dbesc($data["url"]),
dbesc(normalise_link($data["url"])),
dbesc($data["addr"]),
dbesc($data["alias"]),
dbesc($data["notify"]),
dbesc($data["poll"]),
dbesc($data["name"]),
dbesc($data["nick"]),
dbesc($data["photo"]),
dbesc($data["network"]),
dbesc($data["pubkey"]),
intval(CONTACT_IS_SHARING),
intval($data["priority"]),
dbesc($data["batch"]),
dbesc($data["request"]),
dbesc($data["confirm"]),
dbesc($data["poco"])
);
$contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
dbesc(normalise_link($data["url"])),
intval($uid));
if (!$contact)
return 0;
$contactid = $contact[0]["id"];
}
require_once("Photo.php");
$photos = import_profile_photo($data["photo"],$uid,$contactid);
q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s',
`addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
`name-date` = '%s', `uri-date` = '%s', `avatar-date` = '%s' WHERE `id` = %d",
dbesc($photos[0]),
dbesc($photos[1]),
dbesc($photos[2]),
dbesc($data["addr"]),
dbesc($data["alias"]),
dbesc($data["name"]),
dbesc($data["nick"]),
dbesc(datetime_convert()),
dbesc(datetime_convert()),
dbesc(datetime_convert()),
intval($contactid)
);
return $contactid;
}

View file

@ -433,7 +433,7 @@ function probe_url($url, $mode = PROBE_NORMAL) {
// to a contact on incoming messages to prevent spam, and we won't know which one // to a contact on incoming messages to prevent spam, and we won't know which one
// to match. So in case of two, one of them is stored as an alias. Only store URL's // to match. So in case of two, one of them is stored as an alias. Only store URL's
// and not webfinger user@host aliases. If they've got more than two non-email style // and not webfinger user@host aliases. If they've got more than two non-email style
// aliases, let's hope we're lucky and get one that matches the feed author-uri because // aliases, let's hope we're lucky and get one that matches the feed author-uri because
// otherwise we're screwed. // otherwise we're screwed.
foreach($links as $link) { foreach($links as $link) {
@ -448,6 +448,10 @@ function probe_url($url, $mode = PROBE_NORMAL) {
} }
} }
} }
// If the profile is different from the url then the url is abviously an alias
if (($alias == "") AND ($profile != "") AND !$at_addr AND (normalise_link($profile) != normalise_link($url)))
$alias = $url;
} }
elseif($mode == PROBE_NORMAL) { elseif($mode == PROBE_NORMAL) {

View file

@ -895,19 +895,92 @@ function diaspora_post($importer,$xml,$msg) {
$datarray['visible'] = ((strlen($body)) ? 1 : 0); $datarray['visible'] = ((strlen($body)) ? 1 : 0);
DiasporaFetchGuid($datarray);
$message_id = item_store($datarray); $message_id = item_store($datarray);
//if($message_id) {
// q("update item set plink = '%s' where id = %d",
// dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
// intval($message_id)
// );
//}
return; return;
} }
function DiasporaFetchGuid($item) {
preg_replace_callback("&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
function ($match) use ($item){
return(DiasporaFetchGuidSub($match, $item));
},$item["body"]);
}
function DiasporaFetchGuidSub($match, $item) {
$a = get_app();
$author = parse_url($item["author-link"]);
$authorserver = $author["scheme"]."://".$author["host"];
$owner = parse_url($item["owner-link"]);
$ownerserver = $owner["scheme"]."://".$owner["host"];
if (!diaspora_store_by_guid($match[1], $authorserver))
diaspora_store_by_guid($match[1], $ownerserver);
}
function diaspora_store_by_guid($guid, $server) {
require_once("include/Contact.php");
logger("fetching item ".$guid." from ".$server, LOGGER_DEBUG);
$item = diaspora_fetch_message($guid, $server);
if (!$item)
return false;
$body = $item["body"];
$str_tags = $item["tag"];
$app = $item["app"];
$created = $item["created"];
$author = $item["author"];
$guid = $item["guid"];
$private = $item["private"];
$message_id = $author.':'.$guid;
$r = q("SELECT `id` FROM `item` WHERE `uid` = 0 AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
dbesc($message_id),
dbesc($guid)
);
if(count($r))
return $r[0]["id"];
$person = find_diaspora_person_by_handle($author);
$datarray = array();
$datarray['uid'] = 0;
$datarray['contact-id'] = get_contact($person['url'], 0);
$datarray['wall'] = 0;
$datarray['network'] = NETWORK_DIASPORA;
$datarray['guid'] = $guid;
$datarray['uri'] = $datarray['parent-uri'] = $message_id;
$datarray['changed'] = $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
$datarray['private'] = $private;
$datarray['parent'] = 0;
$datarray['plink'] = 'https://'.substr($author,strpos($author,'@')+1).'/posts/'.$guid;
$datarray['author-name'] = $person['name'];
$datarray['author-link'] = $person['url'];
$datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
$datarray['owner-name'] = $datarray['author-name'];
$datarray['owner-link'] = $datarray['author-link'];
$datarray['owner-avatar'] = $datarray['author-avatar'];
$datarray['body'] = $body;
$datarray['tag'] = $str_tags;
$datarray['app'] = $app;
$datarray['visible'] = ((strlen($body)) ? 1 : 0);
DiasporaFetchGuid($datarray);
$message_id = item_store($datarray);
// To-Do:
// Looking if there is some subscribe mechanism in Diaspora to get all comments for this post
return $message_id;
}
function diaspora_fetch_message($guid, $server, $level = 0) { function diaspora_fetch_message($guid, $server, $level = 0) {
if ($level > 5) if ($level > 5)
@ -1102,7 +1175,8 @@ function diaspora_reshare($importer,$xml,$msg) {
$orig_created = $item["created"]; $orig_created = $item["created"];
$orig_author = $item["author"]; $orig_author = $item["author"];
$orig_guid = $item["guid"]; $orig_guid = $item["guid"];
//$create_original_post = ($body != ""); $create_original_post = ($body != "");
$orig_url = $a->get_baseurl()."/display/".$orig_guid;
} }
} }
@ -1164,12 +1238,13 @@ function diaspora_reshare($importer,$xml,$msg) {
$datarray['visible'] = ((strlen($body)) ? 1 : 0); $datarray['visible'] = ((strlen($body)) ? 1 : 0);
// Store the original item of a reshare // Store the original item of a reshare
// Deactivated by now. Items without a matching contact can't be shown via "mod/display.php" by now.
if ($create_original_post) { if ($create_original_post) {
require_once("include/Contact.php");
$datarray2 = $datarray; $datarray2 = $datarray;
$datarray2['uid'] = 0; $datarray2['uid'] = 0;
$datarray2['contact-id'] = 0; $datarray2['contact-id'] = get_contact($person['url'], 0);
$datarray2['guid'] = $orig_guid; $datarray2['guid'] = $orig_guid;
$datarray2['uri'] = $datarray2['parent-uri'] = $orig_author.':'.$orig_guid; $datarray2['uri'] = $datarray2['parent-uri'] = $orig_author.':'.$orig_guid;
$datarray2['changed'] = $datarray2['created'] = $datarray2['edited'] = datetime_convert('UTC','UTC',$orig_created); $datarray2['changed'] = $datarray2['created'] = $datarray2['edited'] = datetime_convert('UTC','UTC',$orig_created);
@ -1183,11 +1258,13 @@ function diaspora_reshare($importer,$xml,$msg) {
$datarray2['owner-avatar'] = $datarray2['author-avatar']; $datarray2['owner-avatar'] = $datarray2['author-avatar'];
$datarray2['body'] = $body; $datarray2['body'] = $body;
DiasporaFetchGuid($datarray2);
$message_id = item_store($datarray2); $message_id = item_store($datarray2);
logger("Store original item ".$orig_guid." under message id ".$message_id); logger("Store original item ".$orig_guid." under message id ".$message_id);
} }
DiasporaFetchGuid($datarray);
$message_id = item_store($datarray); $message_id = item_store($datarray);
return; return;
@ -1280,6 +1357,7 @@ function diaspora_asphoto($importer,$xml,$msg) {
$datarray['app'] = 'Diaspora/Cubbi.es'; $datarray['app'] = 'Diaspora/Cubbi.es';
DiasporaFetchGuid($datarray);
$message_id = item_store($datarray); $message_id = item_store($datarray);
//if($message_id) { //if($message_id) {
@ -1460,6 +1538,7 @@ function diaspora_comment($importer,$xml,$msg) {
if(($parent_item['origin']) && (! $parent_author_signature)) if(($parent_item['origin']) && (! $parent_author_signature))
$datarray['app'] = 'Diaspora'; $datarray['app'] = 'Diaspora';
DiasporaFetchGuid($datarray);
$message_id = item_store($datarray); $message_id = item_store($datarray);
//if($message_id) { //if($message_id) {

View file

@ -66,9 +66,11 @@ function add_thread($itemid, $onlyshadow = false) {
if (!$r) { if (!$r) {
// Preparing public shadow (removing user specific data) // Preparing public shadow (removing user specific data)
require_once("include/items.php"); require_once("include/items.php");
require_once("include/Contact.php");
unset($item[0]['id']); unset($item[0]['id']);
$item[0]['uid'] = 0; $item[0]['uid'] = 0;
$item[0]['contact-id'] = 0; $item[0]['contact-id'] = get_contact($item[0]['author-link'], 0);
$public_shadow = item_store($item[0], false, false, true); $public_shadow = item_store($item[0], false, false, true);
logger("add_thread: Stored public shadow for post ".$itemid." under id ".$public_shadow, LOGGER_DEBUG); logger("add_thread: Stored public shadow for post ".$itemid." under id ".$public_shadow, LOGGER_DEBUG);

View file

@ -41,6 +41,18 @@ function display_init(&$a) {
$itemuid = $r[0]["uid"]; $itemuid = $r[0]["uid"];
} }
} }
// Is it an item with uid=0?
if ($nick == "") {
$r = q("SELECT `item`.`id`, `item`.`parent`, `item`.`author-name`,
`item`.`author-link`, `item`.`author-avatar`, `item`.`network`, `item`.`uid`, `item`.`body`
FROM `item` WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
AND `item`.`private` = 0 AND `item`.`uid` = 0
AND `item`.`guid` = '%s'", $a->argv[1]);
// AND `item`.`private` = 0 AND `item`.`wall` = 1
}
if (count($r)) { if (count($r)) {
if ($r[0]["id"] != $r[0]["parent"]) if ($r[0]["id"] != $r[0]["parent"])
$r = q("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid` FROM `item` $r = q("SELECT `id`, `author-name`, `author-link`, `author-avatar`, `network`, `body`, `uid` FROM `item`
@ -252,6 +264,18 @@ function display_content(&$a, $update = 0) {
$nick = $r[0]["nickname"]; $nick = $r[0]["nickname"];
} }
} }
if ($nick == "") {
$r = q("SELECT `item`.`id` FROM `item`
WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
AND `item`.`private` = 0 AND `item`.`uid` = 0
AND `item`.`guid` = '%s'", $a->argv[1]);
// AND `item`.`private` = 0 AND `item`.`wall` = 1
if (count($r)) {
$item_id = $r[0]["id"];
}
}
} }
} }