Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utilize created_at from rustla username api for adding a birthday flair #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"errors"
"fmt"

//"github.com/emicklei/hopwatch"
"bytes"
"log"
"runtime"
"time"
)

// ErrorTrace ...
// source https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/C24fRw8HDmI
// from David Wright
type ErrorTrace struct {
Expand Down Expand Up @@ -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())
Expand Down
8 changes: 4 additions & 4 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
53 changes: 34 additions & 19 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
ISPROTECTED = 1 << iota
ISSUBSCRIBER = 1 << iota
ISBOT = 1 << iota
ISBIRTHDAY = 1 << iota
)

type uidprot struct {
Expand Down Expand Up @@ -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
}

Expand All @@ -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) {
Expand All @@ -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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the simplified user object needs to be regenerated when this changes. rn it will only work if the user logs in on their birthday and the flair will stay until they log in again

if now.YearDay() == t.YearDay() {
features = append(features, "birthday")
}

// finally update records...
db.updateUser(Userid(uid), username, ip)

Expand Down Expand Up @@ -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
Expand All @@ -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)))
}
}
}
Expand Down Expand Up @@ -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)
Expand Down