Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.0 KiB

  1. package Users
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
  7. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
  8. )
  9. // SearchUsers searches a for a user by username
  10. func SearchUsers(w http.ResponseWriter, r *http.Request) {
  11. var (
  12. user Models.User
  13. query url.Values
  14. rawUsername []string
  15. username string
  16. returnJSON []byte
  17. ok bool
  18. err error
  19. )
  20. query = r.URL.Query()
  21. rawUsername, ok = query["username"]
  22. if !ok {
  23. http.Error(w, "Not Found", http.StatusNotFound)
  24. return
  25. }
  26. if len(rawUsername) != 1 {
  27. http.Error(w, "Not Found", http.StatusNotFound)
  28. return
  29. }
  30. username = rawUsername[0]
  31. user, err = Database.GetUserByUsername(username)
  32. if err != nil {
  33. http.Error(w, "Not Found", http.StatusNotFound)
  34. return
  35. }
  36. user.Password = ""
  37. user.AsymmetricPrivateKey = ""
  38. returnJSON, err = json.MarshalIndent(user, "", " ")
  39. if err != nil {
  40. http.Error(w, "Not Found", http.StatusNotFound)
  41. return
  42. }
  43. w.WriteHeader(http.StatusOK)
  44. w.Write(returnJSON)
  45. }