This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/stats/users.go

48 lines
1 KiB
Go
Raw Normal View History

package main
import (
"log"
"strings"
"github.com/boltdb/bolt"
2018-04-03 10:07:06 +00:00
"github.com/hamcha/tg"
)
var users map[string]string
func getNick(apiuser tg.APIUser) {
if _, ok := users[apiuser.Username]; ok && strings.HasPrefix(users[apiuser.Username], apiuser.FirstName) {
// It's updated, don't bother
return
}
users[apiuser.Username] = apiuser.FirstName
if apiuser.LastName != "" {
users[apiuser.Username] += " " + apiuser.LastName
}
err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("usernames"))
return b.Put([]byte(apiuser.Username), []byte(users[apiuser.Username]))
})
if err != nil {
log.Printf("[getNick] Could not update %s name: %s\n", apiuser.Username, err.Error())
}
}
func loadUsers() {
users = make(map[string]string)
err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("usernames"))
if err != nil {
return err
}
b.ForEach(func(user, name []byte) error {
users[string(user)] = string(name)
return nil
})
return nil
})
assert(err)
}