]> git.mxchange.org Git - friendica-addons.git/blobdiff - twitter/twitter.php
[various] Set config loaded from default values to SOURCE_STATIC
[friendica-addons.git] / twitter / twitter.php
index 5e3a4b9c2c17308dc62c55fe6bef172766a62089..39e5b44795b326041b882f209c4ce16660026699 100644 (file)
  *     we do not need "Twitter as login". When you've registered the app you get the
  *     OAuth Consumer key and secret pair for your application/site.
  *
- *     Add this key pair to your global config/addon.config.php or use the admin panel.
+ *     Add this key pair to your config/twitter.config.php file or use the admin panel.
  *
- *             'twitter' => [
- *                 'consumerkey' => '',
- *             'consumersecret' => '',
- *      ],
+ *             return [
+ *          'twitter' => [
+ *                     'consumerkey' => '',
+ *                 'consumersecret' => '',
+ *          ],
+ *      ];
  *
  *     To activate the addon itself add it to the system.addon
  *     setting. After this, your user can configure their Twitter account settings
@@ -123,7 +125,7 @@ function twitter_install()
 
 function twitter_load_config(App $a, ConfigFileLoader $loader)
 {
-       $a->getConfigCache()->load($loader->loadAddonConfig('twitter'));
+       $a->getConfigCache()->load($loader->loadAddonConfig('twitter'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
 }
 
 function twitter_check_item_notification(App $a, array &$notification_data)
@@ -199,22 +201,22 @@ function twitter_api_contact(string $apiPath, array $contact, int $uid): ?bool
                return null;
        }
 
-       return twitter_api_call($uid, $apiPath, ['screen_name' => $contact['nick']]);
+       return (bool)twitter_api_call($uid, $apiPath, ['screen_name' => $contact['nick']]);
 }
 
 function twitter_jot_nets(App $a, array &$jotnets_fields)
 {
-       if (!local_user()) {
+       if (!DI::userSession()->getLocalUserId()) {
                return;
        }
 
-       if (DI::pConfig()->get(local_user(), 'twitter', 'post')) {
+       if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'post')) {
                $jotnets_fields[] = [
                        'type' => 'checkbox',
                        'field' => [
                                'twitter_enable',
                                DI::l10n()->t('Post to Twitter'),
-                               DI::pConfig()->get(local_user(), 'twitter', 'post_by_default')
+                               DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'post_by_default')
                        ]
                ];
        }
@@ -223,7 +225,7 @@ function twitter_jot_nets(App $a, array &$jotnets_fields)
 
 function twitter_settings_post(App $a)
 {
-       if (!local_user()) {
+       if (!DI::userSession()->getLocalUserId()) {
                return;
        }
        // don't check twitter settings if twitter submit button is not clicked
@@ -236,17 +238,19 @@ function twitter_settings_post(App $a)
                 * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
                 * from the user configuration
                 */
-               DI::pConfig()->delete(local_user(), 'twitter', 'consumerkey');
-               DI::pConfig()->delete(local_user(), 'twitter', 'consumersecret');
-               DI::pConfig()->delete(local_user(), 'twitter', 'oauthtoken');
-               DI::pConfig()->delete(local_user(), 'twitter', 'oauthsecret');
-               DI::pConfig()->delete(local_user(), 'twitter', 'post');
-               DI::pConfig()->delete(local_user(), 'twitter', 'post_by_default');
-               DI::pConfig()->delete(local_user(), 'twitter', 'lastid');
-               DI::pConfig()->delete(local_user(), 'twitter', 'mirror_posts');
-               DI::pConfig()->delete(local_user(), 'twitter', 'import');
-               DI::pConfig()->delete(local_user(), 'twitter', 'create_user');
-               DI::pConfig()->delete(local_user(), 'twitter', 'own_id');
+               DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'consumerkey');
+               DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'consumersecret');
+               DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'oauthtoken');
+               DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'oauthsecret');
+               DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'post');
+               DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'post_by_default');
+               DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'lastid');
+               DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'thread');
+               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'])) {
                        //  if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
@@ -264,25 +268,27 @@ function twitter_settings_post(App $a)
                                $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
                                $token = $connection->oauth('oauth/access_token', ['oauth_verifier' => $_POST['twitter-pin']]);
                                //  ok, now that we have the Access Token, save them in the user config
