TerraOceanPlugin/GoBot/db_trust.go

47 lines
1.1 KiB
Go

package main
import (
"errors"
"time"
)
var errRecentlyChanged = errors.New("db: attempt to change the trust for a user twice in a month")
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
}
func updateDbTrust(sourceID, targetID string, voteValue int) error {
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
}
}
rows.Close()
_, err = db.Exec("REPLACE INTO trustVote(sourceUser,targetUser,lastUpdated,trust) VALUES(?,?,?,?)", sourceID, targetID, time.Now(), voteValue)
return err
}