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.

64 lines
2.1 KiB

2 years ago
  1. package Messages
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/gofrs/uuid"
  6. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
  7. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
  8. )
  9. // RawCreateConversationData for holding POST payload
  10. type RawCreateConversationData struct {
  11. ID string `json:"id"`
  12. Name string `json:"name"`
  13. TwoUser string `json:"two_user"`
  14. AdminAddMembers string `json:"admin_add_members"`
  15. AdminEditInfo string `json:"admin_edit_info"`
  16. AdminSendMessages string `json:"admin_send_messages"`
  17. Users []Models.ConversationDetailUser `json:"users"`
  18. UserConversations []Models.UserConversation `json:"user_conversations"`
  19. }
  20. // CreateConversation creates ConversationDetail, ConversationDetailUsers and UserConversations
  21. func CreateConversation(w http.ResponseWriter, r *http.Request) {
  22. var (
  23. rawConversationData RawCreateConversationData
  24. messageThread Models.ConversationDetail
  25. err error
  26. )
  27. err = json.NewDecoder(r.Body).Decode(&rawConversationData)
  28. if err != nil {
  29. http.Error(w, "Error", http.StatusInternalServerError)
  30. return
  31. }
  32. messageThread = Models.ConversationDetail{
  33. Base: Models.Base{
  34. ID: uuid.FromStringOrNil(rawConversationData.ID),
  35. },
  36. Name: rawConversationData.Name,
  37. TwoUser: rawConversationData.TwoUser,
  38. AdminAddMembers: rawConversationData.AdminAddMembers,
  39. AdminEditInfo: rawConversationData.AdminEditInfo,
  40. AdminSendMessages: rawConversationData.AdminSendMessages,
  41. Users: rawConversationData.Users,
  42. }
  43. err = Database.CreateConversationDetail(&messageThread)
  44. if err != nil {
  45. http.Error(w, "Error", http.StatusInternalServerError)
  46. return
  47. }
  48. err = Database.CreateUserConversations(&rawConversationData.UserConversations)
  49. if err != nil {
  50. http.Error(w, "Error", http.StatusInternalServerError)
  51. return
  52. }
  53. w.WriteHeader(http.StatusNoContent)
  54. }