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.

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