From 9ac83c42921321f093b4a722a700c699e70851a9 Mon Sep 17 00:00:00 2001 From: sunface Date: Wed, 9 Jun 2021 17:02:20 +0800 Subject: [PATCH] upadte --- pages/notifications.tsx | 1 + server/internal/api/tag.go | 10 +++++++ server/internal/interaction/follow.go | 4 +-- server/internal/interaction/like.go | 8 +++++- server/internal/notification/notification.go | 30 ++++++++------------ server/internal/storage/sql_tables.go | 2 ++ server/internal/story/comment.go | 4 +-- server/internal/story/post.go | 4 +-- server/pkg/models/notification.go | 1 + 9 files changed, 39 insertions(+), 25 deletions(-) diff --git a/pages/notifications.tsx b/pages/notifications.tsx index 1f491a56..dc7578ec 100644 --- a/pages/notifications.tsx +++ b/pages/notifications.tsx @@ -15,6 +15,7 @@ import Notifications from "components/notifications" const filters = [ {icon: 'bell',label:'All',type: 0}, + {icon: 'comments',label:'System',type: 7}, {icon: 'comments',label:'Comments',type: 1}, {icon: 'favorites',label:'Likes', type: 2}, {icon: 'follow',label:'Follows', type: 5}, diff --git a/server/internal/api/tag.go b/server/internal/api/tag.go index 2949965f..ad3732e9 100644 --- a/server/internal/api/tag.go +++ b/server/internal/api/tag.go @@ -6,6 +6,8 @@ import ( "github.com/gin-gonic/gin" "github.com/imdotdev/im.dev/server/internal/interaction" + "github.com/imdotdev/im.dev/server/internal/notification" + "github.com/imdotdev/im.dev/server/internal/story" "github.com/imdotdev/im.dev/server/internal/tags" "github.com/imdotdev/im.dev/server/internal/user" "github.com/imdotdev/im.dev/server/pkg/common" @@ -210,6 +212,14 @@ func RemoveTagStory(c *gin.Context) { return } + s, err := story.GetStory(storyID, "") + if err == nil { + t, err := tags.GetTag(tagID, "") + if err == nil { + notification.Send(s.CreatorID, "", models.NotificationSystem, storyID, " delete your story from tag "+t.Name, user.ID) + } + } + c.JSON(http.StatusOK, common.RespSuccess(nil)) } diff --git a/server/internal/interaction/follow.go b/server/internal/interaction/follow.go index feecb1f0..e927d9c9 100644 --- a/server/internal/interaction/follow.go +++ b/server/internal/interaction/follow.go @@ -66,9 +66,9 @@ func Follow(targetID string, userId string) *e.Error { if !followed { if models.GetIDType(targetID) == models.IDTypeUser { - notification.Send(targetID, "", models.NotificationFollow, targetID, userId) + notification.Send(targetID, "", models.NotificationFollow, targetID, " started following you", userId) } else { - notification.Send("", targetID, models.NotificationFollow, targetID, userId) + notification.Send("", targetID, models.NotificationFollow, targetID, " started following you", userId) } } diff --git a/server/internal/interaction/like.go b/server/internal/interaction/like.go index 44cd9598..5be97f96 100644 --- a/server/internal/interaction/like.go +++ b/server/internal/interaction/like.go @@ -76,7 +76,13 @@ func Like(storyID string, userId string) *e.Error { // send notification to story creator and owner creator, owner := models.GetStoryCreatorAndOrg(storyID) if creator != "" { - notification.Send(creator, owner, models.NotificationLike, storyID, userId) + var t string + if models.GetIDType(storyID) == models.IDTypeComment { + t = " liked your comment" + } else { + t = " liked your story" + } + notification.Send(creator, owner, models.NotificationLike, storyID, t, userId) } } return nil diff --git a/server/internal/notification/notification.go b/server/internal/notification/notification.go index 6003598c..14800c96 100644 --- a/server/internal/notification/notification.go +++ b/server/internal/notification/notification.go @@ -13,18 +13,18 @@ import ( var logger = log.RootLogger.New("logger", "notification") -func Send(userID, orgID string, noType int, noID string, operatorID string) { +func Send(userID, orgID string, noType int, noID string, noTitle string, operatorID string) { if userID != "" { - _, err := db.Conn.Exec("INSERT INTO user_notification (user_id,operator_id,notifiable_type,notifiable_id,created) VALUES (?,?,?,?,?)", - userID, operatorID, noType, noID, time.Now()) + _, err := db.Conn.Exec("INSERT INTO user_notification (user_id,operator_id,notifiable_type,notifiable_id,no_title,created) VALUES (?,?,?,?,?,?)", + userID, operatorID, noType, noID, noTitle, time.Now()) if err != nil { logger.Warn("send notification error", "error", err) } } if orgID != "" { - _, err := db.Conn.Exec("INSERT INTO org_notification (user_id,operator_id,notifiable_type,notifiable_id,created) VALUES (?,?,?,?,?)", - orgID, operatorID, noType, noID, time.Now()) + _, err := db.Conn.Exec("INSERT INTO org_notification (user_id,operator_id,notifiable_type,notifiable_id,no_title,created) VALUES (?,?,?,?,?,?)", + orgID, operatorID, noType, noID, noTitle, time.Now()) if err != nil && !e.IsErrUniqueConstraint(err) { logger.Warn("send notification error", "error", err) } @@ -37,11 +37,11 @@ func Query(user *models.User, tp int, page int) ([]*models.Notification, *e.Erro var rows *sql.Rows var err error if tp == 0 { - rows, err = db.Conn.Query("SELECT operator_id,notifiable_type,notifiable_id,read,created FROM user_notification WHERE user_id=? ORDER BY created DESC LIMIT ?,?", user.ID, (page-1)*perPage, page*perPage) + rows, err = db.Conn.Query("SELECT operator_id,notifiable_type,notifiable_id,no_title,read,created FROM user_notification WHERE user_id=? ORDER BY created DESC LIMIT ?,?", user.ID, (page-1)*perPage, page*perPage) } else if tp == models.NotificationComment { - rows, err = db.Conn.Query("SELECT operator_id,notifiable_type,notifiable_id,read,created FROM user_notification WHERE user_id=? and notifiable_type in ('1','6') ORDER BY created DESC LIMIT ?,?", user.ID, (page-1)*perPage, page*perPage) + rows, err = db.Conn.Query("SELECT operator_id,notifiable_type,notifiable_id,no_title,read,created FROM user_notification WHERE user_id=? and notifiable_type in ('1','6') ORDER BY created DESC LIMIT ?,?", user.ID, (page-1)*perPage, page*perPage) } else { - rows, err = db.Conn.Query("SELECT operator_id,notifiable_type,notifiable_id,read,created FROM user_notification WHERE user_id=? and notifiable_type=? ORDER BY created DESC LIMIT ?,?", user.ID, tp, (page-1)*perPage, page*perPage) + rows, err = db.Conn.Query("SELECT operator_id,notifiable_type,notifiable_id,no_title,read,created FROM user_notification WHERE user_id=? and notifiable_type=? ORDER BY created DESC LIMIT ?,?", user.ID, tp, (page-1)*perPage, page*perPage) } if err != nil { @@ -56,7 +56,8 @@ func Query(user *models.User, tp int, page int) ([]*models.Notification, *e.Erro var noID string var read bool var created time.Time - err := rows.Scan(&operatorID, &noType, &noID, &read, &created) + var title string + err := rows.Scan(&operatorID, &noType, &noID, &title, &read, &created) if err != nil { logger.Warn("scan notification", "error", err) continue @@ -65,34 +66,27 @@ func Query(user *models.User, tp int, page int) ([]*models.Notification, *e.Erro operator := &models.UserSimple{ID: operatorID} err = operator.Query() - no := &models.Notification{Created: created, Type: noType, User: operator, Read: read} + no := &models.Notification{Created: created, Type: noType, User: operator, Read: read, Title: title} switch no.Type { - case models.NotificationComment: - no.Title = " commented on your story" + case models.NotificationComment, models.NotificationSystem: no.SubTitle = models.GetStoryTitle(noID) no.StoryID = noID case models.NotificationReply: - no.Title = " replied to your comment" no.SubTitle = models.GetStoryTitle(noID) no.StoryID = noID case models.NotificationLike: if models.GetIDType(noID) == models.IDTypeComment { - no.Title = " liked your comment" id := models.GetCommentStoryID(noID) if id != "" { no.SubTitle = models.GetStoryTitle(id) no.StoryID = id } } else { - no.Title = " liked your story" no.SubTitle = models.GetStoryTitle(noID) no.StoryID = noID } - case models.NotificationFollow: - no.Title = " started following you" case models.NotificationPublish: - no.Title = " published a new story" no.SubTitle = models.GetStoryTitle(noID) no.StoryID = noID } diff --git a/server/internal/storage/sql_tables.go b/server/internal/storage/sql_tables.go index b2ff14bd..fa0218bf 100644 --- a/server/internal/storage/sql_tables.go +++ b/server/internal/storage/sql_tables.go @@ -264,6 +264,7 @@ var sqlTables = map[string]string{ operator_id VARCHAR(255), notifiable_type TINYINT, notifiable_id VARCHAR(255), + no_title VARCHAR(255), read BOOL DEFAULT false, created DATETIME NOT NULL ); @@ -277,6 +278,7 @@ var sqlTables = map[string]string{ operator_id VARCHAR(255), notifiable_type TINYINT, notifiable_id VARCHAR(255), + no_title VARCHAR(255), read BOOL DEFAULT false, created DATETIME NOT NULL ); diff --git a/server/internal/story/comment.go b/server/internal/story/comment.go index 0fbc5ebb..8cbd71f9 100644 --- a/server/internal/story/comment.go +++ b/server/internal/story/comment.go @@ -63,10 +63,10 @@ func AddComment(c *models.Comment) *e.Error { if creator != "" && creator != c.CreatorID { if models.GetIDType(c.TargetID) == models.IDTypeComment { // reply - notification.Send(creator, owner, models.NotificationReply, storyID, c.CreatorID) + notification.Send(creator, owner, models.NotificationReply, storyID, " replied to your comment", c.CreatorID) } else { // comment - notification.Send(creator, owner, models.NotificationComment, storyID, c.CreatorID) + notification.Send(creator, owner, models.NotificationComment, storyID, " commented on your story", c.CreatorID) } } diff --git a/server/internal/story/post.go b/server/internal/story/post.go index 0799700d..d1d96863 100644 --- a/server/internal/story/post.go +++ b/server/internal/story/post.go @@ -127,7 +127,7 @@ func SubmitStory(c *gin.Context) (map[string]string, *e.Error) { followers, err1 := interaction.GetFollowerIDs(post.CreatorID) if err1 == nil { for _, f := range followers { - notification.Send(f, "", models.NotificationPublish, post.ID, post.CreatorID) + notification.Send(f, "", models.NotificationPublish, post.ID, " published a new story", post.CreatorID) } } @@ -135,7 +135,7 @@ func SubmitStory(c *gin.Context) (map[string]string, *e.Error) { followers, err1 = interaction.GetFollowerIDs(post.OwnerID) if err1 == nil { for _, f := range followers { - notification.Send("", f, models.NotificationPublish, post.ID, post.CreatorID) + notification.Send("", f, models.NotificationPublish, post.ID, " published a new story", post.CreatorID) } } } diff --git a/server/pkg/models/notification.go b/server/pkg/models/notification.go index 4ea6f154..d882bf77 100644 --- a/server/pkg/models/notification.go +++ b/server/pkg/models/notification.go @@ -9,6 +9,7 @@ const ( NotificationPublish = 4 NotificationFollow = 5 NotificationReply = 6 + NotificationSystem = 7 ) type Notification struct {