diff --git a/linter-fast.json b/linter-fast.json index fce445f2..3825fa61 100644 --- a/linter-fast.json +++ b/linter-fast.json @@ -11,6 +11,7 @@ "gas", "misspell", "errcheck", - "vet" + "vet", + "goconst" ] } diff --git a/linter.json b/linter.json index 271a13cc..53308ba5 100644 --- a/linter.json +++ b/linter.json @@ -17,6 +17,7 @@ "unparam", "errcheck", "vet", - "megacheck" + "megacheck", + "goconst" ] } diff --git a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer.go b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer.go index 6aa76cf0..83560373 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer.go @@ -63,22 +63,22 @@ func SelectThumbnail(desired types.ThumbnailSize, thumbnails []*types.ThumbnailM bestFit := newThumbnailFitness() for _, thumbnail := range thumbnails { - if desired.ResizeMethod == "scale" && thumbnail.ThumbnailSize.ResizeMethod != "scale" { + if desired.ResizeMethod == types.Scale && thumbnail.ThumbnailSize.ResizeMethod != types.Scale { continue } fitness := calcThumbnailFitness(thumbnail.ThumbnailSize, thumbnail.MediaMetadata, desired) - if isBetter := fitness.betterThan(bestFit, desired.ResizeMethod == "crop"); isBetter { + if isBetter := fitness.betterThan(bestFit, desired.ResizeMethod == types.Crop); isBetter { bestFit = fitness chosenThumbnail = thumbnail } } for _, thumbnailSize := range thumbnailSizes { - if desired.ResizeMethod == "scale" && thumbnailSize.ResizeMethod != "scale" { + if desired.ResizeMethod == types.Scale && thumbnailSize.ResizeMethod != types.Scale { continue } fitness := calcThumbnailFitness(types.ThumbnailSize(thumbnailSize), nil, desired) - if isBetter := fitness.betterThan(bestFit, desired.ResizeMethod == "crop"); isBetter { + if isBetter := fitness.betterThan(bestFit, desired.ResizeMethod == types.Crop); isBetter { bestFit = fitness chosenThumbnailSize = (*types.ThumbnailSize)(&thumbnailSize) } diff --git a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go index a55a6d2a..caabf420 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go @@ -149,7 +149,7 @@ func createThumbnail(src types.Path, img image.Image, config types.ThumbnailSize } start := time.Now() - width, height, err := adjustSize(dst, img, config.Width, config.Height, config.ResizeMethod == "crop", logger) + width, height, err := adjustSize(dst, img, config.Width, config.Height, config.ResizeMethod == types.Crop, logger) if err != nil { return false, err } diff --git a/src/github.com/matrix-org/dendrite/mediaapi/types/types.go b/src/github.com/matrix-org/dendrite/mediaapi/types/types.go index c7e5860d..855e8fe2 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/types/types.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/types/types.go @@ -102,3 +102,9 @@ type ActiveThumbnailGeneration struct { // The string key is a thumbnail file path PathToResult map[string]*ThumbnailGenerationResult } + +// Crop indicates we should crop the thumbnail on resize +const Crop = "crop" + +// Scale indicates we should scale the thumbnail on resize +const Scale = "scale" diff --git a/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go b/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go index 901ab6d3..81907d55 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go @@ -179,9 +179,9 @@ func (r *downloadRequest) Validate() *util.JSONResponse { } // Default method to scale if not set if r.ThumbnailSize.ResizeMethod == "" { - r.ThumbnailSize.ResizeMethod = "scale" + r.ThumbnailSize.ResizeMethod = types.Scale } - if r.ThumbnailSize.ResizeMethod != "crop" && r.ThumbnailSize.ResizeMethod != "scale" { + if r.ThumbnailSize.ResizeMethod != types.Crop && r.ThumbnailSize.ResizeMethod != types.Scale { return &util.JSONResponse{ Code: 400, JSON: jsonerror.Unknown("method must be one of crop or scale"), diff --git a/src/github.com/matrix-org/dendrite/roomserver/input/membership.go b/src/github.com/matrix-org/dendrite/roomserver/input/membership.go index f4d8e02c..cd09001e 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/input/membership.go +++ b/src/github.com/matrix-org/dendrite/roomserver/input/membership.go @@ -23,6 +23,13 @@ import ( "github.com/matrix-org/gomatrixserverlib" ) +// Membership values +// TODO: Factor these out somewhere sensible? +const join = "join" +const leave = "leave" +const invite = "invite" +const ban = "ban" + // updateMembership updates the current membership and the invites for each // user affected by a change in the current state of the room. // Returns a list of output events to write to the kafka log to inform the @@ -83,9 +90,9 @@ func updateMembership( updates []api.OutputEvent, ) ([]api.OutputEvent, error) { var err error - // Default the membership to "leave" if no event was added or removed. - old := "leave" - new := "leave" + // Default the membership to Leave if no event was added or removed. + old := leave + new := leave if remove != nil { old, err = remove.Membership() @@ -99,9 +106,9 @@ func updateMembership( return nil, err } } - if old == new && new != "join" { + if old == new && new != join { // If the membership is the same then nothing changed and we can return - // immediately, unless it's a "join" update (e.g. profile update). + // immediately, unless it's a Join update (e.g. profile update). return updates, nil } @@ -111,11 +118,11 @@ func updateMembership( } switch new { - case "invite": + case invite: return updateToInviteMembership(mu, add, updates) - case "join": + case join: return updateToJoinMembership(mu, add, updates) - case "leave", "ban": + case leave, ban: return updateToLeaveMembership(mu, add, new, updates) default: panic(fmt.Errorf( @@ -176,7 +183,7 @@ func updateToJoinMembership( for _, eventID := range retired { orie := api.OutputRetireInviteEvent{ EventID: eventID, - Membership: "join", + Membership: join, } if add != nil { orie.RetiredByEventID = add.EventID()