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.

93 lines
2.2 KiB

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