From 442add4a29372a8f264eb54d0786bb02bcffab63 Mon Sep 17 00:00:00 2001 From: Giovanni Harting <539@idlegandalf.com> Date: Sat, 12 Feb 2022 02:58:35 +0100 Subject: [PATCH] fixed translations being sent regardless of setting --- main.go | 112 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 45 deletions(-) diff --git a/main.go b/main.go index cbf60e2..572f6ea 100644 --- a/main.go +++ b/main.go @@ -687,61 +687,83 @@ func getMatchChat(w http.ResponseWriter, r *http.Request) { } resp := map[string][]*utils.ChatResponse{} - err = rdc.Get(context.Background(), fmt.Sprintf(utils.MatchChatCacheKey, matchId, lang.String()), &resp) - if err != nil { - tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background()) + if translate { + err = rdc.Get(context.Background(), fmt.Sprintf(utils.MatchChatCacheKey, matchId, lang.String()), &resp) if err != nil { - log.Infof("[GMC] match %d not found: %+v", matchId, err) - w.WriteHeader(http.StatusNotFound) - return - } - - for _, stat := range tStats { - steamid := strconv.FormatUint(stat.Edges.MatchPlayer.PlayerStats, 10) - if _, ok := resp[steamid]; !ok { - resp[steamid] = make([]*utils.ChatResponse, 0) + tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background()) + if err != nil { + log.Infof("[GMC] match %d not found: %+v", matchId, err) + w.WriteHeader(http.StatusNotFound) + return } - if translate { - translated, srcLang, err := utils.TranslateWithDeepL(stat.Message, lang.String(), - conf.DeepL.BaseURL, conf.DeepL.APIKey, conf.DeepL.Timeout) - if err != nil { - log.Warningf("[GMC] Unable to translate %s with DeepL: %v", stat.Message, err) - goto sendNormalResp + for _, stat := range tStats { + steamid := strconv.FormatUint(stat.Edges.MatchPlayer.PlayerStats, 10) + if _, ok := resp[steamid]; !ok { + resp[steamid] = make([]*utils.ChatResponse, 0) } - if srcLang == lang.String() { - goto sendNormalResp + if translate { + translated, srcLang, err := utils.TranslateWithDeepL(stat.Message, lang.String(), + conf.DeepL.BaseURL, conf.DeepL.APIKey, conf.DeepL.Timeout) + if err != nil { + log.Warningf("[GMC] Unable to translate %s with DeepL: %v", stat.Message, err) + goto sendNormalResp + } + + if srcLang == lang.String() { + goto sendNormalResp + } + + resp[steamid] = append(resp[steamid], &utils.ChatResponse{ + Message: translated, + AllChat: stat.AllChat, + Tick: stat.Tick, + TranslatedFrom: srcLang, + TranslatedTo: lang.String(), + }) + + continue + } + sendNormalResp: + resp[steamid] = append(resp[steamid], &utils.ChatResponse{ + Message: stat.Message, + AllChat: stat.AllChat, + Tick: stat.Tick, + }) + } + + err = rdc.Set(&cache.Item{ + Ctx: context.Background(), + Key: fmt.Sprintf(utils.MatchChatCacheKey, matchId, lang.String()), + Value: resp, + TTL: time.Hour * 24 * 30, + }) + if err != nil { + log.Errorf("[GMC] Failure saving to cache: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + } else { + tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).WithMatchPlayer().All(context.Background()) + if err != nil { + log.Infof("[GMC] match %d not found: %+v", matchId, err) + w.WriteHeader(http.StatusNotFound) + return + } + + for _, stat := range tStats { + steamid := strconv.FormatUint(stat.Edges.MatchPlayer.PlayerStats, 10) + if _, ok := resp[steamid]; !ok { + resp[steamid] = make([]*utils.ChatResponse, 0) } resp[steamid] = append(resp[steamid], &utils.ChatResponse{ - Message: translated, - AllChat: stat.AllChat, - Tick: stat.Tick, - TranslatedFrom: srcLang, - TranslatedTo: lang.String(), + Message: stat.Message, + AllChat: stat.AllChat, + Tick: stat.Tick, }) - - continue } - sendNormalResp: - resp[steamid] = append(resp[steamid], &utils.ChatResponse{ - Message: stat.Message, - AllChat: stat.AllChat, - Tick: stat.Tick, - }) - } - - err = rdc.Set(&cache.Item{ - Ctx: context.Background(), - Key: fmt.Sprintf(utils.MatchChatCacheKey, matchId, lang.String()), - Value: resp, - TTL: time.Hour * 24 * 30, - }) - if err != nil { - log.Errorf("[GMC] Failure saving to cache: %v", err) - w.WriteHeader(http.StatusInternalServerError) - return } }