Skip to content

Commit dce4a3c

Browse files
committed
markdown: Remove most of Twitter integration.
Twitter removed their v1 API. We take care to keep the existing cached results around for now, and to not poison that cache, since we might be able replace this with something that can still use the existing cache.
1 parent ecc1433 commit dce4a3c

File tree

3 files changed

+12
-546
lines changed

3 files changed

+12
-546
lines changed

zerver/lib/markdown/__init__.py

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
from zerver.lib.subdomains import is_static_or_current_realm_url
6666
from zerver.lib.tex import render_tex
6767
from zerver.lib.thumbnail import user_uploads_or_external
68-
from zerver.lib.timeout import TimeoutExpiredError, timeout
68+
from zerver.lib.timeout import timeout
6969
from zerver.lib.timezone import common_timezones
7070
from zerver.lib.types import LinkifierDict
7171
from zerver.lib.url_encoding import encode_stream, hash_util_encode
@@ -420,68 +420,13 @@ def has_blockquote_ancestor(element_pair: Optional[ElementPair]) -> bool:
420420

421421
@cache_with_key(lambda tweet_id: tweet_id, cache_name="database")
422422
def fetch_tweet_data(tweet_id: str) -> Optional[Dict[str, Any]]:
423-
if settings.TEST_SUITE:
424-
from . import testing_mocks
425-
426-
res = testing_mocks.twitter(tweet_id)
427-
else:
428-
creds = {
429-
"consumer_key": settings.TWITTER_CONSUMER_KEY,
430-
"consumer_secret": settings.TWITTER_CONSUMER_SECRET,
431-
"access_token_key": settings.TWITTER_ACCESS_TOKEN_KEY,
432-
"access_token_secret": settings.TWITTER_ACCESS_TOKEN_SECRET,
433-
}
434-
if not all(creds.values()):
435-
return None
436-
437-
# We lazily import twitter here because its import process is
438-
# surprisingly slow, and doing so has a significant impact on
439-
# the startup performance of `manage.py` commands.
440-
import twitter
441-
442-
api = twitter.Api(tweet_mode="extended", **creds)
443-
444-
try:
445-
# Sometimes Twitter hangs on responses. Timing out here
446-
# will cause the Tweet to go through as-is with no inline
447-
# preview, rather than having the message be rejected
448-
# entirely. This timeout needs to be less than our overall
449-
# formatting timeout.
450-
tweet = timeout(3, lambda: api.GetStatus(tweet_id))
451-
res = tweet.AsDict()
452-
except TimeoutExpiredError:
453-
# We'd like to try again later and not cache the bad result,
454-
# so we need to re-raise the exception (just as though
455-
# we were being rate-limited)
456-
raise
457-
except twitter.TwitterError as e:
458-
t = e.args[0]
459-
if len(t) == 1 and ("code" in t[0]):
460-
# https://developer.twitter.com/en/docs/basics/response-codes
461-
code = t[0]["code"]
462-
if code in [34, 144, 421, 422]:
463-
# All these "correspond with HTTP 404," and mean
464-
# that the message doesn't exist; return None so
465-
# that we will cache the error.
466-
return None
467-
elif code in [63, 179]:
468-
# 63 is that the account is suspended, 179 is that
469-
# it is now locked; cache the None.
470-
return None
471-
elif code in [88, 130, 131]:
472-
# Code 88 means that we were rate-limited, 130
473-
# means Twitter is having capacity issues, and 131
474-
# is other 400-equivalent; in these cases, raise
475-
# the error so we don't cache None and will try
476-
# again later.
477-
raise
478-
# It's not clear what to do in cases of other errors,
479-
# but for now it seems reasonable to log at error
480-
# level (so that we get notified), but then cache the
481-
# failure to proceed with our usual work
482-
markdown_logger.exception("Unknown error fetching tweet data", stack_info=True)
483-
return None
484-
return res
423+
# Twitter removed support for the v1 API that this integration
424+
# used. Given that, there's no point wasting time trying to make
425+
# network requests to Twitter. But we leave this function, because
426+
# existing cached renderings for Tweets is useful. We throw an
427+
# exception rather than returning `None` to avoid caching that the
428+
# link doesn't exist.
429+
raise NotImplementedError("Twitter desupported their v1 API")
485430

486431

487432
class OpenGraphSession(OutgoingSession):
@@ -1049,6 +994,8 @@ def twitter_link(self, url: str) -> Optional[Element]:
1049994
img.set("src", media_url)
1050995

1051996
return tweet
997+
except NotImplementedError:
998+
return None
1052999
except Exception:
10531000
# We put this in its own try-except because it requires external
10541001
# connectivity. If Twitter flakes out, we don't want to not-render

zerver/lib/markdown/testing_mocks.py

Lines changed: 0 additions & 233 deletions
This file was deleted.

0 commit comments

Comments
 (0)