Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin: Add support for comment search by URL #1000

Merged
merged 4 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ 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)

.. _#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

Breaking Changes
^^^^^^^^^^^^^^^^
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
26 changes: 22 additions & 4 deletions isso/views/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
pkvach marked this conversation as resolved.
Show resolved Hide resolved
# 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'])
Expand All @@ -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
Expand Down
Loading