diff options
Diffstat (limited to 'internal/route')
-rw-r--r-- | internal/route/api/v1/user/follower.go | 14 | ||||
-rw-r--r-- | internal/route/lfs/mocks_test.go | 260 | ||||
-rw-r--r-- | internal/route/user/profile.go | 22 |
3 files changed, 285 insertions, 11 deletions
diff --git a/internal/route/api/v1/user/follower.go b/internal/route/api/v1/user/follower.go index c587547b..d21c6029 100644 --- a/internal/route/api/v1/user/follower.go +++ b/internal/route/api/v1/user/follower.go @@ -20,9 +20,9 @@ func responseApiUsers(c *context.APIContext, users []*db.User) { } func listUserFollowers(c *context.APIContext, u *db.User) { - users, err := u.GetFollowers(c.QueryInt("page")) + users, err := db.Users.ListFollowers(c.Req.Context(), u.ID, c.QueryInt("page"), db.ItemsPerPage) if err != nil { - c.Error(err, "get followers") + c.Error(err, "list followers") return } responseApiUsers(c, users) @@ -41,9 +41,9 @@ func ListFollowers(c *context.APIContext) { } func listUserFollowing(c *context.APIContext, u *db.User) { - users, err := u.GetFollowing(c.QueryInt("page")) + users, err := db.Users.ListFollowings(c.Req.Context(), u.ID, c.QueryInt("page"), db.ItemsPerPage) if err != nil { - c.Error(err, "get following") + c.Error(err, "list followings") return } responseApiUsers(c, users) @@ -62,7 +62,7 @@ func ListFollowing(c *context.APIContext) { } func checkUserFollowing(c *context.APIContext, u *db.User, followID int64) { - if u.IsFollowing(followID) { + if db.Follows.IsFollowing(c.Req.Context(), u.ID, followID) { c.NoContent() } else { c.NotFound() @@ -94,7 +94,7 @@ func Follow(c *context.APIContext) { if c.Written() { return } - if err := db.FollowUser(c.User.ID, target.ID); err != nil { + if err := db.Follows.Follow(c.Req.Context(), c.User.ID, target.ID); err != nil { c.Error(err, "follow user") return } @@ -106,7 +106,7 @@ func Unfollow(c *context.APIContext) { if c.Written() { return } - if err := db.UnfollowUser(c.User.ID, target.ID); err != nil { + if err := db.Follows.Unfollow(c.Req.Context(), c.User.ID, target.ID); err != nil { c.Error(err, "unfollow user") return } diff --git a/internal/route/lfs/mocks_test.go b/internal/route/lfs/mocks_test.go index 57bc9fbf..9cf1827b 100644 --- a/internal/route/lfs/mocks_test.go +++ b/internal/route/lfs/mocks_test.go @@ -2311,6 +2311,12 @@ type MockUsersStore struct { // HasForkedRepositoryFunc is an instance of a mock function object // controlling the behavior of the method HasForkedRepository. HasForkedRepositoryFunc *UsersStoreHasForkedRepositoryFunc + // ListFollowersFunc is an instance of a mock function object + // controlling the behavior of the method ListFollowers. + ListFollowersFunc *UsersStoreListFollowersFunc + // ListFollowingsFunc is an instance of a mock function object + // controlling the behavior of the method ListFollowings. + ListFollowingsFunc *UsersStoreListFollowingsFunc } // NewMockUsersStore creates a new mock of the UsersStore interface. All @@ -2347,6 +2353,16 @@ func NewMockUsersStore() *MockUsersStore { return }, }, + ListFollowersFunc: &UsersStoreListFollowersFunc{ + defaultHook: func(context.Context, int64, int, int) (r0 []*db.User, r1 error) { + return + }, + }, + ListFollowingsFunc: &UsersStoreListFollowingsFunc{ + defaultHook: func(context.Context, int64, int, int) (r0 []*db.User, r1 error) { + return + }, + }, } } @@ -2384,6 +2400,16 @@ func NewStrictMockUsersStore() *MockUsersStore { panic("unexpected invocation of MockUsersStore.HasForkedRepository") }, }, + ListFollowersFunc: &UsersStoreListFollowersFunc{ + defaultHook: func(context.Context, int64, int, int) ([]*db.User, error) { + panic("unexpected invocation of MockUsersStore.ListFollowers") + }, + }, + ListFollowingsFunc: &UsersStoreListFollowingsFunc{ + defaultHook: func(context.Context, int64, int, int) ([]*db.User, error) { + panic("unexpected invocation of MockUsersStore.ListFollowings") + }, + }, } } @@ -2409,6 +2435,12 @@ func NewMockUsersStoreFrom(i db.UsersStore) *MockUsersStore { HasForkedRepositoryFunc: &UsersStoreHasForkedRepositoryFunc{ defaultHook: i.HasForkedRepository, }, + ListFollowersFunc: &UsersStoreListFollowersFunc{ + defaultHook: i.ListFollowers, + }, + ListFollowingsFunc: &UsersStoreListFollowingsFunc{ + defaultHook: i.ListFollowings, + }, } } @@ -3072,3 +3104,231 @@ func (c UsersStoreHasForkedRepositoryFuncCall) Args() []interface{} { func (c UsersStoreHasForkedRepositoryFuncCall) Results() []interface{} { return []interface{}{c.Result0} } + +// UsersStoreListFollowersFunc describes the behavior when the ListFollowers +// method of the parent MockUsersStore instance is invoked. +type UsersStoreListFollowersFunc struct { + defaultHook func(context.Context, int64, int, int) ([]*db.User, error) + hooks []func(context.Context, int64, int, int) ([]*db.User, error) + history []UsersStoreListFollowersFuncCall + mutex sync.Mutex +} + +// ListFollowers delegates to the next hook function in the queue and stores +// the parameter and result values of this invocation. +func (m *MockUsersStore) ListFollowers(v0 context.Context, v1 int64, v2 int, v3 int) ([]*db.User, error) { + r0, r1 := m.ListFollowersFunc.nextHook()(v0, v1, v2, v3) + m.ListFollowersFunc.appendCall(UsersStoreListFollowersFuncCall{v0, v1, v2, v3, r0, r1}) + return r0, r1 +} + +// SetDefaultHook sets function that is called when the ListFollowers method +// of the parent MockUsersStore instance is invoked and the hook queue is +// empty. +func (f *UsersStoreListFollowersFunc) SetDefaultHook(hook func(context.Context, int64, int, int) ([]*db.User, error)) { + f.defaultHook = hook +} + +// PushHook adds a function to the end of hook queue. Each invocation of the +// ListFollowers method of the parent MockUsersStore instance invokes the +// hook at the front of the queue and discards it. After the queue is empty, +// the default hook function is invoked for any future action. +func (f *UsersStoreListFollowersFunc) PushHook(hook func(context.Context, int64, int, int) ([]*db.User, error)) { + f.mutex.Lock() + f.hooks = append(f.hooks, hook) + f.mutex.Unlock() +} + +// SetDefaultReturn calls SetDefaultHook with a function that returns the +// given values. +func (f *UsersStoreListFollowersFunc) SetDefaultReturn(r0 []*db.User, r1 error) { + f.SetDefaultHook(func(context.Context, int64, int, int) ([]*db.User, error) { + return r0, r1 + }) +} + +// PushReturn calls PushHook with a function that returns the given values. +func (f *UsersStoreListFollowersFunc) PushReturn(r0 []*db.User, r1 error) { + f.PushHook(func(context.Context, int64, int, int) ([]*db.User, error) { + return r0, r1 + }) +} + +func (f *UsersStoreListFollowersFunc) nextHook() func(context.Context, int64, int, int) ([]*db.User, error) { + f.mutex.Lock() + defer f.mutex.Unlock() + + if len(f.hooks) == 0 { + return f.defaultHook + } + + hook := f.hooks[0] + f.hooks = f.hooks[1:] + return hook +} + +func (f *UsersStoreListFollowersFunc) appendCall(r0 UsersStoreListFollowersFuncCall) { + f.mutex.Lock() + f.history = append(f.history, r0) + f.mutex.Unlock() +} + +// History returns a sequence of UsersStoreListFollowersFuncCall objects +// describing the invocations of this function. +func (f *UsersStoreListFollowersFunc) History() []UsersStoreListFollowersFuncCall { + f.mutex.Lock() + history := make([]UsersStoreListFollowersFuncCall, len(f.history)) + copy(history, f.history) + f.mutex.Unlock() + + return history +} + +// UsersStoreListFollowersFuncCall is an object that describes an invocation +// of method ListFollowers on an instance of MockUsersStore. +type UsersStoreListFollowersFuncCall struct { + // Arg0 is the value of the 1st argument passed to this method + // invocation. + Arg0 context.Context + // Arg1 is the value of the 2nd argument passed to this method + // invocation. + Arg1 int64 + // Arg2 is the value of the 3rd argument passed to this method + // invocation. + Arg2 int + // Arg3 is the value of the 4th argument passed to this method + // invocation. + Arg3 int + // Result0 is the value of the 1st result returned from this method + // invocation. + Result0 []*db.User + // Result1 is the value of the 2nd result returned from this method + // invocation. + Result1 error +} + +// Args returns an interface slice containing the arguments of this +// invocation. +func (c UsersStoreListFollowersFuncCall) Args() []interface{} { + return []interface{}{c.Arg0, c.Arg1, c.Arg2, c.Arg3} +} + +// Results returns an interface slice containing the results of this +// invocation. +func (c UsersStoreListFollowersFuncCall) Results() []interface{} { + return []interface{}{c.Result0, c.Result1} +} + +// UsersStoreListFollowingsFunc describes the behavior when the +// ListFollowings method of the parent MockUsersStore instance is invoked. +type UsersStoreListFollowingsFunc struct { + defaultHook func(context.Context, int64, int, int) ([]*db.User, error) + hooks []func(context.Context, int64, int, int) ([]*db.User, error) + history []UsersStoreListFollowingsFuncCall + mutex sync.Mutex +} + +// ListFollowings delegates to the next hook function in the queue and +// stores the parameter and result values of this invocation. +func (m *MockUsersStore) ListFollowings(v0 context.Context, v1 int64, v2 int, v3 int) ([]*db.User, error) { + r0, r1 := m.ListFollowingsFunc.nextHook()(v0, v1, v2, v3) + m.ListFollowingsFunc.appendCall(UsersStoreListFollowingsFuncCall{v0, v1, v2, v3, r0, r1}) + return r0, r1 +} + +// SetDefaultHook sets function that is called when the ListFollowings +// method of the parent MockUsersStore instance is invoked and the hook +// queue is empty. +func (f *UsersStoreListFollowingsFunc) SetDefaultHook(hook func(context.Context, int64, int, int) ([]*db.User, error)) { + f.defaultHook = hook +} + +// PushHook adds a function to the end of hook queue. Each invocation of the +// ListFollowings method of the parent MockUsersStore instance invokes the +// hook at the front of the queue and discards it. After the queue is empty, +// the default hook function is invoked for any future action. +func (f *UsersStoreListFollowingsFunc) PushHook(hook func(context.Context, int64, int, int) ([]*db.User, error)) { + f.mutex.Lock() + f.hooks = append(f.hooks, hook) + f.mutex.Unlock() +} + +// SetDefaultReturn calls SetDefaultHook with a function that returns the +// given values. +func (f *UsersStoreListFollowingsFunc) SetDefaultReturn(r0 []*db.User, r1 error) { + f.SetDefaultHook(func(context.Context, int64, int, int) ([]*db.User, error) { + return r0, r1 + }) +} + +// PushReturn calls PushHook with a function that returns the given values. +func (f *UsersStoreListFollowingsFunc) PushReturn(r0 []*db.User, r1 error) { + f.PushHook(func(context.Context, int64, int, int) ([]*db.User, error) { + return r0, r1 + }) +} + +func (f *UsersStoreListFollowingsFunc) nextHook() func(context.Context, int64, int, int) ([]*db.User, error) { + f.mutex.Lock() + defer f.mutex.Unlock() + + if len(f.hooks) == 0 { + return f.defaultHook + } + + hook := f.hooks[0] + f.hooks = f.hooks[1:] + return hook +} + +func (f *UsersStoreListFollowingsFunc) appendCall(r0 UsersStoreListFollowingsFuncCall) { + f.mutex.Lock() + f.history = append(f.history, r0) + f.mutex.Unlock() +} + +// History returns a sequence of UsersStoreListFollowingsFuncCall objects +// describing the invocations of this function. +func (f *UsersStoreListFollowingsFunc) History() []UsersStoreListFollowingsFuncCall { + f.mutex.Lock() + history := make([]UsersStoreListFollowingsFuncCall, len(f.history)) + copy(history, f.history) + f.mutex.Unlock() + + return history +} + +// UsersStoreListFollowingsFuncCall is an object that describes an +// invocation of method ListFollowings on an instance of MockUsersStore. +type UsersStoreListFollowingsFuncCall struct { + // Arg0 is the value of the 1st argument passed to this method + // invocation. + Arg0 context.Context + // Arg1 is the value of the 2nd argument passed to this method + // invocation. + Arg1 int64 + // Arg2 is the value of the 3rd argument passed to this method + // invocation. + Arg2 int + // Arg3 is the value of the 4th argument passed to this method + // invocation. + Arg3 int + // Result0 is the value of the 1st result returned from this method + // invocation. + Result0 []*db.User + // Result1 is the value of the 2nd result returned from this method + // invocation. + Result1 error +} + +// Args returns an interface slice containing the arguments of this +// invocation. +func (c UsersStoreListFollowingsFuncCall) Args() []interface{} { + return []interface{}{c.Arg0, c.Arg1, c.Arg2, c.Arg3} +} + +// Results returns an interface slice containing the results of this +// invocation. +func (c UsersStoreListFollowingsFuncCall) Results() []interface{} { + return []interface{}{c.Result0, c.Result1} +} diff --git a/internal/route/user/profile.go b/internal/route/user/profile.go index 783fb63c..1fb2df73 100644 --- a/internal/route/user/profile.go +++ b/internal/route/user/profile.go @@ -88,7 +88,14 @@ func Followers(c *context.Context, puser *context.ParamsUser) { c.PageIs("Followers") c.Data["CardsTitle"] = c.Tr("user.followers") c.Data["Owner"] = puser - repo.RenderUserCards(c, puser.NumFollowers, puser.GetFollowers, FOLLOWERS) + repo.RenderUserCards( + c, + puser.NumFollowers, + func(page int) ([]*db.User, error) { + return db.Users.ListFollowers(c.Req.Context(), puser.ID, page, db.ItemsPerPage) + }, + FOLLOWERS, + ) } func Following(c *context.Context, puser *context.ParamsUser) { @@ -96,7 +103,14 @@ func Following(c *context.Context, puser *context.ParamsUser) { c.PageIs("Following") c.Data["CardsTitle"] = c.Tr("user.following") c.Data["Owner"] = puser - repo.RenderUserCards(c, puser.NumFollowing, puser.GetFollowing, FOLLOWERS) + repo.RenderUserCards( + c, + puser.NumFollowing, + func(page int) ([]*db.User, error) { + return db.Users.ListFollowings(c.Req.Context(), puser.ID, page, db.ItemsPerPage) + }, + FOLLOWERS, + ) } func Stars(_ *context.Context) { @@ -106,9 +120,9 @@ func Action(c *context.Context, puser *context.ParamsUser) { var err error switch c.Params(":action") { case "follow": - err = db.FollowUser(c.UserID(), puser.ID) + err = db.Follows.Follow(c.Req.Context(), c.UserID(), puser.ID) case "unfollow": - err = db.UnfollowUser(c.UserID(), puser.ID) + err = db.Follows.Unfollow(c.Req.Context(), c.UserID(), puser.ID) } if err != nil { |