TerraOceanPlugin/GoBot/db_trust.go

49 lines
1.1 KiB
Go

2019-10-11 12:25:09 +07:00
package main
2019-10-11 14:29:07 +07:00
import (
"errors"
"time"
)
2019-10-11 12:25:09 +07:00
2019-10-11 14:29:07 +07:00
var errRecentlyChanged = errors.New("db: attempt to change the trust for a user twice in a month")
2019-10-11 12:25:09 +07:00
func getTrustVote(targetID string) ([]int, error) {
rows, err := db.Query("SELECT trust FROM trustVote WHERE targetUser=?", targetID)
if err != nil {
return nil, err
}
defer rows.Close()
array := make([]int, 0)
for rows.Next() {
var trust int
rows.Scan(&trust)
array = append(array, trust)
}
return array, nil
}
2019-10-11 14:29:07 +07:00
2019-10-12 06:40:46 +07:00
func updateDbTrust(sourceID, targetID string, voteValue int, force bool) error {
if !force {
rows, err := db.Query("SELECT lastUpdated FROM trustVote WHERE sourceUser=? AND targetUser=?", sourceID, targetID)
if err != nil {
return err
}
if rows.Next() {
//Check that it was not changed within a month.
var lastUpdated time.Time
rows.Scan(&lastUpdated)
if time.Now().Sub(lastUpdated) > 24*30*time.Hour {
rows.Close()
return errRecentlyChanged
}
2019-10-11 14:29:07 +07:00
}
2019-10-12 06:40:46 +07:00
rows.Close()
2019-10-11 14:29:07 +07:00
}
2019-10-12 06:40:46 +07:00
_, err := db.Exec("REPLACE INTO trustVote(sourceUser,targetUser,lastUpdated,trust) VALUES(?,?,?,?)", sourceID, targetID, time.Now(), voteValue)
2019-10-11 14:29:07 +07:00
return err
}