Automatically subscribe to the followings of a given AP account

This commit is contained in:
Michael 2021-07-13 06:06:08 +00:00
parent 299cff8c50
commit 1466afe3b2
6 changed files with 39 additions and 56 deletions

View file

@ -22,7 +22,9 @@
use Friendica\App; use Friendica\App;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\APContact;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Protocol\ActivityPub;
function ostatus_subscribe_content(App $a) function ostatus_subscribe_content(App $a)
{ {
@ -32,11 +34,11 @@ function ostatus_subscribe_content(App $a)
// NOTREACHED // NOTREACHED
} }
$o = '<h2>' . DI::l10n()->t('Subscribing to OStatus contacts') . '</h2>'; $o = '<h2>' . DI::l10n()->t('Subscribing to contacts') . '</h2>';
$uid = local_user(); $uid = local_user();
$counter = intval($_REQUEST['counter']); $counter = intval($_REQUEST['counter'] ?? 0);
if (DI::pConfig()->get($uid, 'ostatus', 'legacy_friends') == '') { if (DI::pConfig()->get($uid, 'ostatus', 'legacy_friends') == '') {
@ -51,17 +53,38 @@ function ostatus_subscribe_content(App $a)
return $o . DI::l10n()->t('Couldn\'t fetch information for contact.'); return $o . DI::l10n()->t('Couldn\'t fetch information for contact.');
} }
$api = $contact['baseurl'] . '/api/'; if ($contact['network'] == Protocol::OSTATUS) {
$api = $contact['baseurl'] . '/api/';
// Fetching friends // Fetching friends
$curlResult = DI::httpRequest()->get($api . 'statuses/friends.json?screen_name=' . $contact['nick']); $curlResult = DI::httpRequest()->get($api . 'statuses/friends.json?screen_name=' . $contact['nick']);
if (!$curlResult->isSuccess()) { if (!$curlResult->isSuccess()) {
DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
return $o . DI::l10n()->t('Couldn\'t fetch friends for contact.');
}
$friends = $curlResult->getBody();
if (empty($friends)) {
DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
return $o . DI::l10n()->t('Couldn\'t fetch following contacts.');
}
DI::pConfig()->set($uid, 'ostatus', 'legacy_friends', $friends);
} elseif ($apcontact = APContact::getByURL($contact['url'])) {
if (empty($apcontact['following'])) {
DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
return $o . DI::l10n()->t('Couldn\'t fetch remote profile.');
}
$followings = ActivityPub::fetchItems($apcontact['following']);
if (empty($followings)) {
DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
return $o . DI::l10n()->t('Couldn\'t fetch following contacts.');
}
DI::pConfig()->set($uid, 'ostatus', 'legacy_friends', json_encode($followings));
} else {
DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact'); DI::pConfig()->delete($uid, 'ostatus', 'legacy_contact');
return $o . DI::l10n()->t('Couldn\'t fetch friends for contact.'); return $o . DI::l10n()->t('Unsupported network');
} }
DI::pConfig()->set($uid, 'ostatus', 'legacy_friends', $curlResult->getBody());
} }
$friends = json_decode(DI::pConfig()->get($uid, 'ostatus', 'legacy_friends')); $friends = json_decode(DI::pConfig()->get($uid, 'ostatus', 'legacy_friends'));
@ -82,13 +105,13 @@ function ostatus_subscribe_content(App $a)
$friend = $friends[$counter++]; $friend = $friends[$counter++];
$url = $friend->statusnet_profile_url; $url = $friend->statusnet_profile_url ?? $friend;
$o .= '<p>' . $counter . '/' . $total . ': ' . $url; $o .= '<p>' . $counter . '/' . $total . ': ' . $url;
$probed = Contact::getByURL($url); $probed = Contact::getByURL($url);
if ($probed['network'] == Protocol::OSTATUS) { if (in_array($probed['network'], Protocol::FEDERATED)) {
$result = Contact::createFromProbe($a->user, $probed['url'], true, Protocol::OSTATUS); $result = Contact::createFromProbe($a->user, $probed['url']);
if ($result['success']) { if ($result['success']) {
$o .= ' - ' . DI::l10n()->t('success'); $o .= ' - ' . DI::l10n()->t('success');
} else { } else {

View file

@ -169,24 +169,6 @@ function salmon_post(App $a, $xml = '') {
intval($importer['uid']) intval($importer['uid'])
); );
if (!DBA::isResult($r)) {
Logger::log('Author ' . $author_link . ' unknown to user ' . $importer['uid'] . '.');
if (DI::pConfig()->get($importer['uid'], 'system', 'ostatus_autofriend')) {
$result = Contact::createFromProbe($importer, $author_link);
if ($result['success']) {
$r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s')
AND `uid` = %d LIMIT 1",
DBA::escape(Protocol::OSTATUS),
DBA::escape($author_link),
DBA::escape($author_link),
intval($importer['uid'])
);
}
}
}
if (!empty($r[0]['gsid'])) { if (!empty($r[0]['gsid'])) {
GServer::setProtocol($r[0]['gsid'], Post\DeliveryData::OSTATUS); GServer::setProtocol($r[0]['gsid'], Post\DeliveryData::OSTATUS);
} }
@ -194,7 +176,6 @@ function salmon_post(App $a, $xml = '') {
// Have we ignored the person? // Have we ignored the person?
// If so we can not accept this post. // If so we can not accept this post.
//if((DBA::isResult($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == Contact::FOLLOWER) || ($r[0]['blocked']))) {
if (DBA::isResult($r) && $r[0]['blocked']) { if (DBA::isResult($r) && $r[0]['blocked']) {
Logger::log('Ignoring this author.'); Logger::log('Ignoring this author.');
throw new \Friendica\Network\HTTPException\AcceptedException(); throw new \Friendica\Network\HTTPException\AcceptedException();

View file

@ -139,8 +139,6 @@ function settings_post(App $a)
DI::pConfig()->set(local_user(), 'system', 'no_intelligent_shortening', intval($_POST['no_intelligent_shortening'])); DI::pConfig()->set(local_user(), 'system', 'no_intelligent_shortening', intval($_POST['no_intelligent_shortening']));
DI::pConfig()->set(local_user(), 'system', 'simple_shortening', intval($_POST['simple_shortening'])); DI::pConfig()->set(local_user(), 'system', 'simple_shortening', intval($_POST['simple_shortening']));
DI::pConfig()->set(local_user(), 'system', 'attach_link_title', intval($_POST['attach_link_title'])); DI::pConfig()->set(local_user(), 'system', 'attach_link_title', intval($_POST['attach_link_title']));
DI::pConfig()->set(local_user(), 'system', 'ostatus_autofriend', intval($_POST['snautofollow']));
DI::pConfig()->set(local_user(), 'ostatus', 'default_group', $_POST['group-selection']);
DI::pConfig()->set(local_user(), 'ostatus', 'legacy_contact', $_POST['legacy_contact']); DI::pConfig()->set(local_user(), 'ostatus', 'legacy_contact', $_POST['legacy_contact']);
} elseif (!empty($_POST['imap-submit'])) { } elseif (!empty($_POST['imap-submit'])) {
$mail_server = $_POST['mail_server'] ?? ''; $mail_server = $_POST['mail_server'] ?? '';
@ -549,15 +547,13 @@ function settings_content(App $a)
$no_intelligent_shortening = intval(DI::pConfig()->get(local_user(), 'system', 'no_intelligent_shortening')); $no_intelligent_shortening = intval(DI::pConfig()->get(local_user(), 'system', 'no_intelligent_shortening'));
$simple_shortening = intval(DI::pConfig()->get(local_user(), 'system', 'simple_shortening')); $simple_shortening = intval(DI::pConfig()->get(local_user(), 'system', 'simple_shortening'));
$attach_link_title = intval(DI::pConfig()->get(local_user(), 'system', 'attach_link_title')); $attach_link_title = intval(DI::pConfig()->get(local_user(), 'system', 'attach_link_title'));
$ostatus_autofriend = intval(DI::pConfig()->get(local_user(), 'system', 'ostatus_autofriend'));
$default_group = DI::pConfig()->get(local_user(), 'ostatus', 'default_group');
$legacy_contact = DI::pConfig()->get(local_user(), 'ostatus', 'legacy_contact'); $legacy_contact = DI::pConfig()->get(local_user(), 'ostatus', 'legacy_contact');
if (!empty($legacy_contact)) { if (!empty($legacy_contact)) {
/// @todo Isn't it supposed to be a $a->internalRedirect() call? /// @todo Isn't it supposed to be a $a->internalRedirect() call?
DI::page()['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . DI::baseUrl().'/ostatus_subscribe?url=' . urlencode($legacy_contact) . '">'; DI::page()['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . DI::baseUrl().'/ostatus_subscribe?url=' . urlencode($legacy_contact) . '">';
} }
$settings_connectors = ''; $settings_connectors = '';
Hook::callAll('connector_settings', $settings_connectors); Hook::callAll('connector_settings', $settings_connectors);
@ -616,9 +612,7 @@ function settings_content(App $a)
'$no_intelligent_shortening' => ['no_intelligent_shortening', DI::l10n()->t('Disable intelligent shortening'), $no_intelligent_shortening, DI::l10n()->t('Normally the system tries to find the best link to add to shortened posts. If this option is enabled then every shortened post will always point to the original friendica post.')], '$no_intelligent_shortening' => ['no_intelligent_shortening', DI::l10n()->t('Disable intelligent shortening'), $no_intelligent_shortening, DI::l10n()->t('Normally the system tries to find the best link to add to shortened posts. If this option is enabled then every shortened post will always point to the original friendica post.')],
'$simple_shortening' => ['simple_shortening', DI::l10n()->t('Enable simple text shortening'), $simple_shortening, DI::l10n()->t('Normally the system shortens posts at the next line feed. If this option is enabled then the system will shorten the text at the maximum character limit.')], '$simple_shortening' => ['simple_shortening', DI::l10n()->t('Enable simple text shortening'), $simple_shortening, DI::l10n()->t('Normally the system shortens posts at the next line feed. If this option is enabled then the system will shorten the text at the maximum character limit.')],
'$attach_link_title' => ['attach_link_title', DI::l10n()->t('Attach the link title'), $attach_link_title, DI::l10n()->t('When activated, the title of the attached link will be added as a title on posts to Diaspora. This is mostly helpful with "remote-self" contacts that share feed content.')], '$attach_link_title' => ['attach_link_title', DI::l10n()->t('Attach the link title'), $attach_link_title, DI::l10n()->t('When activated, the title of the attached link will be added as a title on posts to Diaspora. This is mostly helpful with "remote-self" contacts that share feed content.')],
'$ostatus_autofriend' => ['snautofollow', DI::l10n()->t("Automatically follow any GNU Social \x28OStatus\x29 followers/mentioners"), $ostatus_autofriend, DI::l10n()->t('If you receive a message from an unknown OStatus user, this option decides what to do. If it is checked, a new contact will be created for every unknown user.')], '$legacy_contact' => ['legacy_contact', DI::l10n()->t('Your legacy ActivityPub/GNU Social account'), $legacy_contact, DI::l10n()->t("If you enter your old account name from an ActivityPub based system or your GNU Social/Statusnet account name here (in the format user@domain.tld), your contacts will be added automatically. The field will be emptied when done.")],
'$default_group' => Group::displayGroupSelection(local_user(), $default_group, DI::l10n()->t("Default group for OStatus contacts")),
'$legacy_contact' => ['legacy_contact', DI::l10n()->t('Your legacy GNU Social account'), $legacy_contact, DI::l10n()->t("If you enter your old GNU Social/Statusnet account name here \x28in the format user@domain.tld\x29, your contacts will be added automatically. The field will be emptied when done.")],
'$repair_ostatus_url' => DI::baseUrl() . '/repair_ostatus', '$repair_ostatus_url' => DI::baseUrl() . '/repair_ostatus',
'$repair_ostatus_text' => DI::l10n()->t('Repair OStatus subscriptions'), '$repair_ostatus_text' => DI::l10n()->t('Repair OStatus subscriptions'),

View file

@ -476,20 +476,11 @@ class User
*/ */
public static function getDefaultGroup($uid, $network = '') public static function getDefaultGroup($uid, $network = '')
{ {
$default_group = 0;
if ($network == Protocol::OSTATUS) {
$default_group = DI::pConfig()->get($uid, "ostatus", "default_group");
}
if ($default_group != 0) {
return $default_group;
}
$user = DBA::selectFirst('user', ['def_gid'], ['uid' => $uid]); $user = DBA::selectFirst('user', ['def_gid'], ['uid' => $uid]);
if (DBA::isResult($user)) { if (DBA::isResult($user)) {
$default_group = $user["def_gid"]; $default_group = $user["def_gid"];
} else {
$default_group = 0;
} }
return $default_group; return $default_group;

View file

@ -16,8 +16,6 @@
{{include file="field_checkbox.tpl" field=$no_intelligent_shortening}} {{include file="field_checkbox.tpl" field=$no_intelligent_shortening}}
{{include file="field_checkbox.tpl" field=$simple_shortening}} {{include file="field_checkbox.tpl" field=$simple_shortening}}
{{include file="field_checkbox.tpl" field=$attach_link_title}} {{include file="field_checkbox.tpl" field=$attach_link_title}}
{{include file="field_checkbox.tpl" field=$ostatus_autofriend}}
{{$default_group nofilter}}
{{include file="field_input.tpl" field=$legacy_contact}} {{include file="field_input.tpl" field=$legacy_contact}}
<p><a href="{{$repair_ostatus_url}}">{{$repair_ostatus_text}}</a></p> <p><a href="{{$repair_ostatus_url}}">{{$repair_ostatus_text}}</a></p>

View file

@ -28,10 +28,6 @@
{{include file="field_checkbox.tpl" field=$attach_link_title}} {{include file="field_checkbox.tpl" field=$attach_link_title}}
{{include file="field_checkbox.tpl" field=$ostatus_autofriend}}
{{$default_group nofilter}}
{{include file="field_input.tpl" field=$legacy_contact}} {{include file="field_input.tpl" field=$legacy_contact}}
<p><a href="{{$repair_ostatus_url}}">{{$repair_ostatus_text}}</a></p> <p><a href="{{$repair_ostatus_url}}">{{$repair_ostatus_text}}</a></p>