Compare commits
5 Commits
ca2ccc569d
...
c1ad076828
Author | SHA1 | Date |
---|---|---|
ekzyis | c1ad076828 | |
ekzyis | b6991ce3e5 | |
ekzyis | 2037fb0578 | |
ekzyis | 894ad95d73 | |
ekzyis | bd24198b05 |
4
db/db.go
4
db/db.go
|
@ -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
80
main.go
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue