Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d954b7c

Browse files
author
Rob Loranger
committed
add user invite instructions
this adds a new page with instructions for sharing user invites if a user clicks the link for one of their own invite codes they are directed to a page with clear instructions for it's use. if a user clicks another users link they are redirectec to their account settings witha flash telling them they do not need to register.
1 parent 6b99d75 commit d954b7c

4 files changed

Lines changed: 68 additions & 8 deletions

File tree

database.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,19 @@ func (db *datastore) GetUserInvite(id string) (*Invite, error) {
22572257
return &i, nil
22582258
}
22592259

2260+
// IsUsersInvite returns true if the user with ID created the invite with code
2261+
// and an error other than sql no rows, if any. Will return false in the event
2262+
// of an error.
2263+
func (db *datastore) IsUsersInvite(code string, userID int64) (bool, error) {
2264+
var id string
2265+
err := db.QueryRow("SELECT id FROM userinvites WHERE id = ? AND owner_id = ?", code, userID).Scan(&id)
2266+
if err != nil && err != sql.ErrNoRows {
2267+
log.Error("Failed selecting invite: %v", err)
2268+
return false, err
2269+
}
2270+
return id != "", nil
2271+
}
2272+
22602273
func (db *datastore) GetUsersInvitedCount(id string) int64 {
22612274
var count int64
22622275
err := db.QueryRow("SELECT COUNT(*) FROM usersinvited WHERE invite_id = ?", id).Scan(&count)

invites.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ package writefreely
1212

1313
import (
1414
"database/sql"
15+
"html/template"
16+
"net/http"
17+
"strconv"
18+
"time"
19+
1520
"github.com/gorilla/mux"
1621
"github.com/writeas/impart"
1722
"github.com/writeas/nerds/store"
1823
"github.com/writeas/web-core/log"
1924
"github.com/writeas/writefreely/page"
20-
"html/template"
21-
"net/http"
22-
"strconv"
23-
"time"
2425
)
2526

2627
type Invite struct {
@@ -109,6 +110,26 @@ func handleCreateUserInvite(app *App, u *User, w http.ResponseWriter, r *http.Re
109110
func handleViewInvite(app *App, w http.ResponseWriter, r *http.Request) error {
110111
inviteCode := mux.Vars(r)["code"]
111112

113+
if u := getUserSession(app, r); u != nil {
114+
// check if invite belongs to another user
115+
// error can be ignored as not important in this case
116+
if ownInvite, _ := app.db.IsUsersInvite(inviteCode, u.ID); !ownInvite {
117+
addSessionFlash(app, w, r, "No need for an invite, You are already registered.", nil)
118+
// show homepage
119+
return impart.HTTPError{http.StatusFound, "/me/settings"}
120+
}
121+
// show invite instructions
122+
p := struct {
123+
*UserPage
124+
InviteID string
125+
}{
126+
UserPage: NewUserPage(app, r, u, "Invite Instructions", nil),
127+
InviteID: inviteCode,
128+
}
129+
showUserPage(w, "invite-instructions", p)
130+
return nil
131+
}
132+
112133
i, err := app.db.GetUserInvite(inviteCode)
113134
if err != nil {
114135
return err

routes.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
package writefreely
1212

1313
import (
14+
"net/http"
15+
"path/filepath"
16+
"strings"
17+
1418
"github.com/gorilla/mux"
1519
"github.com/writeas/go-webfinger"
1620
"github.com/writeas/web-core/log"
1721
"github.com/writefreely/go-nodeinfo"
18-
"net/http"
19-
"path/filepath"
20-
"strings"
2122
)
2223

2324
// InitStaticRoutes adds routes for serving static files.
@@ -151,7 +152,7 @@ func InitRoutes(apper Apper, r *mux.Router) *mux.Router {
151152
// Handle special pages first
152153
write.HandleFunc("/login", handler.Web(viewLogin, UserLevelNoneRequired))
153154
write.HandleFunc("/signup", handler.Web(handleViewLanding, UserLevelNoneRequired))
154-
write.HandleFunc("/invite/{code}", handler.Web(handleViewInvite, UserLevelNoneRequired)).Methods("GET")
155+
write.HandleFunc("/invite/{code}", handler.Web(handleViewInvite, UserLevelOptional)).Methods("GET")
155156
// TODO: show a reader-specific 404 page if the function is disabled
156157
write.HandleFunc("/read", handler.Web(viewLocalTimeline, UserLevelReader))
157158
RouteRead(handler, UserLevelReader, write.PathPrefix("/read").Subrouter())
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{{define "invite-instructions"}}
2+
{{template "header" .}}
3+
<style>
4+
.copy-link {
5+
width: 100%;
6+
margin: 2em 0;
7+
text-align: center;
8+
font-size-adjust: .7;
9+
color: #555;
10+
}
11+
</style>
12+
<div class="snug content-container">
13+
<h1>Invite Instructions</h1>
14+
<p>This is a special link that you can send to anyone you want to join <em>{{ .SiteName }}</em>. Copy the link below and paste it into an email, instant message, or text message and send it to the person you want. When they navigate to the link, they'll be able to create an account.</p>
15+
<input
16+
class="copy-link"
17+
type="text"
18+
name="invite-url"
19+
value="{{$.Host}}/invite/{{.InviteID}}"
20+
onfocus="if (this.select) this.select(); else this.setSelectionRange(0, this.value.length);"
21+
readonly/>
22+
</div>
23+
24+
{{template "footer" .}}
25+
{{end}}

0 commit comments

Comments
 (0)