Messaging/pkg/internal/services/direct_channels.go

26 lines
838 B
Go
Raw Normal View History

package services
import (
"fmt"
2024-11-02 05:23:27 +00:00
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
2024-09-11 15:58:02 +00:00
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
)
2024-11-02 05:23:27 +00:00
func GetDirectChannelByUser(user authm.Account, other authm.Account) (models.Channel, error) {
memberTable := "channel_members"
channelTable := "channels"
var channel models.Channel
if err := database.C.Preload("Members").
Where("type = ?", models.ChannelTypeDirect).
Joins(fmt.Sprintf("JOIN %s cm1 ON cm1.channel_id = %s.id AND cm1.account_id = ?", memberTable, channelTable), user.ID).
Joins(fmt.Sprintf("JOIN %s cm2 ON cm2.channel_id = %s.id AND cm2.account_id = ?", memberTable, channelTable), other.ID).
First(&channel).Error; err != nil {
return channel, err
} else {
return channel, nil
}
}