mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 23:51:15 +00:00
feat: notifications
This commit is contained in:
@@ -2,9 +2,10 @@ package dao
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"nysoure/server/model"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func AddNewResourceActivity(userID, resourceID uint) error {
|
||||
@@ -42,13 +43,20 @@ func AddUpdateResourceActivity(userID, resourceID uint) error {
|
||||
return db.Create(activity).Error
|
||||
}
|
||||
|
||||
func AddNewCommentActivity(userID, commentID uint) error {
|
||||
activity := &model.Activity{
|
||||
UserID: userID,
|
||||
Type: model.ActivityTypeNewComment,
|
||||
RefID: commentID,
|
||||
}
|
||||
return db.Create(activity).Error
|
||||
func AddNewCommentActivity(userID, commentID, notifyTo uint) error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
activity := &model.Activity{
|
||||
UserID: userID,
|
||||
Type: model.ActivityTypeNewComment,
|
||||
RefID: commentID,
|
||||
NotifyTo: notifyTo,
|
||||
}
|
||||
err := tx.Create(activity).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Model(&model.User{}).Where("id = ?", notifyTo).UpdateColumn("unread_notifications_count", gorm.Expr("unread_notifications_count + ?", 1)).Error
|
||||
})
|
||||
}
|
||||
|
||||
func AddNewFileActivity(userID, fileID uint) error {
|
||||
@@ -82,3 +90,18 @@ func GetActivityList(offset, limit int) ([]model.Activity, int, error) {
|
||||
|
||||
return activities, int(total), nil
|
||||
}
|
||||
|
||||
func GetUserNotifications(userID uint, offset, limit int) ([]model.Activity, int, error) {
|
||||
var activities []model.Activity
|
||||
var total int64
|
||||
|
||||
if err := db.Model(&model.Activity{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if err := db.Where("notify_to = ?", userID).Offset(offset).Limit(limit).Order("id DESC").Find(&activities).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return activities, int(total), nil
|
||||
}
|
||||
|
||||
@@ -693,3 +693,14 @@ func UpdateResourceImage(resourceID, oldImageID, newImageID uint) error {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func GetResourceOwnerID(resourceID uint) (uint, error) {
|
||||
var uid uint
|
||||
if err := db.Model(&model.Resource{}).Select("user_id").Where("id = ?", resourceID).First(&uid).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return 0, model.NewNotFoundError("Resource not found")
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
@@ -2,8 +2,9 @@ package dao
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"nysoure/server/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func CreateUser(username string, hashedPassword []byte) (model.User, error) {
|
||||
@@ -132,3 +133,15 @@ func DeleteUser(id uint) error {
|
||||
}
|
||||
return db.Delete(&model.User{}, id).Error
|
||||
}
|
||||
|
||||
func ResetUserNotificationsCount(userID uint) error {
|
||||
return db.Model(&model.User{}).Where("id = ?", userID).Update("unread_notifications_count", 0).Error
|
||||
}
|
||||
|
||||
func GetUserNotificationCount(userID uint) (uint, error) {
|
||||
var count uint
|
||||
if err := db.Model(&model.User{}).Where("id = ?", userID).Select("unread_notifications_count").Scan(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user