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.

74 lines
1.8 KiB

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