modernize the codebase
Run the modernize linter across the codebase. Change generated by running: ``` go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... ``` Ref: https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize
This commit is contained in:
		@@ -23,18 +23,18 @@ func main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			// Configuration
 | 
								// Configuration
 | 
				
			||||||
			config.Load,
 | 
								config.Load,
 | 
				
			||||||
			
 | 
					
 | 
				
			||||||
			// Database
 | 
								// Database
 | 
				
			||||||
			database.Connect,
 | 
								database.Connect,
 | 
				
			||||||
			
 | 
					
 | 
				
			||||||
			// Services
 | 
								// Services
 | 
				
			||||||
			services.NewPostService,
 | 
								services.NewPostService,
 | 
				
			||||||
			services.NewImgKitHelper,
 | 
								services.NewImgKitHelper,
 | 
				
			||||||
			
 | 
					
 | 
				
			||||||
			// Auth
 | 
								// Auth
 | 
				
			||||||
			auth.NewJwtTokenGenerator,
 | 
								auth.NewJwtTokenGenerator,
 | 
				
			||||||
			auth.NewGiteaOauth2Token,
 | 
								auth.NewGiteaOauth2Token,
 | 
				
			||||||
			
 | 
					
 | 
				
			||||||
			// Handlers
 | 
								// Handlers
 | 
				
			||||||
			handlers.NewAdminDashboardHandler,
 | 
								handlers.NewAdminDashboardHandler,
 | 
				
			||||||
			handlers.NewApiEndpointHandler,
 | 
								handlers.NewApiEndpointHandler,
 | 
				
			||||||