-                               DI::pConfig()->set(local_user(), 'twitter', 'oauthtoken', $token['oauth_token']);
-                               DI::pConfig()->set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']);
-                               DI::pConfig()->set(local_user(), 'twitter', 'post', 1);
+                               DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'oauthtoken', $token['oauth_token']);
+                               DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'oauthsecret', $token['oauth_token_secret']);
+                               DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'post', 1);
                        } catch(Exception $e) {
-                               notice($e->getMessage());
+                               DI::sysmsg()->addNotice($e->getMessage());
                        } catch(TwitterOAuthException $e) {
-                               notice($e->getMessage());
+                               DI::sysmsg()->addNotice($e->getMessage());
                        }
                } else {
                        //  if no PIN is supplied in the POST variables, the user has changed the setting
                        //  to post a tweet for every new __public__ posting to the wall
-                       DI::pConfig()->set(local_user(), 'twitter', 'post', intval($_POST['twitter-enable']));
-                       DI::pConfig()->set(local_user(), 'twitter', 'post_by_default', intval($_POST['twitter-default']));
-                       DI::pConfig()->set(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
-                       DI::pConfig()->set(local_user(), 'twitter', 'import', intval($_POST['twitter-import']));
-                       DI::pConfig()->set(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
+                       DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'post', intval($_POST['twitter-enable']));
+                       DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'post_by_default', intval($_POST['twitter-default']));
+                       DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'thread', intval($_POST['twitter-thread']));
+                       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(local_user(), 'twitter', 'lastid');
+                               DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'twitter', 'lastid');
                        }
                }
        }
@@ -290,11 +296,11 @@ function twitter_settings_post(App $a)
 
 function twitter_settings(App $a, array &$data)
 {
-       if (!local_user()) {
+       if (!DI::userSession()->getLocalUserId()) {
                return;
        }
 
-       $user = User::getById(local_user());
+       $user = User::getById(DI::userSession()->getLocalUserId());
 
        DI::page()->registerStylesheet(__DIR__ . '/twitter.css', 'all');
 
@@ -305,14 +311,16 @@ function twitter_settings(App $a, array &$data)
         */
        $ckey    = DI::config()->get('twitter', 'consumerkey');
        $csecret = DI::config()->get('twitter', 'consumersecret');
-       $otoken  = DI::pConfig()->get(local_user(), 'twitter', 'oauthtoken');
-       $osecret = DI::pConfig()->get(local_user(), 'twitter', 'oauthsecret');
+       $otoken  = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'oauthtoken');
+       $osecret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'oauthsecret');
 
-       $enabled            = intval(DI::pConfig()->get(local_user(), 'twitter', 'post'));
-       $defenabled         = intval(DI::pConfig()->get(local_user(), 'twitter', 'post_by_default'));
-       $mirrorenabled      = intval(DI::pConfig()->get(local_user(), 'twitter', 'mirror_posts'));
-       $importenabled      = intval(DI::pConfig()->get(local_user(), 'twitter', 'import'));
-       $create_userenabled = intval(DI::pConfig()->get(local_user(), 'twitter', 'create_user'));
+       $enabled            = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'post'));
+       $defenabled         = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'post_by_default'));
+       $threadenabled      = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'thread'));
+       $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 = '';
@@ -380,9 +388,11 @@ function twitter_settings(App $a, array &$data)
                                        '$account'     => $account,
                                        '$enable'      => ['twitter-enable', DI::l10n()->t('Allow posting to Twitter'), $enabled, DI::l10n()->t('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.')],
                                        '$default'     => ['twitter-default', DI::l10n()->t('Send public postings to Twitter by default'), $defenabled],
+                                       '$thread'      => ['twitter-thread', DI::l10n()->t('Use threads instead of truncating the content'), $threadenabled],
                                        '$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
@@ -432,7 +442,7 @@ function twitter_hook_fork(App $a, array &$b)
                return;
        }
 
