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.

100 lines
2.3 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, _ = Auth.CheckCookie(r)
  28. conversationDetails, err = Database.GetUserConversationsByUserId(
  29. userSession.UserID.String(),
  30. page,
  31. )
  32. if err != nil {
  33. http.Error(w, "Error", http.StatusInternalServerError)
  34. return
  35. }
  36. returnJSON, err = json.MarshalIndent(conversationDetails, "", " ")
  37. if err != nil {
  38. http.Error(w, "Error", http.StatusInternalServerError)
  39. return
  40. }
  41. w.WriteHeader(http.StatusOK)
  42. w.Write(returnJSON)
  43. }
  44. // ConversationDetailsList returns an encrypted list of all ConversationDetails
  45. func ConversationDetailsList(w http.ResponseWriter, r *http.Request) {
  46. var (
  47. conversationDetails []Models.ConversationDetail
  48. detail Models.ConversationDetail
  49. query url.Values
  50. conversationIds []string
  51. returnJSON []byte
  52. i int
  53. ok bool
  54. err error
  55. )
  56. query = r.URL.Query()
  57. conversationIds, ok = query["conversation_detail_ids"]
  58. if !ok {
  59. http.Error(w, "Invalid Data", http.StatusBadGateway)
  60. return
  61. }
  62. conversationIds = strings.Split(conversationIds[0], ",")
  63. conversationDetails, err = Database.GetConversationDetailsByIds(
  64. conversationIds,
  65. )
  66. if err != nil {
  67. http.Error(w, "Error", http.StatusInternalServerError)
  68. return
  69. }
  70. for i, detail = range conversationDetails {
  71. if detail.AttachmentID == nil {
  72. continue
  73. }
  74. conversationDetails[i].Attachment.ImageLink = detail.Attachment.FilePath
  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. }