Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nkonev committed Jan 22, 2025
1 parent fd50b27 commit 348f631
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 529 deletions.
135 changes: 7 additions & 128 deletions chat/db/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,6 @@ type Blog struct {
Avatar null.String
}

type ChatWithParticipants struct {
Chat
ParticipantsIds []int64
ParticipantsCount int
IsAdmin bool
}

type ChatWithoutParticipants struct {
Chat
IsAdmin bool
Expand Down Expand Up @@ -190,38 +183,14 @@ func getChatSearchClause(additionalFoundUserIds []int64) string {
)
}

func convertToWithParticipants(ctx context.Context, db CommonOperations, chat *Chat, behalfUserId int64, participantsSize, participantsOffset int) (*ChatWithParticipants, error) {
if ids, err := db.GetParticipantIds(ctx, chat.Id, participantsSize, participantsOffset); err != nil {
return nil, err
} else {
admin, err := db.IsAdmin(ctx, behalfUserId, chat.Id)
if err != nil {
return nil, err
}
participantsCount, err := db.GetParticipantsCount(ctx, chat.Id)
if err != nil {
return nil, err
}
ccc := &ChatWithParticipants{
Chat: *chat,
ParticipantsIds: ids,
IsAdmin: admin,
ParticipantsCount: participantsCount,
}
return ccc, nil
}
}

