A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
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.
 
 
 
 
 

151 lines
3.4 KiB

  1. /*
  2. * Copyright © 2019 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package writefreely
  11. import (
  12. "database/sql"
  13. "github.com/gorilla/mux"
  14. "github.com/writeas/impart"
  15. "github.com/writeas/nerds/store"
  16. "github.com/writeas/web-core/log"
  17. "github.com/writeas/writefreely/page"
  18. "html/template"
  19. "net/http"
  20. "strconv"
  21. "time"
  22. )
  23. type Invite struct {
  24. ID string
  25. MaxUses sql.NullInt64
  26. Created time.Time
  27. Expires *time.Time
  28. Inactive bool
  29. uses int64
  30. }
  31. func (i Invite) Uses() int64 {
  32. return i.uses
  33. }
  34. func (i Invite) Expired() bool {
  35. return i.Expires != nil && i.Expires.Before(time.Now())
  36. }
  37. func (i Invite) ExpiresFriendly() string {
  38. return i.Expires.Format("January 2, 2006, 3:04 PM")
  39. }
  40. func handleViewUserInvites(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  41. // Don't show page if instance doesn't allow it
  42. if !(app.cfg.App.UserInvites != "" && (u.IsAdmin() || app.cfg.App.UserInvites != "admin")) {
  43. return impart.HTTPError{http.StatusNotFound, ""}
  44. }
  45. f, _ := getSessionFlashes(app, w, r, nil)
  46. p := struct {
  47. *UserPage
  48. Invites *[]Invite
  49. }{
  50. UserPage: NewUserPage(app, r, u, "Invite People", f),
  51. }
  52. var err error
  53. p.Invites, err = app.db.GetUserInvites(u.ID)
  54. if err != nil {
  55. return err
  56. }
  57. for i := range *p.Invites {
  58. (*p.Invites)[i].uses = app.db.GetUsersInvitedCount((*p.Invites)[i].ID)
  59. }
  60. showUserPage(w, "invite", p)
  61. return nil
  62. }
  63. func handleCreateUserInvite(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  64. muVal := r.FormValue("uses")
  65. expVal := r.FormValue("expires")
  66. var err error
  67. var maxUses int
  68. if muVal != "0" {
  69. maxUses, err = strconv.Atoi(muVal)
  70. if err != nil {
  71. return impart.HTTPError{http.StatusBadRequest, "Invalid value for 'max_uses'"}
  72. }
  73. }
  74. var expDate *time.Time
  75. var expires int
  76. if expVal != "0" {
  77. expires, err = strconv.Atoi(expVal)
  78. if err != nil {
  79. return impart.HTTPError{http.StatusBadRequest, "Invalid value for 'expires'"}
  80. }
  81. ed := time.Now().Add(time.Duration(expires) * time.Minute)
  82. expDate = &ed
  83. }
  84. inviteID := store.GenerateRandomString("0123456789BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz", 6)
  85. err = app.db.CreateUserInvite(inviteID, u.ID, maxUses, expDate)
  86. if err != nil {
  87. return err
  88. }
  89. return impart.HTTPError{http.StatusFound, "/me/invites"}
  90. }
  91. func handleViewInvite(app *App, w http.ResponseWriter, r *http.Request) error {
  92. inviteCode := mux.Vars(r)["code"]
  93. i, err := app.db.GetUserInvite(inviteCode)
  94. if err != nil {
  95. return err
  96. }
  97. p := struct {
  98. page.StaticPage
  99. Error string
  100. Flashes []template.HTML
  101. Invite string
  102. }{
  103. StaticPage: pageForReq(app, r),
  104. Invite: inviteCode,
  105. }
  106. if i.Expired() {
  107. p.Error = "This invite link has expired."
  108. }
  109. if i.MaxUses.Valid && i.MaxUses.Int64 > 0 {
  110. if c := app.db.GetUsersInvitedCount(inviteCode); c >= i.MaxUses.Int64 {
  111. p.Error = "This invite link has expired."
  112. }
  113. }
  114. // Get error messages
  115. session, err := app.sessionStore.Get(r, cookieName)
  116. if err != nil {
  117. // Ignore this
  118. log.Error("Unable to get session in handleViewInvite; ignoring: %v", err)
  119. }
  120. flashes, _ := getSessionFlashes(app, w, r, session)
  121. for _, flash := range flashes {
  122. p.Flashes = append(p.Flashes, template.HTML(flash))
  123. }
  124. // Show landing page
  125. return renderPage(w, "signup.tmpl", p)
  126. }