-       if (substr($post['app'], 0, 7) == 'Twitter') {
+       if (substr($post['app'] ?? '', 0, 7) == 'Twitter') {
                DI::logger()->info('No Twitter app');
                $b['execute'] = false;
                return;
@@ -447,7 +457,7 @@ function twitter_hook_fork(App $a, array &$b)
                }
        } else {
                // Comments are never exported when we don't import the twitter timeline
-               if (!strstr($post['postopts'], 'twitter') || ($post['parent'] != $post['id']) || $post['private']) {
+               if (!strstr($post['postopts'] ?? '', 'twitter') || ($post['parent'] != $post['id']) || $post['private']) {
                        DI::logger()->info('Comments are never exported when we don\'t import the twitter timeline');
                        $b['execute'] = false;
                        return;
@@ -461,15 +471,15 @@ function twitter_post_local(App $a, array &$b)
                return;
        }
 
-       if (!local_user() || (local_user() != $b['uid'])) {
+       if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
                return;
        }
 
-       $twitter_post = intval(DI::pConfig()->get(local_user(), 'twitter', 'post'));
+       $twitter_post = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'post'));
        $twitter_enable = (($twitter_post && !empty($_REQUEST['twitter_enable'])) ? intval($_REQUEST['twitter_enable']) : 0);
 
        // if API is used, default to the chosen settings
-       if ($b['api_source'] && intval(DI::pConfig()->get(local_user(), 'twitter', 'post_by_default'))) {
+       if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'post_by_default'))) {
                $twitter_enable = 1;
        }
 
@@ -547,14 +557,14 @@ function twitter_item_by_link(App $a, array &$hookData)
                empty(DI::pConfig()->get($hookData['uid'], 'twitter', 'oauthtoken'))
                || empty(DI::pConfig()->get($hookData['uid'], 'twitter', 'oauthsecret'))
        ) {
-               notice(DI::l10n()->t('Please connect a Twitter account in your Social Network settings to import Twitter posts.'));
+               DI::sysmsg()->addNotice(DI::l10n()->t('Please connect a Twitter account in your Social Network settings to import Twitter posts.'));
                return;
        }
 
        $status = twitter_statuses_show($matches[1]);
 
        if (empty($status->id_str)) {
-               notice(DI::l10n()->t('Twitter post not found.'));
+               DI::sysmsg()->addNotice(DI::l10n()->t('Twitter post not found.'));
                return;
        }
 
@@ -564,16 +574,16 @@ function twitter_item_by_link(App $a, array &$hookData)
        }
 }
 
-function twitter_api_post(string $apiPath, string $pid, int $uid): ?bool
+function twitter_api_post(string $apiPath, string $pid, int $uid): ?object
 {
        if (empty($pid)) {
-               return false;
+               return null;
        }
 
        return twitter_api_call($uid, $apiPath, ['id' => $pid]);
 }
 
-function twitter_api_call(int $uid, string $apiPath, array $parameters = []): ?bool
+function twitter_api_call(int $uid, string $apiPath, array $parameters = []): ?object
 {
        $ckey = DI::config()->get('twitter', 'consumerkey');
        $csecret = DI::config()->get('twitter', 'consumersecret');
@@ -600,13 +610,13 @@ function twitter_api_call(int $uid, string $apiPath, array $parameters = []): ?b
                Logger::info('[twitter] API call successful', ['apiPath' => $apiPath, 'parameters' => $parameters]);
                Logger::debug('[twitter] API call result', ['apiPath' => $apiPath, 'parameters' => $parameters, 'result' => $result]);
 
-               return true;
+               return $result;
        } catch (TwitterOAuthException $twitterOAuthException) {
-               Logger::warning('Unable to communicate with twitter', ['apiPath' => $apiPath, 'parameters' => $parameters, 'code' => $twitterOAuthException->getCode(), 'exception' => $twitterOAuthException]);
-               return false;
+               Logger::notice('Unable to communicate with twitter', ['apiPath' => $apiPath, 'parameters' => $parameters, 'code' => $twitterOAuthException->getCode(), 'exception' => $twitterOAuthException]);
+               return null;
        } catch (Exception $e) {
                Logger::notice('[twitter] API call failed', ['apiPath' => $apiPath, 'parameters' => $parameters, 'code' => $e->getCode(), 'message' => $e->getMessage()]);
-               return false;
+               return null;
        }
 }
 
