From 5119952846b48ecdce30a7062576d0d67f388891 Mon Sep 17 00:00:00 2001 From: Xeronith Date: Sat, 1 Oct 2022 10:51:01 +0330 Subject: [PATCH] refactor(app): :art: improve structure --- greataped/app/models/repos/followers.go | 8 ++-- greataped/app/models/repos/inbox.go | 51 +++++++++++++++++++------ greataped/app/models/repos/outbox.go | 51 +++++++++++++++++++------ greataped/app/routes/inbox.go | 12 +++--- greataped/app/routes/message.go | 13 ++----- greataped/app/routes/outbox.go | 12 +++--- 6 files changed, 96 insertions(+), 51 deletions(-) diff --git a/greataped/app/models/repos/followers.go b/greataped/app/models/repos/followers.go index e1c7c56..cd7ff4e 100644 --- a/greataped/app/models/repos/followers.go +++ b/greataped/app/models/repos/followers.go @@ -50,12 +50,12 @@ func FindFollower(conds ...any) (*Follower, error) { // FindFollowers finds the user's followers func FindFollowers(userIden interface{}) ([]Follower, error) { - followers := &[]Follower{} - if err := db.Executor.Model(&Follower{}).Find(followers, "`target` = ?", userIden).Error; err != nil { - return *followers, err + result := &[]Follower{} + if err := db.Executor.Model(&Follower{}).Find(result, "`target` = ?", userIden).Error; err != nil { + return *result, err } - return *followers, nil + return *result, nil } // FindFollowerById searches the followers's table with the id given diff --git a/greataped/app/models/repos/inbox.go b/greataped/app/models/repos/inbox.go index 87f0eb3..1d56e7c 100644 --- a/greataped/app/models/repos/inbox.go +++ b/greataped/app/models/repos/inbox.go @@ -1,8 +1,11 @@ package repos import ( + "contracts" "db" + "errors" + "github.com/gofiber/fiber/v2" "gorm.io/gorm" ) @@ -17,27 +20,51 @@ type IncomingActivity struct { } // CreateIncomingActivity creates an activity entry in the incoming activities table -func CreateIncomingActivity(activity *IncomingActivity) *gorm.DB { - return db.Executor.Create(activity) -} +func CreateIncomingActivity(activity *IncomingActivity) error { + if err := db.Executor.Create(activity).Error; err != nil { + return err + } -// FindIncomingActivitiesForUser finds the activities posted to user -func FindIncomingActivitiesForUser(dest interface{}, userIden interface{}) *gorm.DB { - return db.Executor.Model(&IncomingActivity{}).Find(dest, "`to` = ?", userIden) + return nil } // FindIncomingActivity searches the incoming activities table with the condition given // and returns a single record. -func FindIncomingActivity(dest interface{}, conds ...interface{}) *gorm.DB { - return db.Executor.Model(&IncomingActivity{}).Take(dest, conds...) +func FindIncomingActivity(conds ...interface{}) (*IncomingActivity, error) { + dest := &IncomingActivity{} + if err := db.Executor.Model(&IncomingActivity{}).Take(dest, conds...).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, &fiber.Error{ + Code: contracts.StatusNotFound, + Message: "activity not found", + } + } else { + return nil, &fiber.Error{ + Code: contracts.StatusInternalServerError, + Message: err.Error(), + } + } + } + + return dest, nil +} + +// FindIncomingActivitiesForUser finds the activities posted to user +func FindIncomingActivitiesForUser(userIden interface{}) ([]IncomingActivity, error) { + result := &[]IncomingActivity{} + if err := db.Executor.Model(&IncomingActivity{}).Find(result, "`to` = ?", userIden).Error; err != nil { + return *result, err + } + + return *result, nil } // FindIncomingActivityById searches the incoming activities table with the id given -func FindIncomingActivityById(dest interface{}, id uint) *gorm.DB { - return FindIncomingActivity(dest, "id = ?", id) +func FindIncomingActivityById(id uint) (*IncomingActivity, error) { + return FindIncomingActivity("id = ?", id) } // FindIncomingActivityByGuid searches the incoming activities table with the guid given -func FindIncomingActivityByGuid(dest interface{}, guid string) *gorm.DB { - return FindIncomingActivity(dest, "guid = ?", guid) +func FindIncomingActivityByGuid(guid string) (*IncomingActivity, error) { + return FindIncomingActivity("guid = ?", guid) } diff --git a/greataped/app/models/repos/outbox.go b/greataped/app/models/repos/outbox.go index dddb207..982c8e0 100644 --- a/greataped/app/models/repos/outbox.go +++ b/greataped/app/models/repos/outbox.go @@ -1,8 +1,11 @@ package repos import ( + "contracts" "db" + "errors" + "github.com/gofiber/fiber/v2" "gorm.io/gorm" ) @@ -17,27 +20,51 @@ type OutgoingActivity struct { } // CreateOutgoingActivity creates an activity entry in the outgoing activities table -func CreateOutgoingActivity(activity *OutgoingActivity) *gorm.DB { - return db.Executor.Create(activity) -} +func CreateOutgoingActivity(activity *OutgoingActivity) error { + if err := db.Executor.Create(activity).Error; err != nil { + return err + } -// FindOutgoingActivitiesByUser finds the activities posted by user -func FindOutgoingActivitiesByUser(dest interface{}, userIden interface{}) *gorm.DB { - return db.Executor.Model(&OutgoingActivity{}).Find(dest, "`from` = ?", userIden) + return nil } // FindOutgoingActivity searches the outgoing activities table with the condition given // and returns a single record. -func FindOutgoingActivity(dest interface{}, conds ...interface{}) *gorm.DB { - return db.Executor.Model(&OutgoingActivity{}).Take(dest, conds...) +func FindOutgoingActivity(conds ...interface{}) (*OutgoingActivity, error) { + dest := &OutgoingActivity{} + if err := db.Executor.Model(&OutgoingActivity{}).Take(dest, conds...).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, &fiber.Error{ + Code: contracts.StatusNotFound, + Message: "activity not found", + } + } else { + return nil, &fiber.Error{ + Code: contracts.StatusInternalServerError, + Message: err.Error(), + } + } + } + + return dest, nil +} + +// FindOutgoingActivitiesByUser finds the activities posted by user +func FindOutgoingActivitiesByUser(userIden interface{}) ([]OutgoingActivity, error) { + result := &[]OutgoingActivity{} + if err := db.Executor.Model(&OutgoingActivity{}).Find(result, "`from` = ?", userIden).Error; err != nil { + return *result, err + } + + return *result, nil } // FindOutgoingActivityById searches the outgoing activities table with the id given -func FindOutgoingActivityById(dest interface{}, id uint) *gorm.DB { - return FindOutgoingActivity(dest, "id = ?", id) +func FindOutgoingActivityById(id uint) (*OutgoingActivity, error) { + return FindOutgoingActivity("id = ?", id) } // FindOutgoingActivityByGuid searches the outgoing activities table with the guid given -func FindOutgoingActivityByGuid(dest interface{}, guid string) *gorm.DB { - return FindOutgoingActivity(dest, "guid = ?", guid) +func FindOutgoingActivityByGuid(guid string) (*OutgoingActivity, error) { + return FindOutgoingActivity("guid = ?", guid) } diff --git a/greataped/app/routes/inbox.go b/greataped/app/routes/inbox.go index ace5ce9..48fa1fc 100644 --- a/greataped/app/routes/inbox.go +++ b/greataped/app/routes/inbox.go @@ -2,7 +2,6 @@ package routes import ( "activitypub" - "app/models/dto" "app/models/repos" "config" . "contracts" @@ -107,8 +106,8 @@ var InboxPost = route.New(HttpPost, "/u/:username/inbox", func(x IContext) error Content: note.Content, } - if err := repos.CreateIncomingActivity(message); err.Error != nil { - return x.Conflict(err.Error) + if err := repos.CreateIncomingActivity(message); err != nil { + return x.Conflict(err) } return x.Nothing() @@ -128,14 +127,13 @@ var InboxGet = route.New(HttpGet, "/u/:username/inbox", func(x IContext) error { actor := x.StringUtil().Format("%s://%s/u/%s", config.PROTOCOL, config.DOMAIN, username) id := x.StringUtil().Format("%s://%s/u/%s/inbox", config.PROTOCOL, config.DOMAIN, username) - messages := &[]dto.MessageResponse{} - err := repos.FindIncomingActivitiesForUser(messages, actor).Error + messages, err := repos.FindIncomingActivitiesForUser(actor) if err != nil { - x.InternalServerError("internal_server_error") + return err } items := []*activitypub.Activity{} - for _, message := range *messages { + for _, message := range messages { note := activitypub.NewPublicNote(message.From, message.Content) activity := note.Wrap(username) items = append(items, activity) diff --git a/greataped/app/routes/message.go b/greataped/app/routes/message.go index 2361cae..cb74a51 100644 --- a/greataped/app/routes/message.go +++ b/greataped/app/routes/message.go @@ -1,25 +1,20 @@ package routes import ( - "app/models/dto" "app/models/repos" . "contracts" - "errors" "server/route" - - "gorm.io/gorm" ) var Message = route.New(HttpGet, "/m/:guid", func(x IContext) error { guid := x.Request().Params("guid") if guid == "" { - return x.BadRequest("Bad request.") + return x.BadRequest("bad_request") } - response := &dto.MessageResponse{} - err := repos.FindOutgoingActivityByGuid(response, guid).Error - if errors.Is(err, gorm.ErrRecordNotFound) { - return x.NotFound("Message not found") + response, err := repos.FindOutgoingActivityByGuid(guid) + if err != nil { + return err } return x.String(response.Content) diff --git a/greataped/app/routes/outbox.go b/greataped/app/routes/outbox.go index 622c8df..88491b4 100644 --- a/greataped/app/routes/outbox.go +++ b/greataped/app/routes/outbox.go @@ -2,7 +2,6 @@ package routes import ( "activitypub" - "app/models/dto" "app/models/repos" "config" . "contracts" @@ -61,8 +60,8 @@ var OutboxPost = route.New(HttpPost, "/u/:username/outbox", func(x IContext) err Content: note.Content, } - if err := repos.CreateOutgoingActivity(message); err.Error != nil { - return x.Conflict(err.Error) + if err := repos.CreateOutgoingActivity(message); err != nil { + return x.Conflict(err) } return x.Nothing() @@ -77,14 +76,13 @@ var OutboxGet = route.New(HttpGet, "/u/:username/outbox", func(x IContext) error actor := x.StringUtil().Format("%s://%s/u/%s", config.PROTOCOL, config.DOMAIN, username) id := x.StringUtil().Format("%s://%s/u/%s/outbox", config.PROTOCOL, config.DOMAIN, username) - messages := &[]dto.MessageResponse{} - err := repos.FindOutgoingActivitiesByUser(messages, actor).Error + messages, err := repos.FindOutgoingActivitiesByUser(actor) if err != nil { - x.InternalServerError("internal_server_error") + return err } items := []*activitypub.Activity{} - for _, message := range *messages { + for _, message := range messages { note := activitypub.NewPublicNote(actor, message.Content) activity := note.Wrap(username) items = append(items, activity)