dkforest

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

commit 7a0e811a93878b73d100016f94d598144b470500
parent 341188cd7ded3309c6ff8f1c3191e177f3a16191
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu, 12 Jan 2023 15:23:47 -0800

move code

Diffstat:
Mpkg/database/tableChatRooms.go | 14++++++++++++++
Mpkg/database/tableUserRoomSubscriptions.go | 14--------------
2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/pkg/database/tableChatRooms.go b/pkg/database/tableChatRooms.go @@ -148,6 +148,20 @@ ORDER BY r.id ASC`, userID, userID).Scan(&out).Error return } +func GetUserRoomSubscriptions(userID UserID) (out []ChatRoomAug, err error) { + err = DB.Raw(`SELECT r.*, +COALESCE((rr.read_at < m.created_at), 1) as is_unread +FROM user_room_subscriptions s +INNER JOIN chat_rooms r ON r.id = s.room_id +-- Find last message for room +LEFT JOIN chat_messages m ON m.id = (SELECT max(id) FROM chat_messages WHERE room_id = r.id AND (to_user_id IS NULL OR to_user_id = ?)) +-- Get read record for the authUser & room +LEFT JOIN chat_read_records rr ON rr.user_id = ? AND rr.room_id = r.id +WHERE s.user_id = ? +ORDER BY r.id ASC`, userID, userID, userID).Scan(&out).Error + return +} + func GetOfficialChatRooms() (out []ChatRoom, err error) { err = DB.Where("id IN (1, 2, 3, 4, 14)").Preload("ReadRecord").Find(&out).Error return diff --git a/pkg/database/tableUserRoomSubscriptions.go b/pkg/database/tableUserRoomSubscriptions.go @@ -19,20 +19,6 @@ func (s *UserRoomSubscription) DoSave() { } } -func GetUserRoomSubscriptions(userID UserID) (out []ChatRoomAug, err error) { - err = DB.Raw(`SELECT r.*, -COALESCE((rr.read_at < m.created_at), 1) as is_unread -FROM user_room_subscriptions s -INNER JOIN chat_rooms r ON r.id = s.room_id --- Find last message for room -LEFT JOIN chat_messages m ON m.id = (SELECT max(id) FROM chat_messages WHERE room_id = r.id AND (to_user_id IS NULL OR to_user_id = ?)) --- Get read record for the authUser & room -LEFT JOIN chat_read_records rr ON rr.user_id = ? AND rr.room_id = r.id -WHERE s.user_id = ? -ORDER BY r.id ASC`, userID, userID, userID).Scan(&out).Error - return -} - func SubscribeToRoom(userID UserID, roomID RoomID) (err error) { return DB.Create(&UserRoomSubscription{UserID: userID, RoomID: roomID}).Error }