From 71dc1fccdc68d0b9d224933a22d61dcc1ae92086 Mon Sep 17 00:00:00 2001 From: jbpratt78 Date: Fri, 26 Feb 2021 16:10:00 -0600 Subject: [PATCH] utilize created_at from rustla username api for adding a birthday flair on load of username from api, grab the created_at date, compare it to today's YearDay then append a birthday flair if matching. lint errors for shadowing, range simplification, http errors using code, and capitalized error strings --- connection.go | 9 +++++---- data.go | 2 +- debug.go | 4 +++- hub.go | 8 ++++---- main.go | 20 +++++++++---------- user.go | 53 +++++++++++++++++++++++++++++++++------------------ 6 files changed, 57 insertions(+), 39 deletions(-) diff --git a/connection.go b/connection.go index 6b5cc9a..ef83d62 100644 --- a/connection.go +++ b/connection.go @@ -221,7 +221,7 @@ func (c *Connection) writePumpText() { c.rlockUserIfExists() if data, err := Marshal(m.data); err == nil { c.runlockUserIfExists() - if data, err := Pack(m.event, data); err == nil { + if data, err = Pack(m.event, data); err == nil { if err := c.write(websocket.TextMessage, data); err != nil { return } @@ -233,7 +233,7 @@ func (c *Connection) writePumpText() { c.rlockUserIfExists() if data, err := Marshal(m.data); err == nil { c.runlockUserIfExists() - if data, err := Pack(m.event, data); err == nil { + if data, err = Pack(m.event, data); err == nil { typ := m.msgtyp if typ == 0 { typ = websocket.TextMessage @@ -246,13 +246,14 @@ func (c *Connection) writePumpText() { c.runlockUserIfExists() } case message := <-c.sendmarshalled: + var err error data := message.data.([]byte) - if data, err := Pack(message.event, data); err == nil { + if data, err = Pack(message.event, data); err == nil { typ := message.msgtyp if typ == 0 { typ = websocket.TextMessage } - if err := c.write(typ, data); err != nil { + if err = c.write(typ, data); err != nil { return } } diff --git a/data.go b/data.go index baee78d..8299e60 100644 --- a/data.go +++ b/data.go @@ -9,7 +9,7 @@ import ( func Unpack(data string) (string, []byte, error) { result := strings.SplitN(data, " ", 2) if len(result) != 2 { - return "", nil, errors.New("Unable to extract event name from data.") + return "", nil, errors.New("unable to extract event name from data") } return result[0], []byte(result[1]), nil } diff --git a/debug.go b/debug.go index b209fc2..e2386e0 100644 --- a/debug.go +++ b/debug.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + //"github.com/emicklei/hopwatch" "bytes" "log" @@ -10,6 +11,7 @@ import ( "time" ) +// ErrorTrace ... // source https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/C24fRw8HDmI // from David Wright type ErrorTrace struct { @@ -46,7 +48,7 @@ func B(v ...interface{}) { println(ts, NewErrorTrace(v...).Error()) } -// Unused ... +// F ... func F(v ...interface{}) { ts := time.Now().Format("2006-02-01 15:04:05: ") println(ts, NewErrorTrace(v...).Error()) diff --git a/hub.go b/hub.go index 376a68f..ff9473d 100644 --- a/hub.go +++ b/hub.go @@ -45,13 +45,13 @@ func (hub *Hub) run() { case c := <-hub.unregister: delete(hub.connections, c) case userid := <-hub.refreshuser: - for c, _ := range hub.connections { + for c := range hub.connections { if c.user != nil && c.user.id == userid { go c.Refresh() } } case userid := <-hub.bans: - for c, _ := range hub.connections { + for c := range hub.connections { if c.user != nil && c.user.id == userid { go c.Banned() } @@ -65,7 +65,7 @@ func (hub *Hub) run() { } case d := <-hub.getips: ips := make([]string, 0, 3) - for c, _ := range hub.connections { + for c := range hub.connections { if c.user != nil && c.user.id == d.userid { ips = append(ips, c.ip) } @@ -84,7 +84,7 @@ func (hub *Hub) run() { } } case p := <-hub.privmsg: - for c, _ := range hub.connections { + for c := range hub.connections { if c.user != nil && c.user.id == p.targetuid { if len(c.sendmarshalled) < SENDCHANNELSIZE { c.sendmarshalled <- &p.message diff --git a/main.go b/main.go index f43eb0a..ba0377d 100644 --- a/main.go +++ b/main.go @@ -80,7 +80,7 @@ func main() { nc.AddOption("default", "rarechance", strconv.FormatFloat(RARECHANCE, 'f', -1, 64)) nc.AddOption("default", "emotemanifest", EMOTEMANIFEST) - if err := nc.WriteConfigFile("settings.cfg", 0644, "ChatBackend"); err != nil { + if err = nc.WriteConfigFile("settings.cfg", 0644, "ChatBackend"); err != nil { log.Fatal("Unable to create settings.cfg: ", err) } if c, err = conf.ReadConfigFile("settings.cfg"); err != nil { @@ -132,7 +132,7 @@ func main() { checkOrigin = func(r *http.Request) bool { return true } } - var upgrader = websocket.Upgrader{ + upgrader := websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, CheckOrigin: checkOrigin, @@ -141,23 +141,23 @@ func main() { // TODO hacked in api for compat http.HandleFunc("/api/chat/me", func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { - http.Error(w, "Method not allowed", 405) + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } jwtcookie, err := r.Cookie(JWTCOOKIENAME) if err != nil { - http.Error(w, "Not logged in", 401) + http.Error(w, "Not logged in", http.StatusUnauthorized) return } claims, err := parseJwt(jwtcookie.Value) if err != nil { - http.Error(w, "Not logged in", 401) + http.Error(w, "Not logged in", http.StatusUnauthorized) return } - username, err := userFromAPI(claims.UserId) + username, _, err := userFromAPI(claims.UserID) if err != nil { - http.Error(w, "Really makes you think", 401) + http.Error(w, "Really makes you think", http.StatusUnauthorized) return } @@ -168,7 +168,7 @@ func main() { // TODO cache foo http.HandleFunc("/api/chat/history", func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { - http.Error(w, "Method not allowed", 405) + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } @@ -184,7 +184,7 @@ func main() { // TODO cache foo http.HandleFunc("/api/chat/viewer-states", func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { - http.Error(w, "Method not allowed", 405) + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } @@ -196,7 +196,7 @@ func main() { http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { - http.Error(w, "Method not allowed", 405) + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } diff --git a/user.go b/user.go index b5dde0f..b025c27 100644 --- a/user.go +++ b/user.go @@ -31,6 +31,7 @@ const ( ISPROTECTED = 1 << iota ISSUBSCRIBER = 1 << iota ISBOT = 1 << iota + ISBIRTHDAY = 1 << iota ) type uidprot struct { @@ -87,7 +88,7 @@ type User struct { } type UserClaims struct { - UserId string `json:"id"` // TODO from rustla2 backend impl + UserID string `json:"id"` // TODO from rustla2 backend impl jwt.StandardClaims } @@ -98,62 +99,63 @@ func parseJwt(cookie string) (*UserClaims, error) { return []byte(JWTSECRET), nil }) if err != nil { - return nil, errors.New("Token invalid") + return nil, errors.New("token invalid") } if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) + return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } claims, ok := token.Claims.(*UserClaims) // TODO if !ok || !token.Valid { - return nil, errors.New("Token invalid") + return nil, errors.New("token invalid") } return claims, nil } // TODO -func userFromAPI(uuid string) (username string, err error) { +func userFromAPI(uuid string) (username string, createdAt int64, err error) { // TODO here we trusted signed id in claims json is well-formed uuid... // TODO check exp-time as the backend does! (or not?) -- {"id":"uuid","exp":futurts} if err != nil { fmt.Println("err1", uuid) - return "", err + return "", 0, err } // TODO - get username from api type un struct { - Username string `json:"username"` + Username string `json:"username"` + CreatedAt int64 `json:"created_at"` } resp, err := http.Get(fmt.Sprintf("%s%s", USERNAMEAPI, uuid)) if err != nil { - return "", err + return "", 0, err } body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("err2", err) - return "", err + return "", 0, err } response := un{} err = json.Unmarshal(body, &response) if err != nil { fmt.Println("err3", err) - return "", err + return "", 0, err } D("username parsed:", response) if response.Username == "" { - return "", errors.New("User needs to set a username") + return "", 0, errors.New("User needs to set a username") } - return response.Username, nil + return response.Username, response.CreatedAt, nil } func userfromCookie(cookie string, ip string) (u *User, err error) { @@ -165,25 +167,33 @@ func userfromCookie(cookie string, ip string) (u *User, err error) { return nil, err } - username, err := userFromAPI(claims.UserId) + username, createdAt, err := userFromAPI(claims.UserID) if err != nil { return nil, err } // if user not found, insert new user into db - // ignoring the error for now - db.newUser(claims.UserId, username, ip) // TODO err is expected for non-new users... + // ignoring the error for now + _ = db.newUser(claims.UserID, username, ip) // now get features from db, update stuff - TODO - features, uid, err := db.getUserInfo(claims.UserId) + now := time.Now() + t := time.Unix(createdAt, 0) + + features, uid, err := db.getUserInfo(claims.UserID) if err != nil { fmt.Println("err4", err) return nil, err } + // if now.Day() == t.Day() && now.Month() == t.Month() && now.Year() == t.Year() { + if now.YearDay() == t.YearDay() { + features = append(features, "birthday") + } + // finally update records... db.updateUser(Userid(uid), username, ip) @@ -264,6 +274,8 @@ func (u *User) setFeatures(features []string) { u.featureSet(ISVIP) case "bot": u.featureSet(ISBOT) + case "birthday": + u.featureSet(ISBIRTHDAY) case "": continue default: // flairNN for future flairs @@ -273,8 +285,8 @@ func (u *User) setFeatures(features []string) { D("Could not parse unknown feature:", feature, err) continue } - // six proper features, all others are just useless flairs - u.featureSet(1 << (6 + uint8(flair))) + // seven proper features, all others are just useless flairs + u.featureSet(1 << (7 + uint8(flair))) } } } @@ -309,8 +321,11 @@ func (u *User) assembleSimplifiedUser() { if u.featureGet(ISBOT) { f = append(f, "bot") } + if u.featureGet(ISBIRTHDAY) { + f = append(f, "birthday") + } - for i := uint8(6); i <= 26; i++ { + for i := uint8(7); i <= 26; i++ { if u.featureGet(1 << i) { flair := fmt.Sprintf("flair%d", i-6) f = append(f, flair)