Like buttons in photo notifications

This commit is contained in:
Noah Petherbridge 2023-06-15 19:06:16 -07:00
parent b262ac4be6
commit 70d252dd75
4 changed files with 92 additions and 5 deletions

View File

@ -43,6 +43,16 @@ func Dashboard() http.HandlerFunc {
notifMap := models.MapNotifications(notifs)
models.SetUserRelationshipsInNotifications(currentUser, notifs)
// Map likes for in-line like buttons on (other peoples) photos.
// NOTE: comments can be trickier since the Notification.table_name='photos' if the comment is on a photo,
// hard to create a LikesMap for the specific comment ID.
var photoIDs = []uint64{}
for _, notif := range notifs {
if notif.TableName == "photos" {
photoIDs = append(photoIDs, notif.TableID)
}
}
// Restricted profile warnings.
var (
isShyUser = currentUser.IsShy()
@ -58,6 +68,8 @@ func Dashboard() http.HandlerFunc {
// Show a warning to 'restricted' profiles who are especially private.
"IsShyUser": isShyUser,
"HasPublicPhoto": hasPublic,
"PhotoLikeMap": models.MapLikes(currentUser, "photos", photoIDs),
}
if err := tmpl.Execute(w, r, vars); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@ -110,7 +110,7 @@ func Likes() http.HandlerFunc {
}
// Remove the target's notification about this like.
models.RemoveNotification(req.TableName, req.TableID)
models.RemoveSpecificNotification(targetUser.ID, models.NotificationLike, req.TableName, req.TableID)
} else {
if err := models.AddLike(currentUser, req.TableName, req.TableID); err != nil {
SendJSON(w, http.StatusBadRequest, Response{

View File

@ -147,10 +147,12 @@ func (n *Notification) Save() error {
// NotificationBody can store remote tables mapped.
type NotificationBody struct {
PhotoID uint64
ThreadID uint64
Photo *Photo
Thread *Thread
PhotoID uint64
ThreadID uint64
CommentID uint64
Photo *Photo
Thread *Thread
Comment *Comment
}
type NotificationMap map[uint64]*NotificationBody
@ -179,6 +181,12 @@ func MapNotifications(ns []*Notification) NotificationMap {
result.mapNotificationPhotos(IDs)
result.mapNotificationThreads(IDs)
// NOTE: comment loading is not used - was added when trying to add "Like" buttons inside
// your Comment notifications. But when a photo is commented on, the notification table_name=photos,
// with the comment ID not so readily accessible.
//
// result.mapNotificationComments(IDs)
return result
}
@ -275,3 +283,52 @@ func (nm NotificationMap) mapNotificationThreads(IDs []uint64) {
}
}
}
// Helper function of MapNotifications to eager load Comment attachments.
func (nm NotificationMap) mapNotificationComments(IDs []uint64) {
type scanner struct {
CommentID uint64
NotificationID uint64
}
var scan []scanner
// Load all of these that have comments.
err := DB.Table(
"notifications",
).Joins(
"JOIN comments ON (notifications.table_name='comments' AND notifications.table_id=comments.id)",
).Select(
"comments.id AS comment_id",
"notifications.id AS notification_id",
).Where(
"notifications.id IN ?",
IDs,
).Scan(&scan)
if err.Error != nil {
log.Error("Couldn't select comment IDs for notifications: %s", err.Error)
}
// Collect and load all the comments by ID.
var commentIDs = []uint64{}
for _, row := range scan {
// Store the comment ID in the result now.
nm[row.NotificationID].CommentID = row.CommentID
commentIDs = append(commentIDs, row.CommentID)
}
log.Error("MAP COMMENT IDS GOT: %+v", commentIDs)
// Load the comments.
if len(commentIDs) > 0 {
if comments, err := GetComments(commentIDs); err != nil {
log.Error("Couldn't load comment IDs for notifications: %s", err)
} else {
// Marry them to their notification IDs.
for _, body := range nm {
if comment, ok := comments[body.CommentID]; ok {
body.Comment = comment
}
}
}
}
}

View File

@ -420,6 +420,24 @@
{{else}}
<small><em>No caption.</em></small>
{{end}}
<!-- Like button for the photo right here -->
{{if ne $Body.Photo.UserID $Root.CurrentUser.ID}}
<div class="mt-1">
{{$Like := $Root.PhotoLikeMap.Get $Body.PhotoID}}
<a href="#" class="button is-small nonshy-like-button"
data-table-name="photos" data-table-id="{{$Body.PhotoID}}"
title="Like">
<span class="icon{{if $Like.UserLikes}} has-text-danger{{end}}"><i class="fa fa-heart"></i></span>
<span class="nonshy-likes">
Like
{{if gt $Like.Count 0}}
({{$Like.Count}})
{{end}}
</span>
</a>
</div>
{{end}}
</div>
{{end}}
</div>