Merge pull request #599 from annando/fix-contact-update

Twitter: Fix so that public contacts are created
pull/608/head
Hypolite Petovan 2018-05-09 11:56:38 -04:00 committed by GitHub
commit 8047969799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 78 additions and 128 deletions

View File

@ -70,6 +70,7 @@ use Friendica\Core\L10n;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Model\GContact; use Friendica\Model\GContact;
use Friendica\Model\Contact;
use Friendica\Model\Group; use Friendica\Model\Group;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\ItemContent; use Friendica\Model\ItemContent;
@ -175,7 +176,7 @@ function twitter_follow(App $a, &$contact)
FROM `contact` WHERE `uid` = %d AND `nick` = '%s'", FROM `contact` WHERE `uid` = %d AND `nick` = '%s'",
intval($uid), intval($uid),
dbesc($nickname)); dbesc($nickname));
if (count($r)) { if (DBM::is_result($r)) {
$contact["contact"] = $r[0]; $contact["contact"] = $r[0];
} }
} }
@ -448,7 +449,7 @@ function twitter_post_hook(App $a, &$b)
dbesc($b["thr-parent"]), dbesc($b["thr-parent"]),
intval($b["uid"])); intval($b["uid"]));
if (!count($r)) { if (!DBM::is_result($r)) {
logger("twitter_post_hook: no parent found " . $b["thr-parent"]); logger("twitter_post_hook: no parent found " . $b["thr-parent"]);
return; return;
} else { } else {
@ -603,7 +604,7 @@ function twitter_post_hook(App $a, &$b)
logger('Send to Twitter failed: "' . print_r($result->errors, true) . '"'); logger('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
$r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid'])); $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid']));
if (count($r)) { if (DBM::is_result($r)) {
$a->contact = $r[0]["id"]; $a->contact = $r[0]["id"];
} }
@ -658,7 +659,7 @@ function twitter_cron(App $a, $b)
logger('twitter: cron_start'); logger('twitter: cron_start');
$r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1'"); $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1'");
if (count($r)) { if (DBM::is_result($r)) {
foreach ($r as $rr) { foreach ($r as $rr) {
logger('twitter: fetching for user ' . $rr['uid']); logger('twitter: fetching for user ' . $rr['uid']);
Worker::add(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 1, (int) $rr['uid']); Worker::add(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 1, (int) $rr['uid']);
@ -673,11 +674,11 @@ function twitter_cron(App $a, $b)
$abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400); $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
$r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1'"); $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1'");
if (count($r)) { if (DBM::is_result($r)) {
foreach ($r as $rr) { foreach ($r as $rr) {
if ($abandon_days != 0) { if ($abandon_days != 0) {
$user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit); $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
if (!count($user)) { if (!DBM::is_result($user)) {
logger('abandoned account: timeline from user ' . $rr['uid'] . ' will not be imported'); logger('abandoned account: timeline from user ' . $rr['uid'] . ' will not be imported');
continue; continue;
} }
@ -730,7 +731,7 @@ function twitter_expire(App $a, $b)
logger('twitter_expire: expire_start'); logger('twitter_expire: expire_start');
$r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()"); $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
if (count($r)) { if (DBM::is_result($r)) {
foreach ($r as $rr) { foreach ($r as $rr) {
logger('twitter_expire: user ' . $rr['uid']); logger('twitter_expire: user ' . $rr['uid']);
Item::expire($rr['uid'], $days, NETWORK_TWITTER, true); Item::expire($rr['uid'], $days, NETWORK_TWITTER, true);
@ -755,7 +756,7 @@ function twitter_prepare_body(App $a, &$b)
dbesc($item["thr-parent"]), dbesc($item["thr-parent"]),
intval(local_user())); intval(local_user()));
if (count($r)) { if (DBM::is_result($r)) {
$orig_post = $r[0]; $orig_post = $r[0];
$nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]); $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
@ -904,7 +905,7 @@ function twitter_queue_hook(App $a, &$b)
$qi = q("SELECT * FROM `queue` WHERE `network` = '%s'", $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
dbesc(NETWORK_TWITTER) dbesc(NETWORK_TWITTER)
); );
if (!count($qi)) { if (!DBM::is_result($qi)) {
return; return;
} }
@ -919,7 +920,7 @@ function twitter_queue_hook(App $a, &$b)
WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1", WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
intval($x['cid']) intval($x['cid'])
); );
if (!count($r)) { if (!DBM::is_result($r)) {
continue; continue;
} }
@ -971,136 +972,85 @@ function twitter_fix_avatar($avatar)
return $new_avatar; return $new_avatar;
} }
function twitter_fetch_contact($uid, $contact, $create_user) function twitter_fetch_contact($uid, $data, $create_user)
{ {
if ($contact->id_str == "") { if ($data->id_str == "") {
return -1; return -1;
} }
$avatar = twitter_fix_avatar($contact->profile_image_url_https); $avatar = twitter_fix_avatar($data->profile_image_url_https);
$url = "https://twitter.com/" . $data->screen_name;
$addr = $data->screen_name . "@twitter.com";
GContact::update(["url" => "https://twitter.com/" . $contact->screen_name, GContact::update(["url" => $url, "network" => NETWORK_TWITTER,
"network" => NETWORK_TWITTER, "photo" => $avatar, "hide" => true, "photo" => $avatar, "hide" => true,
"name" => $contact->name, "nick" => $contact->screen_name, "name" => $data->name, "nick" => $data->screen_name,
"location" => $contact->location, "about" => $contact->description, "location" => $data->location, "about" => $data->description,
"addr" => $contact->screen_name . "@twitter.com", "generation" => 2]); "addr" => $addr, "generation" => 2]);
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", $fields = ['url' => $url, 'network' => NETWORK_TWITTER,
intval($uid), 'name' => $data->name, 'nick' => $data->screen_name, 'addr' => $addr,
dbesc("twitter::" . $contact->id_str)); 'location' => $data->location, 'about' => $data->description];
if (!count($r) && !$create_user) { $cid = Contact::getIdForURL($url, 0, true, $fields);
if (!empty($cid)) {
dba::update('contact', $fields, ['id' => $cid]);
Contact::updateAvatar($avatar, 0, $cid);
}
$contact = dba::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
if (!DBM::is_result($contact) && !$create_user) {
return 0; return 0;
} }
if (count($r) && ($r[0]["readonly"] || $r[0]["blocked"])) { if (!DBM::is_result($contact)) {
logger("twitter_fetch_contact: Contact '" . $r[0]["nick"] . "' is blocked or readonly.", LOGGER_DEBUG);
return -1;
}
if (!count($r)) {
// create contact record // create contact record
q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`, $fields['uid'] = $uid;
`name`, `nick`, `photo`, `network`, `rel`, `priority`, $fields['created'] = DateTimeFormat::utcNow();
`location`, `about`, `writable`, `blocked`, `readonly`, `pending`) $fields['nurl'] = normalise_link($url);
VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', %d, 0, 0, 0)", $fields['alias'] = 'twitter::' . $data->id_str;
intval($uid), $fields['poll'] = 'twitter::' . $data->id_str;
dbesc(DateTimeFormat::utcNow()), $fields['rel'] = CONTACT_IS_FRIEND;
dbesc("https://twitter.com/" . $contact->screen_name), $fields['priority'] = 1;
dbesc(normalise_link("https://twitter.com/" . $contact->screen_name)), $fields['writable'] = true;
dbesc($contact->screen_name."@twitter.com"), $fields['blocked'] = false;
dbesc("twitter::" . $contact->id_str), $fields['readonly'] = false;
dbesc(''), $fields['pending'] = false;
dbesc("twitter::" . $contact->id_str),
dbesc($contact->name),
dbesc($contact->screen_name),
dbesc($avatar),
dbesc(NETWORK_TWITTER),
intval(CONTACT_IS_FRIEND),
intval(1),
dbesc($contact->location),
dbesc($contact->description),
intval(1)
);
$r = q("SELECT * FROM `contact` WHERE `alias` = '%s' AND `uid` = %d LIMIT 1", if (!dba::insert('contact', $fields)) {
dbesc("twitter::".$contact->id_str),
intval($uid)
);
if (!count($r)) {
return false; return false;
} }
$contact_id = $r[0]['id']; $contact_id = dba::lastInsertId();
Group::addMember(User::getDefaultGroup($uid), $contact_id); Group::addMember(User::getDefaultGroup($uid), $contact_id);
$photos = Photo::importProfilePhoto($avatar, $uid, $contact_id, true); Contact::updateAvatar($avatar, $uid, $contact_id);
if ($photos) {
q("UPDATE `contact` SET `photo` = '%s',
`thumb` = '%s',
`micro` = '%s',
`name-date` = '%s',
`uri-date` = '%s',
`avatar-date` = '%s'
WHERE `id` = %d",
dbesc($photos[0]),
dbesc($photos[1]),
dbesc($photos[2]),
dbesc(DateTimeFormat::utcNow()),
dbesc(DateTimeFormat::utcNow()),
dbesc(DateTimeFormat::utcNow()),
intval($contact_id)
);
}
} else { } else {
// update profile photos once every two weeks as we have no notification of when they change. if ($contact["readonly"] || $contact["blocked"]) {
//$update_photo = (($r[0]['avatar-date'] < DateTimeFormat::convert('now -2 days', '', '', )) ? true : false); logger("twitter_fetch_contact: Contact '" . $contact["nick"] . "' is blocked or readonly.", LOGGER_DEBUG);
$update_photo = ($r[0]['avatar-date'] < DateTimeFormat::utc('now -12 hours')); return -1;
}
$contact_id = $contact['id'];
// update profile photos once every twelve hours as we have no notification of when they change.
$update_photo = ($contact['avatar-date'] < DateTimeFormat::utc('now -12 hours'));
// check that we have all the photos, this has been known to fail on occasion // check that we have all the photos, this has been known to fail on occasion
if ((!$r[0]['photo']) || (!$r[0]['thumb']) || (!$r[0]['micro']) || ($update_photo)) { if (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']) || $update_photo) {
logger("twitter_fetch_contact: Updating contact " . $contact->screen_name, LOGGER_DEBUG); logger("twitter_fetch_contact: Updating contact " . $data->screen_name, LOGGER_DEBUG);
$photos = Photo::importProfilePhoto($avatar, $uid, $r[0]['id'], true); Contact::updateAvatar($avatar, $uid, $contact['id']);
if ($photos) { $fields['name-date'] = DateTimeFormat::utcNow();
q("UPDATE `contact` SET `photo` = '%s', $fields['uri-date'] = DateTimeFormat::utcNow();
`thumb` = '%s',
`micro` = '%s', dba::update('contact', $fields, ['id' => $contact['id']]);
`name-date` = '%s',
`uri-date` = '%s',
`avatar-date` = '%s',
`url` = '%s',
`nurl` = '%s',
`addr` = '%s',
`name` = '%s',
`nick` = '%s',
`location` = '%s',
`about` = '%s'
WHERE `id` = %d",
dbesc($photos[0]),
dbesc($photos[1]),
dbesc($photos[2]),
dbesc(DateTimeFormat::utcNow()),
dbesc(DateTimeFormat::utcNow()),
dbesc(DateTimeFormat::utcNow()),
dbesc("https://twitter.com/".$contact->screen_name),
dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
dbesc($contact->screen_name."@twitter.com"),
dbesc($contact->name),
dbesc($contact->screen_name),
dbesc($contact->location),
dbesc($contact->description),
intval($r[0]['id'])
);
}
} }
} }
return $r[0]["id"]; return $contact_id;
} }
function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "") function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
@ -1113,7 +1063,7 @@ function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
$r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
intval($uid)); intval($uid));
if (count($r)) { if (DBM::is_result($r)) {
$self = $r[0]; $self = $r[0];
} else { } else {
return; return;
@ -1366,7 +1316,7 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis
intval($uid) intval($uid)
); );
if (count($r)) { if (DBM::is_result($r)) {
logger("Item with extid " . $postarray['uri'] . " found.", LOGGER_DEBUG); logger("Item with extid " . $postarray['uri'] . " found.", LOGGER_DEBUG);
return []; return [];
} }
@ -1380,7 +1330,7 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis
dbesc($parent), dbesc($parent),
intval($uid) intval($uid)
); );
if (count($r)) { if (DBM::is_result($r)) {
$postarray['thr-parent'] = $r[0]["uri"]; $postarray['thr-parent'] = $r[0]["uri"];
$postarray['parent-uri'] = $r[0]["parent-uri"]; $postarray['parent-uri'] = $r[0]["parent-uri"];
$postarray['parent'] = $r[0]["parent"]; $postarray['parent'] = $r[0]["parent"];
@ -1390,7 +1340,7 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis
dbesc($parent), dbesc($parent),
intval($uid) intval($uid)
); );
if (count($r)) { if (DBM::is_result($r)) {
$postarray['thr-parent'] = $r[0]['uri']; $postarray['thr-parent'] = $r[0]['uri'];
$postarray['parent-uri'] = $r[0]['parent-uri']; $postarray['parent-uri'] = $r[0]['parent-uri'];
$postarray['parent'] = $r[0]['parent']; $postarray['parent'] = $r[0]['parent'];
@ -1409,7 +1359,7 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis
$r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
intval($uid)); intval($uid));
if (count($r)) { if (DBM::is_result($r)) {
$contactid = $r[0]["id"]; $contactid = $r[0]["id"];
$postarray['owner-name'] = $r[0]["name"]; $postarray['owner-name'] = $r[0]["name"];
@ -1531,7 +1481,7 @@ function twitter_checknotification(App $a, $uid, $own_id, $top_item, $postarray)
intval($uid) intval($uid)
); );
if (!count($user)) { if (!DBM::is_result($user)) {
return; return;
} }
@ -1545,7 +1495,7 @@ function twitter_checknotification(App $a, $uid, $own_id, $top_item, $postarray)
dbesc("twitter::".$own_id) dbesc("twitter::".$own_id)
); );
if (!count($own_user)) { if (!DBM::is_result($own_user)) {
return; return;
} }
@ -1559,7 +1509,7 @@ function twitter_checknotification(App $a, $uid, $own_id, $top_item, $postarray)
intval($uid) intval($uid)
); );
if (count($myconv)) { if (DBM::is_result($myconv)) {
foreach ($myconv as $conv) { foreach ($myconv as $conv) {
// now if we find a match, it means we're in this conversation // now if we find a match, it means we're in this conversation
if (!link_compare($conv['author-link'], $user[0]["url"]) && !link_compare($conv['author-link'], $own_user[0]["url"])) { if (!link_compare($conv['author-link'], $user[0]["url"]) && !link_compare($conv['author-link'], $own_user[0]["url"])) {
@ -1614,7 +1564,7 @@ function twitter_fetchparentposts(App $a, $uid, $post, $connection, $self, $own_
intval($uid) intval($uid)
); );
if (count($r)) { if (DBM::is_result($r)) {
break; break;
} }
@ -1677,7 +1627,7 @@ function twitter_fetchhometimeline(App $a, $uid)
intval($own_contact), intval($own_contact),
intval($uid)); intval($uid));
if (count($r)) { if (DBM::is_result($r)) {
$own_id = $r[0]["nick"]; $own_id = $r[0]["nick"];
} else { } else {
logger("twitter_fetchhometimeline: Own twitter contact not found for user " . $uid, LOGGER_DEBUG); logger("twitter_fetchhometimeline: Own twitter contact not found for user " . $uid, LOGGER_DEBUG);
@ -1687,7 +1637,7 @@ function twitter_fetchhometimeline(App $a, $uid)
$r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
intval($uid)); intval($uid));
if (count($r)) { if (DBM::is_result($r)) {
$self = $r[0]; $self = $r[0];
} else { } else {
logger("twitter_fetchhometimeline: Own contact not found for user " . $uid, LOGGER_DEBUG); logger("twitter_fetchhometimeline: Own contact not found for user " . $uid, LOGGER_DEBUG);
@ -1696,7 +1646,7 @@ function twitter_fetchhometimeline(App $a, $uid)
$u = q("SELECT * FROM user WHERE uid = %d LIMIT 1", $u = q("SELECT * FROM user WHERE uid = %d LIMIT 1",
intval($uid)); intval($uid));
if (!count($u)) { if (!DBM::is_result($u)) {
logger("twitter_fetchhometimeline: Own user not found for user " . $uid, LOGGER_DEBUG); logger("twitter_fetchhometimeline: Own user not found for user " . $uid, LOGGER_DEBUG);
return; return;
} }
@ -1833,7 +1783,7 @@ function twitter_fetchhometimeline(App $a, $uid)
dbesc($postarray['uri']), dbesc($postarray['uri']),
intval($uid) intval($uid)
); );
if (count($r)) { if (DBM::is_result($r)) {
$item = $r[0]['id']; $item = $r[0]['id'];
$parent_id = $r[0]['parent']; $parent_id = $r[0]['parent'];
} }
@ -1890,7 +1840,7 @@ function twitter_fetch_own_contact(App $a, $uid)
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
intval($uid), intval($uid),
dbesc("twitter::" . $own_id)); dbesc("twitter::" . $own_id));
if (count($r)) { if (DBM::is_result($r)) {
$contact_id = $r[0]["id"]; $contact_id = $r[0]["id"];
} else { } else {
PConfig::delete($uid, 'twitter', 'own_id'); PConfig::delete($uid, 'twitter', 'own_id');