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.

97 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. conversationIds = strings.Split(conversationIds[0], ",")
  59. conversationDetails, err = Database.GetConversationDetailsByIds(
  60. conversationIds,
  61. )
  62. if err != nil {
  63. http.Error(w, "Error", http.StatusInternalServerError)
  64. return
  65. }
  66. for i, detail = range conversationDetails {
  67. if detail.AttachmentID == nil {
  68. continue
  69. }
  70. conversationDetails[i].Attachment.ImageLink = fmt.Sprintf(
  71. "http://192.168.1.5:8080/files/%s",
  72. detail.Attachment.FilePath,
  73. )
  74. }
  75. returnJSON, err = json.MarshalIndent(conversationDetails, "", " ")
  76. if err != nil {
  77. http.Error(w, "Error", http.StatusInternalServerError)
  78. return
  79. }
  80. w.WriteHeader(http.StatusOK)
  81. w.Write(returnJSON)
  82. }