Like buttons in photo notifications
This commit is contained in:
parent
b262ac4be6
commit
70d252dd75
|
@ -43,6 +43,16 @@ func Dashboard() http.HandlerFunc {
|
||||||
notifMap := models.MapNotifications(notifs)
|
notifMap := models.MapNotifications(notifs)
|
||||||
models.SetUserRelationshipsInNotifications(currentUser, 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.
|
// Restricted profile warnings.
|
||||||
var (
|
var (
|
||||||
isShyUser = currentUser.IsShy()
|
isShyUser = currentUser.IsShy()
|
||||||
|
@ -58,6 +68,8 @@ func Dashboard() http.HandlerFunc {
|
||||||
// Show a warning to 'restricted' profiles who are especially private.
|
// Show a warning to 'restricted' profiles who are especially private.
|
||||||
"IsShyUser": isShyUser,
|
"IsShyUser": isShyUser,
|
||||||
"HasPublicPhoto": hasPublic,
|
"HasPublicPhoto": hasPublic,
|
||||||
|
|
||||||
|
"PhotoLikeMap": models.MapLikes(currentUser, "photos", photoIDs),
|
||||||
}
|
}
|
||||||
if err := tmpl.Execute(w, r, vars); err != nil {
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
|
|
@ -110,7 +110,7 @@ func Likes() http.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the target's notification about this like.
|
// 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 {
|
} else {
|
||||||
if err := models.AddLike(currentUser, req.TableName, req.TableID); err != nil {
|
if err := models.AddLike(currentUser, req.TableName, req.TableID); err != nil {
|
||||||
SendJSON(w, http.StatusBadRequest, Response{
|
SendJSON(w, http.StatusBadRequest, Response{
|
||||||
|
|
|
@ -149,8 +149,10 @@ func (n *Notification) Save() error {
|
||||||
type NotificationBody struct {
|
type NotificationBody struct {
|
||||||
PhotoID uint64
|
PhotoID uint64
|
||||||
ThreadID uint64
|
ThreadID uint64
|
||||||
|
CommentID uint64
|
||||||
Photo *Photo
|
Photo *Photo
|
||||||
Thread *Thread
|
Thread *Thread
|
||||||
|
Comment *Comment
|
||||||
}
|
}
|
||||||
|
|
||||||
type NotificationMap map[uint64]*NotificationBody
|
type NotificationMap map[uint64]*NotificationBody
|
||||||
|
@ -179,6 +181,12 @@ func MapNotifications(ns []*Notification) NotificationMap {
|
||||||
result.mapNotificationPhotos(IDs)
|
result.mapNotificationPhotos(IDs)
|
||||||
result.mapNotificationThreads(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
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -420,6 +420,24 @@
|
||||||
{{else}}
|
{{else}}
|
||||||
<small><em>No caption.</em></small>
|
<small><em>No caption.</em></small>
|
||||||
{{end}}
|
{{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>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user