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.

59 lines
1.2 KiB

  1. package Messages
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  7. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
  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 []Models.Message
  14. message Models.Message
  15. urlVars map[string]string
  16. associationKey string
  17. returnJSON []byte
  18. i int
  19. ok bool
  20. err error
  21. )
  22. urlVars = mux.Vars(r)
  23. associationKey, ok = urlVars["associationKey"]
  24. if !ok {
  25. http.Error(w, "Not Found", http.StatusNotFound)
  26. return
  27. }
  28. messages, err = Database.GetMessagesByAssociationKey(associationKey)
  29. if !ok {
  30. http.Error(w, "Not Found", http.StatusNotFound)
  31. return
  32. }
  33. for i, message = range messages {
  34. if message.MessageData.AttachmentID == nil {
  35. continue
  36. }
  37. messages[i].MessageData.Attachment.ImageLink = fmt.Sprintf(
  38. "http://192.168.1.5:8080/files/%s",
  39. message.MessageData.Attachment.FilePath,
  40. )
  41. }
  42. returnJSON, err = json.MarshalIndent(messages, "", " ")
  43. if err != nil {
  44. http.Error(w, "Error", http.StatusInternalServerError)
  45. return
  46. }
  47. w.WriteHeader(http.StatusOK)
  48. w.Write(returnJSON)
  49. }