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.

98 lines
2.3 KiB

  1. package Messages
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth"
  9. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  10. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
  11. )
  12. // EncryptedConversationList returns an encrypted list of all Conversations
  13. func EncryptedConversationList(w http.ResponseWriter, r *http.Request) {
  14. var (
  15. conversationDetails []Models.UserConversation
  16. userSession Models.Session
  17. returnJSON []byte
  18. err error
  19. )
  20. userSession, err = Auth.CheckCookie(r)
  21. if err != nil {
  22. http.Error(w, "Forbidden", http.StatusUnauthorized)
  23. return
  24. }
  25. conversationDetails, err = Database.GetUserConversationsByUserId(
  26. userSession.UserID.String(),
  27. )
  28. if err != nil {
  29. http.Error(w, "Error", http.StatusInternalServerError)
  30. return
  31. }
  32. returnJSON, err = json.MarshalIndent(conversationDetails, "", " ")
  33. if err != nil {
  34. http.Error(w, "Error", http.StatusInternalServerError)
  35. return
  36. }
  37. w.WriteHeader(http.StatusOK)
  38. w.Write(returnJSON)
  39. }
  40. // EncryptedConversationDetailsList returns an encrypted list of all ConversationDetails
  41. func EncryptedConversationDetailsList(w http.ResponseWriter, r *http.Request) {
  42. var (
  43. conversationDetails []Models.ConversationDetail
  44. detail Models.ConversationDetail
  45. query url.Values
  46. conversationIds []string
  47. returnJSON []byte
  48. i int
  49. ok bool
  50. err error
  51. )
  52. query = r.URL.Query()
  53. conversationIds, ok = query["conversation_detail_ids"]
  54. if !ok {
  55. http.Error(w, "Invalid Data", http.StatusBadGateway)
  56. return
  57. }
  58. // TODO: Fix error handling here
  59. conversationIds = strings.Split(conversationIds[0], ",")
  60. conversationDetails, err = Database.GetConversationDetailsByIds(
  61. conversationIds,
  62. )
  63. if err != nil {
  64. http.Error(w, "Error", http.StatusInternalServerError)
  65. return
  66. }
  67. for i, detail = range conversationDetails {
  68. if detail.AttachmentID == nil {
  69. continue
  70. }
  71. conversationDetails[i].Attachment.ImageLink = fmt.Sprintf(
  72. "http://192.168.1.5:8080/files/%s",
  73. detail.Attachment.FilePath,
  74. )
  75. }
  76. returnJSON, err = json.MarshalIndent(conversationDetails, "", " ")
  77. if err != nil {
  78. http.Error(w, "Error", http.StatusInternalServerError)
  79. return
  80. }
  81. w.WriteHeader(http.StatusOK)
  82. w.Write(returnJSON)
  83. }