✨ Notify is back!
This commit is contained in:
@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -84,10 +85,17 @@ func createComment(c *fiber.Ctx) error {
|
||||
var err error
|
||||
var res models.Feed
|
||||
|
||||
var columnName string
|
||||
var tableName string
|
||||
|
||||
switch postType {
|
||||
case "moments":
|
||||
columnName = "moment"
|
||||
tableName = viper.GetString("database.table_prefix") + "moments"
|
||||
err = database.C.Model(&models.Moment{}).Where("alias = ?", alias).Select("id").First(&res).Error
|
||||
case "articles":
|
||||
columnName = "article"
|
||||
tableName = viper.GetString("database.table_prefix") + "articles"
|
||||
err = database.C.Model(&models.Article{}).Where("alias = ?", alias).Select("id").First(&res).Error
|
||||
default:
|
||||
return fiber.NewError(fiber.StatusBadRequest, "comment must belongs to a resource")
|
||||
@ -116,11 +124,15 @@ func createComment(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
if item, err := services.NewPost(item); err != nil {
|
||||
item, err = services.NewPost(item)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(item)
|
||||
}
|
||||
|
||||
// Notify the original poster their post is commented by someone
|
||||
go services.CommentNotify(item, res, columnName, tableName)
|
||||
|
||||
return c.JSON(item)
|
||||
}
|
||||
|
||||
func editComment(c *fiber.Ctx) error {
|
||||
|
@ -1,11 +1,11 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/grpc/proto"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/database"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/grpc"
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/models"
|
||||
"context"
|
||||
"github.com/spf13/viper"
|
||||
"time"
|
||||
)
|
||||
@ -34,7 +34,7 @@ func GetAccountFollowed(user models.Account, target models.Account) (models.Acco
|
||||
return relationship, err == nil
|
||||
}
|
||||
|
||||
func NotifyAccount(user models.Account, subject, content string, links ...*proto.NotifyLink) error {
|
||||
func NotifyAccount(user models.Account, subject, content string, realtime bool, links ...*proto.NotifyLink) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
@ -45,6 +45,7 @@ func NotifyAccount(user models.Account, subject, content string, links ...*proto
|
||||
Content: content,
|
||||
Links: links,
|
||||
RecipientId: uint64(user.ID),
|
||||
IsRealtime: realtime,
|
||||
IsImportant: false,
|
||||
})
|
||||
|
||||
|
@ -2,6 +2,8 @@ package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/grpc/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hydrogen/interactive/pkg/database"
|
||||
@ -80,3 +82,22 @@ func (v *PostTypeContext) CountComment(id uint) (int64, error) {
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func CommentNotify(this models.PostInterface, original models.Feed, columnName, tableName string) {
|
||||
var op models.Feed
|
||||
if err := database.C.Where(columnName+"_id = ?", original.ID).Preload("Author").Table(tableName).First(&op).Error; err == nil {
|
||||
if op.Author.ID != this.GetAuthor().ID {
|
||||
postUrl := fmt.Sprintf("https://%s/posts/%d", viper.GetString("domain"), this.GetID())
|
||||
err := NotifyAccount(
|
||||
op.Author,
|
||||
fmt.Sprintf("%s commented you", this.GetAuthor().Name),
|
||||
fmt.Sprintf("%s commented your post. Check it out!", this.GetAuthor().Name),
|
||||
false,
|
||||
&proto.NotifyLink{Label: "Related post", Url: postUrl},
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when notifying user...")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -321,6 +321,7 @@ func NewPost[T models.PostInterface](item T) (T, error) {
|
||||
op.Author,
|
||||
fmt.Sprintf("%s replied you", item.GetAuthor().Name),
|
||||
fmt.Sprintf("%s replied your post. Check it out!", item.GetAuthor().Name),
|
||||
false,
|
||||
&proto.NotifyLink{Label: "Related post", Url: postUrl},
|
||||
)
|
||||
if err != nil {
|
||||
@ -345,7 +346,8 @@ func NewPost[T models.PostInterface](item T) (T, error) {
|
||||
err := NotifyAccount(
|
||||
account,
|
||||
fmt.Sprintf("%s just posted a post", item.GetAuthor().Name),
|
||||
"Account you followed post a brand new post. Check it out!",
|
||||
"Someone you followed post a brand new post. Check it out!",
|
||||
false,
|
||||
&proto.NotifyLink{Label: "Related post", Url: postUrl},
|
||||
)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user