2019-01-18 06:05:50 +01:00
|
|
|
/*
|
2022-11-11 05:49:16 +01:00
|
|
|
* Copyright © 2019-2021 Musing Studio LLC.
|
2019-01-18 06:05:50 +01:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2019-08-28 21:37:45 +02:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2019-01-18 06:05:50 +01:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/writeas/impart"
|
2021-03-30 18:49:12 +02:00
|
|
|
"github.com/writeas/web-core/id"
|
2019-01-18 06:05:50 +01:00
|
|
|
"github.com/writeas/web-core/log"
|
2021-04-06 23:24:07 +02:00
|
|
|
"github.com/writefreely/writefreely/page"
|
2019-01-18 06:05:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Invite struct {
|
|
|
|
ID string
|
|
|
|
MaxUses sql.NullInt64
|
|
|
|
Created time.Time
|
|
|
|
Expires *time.Time
|
|
|
|
Inactive bool
|
|
|
|
|
|
|
|
uses int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i Invite) Uses() int64 {
|
|
|
|
return i.uses
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i Invite) Expired() bool {
|
|
|
|
return i.Expires != nil && i.Expires.Before(time.Now())
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:18:23 +02:00
|
|
|
func (i Invite) Active(db *datastore) bool {
|
|
|
|
if i.Expired() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if i.MaxUses.Valid && i.MaxUses.Int64 > 0 {
|
|
|
|
if c := db.GetUsersInvitedCount(i.ID); c >= i.MaxUses.Int64 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-01-18 06:05:50 +01:00
|
|
|
func (i Invite) ExpiresFriendly() string {
|
|
|
|
return i.Expires.Format("January 2, 2006, 3:04 PM")
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func handleViewUserInvites(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
2019-01-18 06:05:50 +01:00
|
|
|
// Don't show page if instance doesn't allow it
|
|
|
|
if !(app.cfg.App.UserInvites != "" && (u.IsAdmin() || app.cfg.App.UserInvites != "admin")) {
|
|
|
|
return impart.HTTPError{http.StatusNotFound, ""}
|
|
|
|
}
|
|
|
|
|
|
|
|
f, _ := getSessionFlashes(app, w, r, nil)
|
|
|
|
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
2020-02-09 17:24:48 +01:00
|
|
|
Invites *[]Invite
|
|
|
|
Silenced bool
|
2019-01-18 06:05:50 +01:00
|
|
|
}{
|
|
|
|
UserPage: NewUserPage(app, r, u, "Invite People", f),
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2019-11-11 23:25:34 +01:00
|
|
|
|
2020-02-09 17:24:48 +01:00
|
|
|
p.Silenced, err = app.db.IsUserSilenced(u.ID)
|
2019-11-11 23:25:34 +01:00
|
|
|
if err != nil {
|
2021-06-27 23:57:07 +02:00
|
|
|
if err == ErrUserNotFound {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-11 23:25:34 +01:00
|
|
|
log.Error("view invites: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-18 06:05:50 +01:00
|
|
|
p.Invites, err = app.db.GetUserInvites(u.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range *p.Invites {
|
|
|
|
(*p.Invites)[i].uses = app.db.GetUsersInvitedCount((*p.Invites)[i].ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
showUserPage(w, "invite", p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func handleCreateUserInvite(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
2019-01-18 06:05:50 +01:00
|
|
|
muVal := r.FormValue("uses")
|
|
|
|
expVal := r.FormValue("expires")
|
|
|
|
|
2019-11-11 16:40:16 +01:00
|
|
|
if u.IsSilenced() {
|
2019-11-12 00:21:45 +01:00
|
|
|
return ErrUserSilenced
|
2019-08-28 21:37:45 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 06:05:50 +01:00
|
|
|
var err error
|
|
|
|
var maxUses int
|
|
|
|
if muVal != "0" {
|
|
|
|
maxUses, err = strconv.Atoi(muVal)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusBadRequest, "Invalid value for 'max_uses'"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var expDate *time.Time
|
|
|
|
var expires int
|
|
|
|
if expVal != "0" {
|
|
|
|
expires, err = strconv.Atoi(expVal)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusBadRequest, "Invalid value for 'expires'"}
|
|
|
|
}
|
|
|
|
ed := time.Now().Add(time.Duration(expires) * time.Minute)
|
|
|
|
expDate = &ed
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:49:12 +02:00
|
|
|
inviteID := id.GenerateRandomString("0123456789BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz", 6)
|
2019-01-18 06:05:50 +01:00
|
|
|
err = app.db.CreateUserInvite(inviteID, u.ID, maxUses, expDate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return impart.HTTPError{http.StatusFound, "/me/invites"}
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:55:30 +02:00
|
|
|
func handleViewInvite(app *App, w http.ResponseWriter, r *http.Request) error {
|
2019-01-18 06:05:50 +01:00
|
|
|
inviteCode := mux.Vars(r)["code"]
|
|
|
|
|
|
|
|
i, err := app.db.GetUserInvite(inviteCode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-23 15:45:36 +02:00
|
|
|
expired := i.Expired()
|
|
|
|
if !expired && i.MaxUses.Valid && i.MaxUses.Int64 > 0 {
|
|
|
|
// Invite has a max-use number, so check if we're past that limit
|
|
|
|
i.uses = app.db.GetUsersInvitedCount(inviteCode)
|
|
|
|
expired = i.uses >= i.MaxUses.Int64
|
|
|
|
}
|
|
|
|
|
2019-09-13 19:58:17 +02:00
|
|
|
if u := getUserSession(app, r); u != nil {
|
|
|
|
// check if invite belongs to another user
|
|
|
|
// error can be ignored as not important in this case
|
|
|
|
if ownInvite, _ := app.db.IsUsersInvite(inviteCode, u.ID); !ownInvite {
|
2019-09-23 16:44:23 +02:00
|
|
|
addSessionFlash(app, w, r, "You're already registered and logged in.", nil)
|
2019-09-13 19:58:17 +02:00
|
|
|
// show homepage
|
|
|
|
return impart.HTTPError{http.StatusFound, "/me/settings"}
|
|
|
|
}
|
2019-09-23 15:45:36 +02:00
|
|
|
|
2019-09-13 19:58:17 +02:00
|
|
|
// show invite instructions
|
|
|
|
p := struct {
|
|
|
|
*UserPage
|
2019-09-23 16:04:45 +02:00
|
|
|
Invite *Invite
|
2019-09-23 15:45:36 +02:00
|
|
|
Expired bool
|
2019-09-13 19:58:17 +02:00
|
|
|
}{
|
2019-09-23 16:02:36 +02:00
|
|
|
UserPage: NewUserPage(app, r, u, "Invite to "+app.cfg.App.SiteName, nil),
|
2019-09-23 16:04:45 +02:00
|
|
|
Invite: i,
|
2019-09-23 15:45:36 +02:00
|
|
|
Expired: expired,
|
2019-09-13 19:58:17 +02:00
|
|
|
}
|
2019-09-23 16:31:38 +02:00
|
|
|
showUserPage(w, "invite-help", p)
|
2019-09-13 19:58:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-18 06:05:50 +01:00
|
|
|
p := struct {
|
|
|
|
page.StaticPage
|
2020-08-19 19:35:21 +02:00
|
|
|
*OAuthButtons
|
2019-01-18 06:05:50 +01:00
|
|
|
Error string
|
|
|
|
Flashes []template.HTML
|
|
|
|
Invite string
|
|
|
|
}{
|
2020-08-19 19:35:21 +02:00
|
|
|
StaticPage: pageForReq(app, r),
|
|
|
|
OAuthButtons: NewOAuthButtons(app.cfg),
|
|
|
|
Invite: inviteCode,
|
2019-01-18 06:05:50 +01:00
|
|
|
}
|
|
|
|
|
2019-09-23 15:45:36 +02:00
|
|
|
if expired {
|
2019-01-18 06:05:50 +01:00
|
|
|
p.Error = "This invite link has expired."
|
|
|
|
}
|
|
|
|
|
2020-02-23 01:46:36 +01:00
|
|
|
// Tell search engines not to index invite links
|
|
|
|
w.Header().Set("X-Robots-Tag", "noindex")
|
|
|
|
|
2019-01-18 06:05:50 +01:00
|
|
|
// Get error messages
|
|
|
|
session, err := app.sessionStore.Get(r, cookieName)
|
|
|
|
if err != nil {
|
|
|
|
// Ignore this
|
|
|
|
log.Error("Unable to get session in handleViewInvite; ignoring: %v", err)
|
|
|
|
}
|
|
|
|
flashes, _ := getSessionFlashes(app, w, r, session)
|
|
|
|
for _, flash := range flashes {
|
|
|
|
p.Flashes = append(p.Flashes, template.HTML(flash))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show landing page
|
|
|
|
return renderPage(w, "signup.tmpl", p)
|
|
|
|
}
|