Compare commits

...

5 Commits

Author SHA1 Message Date
ekzyis c1ad076828 Move error handling into own function 2024-10-16 19:55:26 +02:00
ekzyis b6991ce3e5 Ignore nested replies 2024-10-16 19:51:55 +02:00
ekzyis 2037fb0578 Remove noisy log message 2024-10-16 19:46:50 +02:00
ekzyis 894ad95d73 Move logic into alreadyHandled function 2024-10-16 19:46:50 +02:00
ekzyis bd24198b05 Move logic into isRecent function 2024-10-16 19:46:50 +02:00
2 changed files with 46 additions and 38 deletions

View File

@ -62,10 +62,6 @@ func ItemHasReply(parentId int, userId int) (bool, error) {
return true, err
}
if count > 0 {
log.Printf("ignoring known item %d", parentId)
}
return count > 0, nil
}

80
main.go
View File

@ -44,36 +44,22 @@ func tickGameStart(c *sn.Client) {
for _, n := range mentions {
// we only care about current notifications
x := time.Now().Add(-30 * time.Second)
if n.Item.CreatedAt.Before(x) {
if !isRecent(n.Item.CreatedAt) {
log.Printf("ignoring old mention %d\n", n.Item.Id)
continue
}
if exists, err := db.ItemHasReply(n.Item.Id, meId); err != nil {
if handled, err := alreadyHandled(n.Item.Id); err != nil {
log.Printf("failed to check for existing reply to game start in item %d: %v\n", n.Item.Id, err)
continue
} else if exists {
} else if handled {
// TODO: check if move changed
log.Printf("reply to game start in item %d already exists\n", n.Item.Id)
continue
}
if err = handleGameStart(&n.Item); err != nil {
// don't reply to mentions that we failed to parse as a game start
// to support unrelated mentions
if err.Error() == "failed to parse game start" {
log.Printf("ignoring error for item %d: %v\n", n.Item.Id, err)
return
}
if _, err2 := createComment(n.Item.Id, fmt.Sprintf("`%v`", err)); err2 != nil {
log.Printf("failed to reply with error to item %d: %v\n", n.Item.Id, err2)
} else {
log.Printf("replied to game start in item %d with error: %v\n", n.Item.Id, err)
}
handleError(&n.Item, err)
} else {
log.Printf("started new game via item %d\n", n.Item.Id)
}
@ -95,34 +81,30 @@ func tickGameProgress(c *sn.Client) {
for _, n := range replies {
// we only care about current notifications
x := time.Now().Add(-30 * time.Second)
if n.Item.CreatedAt.Before(x) {
if !isRecent(n.Item.CreatedAt) {
log.Printf("ignoring old reply %d\n", n.Item.Id)
continue
}
if exists, err := db.ItemHasReply(n.Item.Id, meId); err != nil {
if handled, err := alreadyHandled(n.Item.Id); err != nil {
log.Printf("failed to check for existing reply to game update in item %d: %v\n", n.Item.Id, err)
continue
} else if exists {
} else if handled {
// TODO: check if move changed
log.Printf("reply to game update in item %d already exists\n", n.Item.Id)
continue
}
if parent, err := c.Item(n.Item.ParentId); err != nil {
log.Printf("failed to fetch parent %d of %d\n", n.Item.ParentId, n.Item.Id)
continue
} else if parent.User.Id != meId {
log.Printf("ignoring nested reply %d\n", n.Item.Id)
continue
}
if err = handleGameProgress(&n.Item); err != nil {
if err.Error() == "failed to parse game update" {
log.Printf("ignoring error for item %d: %v\n", n.Item.Id, err)
return
}
if _, err2 := createComment(n.Item.Id, fmt.Sprintf("`%v`", err)); err2 != nil {
log.Printf("failed to reply with error to item %d: %v\n", n.Item.Id, err2)
} else {
log.Printf("replied to game start in item %d with error: %v\n", n.Item.Id, err)
}
handleError(&n.Item, err)
} else {
log.Printf("updated game via item %d\n", n.Item.Id)
}
@ -237,6 +219,27 @@ func handleGameProgress(req *sn.Item) error {
return nil
}
func handleError(req *sn.Item, err error) {
// don't reply to mentions that we failed to parse as a game start
// to support unrelated mentions
if err.Error() == "failed to parse game start" {
log.Printf("ignoring error for item %d: %v\n", req.Id, err)
return
}
if err.Error() == "failed to parse game update" {
log.Printf("ignoring error for item %d: %v\n", req.Id, err)
return
}
if _, err2 := createComment(req.Id, fmt.Sprintf("`%v`", err)); err2 != nil {
log.Printf("failed to reply with error to item %d: %v\n", req.Id, err2)
} else {
log.Printf("replied to game start in item %d with error: %v\n", req.Id, err)
}
}
func createComment(parentId int, text string) (*sn.Item, error) {
var (
commentId int
@ -295,3 +298,12 @@ func parseGameProgress(input string) (string, error) {
return "", errors.New("failed to parse game update")
}
func isRecent(t time.Time) bool {
x := time.Now().Add(-30 * time.Second)
return t.After(x)
}
func alreadyHandled(id int) (bool, error) {
return db.ItemHasReply(id, meId)
}