dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

tableNotifications.go (3128B)


      1 package database
      2 
      3 import (
      4 	"time"
      5 
      6 	"github.com/sirupsen/logrus"
      7 )
      8 
      9 type Notification struct {
     10 	ID        int64
     11 	Message   string
     12 	UserID    UserID
     13 	IsRead    bool
     14 	ReadAt    *time.Time
     15 	CreatedAt time.Time
     16 	User      User
     17 }
     18 
     19 type SessionNotification struct {
     20 	ID           int64
     21 	Message      string
     22 	SessionToken string
     23 	IsRead       bool
     24 	ReadAt       *time.Time
     25 	CreatedAt    time.Time
     26 }
     27 
     28 func (d *DkfDB) DeleteOldSessionNotifications() {
     29 	if err := d.db.Delete(SessionNotification{}, "created_at < date('now', '-90 Day')").Error; err != nil {
     30 		logrus.Error(err)
     31 	}
     32 }
     33 
     34 func (d *DkfDB) GetUserNotifications(userID UserID) (msgs []Notification, err error) {
     35 	err = d.db.Order("id DESC").
     36 		Limit(50).
     37 		Preload("User").
     38 		Find(&msgs, "user_id = ?", userID).Error
     39 	var ids []int64
     40 	for _, msg := range msgs {
     41 		ids = append(ids, msg.ID)
     42 	}
     43 	now := time.Now()
     44 	if err := d.db.Model(&Notification{}).Where("id IN (?)", ids).
     45 		Updates(map[string]any{"is_read": true, "read_at": &now}).Error; err != nil {
     46 		logrus.Error(err)
     47 	}
     48 	return
     49 }
     50 
     51 func (d *DkfDB) GetUserSessionNotifications(sessionToken string) (msgs []SessionNotification, err error) {
     52 	err = d.db.Order("session_notifications.id DESC").
     53 		Limit(50).
     54 		Joins("INNER JOIN sessions s ON s.token = session_token").
     55 		Joins("INNER JOIN users u ON u.id = s.user_id").
     56 		Find(&msgs, "session_token = ?", sessionToken).Error
     57 	var ids []int64
     58 	for _, msg := range msgs {
     59 		ids = append(ids, msg.ID)
     60 	}
     61 	now := time.Now()
     62 	if err := d.db.Table("session_notifications").Where("id IN (?)", ids).
     63 		Updates(map[string]any{"is_read": true, "read_at": &now}).Error; err != nil {
     64 		logrus.Error(err)
     65 	}
     66 	return
     67 }
     68 
     69 func (d *DkfDB) DeleteNotificationByID(notificationID int64) error {
     70 	return d.db.Where("id = ?", notificationID).Delete(&Notification{}).Error
     71 }
     72 
     73 func (d *DkfDB) DeleteSessionNotificationByID(sessionNotificationID int64) error {
     74 	return d.db.Where("id = ?", sessionNotificationID).Delete(&SessionNotification{}).Error
     75 }
     76 
     77 func (d *DkfDB) DeleteAllNotifications(userID UserID) error {
     78 	return d.db.Where("user_id = ?", userID).Delete(&Notification{}).Error
     79 }
     80 
     81 func (d *DkfDB) CreateNotification(msg string, userID UserID) {
     82 	inbox := Notification{Message: msg, UserID: userID, IsRead: false}
     83 	if err := d.db.Create(&inbox).Error; err != nil {
     84 		logrus.Error(err)
     85 	}
     86 }
     87 
     88 func (d *DkfDB) GetUserNotificationsCount(userID UserID) (count int64) {
     89 	d.db.Table("notifications").Where("user_id = ? AND is_read = ?", userID, false).Count(&count)
     90 	return
     91 }
     92 
     93 func (d *DkfDB) GetUserSessionNotificationsCount(sessionToken string) (count int64) {
     94 	d.db.Table("session_notifications").Where("session_token = ? AND is_read = ?", sessionToken, false).Count(&count)
     95 	return
     96 }
     97 
     98 func (d *DkfDB) CreateSessionNotification(msg string, sessionToken string) {
     99 	inbox := SessionNotification{Message: msg, SessionToken: sessionToken, IsRead: false}
    100 	if err := d.db.Create(&inbox).Error; err != nil {
    101 		logrus.Error(err)
    102 	}
    103 }
    104 
    105 func (d *DkfDB) DeleteAllSessionNotifications(sessionToken string) error {
    106 	return d.db.Where("session_token = ?", sessionToken).Delete(&SessionNotification{}).Error
    107 }