From f0fe2e78fac15e58bce16526487ef8887e8abac9 Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Mon, 13 May 2024 23:52:26 -0600 Subject: [PATCH 1/6] Fix link to API repo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 75a8a7f..e1b9da1 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This is the Federation service for the Sublinks project. It's built using Go. -Together with the [Sublinks Core](https://github.com/sublinks/sublinks) and [Sublinks Frontend](https://github.com/sublinks/sublinks-frontend) it's creating a federated link aggregation and microblogging platform. +Together with the [Sublinks Core](https://github.com/sublinks/sublinks-api) and [Sublinks Frontend](https://github.com/sublinks/sublinks-frontend) it's creating a federated link aggregation and microblogging platform. ## Contributing From e964e333976e308d88aff1f54f410621c54ad6b8 Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Tue, 21 May 2024 20:54:17 -0600 Subject: [PATCH 2/6] feat: add shellcheck disable directive to .env-sample Add a comment to disable shellcheck SC2034 warning in the .env-sample file and set the default DB_TYPE to "postgres". --- .env-sample | 1 + 1 file changed, 1 insertion(+) diff --git a/.env-sample b/.env-sample index 1e2bf90..7f26d1e 100644 --- a/.env-sample +++ b/.env-sample @@ -1,3 +1,4 @@ +# shellcheck disable=SC2034 #DB_TYPE="mysql" #DB_DSN="federation:federation@tcp(127.0.0.1:3306)/federation?charset=utf8mb4&parseTime=True&loc=Local" DB_TYPE="postgres" From be6ae0dc2e4394a436eadfc5b55fc8470b24806f Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Sun, 26 May 2024 13:37:21 -0600 Subject: [PATCH 3/6] Convert to using service layer Refactor federation command and HTTP handlers to use new service layer for actors, posts, and comments. Replace direct database access with service methods to improve modularity and separation of concerns. Remove the repository layer as it's now redundant with the service layer. Update model definitions to reflect changes in primary keys and fields. Adjust queue processing to utilize the new service layer for actor, post, and comment handling. Add error handling where necessary and ensure proper logging of errors. --- cmd/federation.go | 13 ++++++++-- go.mod | 2 +- internal/http/activity.go | 11 +++----- internal/http/community.go | 15 +++++------ internal/http/post.go | 14 +++------- internal/http/server.go | 4 +++ internal/http/user.go | 14 +++++----- internal/model/actor.go | 6 ++--- internal/queue/consumer.go | 16 ++++++------ internal/queue/queue.go | 32 ++++++++++++----------- internal/repository/repository.go | 26 ------------------- internal/service/actors/actors.go | 37 +++++++++++++++++++++++++++ internal/service/comments/comments.go | 28 ++++++++++++++++++++ internal/service/posts/posts.go | 28 ++++++++++++++++++++ internal/service/service.go | 4 +++ internal/worker/actor.go | 12 ++++----- internal/worker/comment.go | 12 ++++----- internal/worker/post.go | 10 ++++---- 18 files changed, 179 insertions(+), 105 deletions(-) delete mode 100644 internal/repository/repository.go create mode 100644 internal/service/actors/actors.go create mode 100644 internal/service/comments/comments.go create mode 100644 internal/service/posts/posts.go create mode 100644 internal/service/service.go diff --git a/cmd/federation.go b/cmd/federation.go index 4246595..3da4bb5 100644 --- a/cmd/federation.go +++ b/cmd/federation.go @@ -6,6 +6,10 @@ import ( "sublinks/sublinks-federation/internal/http" "sublinks/sublinks-federation/internal/log" "sublinks/sublinks-federation/internal/queue" + "sublinks/sublinks-federation/internal/service" + "sublinks/sublinks-federation/internal/service/actors" + "sublinks/sublinks-federation/internal/service/comments" + "sublinks/sublinks-federation/internal/service/posts" "github.com/joho/godotenv" ) @@ -33,11 +37,16 @@ func main() { logger.Fatal("failed connecting to queue service", err) } defer q.Close() - q.Run(conn) + services := map[string]service.Service{ + "actors": *actors.NewActorService(conn), + "posts": *posts.NewPostService(conn), + "comments": *comments.NewCommentService(conn), + } + q.Run(services) config := http.ServerConfig{ Logger: logger, - Database: conn, Queue: q, + Services: services, } s := http.NewServer(config) s.RunServer() diff --git a/go.mod b/go.mod index f67c5c7..a1784e5 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/joho/godotenv v1.5.1 github.com/rabbitmq/amqp091-go v1.10.0 github.com/rs/zerolog v1.33.0 + golang.org/x/sync v0.5.0 golang.org/x/text v0.15.0 gorm.io/driver/mysql v1.5.6 gorm.io/driver/postgres v1.5.7 @@ -26,6 +27,5 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect go.uber.org/atomic v1.11.0 // indirect golang.org/x/crypto v0.20.0 // indirect - golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.21.0 // indirect ) diff --git a/internal/http/activity.go b/internal/http/activity.go index 07a90b4..491804f 100644 --- a/internal/http/activity.go +++ b/internal/http/activity.go @@ -5,7 +5,7 @@ import ( "fmt" "net/http" "sublinks/sublinks-federation/internal/activitypub" - "sublinks/sublinks-federation/internal/model" + "sublinks/sublinks-federation/internal/service/posts" "golang.org/x/text/cases" "golang.org/x/text/language" @@ -53,11 +53,6 @@ func (server *Server) getActivityHandler(w http.ResponseWriter, r *http.Request) } func (server *Server) GetPostActivityObject(id string) (*activitypub.Page, error) { - post := model.Post{UrlStub: id} - err := server.Database.Find(&post) - if err != nil { - server.Logger.Error("Error reading post", err) - return nil, err - } - return activitypub.ConvertPostToPage(&post), nil + post := server.Services["posts"].(posts.PostService).FindPost(id) + return activitypub.ConvertPostToPage(post), nil } diff --git a/internal/http/community.go b/internal/http/community.go index aa9740f..fa117fc 100644 --- a/internal/http/community.go +++ b/internal/http/community.go @@ -5,7 +5,7 @@ import ( "fmt" "net/http" "sublinks/sublinks-federation/internal/activitypub" - "sublinks/sublinks-federation/internal/model" + "sublinks/sublinks-federation/internal/service/actors" "github.com/gorilla/mux" ) @@ -17,18 +17,17 @@ func (server *Server) SetupCommunityRoutes() { func (server *Server) getCommunityInfoHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) server.Logger.Info(fmt.Sprintf("Looking up community %s", vars["community"])) - community := model.Actor{Username: vars["community"], ActorType: "Group"} - err := server.Database.Find(&community) - if err != nil { - server.Logger.Error("Error reading community", err) + community := server.Services["actors"].(actors.ActorService).FindCommunity(vars["community"]) + if community == nil { + server.Logger.Error("Community not found", nil) + w.WriteHeader(http.StatusNotFound) return } - - communityLd := activitypub.ConvertActorToGroup(&community) + communityLd := activitypub.ConvertActorToGroup(community) w.WriteHeader(http.StatusOK) w.Header().Add("content-type", "application/activity+json") content, _ := json.MarshalIndent(communityLd, "", " ") - _, err = w.Write(content) + _, err := w.Write(content) if err != nil { server.Logger.Error("Error writing response", err) } diff --git a/internal/http/post.go b/internal/http/post.go index 1fb913b..ffe6efb 100644 --- a/internal/http/post.go +++ b/internal/http/post.go @@ -2,10 +2,9 @@ package http import ( "encoding/json" - "fmt" "net/http" "sublinks/sublinks-federation/internal/activitypub" - "sublinks/sublinks-federation/internal/model" + "sublinks/sublinks-federation/internal/service/posts" "github.com/gorilla/mux" ) @@ -16,18 +15,13 @@ func (server *Server) SetupPostRoutes() { func (server *Server) getPostHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - post := model.Post{UrlStub: vars["postId"]} - err := server.Database.Find(&post) - if err != nil { - server.Logger.Error(fmt.Sprintf("Error reading post: %+v %s", post, err), err) - return - } - postLd := activitypub.ConvertPostToPage(&post) + post := server.Services["posts"].(posts.PostService).FindPost(vars["postId"]) + postLd := activitypub.ConvertPostToPage(post) postLd.Context = activitypub.GetContext() w.WriteHeader(http.StatusOK) w.Header().Add("content-type", "application/activity+json") content, _ := json.MarshalIndent(postLd, "", " ") - _, err = w.Write(content) + _, err := w.Write(content) if err != nil { server.Logger.Error("Error writing response", err) } diff --git a/internal/http/server.go b/internal/http/server.go index e51f90e..65e044e 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -9,6 +9,7 @@ import ( "sublinks/sublinks-federation/internal/db" "sublinks/sublinks-federation/internal/log" "sublinks/sublinks-federation/internal/queue" + "sublinks/sublinks-federation/internal/service" "time" "github.com/gorilla/mux" @@ -19,12 +20,14 @@ type Server struct { log.Logger db.Database queue.Queue + Services map[string]service.Service } type ServerConfig struct { log.Logger db.Database queue.Queue + Services map[string]service.Service } func NewServer(config ServerConfig) *Server { @@ -35,6 +38,7 @@ func NewServer(config ServerConfig) *Server { Logger: config.Logger, Database: config.Database, Queue: config.Queue, + Services: config.Services, } } diff --git a/internal/http/user.go b/internal/http/user.go index 566c321..004f0ba 100644 --- a/internal/http/user.go +++ b/internal/http/user.go @@ -5,7 +5,7 @@ import ( "fmt" "net/http" "sublinks/sublinks-federation/internal/activitypub" - "sublinks/sublinks-federation/internal/model" + "sublinks/sublinks-federation/internal/service/actors" "github.com/gorilla/mux" ) @@ -17,18 +17,18 @@ func (server *Server) SetupUserRoutes() { func (server *Server) getUserInfoHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) server.Logger.Info(fmt.Sprintf("Looking up user %s", vars["user"])) - user := model.Actor{Username: vars["user"], ActorType: "Person"} - err := server.Database.Find(&user) - if err != nil { - server.Logger.Error("Error reading user", err) + user := server.Services["actors"].(actors.ActorService).FindUser(vars["user"]) + if user == nil { + server.Logger.Error("User not found", nil) + w.WriteHeader(http.StatusNotFound) return } - userLd := activitypub.ConvertActorToPerson(&user) + userLd := activitypub.ConvertActorToPerson(user) w.WriteHeader(http.StatusOK) w.Header().Add("content-type", "application/activity+json") content, _ := json.MarshalIndent(userLd, "", " ") - _, err = w.Write(content) + _, err := w.Write(content) if err != nil { server.Logger.Error("Error writing response", err) } diff --git a/internal/model/actor.go b/internal/model/actor.go index a5591f2..bff4ad5 100644 --- a/internal/model/actor.go +++ b/internal/model/actor.go @@ -1,9 +1,9 @@ package model type Actor struct { - ActorType string `json:"actor_type" gorm:"index"` - Id string `json:"id" gorm:"primarykey"` - Username string `json:"username"` + ActorType string `json:"actor_type" gorm:"primarykey"` + Id string `json:"actor_id" gorm:"primarykey"` + Username string `json:"display_name,omitempty" gorm:"not null"` Name string `json:"name,omitempty" gorm:"nullable"` Bio string `json:"bio"` MatrixUserId string `json:"matrix_user_id,omitempty"` diff --git a/internal/queue/consumer.go b/internal/queue/consumer.go index 1234abb..2c1ea5b 100644 --- a/internal/queue/consumer.go +++ b/internal/queue/consumer.go @@ -1,10 +1,10 @@ package queue import ( - "errors" "fmt" - "golang.org/x/sync/errgroup" "sublinks/sublinks-federation/internal/worker" + + "golang.org/x/sync/errgroup" ) type ConsumerQueue struct { @@ -23,7 +23,7 @@ func (q *RabbitQueue) createConsumer(queueData ConsumerQueue) error { return err } - for routingKey, _ := range queueData.RoutingKeys { + for routingKey := range queueData.RoutingKeys { err = channelRabbitMQ.QueueBind( queueData.QueueName, // queue name routingKey, // routing key @@ -59,7 +59,7 @@ func (q *RabbitQueue) StartConsumer(queueData ConsumerQueue) error { } messages, ok := q.consumers[queueData.QueueName] if !ok { - return errors.New("consumer not found") + return fmt.Errorf("consumer not found") } errGroup := new(errgroup.Group) @@ -67,7 +67,7 @@ func (q *RabbitQueue) StartConsumer(queueData ConsumerQueue) error { errGroup.Go(func() error { cbWorker, ok := queueData.RoutingKeys[message.RoutingKey] if !ok { - return errors.New(fmt.Sprintf("%s not implemented as valid routing key", message.RoutingKey)) + return fmt.Errorf("%s not implemented as valid routing key", message.RoutingKey) } err := cbWorker.Process(message.Body) @@ -75,14 +75,14 @@ func (q *RabbitQueue) StartConsumer(queueData ConsumerQueue) error { if err != nil { err = message.Acknowledger.Nack(message.DeliveryTag, false, true) if err != nil { - return errors.New(fmt.Sprintf("error nack'ing the message: %s", err.Error())) + return fmt.Errorf("error nack'ing the message: %s", err.Error()) } - return errors.New(fmt.Sprintf("error processing message body: %s", err.Error())) + return fmt.Errorf("error processing message body: %s", err.Error()) } err = message.Acknowledger.Ack(message.DeliveryTag, false) if err != nil { - return errors.New(fmt.Sprintf("error ack'ing the message: %s", err.Error())) + return fmt.Errorf("error ack'ing the message: %s", err.Error()) } return nil }) diff --git a/internal/queue/queue.go b/internal/queue/queue.go index c560b1b..ddac9b7 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -2,17 +2,19 @@ package queue import ( "os" - "sublinks/sublinks-federation/internal/repository" + "sublinks/sublinks-federation/internal/log" + "sublinks/sublinks-federation/internal/service" + "sublinks/sublinks-federation/internal/service/actors" + "sublinks/sublinks-federation/internal/service/comments" + "sublinks/sublinks-federation/internal/service/posts" "sublinks/sublinks-federation/internal/worker" amqp "github.com/rabbitmq/amqp091-go" - "sublinks/sublinks-federation/internal/db" - "sublinks/sublinks-federation/internal/log" ) type Queue interface { Connect() error - Run(conn db.Database) + Run(services map[string]service.Service) PublishMessage(queueName string, message string) error StartConsumer(queueData ConsumerQueue) error Status() map[string]map[string]bool @@ -49,19 +51,19 @@ func (q *RabbitQueue) Status() map[string]map[string]bool { return status } -func (q *RabbitQueue) Run(conn db.Database) { - q.processActors(conn) - q.processObjects(conn) +func (q *RabbitQueue) Run(services map[string]service.Service) { + q.processActors(services) + q.processObjects(services) } -func (q *RabbitQueue) processActors(conn db.Database) { +func (q *RabbitQueue) processActors(services map[string]service.Service) { actorCQ := ConsumerQueue{ QueueName: "actor_create_queue", Exchange: "federation", RoutingKeys: map[string]worker.Worker{ ActorRoutingKey: &worker.ActorWorker{ - Logger: q.logger, - Repository: repository.NewRepository(conn), + Logger: q.logger, + Service: services["actors"].(actors.ActorService), }, }, } @@ -72,18 +74,18 @@ func (q *RabbitQueue) processActors(conn db.Database) { } } -func (q *RabbitQueue) processObjects(conn db.Database) { +func (q *RabbitQueue) processObjects(services map[string]service.Service) { queue := ConsumerQueue{ QueueName: "object_create_queue", Exchange: "federation", RoutingKeys: map[string]worker.Worker{ PostRoutingKey: &worker.PostWorker{ - Logger: q.logger, - Repository: repository.NewRepository(conn), + Logger: q.logger, + Service: services["posts"].(posts.PostService), }, CommentRoutingKey: &worker.CommentWorker{ - Logger: q.logger, - Repository: repository.NewRepository(conn), + Logger: q.logger, + Service: services["comments"].(comments.CommentService), }, }, } diff --git a/internal/repository/repository.go b/internal/repository/repository.go deleted file mode 100644 index 9d966bc..0000000 --- a/internal/repository/repository.go +++ /dev/null @@ -1,26 +0,0 @@ -package repository - -import ( - "sublinks/sublinks-federation/internal/db" -) - -type Repository interface { - Save(interface{}) error - Find(*interface{}, ...interface{}) error -} - -type RepositoryImpl struct { - db db.Database -} - -func NewRepository(db db.Database) Repository { - return &RepositoryImpl{db: db} -} - -func (repository *RepositoryImpl) Save(a interface{}) error { - return repository.db.Save(a) -} - -func (repository *RepositoryImpl) Find(a *interface{}, params ...interface{}) error { - return repository.db.Find(a, params...) -} diff --git a/internal/service/actors/actors.go b/internal/service/actors/actors.go new file mode 100644 index 0000000..72d641e --- /dev/null +++ b/internal/service/actors/actors.go @@ -0,0 +1,37 @@ +package actors + +import ( + "sublinks/sublinks-federation/internal/db" + "sublinks/sublinks-federation/internal/model" +) + +type ActorService struct { + db db.Database +} + +func NewActorService(db db.Database) *ActorService { + return &ActorService{db} +} + +func (a ActorService) FindCommunity(id string) *model.Actor { + actor := model.Actor{ActorType: "Group"} + return a.Load(&actor, id) +} + +func (a ActorService) FindUser(id string) *model.Actor { + actor := model.Actor{ActorType: "Person"} + return a.Load(&actor, id) +} + +func (a ActorService) Load(actor *model.Actor, id string) *model.Actor { + err := a.db.Find(actor, id) + if err != nil { + return actor + } + return nil +} + +func (a *ActorService) Save(actor *model.Actor) bool { + err := a.db.Save(actor) + return err == nil +} diff --git a/internal/service/comments/comments.go b/internal/service/comments/comments.go new file mode 100644 index 0000000..de0b363 --- /dev/null +++ b/internal/service/comments/comments.go @@ -0,0 +1,28 @@ +package comments + +import ( + "sublinks/sublinks-federation/internal/db" + "sublinks/sublinks-federation/internal/model" +) + +type CommentService struct { + db db.Database +} + +func NewCommentService(db db.Database) *CommentService { + return &CommentService{db} +} + +func (p CommentService) FindComment(id string) *model.Comment { + comment := &model.Comment{} + err := p.db.Find(comment, id) + if err != nil { + return comment + } + return nil +} + +func (p *CommentService) Save(comment *model.Comment) bool { + err := p.db.Save(comment) + return err == nil +} diff --git a/internal/service/posts/posts.go b/internal/service/posts/posts.go new file mode 100644 index 0000000..7aa341c --- /dev/null +++ b/internal/service/posts/posts.go @@ -0,0 +1,28 @@ +package posts + +import ( + "sublinks/sublinks-federation/internal/db" + "sublinks/sublinks-federation/internal/model" +) + +type PostService struct { + db db.Database +} + +func NewPostService(db db.Database) *PostService { + return &PostService{db} +} + +func (p PostService) FindPost(id string) *model.Post { + post := &model.Post{} + err := p.db.Find(post, id) + if err != nil { + return post + } + return nil +} + +func (p *PostService) Save(post *model.Post) bool { + err := p.db.Save(post) + return err == nil +} diff --git a/internal/service/service.go b/internal/service/service.go new file mode 100644 index 0000000..dbb5531 --- /dev/null +++ b/internal/service/service.go @@ -0,0 +1,4 @@ +package service + +type Service interface { +} diff --git a/internal/worker/actor.go b/internal/worker/actor.go index be3bf79..c9ccd93 100644 --- a/internal/worker/actor.go +++ b/internal/worker/actor.go @@ -2,14 +2,15 @@ package worker import ( "encoding/json" + "errors" "sublinks/sublinks-federation/internal/log" "sublinks/sublinks-federation/internal/model" - "sublinks/sublinks-federation/internal/repository" + "sublinks/sublinks-federation/internal/service/actors" ) type ActorWorker struct { log.Logger - repository.Repository + Service actors.ActorService } func (w *ActorWorker) Process(msg []byte) error { @@ -19,10 +20,9 @@ func (w *ActorWorker) Process(msg []byte) error { w.Logger.Error("Error unmarshalling actor", err) return err } - err = w.Repository.Save(&actor) - if err != nil { - w.Logger.Error("Error saving actor", err) - return err + if !w.Service.Save(&actor) { + w.Logger.Error("Error saving actor", nil) + return errors.New("Error saving actor") } return nil } diff --git a/internal/worker/comment.go b/internal/worker/comment.go index 8529ab7..4758546 100644 --- a/internal/worker/comment.go +++ b/internal/worker/comment.go @@ -2,16 +2,17 @@ package worker import ( "encoding/json" + "errors" "os" "strings" "sublinks/sublinks-federation/internal/log" "sublinks/sublinks-federation/internal/model" - "sublinks/sublinks-federation/internal/repository" + "sublinks/sublinks-federation/internal/service/comments" ) type CommentWorker struct { log.Logger - repository.Repository + Service comments.CommentService } func (w *CommentWorker) Process(msg []byte) error { @@ -23,10 +24,9 @@ func (w *CommentWorker) Process(msg []byte) error { w.Logger.Error("Error unmarshalling comment: %s", err) return err } - err = w.Repository.Save(comment) - if err != nil { - w.Logger.Error("Error saving comment: %s", err) - return err + if !w.Service.Save(&comment) { + w.Logger.Error("Error saving comment", nil) + return errors.New("Error saving comment") } return nil } diff --git a/internal/worker/post.go b/internal/worker/post.go index 8998405..c9b8646 100644 --- a/internal/worker/post.go +++ b/internal/worker/post.go @@ -6,12 +6,12 @@ import ( "strings" "sublinks/sublinks-federation/internal/log" "sublinks/sublinks-federation/internal/model" - "sublinks/sublinks-federation/internal/repository" + "sublinks/sublinks-federation/internal/service/posts" ) type PostWorker struct { log.Logger - repository.Repository + Service posts.PostService } func (w *PostWorker) Process(msg []byte) error { @@ -23,9 +23,9 @@ func (w *PostWorker) Process(msg []byte) error { w.Logger.Error("Error unmarshalling post: %s", err) return err } - err = w.Repository.Save(post) - if err != nil { - w.Logger.Error("Error saving post: %s", err) + res := w.Service.Save(&post) + if !res { + w.Logger.Error("Error saving post", nil) return err } return nil From c4116ab36bc94eae7a4ee6b5a134523c5a64a7a8 Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Fri, 7 Jun 2024 23:31:04 -0600 Subject: [PATCH 4/6] refactor: restructure services and update HTTP handlers - Replace the map of services with a ServiceManager to manage services - Update federation.go to use ServiceManager instead of a service map - Modify HTTP handlers to use ServiceManager's methods for accessing services - Remove ActorService and split its responsibilities into UserService and CommunityService - Update Queue to use ServiceManager and adjust workers to use new services - Refactor PostService and CommentService to have a consistent GetById method - Remove unnecessary type assertions in HTTP handlers and workers - Add constructors for new UserService, CommunityService, and workers - Ensure all services and workers follow the new structure and interfaces --- cmd/federation.go | 19 +++++++------ internal/http/activity.go | 3 +- internal/http/community.go | 3 +- internal/http/post.go | 3 +- internal/http/server.go | 14 +++++----- internal/http/user.go | 3 +- internal/queue/queue.go | 40 +++++++++++++-------------- internal/service/actors/actors.go | 37 ------------------------- internal/service/actors/community.go | 29 +++++++++++++++++++ internal/service/actors/user.go | 32 +++++++++++++++++++++ internal/service/comments/comments.go | 4 +-- internal/service/posts/posts.go | 4 +-- internal/service/service.go | 37 ++++++++++++++++++++++++- internal/worker/actor.go | 22 ++++++++++++--- internal/worker/comment.go | 11 ++++++-- internal/worker/post.go | 11 ++++++-- 16 files changed, 177 insertions(+), 95 deletions(-) delete mode 100644 internal/service/actors/actors.go create mode 100644 internal/service/actors/community.go create mode 100644 internal/service/actors/user.go diff --git a/cmd/federation.go b/cmd/federation.go index 3da4bb5..c4f3a88 100644 --- a/cmd/federation.go +++ b/cmd/federation.go @@ -37,16 +37,17 @@ func main() { logger.Fatal("failed connecting to queue service", err) } defer q.Close() - services := map[string]service.Service{ - "actors": *actors.NewActorService(conn), - "posts": *posts.NewPostService(conn), - "comments": *comments.NewCommentService(conn), - } - q.Run(services) + serviceManager := service.NewServiceManager( + actors.NewUserService(conn), + actors.NewCommunityService(conn), + posts.NewPostService(conn), + comments.NewCommentService(conn), + ) + q.Run(serviceManager) config := http.ServerConfig{ - Logger: logger, - Queue: q, - Services: services, + Logger: logger, + Queue: q, + ServiceManager: serviceManager, } s := http.NewServer(config) s.RunServer() diff --git a/internal/http/activity.go b/internal/http/activity.go index 491804f..60e6e22 100644 --- a/internal/http/activity.go +++ b/internal/http/activity.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "sublinks/sublinks-federation/internal/activitypub" - "sublinks/sublinks-federation/internal/service/posts" "golang.org/x/text/cases" "golang.org/x/text/language" @@ -53,6 +52,6 @@ func (server *Server) getActivityHandler(w http.ResponseWriter, r *http.Request) } func (server *Server) GetPostActivityObject(id string) (*activitypub.Page, error) { - post := server.Services["posts"].(posts.PostService).FindPost(id) + post := server.ServiceManager.GetPostService().GetById(id) return activitypub.ConvertPostToPage(post), nil } diff --git a/internal/http/community.go b/internal/http/community.go index fa117fc..5141465 100644 --- a/internal/http/community.go +++ b/internal/http/community.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "sublinks/sublinks-federation/internal/activitypub" - "sublinks/sublinks-federation/internal/service/actors" "github.com/gorilla/mux" ) @@ -17,7 +16,7 @@ func (server *Server) SetupCommunityRoutes() { func (server *Server) getCommunityInfoHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) server.Logger.Info(fmt.Sprintf("Looking up community %s", vars["community"])) - community := server.Services["actors"].(actors.ActorService).FindCommunity(vars["community"]) + community := server.ServiceManager.GetCommunityService().GetById(vars["community"]) if community == nil { server.Logger.Error("Community not found", nil) w.WriteHeader(http.StatusNotFound) diff --git a/internal/http/post.go b/internal/http/post.go index ffe6efb..6dccade 100644 --- a/internal/http/post.go +++ b/internal/http/post.go @@ -4,7 +4,6 @@ import ( "encoding/json" "net/http" "sublinks/sublinks-federation/internal/activitypub" - "sublinks/sublinks-federation/internal/service/posts" "github.com/gorilla/mux" ) @@ -15,7 +14,7 @@ func (server *Server) SetupPostRoutes() { func (server *Server) getPostHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - post := server.Services["posts"].(posts.PostService).FindPost(vars["postId"]) + post := server.ServiceManager.GetPostService().GetById(vars["postId"]) postLd := activitypub.ConvertPostToPage(post) postLd.Context = activitypub.GetContext() w.WriteHeader(http.StatusOK) diff --git a/internal/http/server.go b/internal/http/server.go index 65e044e..e2d3be9 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -20,25 +20,25 @@ type Server struct { log.Logger db.Database queue.Queue - Services map[string]service.Service + ServiceManager *service.ServiceManager } type ServerConfig struct { log.Logger db.Database queue.Queue - Services map[string]service.Service + ServiceManager *service.ServiceManager } func NewServer(config ServerConfig) *Server { r := mux.NewRouter() return &Server{ - Router: r, - Logger: config.Logger, - Database: config.Database, - Queue: config.Queue, - Services: config.Services, + Router: r, + Logger: config.Logger, + Database: config.Database, + Queue: config.Queue, + ServiceManager: config.ServiceManager, } } diff --git a/internal/http/user.go b/internal/http/user.go index 004f0ba..f7e914a 100644 --- a/internal/http/user.go +++ b/internal/http/user.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "sublinks/sublinks-federation/internal/activitypub" - "sublinks/sublinks-federation/internal/service/actors" "github.com/gorilla/mux" ) @@ -17,7 +16,7 @@ func (server *Server) SetupUserRoutes() { func (server *Server) getUserInfoHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) server.Logger.Info(fmt.Sprintf("Looking up user %s", vars["user"])) - user := server.Services["actors"].(actors.ActorService).FindUser(vars["user"]) + user := server.ServiceManager.GetUserService().GetById(vars["user"]) if user == nil { server.Logger.Error("User not found", nil) w.WriteHeader(http.StatusNotFound) diff --git a/internal/queue/queue.go b/internal/queue/queue.go index ddac9b7..148f265 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -4,9 +4,6 @@ import ( "os" "sublinks/sublinks-federation/internal/log" "sublinks/sublinks-federation/internal/service" - "sublinks/sublinks-federation/internal/service/actors" - "sublinks/sublinks-federation/internal/service/comments" - "sublinks/sublinks-federation/internal/service/posts" "sublinks/sublinks-federation/internal/worker" amqp "github.com/rabbitmq/amqp091-go" @@ -14,7 +11,7 @@ import ( type Queue interface { Connect() error - Run(services map[string]service.Service) + Run(serviceManager *service.ServiceManager) PublishMessage(queueName string, message string) error StartConsumer(queueData ConsumerQueue) error Status() map[string]map[string]bool @@ -51,20 +48,21 @@ func (q *RabbitQueue) Status() map[string]map[string]bool { return status } -func (q *RabbitQueue) Run(services map[string]service.Service) { - q.processActors(services) - q.processObjects(services) +func (q *RabbitQueue) Run(serviceManager *service.ServiceManager) { + q.processActors(serviceManager) + q.processObjects(serviceManager) } -func (q *RabbitQueue) processActors(services map[string]service.Service) { +func (q *RabbitQueue) processActors(serviceManager *service.ServiceManager) { actorCQ := ConsumerQueue{ QueueName: "actor_create_queue", Exchange: "federation", RoutingKeys: map[string]worker.Worker{ - ActorRoutingKey: &worker.ActorWorker{ - Logger: q.logger, - Service: services["actors"].(actors.ActorService), - }, + ActorRoutingKey: worker.NewActorWorker( + q.logger, + serviceManager.GetUserService(), + serviceManager.GetCommunityService(), + ), }, } @@ -74,19 +72,19 @@ func (q *RabbitQueue) processActors(services map[string]service.Service) { } } -func (q *RabbitQueue) processObjects(services map[string]service.Service) { +func (q *RabbitQueue) processObjects(serviceManager *service.ServiceManager) { queue := ConsumerQueue{ QueueName: "object_create_queue", Exchange: "federation", RoutingKeys: map[string]worker.Worker{ - PostRoutingKey: &worker.PostWorker{ - Logger: q.logger, - Service: services["posts"].(posts.PostService), - }, - CommentRoutingKey: &worker.CommentWorker{ - Logger: q.logger, - Service: services["comments"].(comments.CommentService), - }, + PostRoutingKey: worker.NewPostWorker( + q.logger, + serviceManager.GetPostService(), + ), + CommentRoutingKey: worker.NewCommentWorker( + q.logger, + serviceManager.GetCommentService(), + ), }, } diff --git a/internal/service/actors/actors.go b/internal/service/actors/actors.go deleted file mode 100644 index 72d641e..0000000 --- a/internal/service/actors/actors.go +++ /dev/null @@ -1,37 +0,0 @@ -package actors - -import ( - "sublinks/sublinks-federation/internal/db" - "sublinks/sublinks-federation/internal/model" -) - -type ActorService struct { - db db.Database -} - -func NewActorService(db db.Database) *ActorService { - return &ActorService{db} -} - -func (a ActorService) FindCommunity(id string) *model.Actor { - actor := model.Actor{ActorType: "Group"} - return a.Load(&actor, id) -} - -func (a ActorService) FindUser(id string) *model.Actor { - actor := model.Actor{ActorType: "Person"} - return a.Load(&actor, id) -} - -func (a ActorService) Load(actor *model.Actor, id string) *model.Actor { - err := a.db.Find(actor, id) - if err != nil { - return actor - } - return nil -} - -func (a *ActorService) Save(actor *model.Actor) bool { - err := a.db.Save(actor) - return err == nil -} diff --git a/internal/service/actors/community.go b/internal/service/actors/community.go new file mode 100644 index 0000000..3ff15f7 --- /dev/null +++ b/internal/service/actors/community.go @@ -0,0 +1,29 @@ +package actors + +import ( + "sublinks/sublinks-federation/internal/db" + "sublinks/sublinks-federation/internal/model" +) + +type CommunityService struct { + db db.Database +} + +func NewCommunityService(db db.Database) *CommunityService { + return &CommunityService{db} +} + +func (a CommunityService) GetById(id string) *model.Actor { + actor := &model.Actor{ActorType: "Group"} + a.load(actor, id) + return actor +} + +func (a CommunityService) load(actor *model.Actor, id string) { + _ = a.db.Find(actor, id) +} + +func (a CommunityService) Save(actor *model.Actor) bool { + err := a.db.Save(actor) + return err == nil +} diff --git a/internal/service/actors/user.go b/internal/service/actors/user.go new file mode 100644 index 0000000..1d23629 --- /dev/null +++ b/internal/service/actors/user.go @@ -0,0 +1,32 @@ +package actors + +import ( + "sublinks/sublinks-federation/internal/db" + "sublinks/sublinks-federation/internal/model" +) + +type UserService struct { + db db.Database +} + +func NewUserService(db db.Database) *UserService { + return &UserService{db} +} + +func (a UserService) GetById(id string) *model.Actor { + actor := model.Actor{ActorType: "Person"} + return a.Load(&actor, id) +} + +func (a UserService) Load(actor *model.Actor, id string) *model.Actor { + err := a.db.Find(actor, id) + if err != nil { + return actor + } + return nil +} + +func (a UserService) Save(actor *model.Actor) bool { + err := a.db.Save(actor) + return err == nil +} diff --git a/internal/service/comments/comments.go b/internal/service/comments/comments.go index de0b363..6d79daa 100644 --- a/internal/service/comments/comments.go +++ b/internal/service/comments/comments.go @@ -13,7 +13,7 @@ func NewCommentService(db db.Database) *CommentService { return &CommentService{db} } -func (p CommentService) FindComment(id string) *model.Comment { +func (p CommentService) GetById(id string) interface{} { comment := &model.Comment{} err := p.db.Find(comment, id) if err != nil { @@ -22,7 +22,7 @@ func (p CommentService) FindComment(id string) *model.Comment { return nil } -func (p *CommentService) Save(comment *model.Comment) bool { +func (p CommentService) Save(comment interface{}) bool { err := p.db.Save(comment) return err == nil } diff --git a/internal/service/posts/posts.go b/internal/service/posts/posts.go index 7aa341c..d7cf4ed 100644 --- a/internal/service/posts/posts.go +++ b/internal/service/posts/posts.go @@ -13,7 +13,7 @@ func NewPostService(db db.Database) *PostService { return &PostService{db} } -func (p PostService) FindPost(id string) *model.Post { +func (p PostService) GetById(id string) *model.Post { post := &model.Post{} err := p.db.Find(post, id) if err != nil { @@ -22,7 +22,7 @@ func (p PostService) FindPost(id string) *model.Post { return nil } -func (p *PostService) Save(post *model.Post) bool { +func (p PostService) Save(post *model.Post) bool { err := p.db.Save(post) return err == nil } diff --git a/internal/service/service.go b/internal/service/service.go index dbb5531..2dfa474 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -1,4 +1,39 @@ package service -type Service interface { +import ( + "sublinks/sublinks-federation/internal/service/actors" + "sublinks/sublinks-federation/internal/service/comments" + "sublinks/sublinks-federation/internal/service/posts" +) + +type ServiceManager struct { + userService *actors.UserService + communityService *actors.CommunityService + postService *posts.PostService + commentService *comments.CommentService +} + +func NewServiceManager(userService *actors.UserService, communityService *actors.CommunityService, postService *posts.PostService, commentService *comments.CommentService) *ServiceManager { + return &ServiceManager{ + userService: userService, + communityService: communityService, + postService: postService, + commentService: commentService, + } +} + +func (sm *ServiceManager) GetCommunityService() *actors.CommunityService { + return sm.communityService +} + +func (sm *ServiceManager) GetPostService() *posts.PostService { + return sm.postService +} + +func (sm *ServiceManager) GetUserService() *actors.UserService { + return sm.userService +} + +func (sm *ServiceManager) GetCommentService() *comments.CommentService { + return sm.commentService } diff --git a/internal/worker/actor.go b/internal/worker/actor.go index c9ccd93..3be72a6 100644 --- a/internal/worker/actor.go +++ b/internal/worker/actor.go @@ -10,7 +10,16 @@ import ( type ActorWorker struct { log.Logger - Service actors.ActorService + userService *actors.UserService + communityService *actors.CommunityService +} + +func NewActorWorker(logger log.Logger, userService *actors.UserService, communityService *actors.CommunityService) *ActorWorker { + return &ActorWorker{ + Logger: logger, + userService: userService, + communityService: communityService, + } } func (w *ActorWorker) Process(msg []byte) error { @@ -20,9 +29,14 @@ func (w *ActorWorker) Process(msg []byte) error { w.Logger.Error("Error unmarshalling actor", err) return err } - if !w.Service.Save(&actor) { - w.Logger.Error("Error saving actor", nil) - return errors.New("Error saving actor") + if actor.ActorType == "Group" && !w.communityService.Save(&actor) { + w.Logger.Error("Error saving actor (community)", nil) + return errors.New("Error saving actor (community)") + } + + if actor.ActorType == "Person" && !w.userService.Save(&actor) { + w.Logger.Error("Error saving actor (user)", nil) + return errors.New("Error saving actor (user)") } return nil } diff --git a/internal/worker/comment.go b/internal/worker/comment.go index 4758546..266dc3a 100644 --- a/internal/worker/comment.go +++ b/internal/worker/comment.go @@ -12,7 +12,14 @@ import ( type CommentWorker struct { log.Logger - Service comments.CommentService + service *comments.CommentService +} + +func NewCommentWorker(logger log.Logger, service *comments.CommentService) *CommentWorker { + return &CommentWorker{ + Logger: logger, + service: service, + } } func (w *CommentWorker) Process(msg []byte) error { @@ -24,7 +31,7 @@ func (w *CommentWorker) Process(msg []byte) error { w.Logger.Error("Error unmarshalling comment: %s", err) return err } - if !w.Service.Save(&comment) { + if !w.service.Save(&comment) { w.Logger.Error("Error saving comment", nil) return errors.New("Error saving comment") } diff --git a/internal/worker/post.go b/internal/worker/post.go index c9b8646..403ecb4 100644 --- a/internal/worker/post.go +++ b/internal/worker/post.go @@ -11,7 +11,14 @@ import ( type PostWorker struct { log.Logger - Service posts.PostService + service *posts.PostService +} + +func NewPostWorker(logger log.Logger, service *posts.PostService) *PostWorker { + return &PostWorker{ + Logger: logger, + service: service, + } } func (w *PostWorker) Process(msg []byte) error { @@ -23,7 +30,7 @@ func (w *PostWorker) Process(msg []byte) error { w.Logger.Error("Error unmarshalling post: %s", err) return err } - res := w.Service.Save(&post) + res := w.service.Save(&post) if !res { w.Logger.Error("Error saving post", nil) return err From 63ac8fadf72c7e6e9098bff31bf2af6e507e73e1 Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Fri, 7 Jun 2024 23:38:53 -0600 Subject: [PATCH 5/6] refactor: consolidate service packages and update references Move individual service packages into a single 'service' package and update all references to use the new structure. This includes renaming and moving files for user, community, post, and comment services, as well as updating the ServiceManager and worker implementations to align with the new package structure. --- cmd/federation.go | 11 ++++------ internal/service/{comments => }/comments.go | 2 +- internal/service/{actors => }/community.go | 2 +- internal/service/{posts => }/posts.go | 2 +- internal/service/service.go | 24 ++++++++------------- internal/service/{actors => }/user.go | 2 +- internal/worker/actor.go | 8 +++---- internal/worker/comment.go | 6 +++--- internal/worker/post.go | 6 +++--- 9 files changed, 27 insertions(+), 36 deletions(-) rename internal/service/{comments => }/comments.go (96%) rename internal/service/{actors => }/community.go (97%) rename internal/service/{posts => }/posts.go (96%) rename internal/service/{actors => }/user.go (97%) diff --git a/cmd/federation.go b/cmd/federation.go index c4f3a88..dfc2d21 100644 --- a/cmd/federation.go +++ b/cmd/federation.go @@ -7,9 +7,6 @@ import ( "sublinks/sublinks-federation/internal/log" "sublinks/sublinks-federation/internal/queue" "sublinks/sublinks-federation/internal/service" - "sublinks/sublinks-federation/internal/service/actors" - "sublinks/sublinks-federation/internal/service/comments" - "sublinks/sublinks-federation/internal/service/posts" "github.com/joho/godotenv" ) @@ -38,10 +35,10 @@ func main() { } defer q.Close() serviceManager := service.NewServiceManager( - actors.NewUserService(conn), - actors.NewCommunityService(conn), - posts.NewPostService(conn), - comments.NewCommentService(conn), + service.NewUserService(conn), + service.NewCommunityService(conn), + service.NewPostService(conn), + service.NewCommentService(conn), ) q.Run(serviceManager) config := http.ServerConfig{ diff --git a/internal/service/comments/comments.go b/internal/service/comments.go similarity index 96% rename from internal/service/comments/comments.go rename to internal/service/comments.go index 6d79daa..e95e05b 100644 --- a/internal/service/comments/comments.go +++ b/internal/service/comments.go @@ -1,4 +1,4 @@ -package comments +package service import ( "sublinks/sublinks-federation/internal/db" diff --git a/internal/service/actors/community.go b/internal/service/community.go similarity index 97% rename from internal/service/actors/community.go rename to internal/service/community.go index 3ff15f7..592bb14 100644 --- a/internal/service/actors/community.go +++ b/internal/service/community.go @@ -1,4 +1,4 @@ -package actors +package service import ( "sublinks/sublinks-federation/internal/db" diff --git a/internal/service/posts/posts.go b/internal/service/posts.go similarity index 96% rename from internal/service/posts/posts.go rename to internal/service/posts.go index d7cf4ed..b6c77f3 100644 --- a/internal/service/posts/posts.go +++ b/internal/service/posts.go @@ -1,4 +1,4 @@ -package posts +package service import ( "sublinks/sublinks-federation/internal/db" diff --git a/internal/service/service.go b/internal/service/service.go index 2dfa474..9c4c849 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -1,19 +1,13 @@ package service -import ( - "sublinks/sublinks-federation/internal/service/actors" - "sublinks/sublinks-federation/internal/service/comments" - "sublinks/sublinks-federation/internal/service/posts" -) - type ServiceManager struct { - userService *actors.UserService - communityService *actors.CommunityService - postService *posts.PostService - commentService *comments.CommentService + userService *UserService + communityService *CommunityService + postService *PostService + commentService *CommentService } -func NewServiceManager(userService *actors.UserService, communityService *actors.CommunityService, postService *posts.PostService, commentService *comments.CommentService) *ServiceManager { +func NewServiceManager(userService *UserService, communityService *CommunityService, postService *PostService, commentService *CommentService) *ServiceManager { return &ServiceManager{ userService: userService, communityService: communityService, @@ -22,18 +16,18 @@ func NewServiceManager(userService *actors.UserService, communityService *actors } } -func (sm *ServiceManager) GetCommunityService() *actors.CommunityService { +func (sm *ServiceManager) GetCommunityService() *CommunityService { return sm.communityService } -func (sm *ServiceManager) GetPostService() *posts.PostService { +func (sm *ServiceManager) GetPostService() *PostService { return sm.postService } -func (sm *ServiceManager) GetUserService() *actors.UserService { +func (sm *ServiceManager) GetUserService() *UserService { return sm.userService } -func (sm *ServiceManager) GetCommentService() *comments.CommentService { +func (sm *ServiceManager) GetCommentService() *CommentService { return sm.commentService } diff --git a/internal/service/actors/user.go b/internal/service/user.go similarity index 97% rename from internal/service/actors/user.go rename to internal/service/user.go index 1d23629..71db0a1 100644 --- a/internal/service/actors/user.go +++ b/internal/service/user.go @@ -1,4 +1,4 @@ -package actors +package service import ( "sublinks/sublinks-federation/internal/db" diff --git a/internal/worker/actor.go b/internal/worker/actor.go index 3be72a6..20dafb2 100644 --- a/internal/worker/actor.go +++ b/internal/worker/actor.go @@ -5,16 +5,16 @@ import ( "errors" "sublinks/sublinks-federation/internal/log" "sublinks/sublinks-federation/internal/model" - "sublinks/sublinks-federation/internal/service/actors" + "sublinks/sublinks-federation/internal/service" ) type ActorWorker struct { log.Logger - userService *actors.UserService - communityService *actors.CommunityService + userService *service.UserService + communityService *service.CommunityService } -func NewActorWorker(logger log.Logger, userService *actors.UserService, communityService *actors.CommunityService) *ActorWorker { +func NewActorWorker(logger log.Logger, userService *service.UserService, communityService *service.CommunityService) *ActorWorker { return &ActorWorker{ Logger: logger, userService: userService, diff --git a/internal/worker/comment.go b/internal/worker/comment.go index 266dc3a..37f5d59 100644 --- a/internal/worker/comment.go +++ b/internal/worker/comment.go @@ -7,15 +7,15 @@ import ( "strings" "sublinks/sublinks-federation/internal/log" "sublinks/sublinks-federation/internal/model" - "sublinks/sublinks-federation/internal/service/comments" + "sublinks/sublinks-federation/internal/service" ) type CommentWorker struct { log.Logger - service *comments.CommentService + service *service.CommentService } -func NewCommentWorker(logger log.Logger, service *comments.CommentService) *CommentWorker { +func NewCommentWorker(logger log.Logger, service *service.CommentService) *CommentWorker { return &CommentWorker{ Logger: logger, service: service, diff --git a/internal/worker/post.go b/internal/worker/post.go index 403ecb4..e82c753 100644 --- a/internal/worker/post.go +++ b/internal/worker/post.go @@ -6,15 +6,15 @@ import ( "strings" "sublinks/sublinks-federation/internal/log" "sublinks/sublinks-federation/internal/model" - "sublinks/sublinks-federation/internal/service/posts" + "sublinks/sublinks-federation/internal/service" ) type PostWorker struct { log.Logger - service *posts.PostService + service *service.PostService } -func NewPostWorker(logger log.Logger, service *posts.PostService) *PostWorker { +func NewPostWorker(logger log.Logger, service *service.PostService) *PostWorker { return &PostWorker{ Logger: logger, service: service, From d8ba86fa3d657a80176e6febf2a5223dc4fd13fa Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Sat, 8 Jun 2024 00:03:18 -0600 Subject: [PATCH 6/6] Don't use getters --- internal/http/activity.go | 2 +- internal/http/community.go | 2 +- internal/http/post.go | 2 +- internal/http/user.go | 2 +- internal/queue/queue.go | 8 ++++---- internal/service/service.go | 8 ++++---- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/http/activity.go b/internal/http/activity.go index 60e6e22..b79e36c 100644 --- a/internal/http/activity.go +++ b/internal/http/activity.go @@ -52,6 +52,6 @@ func (server *Server) getActivityHandler(w http.ResponseWriter, r *http.Request) } func (server *Server) GetPostActivityObject(id string) (*activitypub.Page, error) { - post := server.ServiceManager.GetPostService().GetById(id) + post := server.ServiceManager.PostService().GetById(id) return activitypub.ConvertPostToPage(post), nil } diff --git a/internal/http/community.go b/internal/http/community.go index 5141465..37358dd 100644 --- a/internal/http/community.go +++ b/internal/http/community.go @@ -16,7 +16,7 @@ func (server *Server) SetupCommunityRoutes() { func (server *Server) getCommunityInfoHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) server.Logger.Info(fmt.Sprintf("Looking up community %s", vars["community"])) - community := server.ServiceManager.GetCommunityService().GetById(vars["community"]) + community := server.ServiceManager.CommunityService().GetById(vars["community"]) if community == nil { server.Logger.Error("Community not found", nil) w.WriteHeader(http.StatusNotFound) diff --git a/internal/http/post.go b/internal/http/post.go index 6dccade..573264a 100644 --- a/internal/http/post.go +++ b/internal/http/post.go @@ -14,7 +14,7 @@ func (server *Server) SetupPostRoutes() { func (server *Server) getPostHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - post := server.ServiceManager.GetPostService().GetById(vars["postId"]) + post := server.ServiceManager.PostService().GetById(vars["postId"]) postLd := activitypub.ConvertPostToPage(post) postLd.Context = activitypub.GetContext() w.WriteHeader(http.StatusOK) diff --git a/internal/http/user.go b/internal/http/user.go index f7e914a..67c7e8f 100644 --- a/internal/http/user.go +++ b/internal/http/user.go @@ -16,7 +16,7 @@ func (server *Server) SetupUserRoutes() { func (server *Server) getUserInfoHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) server.Logger.Info(fmt.Sprintf("Looking up user %s", vars["user"])) - user := server.ServiceManager.GetUserService().GetById(vars["user"]) + user := server.ServiceManager.UserService().GetById(vars["user"]) if user == nil { server.Logger.Error("User not found", nil) w.WriteHeader(http.StatusNotFound) diff --git a/internal/queue/queue.go b/internal/queue/queue.go index 148f265..37ca7ff 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -60,8 +60,8 @@ func (q *RabbitQueue) processActors(serviceManager *service.ServiceManager) { RoutingKeys: map[string]worker.Worker{ ActorRoutingKey: worker.NewActorWorker( q.logger, - serviceManager.GetUserService(), - serviceManager.GetCommunityService(), + serviceManager.UserService(), + serviceManager.CommunityService(), ), }, } @@ -79,11 +79,11 @@ func (q *RabbitQueue) processObjects(serviceManager *service.ServiceManager) { RoutingKeys: map[string]worker.Worker{ PostRoutingKey: worker.NewPostWorker( q.logger, - serviceManager.GetPostService(), + serviceManager.PostService(), ), CommentRoutingKey: worker.NewCommentWorker( q.logger, - serviceManager.GetCommentService(), + serviceManager.CommentService(), ), }, } diff --git a/internal/service/service.go b/internal/service/service.go index 9c4c849..deff114 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -16,18 +16,18 @@ func NewServiceManager(userService *UserService, communityService *CommunityServ } } -func (sm *ServiceManager) GetCommunityService() *CommunityService { +func (sm *ServiceManager) CommunityService() *CommunityService { return sm.communityService } -func (sm *ServiceManager) GetPostService() *PostService { +func (sm *ServiceManager) PostService() *PostService { return sm.postService } -func (sm *ServiceManager) GetUserService() *UserService { +func (sm *ServiceManager) UserService() *UserService { return sm.userService } -func (sm *ServiceManager) GetCommentService() *CommentService { +func (sm *ServiceManager) CommentService() *CommentService { return sm.commentService }