Compare commits

..

No commits in common. "c1ad076828e798fb142dd992b0776a9726c0ad81" and "ca2ccc569d17f9b613fa10ed849e7a354812687f" have entirely different histories.

2 changed files with 38 additions and 46 deletions

View File

@ -62,6 +62,10 @@ 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
}

78
main.go
View File

@ -44,22 +44,36 @@ func tickGameStart(c *sn.Client) {
for _, n := range mentions {
if !isRecent(n.Item.CreatedAt) {
// we only care about current notifications
x := time.Now().Add(-30 * time.Second)
if n.Item.CreatedAt.Before(x) {
log.Printf("ignoring old mention %d\n", n.Item.Id)
continue
}
if handled, err := alreadyHandled(n.Item.Id); err != nil {
if exists, err := db.ItemHasReply(n.Item.Id, meId); 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 handled {
} else if exists {
// 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 {
handleError(&n.Item, err)
// 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)
}
} else {
log.Printf("started new game via item %d\n", n.Item.Id)
}
@ -81,30 +95,34 @@ func tickGameProgress(c *sn.Client) {
for _, n := range replies {
if !isRecent(n.Item.CreatedAt) {
// we only care about current notifications
x := time.Now().Add(-30 * time.Second)
if n.Item.CreatedAt.Before(x) {
log.Printf("ignoring old reply %d\n", n.Item.Id)
continue
}
if handled, err := alreadyHandled(n.Item.Id); err != nil {
if exists, err := db.ItemHasReply(n.Item.Id, meId); 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 handled {
} else if exists {
// 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 err = handleGameProgress(&n.Item); err != nil {
handleError(&n.Item, err)
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)
}
} else {
log.Printf("updated game via item %d\n", n.Item.Id)
}
@ -219,27 +237,6 @@ 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
@ -298,12 +295,3 @@ 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)
}