From a6965c49d642d57d0f8c7190b2db1877500c1854 Mon Sep 17 00:00:00 2001 From: Pavel Kvach Date: Sun, 17 Mar 2024 00:59:47 +0200 Subject: [PATCH] admin: Add support for comment search by URL Closes https://github.com/isso-comments/isso/issues/642 --- isso/css/admin.css | 34 ++++++++++++++++++++++++++++++++++ isso/db/comments.py | 12 +++++++++--- isso/templates/admin.html | 10 ++++++++++ isso/views/comments.py | 26 ++++++++++++++++++++++---- 4 files changed, 75 insertions(+), 7 deletions(-) diff --git a/isso/css/admin.css b/isso/css/admin.css index 3f694c34f..e834545a8 100644 --- a/isso/css/admin.css +++ b/isso/css/admin.css @@ -126,6 +126,40 @@ a { float: left; margin-left: 2em; } + +.search { + float: right; +} + +.search .search__input { + margin: 0 .3em; + padding: .3em 10px; + width: 18em; + border-radius: 3px; + background-color: #fff; + border: 1px solid rgba(0, 0, 0, 0.2); + box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); +} +.search .search__input:focus { + outline: 2px solid #3584e4; +} + +.search .search__button { + margin: 0 .3em; + padding: .3em; + border-radius: 2px; + border: 1px solid #ccc; + background-color: #ddd; + cursor: pointer; + box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); +} +.search .search__button:hover { + background-color: #ccc; +} +.search .search__button:active { + background-color: #bbb; +} + .editable { border: 1px solid #aaa; border-radius: 5px; diff --git a/isso/db/comments.py b/isso/db/comments.py index 603799243..9a0edddbb 100644 --- a/isso/db/comments.py +++ b/isso/db/comments.py @@ -167,7 +167,7 @@ def count_modes(self): return dict(comment_count) def fetchall(self, mode=5, after=0, parent='any', order_by='id', - limit=100, page=0, asc=1): + limit=100, page=0, asc=1, comment_id=None): """ Return comments for admin with :param:`mode`. """ @@ -182,8 +182,14 @@ def fetchall(self, mode=5, after=0, parent='any', order_by='id', sql = ['SELECT ' + sql_comments_fields + ', ' + sql_threads_fields + ' ' 'FROM comments INNER JOIN threads ' 'ON comments.tid=threads.id ' - 'WHERE comments.mode = ? '] - sql_args = [mode] + 'WHERE '] + + if comment_id: + sql.append('comments.id = ? ') + sql_args = [comment_id] + else: + sql.append('comments.mode = ? ') + sql_args = [mode] if parent != 'any': if parent is None: diff --git a/isso/templates/admin.html b/isso/templates/admin.html index 908947b2a..a6220301b 100644 --- a/isso/templates/admin.html +++ b/isso/templates/admin.html @@ -71,6 +71,16 @@

Administration

{% endfor %} +
diff --git a/isso/views/comments.py b/isso/views/comments.py index 3e31f2293..ab9709d13 100644 --- a/isso/views/comments.py +++ b/isso/views/comments.py @@ -1376,6 +1376,8 @@ def login(self, env, req): Comment ordering @apiQuery {Number{0,1}} [asc=0] Ascending + @apiQuery {String} comment_url + Search comment by URL @apiExample {curl} Listing of published comments: curl 'https://comments.example.com/admin/?mode=1&page=0&order_by=modified&asc=1' -b cookie.txt @@ -1396,10 +1398,25 @@ def admin(self, env, req): order_by = req.args.get('order_by', 'created') asc = int(req.args.get('asc', 0)) mode = int(req.args.get('mode', 2)) - comments = self.comments.fetchall(mode=mode, page=page, - limit=page_size, - order_by=order_by, - asc=asc) + comment_url = req.args.get('comment_url', '') + + # Search for a specific comment by URL + if comment_url: + try: + # Parse the comment URL to extract the comment ID from the fragment + parsed_url = urlparse(comment_url) + fragment = parsed_url.fragment + comment_id = int(fragment.split('-')[-1]) + + comments = self.comments.fetchall(comment_id=comment_id, limit=1) + except Exception: + # If the URL is malformed or the comment does not exist + comments = [] + else: + comments = self.comments.fetchall(mode=mode, page=page, + limit=page_size, + order_by=order_by, + asc=asc) comments_enriched = [] for comment in list(comments): comment['hash'] = self.isso.sign(comment['id']) @@ -1411,6 +1428,7 @@ def admin(self, env, req): conf=self.conf, max_page=max_page, counts=comment_mode_count, order_by=order_by, asc=asc, + comment_url=comment_url, isso_host_script=isso_host_script) """ @api {get} /latest latest