From f7bf9868689403c457b4f733db8eb98193d4ff42 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Fri, 20 Dec 2024 09:52:20 -0800 Subject: [PATCH] redirect https://bsky.brid.gy/hashtag/TAG to https://bsky.app/search?q=#TAG for #1634, thanks @Tamschi! --- atproto.py | 20 ++++++++++++++++---- tests/test_atproto.py | 13 +++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/atproto.py b/atproto.py index 2b7d650a..d7a56bed 100644 --- a/atproto.py +++ b/atproto.py @@ -23,7 +23,7 @@ ) import brevity import dag_json -from flask import abort, request +from flask import abort, redirect, request from google.cloud import dns from google.cloud.dns.resource_record_set import ResourceRecordSet from google.cloud import ndb @@ -36,9 +36,11 @@ from oauth_dropins.webutil import util from oauth_dropins.webutil.appengine_config import ndb_client from oauth_dropins.webutil.appengine_info import DEBUG -from oauth_dropins.webutil.flask_util import cloud_tasks_only, get_required_param +from oauth_dropins.webutil import flask_util +from oauth_dropins.webutil.flask_util import get_required_param from oauth_dropins.webutil.models import StringIdModel from oauth_dropins.webutil.util import add, json_dumps, json_loads +from werkzeug.exceptions import NotFound import common from common import ( @@ -50,7 +52,7 @@ SUPERDOMAIN, USER_AGENT, ) -import flask_app +from flask_app import app import ids from models import Follower, Object, PROTOCOLS, Target, User from protocol import Protocol @@ -1054,7 +1056,7 @@ def send_chat(*, msg, from_repo, to_did): return True -@cloud_tasks_only(log=False) +@flask_util.cloud_tasks_only(log=False) def poll_chat_task(): """Polls for incoming chat messages for our protocol bot users. @@ -1106,3 +1108,13 @@ def poll_chat_task(): # done! bot.put() return 'OK' + + +@app.get(f'/hashtag/') +@flask_util.headers(common.CACHE_CONTROL) +def hashtag_redirect(hashtag): + if (util.domain_from_link(request.host_url) == + f'{ATProto.ABBREV}{common.SUPERDOMAIN}'): + return redirect(f'https://bsky.app/search?q=%23{hashtag}') + + raise NotFound() diff --git a/tests/test_atproto.py b/tests/test_atproto.py index 87fbcb68..3d38f603 100644 --- a/tests/test_atproto.py +++ b/tests/test_atproto.py @@ -2697,3 +2697,16 @@ def test_poll_atproto_chat_messages(self, mock_get, mock_create_task): received_at='1900-04-04T00:04:04Z') self.assertEqual('dunn', fa.key.get().atproto_last_chat_log_cursor) + + def test_hashtag_redirect(self): + resp = self.get('/hashtag/foo', base_url='https://bsky.brid.gy') + self.assert_equals(302, resp.status_code) + self.assert_equals('https://bsky.app/search?q=%23foo', + resp.headers['Location']) + + # only on bsky subdomain + resp = self.get('/hashtag/foo') + self.assert_equals(404, resp.status_code) + + resp = self.get('/hashtag/foo', base_url='https://web.brid.gy') + self.assert_equals(404, resp.status_code)