Twitter: automatically follow fediverse accounts in profile

pull/1310/head
Michael 2022-11-13 10:16:58 +00:00
parent 18e7d5bfea
commit f71b2bda38
3 changed files with 119 additions and 26 deletions

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-10-02 23:56+0000\n"
"POT-Creation-Date: 2022-11-13 10:15+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -21,19 +21,19 @@ msgstr ""
msgid "Post to Twitter"
msgstr ""
#: twitter.php:262
#: twitter.php:263
msgid ""
"You submitted an empty PIN, please Sign In with Twitter again to get a new "
"one."
msgstr ""
#: twitter.php:327
#: twitter.php:330
msgid ""
"No consumer key pair for Twitter found. Please contact your site "
"administrator."
msgstr ""
#: twitter.php:340
#: twitter.php:343
msgid ""
"At this Friendica instance the Twitter addon was enabled but you have not "
"yet connected your account to your Twitter account. To do so click the "
@ -42,26 +42,26 @@ msgid ""
"be posted to Twitter."
msgstr ""
#: twitter.php:341
#: twitter.php:344
msgid "Log in with Twitter"
msgstr ""
#: twitter.php:343
#: twitter.php:346
msgid "Copy the PIN from Twitter here"
msgstr ""
#: twitter.php:351 twitter.php:395
#: twitter.php:354 twitter.php:399
msgid "An error occured: "
msgstr ""
#: twitter.php:365
#: twitter.php:368
#, php-format
msgid ""
"Currently connected to: <a href=\"https://twitter.com/%1$s\" target="
"\"_twitter\">%1$s</a>"
msgstr ""
#: twitter.php:371
#: twitter.php:374
msgid ""
"<strong>Note</strong>: Due to your privacy settings (<em>Hide your profile "
"details from unknown viewers?</em>) the link potentially included in public "
@ -69,46 +69,46 @@ msgid ""
"the visitor that the access to your profile has been restricted."
msgstr ""
#: twitter.php:378
#: twitter.php:381
msgid "Invalid Twitter info"
msgstr ""
#: twitter.php:379
#: twitter.php:382
msgid "Disconnect"
msgstr ""
#: twitter.php:384
#: twitter.php:387
msgid "Allow posting to Twitter"
msgstr ""
#: twitter.php:384
#: twitter.php:387
msgid ""
"If enabled all your <strong>public</strong> postings can be posted to the "
"associated Twitter account. You can choose to do so by default (here) or for "
"every posting separately in the posting options when writing the entry."
msgstr ""
#: twitter.php:385
#: twitter.php:388
msgid "Send public postings to Twitter by default"
msgstr ""
#: twitter.php:386
#: twitter.php:389
msgid "Use threads instead of truncating the content"
msgstr ""
#: twitter.php:387
#: twitter.php:390
msgid "Mirror all posts from twitter that are no replies"
msgstr ""
#: twitter.php:388
#: twitter.php:391
msgid "Import the remote timeline"
msgstr ""
#: twitter.php:389
#: twitter.php:392
msgid "Automatically create contacts"
msgstr ""
#: twitter.php:389
#: twitter.php:392
msgid ""
"This will automatically create a contact in Friendica as soon as you receive "
"a message from an existing contact via the Twitter network. If you do not "
@ -116,33 +116,44 @@ msgid ""
"from whom you would like to see posts here."
msgstr ""
#: twitter.php:402
#: twitter.php:393
msgid "Follow in fediverse"
msgstr ""
#: twitter.php:393
msgid ""
"Automatically subscribe to the contact in the fediverse, when a fediverse "
"account is mentioned in name or description and we are following the Twitter "
"contact."
msgstr ""
#: twitter.php:406
msgid "Twitter Import/Export/Mirror"
msgstr ""
#: twitter.php:554
#: twitter.php:558
msgid ""
"Please connect a Twitter account in your Social Network settings to import "
"Twitter posts."
msgstr ""
#: twitter.php:561
#: twitter.php:565
msgid "Twitter post not found."
msgstr ""
#: twitter.php:961
#: twitter.php:965
msgid "Save Settings"
msgstr ""
#: twitter.php:963
#: twitter.php:967
msgid "Consumer key"
msgstr ""
#: twitter.php:964
#: twitter.php:968
msgid "Consumer secret"
msgstr ""
#: twitter.php:1163
#: twitter.php:1167
#, php-format
msgid "%s on Twitter"
msgstr ""

View File

