Support configuring default collection visibility

This adds a new `default_visibility` config value that lets an instance
admin set the visibility of newly created collections.

Ref T675
This commit is contained in:
Matt Baer 2019-07-31 22:18:40 -04:00
parent ba3cb4b4ff
commit a75b45f060
5 changed files with 46 additions and 18 deletions

View File

@ -163,7 +163,7 @@ func signupWithRegistration(app *App, signup userRegistration, w http.ResponseWr
} }
// Create actual user // Create actual user
if err := app.db.CreateUser(u, desiredUsername); err != nil { if err := app.db.CreateUser(app, u, desiredUsername); err != nil {
return nil, err return nil, err
} }

4
app.go
View File

@ -507,7 +507,7 @@ func DoConfig(app *App, configSections string) {
// Create blog // Create blog
log.Info("Creating user %s...\n", u.Username) log.Info("Creating user %s...\n", u.Username)
err = app.db.CreateUser(u, app.cfg.App.SiteName) err = app.db.CreateUser(app, u, app.cfg.App.SiteName)
if err != nil { if err != nil {
log.Error("Unable to create user: %s", err) log.Error("Unable to create user: %s", err)
os.Exit(1) os.Exit(1)
@ -702,7 +702,7 @@ func CreateUser(apper Apper, username, password string, isAdmin bool) error {
userType = "admin" userType = "admin"
} }
log.Info("Creating %s %s...", userType, usernameDesc) log.Info("Creating %s %s...", userType, usernameDesc)
err = apper.App().db.CreateUser(u, desiredUsername) err = apper.App().db.CreateUser(apper.App(), u, desiredUsername)
if err != nil { if err != nil {
return fmt.Errorf("Unable to create user: %s", err) return fmt.Errorf("Unable to create user: %s", err)
} }

View File

@ -31,6 +31,7 @@ import (
"github.com/writeas/web-core/log" "github.com/writeas/web-core/log"
waposts "github.com/writeas/web-core/posts" waposts "github.com/writeas/web-core/posts"
"github.com/writeas/writefreely/author" "github.com/writeas/writefreely/author"
"github.com/writeas/writefreely/config"
"github.com/writeas/writefreely/page" "github.com/writeas/writefreely/page"
) )
@ -126,6 +127,21 @@ const (
CollProtected CollProtected
) )
var collVisibilityStrings = map[string]collVisibility{
"unlisted": CollUnlisted,
"public": CollPublic,
"private": CollPrivate,
"protected": CollProtected,
}
func defaultVisibility(cfg *config.Config) collVisibility {
vis, ok := collVisibilityStrings[cfg.App.DefaultVisibility]
if !ok {
vis = CollUnlisted
}
return vis
}
func (cf *CollectionFormat) Ascending() bool { func (cf *CollectionFormat) Ascending() bool {
return cf.Format == "novel" return cf.Format == "novel"
} }
@ -358,35 +374,44 @@ func newCollection(app *App, w http.ResponseWriter, r *http.Request) error {
return impart.HTTPError{http.StatusBadRequest, fmt.Sprintf("Parameter(s) %srequired.", missingParams)} return impart.HTTPError{http.StatusBadRequest, fmt.Sprintf("Parameter(s) %srequired.", missingParams)}
} }
var userID int64
if reqJSON && !c.Web { if reqJSON && !c.Web {
accessToken = r.Header.Get("Authorization") accessToken = r.Header.Get("Authorization")
if accessToken == "" { if accessToken == "" {
return ErrNoAccessToken return ErrNoAccessToken
} }
userID = app.db.GetUserID(accessToken)
if userID == -1 {
return ErrBadAccessToken
}
} else { } else {
u = getUserSession(app, r) u = getUserSession(app, r)
if u == nil { if u == nil {
return ErrNotLoggedIn return ErrNotLoggedIn
} }
userID = u.ID
} }
if !author.IsValidUsername(app.cfg, c.Alias) { if !author.IsValidUsername(app.cfg, c.Alias) {
return impart.HTTPError{http.StatusPreconditionFailed, "Collection alias isn't valid."} return impart.HTTPError{http.StatusPreconditionFailed, "Collection alias isn't valid."}
} }
var coll *Collection coll, err := app.db.CreateCollection(c.Alias, c.Title, userID)
var err error if err != nil {
if accessToken != "" { // TODO: handle this
coll, err = app.db.CreateCollectionFromToken(c.Alias, c.Title, accessToken) return err
}
// Set visibility to configured default
vis := defaultVisibility(app.cfg)
if vis != CollUnlisted {
visInt := int(vis)
err = app.db.UpdateCollection(&SubmittedCollection{
OwnerID: uint64(userID),
Visibility: &visInt,
}, coll.Alias)
if err != nil { if err != nil {
// TODO: handle this log.Error("Unable to set default visibility: %s", err)
return err
}
} else {
coll, err = app.db.CreateCollection(c.Alias, c.Title, u.ID)
if err != nil {
// TODO: handle this
return err
} }
} }

View File

@ -83,6 +83,9 @@ type (
// Additional functions // Additional functions
LocalTimeline bool `ini:"local_timeline"` LocalTimeline bool `ini:"local_timeline"`
UserInvites string `ini:"user_invites"` UserInvites string `ini:"user_invites"`
// Defaults
DefaultVisibility string `ini:"default_visibility"`
} }
// Config holds the complete configuration for running a writefreely instance // Config holds the complete configuration for running a writefreely instance

View File

@ -44,7 +44,7 @@ var (
) )
type writestore interface { type writestore interface {
CreateUser(*User, string) error CreateUser(*App, *User, string) error
UpdateUserEmail(keys *key.Keychain, userID int64, email string) error UpdateUserEmail(keys *key.Keychain, userID int64, email string) error
UpdateEncryptedUserEmail(int64, []byte) error UpdateEncryptedUserEmail(int64, []byte) error
GetUserByID(int64) (*User, error) GetUserByID(int64) (*User, error)
@ -162,7 +162,7 @@ func (db *datastore) dateSub(l int, unit string) string {
return fmt.Sprintf("DATE_SUB(NOW(), INTERVAL %d %s)", l, unit) return fmt.Sprintf("DATE_SUB(NOW(), INTERVAL %d %s)", l, unit)
} }
func (db *datastore) CreateUser(u *User, collectionTitle string) error { func (db *datastore) CreateUser(app *App, u *User, collectionTitle string) error {
if db.PostIDExists(u.Username) { if db.PostIDExists(u.Username) {
return impart.HTTPError{http.StatusConflict, "Invalid collection name."} return impart.HTTPError{http.StatusConflict, "Invalid collection name."}
} }
@ -196,7 +196,7 @@ func (db *datastore) CreateUser(u *User, collectionTitle string) error {
if collectionTitle == "" { if collectionTitle == "" {
collectionTitle = u.Username collectionTitle = u.Username
} }
res, err = t.Exec("INSERT INTO collections (alias, title, description, privacy, owner_id, view_count) VALUES (?, ?, ?, ?, ?, ?)", u.Username, collectionTitle, "", CollUnlisted, u.ID, 0) res, err = t.Exec("INSERT INTO collections (alias, title, description, privacy, owner_id, view_count) VALUES (?, ?, ?, ?, ?, ?)", u.Username, collectionTitle, "", defaultVisibility(app.cfg), u.ID, 0)
if err != nil { if err != nil {
t.Rollback() t.Rollback()
if db.isDuplicateKeyErr(err) { if db.isDuplicateKeyErr(err) {