* 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.ini.php or use the admin panel.
+ * Add this key pair to your global config/addon.config.php or use the admin panel.
*
- * [twitter]
- * consumerkey = your consumer_key here
- * consumersecret = your consumer_secret here
+ * 'twitter' => [
+ * 'consumerkey' => '',
+ * 'consumersecret' => '',
+ * ],
*
- * To activate the addon itself add it to the [system] addon
+ * To activate the addon itself add it to the system.addon
* setting. After this, your user can configure their Twitter account settings
* from "Settings -> Addon Settings".
*
use Abraham\TwitterOAuth\TwitterOAuth;
use Abraham\TwitterOAuth\TwitterOAuthException;
+use Codebird\Codebird;
use Friendica\App;
use Friendica\Content\OEmbed;
+use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\Plaintext;
-use Friendica\Core\Addon;
use Friendica\Core\Config;
+use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Core\PConfig;
use Friendica\Core\Protocol;
+use Friendica\Core\Renderer;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\Conversation;
-use Friendica\Model\GContact;
use Friendica\Model\Group;
use Friendica\Model\Item;
use Friendica\Model\ItemContent;
-use Friendica\Model\Queue;
use Friendica\Model\User;
use Friendica\Object\Image;
+use Friendica\Protocol\Activity;
+use Friendica\Util\ConfigFileLoader;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Network;
-
-require_once 'boot.php';
-require_once 'include/dba.php';
-require_once 'include/enotify.php';
-require_once 'include/text.php';
+use Friendica\Util\Strings;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
function twitter_install()
{
// we need some hooks, for the configuration and for sending tweets
- Addon::registerHook('load_config' , __FILE__, 'twitter_load_config');
- Addon::registerHook('connector_settings' , __FILE__, 'twitter_settings');
- Addon::registerHook('connector_settings_post', __FILE__, 'twitter_settings_post');
- Addon::registerHook('post_local' , __FILE__, 'twitter_post_local');
- Addon::registerHook('notifier_normal' , __FILE__, 'twitter_post_hook');
- Addon::registerHook('jot_networks' , __FILE__, 'twitter_jot_nets');
- Addon::registerHook('cron' , __FILE__, 'twitter_cron');
- Addon::registerHook('queue_predeliver' , __FILE__, 'twitter_queue_hook');
- Addon::registerHook('follow' , __FILE__, 'twitter_follow');
- Addon::registerHook('expire' , __FILE__, 'twitter_expire');
- Addon::registerHook('prepare_body' , __FILE__, 'twitter_prepare_body');
- Addon::registerHook('check_item_notification', __FILE__, 'twitter_check_item_notification');
- Logger::log("installed twitter");
+ Hook::register('load_config' , __FILE__, 'twitter_load_config');
+ Hook::register('connector_settings' , __FILE__, 'twitter_settings');
+ Hook::register('connector_settings_post', __FILE__, 'twitter_settings_post');
+ Hook::register('hook_fork' , __FILE__, 'twitter_hook_fork');
+ Hook::register('post_local' , __FILE__, 'twitter_post_local');
+ Hook::register('notifier_normal' , __FILE__, 'twitter_post_hook');
+ Hook::register('jot_networks' , __FILE__, 'twitter_jot_nets');
+ Hook::register('cron' , __FILE__, 'twitter_cron');
+ Hook::register('follow' , __FILE__, 'twitter_follow');
+ Hook::register('expire' , __FILE__, 'twitter_expire');
+ Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body');
+ Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification');
+ Logger::info("installed twitter");
}
function twitter_uninstall()
{
- Addon::unregisterHook('load_config' , __FILE__, 'twitter_load_config');
- Addon::unregisterHook('connector_settings' , __FILE__, 'twitter_settings');
- Addon::unregisterHook('connector_settings_post', __FILE__, 'twitter_settings_post');
- Addon::unregisterHook('post_local' , __FILE__, 'twitter_post_local');
- Addon::unregisterHook('notifier_normal' , __FILE__, 'twitter_post_hook');
- Addon::unregisterHook('jot_networks' , __FILE__, 'twitter_jot_nets');
- Addon::unregisterHook('cron' , __FILE__, 'twitter_cron');
- Addon::unregisterHook('queue_predeliver' , __FILE__, 'twitter_queue_hook');
- Addon::unregisterHook('follow' , __FILE__, 'twitter_follow');
- Addon::unregisterHook('expire' , __FILE__, 'twitter_expire');
- Addon::unregisterHook('prepare_body' , __FILE__, 'twitter_prepare_body');
- Addon::unregisterHook('check_item_notification', __FILE__, 'twitter_check_item_notification');
+ Hook::unregister('load_config' , __FILE__, 'twitter_load_config');
+ Hook::unregister('connector_settings' , __FILE__, 'twitter_settings');
+ Hook::unregister('connector_settings_post', __FILE__, 'twitter_settings_post');
+ Hook::unregister('hook_fork' , __FILE__, 'twitter_hook_fork');
+ Hook::unregister('post_local' , __FILE__, 'twitter_post_local');
+ Hook::unregister('notifier_normal' , __FILE__, 'twitter_post_hook');
+ Hook::unregister('jot_networks' , __FILE__, 'twitter_jot_nets');
+ Hook::unregister('cron' , __FILE__, 'twitter_cron');
+ Hook::unregister('follow' , __FILE__, 'twitter_follow');
+ Hook::unregister('expire' , __FILE__, 'twitter_expire');
+ Hook::unregister('prepare_body' , __FILE__, 'twitter_prepare_body');
+ Hook::unregister('check_item_notification', __FILE__, 'twitter_check_item_notification');
// old setting - remove only
- Addon::unregisterHook('post_local_end' , __FILE__, 'twitter_post_hook');
- Addon::unregisterHook('addon_settings' , __FILE__, 'twitter_settings');
- Addon::unregisterHook('addon_settings_post', __FILE__, 'twitter_settings_post');
+ Hook::unregister('post_local_end' , __FILE__, 'twitter_post_hook');
+ Hook::unregister('addon_settings' , __FILE__, 'twitter_settings');
+ Hook::unregister('addon_settings_post', __FILE__, 'twitter_settings_post');
}
-function twitter_load_config(App $a)
+function twitter_load_config(App $a, ConfigFileLoader $loader)
{
- $a->loadConfigFile(__DIR__ . '/config/twitter.ini.php');
+ $a->getConfigCache()->load($loader->loadAddonConfig('twitter'));
}
function twitter_check_item_notification(App $a, array &$notification_data)
function twitter_follow(App $a, array &$contact)
{
- Logger::log("twitter_follow: Check if contact is twitter contact. " . $contact["url"], Logger::DEBUG);
+ Logger::info('Check if contact is twitter contact', ['url' => $contact["url"]]);
if (!strstr($contact["url"], "://twitter.com") && !strstr($contact["url"], "@twitter.com")) {
return;
}
}
-function twitter_jot_nets(App $a, &$b)
+function twitter_jot_nets(App $a, array &$jotnets_fields)
{
if (!local_user()) {
return;
}
- $tw_post = PConfig::get(local_user(), 'twitter', 'post');
- if (intval($tw_post) == 1) {
- $tw_defpost = PConfig::get(local_user(), 'twitter', 'post_by_default');
- $selected = ((intval($tw_defpost) == 1) ? ' checked="checked" ' : '');
- $b .= '<div class="profile-jot-net"><input type="checkbox" name="twitter_enable"' . $selected . ' value="1" /> '
- . L10n::t('Post to Twitter') . '</div>';
+ if (PConfig::get(local_user(), 'twitter', 'post')) {
+ $jotnets_fields[] = [
+ 'type' => 'checkbox',
+ 'field' => [
+ 'twitter_enable',
+ L10n::t('Post to Twitter'),
+ PConfig::get(local_user(), 'twitter', 'post_by_default')
+ ]
+ ];
}
}
+
function twitter_settings_post(App $a)
{
if (!local_user()) {
} else {
if (isset($_POST['twitter-pin'])) {
// if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
- Logger::log('got a Twitter PIN');
+ Logger::notice('got a Twitter PIN');
$ckey = Config::get('twitter', 'consumerkey');
$csecret = Config::get('twitter', 'consumersecret');
// the token and secret for which the PIN was generated were hidden in the settings
try {
$details = $connection->get('account/verify_credentials');
- $field_checkbox = get_markup_template('field_checkbox.tpl');
+ $field_checkbox = Renderer::getMarkupTemplate('field_checkbox.tpl');
- $s .= '<div id="twitter-info" >
+ if (property_exists($details, 'screen_name') &&
+ property_exists($details, 'description') &&
+ property_exists($details, 'profile_image_url')) {
+ $s .= '<div id="twitter-info" >
<p>' . L10n::t('Currently connected to: ') . '<a href="https://twitter.com/' . $details->screen_name . '" target="_twitter">' . $details->screen_name . '</a>
<button type="submit" name="twitter-disconnect" value="1">' . L10n::t('Disconnect') . '</button>
</p>
<em>' . $details->description . '</em>
</p>
</div>';
+ } else {
+ $s .= '<div id="twitter-info" >
+ <p>Invalid Twitter info</p>
+ </div>';
+ Logger::info('Invalid twitter info (verify credentials).', ['auth' => TwitterOAuth::class]);
+ }
$s .= '<div class="clear"></div>';
- $s .= replace_macros($field_checkbox, [
+ $s .= Renderer::replaceMacros($field_checkbox, [
'$field' => ['twitter-enable', L10n::t('Allow posting to Twitter'), $enabled, 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.')]
]);
if ($a->user['hidewall']) {
$s .= '<p>' . L10n::t('<strong>Note</strong>: Due to your privacy settings (<em>Hide your profile details from unknown viewers?</em>) the link potentially included in public postings relayed to Twitter will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.') . '</p>';
}
- $s .= replace_macros($field_checkbox, [
+ $s .= Renderer::replaceMacros($field_checkbox, [
'$field' => ['twitter-default', L10n::t('Send public postings to Twitter by default'), $defenabled, '']
]);
- $s .= replace_macros($field_checkbox, [
+ $s .= Renderer::replaceMacros($field_checkbox, [
'$field' => ['twitter-mirror', L10n::t('Mirror all posts from twitter that are no replies'), $mirrorenabled, '']
]);
- $s .= replace_macros($field_checkbox, [
+ $s .= Renderer::replaceMacros($field_checkbox, [
'$field' => ['twitter-import', L10n::t('Import the remote timeline'), $importenabled, '']
]);
- $s .= replace_macros($field_checkbox, [
+ $s .= Renderer::replaceMacros($field_checkbox, [
'$field' => ['twitter-create_user', L10n::t('Automatically create contacts'), $create_userenabled, 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. However if enabled, you cannot merely remove a twitter contact from the Friendica contact list, as it will recreate this contact when they post again.')]
]);
$s .= '<div class="clear"></div>';
$s .= '</div><div class="clear"></div>';
}
+function twitter_hook_fork(App $a, array &$b)
+{
+ if ($b['name'] != 'notifier_normal') {
+ return;
+ }
+
+ $post = $b['data'];
+
+ // Deleting and editing is not supported by the addon (deleting could, but isn't by now)
+ if ($post['deleted'] || ($post['created'] !== $post['edited'])) {
+ $b['execute'] = false;
+ return;
+ }
+
+ // if post comes from twitter don't send it back
+ if ($post['extid'] == Protocol::TWITTER) {
+ $b['execute'] = false;
+ return;
+ }
+
+ if ($post['app'] == 'Twitter') {
+ $b['execute'] = false;
+ return;
+ }
+
+ if (PConfig::get($post['uid'], 'twitter', 'import')) {
+ // Don't fork if it isn't a reply to a twitter post
+ if (($post['parent'] != $post['id']) && !Item::exists(['id' => $post['parent'], 'network' => Protocol::TWITTER])) {
+ Logger::notice('No twitter parent found', ['item' => $post['id']]);
+ $b['execute'] = false;
+ return;
+ }
+ } else {
+ // Comments are never exported when we don't import the twitter timeline
+ if (!strstr($post['postopts'], 'twitter') || ($post['parent'] != $post['id']) || $post['private']) {
+ $b['execute'] = false;
+ return;
+ }
+ }
+}
+
function twitter_post_local(App $a, array &$b)
{
if ($b['edit']) {
}
$twitter_post = intval(PConfig::get(local_user(), 'twitter', 'post'));
- $twitter_enable = (($twitter_post && x($_REQUEST, 'twitter_enable')) ? intval($_REQUEST['twitter_enable']) : 0);
+ $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(PConfig::get(local_user(), 'twitter', 'post_by_default'))) {
}
}
- if (($b['verb'] == ACTIVITY_POST) && $b['deleted']) {
+ if (($b['verb'] == Activity::POST) && $b['deleted']) {
twitter_action($a, $b["uid"], substr($orig_post["uri"], 9), "delete");
}
- if ($b['verb'] == ACTIVITY_LIKE) {
+ if ($b['verb'] == Activity::LIKE) {
Logger::log("twitter_post_hook: parameter 2 " . substr($b["thr-parent"], 9), Logger::DEBUG);
if ($b['deleted']) {
twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "unlike");
return;
}
- Logger::log('twitter post invoked');
+ Logger::notice('twitter post invoked', ['id' => $b['id'], 'guid' => $b['guid']]);
PConfig::load($b['uid'], 'twitter');
return;
}
+ Codebird::setConsumerKey($ckey, $csecret);
+ $cb = Codebird::getInstance();
+ $cb->setToken($otoken, $osecret);
+
$connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
// Set the timeout for upload to 30 seconds
$b['body'] = twitter_update_mentions($b['body']);
$msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 8);
+ Logger::info('Got plaintext', ['id' => $b['id'], 'message' => $msgarr]);
$msg = $msgarr["text"];
if (($msg == "") && isset($msgarr["title"])) {
$msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
}
- $image = "";
-
- if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
+ if (!empty($msgarr['url']) && ($msgarr['url'] == $b['plink']) && !empty($msgarr['images']) && (count($msgarr['images']) <= 4)) {
+ $url_added = false;
+ } elseif (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
$msg .= "\n" . $msgarr["url"];
$url_added = true;
} else {
$url_added = false;
}
- if (isset($msgarr["image"]) && ($msgarr["type"] != "video")) {
- $image = $msgarr["image"];
- }
-
if (empty($msg)) {
+ Logger::info('Empty message', ['id' => $b['id']]);
return;
}
// and now tweet it :-)
$post = [];
- if (!empty($image)) {
+ if (!empty($msgarr['images'])) {
+ Logger::info('Got images', ['id' => $b['id'], 'images' => $msgarr['images']]);
try {
- $img_str = Network::fetchUrl($image);
+ $media_ids = [];
+ foreach ($msgarr['images'] as $image) {
+ if (count($media_ids) == 4) {
+ continue;
+ }
- $tempfile = tempnam(get_temppath(), 'cache');
- file_put_contents($tempfile, $img_str);
+ $img_str = Network::fetchUrl($image['url']);
- $media = $connection->upload('media/upload', ['media' => $tempfile]);
+ $tempfile = tempnam(get_temppath(), 'cache');
+ file_put_contents($tempfile, $img_str);
- unlink($tempfile);
+ Logger::info('Uploading', ['id' => $b['id'], 'image' => $image['url']]);
+ $media = $connection->upload('media/upload', ['media' => $tempfile]);
- if (isset($media->media_id_string)) {
- $post['media_ids'] = $media->media_id_string;
- } else {
- throw new Exception('Failed upload of ' . $image);
- }
- } catch (Exception $e) {
- Logger::log('Exception when trying to send to Twitter: ' . $e->getMessage());
+ unlink($tempfile);
- // Workaround: Remove the picture link so that the post can be reposted without it
- // When there is another url already added, a second url would be superfluous.
- if (!$url_added) {
- $msg .= "\n" . $image;
- }
+ if (isset($media->media_id_string)) {
+ $media_ids[] = $media->media_id_string;
- $image = "";
+ if (!empty($image['description'])) {
+ $data = ['media_id' => $media->media_id_string,
+ 'alt_text' => ['text' => substr($image['description'], 0, 420)]];
+ $ret = $cb->media_metadata_create($data);
+ Logger::info('Metadata create', ['id' => $b['id'], 'data' => $data, 'return' => json_encode($ret)]);
+ }
+ } else {
+ throw new Exception('Failed upload', ['id' => $b['id'], 'image' => $image['url']]);
+ }
+ }
+ $post['media_ids'] = implode(',', $media_ids);
+ if (empty($post['media_ids'])) {
+ unset($post['media_ids']);
+ }
+ } catch (Exception $e) {
+ Logger::info('Exception when trying to send to Twitter', ['id' => $b['id'], 'message' => $e->getMessage()]);
}
}
$url = 'statuses/update';
$result = $connection->post($url, $post);
- Logger::log('twitter_post send, result: ' . print_r($result, true), Logger::DEBUG);
+ Logger::info('twitter_post send', ['id' => $b['id'], 'result' => $result]);
if (!empty($result->source)) {
Config::set("twitter", "application_name", strip_tags($result->source));
}
if (!empty($result->errors)) {
- Logger::log('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
-
- $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid']));
- if (DBA::isResult($r)) {
- $a->contact = $r[0]["id"];
- }
-
- $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $post]);
-
- Queue::add($a->contact, Protocol::TWITTER, $s);
- notice(L10n::t('Twitter post failed. Queued for retry.') . EOL);
+ Logger::info('Send to Twitter failed', ['id' => $b['id'], 'error' => $result->errors]);
+ Worker::defer();
} elseif ($iscomment) {
- Logger::log('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
+ Logger::info('Update extid', ['id' => $b['id'], 'extid' => $result->id_str]);
Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
}
}
function twitter_addon_admin_post(App $a)
{
- $consumerkey = x($_POST, 'consumerkey') ? notags(trim($_POST['consumerkey'])) : '';
- $consumersecret = x($_POST, 'consumersecret') ? notags(trim($_POST['consumersecret'])) : '';
+ $consumerkey = !empty($_POST['consumerkey']) ? Strings::escapeTags(trim($_POST['consumerkey'])) : '';
+ $consumersecret = !empty($_POST['consumersecret']) ? Strings::escapeTags(trim($_POST['consumersecret'])) : '';
Config::set('twitter', 'consumerkey', $consumerkey);
Config::set('twitter', 'consumersecret', $consumersecret);
info(L10n::t('Settings updated.') . EOL);
function twitter_addon_admin(App $a, &$o)
{
- $t = get_markup_template("admin.tpl", "addon/twitter/");
+ $t = Renderer::getMarkupTemplate("admin.tpl", "addon/twitter/");
- $o = replace_macros($t, [
+ $o = Renderer::replaceMacros($t, [
'$submit' => L10n::t('Save Settings'),
// name, label, value, help, [extra values]
'$consumerkey' => ['consumerkey', L10n::t('Consumer key'), Config::get('twitter', 'consumerkey'), ''],
if ($last) {
$next = $last + ($poll_interval * 60);
if ($next > time()) {
- Logger::log('twitter: poll intervall not reached');
+ Logger::notice('twitter: poll intervall not reached');
return;
}
}
- Logger::log('twitter: cron_start');
+ Logger::notice('twitter: cron_start');
$r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1'");
if (DBA::isResult($r)) {
foreach ($r as $rr) {
- Logger::log('twitter: fetching for user ' . $rr['uid']);
- Worker::add(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 1, (int) $rr['uid']);
+ Logger::notice('Fetching', ['user' => $rr['uid']]);
+ Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], "addon/twitter/twitter_sync.php", 1, (int) $rr['uid']);
}
}
if ($abandon_days != 0) {
$user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
if (!DBA::isResult($user)) {
- Logger::log('abandoned account: timeline from user ' . $rr['uid'] . ' will not be imported');
+ Logger::notice('abandoned account: timeline from user will not be imported', ['user' => $rr['uid']]);
continue;
}
}
- Logger::log('twitter: importing timeline from user ' . $rr['uid']);
- Worker::add(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 2, (int) $rr['uid']);
+ Logger::notice('importing timeline', ['user' => $rr['uid']]);
+ Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], "addon/twitter/twitter_sync.php", 2, (int) $rr['uid']);
/*
// To-Do
// check for new contacts once a day
}
}
- Logger::log('twitter: cron_end');
+ Logger::notice('twitter: cron_end');
Config::set('twitter', 'last_poll', time());
}
}
DBA::close($r);
- require_once "include/items.php";
-
- Logger::log('twitter_expire: expire_start');
+ Logger::notice('twitter_expire: expire_start');
$r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
if (DBA::isResult($r)) {
foreach ($r as $rr) {
- Logger::log('twitter_expire: user ' . $rr['uid']);
+ Logger::notice('twitter_expire', ['user' => $rr['uid']]);
Item::expire($rr['uid'], $days, Protocol::TWITTER, true);
}
}
- Logger::log('twitter_expire: expire_end');
+ Logger::notice('twitter_expire: expire_end');
}
function twitter_prepare_body(App $a, array &$b)
if ($b["preview"]) {
$max_char = 280;
$item = $b["item"];
- $item["plink"] = $a->getBaseURL() . "/display/" . $a->user["nickname"] . "/" . $item["parent"];
+ $item["plink"] = $a->getBaseURL() . "/display/" . $item["guid"];
$condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
$orig_post = Item::selectFirst(['author-link'], $condition);
$has_picture = false;
require_once 'mod/item.php';
- require_once 'include/items.php';
require_once 'mod/share.php';
$connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
- $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"];
+ $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true];
$first_time = ($lastid == "");
try {
$items = $connection->get('statuses/user_timeline', $parameters);
} catch (TwitterOAuthException $e) {
- Logger::log('Error fetching timeline for user ' . $uid . ': ' . $e->getMessage());
+ Logger::notice('Error fetching timeline', ['user' => $uid, 'message' => $e->getMessage()]);
return;
}
if (!is_array($items)) {
- Logger::log('No items for user ' . $uid, Logger::INFO);
+ Logger::notice('No items', ['user' => $uid]);
return;
}
Logger::log('Last ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
}
-function twitter_queue_hook(App $a)
-{
- $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
- DBA::escape(Protocol::TWITTER)
- );
- if (!DBA::isResult($qi)) {
- return;
- }
-
- foreach ($qi as $x) {
- if ($x['network'] !== Protocol::TWITTER) {
- continue;
- }
-
- Logger::log('twitter_queue: run');
-
- $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
- WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
- intval($x['cid'])
- );
- if (!DBA::isResult($r)) {
- continue;
- }
-
- $user = $r[0];
-
- $ckey = Config::get('twitter', 'consumerkey');
- $csecret = Config::get('twitter', 'consumersecret');
- $otoken = PConfig::get($user['uid'], 'twitter', 'oauthtoken');
- $osecret = PConfig::get($user['uid'], 'twitter', 'oauthsecret');
-
- $success = false;
-
- if ($ckey && $csecret && $otoken && $osecret) {
- Logger::log('twitter_queue: able to post');
-
- $z = unserialize($x['content']);
-
- $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
- $result = $connection->post($z['url'], $z['post']);
-
- Logger::log('twitter_queue: post result: ' . print_r($result, true), Logger::DEBUG);
-
- if ($result->errors) {
- Logger::log('twitter_queue: Send to Twitter failed: "' . print_r($result->errors, true) . '"');
- } else {
- $success = true;
- Queue::removeItem($x['id']);
- }
- } else {
- Logger::log("twitter_queue: Error getting tokens for user " . $user['uid']);
- }
-
- if (!$success) {
- Logger::log('twitter_queue: delayed');
- Queue::updateTime($x['id']);
- }
- }
-}
-
function twitter_fix_avatar($avatar)
{
$new_avatar = str_replace("_normal.", ".", $avatar);
$url = "https://twitter.com/" . $data->screen_name;
$addr = $data->screen_name . "@twitter.com";
- GContact::update(["url" => $url, "network" => Protocol::TWITTER,
- "photo" => $avatar, "hide" => true,
- "name" => $data->name, "nick" => $data->screen_name,
- "location" => $data->location, "about" => $data->description,
- "addr" => $addr, "generation" => 2]);
-
$fields = ['url' => $url, 'network' => Protocol::TWITTER,
'name' => $data->name, 'nick' => $data->screen_name, 'addr' => $addr,
'location' => $data->location, 'about' => $data->description];
$contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
if (!DBA::isResult($contact) && !$create_user) {
+ Logger::info('User contact not found', ['uid' => $uid, 'twitter-id' => $data->id_str]);
return 0;
}
// create contact record
$fields['uid'] = $uid;
$fields['created'] = DateTimeFormat::utcNow();
- $fields['nurl'] = normalise_link($url);
+ $fields['nurl'] = Strings::normaliseLink($url);
$fields['alias'] = 'twitter::' . $data->id_str;
$fields['poll'] = 'twitter::' . $data->id_str;
$fields['rel'] = Contact::FRIEND;
$tags_arr = [];
foreach ($item->entities->hashtags AS $hashtag) {
- $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . rawurlencode($hashtag->text) . ']' . $hashtag->text . '[/url]';
+ $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . $hashtag->text . ']' . $hashtag->text . '[/url]';
$tags_arr['#' . $hashtag->text] = $url;
$body = str_replace('#' . $hashtag->text, $url, $body);
}
if ($url->url && $url->expanded_url && $url->display_url) {
// Quote tweet, we just remove the quoted tweet URL from the body, the share block will be added later.
- if (isset($item->quoted_status_id_str)
+ if (!empty($item->quoted_status) && isset($item->quoted_status_id_str)
&& substr($url->expanded_url, -strlen($item->quoted_status_id_str)) == $item->quoted_status_id_str ) {
$body = str_replace($url->url, '', $body);
continue;
}
- $expanded_url = Network::finalUrl($url->expanded_url);
+ $expanded_url = $url->expanded_url;
- $oembed_data = OEmbed::fetchURL($expanded_url);
+ $final_url = Network::finalUrl($url->expanded_url);
+
+ $oembed_data = OEmbed::fetchURL($final_url);
if (empty($oembed_data) || empty($oembed_data->type)) {
continue;
} elseif ($oembed_data->type != 'link') {
$body = str_replace($url->url, '[url=' . $expanded_url . ']' . $url->display_url . '[/url]', $body);
} else {
- $img_str = Network::fetchUrl($expanded_url, true, $redirects, 4);
+ $img_str = Network::fetchUrl($final_url, true, 4);
$tempfile = tempnam(get_temppath(), 'cache');
file_put_contents($tempfile, $img_str);
if (substr($mime, 0, 6) == 'image/') {
$type = 'photo';
- $body = str_replace($url->url, '[img]' . $expanded_url . '[/img]', $body);
+ $body = str_replace($url->url, '[img]' . $final_url . '[/img]', $body);
} else {
$type = $oembed_data->type;
$footerurl = $expanded_url;
}
// it seems as if the entities aren't always covering all mentions. So the rest will be checked here
- $tags = get_tags($body);
+ $tags = BBCode::getTags($body);
if (count($tags)) {
foreach ($tags as $tag) {
}
$basetag = str_replace('_', ' ', substr($tag, 1));
- $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
+ $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]';
$body = str_replace($tag, $url, $body);
$tags_arr['#' . $basetag] = $url;
} elseif (strpos($tag, '@') === 0) {
}
switch ($medium->type) {
case 'photo':
- $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
- $postarray['object-type'] = ACTIVITY_OBJ_IMAGE;
+ if (!empty($medium->ext_alt_text)) {
+ Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
+ $media[$medium->url] .= "\n[img=" . $medium->media_url_https .']' . $medium->ext_alt_text . '[/img]';
+ } else {
+ $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
+ }
+
+ $postarray['object-type'] = Activity\ObjectType::IMAGE;
break;
case 'video':
case 'animated_gif':
- $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
- $postarray['object-type'] = ACTIVITY_OBJ_VIDEO;
+ if (!empty($medium->ext_alt_text)) {
+ Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
+ $media[$medium->url] .= "\n[img=" . $medium->media_url_https .']' . $medium->ext_alt_text . '[/img]';
+ } else {
+ $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
+ }
+
+ $postarray['object-type'] = Activity\ObjectType::VIDEO;
if (is_array($medium->video_info->variants)) {
$bitrate = 0;
// We take the video with the highest bitrate
$postarray['thr-parent'] = $parent_item['uri'];
$postarray['parent-uri'] = $parent_item['parent-uri'];
$postarray['parent'] = $parent_item['parent'];
- $postarray['object-type'] = ACTIVITY_OBJ_COMMENT;
+ $postarray['object-type'] = Activity\ObjectType::COMMENT;
} else {
$postarray['thr-parent'] = $postarray['uri'];
$postarray['parent-uri'] = $postarray['uri'];
- $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
+ $postarray['object-type'] = Activity\ObjectType::NOTE;
}
// Is it me?
$create_user = false;
} else {
$postarray['parent-uri'] = $postarray['uri'];
- $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
+ $postarray['object-type'] = Activity\ObjectType::NOTE;
}
if ($contactid == 0) {
$postarray['contact-id'] = $contactid;
- $postarray['verb'] = ACTIVITY_POST;
+ $postarray['verb'] = Activity::POST;
$postarray['author-name'] = $postarray['owner-name'];
$postarray['author-link'] = $postarray['owner-link'];
$postarray['author-avatar'] = $postarray['owner-avatar'];
// When the post contains links then use the correct object type
if (count($post->entities->urls) > 0) {
- $postarray['object-type'] = ACTIVITY_OBJ_BOOKMARK;
+ $postarray['object-type'] = Activity\ObjectType::BOOKMARK;
}
// Search for media links
return [];
}
- $retweet['source'] = $postarray['source'];
- $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['owner-avatar'] = $postarray['owner-avatar'];
+ if (!$noquote) {
+ // Store the original tweet
+ Item::insert($retweet);
+
+ // CHange the other post into a reshare activity
+ $postarray['verb'] = Activity::ANNOUNCE;
+ $postarray['gravity'] = GRAVITY_ACTIVITY;
+ $postarray['object-type'] = Activity\ObjectType::NOTE;
- $postarray = $retweet;
+ $postarray['thr-parent'] = $retweet['uri'];
+ $postarray['parent-uri'] = $retweet['uri'];
+ } else {
+ $retweet['source'] = $postarray['source'];
+ $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['owner-avatar'] = $postarray['owner-avatar'];
+
+ $postarray = $retweet;
+ }
}
if (!empty($post->quoted_status) && !$noquote) {
$quoted = twitter_createpost($a, $uid, $post->quoted_status, $self, false, false, true);
- if (empty($quoted['body'])) {
- return [];
+ if (!empty($quoted['body'])) {
+ $postarray['body'] .= "\n" . share_header(
+ $quoted['author-name'],
+ $quoted['author-link'],
+ $quoted['author-avatar'],
+ "",
+ $quoted['created'],
+ $quoted['plink']
+ );
+
+ $postarray['body'] .= $quoted['body'] . '[/share]';
+ } else {
+ // Quoted post author is blocked/ignored, so we just provide the link to avoid removing quote context.
+ $postarray['body'] .= "\n\nhttps://twitter.com/" . $post->quoted_status->user->screen_name . "/status/" . $post->quoted_status->id_str;
}
-
- $postarray['body'] .= "\n" . share_header(
- $quoted['author-name'],
- $quoted['author-link'],
- $quoted['author-avatar'],
- "",
- $quoted['created'],
- $quoted['plink']
- );
-
- $postarray['body'] .= $quoted['body'] . '[/share]';
}
return $postarray;
$posts = [];
while (!empty($post->in_reply_to_status_id_str)) {
- $parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str];
+ $parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str, "include_ext_alt_text" => true];
try {
$post = $connection->get('statuses/show', $parameters);
}
if (empty($post)) {
- Logger::log("twitter_fetchparentposts: Can't fetch post " . $parameters->id, Logger::DEBUG);
+ Logger::log("twitter_fetchparentposts: Can't fetch post " . $parameters['id'], Logger::DEBUG);
break;
}
if (!empty($posts)) {
foreach ($posts as $post) {
- $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
+ $postarray = twitter_createpost($a, $uid, $post, $self, false, !PConfig::get($uid, 'twitter', 'create_user'), false);
if (empty($postarray['body'])) {
continue;
$application_name = $a->getHostName();
}
- require_once 'include/items.php';
-
$connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
try {
return;
}
- $parameters = ["exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"];
+ $parameters = ["exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true];
//$parameters["count"] = 200;
// Fetching timeline
$lastid = PConfig::get($uid, 'twitter', 'lasthometimelineid');
twitter_fetchparentposts($a, $uid, $post, $connection, $self);
}
- $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
+ $postarray = twitter_createpost($a, $uid, $post, $self, false, !$create_user, false);
if (empty($postarray['body'])) {
continue;
// Fetching user data
// get() may throw TwitterOAuthException, but we will catch it later
$user = $connection->get('account/verify_credentials');
+ if (empty($user) || empty($user->id_str)) {
+ return false;
+ }
PConfig::set($uid, 'twitter', 'own_id', $user->id_str);
function twitter_convert_share(array $attributes, array $author_contact, $content, $is_quote_share)
{
if ($author_contact['network'] == Protocol::TWITTER) {
- $mention = '@' . $author_contact['nickname'];
+ $mention = '@' . $author_contact['nick'];
} else {
$mention = $author_contact['addr'];
}