Skip to content

Commit

Permalink
admin: Add support for comment search by URL (#1000)
Browse files Browse the repository at this point in the history
* admin: Add support for comment search by URL

Fixes #642

Co-authored-by: Jelmer Vernooij <[email protected]>
  • Loading branch information
pkvach and jelmer authored Apr 18, 2024
1 parent d3d8ae5 commit 29236ff
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ New Features

- Add Catalan localisation (`#966`_, welpo)
- Make <code class="language-$lang"> for syntax highlighting (`#998`_, pkvach)
- Add search for comments by URL in the admin interface (`#1000`_, pkvach)
- Add CSS variables for better organization and flexibility (`#1001`_, pkvach)

.. _#966: https://github.com/posativ/isso/pull/966
.. _#998: https://github.com/isso-comments/isso/pull/998
.. _#1000: https://github.com/isso-comments/isso/pull/1000
.. _#966: https://github.com/posativ/isso/pull/966
.. _#998: https://github.com/isso-comments/isso/pull/998
.. _#1001: https://github.com/isso-comments/isso/pull/1001
Expand Down
34 changes: 34 additions & 0 deletions isso/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions isso/db/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
"""
Expand All @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions isso/templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ <h2>Administration</h2>
</span>
</a>
{% endfor %}
<div class="search">
<form action="/admin/" method="get">
<input type="hidden" name="mode" value="0" />
<label>Comment URL
<input type="search" class="search__input" name="comment_url" value="{{comment_url}}" spellcheck="false"
required placeholder="https://example.com/demo/#isso-1" />
</label>
<button type="submit" class="search__button">Search</button>
</form>
</div>
</div>
</div>
<div id="isso-root">
Expand Down
52 changes: 47 additions & 5 deletions isso/views/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from html import escape
from io import BytesIO as StringIO
from os import path as os_path
from urllib.parse import unquote, urlparse
from urllib.parse import unquote, urlparse, urlsplit
from xml.etree import ElementTree as ET

from itsdangerous import SignatureExpired, BadSignature
Expand Down Expand Up @@ -84,6 +84,38 @@ def dec(self, env, req, *args, **kwargs):
return dec


def get_comment_id_from_url(comment_url):
"""
Extracts the comment ID from a given comment URL.
Args:
comment_url (str): The URL of the comment.
Returns:
int or None: The extracted comment ID if successful, None otherwise.
"""
try:
# Parse the comment URL to extract the comment ID from the fragment
parsed_url = urlsplit(comment_url)
except ValueError:
# Handle malformed URL
return None

fragment = parsed_url.fragment
if not fragment or '-' not in fragment:
# Handle missing fragment or fragment without hyphen
return None

last_element = fragment.split('-')[-1]
try:
comment_id = int(last_element)
except ValueError:
# Handle invalid comment ID
return None

return comment_id


class API(object):

FIELDS = set(['id', 'parent', 'text', 'author', 'website',
Expand Down Expand Up @@ -1376,6 +1408,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
Expand All @@ -1396,10 +1430,17 @@ 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:
comment_id = get_comment_id_from_url(comment_url)
comments = self.comments.fetchall(comment_id=comment_id, limit=1) if comment_id else []
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'])
Expand All @@ -1411,6 +1452,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
Expand Down

0 comments on commit 29236ff

Please sign in to comment.