@@ -626,7 +636,7 @@ function twitter_get_id(string $uri)
 
 function twitter_post_hook(App $a, array &$b)
 {
-       DI::logger()->info('twitter_post_hook', $b);
+       DI::logger()->debug('Invoke post hook', $b);
 
        if ($b['deleted']) {
                twitter_delete_item($b);
@@ -639,7 +649,7 @@ function twitter_post_hook(App $a, array &$b)
                return;
        }
 
-       $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
+       $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], DI::contentItem()->addSharedPost($b));
 
        $thr_parent = null;
 
@@ -657,7 +667,7 @@ function twitter_post_hook(App $a, array &$b)
                $condition = ['uri' => $b['thr-parent'], 'uid' => $b['uid']];
                $thr_parent = Post::selectFirst(['uri', 'extid', 'author-link', 'author-nick', 'author-network'], $condition);
                if (!DBA::isResult($thr_parent)) {
-                       Logger::warning('No parent found', ['thr-parent' => $b['thr-parent']]);
+                       Logger::notice('No parent found', ['thr-parent' => $b['thr-parent']]);
                        return;
                }
 
@@ -813,25 +823,67 @@ function twitter_post_hook(App $a, array &$b)
                        }
                }
 
-               $post['status'] = $msg;
+               if (!DI::pConfig()->get($b['uid'], 'twitter', 'thread') || empty($msgarr['parts']) || (count($msgarr['parts']) == 1)) {
+                       Logger::debug('Post single message', ['id' => $b['id']]);
 
-               if ($thr_parent) {
-                       $post['in_reply_to_status_id'] = twitter_get_id($thr_parent['uri']);
-               }
+                       $post['status'] = $msg;
 
-               $result = $connection->post('statuses/update', $post);
-               Logger::info('twitter_post send', ['id' => $b['id'], 'result' => $result]);
+                       if ($thr_parent) {
+                               $post['in_reply_to_status_id'] = twitter_get_id($thr_parent['uri']);
+                       }
 
-               if (!empty($result->source)) {
-                       DI::config()->set('twitter', 'application_name', strip_tags($result->source));
-               }
+                       $result = $connection->post('statuses/update', $post);
+                       Logger::info('twitter_post send', ['id' => $b['id'], 'result' => $result]);
 
-               if (!empty($result->errors)) {
-                       Logger::error('Send to Twitter failed', ['id' => $b['id'], 'error' => $result->errors]);
-                       Worker::defer();
-               } elseif ($thr_parent) {
-                       Logger::notice('Post send, updating extid', ['id' => $b['id'], 'extid' => $result->id_str]);
-                       Item::update(['extid' => 'twitter::' . $result->id_str], ['id' => $b['id']]);
+                       if (!empty($result->source)) {
+                               DI::config()->set('twitter', 'application_name', strip_tags($result->source));
+                       }
+
+                       if (!empty($result->errors)) {
+                               Logger::error('Send to Twitter failed', ['id' => $b['id'], 'error' => $result->errors]);
+                               Worker::defer();
+                       } elseif ($thr_parent) {
+                               Logger::notice('Post send, updating extid', ['id' => $b['id'], 'extid' => $result->id_str]);
+                               Item::update(['extid' => 'twitter::' . $result->id_str], ['id' => $b['id']]);
+                       }
+               } else {
+                       if ($thr_parent) {
+                               $in_reply_to_status_id = twitter_get_id($thr_parent['uri']);
+                       } else {
+                               $in_reply_to_status_id = 0;
+                       }
+
+                       Logger::debug('Post message thread', ['id' => $b['id'], 'parts' => count($msgarr['parts'])]);
+                       foreach ($msgarr['parts'] as $key => $part) {
+                               $post['status'] = $part;
+
+                               if ($in_reply_to_status_id) {
+                                       $post['in_reply_to_status_id'] = $in_reply_to_status_id;
+                               }
+
+                               $result = $connection->post('statuses/update', $post);
+                               Logger::debug('twitter_post send', ['part' => $key, 'id' => $b['id'], 'result' => $result]);
+
+                               if (!empty($result->errors)) {
+                                       Logger::warning('Send to Twitter failed', ['part' => $key, 'id' => $b['id'], 'error' => $result->errors]);
+                                       Worker::defer();
+                                       break;
+                               } elseif ($key == 0) {
+                                       Logger::debug('Updating extid', ['part' => $key, 'id' => $b['id'], 'extid' => $result->id_str]);
+                                       Item::update(['extid' => 'twitter::' . $result->id_str], ['id' => $b['id']]);
+                               }
+
+                               if (!empty($result->source)) {
+                                       $application_name = strip_tags($result->source);
+                               }
+
+                               $in_reply_to_status_id = $result->id_str;
+                               unset($post['media_ids']);
+                       }
+
+                       if (!empty($application_name)) {
+                               DI::config()->set('twitter', 'application_name', strip_tags($result->source));
+                       }
                }
        }
 }