func convertToWithoutParticipants(ctx context.Context, db CommonOperations, chat *Chat, behalfUserId int64) (*ChatWithParticipants, error) {
func convertToWithoutParticipants(ctx context.Context, db CommonOperations, chat *Chat, behalfUserId int64) (*ChatWithoutParticipants, error) {
admin, err := db.IsAdmin(ctx, behalfUserId, chat.Id)
if err != nil {
return nil, err
}
ccc := &ChatWithParticipants{
Chat: *chat,
ParticipantsIds: []int64{}, // to be set in callee
IsAdmin: admin,
ParticipantsCount: 0, // to be set in callee
ccc := &ChatWithoutParticipants{
Chat: *chat,
IsAdmin: admin,
}
return ccc, nil
}
Expand All @@ -240,28 +209,6 @@ type ParticipantIds struct {
ParticipantIds []int64
}

func convertToWithParticipantsBatch(chat *Chat, participantIdsBatch []*ParticipantIds, isAdminBatch map[int64]bool, participantsCountBatch map[int64]int) (*ChatWithParticipants, error) {
participantsCount := participantsCountBatch[chat.Id]

var participantsIds []int64 = make([]int64, 0)
for _, pidsb := range participantIdsBatch {
if pidsb.ChatId == chat.Id {
participantsIds = pidsb.ParticipantIds
break
}
}

admin := isAdminBatch[chat.Id]

ccc := &ChatWithParticipants{
Chat: *chat,
ParticipantsIds: participantsIds,
IsAdmin: admin,
ParticipantsCount: participantsCount,
}
return ccc, nil
}

func convertToWithoutParticipantsBatch(chat *Chat, isAdminBatch map[int64]bool) (*ChatWithoutParticipants, error) {

admin := isAdminBatch[chat.Id]
Expand Down Expand Up @@ -514,83 +461,15 @@ func getChatsCommon(ctx context.Context, co CommonOperations, participantId int6
return list, nil
}

func getChatsWithParticipantsCommon(ctx context.Context, commonOps CommonOperations, participantId int64, limit int, startingFromItemId *int64, reverse, hasHash bool, searchString string, additionalFoundUserIds []int64, participantsSize, participantsOffset int) ([]*ChatWithParticipants, error) {
var err error
var chats []*Chat

chats, err = getChatsCommon(ctx, commonOps, participantId, limit, startingFromItemId, reverse, hasHash, searchString, additionalFoundUserIds)

if err != nil {
return nil, err
} else {
var chatIds []int64 = make([]int64, 0)
for _, cc := range chats {
chatIds = append(chatIds, cc.Id)
}

fixedParticipantsSize := utils.FixSize(participantsSize)
participantIdsBatch, err := commonOps.GetParticipantIdsBatch(ctx, chatIds, fixedParticipantsSize)
if err != nil {
return nil, err
}

isAdminBatch, err := commonOps.IsAdminBatch(ctx, participantId, chatIds)
if err != nil {
return nil, err
}

participantsCountBatch, err := commonOps.GetParticipantsCountBatch(ctx, chatIds)
if err != nil {
return nil, err
}

list := make([]*ChatWithParticipants, 0)

for _, cc := range chats {
if ccc, err := convertToWithParticipantsBatch(cc, participantIdsBatch, isAdminBatch, participantsCountBatch); err != nil {
return nil, err
} else {
list = append(list, ccc)
}
}
return list, nil
}
}
func (db *DB) GetChatsWithParticipants(ctx context.Context, participantId int64, limit int, startingFromItemId *int64, reverse, hasHash bool, searchString string, additionalFoundUserIds []int64, participantsSize, participantsOffset int) ([]*ChatWithParticipants, error) {
return getChatsWithParticipantsCommon(ctx, db, participantId, limit, startingFromItemId, reverse, hasHash, searchString, additionalFoundUserIds, participantsSize, participantsOffset)
}

func (tx *Tx) GetChatsWithParticipants(ctx context.Context, participantId int64, limit int, startingFromItemId *int64, reverse, hasHash bool, searchString string, additionalFoundUserIds []int64, participantsSize, participantsOffset int) ([]*ChatWithParticipants, error) {
return getChatsWithParticipantsCommon(ctx, tx, participantId, limit, startingFromItemId, reverse, hasHash, searchString, additionalFoundUserIds, participantsSize, participantsOffset)
}

func (tx *Tx) GetChatWithParticipants(ctx context.Context, behalfParticipantId, chatId int64, participantsSize, participantsOffset int) (*ChatWithParticipants, error) {
return getChatWithParticipantsCommon(ctx, tx, behalfParticipantId, chatId, participantsSize, participantsOffset)
}

func (db *DB) GetChatWithParticipants(ctx context.Context, behalfParticipantId, chatId int64, participantsSize, participantsOffset int) (*ChatWithParticipants, error) {
return getChatWithParticipantsCommon(ctx, db, behalfParticipantId, chatId, participantsSize, participantsOffset)
}

func getChatWithParticipantsCommon(ctx context.Context, commonOps CommonOperations, behalfParticipantId, chatId int64, participantsSize, participantsOffset int) (*ChatWithParticipants, error) {
if chat, err := commonOps.GetChat(ctx, behalfParticipantId, chatId); err != nil {
return nil, err
} else if chat == nil {
return nil, nil
} else {
return convertToWithParticipants(ctx, commonOps, chat, behalfParticipantId, participantsSize, participantsOffset)
}
}

func (tx *Tx) GetChatWithoutParticipants(ctx context.Context, behalfParticipantId, chatId int64) (*ChatWithParticipants, error) {
func (tx *Tx) GetChatWithoutParticipants(ctx context.Context, behalfParticipantId, chatId int64) (*ChatWithoutParticipants, error) {
return getChatWithoutParticipantsCommon(ctx, tx, behalfParticipantId, chatId)
}

func (db *DB) GetChatWithoutParticipants(ctx context.Context, behalfParticipantId, chatId int64) (*ChatWithParticipants, error) {
func (db *DB) GetChatWithoutParticipants(ctx context.Context, behalfParticipantId, chatId int64) (*ChatWithoutParticipants, error) {
return getChatWithoutParticipantsCommon(ctx, db, behalfParticipantId, chatId)
}

func getChatWithoutParticipantsCommon(ctx context.Context, commonOps CommonOperations, behalfParticipantId, chatId int64) (*ChatWithParticipants, error) {
func getChatWithoutParticipantsCommon(ctx context.Context, commonOps CommonOperations, behalfParticipantId, chatId int64) (*ChatWithoutParticipants, error) {
if chat, err := commonOps.GetChat(ctx, behalfParticipantId, chatId); err != nil {
return nil, err
} else if chat == nil {
Expand Down
4 changes: 1 addition & 3 deletions chat/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ type CommonOperations interface {
IsAdminBatchByParticipants(ctx context.Context, userIds []int64, chatId int64) ([]UserAdminDbDTO, error)
IsParticipant(ctx context.Context, userId int64, chatId int64) (bool, error)
GetChat(ctx context.Context, participantId, chatId int64) (*Chat, error)
GetChatWithParticipants(ctx context.Context, behalfParticipantId, chatId int64, participantsSize, participantsOffset int) (*ChatWithParticipants, error)
GetChatWithoutParticipants(ctx context.Context, behalfParticipantId, chatId int64) (*ChatWithParticipants, error)
GetChatWithoutParticipants(ctx context.Context, behalfParticipantId, chatId int64) (*ChatWithoutParticipants, error)
GetParticipantsCountBatch(ctx context.Context, chatIds []int64) (map[int64]int, error)
GetMessage(ctx context.Context, chatId int64, userId int64, messageId int64) (*Message, error)
GetUnreadMessagesCount(ctx context.Context, chatId int64, userId int64) (int64, error)
Expand All @@ -72,7 +71,6 @@ type CommonOperations interface {
GetBlogPostsByLimitOffset(ctx context.Context, reverse bool, limit int, offset int) ([]*Blog, error)
GetBlogPostsByChatIds(ctx context.Context, ids []int64) ([]*BlogPost, error)
GetMessageBasic(ctx context.Context, chatId int64, messageId int64) (*MessageBasic, error)
GetChatsWithParticipants(ctx context.Context, participantId int64, limit int, startingFromItemId *int64, reverse, hasHash bool, searchString string, additionalFoundUserIds []int64, participantsSize, participantsOffset int) ([]*ChatWithParticipants, error)
GetChatsWithoutParticipants(ctx context.Context, participantId int64, limit int, startingFromItemId *int64, reverse, hasHash bool, searchString string, additionalFoundUserIds []int64) ([]*ChatWithoutParticipants, error)
CountChatsPerUser(ctx context.Context, userId int64) (int64, error)
FlipReaction(ctx context.Context, userId int64, chatId int64, messageId int64, reaction string) (bool, error)
Expand Down
174 changes: 50 additions & 124 deletions chat/dto/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

// designed to be able to get it from db with a single query
// it means without JOINs, without requesting aaa
type LightChatDto struct {
// it means without requesting aaa and performing a heavy unread messages calculations
type BaseChatDto struct {
Id int64 `json:"id"`
Name string `json:"name"`
Avatar null.String `json:"avatar"` // null for tet-a-tet
Expand Down Expand Up @@ -56,60 +56,7 @@ type AdditionalChatMessagesDto struct {
LastMessagePreview null.String `json:"lastMessagePreview"`
}

type BaseChatDto struct {
Id int64 `json:"id"`
Name string `json:"name"`
Avatar null.String `json:"avatar"`
AvatarBig null.String `json:"avatarBig"`
ShortInfo null.String `json:"shortInfo"`
LastUpdateDateTime time.Time `json:"lastUpdateDateTime"`
ParticipantIds []int64 `json:"participantIds"`
CanEdit null.Bool `json:"canEdit"`
CanDelete null.Bool `json:"canDelete"`
CanLeave null.Bool `json:"canLeave"`
UnreadMessages int64 `json:"unreadMessages"`
CanBroadcast bool `json:"canBroadcast"`
CanVideoKick bool `json:"canVideoKick"`
CanChangeChatAdmins bool `json:"canChangeChatAdmins"`
IsTetATet bool `json:"tetATet"`
CanAudioMute bool `json:"canAudioMute"`
ParticipantsCount int `json:"participantsCount"`
CanResend bool `json:"canResend"`
AvailableToSearch bool `json:"availableToSearch"`
IsResultFromSearch null.Bool `json:"isResultFromSearch"`
Pinned bool `json:"pinned"`
Blog bool `json:"blog"`
LoginColor null.String `json:"loginColor"`
RegularParticipantCanPublishMessage bool `json:"regularParticipantCanPublishMessage"`
LastSeenDateTime null.Time `json:"lastSeenDateTime"`
RegularParticipantCanPinMessage bool `json:"regularParticipantCanPinMessage"`
BlogAbout bool `json:"blogAbout"`
RegularParticipantCanWriteMessage bool `json:"regularParticipantCanWriteMessage"`
CanWriteMessage bool `json:"canWriteMessage"`
}

func (copied *BaseChatDto) SetPersonalizedFields(admin bool, unreadMessages int64, participant bool) {
copied.CanEdit = null.BoolFrom(admin && !copied.IsTetATet)
copied.CanDelete = null.BoolFrom(admin)
copied.CanLeave = null.BoolFrom(!admin && !copied.IsTetATet && participant)
copied.UnreadMessages = unreadMessages
copied.CanVideoKick = admin
copied.CanAudioMute = admin
copied.CanChangeChatAdmins = admin && !copied.IsTetATet
copied.CanBroadcast = admin

if !participant {
copied.IsResultFromSearch = null.BoolFrom(true)
}

copied.CanWriteMessage = true
// see also handlers PostMessage, EditMessage, DeleteMessage
if !copied.RegularParticipantCanWriteMessage && !admin {
copied.CanWriteMessage = false
}
}

func (copied *LightChatDto) SetPersonalizedFieldsLight(admin bool, participant bool) {
func (copied *BaseChatDto) SetPersonalizedFields(admin bool, participant bool) {
copied.CanEdit = null.BoolFrom(admin && !copied.IsTetATet)
copied.CanDelete = null.BoolFrom(admin)
copied.CanLeave = null.BoolFrom(!admin && !copied.IsTetATet && participant)
Expand All @@ -133,74 +80,53 @@ type ChatDeletedDto struct {
Id int64 `json:"id"`
}

type ChatDto struct {
BaseChatDto
Participants []*User `json:"participants"`
}

type ChatDtoWithTetATet interface {
GetId() int64
GetName() string
GetAvatar() null.String
GetIsTetATet() bool
SetName(s string)
SetAvatar(s null.String)
SetShortInfo(s null.String)
SetLoginColor(s null.String)
SetLastSeenDateTime(t null.Time)
}

func (r *ChatDto) GetId() int64 {
return r.Id
}

func (r *ChatDto) GetName() string {
return r.Name
}

func (r *ChatDto) SetName(s string) {
r.Name = s
}

func (r *ChatDto) GetIsTetATet() bool {
return r.IsTetATet
}

func (r *BaseChatDto) GetId() int64 {
return r.Id
}

func (r *BaseChatDto) GetName() string {
return r.Name
}

func (r *BaseChatDto) SetName(s string) {
r.Name = s
}

func (r *BaseChatDto) GetAvatar() null.String {
return r.Avatar
}

func (r *BaseChatDto) SetAvatar(s null.String) {
r.Avatar = s
}

func (r *BaseChatDto) SetShortInfo(s null.String) {
r.ShortInfo = s
}

func (r *BaseChatDto) SetLoginColor(s null.String) {
r.LoginColor = s
}

func (r *BaseChatDto) GetIsTetATet() bool {
return r.IsTetATet
}

func (r *BaseChatDto) SetLastSeenDateTime(t null.Time) {
r.LastSeenDateTime = t
}
//type ChatDtoWithTetATet interface {
// GetId() int64
// GetName() string
// GetAvatar() null.String
// GetIsTetATet() bool
// SetName(s string)
// SetAvatar(s null.String)
// SetShortInfo(s null.String)
// SetLoginColor(s null.String)
// SetLastSeenDateTime(t null.Time)
//}
//
//func (r *BaseChatDto) GetId() int64 {
// return r.Id
//}
//
//func (r *BaseChatDto) GetName() string {
// return r.Name
//}
//
//func (r *BaseChatDto) SetName(s string) {
// r.Name = s
//}
//
//func (r *BaseChatDto) GetAvatar() null.String {
// return r.Avatar
//}
//
//func (r *BaseChatDto) SetAvatar(s null.String) {
// r.Avatar = s
//}
//
//func (r *BaseChatDto) SetShortInfo(s null.String) {
// r.ShortInfo = s
//}
//
//func (r *BaseChatDto) SetLoginColor(s null.String) {
// r.LoginColor = s
//}
//
//func (r *BaseChatDto) SetLastSeenDateTime(t null.Time) {
// r.LastSeenDateTime = t
//}
//
//func (r *BaseChatDto) GetIsTetATet() bool {
// return r.IsTetATet
//}

type ChatName struct {
Name string `json:"name"` // chatName or userName in case tet-a-tet
Expand Down
Loading

0 comments on commit 348f631

Please sign in to comment.