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.

70 lines
1.7 KiB

  1. package Messages
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  7. "github.com/gorilla/mux"
  8. )
  9. type rawChangeMessageExpiry struct {
  10. MessageExpiry string `json:"message_expiry"`
  11. }
  12. // ChangeUserMessageExpiry handles changing default message expiry for user
  13. func ChangeConversationMessageExpiry(w http.ResponseWriter, r *http.Request) {
  14. var (
  15. changeMessageExpiry rawChangeMessageExpiry
  16. conversationDetail Database.ConversationDetail
  17. requestBody []byte
  18. urlVars map[string]string
  19. detailID string
  20. ok bool
  21. err error
  22. )
  23. urlVars = mux.Vars(r)
  24. detailID, ok = urlVars["detailID"]
  25. if !ok {
  26. http.Error(w, "Not Found", http.StatusNotFound)
  27. return
  28. }
  29. conversationDetail, err = Database.GetConversationDetailByID(detailID)
  30. if err != nil {
  31. http.Error(w, "Not Found", http.StatusNotFound)
  32. return
  33. }
  34. // Ignore error here, as middleware should handle auth
  35. // TODO: Check if user in conversation
  36. // user, _ = Auth.CheckCookieCurrentUser(w, r)
  37. requestBody, err = ioutil.ReadAll(r.Body)
  38. if err != nil {
  39. http.Error(w, "Error", http.StatusInternalServerError)
  40. return
  41. }
  42. err = json.Unmarshal(requestBody, &changeMessageExpiry)
  43. if err != nil {
  44. http.Error(w, "Error", http.StatusInternalServerError)
  45. return
  46. }
  47. err = conversationDetail.MessageExpiryDefault.Scan(changeMessageExpiry.MessageExpiry)
  48. if err != nil {
  49. http.Error(w, "Error", http.StatusUnprocessableEntity)
  50. return
  51. }
  52. err = (&conversationDetail).UpdateConversationDetail()
  53. if err != nil {
  54. http.Error(w, "Error", http.StatusInternalServerError)
  55. return
  56. }
  57. w.WriteHeader(http.StatusNoContent)
  58. }