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.

51 lines
1.1 KiB

  1. package Messages
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth"
  6. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  7. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
  8. "github.com/gorilla/mux"
  9. )
  10. func MessageThread(w http.ResponseWriter, r *http.Request) {
  11. var (
  12. userData Models.User
  13. messageThread Models.MessageThread
  14. urlVars map[string]string
  15. threadKey string
  16. returnJson []byte
  17. ok bool
  18. err error
  19. )
  20. userData, err = Auth.CheckCookieCurrentUser(w, r)
  21. if !ok {
  22. http.Error(w, "Forbidden", http.StatusUnauthorized)
  23. return
  24. }
  25. urlVars = mux.Vars(r)
  26. threadKey, ok = urlVars["threadKey"]
  27. if !ok {
  28. http.Error(w, "Not Found", http.StatusNotFound)
  29. return
  30. }
  31. messageThread, err = Database.GetMessageThreadById(threadKey, userData)
  32. if !ok {
  33. http.Error(w, "Not Found", http.StatusNotFound)
  34. return
  35. }
  36. returnJson, err = json.MarshalIndent(messageThread, "", " ")
  37. if err != nil {
  38. http.Error(w, "Error", http.StatusInternalServerError)
  39. return
  40. }
  41. w.WriteHeader(http.StatusOK)
  42. w.Write(returnJson)
  43. }