feat(FastAPI): add pkgbase comments (new, edit)

In PHP, this was implemented using an /rpc type 'get-comment-form'.
With FastAPI, we've decided to reorganize this into a non-RPC route:
`/pkgbase/{name}/comments/{id}/form`, rendered via the new
`templates/partials/packages/comment_form.html` template.

When the comment_form.html template is provided a `comment` object,
it will produce an edit comment form. Otherwise, it will produce a new
comment form.

A few new FastAPI routes have been introduced:

- GET `/pkgbase/{name}/comments/{id}/form`
    - Produces a JSON response based on {"form": "<form_markup>"}.
- POST `/pkgbase/{name}/comments'
    - Creates a new comment.
- POST `/pkgbase/{name}/comments/{id}`
    - Edits an existing comment.

In addition, some Javascript has been modified for our new routes.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-09-27 18:46:20 -07:00
parent 0d8216e8ea
commit fc28aad245
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
6 changed files with 333 additions and 79 deletions

View file

@ -16,26 +16,38 @@
)
| safe
}}
{% if is_maintainer %}
<form class="delete-comment-form" method="post" action="/pkgbase/{{ pkgname }}/">
<fieldset style="display:inline;">
<input type="hidden" name="action" value="do_DeleteComment" />
<input type="hidden" name="comment_id" value="{{ comment.ID }}"/>
<input type="hidden" name="return_to" value="/pkgbase/{{ pkgname }}/"/>
<input type="image" class="delete-comment" src="/images/x.min.svg" width="11" height="11" alt="{{ 'Delete comment' | tr }}" title="{{ 'Delete comment' | tr }}" name="submit" value="1" />
</fieldset>
</form>
<a href="/pkgbase/{{ pkgname }}/edit-comment/?comment_id={{ comment.ID }}" class="edit-comment" title="Edit comment"><img src="/images/pencil.min.svg" alt="Edit comment" width="11" height="11"></a>
{% if comment.Editor %}
{% set edited_on = comment.EditedTS | dt | as_timezone(timezone) %}
<span class="edited">
({{ "edited on %s by %s" | tr
| format(edited_on.strftime('%Y-%m-%d %H:%M'),
'<a href="/account/%s">%s</a>' | format(
comment.Editor.Username, comment.Editor.Username))
| safe
}})
</span>
{% endif %}
{% if request.user.is_elevated() or pkgbase.Maintainer == request.user %}
<form class="delete-comment-form" method="post" action="/pkgbase/{{ name }}/">
<fieldset style="display:inline;">
<input type="hidden" name="action" value="do_DeleteComment" />
<input type="hidden" name="comment_id" value="{{ comment.ID }}"/>
<input type="hidden" name="return_to" value="/pkgbase/{{ name }}/"/>
<input type="image" class="delete-comment" src="/images/x.min.svg" width="11" height="11" alt="{{ 'Delete comment' | tr }}" title="{{ 'Delete comment' | tr }}" name="submit" value="1" />
</fieldset>
</form>
<a href="/pkgbase/{{ pkgname }}/edit-comment/?comment_id={{ comment.ID }}" class="edit-comment" title="Edit comment"><img src="/images/pencil.min.svg" alt="Edit comment" width="11" height="11"></a>
<form class="pin-comment-form" method="post" action="/pkgbase/{{ name }}/">
<fieldset style="display:inline;">
<input type="hidden" name="action" value="do_PinComment"/>
<input type="hidden" name="comment_id" value="{{ comment.ID }}"/>
<input type="hidden" name="package_base" value="{{ pkgbase.ID }}"/>
<input type="hidden" name="return_to" value="/pkgbase/{{ name }}/"/>
<input type="image" class="pin-comment" src="/images/pin.min.svg" width="11" height="11" alt="{{ 'Pin comment' | tr }}" title="{{ 'Pin comment' | tr }}" name="submit" value="1"/>
</fieldset>
</form>
{% endif %}
<form class="pin-comment-form" method="post" action="/pkgbase/{{ pkgname }}/">
<fieldset style="display:inline;">
<input type="hidden" name="action" value="do_PinComment"/>
<input type="hidden" name="comment_id" value="{{ comment.ID }}"/>
<input type="hidden" name="package_base" value="{{ pkgbase_id }}"/>
<input type="hidden" name="return_to" value="/pkgbase/{{ pkgname }}/"/>
<input type="image" class="pin-comment" src="/images/pin.min.svg" width="11" height="11" alt="{{ 'Pin comment' | tr }}" title="{{ 'Pin comment' | tr }}" name="submit" value="1"/>
</fieldset>
</form>
</h4>
<div id="comment-{{ comment.ID }}-content" class="article-content">
<div>

View file

@ -0,0 +1,46 @@
{# `action` is assigned the proper route to use for the form action.
When `comment` is provided (PackageComment), we display an edit form
for the comment. Otherwise, we display a new form.
Routes:
new comment - /pkgbase/{name}/comments
edit comment - /pkgbase/{name}/comments/{id}
#}
{% set action = "/pkgbase/%s/comments" | format(pkgbase.Name) %}
{% if comment %}
{% set action = "/pkgbase/%s/comments/%d" | format(pkgbase.Name, comment.ID) %}
{% endif %}
<form action="{{ action }}" method="post">
<fieldset>
<p>
{{ "Git commit identifiers referencing commits in the AUR package "
"repository and URLs are converted to links automatically." | tr }}
{{ "%sMarkdown syntax%s is partially supported." | tr
| format('<a href="https://daringfireball.net/projects/markdown/syntax">',
"</a>")
| safe }}
</p>
<p>
<textarea id="id_comment"
name="comment"
cols="80"
rows="10"
>{% if comment %}{{ comment.Comments or '' }}{% endif %}</textarea>
</p>
<p>
<button type="submit" class="button">
{{ ("Save" if comment else "Add Comment") | tr }}
</button>
{% if comment and not request.user.notified(pkgbase) %}
<span class="comment-enable-notifications">
<input type="checkbox" name="enable_notifications"
id="id_enable_notifications" />
<label for="id_enable_notifications">
{{ "Enable notifications" | tr }}
</label>
</span>
{% endif %}
</p>
</fieldset>
</form>

View file

@ -8,44 +8,7 @@
{% if request.user.is_authenticated() %}
<div id="generic-form" class="box">
<h2>Add Comment</h2>
<form action="/pkgbase/{{ pkgname }}/" method="post">
<fieldset>
<div>
<input type="hidden" name="action" value="do_AddComment"/>
<input type="hidden" name="ID" value="{{ pkgbase_id }}"/>
</div>
<p>
{{
"Git commit identifiers referencing commits in the AUR package"
" repository and URLs are converted to links automatically."
| tr
}}
{{
"%sMarkdown Syntax%s is partially supported."
| tr
| format('<a href="https://daringfireball.net/projects/markdown/syntax">', '</a>')
| safe
}}
</p>
<p>
<textarea id="id_comment" name="comment" cols="80" rows="10"></textarea>
</p>
<p>
<input type="submit" value="{{ 'Add Comment' | tr }}"/>
{% if not notifications_enabled %}
<span class="comment-enable-notifications">
<input id="id_enable_notifications"
type="checkbox"
name="enable_notifications"
/>
<label for="id_enable_notifications">
{{ "Enable notifications" | tr }}
</label>
</span>
{% endif %}
</p>
</fieldset>
</form>
{% include "partials/packages/comment_form.html" %}
</div>
{% endif %}
@ -99,29 +62,28 @@ function handleEditCommentClick(event) {
// The div class="article-content" which contains the comment
const edit_form = parent_element.nextElementSibling;
const params = new URLSearchParams({
type: "get-comment-form",
arg: comment_id,
base_id: {{ pkgbase.ID }},
pkgbase_name: {{ pkgbase.Name }}
});
const url = '/rpc?' + params.toString();
const url = "/pkgbase/{{ pkgbase.Name }}/comments/" + comment_id + "/form";
add_busy_indicator(event.target);
fetch(url, {
method: 'GET'
method: 'GET',
credentials: 'same-origin'
})
.then(function(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response.json();
})
.then(function(response) { return response.json(); })
.then(function(data) {
remove_busy_indicator(event.target);
if (data.success) {
edit_form.innerHTML = data.form;
edit_form.querySelector('textarea').focus();
} else {
alert(data.error);
}
edit_form.innerHTML = data.form;
edit_form.querySelector('textarea').focus();
})
.catch(function(error) {
remove_busy_indicator(event.target);
console.error(error);
});
}