Added logging to twitter addon #907

Merged
annando merged 1 commit from twitter-logging into develop 2019-10-31 19:50:40 +01:00
Showing only changes of commit d53d690f0d - Show all commits

View file

@ -109,7 +109,7 @@ function twitter_install()
Hook::register('expire' , __FILE__, 'twitter_expire'); Hook::register('expire' , __FILE__, 'twitter_expire');
Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body'); Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body');
Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification'); Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification');
Logger::log("installed twitter"); Logger::info("installed twitter");
} }
function twitter_uninstall() function twitter_uninstall()
@ -154,7 +154,7 @@ function twitter_check_item_notification(App $a, array &$notification_data)
function twitter_follow(App $a, array &$contact) 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")) { if (!strstr($contact["url"], "://twitter.com") && !strstr($contact["url"], "@twitter.com")) {
return; return;
@ -239,7 +239,7 @@ function twitter_settings_post(App $a)
} else { } else {
if (isset($_POST['twitter-pin'])) { if (isset($_POST['twitter-pin'])) {
// if the user supplied us with a PIN from Twitter, let the magic of OAuth happen // 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'); $ckey = Config::get('twitter', 'consumerkey');
$csecret = Config::get('twitter', 'consumersecret'); $csecret = Config::get('twitter', 'consumersecret');
// the token and secret for which the PIN was generated were hidden in the settings // the token and secret for which the PIN was generated were hidden in the settings
@ -428,7 +428,7 @@ function twitter_hook_fork(App $a, array &$b)
if (PConfig::get($post['uid'], 'twitter', 'import')) { if (PConfig::get($post['uid'], 'twitter', 'import')) {
// Don't fork if it isn't a reply to a twitter post // 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])) { if (($post['parent'] != $post['id']) && !Item::exists(['id' => $post['parent'], 'network' => Protocol::TWITTER])) {
Logger::log('No twitter parent found for item ' . $post['id']); Logger::notice('No twitter parent found', ['item' => $post['id']]);
$b['execute'] = false; $b['execute'] = false;
return; return;
} }
@ -623,7 +623,7 @@ function twitter_post_hook(App $a, array &$b)
$b['body'] = twitter_update_mentions($b['body']); $b['body'] = twitter_update_mentions($b['body']);
$msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 8); $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 8);
Logger::info('Got plaintext', $msgarr); Logger::info('Got plaintext', ['id' => $b['id'], 'message' => $msgarr]);
$msg = $msgarr["text"]; $msg = $msgarr["text"];
if (($msg == "") && isset($msgarr["title"])) { if (($msg == "") && isset($msgarr["title"])) {
@ -640,6 +640,7 @@ function twitter_post_hook(App $a, array &$b)
} }
if (empty($msg)) { if (empty($msg)) {
Logger::info('Empty message', ['id' => $b['id']]);
return; return;
} }
@ -647,6 +648,7 @@ function twitter_post_hook(App $a, array &$b)
$post = []; $post = [];
if (!empty($msgarr['images'])) { if (!empty($msgarr['images'])) {
Logger::info('Got images', ['id' => $b['id'], 'images' => $msgarr['images']]);
try { try {
$media_ids = []; $media_ids = [];
foreach ($msgarr['images'] as $image) { foreach ($msgarr['images'] as $image) {
@ -659,6 +661,7 @@ function twitter_post_hook(App $a, array &$b)
$tempfile = tempnam(get_temppath(), 'cache'); $tempfile = tempnam(get_temppath(), 'cache');
file_put_contents($tempfile, $img_str); file_put_contents($tempfile, $img_str);
Logger::info('Uploading', ['id' => $b['id'], 'image' => $image['url']]);
$media = $connection->upload('media/upload', ['media' => $tempfile]); $media = $connection->upload('media/upload', ['media' => $tempfile]);
unlink($tempfile); unlink($tempfile);
@ -670,10 +673,10 @@ function twitter_post_hook(App $a, array &$b)
$data = ['media_id' => $media->media_id_string, $data = ['media_id' => $media->media_id_string,
'alt_text' => ['text' => substr($image['description'], 0, 420)]]; 'alt_text' => ['text' => substr($image['description'], 0, 420)]];
$ret = $cb->media_metadata_create($data); $ret = $cb->media_metadata_create($data);
Logger::info('Metadata create', ['data' => $data, 'return' => json_encode($ret)]); Logger::info('Metadata create', ['id' => $b['id'], 'data' => $data, 'return' => json_encode($ret)]);
} }
} else { } else {
throw new Exception('Failed upload of ' . $image['url']); throw new Exception('Failed upload', ['id' => $b['id'], 'image' => $image['url']]);
} }
} }
$post['media_ids'] = implode(',', $media_ids); $post['media_ids'] = implode(',', $media_ids);
@ -681,7 +684,7 @@ function twitter_post_hook(App $a, array &$b)
unset($post['media_ids']); unset($post['media_ids']);
} }
} catch (Exception $e) { } catch (Exception $e) {
Logger::log('Exception when trying to send to Twitter: ' . $e->getMessage()); Logger::info('Exception when trying to send to Twitter', ['id' => $b['id'], 'message' => $e->getMessage()]);
} }
} }
@ -693,17 +696,17 @@ function twitter_post_hook(App $a, array &$b)
$url = 'statuses/update'; $url = 'statuses/update';
$result = $connection->post($url, $post); $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)) { if (!empty($result->source)) {
Config::set("twitter", "application_name", strip_tags($result->source)); Config::set("twitter", "application_name", strip_tags($result->source));
} }
if (!empty($result->errors)) { if (!empty($result->errors)) {
Logger::log('Send to Twitter failed: "' . print_r($result->errors, true) . '"'); Logger::info('Send to Twitter failed', ['id' => $b['id'], 'error' => $result->errors]);
Worker::defer(); Worker::defer();
} elseif ($iscomment) { } 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']]); Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
} }
} }
@ -742,16 +745,16 @@ function twitter_cron(App $a)
if ($last) { if ($last) {
$next = $last + ($poll_interval * 60); $next = $last + ($poll_interval * 60);
if ($next > time()) { if ($next > time()) {
Logger::log('twitter: poll intervall not reached'); Logger::notice('twitter: poll intervall not reached');
return; 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'"); $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1'");
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
foreach ($r as $rr) { foreach ($r as $rr) {
Logger::log('twitter: fetching for user ' . $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']); Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], "addon/twitter/twitter_sync.php", 1, (int) $rr['uid']);
} }
} }
@ -769,12 +772,12 @@ function twitter_cron(App $a)
if ($abandon_days != 0) { if ($abandon_days != 0) {
$user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit); $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
if (!DBA::isResult($user)) { 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; continue;
} }
} }
Logger::log('twitter: importing timeline from user ' . $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']); Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], "addon/twitter/twitter_sync.php", 2, (int) $rr['uid']);
/* /*
// To-Do // To-Do
@ -793,7 +796,7 @@ function twitter_cron(App $a)
} }
} }
Logger::log('twitter: cron_end'); Logger::notice('twitter: cron_end');
Config::set('twitter', 'last_poll', time()); Config::set('twitter', 'last_poll', time());
} }
@ -812,17 +815,17 @@ function twitter_expire(App $a)
} }
DBA::close($r); DBA::close($r);
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()"); $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
foreach ($r as $rr) { 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); 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) function twitter_prepare_body(App $a, array &$b)
@ -956,12 +959,12 @@ function twitter_fetchtimeline(App $a, $uid)
try { try {
$items = $connection->get('statuses/user_timeline', $parameters); $items = $connection->get('statuses/user_timeline', $parameters);
} catch (TwitterOAuthException $e) { } catch (TwitterOAuthException $e) {
Logger::log('Error fetching timeline for user ' . $uid . ': ' . $e->getMessage()); Logger::notice('Error fetching timeline', ['user' => $uid, 'message' => $e->getMessage()]);
return; return;
} }
if (!is_array($items)) { if (!is_array($items)) {
Logger::log('No items for user ' . $uid, Logger::INFO); Logger::notice('No items', ['user' => $uid]);
return; return;
} }