@@ -44,7 +44,7 @@ func main() {
 | 
				
			|||||||
		fx.Invoke(
 | 
							fx.Invoke(
 | 
				
			||||||
			// Start background tasks
 | 
								// Start background tasks
 | 
				
			||||||
			startBackgroundTasks,
 | 
								startBackgroundTasks,
 | 
				
			||||||
			
 | 
					
 | 
				
			||||||
			// Setup and start server
 | 
								// Setup and start server
 | 
				
			||||||
			server.SetupRouter,
 | 
								server.SetupRouter,
 | 
				
			||||||
		),
 | 
							),
 | 
				
			||||||
@@ -62,49 +62,49 @@ func startBackgroundTasks(
 | 
				
			|||||||
	lc.Append(fx.Hook{
 | 
						lc.Append(fx.Hook{
 | 
				
			||||||
		OnStart: func(context.Context) error {
 | 
							OnStart: func(context.Context) error {
 | 
				
			||||||
			ticker := time.NewTicker(10 * time.Minute)
 | 
								ticker := time.NewTicker(10 * time.Minute)
 | 
				
			||||||
			
 | 
					
 | 
				
			||||||
			runFetchPosts := func() {
 | 
								runFetchPosts := func() {
 | 
				
			||||||
				ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
 | 
									ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
 | 
				
			||||||
				defer cancel()
 | 
									defer cancel()
 | 
				
			||||||
				
 | 
					
 | 
				
			||||||
				posts, err := postService.GetPostsFromApi(ctx, cfg.Tag, cfg.Instance)
 | 
									posts, err := postService.GetPostsFromApi(ctx, cfg.Tag, cfg.Instance)
 | 
				
			||||||
				if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
					logger.Error("Failed to fetch posts", zap.Error(err))
 | 
										logger.Error("Failed to fetch posts", zap.Error(err))
 | 
				
			||||||
					return
 | 
										return
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				
 | 
					
 | 
				
			||||||
				existingPostIds := postService.GetExistingPostIds()
 | 
									existingPostIds := postService.GetExistingPostIds()
 | 
				
			||||||
				existingAccountIds := postService.GetExistingAccountIds()
 | 
									existingAccountIds := postService.GetExistingAccountIds()
 | 
				
			||||||
				newPosts := postService.GetNewPosts(existingPostIds, posts)
 | 
									newPosts := postService.GetNewPosts(existingPostIds, posts)
 | 
				
			||||||
				newAccounts := postService.GetNewAccounts(existingAccountIds, newPosts)
 | 
									newAccounts := postService.GetNewAccounts(existingAccountIds, newPosts)
 | 
				
			||||||
				
 | 
					
 | 
				
			||||||
				logger.Info("Fetched posts",
 | 
									logger.Info("Fetched posts",
 | 
				
			||||||
					zap.Int("total", len(posts)),
 | 
										zap.Int("total", len(posts)),
 | 
				
			||||||
					zap.Int("existing", len(existingPostIds)),
 | 
										zap.Int("existing", len(existingPostIds)),
 | 
				
			||||||
					zap.Int("new", len(newPosts)),
 | 
										zap.Int("new", len(newPosts)),
 | 
				
			||||||
					zap.Int("new_accounts", len(newAccounts)))
 | 
										zap.Int("new_accounts", len(newAccounts)))
 | 
				
			||||||
				
 | 
					
 | 
				
			||||||
				if len(newAccounts) > 0 {
 | 
									if len(newAccounts) > 0 {
 | 
				
			||||||
					count := postService.InsertNewAccounts(newAccounts)
 | 
										count := postService.InsertNewAccounts(newAccounts)
 | 
				
			||||||
					logger.Info("Inserted accounts", zap.Int("count", count))
 | 
										logger.Info("Inserted accounts", zap.Int("count", count))
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				
 | 
					
 | 
				
			||||||
				if len(newPosts) > 0 {
 | 
									if len(newPosts) > 0 {
 | 
				
			||||||
					count := postService.InsertNewPosts(newPosts)
 | 
										count := postService.InsertNewPosts(newPosts)
 | 
				
			||||||
					logger.Info("Inserted posts", zap.Int("count", count))
 | 
										logger.Info("Inserted posts", zap.Int("count", count))
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			
 | 
					
 | 
				
			||||||
			// Run initial fetch
 | 
								// Run initial fetch
 | 
				
			||||||
			go runFetchPosts()
 | 
								go runFetchPosts()
 | 
				
			||||||
			
 | 
					
 | 
				
			||||||
			// Start ticker
 | 
								// Start ticker
 | 
				
			||||||
			go func() {
 | 
								go func() {
 | 
				
			||||||
				for range ticker.C {
 | 
									for range ticker.C {
 | 
				
			||||||
					runFetchPosts()
 | 
										runFetchPosts()
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}()
 | 
								}()
 | 
				
			||||||
			
 | 
					
 | 
				
			||||||
			return nil
 | 
								return nil
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		OnStop: func(context.Context) error {
 | 
							OnStop: func(context.Context) error {
 | 
				
			||||||
@@ -129,6 +129,6 @@ func NewFXLogger() *FXLogger {
 | 
				
			|||||||
	return &FXLogger{logger: logger}
 | 
						return &FXLogger{logger: logger}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (l *FXLogger) Printf(str string, args ...interface{}) {
 | 
					func (l *FXLogger) Printf(str string, args ...any) {
 | 
				
			||||||
	l.logger.Sugar().Infof(str, args...)
 | 
						l.logger.Sugar().Infof(str, args...)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@ package auth
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"CatsOfMastodonBotGo/internal/config"
 | 
						"CatsOfMastodonBotGo/internal/config"
 | 
				
			||||||
 | 
						"maps"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -17,16 +18,14 @@ func NewJwtTokenGenerator(cfg *config.Config) *JwtTokenGenerator {
 | 
				
			|||||||
	return &JwtTokenGenerator{cfg: cfg}
 | 
						return &JwtTokenGenerator{cfg: cfg}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (j *JwtTokenGenerator) GenerateToken(claims map[string]interface{}) (string, error) {
 | 
					func (j *JwtTokenGenerator) GenerateToken(claims map[string]any) (string, error) {
 | 
				
			||||||
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
 | 
						token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
 | 
				
			||||||
		"exp": time.Now().AddDate(0, 0, 1).Unix(),
 | 
							"exp": time.Now().AddDate(0, 0, 1).Unix(),
 | 
				
			||||||
		"iat": time.Now().Unix(),
 | 
							"iat": time.Now().Unix(),
 | 
				
			||||||
		"iss": j.cfg.JwtIssuer,
 | 
							"iss": j.cfg.JwtIssuer,
 | 
				
			||||||
		"aud": j.cfg.JwtAudience,
 | 
							"aud": j.cfg.JwtAudience,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	for k, v := range claims {
 | 
						maps.Copy(token.Claims.(jwt.MapClaims), claims)
 | 
				
			||||||
		token.Claims.(jwt.MapClaims)[k] = v
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return token.SignedString([]byte(j.cfg.JwtSecret))
 | 
						return token.SignedString([]byte(j.cfg.JwtSecret))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -41,7 +40,7 @@ func (j *JwtTokenGenerator) GinMiddleware() gin.HandlerFunc {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		tokenString := strings.TrimPrefix(authHeader, "Bearer ")
 | 
							tokenString := strings.TrimPrefix(authHeader, "Bearer ")
 | 
				
			||||||
		t, err := jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) {
 | 
							t, err := jwt.Parse(tokenString, func(t *jwt.Token) (any, error) {
 | 
				
			||||||
			if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
 | 
								if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
 | 
				
			||||||
				return nil, jwt.ErrSignatureInvalid
 | 
									return nil, jwt.ErrSignatureInvalid
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -61,4 +60,4 @@ func (j *JwtTokenGenerator) GinMiddleware() gin.HandlerFunc {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		c.Next()
 | 
							c.Next()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,6 +8,7 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"math/rand"
 | 
						"math/rand"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"slices"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"gorm.io/gorm"
 | 
						"gorm.io/gorm"
 | 
				
			||||||
@@ -163,10 +164,5 @@ func (ps *PostService) GetMedia() domain.MediaAttachment {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func arrayContains(arr []string, str string) bool {
 | 
					func arrayContains(arr []string, str string) bool {
 | 
				
			||||||
	for _, a := range arr {
 | 
						return slices.Contains(arr, str)
 | 
				
			||||||
		if a == str {
 | 
					}
 | 
				
			||||||
			return true
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return false
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,7 +28,7 @@ func NewAdminDashboardHandler(
 | 
				
			|||||||
		postService: postService,
 | 
							postService: postService,
 | 
				
			||||||
		jwt:         jwt,
 | 
							jwt:         jwt,
 | 
				
			||||||
		cfg:         cfg,
 | 
							cfg:         cfg,
 | 
				
			||||||
		 imgKit:      imgKit,
 | 
							imgKit:      imgKit,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -61,8 +61,8 @@ func (adh *AdminDashboardHandler) RejectMedia(c *gin.Context) {
 | 
				
			|||||||
func (adh *AdminDashboardHandler) GetMedia(c *gin.Context) {
 | 
					func (adh *AdminDashboardHandler) GetMedia(c *gin.Context) {
 | 
				
			||||||
	media := adh.postService.GetMedia()
 | 
						media := adh.postService.GetMedia()
 | 
				
			||||||
	// TODO: Fix this - we need to inject ImgKitHelper
 | 
						// TODO: Fix this - we need to inject ImgKitHelper
 | 
				
			||||||
	 media.PreviewUrl = adh.imgKit.GetPreviewUrl(media.RemoteUrl)
 | 
						media.PreviewUrl = adh.imgKit.GetPreviewUrl(media.RemoteUrl)
 | 
				
			||||||
	 media.RemoteUrl = adh.imgKit.GetRemoteUrl(media.RemoteUrl)
 | 
						media.RemoteUrl = adh.imgKit.GetRemoteUrl(media.RemoteUrl)
 | 
				
			||||||
	c.JSON(http.StatusOK, media)
 | 
						c.JSON(http.StatusOK, media)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -77,7 +77,7 @@ func (adh *AdminDashboardHandler) Login(c *gin.Context) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if input.Password == adh.cfg.AdminPassword { // Its more than enough for this project
 | 
						if input.Password == adh.cfg.AdminPassword { // Its more than enough for this project
 | 
				
			||||||
		token, err := adh.jwt.GenerateToken(map[string]interface{}{"role": "admin"})
 | 
							token, err := adh.jwt.GenerateToken(map[string]any{"role": "admin"})
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"})
 | 
								c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"})
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
@@ -97,4 +97,4 @@ func (adh *AdminDashboardHandler) Login(c *gin.Context) {
 | 
				
			|||||||
// Expose the JWT middleware for use in routes
 | 
					// Expose the JWT middleware for use in routes
 | 
				
			||||||
func (adh *AdminDashboardHandler) JWTMiddleware() gin.HandlerFunc {
 | 
					func (adh *AdminDashboardHandler) JWTMiddleware() gin.HandlerFunc {
 | 
				
			||||||
	return adh.jwt.GinMiddleware()
 | 
						return adh.jwt.GinMiddleware()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,9 +10,9 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type OauthLoginHandler struct {
 | 
					type OauthLoginHandler struct {
 | 
				
			||||||
	jwt         *auth.JwtTokenGenerator
 | 
						jwt          *auth.JwtTokenGenerator
 | 
				
			||||||
	oauthHandler *auth.GiteaOAuth2Handler
 | 
						oauthHandler *auth.GiteaOAuth2Handler
 | 
				
			||||||
	cfg         *config.Config
 | 
						cfg          *config.Config
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewOauthLoginHandler(
 | 
					func NewOauthLoginHandler(
 | 
				
			||||||
@@ -55,7 +55,7 @@ func (olh *OauthLoginHandler) LoginWithGitea(c *gin.Context) {
 | 
				
			|||||||
	// Check if the user's email is in the allowed list
 | 
						// Check if the user's email is in the allowed list
 | 
				
			||||||
	for _, email := range olh.cfg.GiteaOauthAllowedEmails {
 | 
						for _, email := range olh.cfg.GiteaOauthAllowedEmails {
 | 
				
			||||||
		if email == userEmail {
 | 
							if email == userEmail {
 | 
				
			||||||
			token, err := olh.jwt.GenerateToken(map[string]interface{}{"role": "admin"})
 | 
								token, err := olh.jwt.GenerateToken(map[string]any{"role": "admin"})
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"})
 | 
									c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"})
 | 
				
			||||||
				return
 | 
									return
 | 
				
			||||||
@@ -70,4 +70,4 @@ func (olh *OauthLoginHandler) LoginWithGitea(c *gin.Context) {
 | 
				
			|||||||
	c.JSON(401, gin.H{
 | 
						c.JSON(401, gin.H{
 | 
				
			||||||
		"error": "oauth login failed or your email does not have access",
 | 
							"error": "oauth login failed or your email does not have access",
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user