Compare commits

...

5 commits

Author SHA1 Message Date
kim
5fd52369c9 [performance] handle emoji refreshes asynchronously when fetched as part of account|status dereferences (#4486)
# Description

Updates our dereferencer emoji handling to work asynchronously when going through the route of account or status dereferencing.

closes https://codeberg.org/superseriousbusiness/gotosocial/issues/4485

## Checklist

- [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md).
- [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat.
- [x] I/we have not leveraged AI to create the proposed changes.
- [x] I/we have performed a self-review of added code.
- [x] I/we have written code that is legible and maintainable by others.
- [ ] I/we have commented the added code, particularly in hard-to-understand areas.
- [ ] I/we have made any necessary changes to documentation.
- [ ] I/we have added tests that cover new code.
- [ ] I/we have run tests and they pass locally with the changes.
- [x] I/we have run `go fmt ./...` and `golangci-lint run`.

Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4486
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
2025-10-08 14:13:40 +02:00
kim
baf2c54730 [performance] add benchmarks for native Go imaging code, small tweaks to reduce nil and boundary checks, some loop unrolling (#4482)
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4482
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
2025-10-08 11:12:12 +02:00
tobi
b012a81f66 [bugfix] Log a warning when clientIP could not be parsed during rate limiting (#4481)
# Description

> If this is a code change, please include a summary of what you've coded, and link to the issue(s) it closes/implements.
>
> If this is a documentation change, please briefly describe what you've changed and why.

Fixes a panic when clientIP cannot be parsed in the rate limiting middleware, and warn logs the derived clientIP and a hint that reverse proxy may be misconfigured.

Closes https://codeberg.org/superseriousbusiness/gotosocial/issues/4479

## Checklist

Please put an x inside each checkbox to indicate that you've read and followed it: `[ ]` -> `[x]`

If this is a documentation change, only the first checkbox must be filled (you can delete the others if you want).

- [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md).
- [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat.
- [x] I/we have not leveraged AI to create the proposed changes.
- [x] I/we have performed a self-review of added code.
- [x] I/we have written code that is legible and maintainable by others.
- [x] I/we have commented the added code, particularly in hard-to-understand areas.
- [ ] I/we have made any necessary changes to documentation.
- [ ] I/we have added tests that cover new code.
- [x] I/we have run tests and they pass locally with the changes.
- [x] I/we have run `go fmt ./...` and `golangci-lint run`.

Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4481
Co-authored-by: tobi <tobi.smethurst@protonmail.com>
Co-committed-by: tobi <tobi.smethurst@protonmail.com>
2025-10-07 16:02:57 +02:00
tobi
c6044d0142 [bugfix] Fix db error checking for int req: sql: no rows in result set (#4478)
Fixes `sql: no rows in result set` when trying to append approvedByURI to a reply that was sent impolitely and approved impolitely.

Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4478
Co-authored-by: tobi <tobi.smethurst@protonmail.com>
Co-committed-by: tobi <tobi.smethurst@protonmail.com>
2025-10-06 13:11:23 +02:00
tobi
03fc6eaf39 [bugfix] Fix nil ptr in DifferentFrom func (#4477)
Closes https://codeberg.org/superseriousbusiness/gotosocial/issues/4476

Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4477
Co-authored-by: tobi <tobi.smethurst@protonmail.com>
Co-committed-by: tobi <tobi.smethurst@protonmail.com>
2025-10-06 11:45:40 +02:00
11 changed files with 798 additions and 335 deletions

View file

@ -51,6 +51,7 @@ func (d *Dereferencer) GetEmoji(
remoteURL string, remoteURL string,
info media.AdditionalEmojiInfo, info media.AdditionalEmojiInfo,
refresh bool, refresh bool,
async bool,
) ( ) (
*gtsmodel.Emoji, *gtsmodel.Emoji,
error, error,
@ -66,7 +67,7 @@ func (d *Dereferencer) GetEmoji(
if emoji != nil { if emoji != nil {
// This was an existing emoji, pass to refresh func. // This was an existing emoji, pass to refresh func.
return d.RefreshEmoji(ctx, emoji, info, refresh) return d.RefreshEmoji(ctx, emoji, info, refresh, async)
} }
if domain == "" { if domain == "" {
@ -112,6 +113,7 @@ func (d *Dereferencer) GetEmoji(
info, info,
) )
}, },
async,
) )
} }
@ -130,6 +132,7 @@ func (d *Dereferencer) RefreshEmoji(
emoji *gtsmodel.Emoji, emoji *gtsmodel.Emoji,
info media.AdditionalEmojiInfo, info media.AdditionalEmojiInfo,
force bool, force bool,
async bool,
) ( ) (
*gtsmodel.Emoji, *gtsmodel.Emoji,
error, error,
@ -162,7 +165,7 @@ func (d *Dereferencer) RefreshEmoji(
// We still want to make sure // We still want to make sure
// the emoji is cached. Simply // the emoji is cached. Simply
// check whether emoji is cached. // check whether emoji is cached.
return d.RecacheEmoji(ctx, emoji) return d.RecacheEmoji(ctx, emoji, async)
} }
// Can't refresh local. // Can't refresh local.
@ -207,6 +210,7 @@ func (d *Dereferencer) RefreshEmoji(
info, info,
) )
}, },
async,
) )
} }
@ -222,6 +226,7 @@ func (d *Dereferencer) RefreshEmoji(
func (d *Dereferencer) RecacheEmoji( func (d *Dereferencer) RecacheEmoji(
ctx context.Context, ctx context.Context,
emoji *gtsmodel.Emoji, emoji *gtsmodel.Emoji,
async bool,
) ( ) (
*gtsmodel.Emoji, *gtsmodel.Emoji,
error, error,
@ -272,23 +277,24 @@ func (d *Dereferencer) RecacheEmoji(
data, data,
) )
}, },
async,
) )
} }
// processingEmojiSafely provides concurrency-safe processing of // processingEmojiSafely provides concurrency-safe processing of
// an emoji with given shortcode+domain. if a copy of the emoji is // an emoji with given shortcode+domain. if a copy of the emoji is
// not already being processed, the given 'process' callback will // not already being processed, the given 'process' callback will
// be used to generate new *media.ProcessingEmoji{} instance. // be used to generate new *media.ProcessingEmoji{} instance. async
// determines whether to load it immediately, or in the background.
func (d *Dereferencer) processEmojiSafely( func (d *Dereferencer) processEmojiSafely(
ctx context.Context, ctx context.Context,
shortcodeDomain string, shortcodeDomain string,
process func() (*media.ProcessingEmoji, error), process func() (*media.ProcessingEmoji, error),
async bool,
) ( ) (
emoji *gtsmodel.Emoji, emoji *gtsmodel.Emoji,
err error, err error,
) { ) {
// Acquire map lock. // Acquire map lock.
d.derefEmojisMu.Lock() d.derefEmojisMu.Lock()
@ -309,17 +315,25 @@ func (d *Dereferencer) processEmojiSafely(
// Add processing emoji media to hash map. // Add processing emoji media to hash map.
d.derefEmojis[shortcodeDomain] = processing d.derefEmojis[shortcodeDomain] = processing
}
// Unlock map.
unlock()
if async {
emoji = processing.LoadAsync(func() {
// Remove on finish.
d.derefEmojisMu.Lock()
delete(d.derefEmojis, shortcodeDomain)
d.derefEmojisMu.Unlock()
})
} else {
defer func() { defer func() {
// Remove on finish. // Remove on finish.
d.derefEmojisMu.Lock() d.derefEmojisMu.Lock()
delete(d.derefEmojis, shortcodeDomain) delete(d.derefEmojis, shortcodeDomain)
d.derefEmojisMu.Unlock() d.derefEmojisMu.Unlock()
}() }()
}
// Unlock map.
unlock()
// Perform emoji load operation. // Perform emoji load operation.
emoji, err = processing.Load(ctx) emoji, err = processing.Load(ctx)
@ -329,6 +343,7 @@ func (d *Dereferencer) processEmojiSafely(
// TODO: in time we should return checkable flags by gtserror.Is___() // TODO: in time we should return checkable flags by gtserror.Is___()
// which can determine if loading error should allow remaining placeholder. // which can determine if loading error should allow remaining placeholder.
} }
}
return return
} }
@ -364,7 +379,7 @@ func (d *Dereferencer) fetchEmojis(
URI: &placeholder.URI, URI: &placeholder.URI,
ImageRemoteURL: &placeholder.ImageRemoteURL, ImageRemoteURL: &placeholder.ImageRemoteURL,
ImageStaticRemoteURL: &placeholder.ImageStaticRemoteURL, ImageStaticRemoteURL: &placeholder.ImageStaticRemoteURL,
}, force) }, force, true)
if err != nil { if err != nil {
log.Errorf(ctx, "error refreshing emoji: %v", err) log.Errorf(ctx, "error refreshing emoji: %v", err)
@ -396,6 +411,7 @@ func (d *Dereferencer) fetchEmojis(
ImageStaticRemoteURL: &placeholder.ImageStaticRemoteURL, ImageStaticRemoteURL: &placeholder.ImageStaticRemoteURL,
}, },
false, false,
true,
) )
if err != nil { if err != nil {
if emoji == nil { if emoji == nil {

View file

@ -54,6 +54,7 @@ func (suite *EmojiTestSuite) TestDereferenceEmojiBlocking() {
VisibleInPicker: &emojiVisibleInPicker, VisibleInPicker: &emojiVisibleInPicker,
}, },
false, false,
false,
) )
suite.NoError(err) suite.NoError(err)
suite.NotNil(emoji) suite.NotNil(emoji)

View file

@ -244,6 +244,12 @@ func (pr1 *PolicyRules) DifferentFrom(pr2 *PolicyRules) bool {
return true return true
} }
// If they're both nil we don't
// need to check anything else.
if pr1 == nil && pr2 == nil {
return false
}
// Check if AutomaticApproval // Check if AutomaticApproval
// differs between the two. // differs between the two.
if slices.Compare( if slices.Compare(

View file

@ -21,6 +21,8 @@ import (
"image" "image"
"image/color" "image/color"
"math" "math"
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
) )
// NOTE: // NOTE:
@ -73,15 +75,15 @@ func resizeDownLinear(img image.Image, width, height int) image.Image {
// flipH flips the image horizontally (left to right). // flipH flips the image horizontally (left to right).
func flipH(img image.Image) image.Image { func flipH(img image.Image) image.Image {
src := newScanner(img) srcW, srcH := img.Bounds().Dx(), img.Bounds().Dy()
dstW := src.w dstW := srcW
dstH := src.h dstH := srcH
rowSize := dstW * 4 rowSize := dstW * 4
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH)) dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for y := 0; y < dstH; y++ { for y := 0; y < dstH; y++ {
i := y * dst.Stride i := y * dst.Stride
srcY := y srcY := y
src.scan(0, srcY, src.w, srcY+1, dst.Pix[i:i+rowSize]) scanImage(img, 0, srcY, srcW, srcY+1, dst.Pix[i:i+rowSize])
reverse(dst.Pix[i : i+rowSize]) reverse(dst.Pix[i : i+rowSize])
} }
return dst return dst
@ -89,45 +91,45 @@ func flipH(img image.Image) image.Image {
// flipV flips the image vertically (from top to bottom). // flipV flips the image vertically (from top to bottom).
func flipV(img image.Image) image.Image { func flipV(img image.Image) image.Image {
src := newScanner(img) srcW, srcH := img.Bounds().Dx(), img.Bounds().Dy()
dstW := src.w dstW := srcW
dstH := src.h dstH := srcH
rowSize := dstW * 4 rowSize := dstW * 4
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH)) dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for y := 0; y < dstH; y++ { for y := 0; y < dstH; y++ {
i := y * dst.Stride i := y * dst.Stride
srcY := dstH - y - 1 srcY := dstH - y - 1
src.scan(0, srcY, src.w, srcY+1, dst.Pix[i:i+rowSize]) scanImage(img, 0, srcY, srcW, srcY+1, dst.Pix[i:i+rowSize])
} }
return dst return dst
} }
// rotate90 rotates the image 90 counter-clockwise. // rotate90 rotates the image 90 counter-clockwise.
func rotate90(img image.Image) image.Image { func rotate90(img image.Image) image.Image {
src := newScanner(img) srcW, srcH := img.Bounds().Dx(), img.Bounds().Dy()
dstW := src.h dstW := srcH
dstH := src.w dstH := srcW
rowSize := dstW * 4 rowSize := dstW * 4
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH)) dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for y := 0; y < dstH; y++ { for y := 0; y < dstH; y++ {
i := y * dst.Stride i := y * dst.Stride
srcX := dstH - y - 1 srcX := dstH - y - 1
src.scan(srcX, 0, srcX+1, src.h, dst.Pix[i:i+rowSize]) scanImage(img, srcX, 0, srcX+1, srcH, dst.Pix[i:i+rowSize])
} }
return dst return dst
} }
// rotate180 rotates the image 180 counter-clockwise. // rotate180 rotates the image 180 counter-clockwise.
func rotate180(img image.Image) image.Image { func rotate180(img image.Image) image.Image {
src := newScanner(img) srcW, srcH := img.Bounds().Dx(), img.Bounds().Dy()
dstW := src.w dstW := srcW
dstH := src.h dstH := srcH
rowSize := dstW * 4 rowSize := dstW * 4
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH)) dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for y := 0; y < dstH; y++ { for y := 0; y < dstH; y++ {
i := y * dst.Stride i := y * dst.Stride
srcY := dstH - y - 1 srcY := dstH - y - 1
src.scan(0, srcY, src.w, srcY+1, dst.Pix[i:i+rowSize]) scanImage(img, 0, srcY, srcW, srcY+1, dst.Pix[i:i+rowSize])
reverse(dst.Pix[i : i+rowSize]) reverse(dst.Pix[i : i+rowSize])
} }
return dst return dst
@ -135,15 +137,15 @@ func rotate180(img image.Image) image.Image {
// rotate270 rotates the image 270 counter-clockwise. // rotate270 rotates the image 270 counter-clockwise.
func rotate270(img image.Image) image.Image { func rotate270(img image.Image) image.Image {
src := newScanner(img) srcW, srcH := img.Bounds().Dx(), img.Bounds().Dy()
dstW := src.h dstW := srcH
dstH := src.w dstH := srcW
rowSize := dstW * 4 rowSize := dstW * 4
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH)) dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for y := 0; y < dstH; y++ { for y := 0; y < dstH; y++ {
i := y * dst.Stride i := y * dst.Stride
srcX := y srcX := y
src.scan(srcX, 0, srcX+1, src.h, dst.Pix[i:i+rowSize]) scanImage(img, srcX, 0, srcX+1, srcH, dst.Pix[i:i+rowSize])
reverse(dst.Pix[i : i+rowSize]) reverse(dst.Pix[i : i+rowSize])
} }
return dst return dst
@ -151,30 +153,30 @@ func rotate270(img image.Image) image.Image {
// transpose flips the image horizontally and rotates 90 counter-clockwise. // transpose flips the image horizontally and rotates 90 counter-clockwise.
func transpose(img image.Image) image.Image { func transpose(img image.Image) image.Image {
src := newScanner(img) srcW, srcH := img.Bounds().Dx(), img.Bounds().Dy()
dstW := src.h dstW := srcH
dstH := src.w dstH := srcW
rowSize := dstW * 4 rowSize := dstW * 4
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH)) dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for y := 0; y < dstH; y++ { for y := 0; y < dstH; y++ {
i := y * dst.Stride i := y * dst.Stride
srcX := y srcX := y
src.scan(srcX, 0, srcX+1, src.h, dst.Pix[i:i+rowSize]) scanImage(img, srcX, 0, srcX+1, srcH, dst.Pix[i:i+rowSize])
} }
return dst return dst
} }
// transverse flips the image vertically and rotates 90 counter-clockwise. // transverse flips the image vertically and rotates 90 counter-clockwise.
func transverse(img image.Image) image.Image { func transverse(img image.Image) image.Image {
src := newScanner(img) srcW, srcH := img.Bounds().Dx(), img.Bounds().Dy()
dstW := src.h dstW := srcH
dstH := src.w dstH := srcW
rowSize := dstW * 4 rowSize := dstW * 4
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH)) dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for y := 0; y < dstH; y++ { for y := 0; y < dstH; y++ {
i := y * dst.Stride i := y * dst.Stride
srcX := dstH - y - 1 srcX := dstH - y - 1
src.scan(srcX, 0, srcX+1, src.h, dst.Pix[i:i+rowSize]) scanImage(img, srcX, 0, srcX+1, srcH, dst.Pix[i:i+rowSize])
reverse(dst.Pix[i : i+rowSize]) reverse(dst.Pix[i : i+rowSize])
} }
return dst return dst
@ -182,12 +184,12 @@ func transverse(img image.Image) image.Image {
// resizeHorizontalLinear resizes image to given width using linear resampling. // resizeHorizontalLinear resizes image to given width using linear resampling.
func resizeHorizontalLinear(img image.Image, dstWidth int) image.Image { func resizeHorizontalLinear(img image.Image, dstWidth int) image.Image {
src := newScanner(img) srcW, srcH := img.Bounds().Dx(), img.Bounds().Dy()
dst := image.NewRGBA(image.Rect(0, 0, dstWidth, src.h)) dst := image.NewRGBA(image.Rect(0, 0, dstWidth, srcH))
weights := precomputeWeightsLinear(dstWidth, src.w) weights := precomputeWeightsLinear(dstWidth, srcW)
scanLine := make([]uint8, src.w*4) scanLine := make([]uint8, srcW*4)
for y := 0; y < src.h; y++ { for y := 0; y < srcH; y++ {
src.scan(0, y, src.w, y+1, scanLine) scanImage(img, 0, y, srcW, y+1, scanLine)
j0 := y * dst.Stride j0 := y * dst.Stride
for x := range weights { for x := range weights {
var r, g, b, a float64 var r, g, b, a float64
@ -201,13 +203,12 @@ func resizeHorizontalLinear(img image.Image, dstWidth int) image.Image {
a += aw a += aw
} }
if a != 0 { if a != 0 {
aInv := 1 / a
j := j0 + x*4 j := j0 + x*4
d := dst.Pix[j : j+4 : j+4] d := dst.Pix[j : j+4 : j+4]
d[0] = clampFloat(r * aInv) d[0] = clampFloatTo8(r / a)
d[1] = clampFloat(g * aInv) d[1] = clampFloatTo8(g / a)
d[2] = clampFloat(b * aInv) d[2] = clampFloatTo8(b / a)
d[3] = clampFloat(a) d[3] = clampFloatTo8(a)
} }
} }
} }
@ -216,12 +217,12 @@ func resizeHorizontalLinear(img image.Image, dstWidth int) image.Image {
// resizeVerticalLinear resizes image to given height using linear resampling. // resizeVerticalLinear resizes image to given height using linear resampling.
func resizeVerticalLinear(img image.Image, height int) image.Image { func resizeVerticalLinear(img image.Image, height int) image.Image {
src := newScanner(img) srcW, srcH := img.Bounds().Dx(), img.Bounds().Dy()
dst := image.NewNRGBA(image.Rect(0, 0, src.w, height)) dst := image.NewNRGBA(image.Rect(0, 0, srcW, height))
weights := precomputeWeightsLinear(height, src.h) weights := precomputeWeightsLinear(height, srcH)
scanLine := make([]uint8, src.h*4) scanLine := make([]uint8, srcH*4)
for x := 0; x < src.w; x++ { for x := 0; x < srcW; x++ {
src.scan(x, 0, x+1, src.h, scanLine) scanImage(img, x, 0, x+1, srcH, scanLine)
for y := range weights { for y := range weights {
var r, g, b, a float64 var r, g, b, a float64
for _, w := range weights[y] { for _, w := range weights[y] {
@ -234,13 +235,12 @@ func resizeVerticalLinear(img image.Image, height int) image.Image {
a += aw a += aw
} }
if a != 0 { if a != 0 {
aInv := 1 / a
j := y*dst.Stride + x*4 j := y*dst.Stride + x*4
d := dst.Pix[j : j+4 : j+4] d := dst.Pix[j : j+4 : j+4]
d[0] = clampFloat(r * aInv) d[0] = clampFloatTo8(r / a)
d[1] = clampFloat(g * aInv) d[1] = clampFloatTo8(g / a)
d[2] = clampFloat(b * aInv) d[2] = clampFloatTo8(b / a)
d[3] = clampFloat(a) d[3] = clampFloatTo8(a)
} }
} }
} }
@ -263,13 +263,14 @@ func precomputeWeightsLinear(dstSize, srcSize int) [][]indexWeight {
out := make([][]indexWeight, dstSize) out := make([][]indexWeight, dstSize)
tmp := make([]indexWeight, 0, dstSize*int(ru+2)*2) tmp := make([]indexWeight, 0, dstSize*int(ru+2)*2)
for v := 0; v < dstSize; v++ { for v := 0; v < len(out); v++ {
fu := (float64(v)+0.5)*du - 0.5 fu := (float64(v)+0.5)*du - 0.5
begin := int(math.Ceil(fu - ru)) begin := int(math.Ceil(fu - ru))
if begin < 0 { if begin < 0 {
begin = 0 begin = 0
} }
end := int(math.Floor(fu + ru)) end := int(math.Floor(fu + ru))
if end > srcSize-1 { if end > srcSize-1 {
end = srcSize - 1 end = srcSize - 1
@ -280,9 +281,13 @@ func precomputeWeightsLinear(dstSize, srcSize int) [][]indexWeight {
w := resampleLinear((float64(u) - fu) / scale) w := resampleLinear((float64(u) - fu) / scale)
if w != 0 { if w != 0 {
sum += w sum += w
tmp = append(tmp, indexWeight{index: u, weight: w}) tmp = append(tmp, indexWeight{
index: u,
weight: w,
})
} }
} }
if sum != 0 { if sum != 0 {
for i := range tmp { for i := range tmp {
tmp[i].weight /= sum tmp[i].weight /= sum
@ -305,37 +310,31 @@ func resampleLinear(x float64) float64 {
return 0 return 0
} }
// scanner wraps an image.Image for
// easier size access and image type
// agnostic access to data at coords.
type scanner struct {
image image.Image
w, h int
palette []color.NRGBA
}
// newScanner wraps an image.Image in scanner{} type.
func newScanner(img image.Image) *scanner {
b := img.Bounds()
s := &scanner{
image: img,
w: b.Dx(),
h: b.Dy(),
}
if img, ok := img.(*image.Paletted); ok {
s.palette = make([]color.NRGBA, len(img.Palette))
for i := 0; i < len(img.Palette); i++ {
s.palette[i] = color.NRGBAModel.Convert(img.Palette[i]).(color.NRGBA)
}
}
return s
}
// scan scans the given rectangular region of the image into dst. // scan scans the given rectangular region of the image into dst.
func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) { func scanImage(img image.Image, x1, y1, x2, y2 int, dst []uint8) {
switch img := s.image.(type) { switch img := img.(type) {
case *image.NRGBA: case *image.NRGBA:
scanNRGBA(img, x1, y1, x2, y2, dst)
case *image.NRGBA64:
scanNRGBA64(img, x1, y1, x2, y2, dst)
case *image.RGBA:
scanRGBA(img, x1, y1, x2, y2, dst)
case *image.RGBA64:
scanRGBA64(img, x1, y1, x2, y2, dst)
case *image.Gray:
scanGray(img, x1, y1, x2, y2, dst)
case *image.Gray16:
scanGray16(img, x1, y1, x2, y2, dst)
case *image.YCbCr:
scanYCbCr(img, x1, y1, x2, y2, dst)
case *image.Paletted:
scanPaletted(img, x1, y1, x2, y2, dst)
default:
scanAny(img, x1, y1, x2, y2, dst)
}
}
func scanNRGBA(img *image.NRGBA, x1, y1, x2, y2 int, dst []uint8) {
size := (x2 - x1) * 4 size := (x2 - x1) * 4
j := 0 j := 0
i := y1*img.Stride + x1*4 i := y1*img.Stride + x1*4
@ -357,8 +356,12 @@ func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
i += img.Stride i += img.Stride
} }
} }
}
case *image.NRGBA64: func scanNRGBA64(img *image.NRGBA64, x1, y1, x2, y2 int, dst []uint8) {
if img == nil {
panic(gtserror.New("nil check elimination"))
}
j := 0 j := 0
for y := y1; y < y2; y++ { for y := y1; y < y2; y++ {
i := y*img.Stride + x1*8 i := y*img.Stride + x1*8
@ -373,8 +376,12 @@ func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
i += 8 i += 8
} }
} }
}
case *image.RGBA: func scanRGBA(img *image.RGBA, x1, y1, x2, y2 int, dst []uint8) {
if img == nil {
panic(gtserror.New("nil check elimination"))
}
j := 0 j := 0
for y := y1; y < y2; y++ { for y := y1; y < y2; y++ {
i := y*img.Stride + x1*4 i := y*img.Stride + x1*4
@ -408,8 +415,12 @@ func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
i += 4 i += 4
} }
} }
}
case *image.RGBA64: func scanRGBA64(img *image.RGBA64, x1, y1, x2, y2 int, dst []uint8) {
if img == nil {
panic(gtserror.New("nil check elimination"))
}
j := 0 j := 0
for y := y1; y < y2; y++ { for y := y1; y < y2; y++ {
i := y*img.Stride + x1*8 i := y*img.Stride + x1*8
@ -440,8 +451,12 @@ func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
i += 8 i += 8
} }
} }
}
case *image.Gray: func scanGray(img *image.Gray, x1, y1, x2, y2 int, dst []uint8) {
if img == nil {
panic(gtserror.New("nil check elimination"))
}
j := 0 j := 0
for y := y1; y < y2; y++ { for y := y1; y < y2; y++ {
i := y*img.Stride + x1 i := y*img.Stride + x1
@ -456,8 +471,12 @@ func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
i++ i++
} }
} }
}
case *image.Gray16: func scanGray16(img *image.Gray16, x1, y1, x2, y2 int, dst []uint8) {
if img == nil {
panic(gtserror.New("nil check elimination"))
}
j := 0 j := 0
for y := y1; y < y2; y++ { for y := y1; y < y2; y++ {
i := y*img.Stride + x1*2 i := y*img.Stride + x1*2
@ -472,9 +491,11 @@ func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
i += 2 i += 2
} }
} }
}
case *image.YCbCr: func scanYCbCr(img *image.YCbCr, x1, y1, x2, y2 int, dst []uint8) {
j := 0 j := 0
x1 += img.Rect.Min.X x1 += img.Rect.Min.X
x2 += img.Rect.Min.X x2 += img.Rect.Min.X
y1 += img.Rect.Min.Y y1 += img.Rect.Min.Y
@ -482,27 +503,16 @@ func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
hy := img.Rect.Min.Y / 2 hy := img.Rect.Min.Y / 2
hx := img.Rect.Min.X / 2 hx := img.Rect.Min.X / 2
switch img.SubsampleRatio {
case image.YCbCrSubsampleRatio420:
for y := y1; y < y2; y++ { for y := y1; y < y2; y++ {
iy := (y-img.Rect.Min.Y)*img.YStride + (x1 - img.Rect.Min.X) iy := (y-img.Rect.Min.Y)*img.YStride + (x1 - img.Rect.Min.X)
var yBase int yBase := (y/2 - hy) * img.CStride
switch img.SubsampleRatio {
case image.YCbCrSubsampleRatio444, image.YCbCrSubsampleRatio422:
yBase = (y - img.Rect.Min.Y) * img.CStride
case image.YCbCrSubsampleRatio420, image.YCbCrSubsampleRatio440:
yBase = (y/2 - hy) * img.CStride
}
for x := x1; x < x2; x++ { for x := x1; x < x2; x++ {
var ic int ic := yBase + (x/2 - hx)
switch img.SubsampleRatio {
case image.YCbCrSubsampleRatio444, image.YCbCrSubsampleRatio440:
ic = yBase + (x - img.Rect.Min.X)
case image.YCbCrSubsampleRatio422, image.YCbCrSubsampleRatio420:
ic = yBase + (x/2 - hx)
default:
ic = img.COffset(x, y)
}
yy1 := int32(img.Y[iy]) * 0x10101 yy1 := int32(img.Y[iy]) * 0x10101
cb1 := int32(img.Cb[ic]) - 128 cb1 := int32(img.Cb[ic]) - 128
@ -540,12 +550,198 @@ func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
} }
} }
case *image.Paletted: case image.YCbCrSubsampleRatio422:
for y := y1; y < y2; y++ {
iy := (y-img.Rect.Min.Y)*img.YStride + (x1 - img.Rect.Min.X)
yBase := (y - img.Rect.Min.Y) * img.CStride
for x := x1; x < x2; x++ {
ic := yBase + (x/2 - hx)
yy1 := int32(img.Y[iy]) * 0x10101
cb1 := int32(img.Cb[ic]) - 128
cr1 := int32(img.Cr[ic]) - 128
r := yy1 + 91881*cr1
if uint32(r)&0xff000000 == 0 { //nolint:gosec
r >>= 16
} else {
r = ^(r >> 31)
}
g := yy1 - 22554*cb1 - 46802*cr1
if uint32(g)&0xff000000 == 0 { //nolint:gosec
g >>= 16
} else {
g = ^(g >> 31)
}
b := yy1 + 116130*cb1
if uint32(b)&0xff000000 == 0 { //nolint:gosec
b >>= 16
} else {
b = ^(b >> 31)
}
d := dst[j : j+4 : j+4]
d[0] = uint8(r) // #nosec G115 -- Overflow desired.
d[1] = uint8(g) // #nosec G115 -- Overflow desired.
d[2] = uint8(b) // #nosec G115 -- Overflow desired.
d[3] = 0xff
iy++
j += 4
}
}
case image.YCbCrSubsampleRatio440:
for y := y1; y < y2; y++ {
iy := (y-img.Rect.Min.Y)*img.YStride + (x1 - img.Rect.Min.X)
yBase := (y/2 - hy) * img.CStride
for x := x1; x < x2; x++ {
ic := yBase + (x - img.Rect.Min.X)
yy1 := int32(img.Y[iy]) * 0x10101
cb1 := int32(img.Cb[ic]) - 128
cr1 := int32(img.Cr[ic]) - 128
r := yy1 + 91881*cr1
if uint32(r)&0xff000000 == 0 { //nolint:gosec
r >>= 16
} else {
r = ^(r >> 31)
}
g := yy1 - 22554*cb1 - 46802*cr1
if uint32(g)&0xff000000 == 0 { //nolint:gosec
g >>= 16
} else {
g = ^(g >> 31)
}
b := yy1 + 116130*cb1
if uint32(b)&0xff000000 == 0 { //nolint:gosec
b >>= 16
} else {
b = ^(b >> 31)
}
d := dst[j : j+4 : j+4]
d[0] = uint8(r) // #nosec G115 -- Overflow desired.
d[1] = uint8(g) // #nosec G115 -- Overflow desired.
d[2] = uint8(b) // #nosec G115 -- Overflow desired.
d[3] = 0xff
iy++
j += 4
}
}
case image.YCbCrSubsampleRatio444:
for y := y1; y < y2; y++ {
iy := (y-img.Rect.Min.Y)*img.YStride + (x1 - img.Rect.Min.X)
yBase := (y - img.Rect.Min.Y) * img.CStride
for x := x1; x < x2; x++ {
ic := yBase + (x - img.Rect.Min.X)
yy1 := int32(img.Y[iy]) * 0x10101
cb1 := int32(img.Cb[ic]) - 128
cr1 := int32(img.Cr[ic]) - 128
r := yy1 + 91881*cr1
if uint32(r)&0xff000000 == 0 { //nolint:gosec
r >>= 16
} else {
r = ^(r >> 31)
}
g := yy1 - 22554*cb1 - 46802*cr1
if uint32(g)&0xff000000 == 0 { //nolint:gosec
g >>= 16
} else {
g = ^(g >> 31)
}
b := yy1 + 116130*cb1
if uint32(b)&0xff000000 == 0 { //nolint:gosec
b >>= 16
} else {
b = ^(b >> 31)
}
d := dst[j : j+4 : j+4]
d[0] = uint8(r) // #nosec G115 -- Overflow desired.
d[1] = uint8(g) // #nosec G115 -- Overflow desired.
d[2] = uint8(b) // #nosec G115 -- Overflow desired.
d[3] = 0xff
iy++
j += 4
}
}
default:
for y := y1; y < y2; y++ {
iy := (y-img.Rect.Min.Y)*img.YStride + (x1 - img.Rect.Min.X)
for x := x1; x < x2; x++ {
ic := img.COffset(x, y)
yy1 := int32(img.Y[iy]) * 0x10101
cb1 := int32(img.Cb[ic]) - 128
cr1 := int32(img.Cr[ic]) - 128
r := yy1 + 91881*cr1
if uint32(r)&0xff000000 == 0 { //nolint:gosec
r >>= 16
} else {
r = ^(r >> 31)
}
g := yy1 - 22554*cb1 - 46802*cr1
if uint32(g)&0xff000000 == 0 { //nolint:gosec
g >>= 16
} else {
g = ^(g >> 31)
}
b := yy1 + 116130*cb1
if uint32(b)&0xff000000 == 0 { //nolint:gosec
b >>= 16
} else {
b = ^(b >> 31)
}
d := dst[j : j+4 : j+4]
d[0] = uint8(r) // #nosec G115 -- Overflow desired.
d[1] = uint8(g) // #nosec G115 -- Overflow desired.
d[2] = uint8(b) // #nosec G115 -- Overflow desired.
d[3] = 0xff
iy++
j += 4
}
}
}
}
func scanPaletted(img *image.Paletted, x1, y1, x2, y2 int, dst []uint8) {
var palette [256]color.NRGBA
if len(palette) < len(img.Palette) {
panic(gtserror.New("bound check elimination"))
}
for i := 0; i < len(img.Palette); i++ {
palette[i] = colorToNRGBA(img.Palette[i])
}
j := 0 j := 0
for y := y1; y < y2; y++ { for y := y1; y < y2; y++ {
i := y*img.Stride + x1 i := y*img.Stride + x1
for x := x1; x < x2; x++ { for x := x1; x < x2; x++ {
c := s.palette[img.Pix[i]] c := palette[img.Pix[i]]
d := dst[j : j+4 : j+4] d := dst[j : j+4 : j+4]
d[0] = c.R d[0] = c.R
d[1] = c.G d[1] = c.G
@ -555,17 +751,53 @@ func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
i++ i++
} }
} }
}
default: // inlined from: image/color.NRGBAModel.Convert()
func colorToNRGBA(c color.Color) color.NRGBA {
if c, ok := c.(color.NRGBA); ok {
return c
}
r, g, b, a := c.RGBA()
if a == 0xffff {
return color.NRGBA{
uint8(r >> 8), // #nosec G115 -- from stdlib
uint8(g >> 8), // #nosec G115 -- from stdlib
uint8(b >> 8), // #nosec G115 -- from stdlib
0xff,
}
}
if a == 0 {
return color.NRGBA{
0,
0,
0,
0,
}
}
// Since Color.RGBA returns an alpha-premultiplied color,
// we should have r <= a && g <= a && b <= a.
r = (r * 0xffff) / a
g = (g * 0xffff) / a
b = (b * 0xffff) / a
return color.NRGBA{
uint8(r >> 8), // #nosec G115 -- from stdlib
uint8(g >> 8), // #nosec G115 -- from stdlib
uint8(b >> 8), // #nosec G115 -- from stdlib
uint8(a >> 8), // #nosec G115 -- from stdlib
}
}
func scanAny(img image.Image, x1, y1, x2, y2 int, dst []uint8) {
j := 0 j := 0
b := s.image.Bounds() b := img.Bounds()
x1 += b.Min.X x1 += b.Min.X
x2 += b.Min.X x2 += b.Min.X
y1 += b.Min.Y y1 += b.Min.Y
y2 += b.Min.Y y2 += b.Min.Y
for y := y1; y < y2; y++ { for y := y1; y < y2; y++ {
for x := x1; x < x2; x++ { for x := x1; x < x2; x++ {
r16, g16, b16, a16 := s.image.At(x, y).RGBA() r16, g16, b16, a16 := img.At(x, y).RGBA()
d := dst[j : j+4 : j+4] d := dst[j : j+4 : j+4]
switch a16 { switch a16 {
case 0xffff: case 0xffff:
@ -587,31 +819,27 @@ func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
j += 4 j += 4
} }
} }
}
} }
// reverse reverses the data // reverse reverses the data
// in contained pixel slice. // in contained pixel slice.
func reverse(pix []uint8) { func reverse(pix8 []uint8) {
if len(pix) <= 4 { if len(pix8) <= 4 || len(pix8)%4 != 0 {
return return
} }
i := 0 for i, j := 0, len(pix8)-4; i < j; i, j = i+4, j-4 {
j := len(pix) - 4 di := pix8[i : i+4 : i+4]
for i < j { dj := pix8[j : j+4 : j+4]
pi := pix[i : i+4 : i+4] di[0], dj[0] = dj[0], di[0]
pj := pix[j : j+4 : j+4] di[1], dj[1] = dj[1], di[1]
pi[0], pj[0] = pj[0], pi[0] di[2], dj[2] = dj[2], di[2]
pi[1], pj[1] = pj[1], pi[1] di[3], dj[3] = dj[3], di[3]
pi[2], pj[2] = pj[2], pi[2]
pi[3], pj[3] = pj[3], pi[3]
i += 4
j -= 4
} }
} }
// clampFloat rounds and clamps float64 value to fit into uint8. // clampFloatTo8 rounds and clamps
func clampFloat(x float64) uint8 { // float64 value to fit into uint8.
func clampFloatTo8(x float64) uint8 {
v := int64(x + 0.5) v := int64(x + 0.5)
if v > 255 { if v > 255 {
return 255 return 255

View file

@ -0,0 +1,157 @@
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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 <http://www.gnu.org/licenses/>.
package media
import (
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"path"
"reflect"
"strings"
"testing"
"golang.org/x/image/webp"
)
func BenchmarkFlipH(b *testing.B) {
benchmarkFunc(b, func(img image.Image) {
_ = flipH(img)
})
}
func BenchmarkFlipV(b *testing.B) {
benchmarkFunc(b, func(img image.Image) {
_ = flipV(img)
})
}
func BenchmarkRotate90(b *testing.B) {
benchmarkFunc(b, func(img image.Image) {
_ = rotate90(img)
})
}
func BenchmarkRotate180(b *testing.B) {
benchmarkFunc(b, func(img image.Image) {
_ = rotate180(img)
})
}
func BenchmarkRotate270(b *testing.B) {
benchmarkFunc(b, func(img image.Image) {
_ = rotate270(img)
})
}
func BenchmarkTranspose(b *testing.B) {
benchmarkFunc(b, func(img image.Image) {
_ = transpose(img)
})
}
func BenchmarkTransverse(b *testing.B) {
benchmarkFunc(b, func(img image.Image) {
_ = transverse(img)
})
}
func BenchmarkResizeHorizontalLinear(b *testing.B) {
benchmarkFunc(b, func(img image.Image) {
_ = resizeHorizontalLinear(img, 64)
})
}
func BenchmarkResizeVerticalLinear(b *testing.B) {
benchmarkFunc(b, func(img image.Image) {
_ = resizeVerticalLinear(img, 64)
})
}
func benchmarkFunc(b *testing.B, fn func(image.Image)) {
b.Helper()
for _, testcase := range []struct {
Path string
Decode func(io.Reader) (image.Image, error)
}{
{
Path: "./test/big-panda.gif",
Decode: gif.Decode,
},
{
Path: "./test/clock-original.gif",
Decode: gif.Decode,
},
{
Path: "./test/test-jpeg.jpg",
Decode: jpeg.Decode,
},
{
Path: "./test/test-png-noalphachannel.png",
Decode: png.Decode,
},
{
Path: "./test/test-png-alphachannel.png",
Decode: png.Decode,
},
{
Path: "./test/rainbow-original.png",
Decode: png.Decode,
},
{
Path: "./test/nb-flag-original.webp",
Decode: webp.Decode,
},
} {
file, err := openRead(testcase.Path)
if err != nil {
panic(err)
}
img, err := testcase.Decode(file)
if err != nil {
panic(err)
}
info, err := file.Stat()
if err != nil {
panic(err)
}
file.Close()
testname := fmt.Sprintf("ext=%s type=%s size=%d",
strings.TrimPrefix(path.Ext(testcase.Path), "."),
strings.TrimPrefix(reflect.TypeOf(img).String(), "*image."),
info.Size(),
)
b.Run(testname, func(b *testing.B) {
b.Helper()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
fn(img)
}
})
})
}
}

View file

@ -138,17 +138,29 @@ func readOrientation(r *os.File) int {
orientationTag = 0x0112 orientationTag = 0x0112
) )
// Setup a discard read buffer. // Setup a read buffer.
buf := new(byteutil.Buffer) var buf byteutil.Buffer
buf.Guarantee(32) buf.B = make([]byte, 0, 64)
// discard simply reads into buf. // discard simply reads into buf.
discard := func(n int) error { discard := func(n int) error {
buf.Guarantee(n) // ensure big enough buf.Guarantee(n)
_, err := io.ReadFull(r, buf.B[:n]) _, err := io.ReadFull(r, buf.B[:n])
return err return err
} }
// readUint16 reads uint16 bytes into buffer then parses.
readUint16 := func(b binary.ByteOrder) (uint16, error) {
_, err := io.ReadFull(r, buf.B[:2])
return b.Uint16(buf.B[:2]), err
}
// readUint32 reads uint32 bytes into buffer then parses.
readUint32 := func(b binary.ByteOrder) (uint32, error) {
_, err := io.ReadFull(r, buf.B[:4])
return b.Uint32(buf.B[:4]), err
}
// Skip past JPEG SOI marker. // Skip past JPEG SOI marker.
if err := discard(2); err != nil { if err := discard(2); err != nil {
return orientationUnspecified return orientationUnspecified
@ -157,13 +169,13 @@ func readOrientation(r *os.File) int {
// Find JPEG // Find JPEG
// APP1 marker. // APP1 marker.
for { for {
var marker, size uint16 marker, err := readUint16(binary.BigEndian)
if err != nil {
if err := binary.Read(r, binary.BigEndian, &marker); err != nil {
return orientationUnspecified return orientationUnspecified
} }
if err := binary.Read(r, binary.BigEndian, &size); err != nil { size, err := readUint16(binary.BigEndian)
if err != nil {
return orientationUnspecified return orientationUnspecified
} }
@ -184,11 +196,9 @@ func readOrientation(r *os.File) int {
} }
} }
// Check if EXIF // Check if EXIF header is present.
// header is present. header, err := readUint32(binary.BigEndian)
var header uint32 if err != nil {
if err := binary.Read(r, binary.BigEndian, &header); err != nil {
return orientationUnspecified return orientationUnspecified
} }
@ -200,17 +210,13 @@ func readOrientation(r *os.File) int {
return orientationUnspecified return orientationUnspecified
} }
// Read byte // Read byte order info.
// order info. byteOrderTag, err := readUint16(binary.BigEndian)
var ( if err != nil {
byteOrderTag uint16
byteOrder binary.ByteOrder
)
if err := binary.Read(r, binary.BigEndian, &byteOrderTag); err != nil {
return orientationUnspecified return orientationUnspecified
} }
var byteOrder binary.ByteOrder
switch byteOrderTag { switch byteOrderTag {
case byteOrderBE: case byteOrderBE:
byteOrder = binary.BigEndian byteOrder = binary.BigEndian
@ -224,11 +230,9 @@ func readOrientation(r *os.File) int {
return orientationUnspecified return orientationUnspecified
} }
// Skip the // Skip the EXIF offset.
// EXIF offset. offset, err := readUint32(byteOrder)
var offset uint32 if err != nil {
if err := binary.Read(r, byteOrder, &offset); err != nil {
return orientationUnspecified return orientationUnspecified
} }
@ -240,19 +244,16 @@ func readOrientation(r *os.File) int {
return orientationUnspecified return orientationUnspecified
} }
// Read the // Read the number of tags.
// number of tags. numTags, err := readUint16(byteOrder)
var numTags uint16 if err != nil {
if err := binary.Read(r, byteOrder, &numTags); err != nil {
return orientationUnspecified return orientationUnspecified
} }
// Find the orientation tag. // Find the orientation tag.
for i := 0; i < int(numTags); i++ { for i := 0; i < int(numTags); i++ {
var tag uint16 tag, err := readUint16(byteOrder)
if err != nil {
if err := binary.Read(r, byteOrder, &tag); err != nil {
return orientationUnspecified return orientationUnspecified
} }
@ -267,9 +268,8 @@ func readOrientation(r *os.File) int {
return orientationUnspecified return orientationUnspecified
} }
var val uint16 val, err := readUint16(byteOrder)
if err != nil {
if err := binary.Read(r, byteOrder, &val); err != nil {
return orientationUnspecified return orientationUnspecified
} }

View file

@ -44,11 +44,6 @@ type ProcessingEmoji struct {
mgr *Manager // mgr instance (access to db / storage) mgr *Manager // mgr instance (access to db / storage)
} }
// ID returns the ID of the underlying emoji.
func (p *ProcessingEmoji) ID() string {
return p.emoji.ID // immutable, safe outside mutex.
}
// LoadEmoji blocks until the static and fullsize image has been processed, and then returns the completed emoji. // LoadEmoji blocks until the static and fullsize image has been processed, and then returns the completed emoji.
func (p *ProcessingEmoji) Load(ctx context.Context) (*gtsmodel.Emoji, error) { func (p *ProcessingEmoji) Load(ctx context.Context) (*gtsmodel.Emoji, error) {
emoji, done, err := p.load(ctx) emoji, done, err := p.load(ctx)
@ -63,6 +58,33 @@ func (p *ProcessingEmoji) Load(ctx context.Context) (*gtsmodel.Emoji, error) {
return emoji, err return emoji, err
} }
func (p *ProcessingEmoji) LoadAsync(deferred func()) *gtsmodel.Emoji {
p.mgr.state.Workers.Dereference.Queue.Push(func(ctx context.Context) {
if deferred != nil {
defer deferred()
}
if _, _, err := p.load(ctx); err != nil {
log.Errorf(ctx, "error loading emoji: %v", err)
}
})
// Placeholder returns a copy of internally stored processing placeholder,
// returning only the fields that may be known *before* completion,
// and as such all fields which are safe to concurrently read.
placeholder := new(gtsmodel.Emoji)
placeholder.ID = p.emoji.ID
placeholder.Shortcode = p.emoji.Shortcode
placeholder.Domain = p.emoji.Domain
placeholder.Cached = new(bool)
placeholder.ImageRemoteURL = p.emoji.ImageRemoteURL
placeholder.ImageStaticRemoteURL = p.emoji.ImageStaticRemoteURL
placeholder.Disabled = p.emoji.Disabled
placeholder.VisibleInPicker = p.emoji.VisibleInPicker
placeholder.CategoryID = p.emoji.CategoryID
return placeholder
}
// load is the package private form of load() that is wrapped to catch context canceled. // load is the package private form of load() that is wrapped to catch context canceled.
func (p *ProcessingEmoji) load(ctx context.Context) ( func (p *ProcessingEmoji) load(ctx context.Context) (
emoji *gtsmodel.Emoji, emoji *gtsmodel.Emoji,

View file

@ -25,6 +25,7 @@ import (
"time" "time"
"code.superseriousbusiness.org/gotosocial/internal/gtserror" "code.superseriousbusiness.org/gotosocial/internal/gtserror"
"code.superseriousbusiness.org/gotosocial/internal/log"
"code.superseriousbusiness.org/gotosocial/internal/util" "code.superseriousbusiness.org/gotosocial/internal/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/ulule/limiter/v3" "github.com/ulule/limiter/v3"
@ -78,30 +79,43 @@ func RateLimit(limit int, except []netip.Prefix) gin.HandlerFunc {
// Use Gin's heuristic for determining // Use Gin's heuristic for determining
// clientIP, which accounts for reverse // clientIP, which accounts for reverse
// proxies and trusted proxies setting. // proxies and trusted proxies setting.
clientIP := netip.MustParseAddr(c.ClientIP()) clientIP := c.ClientIP()
// ClientIP must be parseable.
ip, err := netip.ParseAddr(clientIP)
if err != nil {
log.Warnf(
c.Request.Context(),
"cannot do rate limiting for this request as client IP %s could not be parsed;"+
" your upstream reverse proxy may be misconfigured: %v",
err,
)
c.Next()
return
}
// Check if this IP is exempt from rate // Check if this IP is exempt from rate
// limits and skip further checks if so. // limits and skip further checks if so.
for _, prefix := range except { for _, prefix := range except {
if prefix.Contains(clientIP) { if prefix.Contains(ip) {
c.Next() c.Next()
return return
} }
} }
if clientIP.Is6() { if ip.Is6() {
// Convert to "net" package IP for mask. // Convert to "net" package IP for mask.
asIP := net.IP(clientIP.AsSlice()) asIP := net.IP(ip.AsSlice())
// Apply coarse IPv6 mask. // Apply coarse IPv6 mask.
asIP = asIP.Mask(ipv6Mask) asIP = asIP.Mask(ipv6Mask)
// Convert back to netip.Addr from net.IP. // Convert back to netip.Addr from net.IP.
clientIP, _ = netip.AddrFromSlice(asIP) ip, _ = netip.AddrFromSlice(asIP)
} }
// Fetch rate limit info for this (masked) clientIP. // Fetch rate limit info for this (masked) clientIP.
context, err := limiter.Get(c, clientIP.String()) context, err := limiter.Get(c, ip.String())
if err != nil { if err != nil {
// Since we use an in-memory cache now, // Since we use an in-memory cache now,
// it's actually impossible for this to // it's actually impossible for this to

View file

@ -293,6 +293,7 @@ func (p *Processor) emojiUpdateCopy(
// Ensure target emoji is locally cached. // Ensure target emoji is locally cached.
target, err := p.federator.RecacheEmoji(ctx, target, err := p.federator.RecacheEmoji(ctx,
target, target,
false,
) )
if err != nil { if err != nil {
err := gtserror.Newf("error recaching emoji %s: %w", target.ImageRemoteURL, err) err := gtserror.Newf("error recaching emoji %s: %w", target.ImageRemoteURL, err)

View file

@ -247,6 +247,7 @@ func (p *Processor) getEmojiContent(
emoji, err = p.federator.RecacheEmoji( emoji, err = p.federator.RecacheEmoji(
ctx, ctx,
emoji, emoji,
false,
) )
if err != nil { if err != nil {
err := gtserror.Newf("error recaching emoji: %w", err) err := gtserror.Newf("error recaching emoji: %w", err)

View file

@ -2438,8 +2438,8 @@ func (c *Converter) InteractionReqToASAuthorization(
} }
// appendASInteractionAuthorization is a utility function // appendASInteractionAuthorization is a utility function
// that sets `approvedBy`, and `likeAuthorization`, // that sets `approvedBy`, and (if possible) `likeAuthorization`,
// `replyAuthorization`, or `announceAuthorization`. // `replyAuthorization`, and/or `announceAuthorization`.
func (c *Converter) appendASInteractionAuthorization( func (c *Converter) appendASInteractionAuthorization(
ctx context.Context, ctx context.Context,
approvedByURIStr string, approvedByURIStr string,
@ -2458,11 +2458,28 @@ func (c *Converter) appendASInteractionAuthorization(
gtscontext.SetBarebones(ctx), gtscontext.SetBarebones(ctx),
approvedByURIStr, approvedByURIStr,
) )
if err != nil { if err != nil && !errors.Is(err, db.ErrNoEntries) {
return gtserror.Newf("db error checking for int req: %w", err) return gtserror.Newf("db error checking for int req: %w", err)
} }
// Make sure it's actually accepted. // If the interaction request is nil,
// that means we originally sent out
// the interaction request impolitely,
// and it was accepted impolitely.
// Ie., behavior from <= v0.20.0.
//
// If this is so, just set `approvedBy`
// to given approvedByURIStr and bail,
// as there's nothing else we can do.
if intReq == nil {
if wap, ok := t.(ap.WithApprovedBy); ok {
ap.SetApprovedBy(wap, approvedByURI)
}
return nil
}
// Make sure interaction request
// has actually been accepted.
if !intReq.IsAccepted() { if !intReq.IsAccepted() {
return gtserror.Newf( return gtserror.Newf(
"approvedByURIStr %s corresponded to not-accepted interaction request %s", "approvedByURIStr %s corresponded to not-accepted interaction request %s",