From 70d252dd75e8c3475f5f1ce0caed57d318c7e120 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Thu, 15 Jun 2023 19:06:16 -0700 Subject: [PATCH] Like buttons in photo notifications --- pkg/controller/account/dashboard.go | 12 +++++ pkg/controller/api/likes.go | 2 +- pkg/models/notification.go | 65 ++++++++++++++++++++++++++-- web/templates/account/dashboard.html | 18 ++++++++ 4 files changed, 92 insertions(+), 5 deletions(-) diff --git a/pkg/controller/account/dashboard.go b/pkg/controller/account/dashboard.go index 1443036..87d742c 100644 --- a/pkg/controller/account/dashboard.go +++ b/pkg/controller/account/dashboard.go @@ -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) diff --git a/pkg/controller/api/likes.go b/pkg/controller/api/likes.go index a19a77a..64f9402 100644 --- a/pkg/controller/api/likes.go +++ b/pkg/controller/api/likes.go @@ -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{ diff --git a/pkg/models/notification.go b/pkg/models/notification.go index bbc11a5..8e4b325 100644 --- a/pkg/models/notification.go +++ b/pkg/models/notification.go @@ -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 + } + } + } + } +} diff --git a/web/templates/account/dashboard.html b/web/templates/account/dashboard.html index cb508a2..f7535a4 100644 --- a/web/templates/account/dashboard.html +++ b/web/templates/account/dashboard.html @@ -420,6 +420,24 @@ {{else}} No caption. {{end}} + + + {{if ne $Body.Photo.UserID $Root.CurrentUser.ID}} +
+ {{$Like := $Root.PhotoLikeMap.Get $Body.PhotoID}} + + + + Like + {{if gt $Like.Count 0}} + ({{$Like.Count}}) + {{end}} + + +
+ {{end}} {{end}}