Skip to content

Commit

Permalink
lighten db interface, /internal/basic/:id
Browse files Browse the repository at this point in the history
  • Loading branch information
nkonev committed Jan 22, 2025
1 parent 68de20b commit 8ccadfa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 43 deletions.
32 changes: 0 additions & 32 deletions chat/db/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,6 @@ func convertToWithParticipants(ctx context.Context, db CommonOperations, chat *C
}
}

func convertToWithoutParticipants(ctx context.Context, db CommonOperations, chat *Chat, behalfUserId int64) (*ChatWithParticipants, 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
}
return ccc, nil
}

type ChatQueryByLimitOffset struct {
Limit int
Offset int
Expand Down Expand Up @@ -566,24 +552,6 @@ func getChatWithParticipantsCommon(ctx context.Context, commonOps CommonOperatio
}
}

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

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

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

func (db *DB) CountChats(ctx context.Context) (int64, error) {
var count int64
row := db.QueryRowContext(ctx, "SELECT count(*) FROM chat")
Expand Down
1 change: 0 additions & 1 deletion chat/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ type CommonOperations interface {
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)
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 Down
26 changes: 17 additions & 9 deletions chat/handlers/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -1902,20 +1902,28 @@ func (ch *ChatHandler) GetBasicInfo(c echo.Context) error {
if err != nil {
return err
}
behalfUserId, err := GetQueryParamAsInt64(c, "userId")
if err != nil {
return err
}

chat, err := ch.db.GetChatWithParticipants(c.Request().Context(), behalfUserId, chatId, utils.FixSize(0), utils.FixPage(0))
ret, err := db.TransactWithResult(c.Request().Context(), ch.db, func(tx *db.Tx) (dto.BasicChatDto, error) {
chatBasic, err := tx.GetChatBasic(c.Request().Context(), chatId)
if err != nil {
return dto.BasicChatDto{}, err
}

participantIds, err := tx.GetParticipantIds(c.Request().Context(), chatId, utils.FixSize(0), utils.FixPage(0))
if err != nil {
return dto.BasicChatDto{}, err
}

ret := dto.BasicChatDto{
TetATet: chatBasic.IsTetATet,
ParticipantIds: participantIds,
}
return ret, nil
})
if err != nil {
return err
}

ret := dto.BasicChatDto{
TetATet: chat.TetATet,
ParticipantIds: chat.ParticipantsIds,
}
return c.JSON(http.StatusOK, ret)
}

Expand Down
2 changes: 1 addition & 1 deletion video/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func (h *RestClient) GetS3(c context.Context, filename string, chatId int64, use

func (h *RestClient) GetBasicChatInfo(c context.Context, chatId int64, userId int64) (*dto.BasicChatDto, error) {
contentType := "application/json;charset=UTF-8"
fullUrl := h.chatBaseUrl + h.chatBasicInfoPath + "/" + utils.Int64ToString(chatId) + "?userId=" + utils.Int64ToString(userId)
fullUrl := h.chatBaseUrl + h.chatBasicInfoPath + "/" + utils.Int64ToString(chatId)

requestHeaders := map[string][]string{
"Accept-Encoding": {"gzip, deflate"},
Expand Down

0 comments on commit 8ccadfa

Please sign in to comment.