Skip to content

Commit

Permalink
Merge pull request #1234 from annando/twitter-avatar
Browse files Browse the repository at this point in the history
Twitter: Assign contacts via their twitter id - not via their url
  • Loading branch information
MrPetovan authored Feb 2, 2022
2 parents f9ff50b + fd2fb29 commit dd6bf79
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions twitter/twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,8 @@ function twitter_user_to_contact($data)

$fields = [
'url' => $url,
'nurl' => Strings::normaliseLink($url),
'uri-id' => ItemURI::getIdByURI($url),
'network' => Protocol::TWITTER,
'alias' => 'twitter::' . $data->id_str,
'baseurl' => $baseurl,
Expand All @@ -1361,6 +1363,16 @@ function twitter_user_to_contact($data)
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)
{
$fields = twitter_user_to_contact($data);
Expand Down Expand Up @@ -1402,7 +1414,6 @@ function twitter_fetch_contact($uid, $data, $create_user)
// create contact record
$fields['uid'] = $uid;
$fields['created'] = DateTimeFormat::utcNow();
$fields['nurl'] = Strings::normaliseLink($fields['url']);
$fields['poll'] = 'twitter::' . $data->id_str;
$fields['rel'] = $relation;
$fields['priority'] = 1;
Expand All @@ -1418,8 +1429,6 @@ function twitter_fetch_contact($uid, $data, $create_user)
$contact_id = DBA::lastInsertId();

Group::addMember(User::getDefaultGroup($uid), $contact_id);

Contact::updateAvatar($contact_id, $avatar);
} else {
if ($contact["readonly"] || $contact["blocked"]) {
Logger::notice('Contact is blocked or readonly.', ['nickname' => $contact["nick"]]);
Expand All @@ -1435,8 +1444,6 @@ function twitter_fetch_contact($uid, $data, $create_user)
$update = true;
}

Contact::updateAvatar($contact['id'], $avatar);

if ($contact['name'] != $data->name) {
$fields['name-date'] = $fields['uri-date'] = DateTimeFormat::utcNow();
$update = true;
Expand All @@ -1458,6 +1465,8 @@ function twitter_fetch_contact($uid, $data, $create_user)
}
}

Contact::updateAvatar($contact_id, $avatar);

return $contact_id;
}

Expand Down Expand Up @@ -1787,8 +1796,9 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
if ($contactid == 0) {
$contactid = twitter_fetch_contact($uid, $post->user, $create_user);

$postarray['owner-name'] = $post->user->name;
$postarray['owner-link'] = "https://twitter.com/" . $post->user->screen_name;
$postarray['owner-id'] = twitter_get_contact($post->user);
$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);
}

Expand All @@ -1799,20 +1809,20 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
return [];
}

$postarray['contact-id'] = $contactid;

$postarray['verb'] = Activity::POST;
$postarray['author-name'] = $postarray['owner-name'];
$postarray['author-link'] = $postarray['owner-link'];
$postarray['contact-id'] = $contactid;
$postarray['verb'] = Activity::POST;
$postarray['author-id'] = $postarray['owner-id'];
$postarray['author-name'] = $postarray['owner-name'];
$postarray['author-link'] = $postarray['owner-link'];
$postarray['author-avatar'] = $postarray['owner-avatar'];
$postarray['plink'] = "https://twitter.com/" . $post->user->screen_name . "/status/" . $post->id_str;
$postarray['app'] = strip_tags($post->source);
$postarray['plink'] = "https://twitter.com/" . $post->user->screen_name . "/status/" . $post->id_str;
$postarray['app'] = strip_tags($post->source);

if ($post->user->protected) {
$postarray['private'] = Item::PRIVATE;
$postarray['private'] = Item::PRIVATE;
$postarray['allow_cid'] = '<' . $self['id'] . '>';
} else {
$postarray['private'] = Item::UNLISTED;
$postarray['private'] = Item::UNLISTED;
$postarray['allow_cid'] = '';
}

Expand Down Expand Up @@ -1876,13 +1886,14 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl

$postarray['thr-parent'] = $retweet['uri'];
} else {
$retweet['source'] = $postarray['source'];
$retweet['direction'] = $postarray['direction'];
$retweet['private'] = $postarray['private'];
$retweet['allow_cid'] = $postarray['allow_cid'];
$retweet['contact-id'] = $postarray['contact-id'];
$retweet['owner-name'] = $postarray['owner-name'];
$retweet['owner-link'] = $postarray['owner-link'];
$retweet['source'] = $postarray['source'];
$retweet['direction'] = $postarray['direction'];
$retweet['private'] = $postarray['private'];
$retweet['allow_cid'] = $postarray['allow_cid'];
$retweet['contact-id'] = $postarray['contact-id'];
$retweet['owner-id'] = $postarray['owner-id'];
$retweet['owner-name'] = $postarray['owner-name'];
$retweet['owner-link'] = $postarray['owner-link'];
$retweet['owner-avatar'] = $postarray['owner-avatar'];

$postarray = $retweet;
Expand Down

0 comments on commit dd6bf79

Please sign in to comment.