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.

105 lines
2.5 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. "database/sql/driver"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Auth"
  10. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
  11. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
  12. )
  13. // ConversationList returns an encrypted list of all Conversations
  14. func ConversationList(w http.ResponseWriter, r *http.Request) {
  15. var (
  16. conversationDetails []Models.UserConversation
  17. userSession Models.Session
  18. returnJSON []byte
  19. values url.Values
  20. page int
  21. err error
  22. )
  23. values = r.URL.Query()
  24. page, err = strconv.Atoi(values.Get("page"))
  25. if err != nil {
  26. page = 0
  27. }
  28. userSession, _ = Auth.CheckCookie(r)
  29. conversationDetails, err = Database.GetUserConversationsByUserId(
  30. userSession.UserID.String(),
  31. page,
  32. )
  33. if err != nil {
  34. http.Error(w, "Error", http.StatusInternalServerError)
  35. return
  36. }
  37. returnJSON, err = json.MarshalIndent(conversationDetails, "", " ")
  38. if err != nil {
  39. http.Error(w, "Error", http.StatusInternalServerError)
  40. return
  41. }
  42. w.WriteHeader(http.StatusOK)
  43. w.Write(returnJSON)
  44. }
  45. // ConversationDetailsList returns an encrypted list of all ConversationDetails
  46. func ConversationDetailsList(w http.ResponseWriter, r *http.Request) {
  47. var (
  48. conversationDetails []Models.ConversationDetail
  49. detail Models.ConversationDetail
  50. query url.Values
  51. conversationIds []string
  52. messageExpiryRaw driver.Value
  53. returnJSON []byte
  54. i int
  55. ok bool
  56. err error
  57. )
  58. query = r.URL.Query()
  59. conversationIds, ok = query["conversation_detail_ids"]
  60. if !ok {
  61. http.Error(w, "Invalid Data", http.StatusBadGateway)
  62. return
  63. }
  64. conversationIds = strings.Split(conversationIds[0], ",")
  65. conversationDetails, err = Database.GetConversationDetailsByIds(
  66. conversationIds,
  67. )
  68. if err != nil {
  69. http.Error(w, "Error", http.StatusInternalServerError)
  70. return
  71. }
  72. for i, detail = range conversationDetails {
  73. messageExpiryRaw, _ = detail.MessageExpiryDefault.Value()
  74. conversationDetails[i].MessageExpiry, _ = messageExpiryRaw.(string)
  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. }