mirror of
https://github.com/pagefaultgames/rogueserver.git
synced 2025-04-02 02:57:15 +08:00
Added /account/addfriend endpoint
This commit is contained in:
parent
d5a7b7042a
commit
2899416842
@ -42,6 +42,7 @@ func Init(mux *http.ServeMux) error {
|
||||
mux.HandleFunc("POST /account/login", handleAccountLogin)
|
||||
mux.HandleFunc("POST /account/changepw", handleAccountChangePW)
|
||||
mux.HandleFunc("GET /account/logout", handleAccountLogout)
|
||||
mux.HandleFunc("POST /account/addfriend", handleAddFriend)
|
||||
|
||||
// game
|
||||
mux.HandleFunc("GET /game/titlestats", handleGameTitleStats)
|
||||
|
@ -132,6 +132,28 @@ func handleAccountLogout(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func handleAddFriend(w http.ResponseWriter, r *http.Request) {
|
||||
formErr := r.ParseForm()
|
||||
if formErr != nil {
|
||||
httpError(w, r, fmt.Errorf("failed to parse request form: %s", formErr), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
uuid, err := uuidFromRequest(r)
|
||||
if err != nil {
|
||||
httpError(w, r, err, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
response, err := db.AddFriend(uuid, r.Form.Get("username"))
|
||||
if err != nil {
|
||||
httpError(w, r, err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
jsonResponse(w, r, response)
|
||||
}
|
||||
|
||||
// game
|
||||
func handleGameTitleStats(w http.ResponseWriter, r *http.Request) {
|
||||
stats := defs.TitleStats{
|
||||
|
@ -264,3 +264,42 @@ func FetchUsernameFromUUID(uuid []byte) (string, error) {
|
||||
|
||||
return username, nil
|
||||
}
|
||||
|
||||
func isFriendWith(sourceUsername string, friendUsername string) (bool, error) {
|
||||
var result int
|
||||
err := handle.QueryRow("SELECT COUNT(*) FROM friends WHERE user = ? AND friend = ?", sourceUsername, friendUsername).Scan(&result)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result == 1, nil
|
||||
}
|
||||
|
||||
func AddFriend(uuid []byte, friendUsername string) (bool, error) {
|
||||
username, err := FetchUsernameFromUUID(uuid);
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
var doesUserExist int
|
||||
err = handle.QueryRow("SELECT COUNT(*) FROM accounts WHERE username = ?", friendUsername).Scan(&doesUserExist)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if doesUserExist == 0 {
|
||||
return false, fmt.Errorf("User does not exist")
|
||||
}
|
||||
|
||||
alreadyFriends, _ := isFriendWith(username, friendUsername)
|
||||
if alreadyFriends {
|
||||
return false, fmt.Errorf("Already friend with this user")
|
||||
}
|
||||
|
||||
_, err = handle.Exec("INSERT INTO friends (user, friend, since) VALUES (?, ?, UTC_TIMESTAMP())", username, friendUsername)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user