Twitter: Assign contacts via their twitter id - not via their url #1234

Merged
annando merged 2 commits from twitter-avatar into 2021.12-rc 2022-02-03 00:07:37 +01:00

View file

@ -1346,6 +1346,8 @@ function twitter_user_to_contact($data)
$fields = [ $fields = [
'url' => $url, 'url' => $url,
'nurl' => Strings::normaliseLink($url),
'uri-id' => ItemURI::getIdByURI($url),
'network' => Protocol::TWITTER, 'network' => Protocol::TWITTER,
'alias' => 'twitter::' . $data->id_str, 'alias' => 'twitter::' . $data->id_str,
'baseurl' => $baseurl, 'baseurl' => $baseurl,
@ -1361,6 +1363,16 @@ function twitter_user_to_contact($data)
return $fields; return $fields;
} }
function twitter_get_contact($data, int $uid = 0)
{
$contact = DBA::selectFirst('contact', ['id'], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
if (DBA::isResult($contact)) {
return $contact['id'];
} else {
return twitter_fetch_contact($uid, $data, false);
}
}
function twitter_fetch_contact($uid, $data, $create_user) function twitter_fetch_contact($uid, $data, $create_user)
{ {
$fields = twitter_user_to_contact($data); $fields = twitter_user_to_contact($data);
@ -1402,7 +1414,6 @@ function twitter_fetch_contact($uid, $data, $create_user)
// create contact record // create contact record
$fields['uid'] = $uid; $fields['uid'] = $uid;
$fields['created'] = DateTimeFormat::utcNow(); $fields['created'] = DateTimeFormat::utcNow();
$fields['nurl'] = Strings::normaliseLink($fields['url']);
$fields['poll'] = 'twitter::' . $data->id_str; $fields['poll'] = 'twitter::' . $data->id_str;
$fields['rel'] = $relation; $fields['rel'] = $relation;
$fields['priority'] = 1; $fields['priority'] = 1;
@ -1418,8 +1429,6 @@ function twitter_fetch_contact($uid, $data, $create_user)
$contact_id = DBA::lastInsertId(); $contact_id = DBA::lastInsertId();
Group::addMember(User::getDefaultGroup($uid), $contact_id); Group::addMember(User::getDefaultGroup($uid), $contact_id);
Contact::updateAvatar($contact_id, $avatar);
} else { } else {
if ($contact["readonly"] || $contact["blocked"]) { if ($contact["readonly"] || $contact["blocked"]) {
Logger::notice('Contact is blocked or readonly.', ['nickname' => $contact["nick"]]); Logger::notice('Contact is blocked or readonly.', ['nickname' => $contact["nick"]]);
@ -1435,8 +1444,6 @@ function twitter_fetch_contact($uid, $data, $create_user)
$update = true; $update = true;
} }
Contact::updateAvatar($contact['id'], $avatar);
if ($contact['name'] != $data->name) { if ($contact['name'] != $data->name) {
$fields['name-date'] = $fields['uri-date'] = DateTimeFormat::utcNow(); $fields['name-date'] = $fields['uri-date'] = DateTimeFormat::utcNow();
$update = true; $update = true;
@ -1458,6 +1465,8 @@ function twitter_fetch_contact($uid, $data, $create_user)
} }
} }
Contact::updateAvatar($contact_id, $avatar);
return $contact_id; return $contact_id;
} }
@ -1787,8 +1796,9 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
if ($contactid == 0) { if ($contactid == 0) {
$contactid = twitter_fetch_contact($uid, $post->user, $create_user); $contactid = twitter_fetch_contact($uid, $post->user, $create_user);
$postarray['owner-name'] = $post->user->name; $postarray['owner-id'] = twitter_get_contact($post->user);
$postarray['owner-link'] = "https://twitter.com/" . $post->user->screen_name; $postarray['owner-name'] = $post->user->name;
$postarray['owner-link'] = "https://twitter.com/" . $post->user->screen_name;
$postarray['owner-avatar'] = twitter_fix_avatar($post->user->profile_image_url_https); $postarray['owner-avatar'] = twitter_fix_avatar($post->user->profile_image_url_https);
} }
@ -1799,20 +1809,20 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
return []; return [];
} }
$postarray['contact-id'] = $contactid; $postarray['contact-id'] = $contactid;
$postarray['verb'] = Activity::POST;
$postarray['verb'] = Activity::POST; $postarray['author-id'] = $postarray['owner-id'];
$postarray['author-name'] = $postarray['owner-name']; $postarray['author-name'] = $postarray['owner-name'];
$postarray['author-link'] = $postarray['owner-link']; $postarray['author-link'] = $postarray['owner-link'];
$postarray['author-avatar'] = $postarray['owner-avatar']; $postarray['author-avatar'] = $postarray['owner-avatar'];
$postarray['plink'] = "https://twitter.com/" . $post->user->screen_name . "/status/" . $post->id_str; $postarray['plink'] = "https://twitter.com/" . $post->user->screen_name . "/status/" . $post->id_str;
$postarray['app'] = strip_tags($post->source); $postarray['app'] = strip_tags($post->source);
if ($post->user->protected) { if ($post->user->protected) {
$postarray['private'] = Item::PRIVATE; $postarray['private'] = Item::PRIVATE;
$postarray['allow_cid'] = '<' . $self['id'] . '>'; $postarray['allow_cid'] = '<' . $self['id'] . '>';
} else { } else {
$postarray['private'] = Item::UNLISTED; $postarray['private'] = Item::UNLISTED;
$postarray['allow_cid'] = ''; $postarray['allow_cid'] = '';
} }
@ -1876,13 +1886,14 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
$postarray['thr-parent'] = $retweet['uri']; $postarray['thr-parent'] = $retweet['uri'];
} else { } else {
$retweet['source'] = $postarray['source']; $retweet['source'] = $postarray['source'];
$retweet['direction'] = $postarray['direction']; $retweet['direction'] = $postarray['direction'];
$retweet['private'] = $postarray['private']; $retweet['private'] = $postarray['private'];
$retweet['allow_cid'] = $postarray['allow_cid']; $retweet['allow_cid'] = $postarray['allow_cid'];
$retweet['contact-id'] = $postarray['contact-id']; $retweet['contact-id'] = $postarray['contact-id'];
$retweet['owner-name'] = $postarray['owner-name']; $retweet['owner-id'] = $postarray['owner-id'];
$retweet['owner-link'] = $postarray['owner-link']; $retweet['owner-name'] = $postarray['owner-name'];
$retweet['owner-link'] = $postarray['owner-link'];
$retweet['owner-avatar'] = $postarray['owner-avatar']; $retweet['owner-avatar'] = $postarray['owner-avatar'];
$postarray = $retweet; $postarray = $retweet;