Skip to content

Commit

Permalink
getChatWithoutPersonalization()
Browse files Browse the repository at this point in the history
  • Loading branch information
nkonev committed Jan 22, 2025
1 parent d865f69 commit 25d92be
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
4 changes: 3 additions & 1 deletion chat/dto/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type BaseChatDto struct {
CanWriteMessage bool `json:"canWriteMessage"`
}

func (copied *BaseChatDto) SetPersonalizedFields(admin bool, unreadMessages int64, participant bool) {
func (copied *BaseChatDto) SetPersonalizedFields(admin bool, unreadMessages int64, participant bool, pinned bool) {
copied.CanEdit = null.BoolFrom(admin && !copied.IsTetATet)
copied.CanDelete = null.BoolFrom(admin)
copied.CanLeave = null.BoolFrom(!admin && !copied.IsTetATet && participant)
Expand All @@ -56,6 +56,8 @@ func (copied *BaseChatDto) SetPersonalizedFields(admin bool, unreadMessages int6
if !copied.RegularParticipantCanWriteMessage && !admin {
copied.CanWriteMessage = false
}

copied.Pinned = pinned
}

type ChatDeletedDto struct {
Expand Down
96 changes: 74 additions & 22 deletions chat/handlers/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (ch *ChatHandler) getChats(ctx context.Context, tx *db.Tx, userId int64, si
messages := unreadMessageBatch[cc.Id]
isParticipant := membership[cc.Id]

cd := convertToDto(cc, []*dto.User{}, messages, isParticipant)
cd := convertToDto(cc, true, []*dto.User{}, messages, isParticipant)

chatDtos = append(chatDtos, cd)
}
Expand All @@ -140,7 +140,7 @@ func (ch *ChatHandler) getChats(ctx context.Context, tx *db.Tx, userId int64, si
user := users[participantId]
if user != nil {
chatDto.Participants = append(chatDto.Participants, user)
utils.ReplaceChatNameToLoginForTetATet(chatDto, user, userId, len(chatDto.ParticipantIds) == 1)
utils.ReplaceForTetATet(chatDto, user, userId, len(chatDto.ParticipantIds) == 1)
}
}
}
Expand Down Expand Up @@ -237,13 +237,14 @@ func (ch *ChatHandler) Filter(c echo.Context) error {
})
}

