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.2 KiB

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