0
0
Fork 0
mirror of https://github.com/matrix-org/dendrite synced 2024-11-15 22:31:07 +01:00

Add goconst linter (#246)

This commit is contained in:
Erik Johnston 2017-09-20 15:25:25 +01:00 committed by Mark Haines
parent 340a84cdc0
commit 7a30f2085a
7 changed files with 33 additions and 18 deletions

View file

@ -11,6 +11,7 @@
"gas",
"misspell",
"errcheck",
"vet"
"vet",
"goconst"
]
}

View file

@ -17,6 +17,7 @@
"unparam",
"errcheck",
"vet",
"megacheck"
"megacheck",
"goconst"
]
}

View file

@ -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)
}

View file

@ -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
}

View file

@ -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"

View file

@ -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"),

View file

@ -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()