dkforest

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

tableChatInbox.go (2440B)


      1 package database
      2 
      3 import (
      4 	"time"
      5 
      6 	"github.com/sirupsen/logrus"
      7 )
      8 
      9 type ChatInboxMessage struct {
     10 	ID            int64
     11 	Message       string
     12 	RoomID        RoomID
     13 	UserID        UserID
     14 	ToUserID      UserID
     15 	ChatMessageID *int64
     16 	IsRead        bool
     17 	IsPm          bool
     18 	Moderators    bool
     19 	CreatedAt     time.Time
     20 	User          User
     21 	ToUser        User
     22 	Room          ChatRoom
     23 }
     24 
     25 func (d *DkfDB) DeleteOldChatInboxMessages() {
     26 	if err := d.db.Delete(ChatInboxMessage{}, "created_at < date('now', '-90 Day')").Error; err != nil {
     27 		logrus.Error(err)
     28 	}
     29 }
     30 
     31 func (d *DkfDB) GetUserChatInboxMessages(userID UserID) (msgs []ChatInboxMessage, err error) {
     32 	err = d.db.Order("id DESC").
     33 		Limit(50).
     34 		Preload("User").
     35 		Preload("ToUser").
     36 		Preload("Room").
     37 		Find(&msgs, "to_user_id = ?", userID).Error
     38 	var ids []int64
     39 	for _, msg := range msgs {
     40 		ids = append(ids, msg.ID)
     41 	}
     42 	if err := d.db.Model(&ChatInboxMessage{}).Where("id IN (?)", ids).UpdateColumn("is_read", true).Error; err != nil {
     43 		logrus.Error(err)
     44 	}
     45 	return
     46 }
     47 
     48 func (d *DkfDB) GetUserChatInboxMessagesSent(userID UserID) (msgs []ChatInboxMessage, err error) {
     49 	err = d.db.Order("id DESC").
     50 		Limit(50).
     51 		Preload("User").
     52 		Preload("ToUser").
     53 		Preload("Room").
     54 		Find(&msgs, "user_id = ?", userID).Error
     55 	return
     56 }
     57 
     58 func (d *DkfDB) DeleteChatInboxMessageByID(messageID int64) error {
     59 	return d.db.Where("id = ?", messageID).Delete(&ChatInboxMessage{}).Error
     60 }
     61 
     62 func (d *DkfDB) DeleteChatInboxMessageByChatMessageID(chatMessageID int64) error {
     63 	return d.db.Where("chat_message_id = ?", chatMessageID).Delete(&ChatInboxMessage{}).Error
     64 }
     65 
     66 func (d *DkfDB) DeleteAllChatInbox(userID UserID) error {
     67 	return d.db.Where("to_user_id = ?", userID).Delete(&ChatInboxMessage{}).Error
     68 }
     69 
     70 func (d *DkfDB) DeleteUserChatInboxMessages(userID UserID) error {
     71 	return d.db.Where("user_id = ?", userID).Delete(&ChatInboxMessage{}).Error
     72 }
     73 
     74 func (d *DkfDB) CreateInboxMessage(msg string, roomID RoomID, fromUserID, toUserID UserID, isPm, moderators bool, msgID *int64) {
     75 	inbox := ChatInboxMessage{Message: msg, RoomID: roomID, UserID: fromUserID, ToUserID: toUserID, IsPm: isPm, Moderators: moderators, ChatMessageID: msgID}
     76 	if err := d.db.Create(&inbox).Error; err != nil {
     77 		logrus.Error(err)
     78 	}
     79 }
     80 
     81 func (d *DkfDB) GetUserInboxMessagesCount(userID UserID) (count int64) {
     82 	d.db.Table("chat_inbox_messages").Where("to_user_id = ? AND is_read = ?", userID, false).Count(&count)
     83 	return
     84 }