Notifications on approved friendship and Cert Photo verdicts

This commit is contained in:
Noah 2022-08-25 20:36:59 -07:00
parent 25818a7117
commit dbc9f9e3b3
5 changed files with 70 additions and 11 deletions

View File

@ -104,7 +104,7 @@ func Likes() http.HandlerFunc {
if targetUser != nil { if targetUser != nil {
notif := &models.Notification{ notif := &models.Notification{
UserID: targetUser.ID, UserID: targetUser.ID,
User: *currentUser, AboutUser: *currentUser,
Type: models.NotificationLike, Type: models.NotificationLike,
TableName: req.TableName, TableName: req.TableName,
TableID: req.TableID, TableID: req.TableID,

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"git.kirsle.net/apps/gosocial/pkg/log"
"git.kirsle.net/apps/gosocial/pkg/models" "git.kirsle.net/apps/gosocial/pkg/models"
"git.kirsle.net/apps/gosocial/pkg/session" "git.kirsle.net/apps/gosocial/pkg/session"
"git.kirsle.net/apps/gosocial/pkg/templates" "git.kirsle.net/apps/gosocial/pkg/templates"
@ -76,6 +77,16 @@ func AddFriend() http.HandlerFunc {
session.FlashError(w, r, "Couldn't send friend request: %s.", err) session.FlashError(w, r, "Couldn't send friend request: %s.", err)
} else { } else {
if verdict == "approve" { if verdict == "approve" {
// Notify the requestor they'd been approved.
notif := &models.Notification{
UserID: user.ID,
AboutUser: *currentUser,
Type: models.NotificationFriendApproved,
}
if err := models.CreateNotification(notif); err != nil {
log.Error("Couldn't create approved notification: %s", err)
}
session.Flash(w, r, "You accepted the friend request from %s!", username) session.Flash(w, r, "You accepted the friend request from %s!", username)
templates.Redirect(w, "/friends?view=requests") templates.Redirect(w, "/friends?view=requests")
return return

View File

@ -76,6 +76,7 @@ func Certification() http.HandlerFunc {
} }
cert.Status = models.CertificationPhotoNeeded cert.Status = models.CertificationPhotoNeeded
cert.AdminComment = ""
cert.Save() cert.Save()
// Removing your photo = not certified again. // Removing your photo = not certified again.
@ -252,6 +253,17 @@ func AdminCertification() http.HandlerFunc {
user.Certified = false user.Certified = false
user.Save() user.Save()
// Notify the user about this rejection.
notif := &models.Notification{
UserID: user.ID,
AboutUser: *user,
Type: models.NotificationCertRejected,
Message: comment,
}
if err := models.CreateNotification(notif); err != nil {
log.Error("Couldn't create rejection notification: %s", err)
}
// Notify the user via email. // Notify the user via email.
if err := mail.Send(mail.Message{ if err := mail.Send(mail.Message{
To: user.Email, To: user.Email,
@ -281,6 +293,16 @@ func AdminCertification() http.HandlerFunc {
user.Certified = true user.Certified = true
user.Save() user.Save()
// Notify the user about this approval.
notif := &models.Notification{
UserID: user.ID,
AboutUser: *user,
Type: models.NotificationCertApproved,
}
if err := models.CreateNotification(notif); err != nil {
log.Error("Couldn't create approval notification: %s", err)
}
// Notify the user via email. // Notify the user via email.
if err := mail.Send(mail.Message{ if err := mail.Send(mail.Message{
To: user.Email, To: user.Email,

View File

@ -12,7 +12,7 @@ type Notification struct {
ID uint64 `gorm:"primaryKey"` ID uint64 `gorm:"primaryKey"`
UserID uint64 `gorm:"index"` // who it belongs to UserID uint64 `gorm:"index"` // who it belongs to
AboutUserID *uint64 `form:"index"` // the other party of this notification AboutUserID *uint64 `form:"index"` // the other party of this notification
User User `gorm:"foreignKey:about_user_id"` AboutUser User `gorm:"foreignKey:about_user_id"`
Type NotificationType // like, comment, ... Type NotificationType // like, comment, ...
Read bool `gorm:"index"` Read bool `gorm:"index"`
TableName string // on which of your tables (photos, comments, ...) TableName string // on which of your tables (photos, comments, ...)
@ -24,14 +24,17 @@ type Notification struct {
// Preload related tables for the forum (classmethod). // Preload related tables for the forum (classmethod).
func (n *Notification) Preload() *gorm.DB { func (n *Notification) Preload() *gorm.DB {
return DB.Preload("User.ProfilePhoto") return DB.Preload("AboutUser.ProfilePhoto")
} }
type NotificationType string type NotificationType string
const ( const (
NotificationLike NotificationType = "like" NotificationLike NotificationType = "like"
NotificationFriendApproved = "friendship_approved"
NotificationComment = "comment" NotificationComment = "comment"
NotificationCertRejected = "cert_rejected"
NotificationCertApproved = "cert_approved"
NotificationCustom = "custom" // custom message pushed NotificationCustom = "custom" // custom message pushed
) )

View File

@ -176,10 +176,10 @@
<strong class="tag is-success">NEW!</strong> <strong class="tag is-success">NEW!</strong>
</div> </div>
{{end}} {{end}}
<a href="/u/{{.User.Username}}"> <a href="/u/{{.AboutUser.Username}}">
<figure class="image is-48x48 is-inline-block"> <figure class="image is-48x48 is-inline-block">
{{if .User.ProfilePhoto.ID}} {{if .AboutUser.ProfilePhoto.ID}}
<img src="{{PhotoURL .User.ProfilePhoto.CroppedFilename}}"> <img src="{{PhotoURL .AboutUser.ProfilePhoto.CroppedFilename}}">
{{else}} {{else}}
<img src="/static/img/shy.png"> <img src="/static/img/shy.png">
{{end}} {{end}}
@ -191,7 +191,7 @@
{{if eq .Type "like"}} {{if eq .Type "like"}}
<span class="icon"><i class="fa fa-heart has-text-danger"></i></span> <span class="icon"><i class="fa fa-heart has-text-danger"></i></span>
<span> <span>
<a href="/u/{{.User.Username}}"><strong>{{.User.Username}}</strong></a> <a href="/u/{{.AboutUser.Username}}"><strong>{{.AboutUser.Username}}</strong></a>
liked your liked your
{{if eq .TableName "photos"}} {{if eq .TableName "photos"}}
photo. photo.
@ -201,11 +201,34 @@
{{.TableName}}. {{.TableName}}.
{{end}} {{end}}
</span> </span>
{{else if eq .Type "friendship_approved"}}
<span class="icon"><i class="fa fa-user-group"></i></span>
<span>
<a href="/u/{{.AboutUser.Username}}"><strong>{{.AboutUser.Username}}</strong></a>
accepted your friend request!
</span>
{{else if eq .Type "cert_approved"}}
<span class="icon"><i class="fa fa-certificate has-text-success"></i></span>
<span>
Your <strong>certification photo</strong> was approved!
</span>
{{else if eq .Type "cert_rejected"}}
<span class="icon"><i class="fa fa-certificate has-text-danger"></i></span>
<span>
Your <strong>certification photo</strong> was rejected!
</span>
{{else}} {{else}}
{{.User.Username}} {{.Type}} {{.TableName}} {{.TableID}} {{.AboutUser.Username}} {{.Type}} {{.TableName}} {{.TableID}}
{{end}} {{end}}
</div> </div>
<!-- Attached message? -->
{{if .Message}}
<div class="block content">
{{ToMarkdown .Message}}
</div>
{{end}}
<!-- Photo caption? --> <!-- Photo caption? -->
{{if $Body.Photo}} {{if $Body.Photo}}
<div class="block"> <div class="block">