dkforest

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

tableUserRoomSubscriptions.go (864B)


      1 package database
      2 
      3 import (
      4 	"time"
      5 
      6 	"github.com/sirupsen/logrus"
      7 )
      8 
      9 type UserRoomSubscription struct {
     10 	UserID    UserID
     11 	RoomID    RoomID
     12 	CreatedAt time.Time
     13 	Room      ChatRoom
     14 }
     15 
     16 func (s *UserRoomSubscription) DoSave(db *DkfDB) {
     17 	if err := db.db.Save(s).Error; err != nil {
     18 		logrus.Error(err)
     19 	}
     20 }
     21 
     22 func (d *DkfDB) SubscribeToRoom(userID UserID, roomID RoomID) (err error) {
     23 	return d.db.Create(&UserRoomSubscription{UserID: userID, RoomID: roomID}).Error
     24 }
     25 
     26 func (d *DkfDB) UnsubscribeFromRoom(userID UserID, roomID RoomID) (err error) {
     27 	return d.db.Delete(&UserRoomSubscription{}, "user_id = ? AND room_id = ?", userID, roomID).Error
     28 }
     29 
     30 func (d *DkfDB) IsUserSubscribedToRoom(userID UserID, roomID RoomID) bool {
     31 	var count int64
     32 	d.db.Model(UserRoomSubscription{}).Where("user_id = ? AND room_id = ?", userID, roomID).Count(&count)
     33 	return count == 1
     34 }