@ -22,3 +22,4 @@
{{include file="field_checkbox.tpl" field=$mirror}}
{{include file="field_checkbox.tpl" field=$import}}
{{include file="field_checkbox.tpl" field=$create_user}}
{{include file="field_checkbox.tpl" field=$auto_follow}}

View File

@ -247,6 +247,7 @@ function twitter_settings_post(App $a)
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'mirror_posts');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'import');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'create_user');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'auto_follow');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'own_id');
} else {
if (isset($_POST['twitter-pin'])) {
@ -282,6 +283,7 @@ function twitter_settings_post(App $a)
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'import', intval($_POST['twitter-import']));
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'auto_follow', intval($_POST['twitter-auto_follow']));
if (!intval($_POST['twitter-mirror'])) {
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'lastid');
@ -316,6 +318,7 @@ function twitter_settings(App $a, array &$data)
$mirrorenabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'mirror_posts'));
$importenabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'import'));
$create_userenabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'create_user'));
$auto_followenabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'auto_follow'));
// Hide the submit button by default
$submit = '';
@ -387,6 +390,7 @@ function twitter_settings(App $a, array &$data)
'$mirror' => ['twitter-mirror', DI::l10n()->t('Mirror all posts from twitter that are no replies'), $mirrorenabled],
'$import' => ['twitter-import', DI::l10n()->t('Import the remote timeline'), $importenabled],
'$create_user' => ['twitter-create_user', DI::l10n()->t('Automatically create contacts'), $create_userenabled, DI::l10n()->t('This will automatically create a contact in Friendica as soon as you receive a message from an existing contact via the Twitter network. If you do not enable this, you need to manually add those Twitter contacts in Friendica from whom you would like to see posts here.')],
'$auto_follow' => ['twitter-auto_follow', DI::l10n()->t('Follow in fediverse'), $auto_followenabled, DI::l10n()->t('Automatically subscribe to the contact in the fediverse, when a fediverse account is mentioned in name or description and we are following the Twitter contact.')],
]);
// Enable the default submit button
@ -1533,9 +1537,86 @@ function twitter_fetch_contact($uid, $data, $create_user)
Contact::updateAvatar($contact_id, $avatar);
if (Contact::isSharing($contact_id, $uid, true) && DI::pConfig()->get($uid, 'twitter', 'auto_follow')) {
twitter_auto_follow($uid, $data);
}
return $contact_id;
}
/**
* Follow a fediverse account that is proived in the name or the profile
*
* @param integer $uid
* @param object $data
*/
function twitter_auto_follow(int $uid, object $data)
{
$addrpattern = '([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6})';
// Search for user@domain.tld in the name
if (preg_match('#' . $addrpattern . '#', $data->name, $match)) {
if (twitter_add_contact($match[1], true, $uid)) {
return;
}
}
// Search for @user@domain.tld in the description
if (preg_match('#@' . $addrpattern . '#', $data->description, $match)) {
if (twitter_add_contact($match[1], true, $uid)) {
return;
}
}
// Search for user@domain.tld in the description
// We don't probe here, since this could be a mail address
if (preg_match('#' . $addrpattern . '#', $data->description, $match)) {
if (twitter_add_contact($match[1], false, $uid)) {
return;
}
}
// Search for profile links in the description
foreach ($data->entities->description->urls as $url) {
if (!empty($url->expanded_url)) {
// We only probe on Mastodon style URL to reduce the number of unsuccessful probes
twitter_add_contact($url->expanded_url, strpos($url->expanded_url, '@'), $uid);
}
}
}
/**
* Check if the provided address is a fediverse account and adds it
*
* @param string $addr
* @param boolean $probe
* @param integer $uid
* @return boolean
*/
function twitter_add_contact(string $addr, bool $probe, int $uid): bool
{
$contact = Contact::getByURL($addr, $probe ? null : false, ['id', 'url', 'network']);
if (empty($contact)) {
Logger::debug('Not a contact address', ['uid' => $uid, 'probe' => $probe, 'addr' => $addr]);
return false;
}
if (!in_array($contact['network'], Protocol::FEDERATED)) {
Logger::debug('Not a federated network', ['uid' => $uid, 'addr' => $addr, 'contact' => $contact]);
return false;
}
if (Contact::isSharing($contact['id'], $uid)) {
Logger::debug('Contact has already been added', ['uid' => $uid, 'addr' => $addr, 'contact' => $contact]);
return true;
}
Logger::info('Add contact', ['uid' => $uid, 'addr' => $addr, 'contact' => $contact]);
Worker::add(Worker::PRIORITY_LOW, 'AddContact', $uid, $contact['url']);
return true;
}
/**
* @param string $screen_name
* @return stdClass|null