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
|
return true, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if count > 0 {
|
|
||||||
log.Printf("ignoring known item %d", parentId)
|
|
||||||
}
|
|
||||||
|
|
||||||
return count > 0, nil
|
return count > 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
80
main.go
80
main.go
|
@ -44,36 +44,22 @@ func tickGameStart(c *sn.Client) {
|
||||||
|
|
||||||
for _, n := range mentions {
|
for _, n := range mentions {
|
||||||
|
|
||||||
// we only care about current notifications
|
if !isRecent(n.Item.CreatedAt) {
|
||||||
x := time.Now().Add(-30 * time.Second)
|
|
||||||
if n.Item.CreatedAt.Before(x) {
|
|
||||||
log.Printf("ignoring old mention %d\n", n.Item.Id)
|
log.Printf("ignoring old mention %d\n", n.Item.Id)
|
||||||
continue
|
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)
|
log.Printf("failed to check for existing reply to game start in item %d: %v\n", n.Item.Id, err)
|
||||||
continue
|
continue
|
||||||
} else if exists {
|
} else if handled {
|
||||||
// TODO: check if move changed
|
// TODO: check if move changed
|
||||||
log.Printf("reply to game start in item %d already exists\n", n.Item.Id)
|
log.Printf("reply to game start in item %d already exists\n", n.Item.Id)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = handleGameStart(&n.Item); err != nil {
|
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 {
|
} else {
|
||||||
log.Printf("started new game via item %d\n", n.Item.Id)
|
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 {
|
for _, n := range replies {
|
||||||
|
|
||||||
// we only care about current notifications
|
if !isRecent(n.Item.CreatedAt) {
|
||||||
x := time.Now().Add(-30 * time.Second)
|
|
||||||
if n.Item.CreatedAt.Before(x) {
|
|
||||||
log.Printf("ignoring old reply %d\n", n.Item.Id)
|
log.Printf("ignoring old reply %d\n", n.Item.Id)
|
||||||
continue
|
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)
|
log.Printf("failed to check for existing reply to game update in item %d: %v\n", n.Item.Id, err)
|
||||||
continue
|
continue
|
||||||
} else if exists {
|
} else if handled {
|
||||||
// TODO: check if move changed
|
// TODO: check if move changed
|
||||||
log.Printf("reply to game update in item %d already exists\n", n.Item.Id)
|
log.Printf("reply to game update in item %d already exists\n", n.Item.Id)
|
||||||
continue
|
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 = handleGameProgress(&n.Item); err != nil {
|
||||||
|
handleError(&n.Item, err)
|
||||||
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)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf("updated game via item %d\n", n.Item.Id)
|
log.Printf("updated game via item %d\n", n.Item.Id)
|
||||||
}
|
}
|
||||||
|
@ -237,6 +219,27 @@ func handleGameProgress(req *sn.Item) error {
|
||||||
return nil
|
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) {
|
func createComment(parentId int, text string) (*sn.Item, error) {
|
||||||
var (
|
var (
|
||||||
commentId int
|
commentId int
|
||||||
|
@ -295,3 +298,12 @@ func parseGameProgress(input string) (string, error) {
|
||||||
|
|
||||||
return "", errors.New("failed to parse game update")
|
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