Properly tag 404s from redirector with xkeys
authorMagnus Hagander <magnus@hagander.net>
Wed, 5 Oct 2022 15:19:26 +0000 (17:19 +0200)
committerMagnus Hagander <magnus@hagander.net>
Wed, 5 Oct 2022 15:19:26 +0000 (17:19 +0200)
If the 404s are beceause an item is hidden, we need them to be tagged
with the appropriate xkeys so we can automatically unhide them.

redirector/redirector.py

index d3bb83f5c6fa5c3ee52d4a22d01c394ac3a848b3..666d57db9e523abff11e39c478900c7010c6b57d 100755 (executable)
@@ -56,7 +56,7 @@ def application(environ, start_response):
         # bother with any connection pooling.
         conn = psycopg2.connect(connstr)
         c = conn.cursor()
-        c.execute("SELECT link, feed FROM posts WHERE id=%(id)s AND NOT hidden", {
+        c.execute("SELECT link, feed, hidden FROM posts WHERE id=%(id)s", {
             'id': id
         })
         r = c.fetchall()
@@ -66,19 +66,30 @@ def application(environ, start_response):
         if len(r) != 1:
             raise Make404()
 
-        # We have a link, return a redirect to it
-        start_response('301 Moved Permanently', [
-            ('Content-type', 'text/html'),
-            ('Location', r[0][0]),
-            ('X-Planet', str(id)),
-            ('X-Planet-Feed', str(r[0][1])),
-            ('xkey', 'post_{}  feed_{}'.format(id, r[0][1])),
-        ])
-        return [
-            b"<html>\n<head>\n<title>postgr.es</title>\n</head>\n<body>\n",
-            b"<a href=\"%s\">moved here</a>\n" % r[0][0].encode('utf8'),
-            b"</body>\n</html>\n"
-        ]
+        if r[0][2]:
+            # Entry is hidden. We want a 404 that is tagged with the feed, so we can xkey
+            # purge it properly!
+            start_response('404 Not Found', [
+                ('Content-type', 'text/plain'),
+                ('X-Planet', str(id)),
+                ('X-Planet-Feed', str(r[0][1])),
+                ('xkey', 'post_{}  feed_{}'.format(id, r[0][1])),
+            ])
+            return [b"Link not found\n"]
+        else:
+            # We have a link, return a redirect to it
+            start_response('301 Moved Permanently', [
+                ('Content-type', 'text/html'),
+                ('Location', r[0][0]),
+                ('X-Planet', str(id)),
+                ('X-Planet-Feed', str(r[0][1])),
+                ('xkey', 'post_{}  feed_{}'.format(id, r[0][1])),
+            ])
+            return [
+                b"<html>\n<head>\n<title>postgr.es</title>\n</head>\n<body>\n",
+                b"<a href=\"%s\">moved here</a>\n" % r[0][0].encode('utf8'),
+                b"</body>\n</html>\n"
+            ]
     except Make404:
         start_response('404 Not Found', [
             ('Content-type', 'text/plain'),