@@ -856,7 +908,7 @@ function twitter_delete_item(array $item)
                $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid']];
                $thr_parent = Post::selectFirst(['uri', 'extid', 'author-link', 'author-nick', 'author-network'], $condition);
                if (!DBA::isResult($thr_parent)) {
-                       Logger::warning('No parent found', ['thr-parent' => $item['thr-parent']]);
+                       Logger::notice('No parent found', ['thr-parent' => $item['thr-parent']]);
                        return;
                }
 
@@ -940,7 +992,7 @@ function twitter_cron(App $a)
        $pconfigs = DBA::selectToArray('pconfig', [], ['cat' => 'twitter', 'k' => 'mirror_posts', 'v' => true]);
        foreach ($pconfigs as $rr) {
                Logger::notice('Fetching', ['user' => $rr['uid']]);
-               Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], 'addon/twitter/twitter_sync.php', 1, (int) $rr['uid']);
+               Worker::add(['priority' => Worker::PRIORITY_MEDIUM, 'force_priority' => true], 'addon/twitter/twitter_sync.php', 1, (int) $rr['uid']);
        }
 
        $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
@@ -960,7 +1012,7 @@ function twitter_cron(App $a)
                }
 
                Logger::notice('importing timeline', ['user' => $rr['uid']]);
-               Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], 'addon/twitter/twitter_sync.php', 2, (int) $rr['uid']);
+               Worker::add(['priority' => Worker::PRIORITY_MEDIUM, 'force_priority' => true], 'addon/twitter/twitter_sync.php', 2, (int) $rr['uid']);
                /*
                        // To-Do
                        // check for new contacts once a day
@@ -1023,7 +1075,7 @@ function twitter_prepare_body(App $a, array &$b)
                $item = $b['item'];
                $item['plink'] = DI::baseUrl()->get() . '/display/' . $item['guid'];
 
-               $condition = ['uri' => $item['thr-parent'], 'uid' => local_user()];
+               $condition = ['uri' => $item['thr-parent'], 'uid' => DI::userSession()->getLocalUserId()];
                $orig_post = Post::selectFirst(['author-link'], $condition);
                if (DBA::isResult($orig_post)) {
                        $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post['author-link']);
@@ -1094,6 +1146,9 @@ function twitter_parse_link(App $a, array &$b)
        }
 
        $item = twitter_createpost($a, 0, $status, [], true, false, true);
+       if (empty($item)) {
+               return;
+       }
 
        if ($b['format'] == 'json') {
                $images = [];
@@ -1157,7 +1212,7 @@ function twitter_do_mirrorpost(App $a, int $uid, $post)
                // We don't support nested shares, so we mustn't show quotes as shares on retweets
                $item = twitter_createpost($a, $uid, $post->retweeted_status, ['id' => 0], false, false, true, -1);
 
-               if (empty($item['body'])) {
+               if (empty($item)) {
                        return [];
                }
 
@@ -1173,7 +1228,7 @@ function twitter_do_mirrorpost(App $a, int $uid, $post)
        } else {
                $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false, -1);
 
-               if (empty($item['body'])) {
+               if (empty($item)) {
                        return [];
                }
 
@@ -1194,7 +1249,15 @@ function twitter_do_mirrorpost(App $a, int $uid, $post)
        return $datarray;
 }
 
-function twitter_fetchtimeline(App $a, int $uid)
+/**
+ * Fetches the Twitter user's own posts
+ *
+ * @param App $a
+ * @param int $uid
+ * @return void
+ * @throws Exception
+ */
+function twitter_fetchtimeline(App $a, int $uid): void
 {
        $ckey    = DI::config()->get('twitter', 'consumerkey');
        $csecret = DI::config()->get('twitter', 'consumersecret');
@@ -1214,7 +1277,7 @@ function twitter_fetchtimeline(App $a, int $uid)
        try {
                twitter_fetch_own_contact($a, $uid);
        } catch (TwitterOAuthException $e) {
-               Logger::warning('Error fetching own contact', ['uid' => $uid, 'message' => $e->getMessage()]);
+               Logger::notice('Error fetching own contact', ['uid' => $uid, 'message' => $e->getMessage()]);
                return;
        }
 
@@ -1236,7 +1299,7 @@ function twitter_fetchtimeline(App $a, int $uid)
        try {
                $items = $connection->get('statuses/user_timeline', $parameters);
        } catch (TwitterOAuthException $e) {
-               Logger::warning('Error fetching timeline', ['uid' => $uid, 'message' => $e->getMessage()]);
+               Logger::notice('Error fetching timeline', ['uid' => $uid, 'message' => $e->getMessage()]);
                return;
        }
 
@@ -1257,22 +1320,26 @@ function twitter_fetchtimeline(App $a, int $uid)
                        }
 
                        if ($first_time) {
+                               Logger::notice('First time, continue');
                                continue;
                        }
 
