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.

104 lines
2.4 KiB

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