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.
 
 
 
 
 

187 lines
4.3 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. "html/template"
  14. "net/http"
  15. "strconv"
  16. "time"
  17. "github.com/gorilla/mux"
  18. "github.com/writeas/impart"
  19. "github.com/writeas/nerds/store"
  20. "github.com/writeas/web-core/log"
  21. "github.com/writeas/writefreely/page"
  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. Silenced bool
  50. }{
  51. UserPage: NewUserPage(app, r, u, "Invite People", f),
  52. }
  53. var err error
  54. p.Silenced, err = app.db.IsUserSilenced(u.ID)
  55. if err != nil {
  56. log.Error("view invites: %v", err)
  57. }
  58. p.Invites, err = app.db.GetUserInvites(u.ID)
  59. if err != nil {
  60. return err
  61. }
  62. for i := range *p.Invites {
  63. (*p.Invites)[i].uses = app.db.GetUsersInvitedCount((*p.Invites)[i].ID)
  64. }
  65. showUserPage(w, "invite", p)
  66. return nil
  67. }
  68. func handleCreateUserInvite(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  69. muVal := r.FormValue("uses")
  70. expVal := r.FormValue("expires")
  71. if u.IsSilenced() {
  72. return ErrUserSilenced
  73. }
  74. var err error
  75. var maxUses int
  76. if muVal != "0" {
  77. maxUses, err = strconv.Atoi(muVal)
  78. if err != nil {
  79. return impart.HTTPError{http.StatusBadRequest, "Invalid value for 'max_uses'"}
  80. }
  81. }
  82. var expDate *time.Time
  83. var expires int
  84. if expVal != "0" {
  85. expires, err = strconv.Atoi(expVal)
  86. if err != nil {
  87. return impart.HTTPError{http.StatusBadRequest, "Invalid value for 'expires'"}
  88. }
  89. ed := time.Now().Add(time.Duration(expires) * time.Minute)
  90. expDate = &ed
  91. }
  92. inviteID := store.GenerateRandomString("0123456789BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz", 6)
  93. err = app.db.CreateUserInvite(inviteID, u.ID, maxUses, expDate)
  94. if err != nil {
  95. return err
  96. }
  97. return impart.HTTPError{http.StatusFound, "/me/invites"}
  98. }
  99. func handleViewInvite(app *App, w http.ResponseWriter, r *http.Request) error {
  100. inviteCode := mux.Vars(r)["code"]
  101. i, err := app.db.GetUserInvite(inviteCode)
  102. if err != nil {
  103. return err
  104. }
  105. expired := i.Expired()
  106. if !expired && i.MaxUses.Valid && i.MaxUses.Int64 > 0 {
  107. // Invite has a max-use number, so check if we're past that limit
  108. i.uses = app.db.GetUsersInvitedCount(inviteCode)
  109. expired = i.uses >= i.MaxUses.Int64
  110. }
  111. if u := getUserSession(app, r); u != nil {
  112. // check if invite belongs to another user
  113. // error can be ignored as not important in this case
  114. if ownInvite, _ := app.db.IsUsersInvite(inviteCode, u.ID); !ownInvite {
  115. addSessionFlash(app, w, r, "You're already registered and logged in.", nil)
  116. // show homepage
  117. return impart.HTTPError{http.StatusFound, "/me/settings"}
  118. }
  119. // show invite instructions
  120. p := struct {
  121. *UserPage
  122. Invite *Invite
  123. Expired bool
  124. }{
  125. UserPage: NewUserPage(app, r, u, "Invite to "+app.cfg.App.SiteName, nil),
  126. Invite: i,
  127. Expired: expired,
  128. }
  129. showUserPage(w, "invite-help", p)
  130. return nil
  131. }
  132. p := struct {
  133. page.StaticPage
  134. Error string
  135. Flashes []template.HTML
  136. Invite string
  137. }{
  138. StaticPage: pageForReq(app, r),
  139. Invite: inviteCode,
  140. }
  141. if expired {
  142. p.Error = "This invite link has expired."
  143. }
  144. // Get error messages
  145. session, err := app.sessionStore.Get(r, cookieName)
  146. if err != nil {
  147. // Ignore this
  148. log.Error("Unable to get session in handleViewInvite; ignoring: %v", err)
  149. }
  150. flashes, _ := getSessionFlashes(app, w, r, session)
  151. for _, flash := range flashes {
  152. p.Flashes = append(p.Flashes, template.HTML(flash))
  153. }
  154. // Show landing page
  155. return renderPage(w, "signup.tmpl", p)
  156. }