mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 15:51:14 +00:00
feat: notifications
This commit is contained in:
@@ -68,3 +68,67 @@ func GetActivityList(page int) ([]model.ActivityView, int, error) {
|
||||
|
||||
return views, totalPages, nil
|
||||
}
|
||||
|
||||
func GetUserNotifications(userID uint, page int) ([]model.ActivityView, int, error) {
|
||||
offset := (page - 1) * pageSize
|
||||
limit := pageSize
|
||||
|
||||
activities, total, err := dao.GetUserNotifications(userID, offset, limit)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var views []model.ActivityView
|
||||
for _, activity := range activities {
|
||||
user, err := dao.GetUserByID(activity.UserID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var comment *model.CommentView
|
||||
var resource *model.ResourceView
|
||||
var file *model.FileView
|
||||
switch activity.Type {
|
||||
case model.ActivityTypeNewComment:
|
||||
c, err := dao.GetCommentByID(activity.RefID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
comment = c.ToView()
|
||||
comment.Content, comment.ContentTruncated = restrictCommentLength(c.Content)
|
||||
case model.ActivityTypeNewResource, model.ActivityTypeUpdateResource:
|
||||
r, err := dao.GetResourceByID(activity.RefID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
rv := r.ToView()
|
||||
resource = &rv
|
||||
case model.ActivityTypeNewFile:
|
||||
f, err := dao.GetFileByID(activity.RefID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
fv := f.ToView()
|
||||
file = fv
|
||||
r, err := dao.GetResourceByID(f.ResourceID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
rv := r.ToView()
|
||||
resource = &rv
|
||||
}
|
||||
view := model.ActivityView{
|
||||
ID: activity.ID,
|
||||
User: user.ToView(),
|
||||
Type: activity.Type,
|
||||
Time: activity.CreatedAt,
|
||||
Comment: comment,
|
||||
Resource: resource,
|
||||
File: file,
|
||||
}
|
||||
views = append(views, view)
|
||||
}
|
||||
|
||||
totalPages := (total + pageSize - 1) / pageSize
|
||||
|
||||
return views, totalPages, nil
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ func CreateComment(req CommentRequest, userID uint, refID uint, ip string, cType
|
||||
return nil, model.NewRequestError("Comment content exceeds maximum length of 1024 characters")
|
||||
}
|
||||
|
||||
var notifyTo uint
|
||||
|
||||
switch cType {
|
||||
case model.CommentTypeResource:
|
||||
resourceExists, err := dao.ExistsResource(refID)
|
||||
@@ -39,12 +41,18 @@ func CreateComment(req CommentRequest, userID uint, refID uint, ip string, cType
|
||||
if !resourceExists {
|
||||
return nil, model.NewNotFoundError("Resource not found")
|
||||
}
|
||||
notifyTo, err = dao.GetResourceOwnerID(refID)
|
||||
if err != nil {
|
||||
log.Error("Error getting resource owner ID:", err)
|
||||
return nil, model.NewInternalServerError("Error getting resource owner ID")
|
||||
}
|
||||
case model.CommentTypeReply:
|
||||
_, err := dao.GetCommentByID(refID)
|
||||
comment, err := dao.GetCommentByID(refID)
|
||||
if err != nil {
|
||||
log.Error("Error getting reply comment:", err)
|
||||
return nil, model.NewNotFoundError("Reply comment not found")
|
||||
}
|
||||
notifyTo = comment.UserID
|
||||
}
|
||||
|
||||
userExists, err := dao.ExistsUserByID(userID)
|
||||
@@ -63,7 +71,7 @@ func CreateComment(req CommentRequest, userID uint, refID uint, ip string, cType
|
||||
log.Error("Error creating comment:", err)
|
||||
return nil, model.NewInternalServerError("Error creating comment")
|
||||
}
|
||||
err = dao.AddNewCommentActivity(userID, c.ID)
|
||||
err = dao.AddNewCommentActivity(userID, c.ID, notifyTo)
|
||||
if err != nil {
|
||||
log.Error("Error creating comment activity:", err)
|
||||
}
|
||||
|
||||
@@ -390,3 +390,11 @@ func validateUsername(username string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ResetUserNotificationsCount(userID uint) error {
|
||||
return dao.ResetUserNotificationsCount(userID)
|
||||
}
|
||||
|
||||
func GetUserNotificationsCount(userID uint) (uint, error) {
|
||||
return dao.GetUserNotificationCount(userID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user