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.

84 lines
2.0 KiB

  1. package Messages
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "strings"
  7. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth"
  8. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  9. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
  10. )
  11. // EncryptedConversationList returns an encrypted list of all Conversations
  12. func EncryptedConversationList(w http.ResponseWriter, r *http.Request) {
  13. var (
  14. userConversations []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. userConversations, 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(userConversations, "", " ")
  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. userConversations []Models.ConversationDetail
  43. query url.Values
  44. conversationIds []string
  45. returnJSON []byte
  46. ok bool
  47. err error
  48. )
  49. query = r.URL.Query()
  50. conversationIds, ok = query["conversation_detail_ids"]
  51. if !ok {
  52. http.Error(w, "Invalid Data", http.StatusBadGateway)
  53. return
  54. }
  55. // TODO: Fix error handling here
  56. conversationIds = strings.Split(conversationIds[0], ",")
  57. userConversations, err = Database.GetConversationDetailsByIds(
  58. conversationIds,
  59. )
  60. if err != nil {
  61. http.Error(w, "Error", http.StatusInternalServerError)
  62. return
  63. }
  64. returnJSON, err = json.MarshalIndent(userConversations, "", " ")
  65. if err != nil {
  66. http.Error(w, "Error", http.StatusInternalServerError)
  67. return
  68. }
  69. w.WriteHeader(http.StatusOK)
  70. w.Write(returnJSON)
  71. }