Skip to content

Commit

Permalink
Backend part for sending user call status as video_dial_status_changed
Browse files Browse the repository at this point in the history
  • Loading branch information
nkonev committed Jan 13, 2024
1 parent ec0b3a2 commit 448c0b7
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion event/dto/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type VideoCallInvitation struct {

type VideoDialChanged struct {
UserId int64 `json:"userId"`
Status bool `json:"status"`
Status string `json:"status"`
}

type VideoDialChanges struct {
Expand Down
8 changes: 4 additions & 4 deletions event/graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions event/graph/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion event/graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ type VideoCallInvitationDto {

type VideoDialChanged {
userId: Int64!
status: Boolean!
status: String!
}

type VideoDialChanges {
Expand Down
2 changes: 1 addition & 1 deletion video/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type VideoCallInvitation struct {
// for call owner
type VideoDialChanged struct {
UserId int64 `json:"userId"`
Status bool `json:"status"`
Status string `json:"status"`
}

type VideoDialChanges struct {
Expand Down
18 changes: 14 additions & 4 deletions video/handlers/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (vh *InviteHandler) addToCalling(c echo.Context, callee int64, chatId int64
}

// for better user experience
vh.chatInvitationService.SendInvitations(c.Request().Context(), chatId, userPrincipalDto.UserId, []int64{callee})
vh.chatInvitationService.SendInvitationsWithStatuses(c.Request().Context(), chatId, userPrincipalDto.UserId, getMap([]int64{callee}, status))

err = vh.dialRedisRepository.AddToDialList(c.Request().Context(), callee, chatId, userPrincipalDto.UserId, services.CallStatusInviting)
if err != nil {
Expand Down Expand Up @@ -312,19 +312,27 @@ func (vh *InviteHandler) removeFromCallingList(c echo.Context, chatId int64, use
}

// we send "stop-inviting-for-userPrincipalDto.UserId-signal" to the ownerId (call's owner)
err = vh.dialStatusPublisher.Publish(chatId, usersOfDial, false, ownerId)
err = vh.dialStatusPublisher.Publish(chatId, getMap(usersOfDial, callStatus), ownerId)

if err != nil {
logger.GetLogEntry(c.Request().Context()).Errorf("Error %v", err)
return http.StatusInternalServerError
}

// send the new status immediately
vh.chatInvitationService.SendInvitations(c.Request().Context(), chatId, ownerId, usersOfDial)
vh.chatInvitationService.SendInvitationsWithStatuses(c.Request().Context(), chatId, ownerId, getMap(usersOfDial, callStatus))

return http.StatusOK
}

func getMap(userIds []int64, status string) map[int64]string {
var ret = map[int64]string{}
for _, userId := range userIds {
ret[userId] = status
}
return ret
}

func(vh *InviteHandler) setUserStatus(ctx context.Context, callee, chatId int64, callStatus string) error {
err := vh.dialRedisRepository.SetUserStatus(ctx, callee, callStatus)
if err != nil {
Expand Down Expand Up @@ -443,7 +451,9 @@ func (vh *InviteHandler) SendDialStatusChangedToCallOwner(c echo.Context) error
return c.NoContent(http.StatusOK)
}

err = vh.dialStatusPublisher.Publish(chatId, userIdsToDial, true, userPrincipalDto.UserId)
var statuses = vh.chatDialerService.GetStatuses(c.Request().Context(), userIdsToDial)

err = vh.dialStatusPublisher.Publish(chatId, statuses, userPrincipalDto.UserId)
if err != nil {
Logger.Error(err, "Failed during marshal VideoIsInvitingDto")
return c.NoContent(http.StatusOK)
Expand Down
5 changes: 2 additions & 3 deletions video/producer/rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,11 @@ func NewRabbitInvitePublisher(connection *rabbitmq.Connection) *RabbitInvitePubl

func (rp *RabbitDialStatusPublisher) Publish(
chatId int64,
userIds []int64,
status bool,
userStatuses map[int64]string,
ownerId int64,
) error {
var dials = []*dto.VideoDialChanged{}
for _, userId := range userIds {
for userId, status := range userStatuses {
dials = append(dials, &dto.VideoDialChanged{
UserId: userId,
Status: status,
Expand Down
8 changes: 7 additions & 1 deletion video/services/invitation_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ func NewChatInvitationService(rabbitMqInvitePublisher *producer.RabbitInvitePubl
}
}

func (srv *ChatInvitationService) SendInvitations(ctx context.Context, chatId, ownerId int64, userIdsToDial []int64) {

func (srv *ChatInvitationService) SendInvitationsWithStatuses(ctx context.Context, chatId, ownerId int64, statuses map[int64]string) {
var userIdsToDial []int64 = make([]int64, 0)
for userId, _ := range statuses {
userIdsToDial = append(userIdsToDial, userId)
}

inviteNames, err := srv.chatClient.GetChatNameForInvite(chatId, ownerId, userIdsToDial, ctx)
if err != nil {
GetLogEntry(ctx).Error(err, "Failed during getting chat invite names")
Expand Down
19 changes: 17 additions & 2 deletions video/tasks/chat_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ func (srv *ChatDialerService) makeDial(ctx context.Context, chatId int64) {
return
}

var statuses = srv.GetStatuses(ctx, userIdsToDial)

GetLogEntry(ctx).Infof("Calling userIds %v from chat %v", userIdsToDial, chatId)

srv.chatInvitationService.SendInvitations(ctx, chatId, ownerId, userIdsToDial)
srv.chatInvitationService.SendInvitationsWithStatuses(ctx, chatId, ownerId, statuses)

// send state changes to owner (ownerId) of call
err = srv.dialStatusPublisher.Publish(chatId, userIdsToDial, true, ownerId)
err = srv.dialStatusPublisher.Publish(chatId, statuses, ownerId)
if err != nil {
GetLogEntry(ctx).Error(err, "Failed during marshal VideoIsInvitingDto")
return
Expand Down Expand Up @@ -106,6 +108,19 @@ func (srv *ChatDialerService) checkAndRemoveRedundants(ctx context.Context, chat
}
}

func (srv *ChatDialerService) GetStatuses(ctx context.Context, userIds []int64) map[int64]string {
var ret = map[int64]string{}
for _, userId := range userIds {
status, err := srv.redisService.GetUserCallStatus(ctx, userId)
if err != nil {
GetLogEntry(ctx).Error("An error occured during getting the status for user %", userId)
continue
}
ret[userId] = status
}
return ret
}

type ChatDialerTask struct {
*gointerlock.GoInterval
}
Expand Down

0 comments on commit 448c0b7

Please sign in to comment.