[TwitterBridge] Check if Notice::$lat and Notice::$lon are defined

This commit is contained in:
Alexei Sorokin 2020-08-10 10:35:45 +03:00 committed by Diogo Peralta Cordeiro
parent 01093e3583
commit ed08b46c0b

View File

@ -1,25 +1,25 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/* /*
* StatusNet - the distributed open-source microblogging tool * @copyright 2008-2011 StatusNet, Inc.
* Copyright (C) 2008-2011 StatusNet, Inc. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { defined('GNUSOCIAL') || die();
exit(1);
}
define('TWITTER_SERVICE', 1); // Twitter is foreign_service ID 1 define('TWITTER_SERVICE', 1); // Twitter is foreign_service ID 1
@ -54,8 +54,10 @@ function add_twitter_user($twitter_id, $screen_name)
common_log(LOG_WARNING, "Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name."); common_log(LOG_WARNING, "Twitter bridge - failed to add new Twitter user: $twitter_id - $screen_name.");
common_log_db_error($fuser, 'INSERT', __FILE__); common_log_db_error($fuser, 'INSERT', __FILE__);
} else { } else {
common_log(LOG_INFO, common_log(
"Twitter bridge - Added new Twitter user: $screen_name ($twitter_id)."); LOG_INFO,
"Twitter bridge - Added new Twitter user: {$screen_name} ({$twitter_id})."
);
} }
return $result; return $result;
@ -73,11 +75,13 @@ function save_twitter_user($twitter_id, $screen_name)
if ($fuser->nickname != $screen_name) { if ($fuser->nickname != $screen_name) {
$oldname = $fuser->nickname; $oldname = $fuser->nickname;
$fuser->delete(); $fuser->delete();
common_log(LOG_INFO, sprintf('Twitter bridge - Updated nickname (and URI) ' . common_log(LOG_INFO, sprintf(
'for Twitter user %1$d - %2$s, was %3$s.', 'Twitter bridge - Updated nickname (and URI) '
$fuser->id, . 'for Twitter user %1$d - %2$s, was %3$s.',
$screen_name, $fuser->id,
$oldname)); $screen_name,
$oldname
));
} }
} catch (NoResultException $e) { } catch (NoResultException $e) {
// No old users exist for this id // No old users exist for this id
@ -104,7 +108,8 @@ function save_twitter_user($twitter_id, $screen_name)
return add_twitter_user($twitter_id, $screen_name); return add_twitter_user($twitter_id, $screen_name);
} }
function is_twitter_bound($notice, $flink) { function is_twitter_bound($notice, $flink)
{
// Don't send activity activities (at least for now) // Don't send activity activities (at least for now)
if ($notice->object_type == ActivityObject::ACTIVITY) { if ($notice->object_type == ActivityObject::ACTIVITY) {
@ -114,11 +119,15 @@ function is_twitter_bound($notice, $flink) {
$allowedVerbs = array(ActivityVerb::POST); $allowedVerbs = array(ActivityVerb::POST);
// Default behavior: always send repeats // Default behavior: always send repeats
if (empty($flink)) if (empty($flink)) {
array_push($allowedVerbs, ActivityVerb::SHARE); array_push($allowedVerbs, ActivityVerb::SHARE);
}
// Otherwise, check to see if repeats are allowed // Otherwise, check to see if repeats are allowed
else if (($flink->noticesync & FOREIGN_NOTICE_SEND_REPEAT) == FOREIGN_NOTICE_SEND_REPEAT) elseif (
($flink->noticesync & FOREIGN_NOTICE_SEND_REPEAT) === FOREIGN_NOTICE_SEND_REPEAT
) {
array_push($allowedVerbs, ActivityVerb::SHARE); array_push($allowedVerbs, ActivityVerb::SHARE);
}
// Don't send things that aren't posts or repeats (at least for now) // Don't send things that aren't posts or repeats (at least for now)
if (!in_array($notice->verb, $allowedVerbs)) { if (!in_array($notice->verb, $allowedVerbs)) {
@ -126,13 +135,18 @@ function is_twitter_bound($notice, $flink) {
} }
// Check to see if notice should go to Twitter // Check to see if notice should go to Twitter
if (!empty($flink) && (($flink->noticesync & FOREIGN_NOTICE_SEND) == FOREIGN_NOTICE_SEND)) { if (
(($flink->noticesync & FOREIGN_NOTICE_SEND) === FOREIGN_NOTICE_SEND)
&& !empty($flink)
) {
// If it's not a Twitter-style reply, or if the user WANTS to send replies, // If it's not a Twitter-style reply, or if the user WANTS to send replies,
// or if it's in reply to a twitter notice // or if it's in reply to a twitter notice
if ( (($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) == FOREIGN_NOTICE_SEND_REPLY) || if (
is_twitter_notice($notice->reply_to) || is_twitter_notice($notice->repeat_of) || (($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) === FOREIGN_NOTICE_SEND_REPLY)
(empty($notice->reply_to) && !preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content)) ){ || is_twitter_notice($notice->reply_to)
|| is_twitter_notice($notice->repeat_of)
|| (empty($notice->reply_to) && !preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content))
) {
return true; return true;
} }
} }
@ -272,8 +286,10 @@ function twitter_status_id($notice)
function twitter_update_params($notice) function twitter_update_params($notice)
{ {
$params = array(); $params = array();
if ($notice->lat || $notice->lon) { if (isset($notice->lat)) {
$params['lat'] = $notice->lat; $params['lat'] = $notice->lat;
}
if (isset($notice->lon)) {
$params['long'] = $notice->lon; $params['long'] = $notice->lon;
} }
if (!empty($notice->reply_to) && is_twitter_notice($notice->reply_to)) { if (!empty($notice->reply_to) && is_twitter_notice($notice->reply_to)) {
@ -283,7 +299,8 @@ function twitter_update_params($notice)
return $params; return $params;
} }
function broadcast_oauth($notice, Foreign_link $flink) { function broadcast_oauth($notice, Foreign_link $flink)
{
try { try {
$user = $flink->getUser(); $user = $flink->getUser();
} catch (ServerException $e) { } catch (ServerException $e) {
@ -309,11 +326,13 @@ function broadcast_oauth($notice, Foreign_link $flink) {
if (empty($status)) { if (empty($status)) {
// This could represent a failure posting, // This could represent a failure posting,
// or the Twitter API might just be behaving flakey. // or the Twitter API might just be behaving flakey.
$errmsg = sprintf('Twitter bridge - No data returned by Twitter API when ' . $errmsg = sprintf(
'trying to post notice %d for User %s (user id %d).', 'Twitter bridge - No data returned by Twitter API when '
$notice->id, . 'trying to post notice %d for User %s (user id %d).',
$user->nickname, $notice->id,
$user->id); $user->nickname,
$user->id
);
common_log(LOG_WARNING, $errmsg); common_log(LOG_WARNING, $errmsg);
@ -321,11 +340,13 @@ function broadcast_oauth($notice, Foreign_link $flink) {
} }
// Notice crossed the great divide // Notice crossed the great divide
$msg = sprintf('Twitter bridge - posted notice %d to Twitter using ' . $msg = sprintf(
'OAuth for User %s (user id %d).', 'Twitter bridge - posted notice %d to Twitter using '
$notice->id, . 'OAuth for User %s (user id %d).',
$user->nickname, $notice->id,
$user->id); $user->nickname,
$user->id
);
common_log(LOG_INFO, $msg); common_log(LOG_INFO, $msg);
@ -337,53 +358,47 @@ function process_error($e, $flink, $notice)
$user = $flink->getUser(); $user = $flink->getUser();
$code = $e->getCode(); $code = $e->getCode();
$logmsg = sprintf('Twitter bridge - %d posting notice %d for ' . $logmsg = sprintf(
'User %s (user id: %d): %s.', 'Twitter bridge - %d posting notice %d for User %s (user id: %d): %s.',
$code, $code,
$notice->id, $notice->id,
$user->nickname, $user->nickname,
$user->id, $user->id,
$e->getMessage()); $e->getMessage()
);
common_log(LOG_WARNING, $logmsg); common_log(LOG_WARNING, $logmsg);
// http://dev.twitter.com/pages/responses_errors // http://dev.twitter.com/pages/responses_errors
switch($code) { switch ($code) {
case 400: case 400:
// Probably invalid data (bad Unicode chars or coords) that // Probably invalid data (bad Unicode chars or coords) that
// cannot be resolved by just sending again. // cannot be resolved by just sending again.
// //
// It could also be rate limiting, but retrying immediately // It could also be rate limiting, but retrying immediately
// won't help much with that, so we'll discard for now. // won't help much with that, so we'll discard for now.
// If a facility for retrying things later comes up in future, // If a facility for retrying things later comes up in future,
// we can detect the rate-limiting headers and use that. // we can detect the rate-limiting headers and use that.
// //
// Discard the message permanently. // Discard the message permanently.
return true; return true;
break; case 401:
case 401: // Probably a revoked or otherwise bad access token - nuke!
// Probably a revoked or otherwise bad access token - nuke! remove_twitter_link($flink);
remove_twitter_link($flink); return true;
return true; case 403:
break; // User has exceeder her rate limit -- toss the notice
case 403: return true;
// User has exceeder her rate limit -- toss the notice case 404:
return true; // Resource not found. Shouldn't happen much on posting,
break; // but just in case!
case 404: //
// Resource not found. Shouldn't happen much on posting, // Consider it a matter for tossing the notice.
// but just in case! return true;
// default:
// Consider it a matter for tossing the notice. // For every other case, it's probably some flakiness so try
return true; // sending the notice again later (requeue).
break; return false;
default:
// For every other case, it's probably some flakiness so try
// sending the notice again later (requeue).
return false;
break;
} }
} }
@ -466,16 +481,18 @@ function mail_twitter_bridge_removed($user)
// TRANS: Mail body after forwarding notices to Twitter has stopped working. // TRANS: Mail body after forwarding notices to Twitter has stopped working.
// TRANS: %1$ is the name of the user the mail is sent to, %2$s is a URL to the // TRANS: %1$ is the name of the user the mail is sent to, %2$s is a URL to the
// TRANS: Twitter settings, %3$s is the StatusNet sitename. // TRANS: Twitter settings, %3$s is the StatusNet sitename.
$body = sprintf(_m('Hi, %1$s. We\'re sorry to inform you that your ' . $body = sprintf(
'link to Twitter has been disabled. We no longer seem to have ' . _m('Hi, %1$s. We\'re sorry to inform you that your '
'permission to update your Twitter status. Did you maybe revoke ' . . 'link to Twitter has been disabled. We no longer seem to have '
'%3$s\'s access?' . "\n\n" . . 'permission to update your Twitter status. Did you maybe revoke '
'You can re-enable your Twitter bridge by visiting your ' . . '%3$s\'s access?' . "\n\n"
"Twitter settings page:\n\n\t%2\$s\n\n" . . 'You can re-enable your Twitter bridge by visiting your '
"Regards,\n%3\$s"), . "Twitter settings page:\n\n\t%2\$s\n\n"
. "Regards,\n%3\$s"),
$profile->getBestName(), $profile->getBestName(),
common_local_url('twittersettings'), common_local_url('twittersettings'),
common_config('site', 'name')); common_config('site', 'name')
);
common_switch_locale(); common_switch_locale();
return mail_to_user($user, $subject, $body); return mail_to_user($user, $subject, $body);