func getChat(
func getChatCommon(
ctx context.Context,
lgr *logger.Logger,
dbR db.CommonOperations,
restClient *client.RestClient,
chatId int64,
behalfParticipantId int64,
performPersonalization bool,
participantsSize, participantsOffset int,
) (*dto.ChatDto, error) {
fixedParticipantsSize := utils.FixSize(participantsSize)
Expand All @@ -264,25 +265,33 @@ func getChat(
lgr.WithTracing(ctx).Warn("Error during getting users from aaa")
}

unreadMessages, err := dbR.GetUnreadMessagesCount(ctx, cc.Id, behalfParticipantId)
if err != nil {
return nil, err
var unreadMessages int64
if performPersonalization {
unreadMessages, err = dbR.GetUnreadMessagesCount(ctx, cc.Id, behalfParticipantId)
if err != nil {
return nil, err
}
}

isParticipant, err := dbR.IsParticipant(ctx, behalfParticipantId, chatId)
if err != nil {
return nil, err
}

chatDto := convertToDto(cc, users, unreadMessages, isParticipant)
chatDto := convertToDto(cc, performPersonalization, users, unreadMessages, isParticipant)

if chatDto.IsTetATet {
for _, participant := range users {

isSingleParticipant := len(cc.ParticipantsIds) == 1

utils.ReplaceChatNameToLoginForTetATet(chatDto, participant, behalfParticipantId, isSingleParticipant)
utils.ReplaceForTetATet(chatDto, participant, behalfParticipantId, isSingleParticipant)

// in case tet-a-tet chats most of the operations are prohibited
// only create tet-a-tet is allowed
// behalfParticipantId is an initiator
// so participant.Id != behalfParticipantId == true only for opposite user
// so only in this case we set LastSeenDateTime
// leave LastSeenDateTime not null only if the opposite user isn't online
if participant.Id != behalfParticipantId {
if participant.Id != behalfParticipantId && !isSingleParticipant {
Expand All @@ -307,6 +316,48 @@ func getChat(
return chatDto, nil
}

func getChatWithoutPersonalization(
ctx context.Context,
lgr *logger.Logger,
dbR db.CommonOperations,
restClient *client.RestClient,
chatId int64,
behalfParticipantId int64,
participantsSize, participantsOffset int,
) (*dto.ChatDto, error) {
return getChatCommon(
ctx,
lgr,
dbR,
restClient,
chatId,
behalfParticipantId,
false,
participantsSize, participantsOffset,
)
}

func getChat(
ctx context.Context,
lgr *logger.Logger,
dbR db.CommonOperations,
restClient *client.RestClient,
chatId int64,
behalfParticipantId int64,
participantsSize, participantsOffset int,
) (*dto.ChatDto, error) {
return getChatCommon(
ctx,
lgr,
dbR,
restClient,
chatId,
behalfParticipantId,
true,
participantsSize, participantsOffset,
)
}

func (ch *ChatHandler) GetChat(c echo.Context) error {
var userPrincipalDto, ok = c.Get(utils.USER_PRINCIPAL_DTO).(*auth.AuthResult)
if !ok {
Expand Down Expand Up @@ -420,7 +471,7 @@ func (ch *ChatHandler) IsFreshChatsPage(c echo.Context) error {
})
}

func convertToDto(c *db.ChatWithParticipants, users []*dto.User, unreadMessages int64, participant bool) *dto.ChatDto {
func convertToDto(c *db.ChatWithParticipants, performPersonalization bool, users []*dto.User, unreadMessages int64, participant bool) *dto.ChatDto {
b := dto.BaseChatDto{
Id: c.Id,
Name: c.Title,
Expand All @@ -430,7 +481,6 @@ func convertToDto(c *db.ChatWithParticipants, users []*dto.User, unreadMessages
IsTetATet: c.TetATet,
CanResend: c.CanResend,
AvailableToSearch: c.AvailableToSearch,
Pinned: c.Pinned,
// see also services/events.go:75 chatNotifyCommon()

ParticipantsCount: c.ParticipantsCount,
Expand All @@ -442,7 +492,9 @@ func convertToDto(c *db.ChatWithParticipants, users []*dto.User, unreadMessages
RegularParticipantCanWriteMessage: c.RegularParticipantCanWriteMessage,
}

b.SetPersonalizedFields(c.IsAdmin, unreadMessages, participant)
if performPersonalization {
b.SetPersonalizedFields(c.IsAdmin, unreadMessages, participant, c.Pinned)
}

// set participant order as in c.ParticipantsIds
orderedParticipants := make([]*dto.User, 0)
Expand Down Expand Up @@ -518,7 +570,7 @@ func (ch *ChatHandler) CreateChat(c echo.Context) error {
}

errOuter = db.Transact(c.Request().Context(), ch.db, func(tx *db.Tx) error {
chatDto, err := getChat(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, userPrincipalDto.UserId, 0, 0)
chatDto, err := getChatWithoutPersonalization(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, userPrincipalDto.UserId, 0, 0)
if err != nil {
return err
}
Expand Down Expand Up @@ -681,14 +733,14 @@ func (ch *ChatHandler) EditChat(c echo.Context) error {
return err
}

chatDto, err := getChat(c.Request().Context(), ch.lgr, tx, ch.restClient, bindTo.Id, userPrincipalDto.UserId, 0, 0)
chatDto, err := getChatWithoutPersonalization(c.Request().Context(), ch.lgr, tx, ch.restClient, bindTo.Id, userPrincipalDto.UserId, 0, 0)
if err != nil {
return err
}

var oldBlogAboutChatDto *dto.ChatDto
if oldBlogAboutChatId != nil {
oldBlogAboutChatDto, err = getChat(c.Request().Context(), ch.lgr, tx, ch.restClient, *oldBlogAboutChatId, userPrincipalDto.UserId, 0, 0)
oldBlogAboutChatDto, err = getChatWithoutPersonalization(c.Request().Context(), ch.lgr, tx, ch.restClient, *oldBlogAboutChatId, userPrincipalDto.UserId, 0, 0)
if err != nil {
return err
}
Expand Down Expand Up @@ -756,7 +808,7 @@ func (ch *ChatHandler) LeaveChat(c echo.Context) error {
if err != nil {
return err
}
if chatDto, err := getChat(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, firstUser, 0, 0); err != nil {
if chatDto, err := getChatWithoutPersonalization(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, firstUser, 0, 0); err != nil {
return err
} else {

Expand Down Expand Up @@ -831,7 +883,7 @@ func (ch *ChatHandler) JoinChat(c echo.Context) error {
if err != nil {
return err
}
chatDto, err := getChat(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, firstUser, 0, 0)
chatDto, err := getChatWithoutPersonalization(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, firstUser, 0, 0)
if err != nil {
return err
}
Expand Down Expand Up @@ -932,7 +984,7 @@ func (ch *ChatHandler) ChangeParticipant(c echo.Context) error {
return err
}

chatDto, err := getChat(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, userPrincipalDto.UserId, 0, 0)
chatDto, err := getChatWithoutPersonalization(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, userPrincipalDto.UserId, 0, 0)
if err != nil {
return err
}
Expand Down Expand Up @@ -982,7 +1034,7 @@ func (ch *ChatHandler) PinChat(c echo.Context) error {
return err
}

chatDto, err := getChat(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, userPrincipalDto.UserId, 0, 0)
chatDto, err := getChatWithoutPersonalization(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, userPrincipalDto.UserId, 0, 0)
if err != nil {
return err
}
Expand Down Expand Up @@ -1032,7 +1084,7 @@ func (ch *ChatHandler) DeleteParticipant(c echo.Context) error {
return errOuter
}
errOuter = db.Transact(c.Request().Context(), ch.db, func(tx *db.Tx) error {
chatDto, err := getChat(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, userPrincipalDto.UserId, 0, 0)
chatDto, err := getChatWithoutPersonalization(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, userPrincipalDto.UserId, 0, 0)
if err != nil {
return err
}
Expand Down Expand Up @@ -1167,7 +1219,7 @@ func (ch *ChatHandler) AddParticipants(c echo.Context) error {
return err
}

chatDto, err := getChat(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, userPrincipalDto.UserId, 0, 0)
chatDto, err := getChatWithoutPersonalization(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId, userPrincipalDto.UserId, 0, 0)
if err != nil {
return err
}
Expand Down Expand Up @@ -1661,7 +1713,7 @@ func (ch *ChatHandler) TetATet(c echo.Context) error {
}
}

chatDto, err := getChat(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId2, userPrincipalDto.UserId, 0, 0)
chatDto, err := getChatWithoutPersonalization(c.Request().Context(), ch.lgr, tx, ch.restClient, chatId2, userPrincipalDto.UserId, 0, 0)
if err != nil {
return err
}
Expand Down Expand Up @@ -1886,7 +1938,7 @@ func (ch *ChatHandler) GetNameForInvite(c echo.Context) error {
IsTetATet: chat.TetATet,
Avatar: chat.Avatar,
}
utils.ReplaceChatNameToLoginForTetATet(
utils.ReplaceForTetATet(
sch,
&meAsUser,
user.Id,
Expand Down
4 changes: 2 additions & 2 deletions chat/handlers/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ func (mc *MessageHandler) PostMessage(c echo.Context) error {
}

errOuter = db.Transact(c.Request().Context(), mc.db, func(tx *db.Tx) error {
chatDto, err := getChat(c.Request().Context(), mc.lgr, tx, mc.restClient, chatId, userPrincipalDto.UserId, 0, 0)
chatDto, err := getChatWithoutPersonalization(c.Request().Context(), mc.lgr, tx, mc.restClient, chatId, userPrincipalDto.UserId, 0, 0)
if err != nil {
return err
}
Expand Down Expand Up @@ -809,7 +809,7 @@ func (mc *MessageHandler) PostMessage(c echo.Context) error {
IsTetATet: chatDto.IsTetATet,
Avatar: chatDto.Avatar,
}
utils.ReplaceChatNameToLoginForTetATet(
utils.ReplaceForTetATet(
sch,
&meAsUser,
participantId,
Expand Down
7 changes: 3 additions & 4 deletions chat/services/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ func (not *Events) chatNotifyCommon(ctx context.Context, userIds []int64, newCha
}

// see also handlers/chat.go:199 convertToDto()
copied.SetPersonalizedFields(areAdminsMap[participantId], unreadMessages[participantId], overrideIsParticipant)

// override pinned personally for participantId
copied.Pinned = isChatPinnedMap[participantId]
copied.SetPersonalizedFields(areAdminsMap[participantId], unreadMessages[participantId], overrideIsParticipant, isChatPinnedMap[participantId])

// set chat name and avatar for tet-a-tet
for _, participant := range copied.Participants {
utils.ReplaceChatNameToLoginForTetATet(copied, participant, participantId, isSingleParticipant)
utils.ReplaceForTetATet(copied, participant, participantId, isSingleParticipant)
}

err = not.rabbitEventPublisher.Publish(ctx, dto.GlobalUserEvent{
Expand Down
4 changes: 2 additions & 2 deletions chat/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ func SecondsToStringMilliseconds(seconds int64) string {
return fmt.Sprintf("%v000", seconds)
}

func ReplaceChatNameToLoginForTetATet(chatDto dto.ChatDtoWithTetATet, participant *dto.User, behalfParticipantId int64, isSingleParticipant bool) {
func ReplaceForTetATet(chatDto dto.ChatDtoWithTetATet, participant *dto.User, behalfParticipantId int64, isSingleTetATetParticipant bool) {
if chatDto.GetIsTetATet() {
if participant.Id != behalfParticipantId || isSingleParticipant {
if participant.Id != behalfParticipantId || isSingleTetATetParticipant {
chatDto.SetName(participant.Login)
chatDto.SetAvatar(participant.Avatar)
chatDto.SetShortInfo(participant.ShortInfo)
Expand Down

0 comments on commit 25d92be

Please sign in to comment.