2018-12-24 18:45:15 +01:00
/ *
2022-12-25 06:47:41 +01:00
* Copyright © 2018 - 2022 Musing Studio LLC .
2018-12-24 18:45:15 +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 .
* /
2018-12-31 07:05:26 +01:00
2018-10-17 04:31:27 +02:00
package writefreely
import (
"database/sql"
2018-11-08 07:19:03 +01:00
"encoding/json"
"fmt"
"html/template"
"math"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"unicode"
"github.com/gorilla/mux"
2022-01-18 22:43:17 +01:00
stripmd "github.com/writeas/go-strip-markdown/v2"
2018-11-08 07:19:03 +01:00
"github.com/writeas/impart"
"github.com/writeas/web-core/activitystreams"
"github.com/writeas/web-core/auth"
"github.com/writeas/web-core/bots"
2021-09-14 00:36:36 +02:00
"github.com/writeas/web-core/i18n"
2018-11-08 07:19:03 +01:00
"github.com/writeas/web-core/log"
2022-01-18 22:43:17 +01:00
"github.com/writeas/web-core/posts"
2021-04-06 23:24:07 +02:00
"github.com/writefreely/writefreely/author"
"github.com/writefreely/writefreely/config"
"github.com/writefreely/writefreely/page"
2021-06-22 00:24:40 +02:00
"github.com/writefreely/writefreely/spam"
2021-04-26 17:18:51 +02:00
"golang.org/x/net/idna"
2018-10-17 04:31:27 +02:00
)
2023-10-03 17:55:52 +02:00
const (
collAttrLetterReplyTo = "letter_reply_to"
collMaxLengthTitle = 255
collMaxLengthDescription = 160
)
2021-06-22 00:24:40 +02:00
2018-10-17 04:31:27 +02:00
type (
2018-11-08 07:19:03 +01:00
// TODO: add Direction to db
// TODO: add Language to db
2018-10-17 04:31:27 +02:00
Collection struct {
2018-11-08 07:19:03 +01:00
ID int64 ` datastore:"id" json:"-" `
Alias string ` datastore:"alias" schema:"alias" json:"alias" `
Title string ` datastore:"title" schema:"title" json:"title" `
Description string ` datastore:"description" schema:"description" json:"description" `
Direction string ` schema:"dir" json:"dir,omitempty" `
Language string ` schema:"lang" json:"lang,omitempty" `
StyleSheet string ` datastore:"style_sheet" schema:"style_sheet" json:"style_sheet" `
Script string ` datastore:"script" schema:"script" json:"script,omitempty" `
2020-06-23 22:24:45 +02:00
Signature string ` datastore:"post_signature" schema:"signature" json:"-" `
2018-11-08 07:19:03 +01:00
Public bool ` datastore:"public" json:"public" `
Visibility collVisibility ` datastore:"private" json:"-" `
Format string ` datastore:"format" json:"format,omitempty" `
Views int64 ` json:"views" `
OwnerID int64 ` datastore:"owner_id" json:"-" `
PublicOwner bool ` datastore:"public_owner" json:"-" `
URL string ` json:"url,omitempty" `
2021-03-25 18:02:35 +01:00
Monetization string ` json:"monetization_pointer,omitempty" `
2023-09-22 01:04:34 +02:00
Verification string ` json:"verification_link" `
2020-09-30 20:40:13 +02:00
2019-06-15 00:54:04 +02:00
db * datastore
hostName string
2018-10-17 04:31:27 +02:00
}
CollectionObj struct {
Collection
TotalPosts int ` json:"total_posts" `
Owner * User ` json:"owner,omitempty" `
Posts * [ ] PublicPost ` json:"posts,omitempty" `
2020-01-09 22:48:22 +01:00
Format * CollectionFormat
2018-10-17 04:31:27 +02:00
}
2018-11-08 07:19:03 +01:00
DisplayCollection struct {
* CollectionObj
Prefix string
2023-09-21 22:58:44 +02:00
NavSuffix string
2018-11-08 07:19:03 +01:00
IsTopLevel bool
CurrentPage int
TotalPages int
2019-11-12 00:21:45 +01:00
Silenced bool
2018-11-08 07:19:03 +01:00
}
2023-09-25 22:55:57 +02:00
CollectionNav struct {
* Collection
Path string
SingleUser bool
CanPost bool
}
2018-10-17 04:31:27 +02:00
SubmittedCollection struct {
// Data used for updating a given collection
ID int64
OwnerID uint64
// Form helpers
PreferURL string ` schema:"prefer_url" json:"prefer_url" `
Privacy int ` schema:"privacy" json:"privacy" `
Pass string ` schema:"password" json:"password" `
MathJax bool ` schema:"mathjax" json:"mathjax" `
2021-06-22 00:24:40 +02:00
EmailSubs bool ` schema:"email_subs" json:"email_subs" `
2018-10-17 04:31:27 +02:00
Handle string ` schema:"handle" json:"handle" `
// Actual collection values updated in the DB
2020-09-30 20:40:13 +02:00
Alias * string ` schema:"alias" json:"alias" `
Title * string ` schema:"title" json:"title" `
Description * string ` schema:"description" json:"description" `
2023-11-07 16:54:16 +01:00
StyleSheet * string ` schema:"style_sheet" json:"style_sheet" `
Script * string ` schema:"script" json:"script" `
Signature * string ` schema:"signature" json:"signature" `
2020-09-30 20:40:13 +02:00
Monetization * string ` schema:"monetization_pointer" json:"monetization_pointer" `
2023-09-22 01:04:34 +02:00
Verification * string ` schema:"verification_link" json:"verification_link" `
2021-06-22 00:24:40 +02:00
LetterReply * string ` schema:"letter_reply" json:"letter_reply" `
2020-09-30 20:40:13 +02:00
Visibility * int ` schema:"visibility" json:"public" `
Format * sql . NullString ` schema:"format" json:"format" `
2018-10-17 04:31:27 +02:00
}
CollectionFormat struct {
Format string
}
2018-11-08 07:19:03 +01:00
collectionReq struct {
// Information about the collection request itself
prefix , alias , domain string
isCustomDomain bool
// User-related fields
isCollOwner bool
2021-03-30 02:55:21 +02:00
isAuthorized bool
2018-11-08 07:19:03 +01:00
}
2018-10-17 04:31:27 +02:00
)
2018-11-08 07:19:03 +01:00
func ( sc * SubmittedCollection ) FediverseHandle ( ) string {
if sc . Handle == "" {
return apCustomHandleDefault
}
return getSlug ( sc . Handle , "" )
}
2018-10-17 04:31:27 +02:00
// collVisibility represents the visibility level for the collection.
type collVisibility int
2018-11-08 07:19:03 +01:00
// Visibility levels. Values are bitmasks, stored in the database as
// decimal numbers. If adding types, append them to this list. If removing,
// replace the desired visibility with a new value.
const CollUnlisted collVisibility = 0
const (
CollPublic collVisibility = 1 << iota
CollPrivate
CollProtected
)
2019-08-01 04:18:40 +02:00
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
}
2018-11-08 07:19:03 +01:00
func ( cf * CollectionFormat ) Ascending ( ) bool {
return cf . Format == "novel"
}
func ( cf * CollectionFormat ) ShowDates ( ) bool {
return cf . Format == "blog"
}
func ( cf * CollectionFormat ) PostsPerPage ( ) int {
if cf . Format == "novel" {
return postsPerPage
}
return postsPerPage
}
// Valid returns whether or not a format value is valid.
func ( cf * CollectionFormat ) Valid ( ) bool {
return cf . Format == "blog" ||
cf . Format == "novel" ||
cf . Format == "notebook"
}
// NewFormat creates a new CollectionFormat object from the Collection.
func ( c * Collection ) NewFormat ( ) * CollectionFormat {
cf := & CollectionFormat { Format : c . Format }
// Fill in default format
if cf . Format == "" {
cf . Format = "blog"
}
return cf
}
2021-03-08 18:54:50 +01:00
func ( c * Collection ) IsInstanceColl ( ) bool {
ur , _ := url . Parse ( c . hostName )
return c . Alias == ur . Host
}
2018-11-08 07:19:03 +01:00
func ( c * Collection ) IsUnlisted ( ) bool {
return c . Visibility == 0
}
func ( c * Collection ) IsPrivate ( ) bool {
return c . Visibility & CollPrivate != 0
}
func ( c * Collection ) IsProtected ( ) bool {
return c . Visibility & CollProtected != 0
}
func ( c * Collection ) IsPublic ( ) bool {
return c . Visibility & CollPublic != 0
}
func ( c * Collection ) FriendlyVisibility ( ) string {
if c . IsPrivate ( ) {
return "Private"
}
if c . IsPublic ( ) {
return "Public"
}
if c . IsProtected ( ) {
return "Password-protected"
}
return "Unlisted"
}
func ( c * Collection ) ShowFooterBranding ( ) bool {
// TODO: implement this setting
return true
}
// CanonicalURL returns a fully-qualified URL to the collection.
func ( c * Collection ) CanonicalURL ( ) string {
return c . RedirectingCanonicalURL ( false )
}
func ( c * Collection ) DisplayCanonicalURL ( ) string {
us := c . CanonicalURL ( )
u , err := url . Parse ( us )
if err != nil {
return us
}
p := u . Path
if p == "/" {
p = ""
}
2021-04-26 17:18:51 +02:00
d := u . Hostname ( )
d , _ = idna . ToUnicode ( d )
return d + p
2018-11-08 07:19:03 +01:00
}
2021-06-23 22:50:23 +02:00
// RedirectingCanonicalURL returns the fully-qualified canonical URL for the Collection, with a trailing slash. The
// hostName field needs to be populated for this to work correctly.
2018-11-08 07:19:03 +01:00
func ( c * Collection ) RedirectingCanonicalURL ( isRedir bool ) string {
2019-07-22 20:16:44 +02:00
if c . hostName == "" {
// If this is true, the human programmers screwed up. So ask for a bug report and fail, fail, fail
2021-04-06 23:24:07 +02:00
log . Error ( "[PROGRAMMER ERROR] WARNING: Collection.hostName is empty! Federation and many other things will fail! If you're seeing this in the wild, please report this bug and let us know what you were doing just before this: https://github.com/writefreely/writefreely/issues/new?template=bug_report.md" )
2019-07-22 20:16:44 +02:00
}
2018-11-08 07:19:03 +01:00
if isSingleUser {
2019-06-15 00:54:04 +02:00
return c . hostName + "/"
2018-11-08 07:19:03 +01:00
}
2019-06-15 00:54:04 +02:00
return fmt . Sprintf ( "%s/%s/" , c . hostName , c . Alias )
2018-11-08 07:19:03 +01:00
}
// PrevPageURL provides a full URL for the previous page of collection posts,
// returning a /page/N result for pages >1
2023-09-21 22:58:44 +02:00
func ( c * Collection ) PrevPageURL ( prefix , navSuffix string , n int , tl bool ) string {
2018-11-08 07:19:03 +01:00
u := ""
if n == 2 {
// Previous page is 1; no need for /page/ prefix
if prefix == "" {
2023-09-21 22:58:44 +02:00
u = navSuffix + "/"
2018-11-08 07:19:03 +01:00
}
// Else leave off trailing slash
} else {
2023-09-21 22:58:44 +02:00
u = fmt . Sprintf ( "%s/page/%d" , navSuffix , n - 1 )
2018-11-08 07:19:03 +01:00
}
if tl {
return u
}
return "/" + prefix + c . Alias + u
}
// NextPageURL provides a full URL for the next page of collection posts
2023-09-21 22:58:44 +02:00
func ( c * Collection ) NextPageURL ( prefix , navSuffix string , n int , tl bool ) string {
2018-11-08 07:19:03 +01:00
if tl {
2023-09-21 22:58:44 +02:00
return fmt . Sprintf ( "%s/page/%d" , navSuffix , n + 1 )
2018-11-08 07:19:03 +01:00
}
2023-09-21 22:58:44 +02:00
return fmt . Sprintf ( "/%s%s%s/page/%d" , prefix , c . Alias , navSuffix , n + 1 )
2018-11-08 07:19:03 +01:00
}
func ( c * Collection ) DisplayTitle ( ) string {
if c . Title != "" {
return c . Title
}
return c . Alias
}
func ( c * Collection ) StyleSheetDisplay ( ) template . CSS {
return template . CSS ( c . StyleSheet )
}
// ForPublic modifies the Collection for public consumption, such as via
// the API.
func ( c * Collection ) ForPublic ( ) {
c . URL = c . CanonicalURL ( )
}
2019-01-07 21:40:37 +01:00
var isAvatarChar = regexp . MustCompile ( "[a-z0-9]" ) . MatchString
2018-11-08 07:19:03 +01:00
func ( c * Collection ) PersonObject ( ids ... int64 ) * activitystreams . Person {
accountRoot := c . FederatedAccount ( )
p := activitystreams . NewPerson ( accountRoot )
p . URL = c . CanonicalURL ( )
uname := c . Alias
p . PreferredUsername = uname
p . Name = c . DisplayTitle ( )
p . Summary = c . Description
if p . Name != "" {
2018-11-11 21:28:11 +01:00
if av := c . AvatarURL ( ) ; av != "" {
2018-11-08 07:19:03 +01:00
p . Icon = activitystreams . Image {
Type : "Image" ,
MediaType : "image/png" ,
2018-11-11 21:28:11 +01:00
URL : av ,
2018-11-08 07:19:03 +01:00
}
}
}
collID := c . ID
if len ( ids ) > 0 {
collID = ids [ 0 ]
}
pub , priv := c . db . GetAPActorKeys ( collID )
if pub != nil {
p . AddPubKey ( pub )
p . SetPrivKey ( priv )
}
return p
}
2018-11-11 21:28:11 +01:00
func ( c * Collection ) AvatarURL ( ) string {
fl := string ( unicode . ToLower ( [ ] rune ( c . DisplayTitle ( ) ) [ 0 ] ) )
2019-01-07 21:40:37 +01:00
if ! isAvatarChar ( fl ) {
2018-11-11 21:28:11 +01:00
return ""
}
2019-06-15 00:54:04 +02:00
return c . hostName + "/img/avatars/" + fl + ".png"
2018-11-11 21:28:11 +01:00
}
2018-11-08 07:19:03 +01:00
func ( c * Collection ) FederatedAPIBase ( ) string {
2019-06-15 00:54:04 +02:00
return c . hostName + "/"
2018-11-08 07:19:03 +01:00
}
func ( c * Collection ) FederatedAccount ( ) string {
accountUser := c . Alias
return c . FederatedAPIBase ( ) + "api/collections/" + accountUser
}
func ( c * Collection ) RenderMathJax ( ) bool {
return c . db . CollectionHasAttribute ( c . ID , "render_mathjax" )
}
2021-06-22 00:24:40 +02:00
func ( c * Collection ) EmailSubsEnabled ( ) bool {
return c . db . CollectionHasAttribute ( c . ID , "email_subs" )
}
2021-06-07 21:52:24 +02:00
func ( c * Collection ) MonetizationURL ( ) string {
if c . Monetization == "" {
return ""
}
return strings . Replace ( c . Monetization , "$" , "https://" , 1 )
}
2022-01-18 22:43:17 +01:00
// DisplayDescription returns the description with rendered Markdown and HTML.
func ( c * Collection ) DisplayDescription ( ) * template . HTML {
if c . Description == "" {
s := template . HTML ( "" )
return & s
}
t := template . HTML ( posts . ApplyBasicAccessibleMarkdown ( [ ] byte ( c . Description ) ) )
return & t
}
// PlainDescription returns the description with all Markdown and HTML removed.
func ( c * Collection ) PlainDescription ( ) string {
if c . Description == "" {
return ""
}
desc := stripHTMLWithoutEscaping ( c . Description )
desc = stripmd . Strip ( desc )
return desc
}
2021-06-07 21:52:24 +02:00
func ( c CollectionPage ) DisplayMonetization ( ) string {
return displayMonetization ( c . Monetization , c . Alias )
}
2021-09-14 00:36:36 +02:00
func ( c * DisplayCollection ) Direction ( ) string {
if c . Language == "" {
return "auto"
}
if i18n . LangIsRTL ( c . Language ) {
return "rtl"
}
return "ltr"
}
2019-05-12 22:55:30 +02:00
func newCollection ( app * App , w http . ResponseWriter , r * http . Request ) error {
2019-09-18 21:39:53 +02:00
reqJSON := IsJSON ( r )
2018-11-08 07:19:03 +01:00
alias := r . FormValue ( "alias" )
title := r . FormValue ( "title" )
var missingParams , accessToken string
var u * User
c := struct {
Alias string ` json:"alias" schema:"alias" `
Title string ` json:"title" schema:"title" `
Web bool ` json:"web" schema:"web" `
} { }
if reqJSON {
// Decode JSON request
decoder := json . NewDecoder ( r . Body )
err := decoder . Decode ( & c )
if err != nil {
log . Error ( "Couldn't parse post update JSON request: %v\n" , err )
return ErrBadJSON
}
} else {
// TODO: move form parsing to formDecoder
c . Alias = alias
c . Title = title
}
if c . Alias == "" {
if c . Title != "" {
// If only a title was given, just use it to generate the alias.
c . Alias = getSlug ( c . Title , "" )
} else {
missingParams += "`alias` "
}
}
if c . Title == "" {
missingParams += "`title` "
}
if missingParams != "" {
return impart . HTTPError { http . StatusBadRequest , fmt . Sprintf ( "Parameter(s) %srequired." , missingParams ) }
}
2019-08-01 04:18:40 +02:00
var userID int64
2019-08-28 21:37:45 +02:00
var err error
2018-11-08 07:19:03 +01:00
if reqJSON && ! c . Web {
accessToken = r . Header . Get ( "Authorization" )
if accessToken == "" {
return ErrNoAccessToken
}
2019-08-01 04:18:40 +02:00
userID = app . db . GetUserID ( accessToken )
if userID == - 1 {
return ErrBadAccessToken
}
2018-11-08 07:19:03 +01:00
} else {
u = getUserSession ( app , r )
if u == nil {
return ErrNotLoggedIn
}
2019-08-01 04:18:40 +02:00
userID = u . ID
2018-11-08 07:19:03 +01:00
}
2019-11-12 00:21:45 +01:00
silenced , err := app . db . IsUserSilenced ( userID )
2019-08-28 21:37:45 +02:00
if err != nil {
2019-10-25 21:04:24 +02:00
log . Error ( "new collection: %v" , err )
2019-08-28 21:37:45 +02:00
return ErrInternalGeneral
}
2019-11-12 00:21:45 +01:00
if silenced {
return ErrUserSilenced
2019-08-28 21:37:45 +02:00
}
2018-11-08 07:19:03 +01:00
if ! author . IsValidUsername ( app . cfg , c . Alias ) {
return impart . HTTPError { http . StatusPreconditionFailed , "Collection alias isn't valid." }
}
2019-08-07 22:20:32 +02:00
coll , err := app . db . CreateCollection ( app . cfg , c . Alias , c . Title , userID )
2019-08-01 04:18:40 +02:00
if err != nil {
// TODO: handle this
return err
2018-11-08 07:19:03 +01:00
}
res := & CollectionObj { Collection : * coll }
if reqJSON {
return impart . WriteSuccess ( w , res , http . StatusCreated )
}
redirectTo := "/me/c/"
// TODO: redirect to pad when necessary
return impart . HTTPError { http . StatusFound , redirectTo }
}
2019-05-12 22:55:30 +02:00
func apiCheckCollectionPermissions ( app * App , r * http . Request , c * Collection ) ( int64 , error ) {
2018-11-08 07:19:03 +01:00
accessToken := r . Header . Get ( "Authorization" )
var userID int64 = - 1
if accessToken != "" {
userID = app . db . GetUserID ( accessToken )
}
isCollOwner := userID == c . OwnerID
if c . IsPrivate ( ) && ! isCollOwner {
// Collection is private, but user isn't authenticated
return - 1 , ErrCollectionNotFound
}
if c . IsProtected ( ) {
// TODO: check access token
return - 1 , ErrCollectionUnauthorizedRead
}
return userID , nil
}
// fetchCollection handles the API endpoint for retrieving collection data.
2019-05-12 22:55:30 +02:00
func fetchCollection ( app * App , w http . ResponseWriter , r * http . Request ) error {
2023-09-21 22:16:57 +02:00
if IsActivityPubRequest ( r ) {
2018-11-08 07:19:03 +01:00
return handleFetchCollectionActivities ( app , w , r )
}
vars := mux . Vars ( r )
alias := vars [ "alias" ]
// TODO: move this logic into a common getCollection function
// Get base Collection data
c , err := app . db . GetCollection ( alias )
if err != nil {
return err
}
2019-06-15 00:54:04 +02:00
c . hostName = app . cfg . App . Host
2018-11-08 07:19:03 +01:00
// Redirect users who aren't requesting JSON
2019-09-18 21:39:53 +02:00
reqJSON := IsJSON ( r )
2018-11-08 07:19:03 +01:00
if ! reqJSON {
return impart . HTTPError { http . StatusFound , c . CanonicalURL ( ) }
}
// Check permissions
userID , err := apiCheckCollectionPermissions ( app , r , c )
if err != nil {
return err
}
isCollOwner := userID == c . OwnerID
// Fetch extra data about the Collection
res := & CollectionObj { Collection : * c }
if c . PublicOwner {
u , err := app . db . GetUserByID ( res . OwnerID )
if err != nil {
// Log the error and just continue
log . Error ( "Error getting user for collection: %v" , err )
} else {
res . Owner = u
}
}
2019-11-12 00:21:45 +01:00
// TODO: check status for silenced
2018-11-08 07:19:03 +01:00
app . db . GetPostsCount ( res , isCollOwner )
// Strip non-public information
res . Collection . ForPublic ( )
return impart . WriteSuccess ( w , res , http . StatusOK )
}
// fetchCollectionPosts handles an API endpoint for retrieving a collection's
// posts.
2019-05-12 22:55:30 +02:00
func fetchCollectionPosts ( app * App , w http . ResponseWriter , r * http . Request ) error {
2018-11-08 07:19:03 +01:00
vars := mux . Vars ( r )
alias := vars [ "alias" ]
c , err := app . db . GetCollection ( alias )
if err != nil {
return err
}
2019-06-15 00:54:04 +02:00
c . hostName = app . cfg . App . Host
2018-11-08 07:19:03 +01:00
// Check permissions
userID , err := apiCheckCollectionPermissions ( app , r , c )
if err != nil {
return err
}
isCollOwner := userID == c . OwnerID
// Get page
page := 1
if p := r . FormValue ( "page" ) ; p != "" {
pInt , _ := strconv . Atoi ( p )
if pInt > 0 {
page = pInt
}
}
2022-01-18 22:43:17 +01:00
ps , err := app . db . GetPosts ( app . cfg , c , page , isCollOwner , false , false )
2018-11-08 07:19:03 +01:00
if err != nil {
return err
}
2022-01-18 22:43:17 +01:00
coll := & CollectionObj { Collection : * c , Posts : ps }
2018-11-08 07:19:03 +01:00
app . db . GetPostsCount ( coll , isCollOwner )
// Strip non-public information
coll . Collection . ForPublic ( )
// Transform post bodies if needed
if r . FormValue ( "body" ) == "html" {
for _ , p := range * coll . Posts {
2022-01-18 22:43:17 +01:00
p . Content = posts . ApplyMarkdown ( [ ] byte ( p . Content ) )
2018-11-08 07:19:03 +01:00
}
}
return impart . WriteSuccess ( w , coll , http . StatusOK )
}
type CollectionPage struct {
page . StaticPage
* DisplayCollection
IsCustomDomain bool
IsWelcome bool
IsOwner bool
2021-03-30 02:55:21 +02:00
IsCollLoggedIn bool
2021-06-22 00:24:40 +02:00
Honeypot string
IsSubscriber bool
2018-11-08 07:19:03 +01:00
CanPin bool
Username string
2020-08-19 15:28:44 +02:00
Monetization string
2021-06-22 00:24:40 +02:00
Flash template . HTML
2018-11-08 07:19:03 +01:00
Collections * [ ] Collection
PinnedPosts * [ ] PublicPost
2021-06-22 00:24:40 +02:00
IsAdmin bool
CanInvite bool
2021-06-09 16:04:28 +02:00
// Helper field for Chorus mode
CollAlias string
2018-11-08 07:19:03 +01:00
}
2021-02-25 05:49:15 +01:00
type TagCollectionPage struct {
CollectionPage
Tag string
}
func ( tcp TagCollectionPage ) PrevPageURL ( prefix string , n int , tl bool ) string {
u := fmt . Sprintf ( "/tag:%s" , tcp . Tag )
if n > 2 {
u += fmt . Sprintf ( "/page/%d" , n - 1 )
}
if tl {
return u
}
return "/" + prefix + tcp . Alias + u
}
func ( tcp TagCollectionPage ) NextPageURL ( prefix string , n int , tl bool ) string {
if tl {
return fmt . Sprintf ( "/tag:%s/page/%d" , tcp . Tag , n + 1 )
}
return fmt . Sprintf ( "/%s%s/tag:%s/page/%d" , prefix , tcp . Alias , tcp . Tag , n + 1 )
}
2020-01-09 22:48:22 +01:00
func NewCollectionObj ( c * Collection ) * CollectionObj {
return & CollectionObj {
Collection : * c ,
Format : c . NewFormat ( ) ,
}
}
2018-11-08 07:19:03 +01:00
func ( c * CollectionObj ) ScriptDisplay ( ) template . JS {
return template . JS ( c . Script )
}
var jsSourceCommentReg = regexp . MustCompile ( "(?m)^// src:(.+)$" )
func ( c * CollectionObj ) ExternalScripts ( ) [ ] template . URL {
scripts := [ ] template . URL { }
if c . Script == "" {
return scripts
}
matches := jsSourceCommentReg . FindAllStringSubmatch ( c . Script , - 1 )
for _ , m := range matches {
scripts = append ( scripts , template . URL ( strings . TrimSpace ( m [ 1 ] ) ) )
}
return scripts
}
func ( c * CollectionObj ) CanShowScript ( ) bool {
return false
}
func processCollectionRequest ( cr * collectionReq , vars map [ string ] string , w http . ResponseWriter , r * http . Request ) error {
cr . prefix = vars [ "prefix" ]
cr . alias = vars [ "collection" ]
// Normalize the URL, redirecting user to consistent post URL
if cr . alias != strings . ToLower ( cr . alias ) {
return impart . HTTPError { http . StatusMovedPermanently , fmt . Sprintf ( "/%s/" , strings . ToLower ( cr . alias ) ) }
}
return nil
}
// processCollectionPermissions checks the permissions for the given
// collectionReq, returning a Collection if access is granted; otherwise this
// renders any necessary collection pages, for example, if requesting a custom
// domain that doesn't yet have a collection associated, or if a collection
// requires a password. In either case, this will return nil, nil -- thus both
// values should ALWAYS be checked to determine whether or not to continue.
2019-05-12 22:55:30 +02:00
func processCollectionPermissions ( app * App , cr * collectionReq , u * User , w http . ResponseWriter , r * http . Request ) ( * Collection , error ) {
2018-11-08 07:19:03 +01:00
// Display collection if this is a collection
var c * Collection
var err error
if app . cfg . App . SingleUser {
c , err = app . db . GetCollectionByID ( 1 )
} else {
c , err = app . db . GetCollection ( cr . alias )
}
// TODO: verify we don't reveal the existence of a private collection with redirection
if err != nil {
if err , ok := err . ( impart . HTTPError ) ; ok {
if err . Status == http . StatusNotFound {
if cr . isCustomDomain {
// User is on the site from a custom domain
//tErr := pages["404-domain.tmpl"].ExecuteTemplate(w, "base", pageForHost(page.StaticPage{}, r))
//if tErr != nil {
//log.Error("Unable to render 404-domain page: %v", err)
//}
return nil , nil
}
if len ( cr . alias ) >= minIDLen && len ( cr . alias ) <= maxIDLen {
// Alias is within post ID range, so just be sure this isn't a post
if app . db . PostIDExists ( cr . alias ) {
// TODO: use StatusFound for vanity post URLs when we implement them
return nil , impart . HTTPError { http . StatusMovedPermanently , "/" + cr . alias }
}
}
// Redirect if necessary
newAlias := app . db . GetCollectionRedirect ( cr . alias )
if newAlias != "" {
return nil , impart . HTTPError { http . StatusFound , "/" + newAlias + "/" }
}
}
}
return nil , err
}
2019-06-15 00:54:04 +02:00
c . hostName = app . cfg . App . Host
2018-11-08 07:19:03 +01:00
// Update CollectionRequest to reflect owner status
cr . isCollOwner = u != nil && u . ID == c . OwnerID
// Check permissions
if ! cr . isCollOwner {
if c . IsPrivate ( ) {
return nil , ErrCollectionNotFound
} else if c . IsProtected ( ) {
uname := ""
if u != nil {
uname = u . Username
}
2019-12-17 19:02:21 +01:00
// TODO: move this to all permission checks?
2020-02-09 17:24:48 +01:00
suspended , err := app . db . IsUserSilenced ( c . OwnerID )
2019-12-17 19:02:21 +01:00
if err != nil {
log . Error ( "process protected collection permissions: %v" , err )
return nil , err
}
if suspended {
return nil , ErrCollectionNotFound
}
2018-11-08 07:19:03 +01:00
// See if we've authorized this collection
2021-03-30 02:55:21 +02:00
cr . isAuthorized = isAuthorizedForCollection ( app , c . Alias , r )
2018-11-08 07:19:03 +01:00
2021-03-30 02:55:21 +02:00
if ! cr . isAuthorized {
2018-11-08 07:19:03 +01:00
p := struct {
page . StaticPage
* CollectionObj
Username string
Next string
Flashes [ ] template . HTML
} {
StaticPage : pageForReq ( app , r ) ,
CollectionObj : & CollectionObj { Collection : * c } ,
Username : uname ,
Next : r . FormValue ( "g" ) ,
Flashes : [ ] template . HTML { } ,
}
// Get owner information
p . CollectionObj . Owner , err = app . db . GetUserByID ( c . OwnerID )
if err != nil {
// Log the error and just continue
log . Error ( "Error getting user for collection: %v" , err )
}
flashes , _ := getSessionFlashes ( app , w , r , nil )
for _ , flash := range flashes {
p . Flashes = append ( p . Flashes , template . HTML ( flash ) )
}
err = templates [ "password-collection" ] . ExecuteTemplate ( w , "password-collection" , p )
if err != nil {
log . Error ( "Unable to render password-collection: %v" , err )
return nil , err
}
return nil , nil
}
}
}
return c , nil
}
2019-05-12 22:55:30 +02:00
func checkUserForCollection ( app * App , cr * collectionReq , r * http . Request , isPostReq bool ) ( * User , error ) {
2018-11-08 07:19:03 +01:00
u := getUserSession ( app , r )
return u , nil
}
func newDisplayCollection ( c * Collection , cr * collectionReq , page int ) * DisplayCollection {
coll := & DisplayCollection {
2020-01-09 22:48:22 +01:00
CollectionObj : NewCollectionObj ( c ) ,
2018-11-08 07:19:03 +01:00
CurrentPage : page ,
Prefix : cr . prefix ,
IsTopLevel : isSingleUser ,
}
c . db . GetPostsCount ( coll . CollectionObj , cr . isCollOwner )
return coll
}
2020-09-25 16:47:31 +02:00
// getCollectionPage returns the collection page as an int. If the parsed page value is not
// greater than 0 then the default value of 1 is returned.
2018-11-08 07:19:03 +01:00
func getCollectionPage ( vars map [ string ] string ) int {
2020-09-25 16:47:31 +02:00
if p , _ := strconv . Atoi ( vars [ "page" ] ) ; p > 0 {
return p
2018-11-08 07:19:03 +01:00
}
2020-09-25 16:47:31 +02:00
return 1
2018-11-08 07:19:03 +01:00
}
// handleViewCollection displays the requested Collection
2019-05-12 22:55:30 +02:00
func handleViewCollection ( app * App , w http . ResponseWriter , r * http . Request ) error {
2018-11-08 07:19:03 +01:00
vars := mux . Vars ( r )
cr := & collectionReq { }
err := processCollectionRequest ( cr , vars , w , r )
if err != nil {
return err
}
u , err := checkUserForCollection ( app , cr , r , false )
if err != nil {
return err
}
page := getCollectionPage ( vars )
c , err := processCollectionPermissions ( app , cr , u , w , r )
if c == nil || err != nil {
return err
}
2019-08-12 21:35:17 +02:00
c . hostName = app . cfg . App . Host
2018-11-08 07:19:03 +01:00
2019-11-12 00:21:45 +01:00
silenced , err := app . db . IsUserSilenced ( c . OwnerID )
2019-08-28 21:37:45 +02:00
if err != nil {
2019-10-25 21:04:24 +02:00
log . Error ( "view collection: %v" , err )
2019-08-28 21:37:45 +02:00
return ErrInternalGeneral
}
2018-11-08 07:19:03 +01:00
// Serve ActivityStreams data now, if requested
2023-09-26 20:46:35 +02:00
if IsActivityPubRequest ( r ) {
2018-11-08 07:19:03 +01:00
ac := c . PersonObject ( )
ac . Context = [ ] interface { } { activitystreams . Namespace }
2019-09-09 22:04:20 +02:00
setCacheControl ( w , apCacheTime )
2018-11-08 07:19:03 +01:00
return impart . RenderActivityJSON ( w , ac , http . StatusOK )
}
// Fetch extra data about the Collection
// TODO: refactor out this logic, shared in collection.go:fetchCollection()
coll := newDisplayCollection ( c , cr , page )
coll . TotalPages = int ( math . Ceil ( float64 ( coll . TotalPosts ) / float64 ( coll . Format . PostsPerPage ( ) ) ) )
if coll . TotalPages > 0 && page > coll . TotalPages {
redirURL := fmt . Sprintf ( "/page/%d" , coll . TotalPages )
if ! app . cfg . App . SingleUser {
redirURL = fmt . Sprintf ( "/%s%s%s" , cr . prefix , coll . Alias , redirURL )
}
return impart . HTTPError { http . StatusFound , redirURL }
}
2019-08-07 15:26:07 +02:00
coll . Posts , _ = app . db . GetPosts ( app . cfg , c , page , cr . isCollOwner , false , false )
2018-11-08 07:19:03 +01:00
// Serve collection
displayPage := CollectionPage {
DisplayCollection : coll ,
2021-03-30 02:55:21 +02:00
IsCollLoggedIn : cr . isAuthorized ,
2018-11-08 07:19:03 +01:00
StaticPage : pageForReq ( app , r ) ,
IsCustomDomain : cr . isCustomDomain ,
IsWelcome : r . FormValue ( "greeting" ) != "" ,
2021-06-22 00:24:40 +02:00
Honeypot : spam . HoneypotFieldName ( ) ,
2021-06-09 16:04:28 +02:00
CollAlias : c . Alias ,
2018-11-08 07:19:03 +01:00
}
2021-06-22 00:24:40 +02:00
flashes , _ := getSessionFlashes ( app , w , r , nil )
for _ , f := range flashes {
displayPage . Flash = template . HTML ( f )
}
2019-08-07 15:00:16 +02:00
displayPage . IsAdmin = u != nil && u . IsAdmin ( )
displayPage . CanInvite = canUserInvite ( app . cfg , displayPage . IsAdmin )
2018-11-08 07:19:03 +01:00
var owner * User
if u != nil {
displayPage . Username = u . Username
displayPage . IsOwner = u . ID == coll . OwnerID
2021-06-22 00:24:40 +02:00
displayPage . IsSubscriber = u . IsEmailSubscriber ( app , coll . ID )
2018-11-08 07:19:03 +01:00
if displayPage . IsOwner {
// Add in needed information for users viewing their own collection
owner = u
displayPage . CanPin = true
2019-08-12 21:35:17 +02:00
pubColls , err := app . db . GetPublishableCollections ( owner , app . cfg . App . Host )
2018-11-08 07:19:03 +01:00
if err != nil {
log . Error ( "unable to fetch collections: %v" , err )
}
displayPage . Collections = pubColls
}
}
2019-08-12 18:58:30 +02:00
isOwner := owner != nil
2019-09-10 21:21:45 +02:00
if ! isOwner {
2018-11-08 07:19:03 +01:00
// Current user doesn't own collection; retrieve owner information
owner , err = app . db . GetUserByID ( coll . OwnerID )
if err != nil {
// Log the error and just continue
log . Error ( "Error getting user for collection: %v" , err )
}
}
2019-11-12 00:21:45 +01:00
if ! isOwner && silenced {
2019-10-25 21:04:24 +02:00
return ErrCollectionNotFound
}
2019-11-12 00:21:45 +01:00
displayPage . Silenced = isOwner && silenced
2018-11-08 07:19:03 +01:00
displayPage . Owner = owner
coll . Owner = displayPage . Owner
// Add more data
// TODO: fix this mess of collections inside collections
2019-08-12 23:12:35 +02:00
displayPage . PinnedPosts , _ = app . db . GetPinnedPosts ( coll . CollectionObj , isOwner )
2020-08-19 15:28:44 +02:00
displayPage . Monetization = app . db . GetCollectionAttribute ( coll . ID , "monetization_pointer" )
2018-11-08 07:19:03 +01:00
2019-08-07 15:00:16 +02:00
collTmpl := "collection"
if app . cfg . App . Chorus {
collTmpl = "chorus-collection"
}
err = templates [ collTmpl ] . ExecuteTemplate ( w , "collection" , displayPage )
2018-11-08 07:19:03 +01:00
if err != nil {
log . Error ( "Unable to render collection index: %v" , err )
}
// Update collection view count
go func ( ) {
// Don't update if owner is viewing the collection.
if u != nil && u . ID == coll . OwnerID {
return
}
// Only update for human views
if r . Method == "HEAD" || bots . IsBot ( r . UserAgent ( ) ) {
return
}
_ , err := app . db . Exec ( "UPDATE collections SET view_count = view_count + 1 WHERE id = ?" , coll . ID )
if err != nil {
log . Error ( "Unable to update collections count: %v" , err )
}
} ( )
return err
}
2019-10-10 14:11:46 +02:00
func handleViewMention ( app * App , w http . ResponseWriter , r * http . Request ) error {
vars := mux . Vars ( r )
handle := vars [ "handle" ]
2020-02-08 18:10:47 +01:00
remoteUser , err := app . db . GetProfilePageFromHandle ( app , handle )
2020-02-08 18:55:10 +01:00
if err != nil || remoteUser == "" {
log . Error ( "Couldn't find user %s: %v" , handle , err )
return ErrRemoteUserNotFound
2019-10-10 14:11:46 +02:00
}
2020-02-08 18:10:47 +01:00
return impart . HTTPError { Status : http . StatusFound , Message : remoteUser }
2019-10-10 14:11:46 +02:00
}
2019-05-12 22:55:30 +02:00
func handleViewCollectionTag ( app * App , w http . ResponseWriter , r * http . Request ) error {
2018-11-08 07:19:03 +01:00
vars := mux . Vars ( r )
tag := vars [ "tag" ]
cr := & collectionReq { }
err := processCollectionRequest ( cr , vars , w , r )
if err != nil {
return err
}
u , err := checkUserForCollection ( app , cr , r , false )
if err != nil {
return err
}
page := getCollectionPage ( vars )
c , err := processCollectionPermissions ( app , cr , u , w , r )
if c == nil || err != nil {
return err
}
coll := newDisplayCollection ( c , cr , page )
2023-07-08 06:31:02 +02:00
taggedPostIDs , err := app . db . GetAllPostsTaggedIDs ( c , tag , cr . isCollOwner )
if err != nil {
return err
}
ttlPosts := len ( taggedPostIDs )
pagePosts := coll . Format . PostsPerPage ( )
coll . TotalPages = int ( math . Ceil ( float64 ( ttlPosts ) / float64 ( pagePosts ) ) )
2021-02-25 05:00:21 +01:00
if coll . TotalPages > 0 && page > coll . TotalPages {
redirURL := fmt . Sprintf ( "/page/%d" , coll . TotalPages )
if ! app . cfg . App . SingleUser {
redirURL = fmt . Sprintf ( "/%s%s%s" , cr . prefix , coll . Alias , redirURL )
}
return impart . HTTPError { http . StatusFound , redirURL }
}
2019-08-07 15:26:07 +02:00
coll . Posts , _ = app . db . GetPostsTagged ( app . cfg , c , tag , page , cr . isCollOwner )
2018-11-08 07:19:03 +01:00
if coll . Posts != nil && len ( * coll . Posts ) == 0 {
return ErrCollectionPageNotFound
}
// Serve collection
2021-02-25 05:49:15 +01:00
displayPage := TagCollectionPage {
2018-11-08 07:19:03 +01:00
CollectionPage : CollectionPage {
DisplayCollection : coll ,
StaticPage : pageForReq ( app , r ) ,
IsCustomDomain : cr . isCustomDomain ,
} ,
Tag : tag ,
}
var owner * User
if u != nil {
displayPage . Username = u . Username
displayPage . IsOwner = u . ID == coll . OwnerID
if displayPage . IsOwner {
// Add in needed information for users viewing their own collection
owner = u
displayPage . CanPin = true
2019-08-12 21:35:17 +02:00
pubColls , err := app . db . GetPublishableCollections ( owner , app . cfg . App . Host )
2018-11-08 07:19:03 +01:00
if err != nil {
log . Error ( "unable to fetch collections: %v" , err )
}
displayPage . Collections = pubColls
}
}
2019-08-12 18:58:30 +02:00
isOwner := owner != nil
2019-09-10 21:21:45 +02:00
if ! isOwner {
2018-11-08 07:19:03 +01:00
// Current user doesn't own collection; retrieve owner information
owner , err = app . db . GetUserByID ( coll . OwnerID )
if err != nil {
// Log the error and just continue
log . Error ( "Error getting user for collection: %v" , err )
}
2019-11-12 12:01:14 +01:00
if owner . IsSilenced ( ) {
return ErrCollectionNotFound
}
2018-11-08 07:19:03 +01:00
}
2020-02-09 17:14:14 +01:00
displayPage . Silenced = owner != nil && owner . IsSilenced ( )
2018-11-08 07:19:03 +01:00
displayPage . Owner = owner
coll . Owner = displayPage . Owner
// Add more data
// TODO: fix this mess of collections inside collections
2019-08-12 23:12:35 +02:00
displayPage . PinnedPosts , _ = app . db . GetPinnedPosts ( coll . CollectionObj , isOwner )
2020-08-19 15:28:44 +02:00
displayPage . Monetization = app . db . GetCollectionAttribute ( coll . ID , "monetization_pointer" )
2018-11-08 07:19:03 +01:00
err = templates [ "collection-tags" ] . ExecuteTemplate ( w , "collection-tags" , displayPage )
if err != nil {
log . Error ( "Unable to render collection tag page: %v" , err )
}
return nil
}
2021-09-14 00:36:36 +02:00
func handleViewCollectionLang ( app * App , w http . ResponseWriter , r * http . Request ) error {
vars := mux . Vars ( r )
lang := vars [ "lang" ]
cr := & collectionReq { }
err := processCollectionRequest ( cr , vars , w , r )
if err != nil {
return err
}
u , err := checkUserForCollection ( app , cr , r , false )
if err != nil {
return err
}
page := getCollectionPage ( vars )
c , err := processCollectionPermissions ( app , cr , u , w , r )
if c == nil || err != nil {
return err
}
coll := newDisplayCollection ( c , cr , page )
coll . Language = lang
2023-09-21 22:58:44 +02:00
coll . NavSuffix = fmt . Sprintf ( "/lang:%s" , lang )
2021-09-14 00:36:36 +02:00
2021-09-16 20:23:35 +02:00
ttlPosts , err := app . db . GetCollLangTotalPosts ( coll . ID , lang )
if err != nil {
log . Error ( "Unable to getCollLangTotalPosts: %s" , err )
}
pagePosts := coll . Format . PostsPerPage ( )
coll . TotalPages = int ( math . Ceil ( float64 ( ttlPosts ) / float64 ( pagePosts ) ) )
if coll . TotalPages > 0 && page > coll . TotalPages {
redirURL := fmt . Sprintf ( "/lang:%s/page/%d" , lang , coll . TotalPages )
if ! app . cfg . App . SingleUser {
redirURL = fmt . Sprintf ( "/%s%s%s" , cr . prefix , coll . Alias , redirURL )
}
return impart . HTTPError { http . StatusFound , redirURL }
}
2021-09-14 00:36:36 +02:00
coll . Posts , _ = app . db . GetLangPosts ( app . cfg , c , lang , page , cr . isCollOwner )
if err != nil {
return ErrCollectionPageNotFound
}
2018-11-08 07:19:03 +01:00
// Serve collection
displayPage := struct {
CollectionPage
Tag string
} {
CollectionPage : CollectionPage {
DisplayCollection : coll ,
StaticPage : pageForReq ( app , r ) ,
IsCustomDomain : cr . isCustomDomain ,
} ,
2021-09-14 00:36:36 +02:00
Tag : lang ,
2018-11-08 07:19:03 +01:00
}
var owner * User
if u != nil {
displayPage . Username = u . Username
displayPage . IsOwner = u . ID == coll . OwnerID
if displayPage . IsOwner {
// Add in needed information for users viewing their own collection
owner = u
displayPage . CanPin = true
2019-08-12 21:35:17 +02:00
pubColls , err := app . db . GetPublishableCollections ( owner , app . cfg . App . Host )
2018-11-08 07:19:03 +01:00
if err != nil {
log . Error ( "unable to fetch collections: %v" , err )
}
displayPage . Collections = pubColls
}
}
2019-08-12 18:58:30 +02:00
isOwner := owner != nil
2019-09-10 21:21:45 +02:00
if ! isOwner {
2018-11-08 07:19:03 +01:00
// Current user doesn't own collection; retrieve owner information
owner , err = app . db . GetUserByID ( coll . OwnerID )
if err != nil {
// Log the error and just continue
log . Error ( "Error getting user for collection: %v" , err )
}
2019-11-12 12:01:14 +01:00
if owner . IsSilenced ( ) {
return ErrCollectionNotFound
}
2018-11-08 07:19:03 +01:00
}
2020-02-09 17:14:14 +01:00
displayPage . Silenced = owner != nil && owner . IsSilenced ( )
2018-11-08 07:19:03 +01:00
displayPage . Owner = owner
coll . Owner = displayPage . Owner
// Add more data
// TODO: fix this mess of collections inside collections
2019-08-12 23:12:35 +02:00
displayPage . PinnedPosts , _ = app . db . GetPinnedPosts ( coll . CollectionObj , isOwner )
2020-08-19 15:28:44 +02:00
displayPage . Monetization = app . db . GetCollectionAttribute ( coll . ID , "monetization_pointer" )
2018-11-08 07:19:03 +01:00
2023-09-21 22:59:32 +02:00
collTmpl := "collection"
if app . cfg . App . Chorus {
collTmpl = "chorus-collection"
}
err = templates [ collTmpl ] . ExecuteTemplate ( w , "collection" , displayPage )
2018-11-08 07:19:03 +01:00
if err != nil {
2021-09-14 00:36:36 +02:00
log . Error ( "Unable to render collection lang page: %v" , err )
2018-11-08 07:19:03 +01:00
}
return nil
}
2019-05-12 22:55:30 +02:00
func handleCollectionPostRedirect ( app * App , w http . ResponseWriter , r * http . Request ) error {
2018-11-08 07:19:03 +01:00
vars := mux . Vars ( r )
slug := vars [ "slug" ]
cr := & collectionReq { }
err := processCollectionRequest ( cr , vars , w , r )
if err != nil {
return err
}
// Normalize the URL, redirecting user to consistent post URL
loc := fmt . Sprintf ( "/%s" , slug )
if ! app . cfg . App . SingleUser {
loc = fmt . Sprintf ( "/%s/%s" , cr . alias , slug )
}
return impart . HTTPError { http . StatusFound , loc }
}
2019-05-12 22:55:30 +02:00
func existingCollection ( app * App , w http . ResponseWriter , r * http . Request ) error {
2019-09-18 21:39:53 +02:00
reqJSON := IsJSON ( r )
2018-11-08 07:19:03 +01:00
vars := mux . Vars ( r )
collAlias := vars [ "alias" ]
isWeb := r . FormValue ( "web" ) == "1"
2019-10-25 21:04:24 +02:00
u := & User { }
2018-11-08 07:19:03 +01:00
if reqJSON && ! isWeb {
// Ensure an access token was given
accessToken := r . Header . Get ( "Authorization" )
u . ID = app . db . GetUserID ( accessToken )
if u . ID == - 1 {
return ErrBadAccessToken
}
} else {
u = getUserSession ( app , r )
if u == nil {
return ErrNotLoggedIn
}
}
2019-11-12 00:21:45 +01:00
silenced , err := app . db . IsUserSilenced ( u . ID )
2019-08-28 21:37:45 +02:00
if err != nil {
2019-10-25 21:04:24 +02:00
log . Error ( "existing collection: %v" , err )
2019-08-28 21:37:45 +02:00
return ErrInternalGeneral
}
2019-11-12 00:21:45 +01:00
if silenced {
return ErrUserSilenced
2019-08-28 21:37:45 +02:00
}
2018-11-08 07:19:03 +01:00
if r . Method == "DELETE" {
err := app . db . DeleteCollection ( collAlias , u . ID )
if err != nil {
// TODO: if not HTTPError, report error to admin
log . Error ( "Unable to delete collection: %s" , err )
return err
}
addSessionFlash ( app , w , r , "Deleted your blog, " + collAlias + "." , nil )
return impart . HTTPError { Status : http . StatusNoContent }
}
c := SubmittedCollection { OwnerID : uint64 ( u . ID ) }
if reqJSON {
// Decode JSON request
decoder := json . NewDecoder ( r . Body )
err = decoder . Decode ( & c )
if err != nil {
log . Error ( "Couldn't parse collection update JSON request: %v\n" , err )
return ErrBadJSON
}
} else {
err = r . ParseForm ( )
if err != nil {
log . Error ( "Couldn't parse collection update form request: %v\n" , err )
return ErrBadFormData
}
err = app . formDecoder . Decode ( & c , r . PostForm )
if err != nil {
log . Error ( "Couldn't decode collection update form request: %v\n" , err )
return ErrBadFormData
}
}
2023-09-22 01:04:34 +02:00
err = app . db . UpdateCollection ( app , & c , collAlias )
2018-11-08 07:19:03 +01:00
if err != nil {
if err , ok := err . ( impart . HTTPError ) ; ok {
if reqJSON {
return err
}
addSessionFlash ( app , w , r , err . Message , nil )
return impart . HTTPError { http . StatusFound , "/me/c/" + collAlias }
} else {
log . Error ( "Couldn't update collection: %v\n" , err )
return err
}
}
if reqJSON {
return impart . WriteSuccess ( w , struct {
} { } , http . StatusOK )
}
addSessionFlash ( app , w , r , "Blog updated!" , nil )
return impart . HTTPError { http . StatusFound , "/me/c/" + collAlias }
}
// collectionAliasFromReq takes a request and returns the collection alias
// if it can be ascertained, as well as whether or not the collection uses a
// custom domain.
func collectionAliasFromReq ( r * http . Request ) string {
vars := mux . Vars ( r )
alias := vars [ "subdomain" ]
isSubdomain := alias != ""
if ! isSubdomain {
// Fall back to write.as/{collection} since this isn't a custom domain
alias = vars [ "collection" ]
}
return alias
}
2019-05-12 22:55:30 +02:00
func handleWebCollectionUnlock ( app * App , w http . ResponseWriter , r * http . Request ) error {
2018-11-08 07:19:03 +01:00
var readReq struct {
Alias string ` schema:"alias" json:"alias" `
Pass string ` schema:"password" json:"password" `
Next string ` schema:"to" json:"to" `
}
// Get params
if impart . ReqJSON ( r ) {
decoder := json . NewDecoder ( r . Body )
err := decoder . Decode ( & readReq )
if err != nil {
log . Error ( "Couldn't parse readReq JSON request: %v\n" , err )
return ErrBadJSON
}
} else {
err := r . ParseForm ( )
if err != nil {
log . Error ( "Couldn't parse readReq form request: %v\n" , err )
return ErrBadFormData
}
err = app . formDecoder . Decode ( & readReq , r . PostForm )
if err != nil {
log . Error ( "Couldn't decode readReq form request: %v\n" , err )
return ErrBadFormData
}
}
if readReq . Alias == "" {
return impart . HTTPError { http . StatusBadRequest , "Need a collection `alias` to read." }
}
if readReq . Pass == "" {
return impart . HTTPError { http . StatusBadRequest , "Please supply a password." }
}
var collHashedPass [ ] byte
err := app . db . QueryRow ( "SELECT password FROM collectionpasswords INNER JOIN collections ON id = collection_id WHERE alias = ?" , readReq . Alias ) . Scan ( & collHashedPass )
if err != nil {
if err == sql . ErrNoRows {
log . Error ( "No collectionpassword found when trying to read collection %s" , readReq . Alias )
return impart . HTTPError { http . StatusInternalServerError , "Something went very wrong. The humans have been alerted." }
}
return err
}
if ! auth . Authenticated ( collHashedPass , [ ] byte ( readReq . Pass ) ) {
return impart . HTTPError { http . StatusUnauthorized , "Incorrect password." }
}
// Success; set cookie
session , err := app . sessionStore . Get ( r , blogPassCookieName )
if err == nil {
session . Values [ readReq . Alias ] = true
err = session . Save ( r , w )
if err != nil {
log . Error ( "Didn't save unlocked blog '%s': %v" , readReq . Alias , err )
}
}
next := "/" + readReq . Next
if ! app . cfg . App . SingleUser {
next = "/" + readReq . Alias + next
}
return impart . HTTPError { http . StatusFound , next }
}
2019-05-12 22:55:30 +02:00
func isAuthorizedForCollection ( app * App , alias string , r * http . Request ) bool {
2018-11-08 07:19:03 +01:00
authd := false
session , err := app . sessionStore . Get ( r , blogPassCookieName )
if err == nil {
_ , authd = session . Values [ alias ]
}
return authd
}
2021-03-30 02:55:21 +02:00
func logOutCollection ( app * App , alias string , w http . ResponseWriter , r * http . Request ) error {
session , err := app . sessionStore . Get ( r , blogPassCookieName )
if err != nil {
return err
}
// Remove this from map of blogs logged into
delete ( session . Values , alias )
// If not auth'd with any blog, delete entire cookie
if len ( session . Values ) == 0 {
session . Options . MaxAge = - 1
}
return session . Save ( r , w )
}
func handleLogOutCollection ( app * App , w http . ResponseWriter , r * http . Request ) error {
alias := collectionAliasFromReq ( r )
var c * Collection
var err error
if app . cfg . App . SingleUser {
c , err = app . db . GetCollectionByID ( 1 )
} else {
c , err = app . db . GetCollection ( alias )
}
if err != nil {
return err
}
if ! c . IsProtected ( ) {
// Invalid to log out of this collection
return ErrCollectionPageNotFound
}
err = logOutCollection ( app , c . Alias , w , r )
if err != nil {
addSessionFlash ( app , w , r , "Logging out failed. Try clearing cookies for this site, instead." , nil )
}
return impart . HTTPError { http . StatusFound , c . CanonicalURL ( ) }
}