A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

204 líneas
4.7 KiB

  1. /*
  2. * Copyright © 2019-2020 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) Active(db *datastore) bool {
  38. if i.Expired() {
  39. return false
  40. }
  41. if i.MaxUses.Valid && i.MaxUses.Int64 > 0 {
  42. if c := db.GetUsersInvitedCount(i.ID); c >= i.MaxUses.Int64 {
  43. return false
  44. }
  45. }
  46. return true
  47. }
  48. func (i Invite) ExpiresFriendly() string {
  49. return i.Expires.Format("January 2, 2006, 3:04 PM")
  50. }
  51. func handleViewUserInvites(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  52. // Don't show page if instance doesn't allow it
  53. if !(app.cfg.App.UserInvites != "" && (u.IsAdmin() || app.cfg.App.UserInvites != "admin")) {
  54. return impart.HTTPError{http.StatusNotFound, ""}
  55. }
  56. f, _ := getSessionFlashes(app, w, r, nil)
  57. p := struct {
  58. *UserPage
  59. Invites *[]Invite
  60. Silenced bool
  61. }{
  62. UserPage: NewUserPage(app, r, u, "Invite People", f),
  63. }
  64. var err error
  65. p.Silenced, err = app.db.IsUserSilenced(u.ID)
  66. if err != nil {
  67. log.Error("view invites: %v", err)
  68. }
  69. p.Invites, err = app.db.GetUserInvites(u.ID)
  70. if err != nil {
  71. return err
  72. }
  73. for i := range *p.Invites {
  74. (*p.Invites)[i].uses = app.db.GetUsersInvitedCount((*p.Invites)[i].ID)
  75. }
  76. showUserPage(w, "invite", p)
  77. return nil
  78. }
  79. func handleCreateUserInvite(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
  80. muVal := r.FormValue("uses")
  81. expVal := r.FormValue("expires")
  82. if u.IsSilenced() {
  83. return ErrUserSilenced
  84. }
  85. var err error
  86. var maxUses int
  87. if muVal != "0" {
  88. maxUses, err = strconv.Atoi(muVal)
  89. if err != nil {
  90. return impart.HTTPError{http.StatusBadRequest, "Invalid value for 'max_uses'"}
  91. }
  92. }
  93. var expDate *time.Time
  94. var expires int
  95. if expVal != "0" {
  96. expires, err = strconv.Atoi(expVal)
  97. if err != nil {
  98. return impart.HTTPError{http.StatusBadRequest, "Invalid value for 'expires'"}
  99. }
  100. ed := time.Now().Add(time.Duration(expires) * time.Minute)
  101. expDate = &ed
  102. }
  103. inviteID := store.GenerateRandomString("0123456789BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz", 6)
  104. err = app.db.CreateUserInvite(inviteID, u.ID, maxUses, expDate)
  105. if err != nil {
  106. return err
  107. }
  108. return impart.HTTPError{http.StatusFound, "/me/invites"}
  109. }
  110. func handleViewInvite(app *App, w http.ResponseWriter, r *http.Request) error {
  111. inviteCode := mux.Vars(r)["code"]
  112. i, err := app.db.GetUserInvite(inviteCode)
  113. if err != nil {
  114. return err
  115. }
  116. expired := i.Expired()
  117. if !expired && i.MaxUses.Valid && i.MaxUses.Int64 > 0 {
  118. // Invite has a max-use number, so check if we're past that limit
  119. i.uses = app.db.GetUsersInvitedCount(inviteCode)
  120. expired = i.uses >= i.MaxUses.Int64
  121. }
  122. if u := getUserSession(app, r); u != nil {
  123. // check if invite belongs to another user
  124. // error can be ignored as not important in this case
  125. if ownInvite, _ := app.db.IsUsersInvite(inviteCode, u.ID); !ownInvite {
  126. addSessionFlash(app, w, r, "You're already registered and logged in.", nil)
  127. // show homepage
  128. return impart.HTTPError{http.StatusFound, "/me/settings"}
  129. }
  130. // show invite instructions
  131. p := struct {
  132. *UserPage
  133. Invite *Invite
  134. Expired bool
  135. }{
  136. UserPage: NewUserPage(app, r, u, "Invite to "+app.cfg.App.SiteName, nil),
  137. Invite: i,
  138. Expired: expired,
  139. }
  140. showUserPage(w, "invite-help", p)
  141. return nil
  142. }
  143. p := struct {
  144. page.StaticPage
  145. *OAuthButtons
  146. Error string
  147. Flashes []template.HTML
  148. Invite string
  149. }{
  150. StaticPage: pageForReq(app, r),
  151. OAuthButtons: NewOAuthButtons(app.cfg),
  152. Invite: inviteCode,
  153. }
  154. if expired {
  155. p.Error = "This invite link has expired."
  156. }
  157. // Tell search engines not to index invite links
  158. w.Header().Set("X-Robots-Tag", "noindex")
  159. // Get error messages
  160. session, err := app.sessionStore.Get(r, cookieName)
  161. if err != nil {
  162. // Ignore this
  163. log.Error("Unable to get session in handleViewInvite; ignoring: %v", err)
  164. }
  165. flashes, _ := getSessionFlashes(app, w, r, session)
  166. for _, flash := range flashes {
  167. p.Flashes = append(p.Flashes, template.HTML(flash))
  168. }
  169. // Show landing page
  170. return renderPage(w, "signup.tmpl", p)
  171. }