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.

65 lines
1.3 KiB

  1. package Messages
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  8. "github.com/gorilla/mux"
  9. )
  10. // Messages gets messages by the associationKey
  11. func Messages(w http.ResponseWriter, r *http.Request) {
  12. var (
  13. messages []Database.Message
  14. message Database.Message
  15. urlVars map[string]string
  16. associationKey string
  17. values url.Values
  18. returnJSON []byte
  19. page int
  20. i int
  21. ok bool
  22. err error
  23. )
  24. urlVars = mux.Vars(r)
  25. associationKey, ok = urlVars["associationKey"]
  26. if !ok {
  27. http.Error(w, "Not Found", http.StatusNotFound)
  28. return
  29. }
  30. values = r.URL.Query()
  31. page, err = strconv.Atoi(values.Get("page"))
  32. if err != nil {
  33. page = 0
  34. }
  35. messages, err = Database.GetMessagesByAssociationKey(associationKey, page)
  36. if !ok {
  37. http.Error(w, "Not Found", http.StatusNotFound)
  38. return
  39. }
  40. for i, message = range messages {
  41. if message.MessageData.AttachmentID == nil {
  42. continue
  43. }
  44. messages[i].MessageData.Attachment.ImageLink = message.MessageData.Attachment.FilePath
  45. }
  46. returnJSON, err = json.MarshalIndent(messages, "", " ")
  47. if err != nil {
  48. http.Error(w, "Error", http.StatusInternalServerError)
  49. return
  50. }
  51. w.WriteHeader(http.StatusOK)
  52. w.Write(returnJSON)
  53. }