abort(401) # Must be logged in
comment = None
- if "commentid" in request.args:
- commentid = request.args["commentid"]
- comment = query_db("select * from song_comments inner join users on song_comments.userid == users.userid where commentid = ?", [commentid], one=True)
+ if "replytoid" in request.args:
+ replytoid = request.args["replytoid"]
+ comment = query_db("select * from song_comments inner join users on song_comments.userid == users.userid where commentid = ?", [replytoid], one=True)
session["previous_page"] = request.referrer
return render_template("comment.html", song=song, comment=comment)
abort(404) # Invald songid
comment = None
- if "commentid" in request.args:
- commentid = request.args["commentid"]
- comment = query_db("select * from song_comments inner join users on song_comments.userid == users.userid where commentid = ?", [commentid], one=True)
+ if "replytoid" in request.args:
+ replytoid = request.args["replytoid"]
+ comment = query_db("select * from song_comments inner join users on song_comments.userid == users.userid where commentid = ?", [replytoid], one=True)
if not comment:
abort(404) # Invalid comment
content = request.form["content"]
userid = session["userid"]
songid = request.args["songid"]
- replytoid = request.args.get("commentid", None)
+ replytoid = request.args.get("replytoid", None)
query_db(
"insert into song_comments (songid, userid, replytoid, created, content) values (?, ?, ?, ?, ?)",
args=[songid, userid, replytoid, timestamp, content])
comments = query_db("select * from song_comments inner join users on song_comments.userid == users.userid where songid = ?", [self.songid])
# Top-level comments
song_comments = sorted([dict(c) for c in comments if c["replytoid"] is None], key=lambda c: c["created"])
+ song_comments = list(reversed(song_comments))
# Replies (can only reply to top-level)
for comment in song_comments:
comment["replies"] = sorted([c for c in comments if c["replytoid"] == comment["commentid"]], key=lambda c: c["created"])
<!-- Song Comments -->
<div class="song-comments">
- Comments:
+ Comments:<br>
<a href="/comment?songid={{ song.songid }}">Add a Comment</a>
{% for comment in song.get_comments() %}
<div class="top-level-comment">
- <a href="/users/{{ comment['username'] }}" class="profile-link">{{ comment['username'] }}</a>
- <p>{{ comment['content'] }}</p>
+
+ <a href="/users/{{ comment['username'] }}" class="profile-link">{{ comment['username'] }}:</a>
+ {{ comment['content'] }}
+
+ {% if session['userid'] == comment['userid'] or session['userid'] == song.userid %}
+ <div class="comment-button-container">
+ {% endif %}
+
+ <!-- Only commenter can edit comment -->
+ {% if session['userid'] == comment['userid'] %}
+ <a href="/comment?commentid={{ comment['commentid'] }}&songid={{ song.songid }}" class="comment-button">
+ Edit
+ </a>
+ {% endif %}
+
+ <!-- Commenter and song owner can delete comment -->
+ {% if session['userid'] == comment['userid'] or session['userid'] == song.userid %}
+ <a href="/delete-comment/{{ comment['commentid'] }}" onclick="return confirm("Are you sure you want to delete this comment?")" class="comment-button">
+ Delete
+ </a>
+ {% endif %}
+
+ {% if session['userid'] == comment['userid'] or session['userid'] == song.userid %}
+ </div>
+ {% endif %}
{% for reply in comment['replies'] %}
<div class="reply-comment">
- <a href="/users/{{ reply['username'] }}" class="profile-link">{{ reply['username'] }}</a>
- <p>{{ reply['content'] }}</p>
+
+ <a href="/users/{{ reply['username'] }}" class="profile-link">{{ reply['username'] }}:</a>
+ {{ reply['content'] }}
+
+ {% if session['userid'] == reply['userid'] or session['userid'] == song.userid %}
+ <div class="comment-button-container">
+ {% endif %}
+
+ <!-- Only commenter can edit comment -->
+ {% if session['userid'] == reply['userid'] %}
+ <a href="/comment?commentid={{ reply['commentid'] }}&songid={{ song.songid }}&replytoid={{ comment['commentid'] }}" class="comment-button">
+ Edit
+ </a>
+ {% endif %}
+
+ <!-- Commenter and song owner can delete comment -->
+ {% if session['userid'] == reply['userid'] or session['userid'] == song.userid %}
+ <a href="/delete-comment/{{ reply['commentid'] }}" onclick="return confirm("Are you sure you want to delete this comment?")" class="comment-button">
+ Delete
+ </a>
+ {% endif %}
+
+ {% if session['userid'] == reply['userid'] or session['userid'] == song.userid %}
+ </div>
+ {% endif %}
</div>
{% endfor %}
- <a href="/comment?songid={{ song.songid }}&commentid={{ comment['commentid'] }}">Reply</a>
+ <div class="comment-button-container">
+ <a href="/comment?songid={{ song.songid }}&replytoid={{ comment['commentid'] }}">Reply</a>
+ </div>
</div>
{% endfor %}
</div>