-                       if (!stristr($post->source, $application_name)) {
-                               Logger::info('Preparing mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]);
+                       if (stristr($post->source, $application_name)) {
+                               Logger::notice('Source is application name', ['source' => $post->source, 'application_name' => $application_name]);
+                               continue;
+                       }
+                       Logger::info('Preparing mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]);
 
-                               $mirrorpost = twitter_do_mirrorpost($a, $uid, $post);
+                       $mirrorpost = twitter_do_mirrorpost($a, $uid, $post);
 
-                               if (empty($mirrorpost['body'])) {
-                                       continue;
-                               }
+                       if (empty($mirrorpost['body'])) {
+                               Logger::notice('Body is empty', ['post' => $post, 'mirrorpost' => $mirrorpost]);
+                               continue;
+                       }
 
-                               Logger::info('Posting mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]);
+                       Logger::info('Posting mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]);
 
-                               Post\Delayed::add($mirrorpost['extid'], $mirrorpost, PRIORITY_MEDIUM, Post\Delayed::UNPREPARED);
-                       }
+                       Post\Delayed::add($mirrorpost['extid'], $mirrorpost, Worker::PRIORITY_MEDIUM, Post\Delayed::UNPREPARED);
                }
        }
        DI::pConfig()->set($uid, 'twitter', 'lastid', $lastid);
@@ -1329,7 +1396,7 @@ function twitter_get_relation($uid, $target, $contact = [])
 
                Logger::info('Fetched friendship relation', ['user' => $uid, 'target' => $target, 'relation' => $relation]);
        } catch (Throwable $e) {
-               Logger::warning('Error fetching friendship status', ['uid' => $uid, 'target' => $target, 'message' => $e->getMessage()]);
+               Logger::notice('Error fetching friendship status', ['uid' => $uid, 'target' => $target, 'message' => $e->getMessage()]);
        }
 
        return $relation;
@@ -1402,12 +1469,12 @@ function twitter_fetch_contact($uid, $data, $create_user)
                Contact::update($fields, ['id' => $cid]);
                Contact::updateAvatar($cid, $avatar);
        } else {
-               Logger::warning('No contact found', ['fields' => $fields]);
+               Logger::notice('No contact found', ['fields' => $fields]);
        }
 
        $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => 'twitter::' . $data->id_str]);
        if (!DBA::isResult($contact) && empty($cid)) {
-               Logger::warning('User contact not found', ['uid' => $uid, 'twitter-id' => $data->id_str]);
+               Logger::notice('User contact not found', ['uid' => $uid, 'twitter-id' => $data->id_str]);
                return 0;
        } elseif (!$create_user) {
                return $cid;
@@ -1472,9 +1539,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
@@ -1491,7 +1635,7 @@ function twitter_fetchuser($screen_name)
                $parameters = ['screen_name' => $screen_name];
                $user = $connection->get('users/show', $parameters);
        } catch (TwitterOAuthException $e) {
-               Logger::warning('Error fetching user', ['user' => $screen_name, 'message' => $e->getMessage()]);
+               Logger::notice('Error fetching user', ['user' => $screen_name, 'message' => $e->getMessage()]);
                return null;
        }
 
