Skip to content

Commit

Permalink
admin: Add support for comment search by URL
Browse files Browse the repository at this point in the history
  • Loading branch information
pkvach committed Mar 17, 2024
1 parent 50642b1 commit a6965c4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
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:
# 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

0 comments on commit a6965c4

Please sign in to comment.