Skip to content

Commit

Permalink
feat: make all process in rest handler to be syncronous
Browse files Browse the repository at this point in the history
  • Loading branch information
isdzulqor committed Aug 28, 2024
1 parent 2475405 commit f4b0c56
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
19 changes: 9 additions & 10 deletions domain/handler/slackbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ func (s SlackbotHandler) AddToSlack(w http.ResponseWriter, r *http.Request) {
func (s SlackbotHandler) Callback(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

go s.SlackbotService.ProcessOauth2(ctx, r.FormValue("state"), r.FormValue("code"))
s.SlackbotService.ProcessOauth2(ctx, r.FormValue("state"), r.FormValue("code"))
http.Redirect(w, r, s.Config.Site.LandingPage, http.StatusTemporaryRedirect)
return
}

func (s SlackbotHandler) GetEvents(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -92,8 +91,8 @@ func (s SlackbotHandler) GetEvents(w http.ResponseWriter, r *http.Request) {
// event already proceessed
return
}
go s.GoCache.Set(*requestEvent.EventID, "", s.Config.Cache.RequestExpirationTime)
go s.prepareHandleEvent(ctx, *requestEvent.Event, requestEvent.TeamID)
s.GoCache.Set(*requestEvent.EventID, "", s.Config.Cache.RequestExpirationTime)
s.prepareHandleEvent(ctx, *requestEvent.Event, requestEvent.TeamID)
}

func (s SlackbotHandler) validateSlackEvent(requestEvent external.SlackEventRequestModel) error {
Expand Down Expand Up @@ -144,7 +143,7 @@ func (s SlackbotHandler) HandleRTM(ctx context.Context) {
func (s SlackbotHandler) handleEvent(ctx context.Context, user, eventType, slackChannel, incomingMessage string, threadTs *string, teamInfo model.TeamModel) {
defer func() {
if r := recover(); r != nil {
go s.SlackbotService.NotifySlackError(ctx, &teamInfo.ReferenceToken, slackChannel, fmt.Errorf("Failed to handle event! %v", r), false, threadTs)
s.SlackbotService.NotifySlackError(ctx, &teamInfo.ReferenceToken, slackChannel, fmt.Errorf("Failed to handle event! %v", r), false, threadTs)
logging.Logger(ctx).Error(r)
return
}
Expand All @@ -161,7 +160,7 @@ func (s SlackbotHandler) handleEvent(ctx context.Context, user, eventType, slack
// event already proceessed
return
}
go s.GoCache.Set(slackChannel, "", s.Config.Cache.DefaultExpirationTime)
s.GoCache.Set(slackChannel, "", s.Config.Cache.DefaultExpirationTime)
slackStartedMsg := strings.ReplaceAll(model.SlackStartedMessage, "https://cakcuk.io/play", s.Config.Site.PlayPage)
s.SlackbotService.NotifySlackSuccess(ctx, &teamInfo.ReferenceToken, slackChannel, slackStartedMsg, false, false, threadTs)
case SlackEventMemberJoinedChannel:
Expand Down Expand Up @@ -202,11 +201,11 @@ func (s SlackbotHandler) handleEvent(ctx context.Context, user, eventType, slack
cmdResponse, err := s.CommandService.Prepare(ctx, incomingMessage, user, teamInfo.ReferenceID,
botInfo.Name, model.SourceSlack, slackChannel, &teamInfo)
if err != nil {
go s.SlackbotService.NotifySlackError(ctx, &teamInfo.ReferenceToken, slackChannel, err, cmdResponse.IsFileOutput, threadTs)
s.SlackbotService.NotifySlackError(ctx, &teamInfo.ReferenceToken, slackChannel, err, cmdResponse.IsFileOutput, threadTs)
return
}
if cmdResponse.IsHelp {
go s.SlackbotService.NotifySlackSuccess(ctx, &teamInfo.ReferenceToken, slackChannel, cmdResponse.Message, cmdResponse.IsFileOutput, true, threadTs)
s.SlackbotService.NotifySlackSuccess(ctx, &teamInfo.ReferenceToken, slackChannel, cmdResponse.Message, cmdResponse.IsFileOutput, true, threadTs)
return
}
// isNoResponse only work for commmands other than command group
Expand All @@ -217,13 +216,13 @@ func (s SlackbotHandler) handleEvent(ctx context.Context, user, eventType, slack
}
cmdResponse, err = s.CommandService.Exec(ctx, cmdResponse, botInfo.Name, user, slackChannel)
if err != nil {
go s.SlackbotService.NotifySlackError(ctx, &teamInfo.ReferenceToken, slackChannel, err, cmdResponse.IsFileOutput, threadTs)
s.SlackbotService.NotifySlackError(ctx, &teamInfo.ReferenceToken, slackChannel, err, cmdResponse.IsFileOutput, threadTs)
return
}
// isNoResponse only work for commmands other than command group
// command group isNoResponse is working on child command level
if !cmdResponse.IsNoResponse || cmdResponse.Command.GroupName != "" {
go s.SlackbotService.NotifySlackSuccess(ctx, &teamInfo.ReferenceToken, slackChannel, cmdResponse.Message, cmdResponse.IsFileOutput, true, threadTs)
s.SlackbotService.NotifySlackSuccess(ctx, &teamInfo.ReferenceToken, slackChannel, cmdResponse.Message, cmdResponse.IsFileOutput, true, threadTs)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion domain/service/slackbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (s *SlackbotService) ProcessOauth2(ctx context.Context, state, code string)
}

// send onboard message via PM
go s.SendFirstStartedMessage(ctx, stringLib.ReadSafe(oauth2Response.AuthedUser.ID), stringLib.ReadSafe(oauth2Response.AccessToken))
s.SendFirstStartedMessage(ctx, stringLib.ReadSafe(oauth2Response.AuthedUser.ID), stringLib.ReadSafe(oauth2Response.AccessToken))

logging.Logger(ctx).Debug("New workspace installed, data:", jsonLib.ToPrettyNoError(oauth2Response))

Expand Down

0 comments on commit f4b0c56

Please sign in to comment.