@@ -1766,7 +1910,7 @@ function twitter_createpost(App $a, int $uid, $post, array $self, $create_user,
 
                $item = Post::selectFirst(['uri'], ['uri' => $thr_parent, 'uid' => $uid]);
                if (!DBA::isResult($item)) {
-                       $item = Post::selectFirst(['uri'], ['extid' => $thr_parent, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]);
+                       $item = Post::selectFirst(['uri'], ['extid' => $thr_parent, 'uid' => $uid, 'gravity' => Item::GRAVITY_COMMENT]);
                }
 
                if (DBA::isResult($item)) {
@@ -1784,6 +1928,7 @@ function twitter_createpost(App $a, int $uid, $post, array $self, $create_user,
                        if (DBA::isResult($self)) {
                                $contactid = $self['id'];
 
+                               $postarray['owner-id']     = Contact::getIdForURL($self['url']);
                                $postarray['owner-name']   = $self['name'];
                                $postarray['owner-link']   = $self['url'];
                                $postarray['owner-avatar'] = $self['photo'];
@@ -1876,7 +2021,7 @@ function twitter_createpost(App $a, int $uid, $post, array $self, $create_user,
        if (!empty($post->retweeted_status)) {
                $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
 
-               if (empty($retweet['body'])) {
+               if (empty($retweet)) {
                        return [];
                }
 
@@ -1886,7 +2031,7 @@ function twitter_createpost(App $a, int $uid, $post, array $self, $create_user,
 
                        // CHange the other post into a reshare activity
                        $postarray['verb'] = Activity::ANNOUNCE;
-                       $postarray['gravity'] = GRAVITY_ACTIVITY;
+                       $postarray['gravity'] = Item::GRAVITY_ACTIVITY;
                        $postarray['object-type'] = Activity\ObjectType::NOTE;
 
                        $postarray['thr-parent'] = $retweet['uri'];
@@ -1911,7 +2056,7 @@ function twitter_createpost(App $a, int $uid, $post, array $self, $create_user,
                        $postarray['body'] .= "\n\nhttps://twitter.com/" . $post->quoted_status->user->screen_name . "/status/" . $post->quoted_status->id_str;
                } else {
                        $quoted = twitter_createpost($a, 0, $post->quoted_status, $self, false, false, true);
-                       if (!empty($quoted['body'])) {
+                       if (!empty($quoted)) {
                                Item::insert($quoted);
                                $post = Post::selectFirst(['guid', 'uri-id'], ['uri' => $quoted['uri'], 'uid' => 0]);
                                Logger::info('Stored quoted post', ['uid' => $uid, 'uri-id' => $uriId, 'post' => $post]);
@@ -1960,7 +2105,7 @@ function twitter_fetchparentposts(App $a, int $uid, $post, TwitterOAuth $connect
                try {
                        $post = twitter_statuses_show($post->in_reply_to_status_id_str, $connection);
                } catch (TwitterOAuthException $e) {
-                       Logger::warning('Error fetching parent post', ['uid' => $uid, 'post' => $post->id_str, 'message' => $e->getMessage()]);
+                       Logger::notice('Error fetching parent post', ['uid' => $uid, 'post' => $post->id_str, 'message' => $e->getMessage()]);
                        break;
                }
 
@@ -1989,7 +2134,7 @@ function twitter_fetchparentposts(App $a, int $uid, $post, TwitterOAuth $connect
                foreach ($posts as $post) {
                        $postarray = twitter_createpost($a, $uid, $post, $self, false, !DI::pConfig()->get($uid, 'twitter', 'create_user'), false);
 
-                       if (empty($postarray['body'])) {
+                       if (empty($postarray)) {
                                continue;
                        }
 
@@ -2002,7 +2147,15 @@ function twitter_fetchparentposts(App $a, int $uid, $post, TwitterOAuth $connect
        }
 }
 
-function twitter_fetchhometimeline(App $a, int $uid)
+/**
+ * Fetches the posts received by the Twitter user
+ *
+ * @param App $a
+ * @param int $uid
+ * @return void
+ * @throws Exception
+ */
+function twitter_fetchhometimeline(App $a, int $uid): void
 {
        $ckey    = DI::config()->get('twitter', 'consumerkey');
        $csecret = DI::config()->get('twitter', 'consumersecret');
@@ -2024,7 +2177,7 @@ function twitter_fetchhometimeline(App $a, int $uid)
        try {
                $own_contact = twitter_fetch_own_contact($a, $uid);
        } catch (TwitterOAuthException $e) {
-               Logger::warning('Error fetching own contact', ['uid' => $uid, 'message' => $e->getMessage()]);
+               Logger::notice('Error fetching own contact', ['uid' => $uid, 'message' => $e->getMessage()]);
                return;
        }
 
@@ -2032,7 +2185,7 @@ function twitter_fetchhometimeline(App $a, int $uid)
        if (DBA::isResult($contact)) {
                $own_id = $contact['nick'];
        } else {
-               Logger::warning('Own twitter contact not found', ['uid' => $uid]);
+               Logger::notice('Own twitter contact not found', ['uid' => $uid]);
                return;
        }
 
@@ -2064,17 +2217,17 @@ function twitter_fetchhometimeline(App $a, int $uid)
        try {
                $items = $connection->get('statuses/home_timeline', $parameters);
        } catch (TwitterOAuthException $e) {
-               Logger::warning('Error fetching home timeline', ['uid' => $uid, 'message' => $e->getMessage()]);
+               Logger::notice('Error fetching home timeline', ['uid' => $uid, 'message' => $e->getMessage()]);
                return;
        }
 
        if (!is_array($items)) {
-               Logger::warning('home timeline is no array', ['items' => $items]);
+               Logger::notice('home timeline is no array', ['items' => $items]);
                return;
        }
 
        if (empty($items)) {
-               Logger::notice('No new timeline content', ['uid' => $uid]);
+               Logger::info('No new timeline content', ['uid' => $uid]);
                return;
        }
 
@@ -2111,8 +2264,8 @@ function twitter_fetchhometimeline(App $a, int $uid)
 
                        $postarray = twitter_createpost($a, $uid, $post, $self, $create_user, true, false);
 
-                       if (empty($postarray['body']) || trim($postarray['body']) == '') {
-                               Logger::info('Empty body for post ' . $post->id_str . ' and user ' . $uid);
+                       if (empty($postarray)) {
+                               Logger::info('Empty post ' . $post->id_str . ' and user ' . $uid);
                                continue;
                        }
 
@@ -2121,7 +2274,7 @@ function twitter_fetchhometimeline(App $a, int $uid)
                        if (empty($postarray['thr-parent'])) {
                                $contact = DBA::selectFirst('contact', [], ['id' => $postarray['contact-id'], 'self' => false]);
                                if (DBA::isResult($contact) && Item::isRemoteSelf($contact, $postarray)) {
-                                       $notify = PRIORITY_MEDIUM;
+                                       $notify = Worker::PRIORITY_MEDIUM;
                                }
                        }
 
@@ -2147,12 +2300,12 @@ function twitter_fetchhometimeline(App $a, int $uid)
        try {
                $items = $connection->get('statuses/mentions_timeline', $parameters);
        } catch (TwitterOAuthException $e) {
-               Logger::warning('Error fetching mentions', ['uid' => $uid, 'message' => $e->getMessage()]);
+               Logger::notice('Error fetching mentions', ['uid' => $uid, 'message' => $e->getMessage()]);
                return;
        }
 
        if (!is_array($items)) {
-               Logger::warning('mentions are no arrays', ['items' => $items]);
+               Logger::notice('mentions are no arrays', ['items' => $items]);
                return;
        }
 
@@ -2176,7 +2329,7 @@ function twitter_fetchhometimeline(App $a, int $uid)
 
                        $postarray = twitter_createpost($a, $uid, $post, $self, false, !$create_user, false);
 
-                       if (empty($postarray['body'])) {
+                       if (empty($postarray)) {
                                continue;
                        }