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 {
notif := &models.Notification{
UserID: targetUser.ID,
User: *currentUser,
AboutUser: *currentUser,
Type: models.NotificationLike,
TableName: req.TableName,
TableID: req.TableID,

View File

@ -5,6 +5,7 @@ import (
"net/http"
"strings"
"git.kirsle.net/apps/gosocial/pkg/log"
"git.kirsle.net/apps/gosocial/pkg/models"
"git.kirsle.net/apps/gosocial/pkg/session"
"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)
} else {
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)
templates.Redirect(w, "/friends?view=requests")
return

View File

@ -76,6 +76,7 @@ func Certification() http.HandlerFunc {
}
cert.Status = models.CertificationPhotoNeeded
cert.AdminComment = ""
cert.Save()
// Removing your photo = not certified again.
@ -252,6 +253,17 @@ func AdminCertification() http.HandlerFunc {
user.Certified = false
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.
if err := mail.Send(mail.Message{
To: user.Email,
@ -281,6 +293,16 @@ func AdminCertification() http.HandlerFunc {
user.Certified = true
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.
if err := mail.Send(mail.Message{
To: user.Email,

View File

@ -12,7 +12,7 @@ type Notification struct {
ID uint64 `gorm:"primaryKey"`
UserID uint64 `gorm:"index"` // who it belongs to
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, ...
Read bool `gorm:"index"`
TableName string // on which of your tables (photos, comments, ...)
@ -24,15 +24,18 @@ type Notification struct {
// Preload related tables for the forum (classmethod).
func (n *Notification) Preload() *gorm.DB {
return DB.Preload("User.ProfilePhoto")
return DB.Preload("AboutUser.ProfilePhoto")
}
type NotificationType string
const (
NotificationLike NotificationType = "like"
NotificationComment = "comment"
NotificationCustom = "custom" // custom message pushed
NotificationLike NotificationType = "like"
NotificationFriendApproved = "friendship_approved"
NotificationComment = "comment"
NotificationCertRejected = "cert_rejected"
NotificationCertApproved = "cert_approved"
NotificationCustom = "custom" // custom message pushed
)
// CreateNotification

View File

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