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

View File

@ -22,3 +22,4 @@
{{include file="field_checkbox.tpl" field=$mirror}} {{include file="field_checkbox.tpl" field=$mirror}}
{{include file="field_checkbox.tpl" field=$import}} {{include file="field_checkbox.tpl" field=$import}}
{{include file="field_checkbox.tpl" field=$create_user}} {{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', 'mirror_posts');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'import'); DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'import');
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'create_user'); 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'); DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'own_id');
} else { } else {
if (isset($_POST['twitter-pin'])) { 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', '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', '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', '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'])) { if (!intval($_POST['twitter-mirror'])) {
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'lastid'); 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')); $mirrorenabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'mirror_posts'));
$importenabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'import')); $importenabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'import'));
$create_userenabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'create_user')); $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 // Hide the submit button by default
$submit = ''; $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], '$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], '$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.')], '$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 // Enable the default submit button
@ -1533,9 +1537,86 @@ function twitter_fetch_contact($uid, $data, $create_user)
Contact::updateAvatar($contact_id, $avatar); 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; 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 * @param string $screen_name
* @return stdClass|null * @return stdClass|null