diff --git a/internal/apimodule/status/statuscreate.go b/internal/apimodule/status/statuscreate.go
index a309239..fbae673 100644
--- a/internal/apimodule/status/statuscreate.go
+++ b/internal/apimodule/status/statuscreate.go
@@ -23,6 +23,7 @@ import (
"fmt"
"net"
"net/http"
+ "strings"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
@@ -34,6 +35,24 @@ import (
"github.com/superseriousbusiness/gotosocial/pkg/mastotypes"
)
+type advancedStatusCreateForm struct {
+ mastotypes.StatusCreateRequest
+ AdvancedVisibility *advancedVisibilityFlagsForm `form:"visibility_advanced"`
+}
+
+type advancedVisibilityFlagsForm struct {
+ // The gotosocial visibility model
+ Visibility *model.Visibility
+ // This status will be federated beyond the local timeline(s)
+ Federated *bool `form:"federated"`
+ // This status can be boosted/reblogged
+ Boostable *bool `form:"boostable"`
+ // This status can be replied to
+ Replyable *bool `form:"replyable"`
+ // This status can be liked/faved
+ Likeable *bool `form:"likeable"`
+}
+
func (m *statusModule) statusCreatePOSTHandler(c *gin.Context) {
l := m.log.WithField("func", "statusCreatePOSTHandler")
authed, err := oauth.MustAuth(c, true, true, true, true) // posting a status is serious business so we want *everything*
@@ -51,7 +70,7 @@ func (m *statusModule) statusCreatePOSTHandler(c *gin.Context) {
}
l.Trace("parsing request form")
- form := &mastotypes.StatusCreateRequest{}
+ form := &advancedStatusCreateForm{}
if err := c.ShouldBind(form); err != nil || form == nil {
l.Debugf("could not parse form from request: %s", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "missing one or more required form values"})
@@ -65,6 +84,10 @@ func (m *statusModule) statusCreatePOSTHandler(c *gin.Context) {
return
}
+ // here we check if any advanced visibility flags have been set and fiddle with them if so
+ l.Trace("deriving visibility")
+ basicVis, advancedVis, err := deriveTotalVisibility(form.Visibility, form.AdvancedVisibility, authed.Account.Privacy)
+
clientIP := c.ClientIP()
l.Tracef("attempting to parse client ip address %s", clientIP)
signUpIP := net.ParseIP(clientIP)
@@ -75,23 +98,35 @@ func (m *statusModule) statusCreatePOSTHandler(c *gin.Context) {
}
uris := util.GenerateURIs(authed.Account.Username, m.config.Protocol, m.config.Host)
- newStatusID := uuid.NewString()
+ thisStatusID := uuid.NewString()
+ thisStatusURI := fmt.Sprintf("%s/%s", uris.StatusesURI, thisStatusID)
+ thisStatusURL := fmt.Sprintf("%s/%s", uris.StatusesURL, thisStatusID)
newStatus := &model.Status{
- ID: newStatusID,
- URI: fmt.Sprintf("%s/%s", uris.StatusesURI, newStatusID),
- URL: fmt.Sprintf("%s/%s", uris.StatusesURL, newStatusID),
+ ID: thisStatusID,
+ URI: thisStatusURI,
+ URL: thisStatusURL,
Content: util.HTMLFormat(form.Status),
- Local: true, // will always be true if this status is being created through the client API
+ Local: true, // will always be true if this status is being created through the client API, since only local users can do that
AccountID: authed.Account.ID,
InReplyToID: form.InReplyToID,
ContentWarning: form.SpoilerText,
- ActivityStreamsType: "Note",
+ Visibility: basicVis,
+ VisibilityAdvanced: *advancedVis,
+ ActivityStreamsType: model.ActivityStreamsNote,
+ }
+
+ // take care of side effects -- mentions, updating metadata, etc, etc
+ menchies, err := m.db.AccountStringsToMentions(util.DeriveMentions(form.Status), authed.Account.ID, thisStatusID)
+ if err != nil {
+ l.Debugf("error generating mentions from status: %s", err)
+ c.JSON(http.StatusInternalServerError, gin.H{"error": "error generating mentions from status"})
+ return
}
}
-func validateCreateStatus(form *mastotypes.StatusCreateRequest, config *config.StatusesConfig, accountID string, db db.DB) error {
+func validateCreateStatus(form *advancedStatusCreateForm, config *config.StatusesConfig, accountID string, db db.DB) error {
// validate that, structurally, we have a valid status/post
if form.Status == "" && form.MediaIDs == nil && form.Poll == nil {
return errors.New("no status, media, or poll provided")
@@ -146,7 +181,7 @@ func validateCreateStatus(form *mastotypes.StatusCreateRequest, config *config.S
if err := db.GetByID(form.InReplyToID, s); err != nil {
return fmt.Errorf("status id %s cannot be retrieved from the db: %s", form.InReplyToID, err)
}
- if !*s.VisibilityAdvanced.Replyable {
+ if !s.VisibilityAdvanced.Replyable {
return fmt.Errorf("status with id %s is not replyable", form.InReplyToID)
}
}
@@ -167,3 +202,80 @@ func validateCreateStatus(form *mastotypes.StatusCreateRequest, config *config.S
return nil
}
+
+func deriveTotalVisibility(basicVisForm mastotypes.Visibility, advancedVisForm *advancedVisibilityFlagsForm, accountDefaultVis model.Visibility) (model.Visibility, *model.VisibilityAdvanced, error) {
+ // by default all flags are set to true
+ gtsAdvancedVis := &model.VisibilityAdvanced{
+ Federated: true,
+ Boostable: true,
+ Replyable: true,
+ Likeable: true,
+ }
+
+ var gtsBasicVis model.Visibility
+ // Advanced takes priority if it's set.
+ // If it's not set, take whatever masto visibility is set.
+ // If *that's* not set either, then just take the account default.
+ if advancedVisForm != nil && advancedVisForm.Visibility != nil {
+ gtsBasicVis = *advancedVisForm.Visibility
+ } else if basicVisForm != "" {
+ gtsBasicVis = util.ParseGTSVisFromMastoVis(basicVisForm)
+ } else {
+ gtsBasicVis = accountDefaultVis
+ }
+
+ switch gtsBasicVis {
+ case model.VisibilityPublic:
+ // for public, there's no need to change any of the advanced flags from true regardless of what the user filled out
+ return gtsBasicVis, gtsAdvancedVis, nil
+ case model.VisibilityUnlocked:
+ // for unlocked the user can set any combination of flags they like so look at them all to see if they're set and then apply them
+ if advancedVisForm != nil {
+ if advancedVisForm.Federated != nil {
+ gtsAdvancedVis.Federated = *advancedVisForm.Federated
+ }
+
+ if advancedVisForm.Boostable != nil {
+ gtsAdvancedVis.Boostable = *advancedVisForm.Boostable
+ }
+
+ if advancedVisForm.Replyable != nil {
+ gtsAdvancedVis.Replyable = *advancedVisForm.Replyable
+ }
+
+ if advancedVisForm.Likeable != nil {
+ gtsAdvancedVis.Likeable = *advancedVisForm.Likeable
+ }
+ }
+ return gtsBasicVis, gtsAdvancedVis, nil
+ case model.VisibilityFollowersOnly, model.VisibilityMutualsOnly:
+ // for followers or mutuals only, boostable will *always* be false, but the other fields can be set so check and apply them
+ gtsAdvancedVis.Boostable = false
+
+ if advancedVisForm != nil {
+ if advancedVisForm.Federated != nil {
+ gtsAdvancedVis.Federated = *advancedVisForm.Federated
+ }
+
+ if advancedVisForm.Replyable != nil {
+ gtsAdvancedVis.Replyable = *advancedVisForm.Replyable
+ }
+
+ if advancedVisForm.Likeable != nil {
+ gtsAdvancedVis.Likeable = *advancedVisForm.Likeable
+ }
+ }
+
+ return gtsBasicVis, gtsAdvancedVis, nil
+ case model.VisibilityDirect:
+ // direct is pretty easy: there's only one possible setting so return it
+ gtsAdvancedVis.Federated = true
+ gtsAdvancedVis.Boostable = false
+ gtsAdvancedVis.Federated = true
+ gtsAdvancedVis.Likeable = true
+ return gtsBasicVis, gtsAdvancedVis, nil
+ }
+
+ // this should never happen but just in case...
+ return "", nil, errors.New("could not parse visibility")
+}
diff --git a/internal/db/db.go b/internal/db/db.go
index 4921270..9ab4139 100644
--- a/internal/db/db.go
+++ b/internal/db/db.go
@@ -79,6 +79,11 @@ type DB interface {
// In case of no entries, a 'no entries' error will be returned
GetWhere(key string, value interface{}, i interface{}) error
+ // // GetWhereMany gets one entry where key = value for *ALL* parameters passed as "where".
+ // // That is, if you pass 2 'where' entries, with 1 being Key username and Value test, and the second
+ // // being Key domain and Value example.org, only entries will be returned where BOTH conditions are true.
+ // GetWhereMany(i interface{}, where ...model.Where) error
+
// GetAll will try to get all entries of type i.
// The given interface i will be set to the result of the query, whatever it is. Use a pointer or a slice.
// In case of no entries, a 'no entries' error will be returned
@@ -182,6 +187,11 @@ type DB interface {
// if something goes wrong. The returned account should be ready to serialize on an API level, and may NOT have sensitive fields.
// In other words, this is the public record that the server has of an account.
AccountToMastoPublic(account *model.Account) (*mastotypes.Account, error)
+
+ // AccountStringsToMentions takes a slice of deduplicated account names in the form "@test@whatever.example.org", which have been
+ // mentioned in a status. It takes the id of the account that wrote the status, and the id of the status itself, and then
+ // checks in the database for the mentioned accounts, and returns a slice of mentions generated based on the given parameters.
+ AccountStringsToMentions(targetAccounts []string, originAccountID string, statusID string) ([]*model.Mention, error)
}
// New returns a new database service that satisfies the DB interface and, by extension,
diff --git a/internal/db/model/account.go b/internal/db/model/account.go
index 70ee929..70028be 100644
--- a/internal/db/model/account.go
+++ b/internal/db/model/account.go
@@ -38,8 +38,8 @@ type Account struct {
ID string `pg:"type:uuid,default:gen_random_uuid(),pk,notnull,unique"`
// Username of the account, should just be a string of [a-z0-9_]. Can be added to domain to create the full username in the form ``[username]@[domain]`` eg., ``user_96@example.org``
Username string `pg:",notnull,unique:userdomain"` // username and domain should be unique *with* each other
- // Domain of the account, will be empty if this is a local account, otherwise something like ``example.org`` or ``mastodon.social``. Should be unique with username.
- Domain string `pg:",unique:userdomain"` // username and domain should be unique *with* each other
+ // Domain of the account, will be null if this is a local account, otherwise something like ``example.org`` or ``mastodon.social``. Should be unique with username.
+ Domain string `pg:"default:null,unique:userdomain"` // username and domain should be unique *with* each other
/*
ACCOUNT METADATA
@@ -95,7 +95,7 @@ type Account struct {
// Should this account be shown in the instance's profile directory?
Discoverable bool
// Default post privacy for this account
- Privacy string
+ Privacy Visibility
// Set posts from this account to sensitive by default?
Sensitive bool
// What language does this account post in?
@@ -122,7 +122,7 @@ type Account struct {
// URL for getting the featured collection list of this account
FeaturedCollectionURL string `pg:",unique"`
// What type of activitypub actor is this account?
- ActorType string
+ ActorType ActivityStreamsActor
// This account is associated with x account id
AlsoKnownAs string
diff --git a/internal/db/model/activitystreams.go b/internal/db/model/activitystreams.go
new file mode 100644
index 0000000..b6c9df6
--- /dev/null
+++ b/internal/db/model/activitystreams.go
@@ -0,0 +1,127 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see
-// // - Replacing URLs with hrefs. -// // - Replacing mentions with links to that account's URL as stored in the database. func HTMLFormat(status string) string { // TODO: write proper HTML formatting logic for a status diff --git a/pkg/mastotypes/source.go b/pkg/mastotypes/source.go index 4142540..0445a1f 100644 --- a/pkg/mastotypes/source.go +++ b/pkg/mastotypes/source.go @@ -27,7 +27,7 @@ type Source struct { // unlisted = Unlisted post // private = Followers-only post // direct = Direct post - Privacy string `json:"privacy,omitempty"` + Privacy Visibility `json:"privacy,omitempty"` // Whether new statuses should be marked sensitive by default. Sensitive bool `json:"sensitive,omitempty"` // The default posting language for new statuses.