[feature] Advertise rich text formats, support content_type field (#1370)

* Advertise rich text formats, support content_type field

* Update JSON in instance patch tests

* Replace format with content_type everywhere

* update migration to work with both pg and sqlite

* regenerate swagger docs

* update instance serialization + tests

* fix up

* learn to code tobi please, i'm begging you

---------

Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
This commit is contained in:
Vyr Cossont
2023-03-02 03:06:40 -08:00
committed by GitHub
parent baf933cb9f
commit e6cde25466
23 changed files with 244 additions and 96 deletions

View File

@ -99,9 +99,9 @@ import (
// description: Default language to use for authored statuses (ISO 6391).
// type: string
// -
// name: source[status_format]
// name: source[status_content_type]
// in: formData
// description: Default format to use for authored statuses (plain or markdown).
// description: Default content type to use for authored statuses (text/plain or text/markdown).
// type: string
// -
// name: custom_css
@ -190,8 +190,8 @@ func parseUpdateAccountForm(c *gin.Context) (*apimodel.UpdateCredentialsRequest,
form.Source.Language = &language
}
if statusFormat, ok := sourceMap["status_format"]; ok {
form.Source.StatusFormat = &statusFormat
if statusContentType, ok := sourceMap["status_content_type"]; ok {
form.Source.StatusContentType = &statusContentType
}
if form == nil ||
@ -205,7 +205,7 @@ func parseUpdateAccountForm(c *gin.Context) (*apimodel.UpdateCredentialsRequest,
form.Source.Privacy == nil &&
form.Source.Sensitive == nil &&
form.Source.Language == nil &&
form.Source.StatusFormat == nil &&
form.Source.StatusContentType == nil &&
form.FieldsAttributes == nil &&
form.CustomCSS == nil &&
form.EnableRSS == nil) {

View File

@ -414,13 +414,13 @@ func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerUpd
suite.True(apimodelAccount.Locked)
}
func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerUpdateStatusFormatOK() {
func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerUpdateStatusContentTypeOK() {
// set up the request
// we're updating the language of zork
requestBody, w, err := testrig.CreateMultipartFormData(
"", "",
map[string]string{
"source[status_format]": "markdown",
"source[status_content_type]": "text/markdown",
})
if err != nil {
panic(err)
@ -450,22 +450,22 @@ func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerUpd
// check the returned api model account
// fields should be updated
suite.Equal("markdown", apimodelAccount.Source.StatusFormat)
suite.Equal("text/markdown", apimodelAccount.Source.StatusContentType)
dbAccount, err := suite.db.GetAccountByID(context.Background(), suite.testAccounts["local_account_1"].ID)
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal(dbAccount.StatusFormat, "markdown")
suite.Equal(dbAccount.StatusContentType, "text/markdown")
}
func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerUpdateStatusFormatBad() {
func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerUpdateStatusContentTypeBad() {
// set up the request
// we're updating the language of zork
requestBody, w, err := testrig.CreateMultipartFormData(
"", "",
map[string]string{
"source[status_format]": "peepeepoopoo",
"source[status_content_type]": "peepeepoopoo",
})
if err != nil {
panic(err)
@ -486,7 +486,7 @@ func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerUpd
b, err := ioutil.ReadAll(result.Body)
suite.NoError(err)
suite.Equal(`{"error":"Bad Request: status format 'peepeepoopoo' was not recognized, valid options are 'plain', 'markdown'"}`, string(b))
suite.Equal(`{"error":"Bad Request: status content type 'peepeepoopoo' was not recognized, valid options are 'text/plain', 'text/markdown'"}`, string(b))
}
func TestAccountUpdateTestSuite(t *testing.T) {

View File

@ -90,7 +90,11 @@ func (suite *InstancePatchTestSuite) TestInstancePatch1() {
"statuses": {
"max_characters": 5000,
"max_media_attachments": 6,
"characters_reserved_per_url": 25
"characters_reserved_per_url": 25,
"supported_mime_types": [
"text/plain",
"text/markdown"
]
},
"media_attachments": {
"supported_mime_types": [
@ -188,7 +192,11 @@ func (suite *InstancePatchTestSuite) TestInstancePatch2() {
"statuses": {
"max_characters": 5000,
"max_media_attachments": 6,
"characters_reserved_per_url": 25
"characters_reserved_per_url": 25,
"supported_mime_types": [
"text/plain",
"text/markdown"
]
},
"media_attachments": {
"supported_mime_types": [
@ -286,7 +294,11 @@ func (suite *InstancePatchTestSuite) TestInstancePatch3() {
"statuses": {
"max_characters": 5000,
"max_media_attachments": 6,
"characters_reserved_per_url": 25
"characters_reserved_per_url": 25,
"supported_mime_types": [
"text/plain",
"text/markdown"
]
},
"media_attachments": {
"supported_mime_types": [
@ -435,7 +447,11 @@ func (suite *InstancePatchTestSuite) TestInstancePatch6() {
"statuses": {
"max_characters": 5000,
"max_media_attachments": 6,
"characters_reserved_per_url": 25
"characters_reserved_per_url": 25,
"supported_mime_types": [
"text/plain",
"text/markdown"
]
},
"media_attachments": {
"supported_mime_types": [
@ -554,7 +570,11 @@ func (suite *InstancePatchTestSuite) TestInstancePatch8() {
"statuses": {
"max_characters": 5000,
"max_media_attachments": 6,
"characters_reserved_per_url": 25
"characters_reserved_per_url": 25,
"supported_mime_types": [
"text/plain",
"text/markdown"
]
},
"media_attachments": {
"supported_mime_types": [
@ -689,7 +709,11 @@ func (suite *InstancePatchTestSuite) TestInstancePatch9() {
"statuses": {
"max_characters": 5000,
"max_media_attachments": 6,
"characters_reserved_per_url": 25
"characters_reserved_per_url": 25,
"supported_mime_types": [
"text/plain",
"text/markdown"
]
},
"media_attachments": {
"supported_mime_types": [

View File

@ -105,14 +105,14 @@ func (suite *StatusCreateTestSuite) TestPostNewStatus() {
func (suite *StatusCreateTestSuite) TestPostNewStatusMarkdown() {
// set default post language of account 1 to markdown
testAccount := suite.testAccounts["local_account_1"]
testAccount.StatusFormat = "markdown"
testAccount.StatusContentType = "text/markdown"
a := testAccount
err := suite.db.UpdateAccount(context.Background(), a)
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal(a.StatusFormat, "markdown")
suite.Equal(a.StatusContentType, "text/markdown")
t := suite.testTokens["local_account_1"]
oauthToken := oauth.DBTokenToToken(t)

View File

@ -174,8 +174,8 @@ type UpdateSource struct {
Sensitive *bool `form:"sensitive" json:"sensitive" xml:"sensitive"`
// Default language to use for authored statuses. (ISO 6391)
Language *string `form:"language" json:"language" xml:"language"`
// Default format for authored statuses (plain or markdown).
StatusFormat *string `form:"status_format" json:"status_format" xml:"status_format"`
// Default format for authored statuses (text/plain or text/markdown).
StatusContentType *string `form:"status_content_type" json:"status_content_type" xml:"status_content_type"`
}
// UpdateField is to be used specifically in an UpdateCredentialsRequest.

View File

@ -73,6 +73,10 @@ type InstanceConfigurationStatuses struct {
//
// example: 25
CharactersReservedPerURL int `json:"characters_reserved_per_url"`
// List of mime types that it's possible to use for statuses on this instance.
//
// example: ["text/plain","text/markdown"]
SupportedMimeTypes []string `json:"supported_mime_types,omitempty"`
}
// InstanceConfigurationMediaAttachments models instance media attachment config parameters.

View File

@ -31,8 +31,8 @@ type Source struct {
Sensitive bool `json:"sensitive"`
// The default posting language for new statuses.
Language string `json:"language"`
// The default posting format for new statuses.
StatusFormat string `json:"status_format"`
// The default posting content type for new statuses.
StatusContentType string `json:"status_content_type"`
// Profile bio.
Note string `json:"note"`
// Metadata about the account.

View File

@ -179,9 +179,9 @@ type StatusCreateRequest struct {
// ISO 639 language code for this status.
// in: formData
Language string `form:"language" json:"language" xml:"language"`
// Format to use when parsing this status.
// Content type to use when parsing this status.
// in: formData
Format StatusFormat `form:"format" json:"format" xml:"format"`
ContentType StatusContentType `form:"content_type" json:"content_type" xml:"content_type"`
}
// Visibility models the visibility of a status.
@ -227,16 +227,16 @@ type AdvancedVisibilityFlagsForm struct {
Likeable *bool `form:"likeable" json:"likeable" xml:"likeable"`
}
// StatusFormat is the format in which to parse the submitted status.
// Can be either plain or markdown. Empty will default to plain.
// StatusContentType is the content type with which to parse the submitted status.
// Can be either text/plain or text/markdown. Empty will default to text/plain.
//
// swagger:enum statusFormat
// swagger:enum statusContentType
// swagger:type string
type StatusFormat string
type StatusContentType string
// Format to use when parsing submitted status into an html-formatted status
// Content type to use when parsing submitted status into an html-formatted status
const (
StatusFormatPlain StatusFormat = "plain"
StatusFormatMarkdown StatusFormat = "markdown"
StatusFormatDefault StatusFormat = StatusFormatPlain
StatusContentTypePlain StatusContentType = "text/plain"
StatusContentTypeMarkdown StatusContentType = "text/markdown"
StatusContentTypeDefault = StatusContentTypePlain
)