mod todo, add mod appearence, about for user
This commit is contained in:
+22
-4
@@ -20,9 +20,9 @@ func DbInit(ctx context.Context) {
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(20) UNIQUE NOT NULL,
|
||||
pass_hash VARCHAR(60) NOT NULL,
|
||||
pronouns VARCHAR(15) DEFAULT NULL,
|
||||
name TEXT UNIQUE NOT NULL,
|
||||
pass_hash TEXT NOT NULL,
|
||||
pronouns TEXT DEFAULT NULL,
|
||||
color_red SMALLINT DEFAULT NULL,
|
||||
color_green SMALLINT DEFAULT NULL,
|
||||
color_blue SMALLINT DEFAULT NULL,
|
||||
@@ -36,7 +36,7 @@ func DbInit(ctx context.Context) {
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS chat_groups (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(48) NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
creator_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
owner_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
enable_client_colors BOOLEAN NOT NULL DEFAULT true,
|
||||
@@ -137,6 +137,24 @@ func DbUserSetGroups(ctx context.Context, user *User) error {
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
// DbUserSetColor set user's color based on id
|
||||
// return: error if not successful
|
||||
func DbUserSetColor(ctx context.Context, user *User) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE users SET color_red = $1, color_green = $2, color_blue = $3 WHERE id = $4
|
||||
`, user.Color[0], user.Color[1], user.Color[2], user.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
// DbUserSetPronouns set user's pronouns based on id
|
||||
// return: error if not successful
|
||||
func DbUserSetPronouns(ctx context.Context, user *User) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE users SET pronouns = $1 WHERE id = $2
|
||||
`, user.Pronouns, user.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
// DbGroupSetColor set group's color based on id
|
||||
// return: error if not successful
|
||||
func DbGroupSetColor(ctx context.Context, group *Group) error {
|
||||
|
||||
@@ -87,7 +87,7 @@ func getIfOwnerUserAndGroup(ctx context.Context, response *http.ResponseWriter,
|
||||
return user, group, nil
|
||||
}
|
||||
|
||||
func HttpHandleNewUser(response http.ResponseWriter, request *http.Request) {
|
||||
func HttpHandleUserNew(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
}
|
||||
@@ -134,7 +134,7 @@ func HttpHandleNewUser(response http.ResponseWriter, request *http.Request) {
|
||||
response.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
func HttpHandleDeleteUser(response http.ResponseWriter, request *http.Request) {
|
||||
func HttpHandleUserDelete(response http.ResponseWriter, request *http.Request) {
|
||||
ctx := request.Context()
|
||||
|
||||
userId, err := TokenValidateGetId(request.FormValue("token"))
|
||||
@@ -152,6 +152,53 @@ func HttpHandleDeleteUser(response http.ResponseWriter, request *http.Request) {
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
// HttpHandleUserModifyAppearance currently just color
|
||||
func HttpHandleUserModifyAppearance(response http.ResponseWriter, request *http.Request) {
|
||||
ctx := request.Context()
|
||||
user, err := getUser(ctx, request.FormValue("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
color, err := ConvertStringToRgb(request.FormValue("color"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid color", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user.Color = color
|
||||
err = DbUserSetColor(ctx, user)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
// HttpHandleUserModifyAbout currently just pronouns
|
||||
func HttpHandleUserModifyAbout(response http.ResponseWriter, request *http.Request) {
|
||||
ctx := request.Context()
|
||||
user, err := getUser(ctx, request.FormValue("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
pronouns := request.FormValue("pronouns")
|
||||
if len(pronouns) > 25 && len(pronouns) < 2 {
|
||||
http.Error(response, "invalid pronouns", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
user.Pronouns = pronouns
|
||||
err = DbUserSetPronouns(ctx, user)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HttpHandleNewToken(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
@@ -200,7 +247,7 @@ func HttpHandleNewToken(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
response.WriteHeader(http.StatusCreated)
|
||||
response.Write([]byte(token))
|
||||
}
|
||||
|
||||
@@ -248,7 +295,7 @@ func HttpHandeGroupCreate(response http.ResponseWriter, request *http.Request) {
|
||||
fmt.Fprintf(response, "%d", group.Id)
|
||||
}
|
||||
|
||||
func HttpHandleGroupRemove(response http.ResponseWriter, request *http.Request) {
|
||||
func HttpHandleGroupDelete(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -17,17 +17,19 @@ func main() {
|
||||
ctx := context.Background()
|
||||
DbInit(ctx)
|
||||
|
||||
http.HandleFunc("/new/user", withCORS(HttpHandleNewUser))
|
||||
http.HandleFunc("/new/user", withCORS(HttpHandleUserNew))
|
||||
http.HandleFunc("/new/token", withCORS(HttpHandleNewToken))
|
||||
http.HandleFunc("/new/group", withCORS(HttpHandeGroupCreate))
|
||||
http.HandleFunc("/new/message", withCORS(HttpHandleNewMessage))
|
||||
http.HandleFunc("mod/user/appearence", withCORS(HttpHandleUserModifyAppearance))
|
||||
http.HandleFunc("mod/user/about", withCORS(HttpHandleUserModifyAbout))
|
||||
http.HandleFunc("/mod/group/addusers", withCORS(HttpHandleGroupAddUser))
|
||||
http.HandleFunc("/mod/group/removeusers", withCORS(HttpHandleGroupRemoveUser))
|
||||
http.HandleFunc("/mod/group/color", withCORS(HttpHandleGroupChangeColor))
|
||||
http.HandleFunc("/mod/group/owner", withCORS(HttpHandleGroupChangeOwner))
|
||||
http.HandleFunc("/get/groups", withCORS(HttpHandleGroupsGetWithoutMembers))
|
||||
http.HandleFunc("/get/group/members", withCORS(HttpHandleGroupMembersGet))
|
||||
http.HandleFunc("/del/group", withCORS(HttpHandleGroupRemove))
|
||||
http.HandleFunc("/del/group", withCORS(HttpHandleGroupDelete))
|
||||
http.HandleFunc("/ws", ServeWsConnection)
|
||||
|
||||
log.Println("listening on :8080")
|
||||
|
||||
Reference in New Issue
Block a user