Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aymericbeaumet committed Nov 2, 2023
1 parent f90ce6d commit 69ea6e6
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions ios/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,26 @@ func (app *App) PostMessages(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, newMessage)
}

func (app *App) delayAnswer(chat *Chat) {
<-time.After(3*time.Second + time.Duration(rand.Intn(3))*time.Second)

app.mu.Lock()
defer app.mu.Unlock()

text := app.jokes[rand.Intn(len(app.jokes))]

newMessage := &Message{
ID: newID(),
ChatID: chat.ID,
Author: "bot",
Text: text,
SentAt: time.Now(),
}

chat.messages = append(chat.messages, newMessage)
app.publishEvent("messages", newMessage)
}

func (app *App) findChatByID(rawID string) (*Chat, error) {
chatID, err := strconv.ParseUint(rawID, 10, 32)
if err != nil {
Expand All @@ -223,26 +243,6 @@ func (app *App) publishEvent(stream string, payload any) {
}
}

func (app *App) delayAnswer(chat *Chat) {
<-time.After(3*time.Second + time.Duration(rand.Intn(3))*time.Second)

app.mu.Lock()
defer app.mu.Unlock()

text := app.jokes[rand.Intn(len(app.jokes))]

newMessage := &Message{
ID: newID(),
ChatID: chat.ID,
Author: "bot",
Text: text,
SentAt: time.Now(),
}

chat.messages = append(chat.messages, newMessage)
app.publishEvent("messages", newMessage)
}

//
// Helpers
//
Expand Down

0 comments on commit 69ea6e6

Please sign in to comment.