From 75de9b8c08d83ca3e21b603083f2dfb18f9ffa3a Mon Sep 17 00:00:00 2001 From: Nicolas Perriault Date: Mon, 8 May 2017 12:22:20 +0200 Subject: [PATCH] Fix #145: Paginate account followers and following lists. (#149) --- src/Command.elm | 32 ++++++++++++++++++++++++-------- src/Init.elm | 4 ++-- src/Types.elm | 12 ++++++------ src/Update/Main.elm | 22 ++++++++++++++-------- src/Update/Mastodon.elm | 25 ++++++++++++++----------- src/View/Account.elm | 9 ++++++--- 6 files changed, 66 insertions(+), 38 deletions(-) diff --git a/src/Command.elm b/src/Command.elm index 0c43b45..269b513 100644 --- a/src/Command.elm +++ b/src/Command.elm @@ -165,27 +165,27 @@ loadAccount client accountId = Cmd.none -loadAccountFollowers : Maybe Client -> Int -> Cmd Msg -loadAccountFollowers client accountId = +loadAccountFollowers : Maybe Client -> Int -> Maybe String -> Cmd Msg +loadAccountFollowers client accountId url = case client of Just client -> - HttpBuilder.get (ApiUrl.followers accountId) + HttpBuilder.get (Maybe.withDefault (ApiUrl.followers accountId) url) |> withClient client |> withBodyDecoder (Decode.list accountDecoder) - |> send (MastodonEvent << AccountFollowers) + |> send (MastodonEvent << AccountFollowers (url /= Nothing)) Nothing -> Cmd.none -loadAccountFollowing : Maybe Client -> Int -> Cmd Msg -loadAccountFollowing client accountId = +loadAccountFollowing : Maybe Client -> Int -> Maybe String -> Cmd Msg +loadAccountFollowing client accountId url = case client of Just client -> - HttpBuilder.get (ApiUrl.following accountId) + HttpBuilder.get (Maybe.withDefault (ApiUrl.following accountId) url) |> withClient client |> withBodyDecoder (Decode.list accountDecoder) - |> send (MastodonEvent << AccountFollowing) + |> send (MastodonEvent << AccountFollowing (url /= Nothing)) Nothing -> Cmd.none @@ -345,6 +345,22 @@ loadNextTimeline client currentView id next = _ -> Cmd.none + "account-followers" -> + case currentView of + AccountFollowersView account timeline -> + loadAccountFollowers client account.id (Just next) + + _ -> + Cmd.none + + "account-following" -> + case currentView of + AccountFollowingView account timeline -> + loadAccountFollowing client account.id (Just next) + + _ -> + Cmd.none + _ -> Cmd.none diff --git a/src/Init.elm b/src/Init.elm index 905eec7..39a7a9e 100644 --- a/src/Init.elm +++ b/src/Init.elm @@ -18,8 +18,8 @@ init { registration, client } location = , localTimeline = Update.Timeline.empty "local-timeline" , globalTimeline = Update.Timeline.empty "global-timeline" , accountTimeline = Update.Timeline.empty "account-timeline" - , accountFollowers = [] - , accountFollowing = [] + , accountFollowers = Update.Timeline.empty "account-followers" + , accountFollowing = Update.Timeline.empty "account-following" , accountRelationships = [] , accountRelationship = Nothing , notifications = Update.Timeline.empty "notifications" diff --git a/src/Types.elm b/src/Types.elm index c300f80..591e18e 100644 --- a/src/Types.elm +++ b/src/Types.elm @@ -39,8 +39,8 @@ type alias MastodonResult a = type MastodonMsg = AccessToken (MastodonResult AccessTokenResult) | AccountFollowed (MastodonResult Relationship) - | AccountFollowers (MastodonResult (List Account)) - | AccountFollowing (MastodonResult (List Account)) + | AccountFollowers Bool (MastodonResult (List Account)) + | AccountFollowing Bool (MastodonResult (List Account)) | AccountReceived (MastodonResult Account) | AccountRelationship (MastodonResult (List Relationship)) | AccountRelationships (MastodonResult (List Relationship)) @@ -102,8 +102,8 @@ type Msg type CurrentView = -- Basically, what we should be displaying in the fourth column - AccountFollowersView Account (List Account) - | AccountFollowingView Account (List Account) + AccountFollowersView Account (Timeline Account) + | AccountFollowingView Account (Timeline Account) | AccountView Account | GlobalTimelineView | LocalTimelineView @@ -177,8 +177,8 @@ type alias Model = , localTimeline : Timeline Status , globalTimeline : Timeline Status , accountTimeline : Timeline Status - , accountFollowers : List Account - , accountFollowing : List Account + , accountFollowers : Timeline Account + , accountFollowing : Timeline Account , accountRelationships : List Relationship , accountRelationship : Maybe Relationship , notifications : Timeline NotificationAggregate diff --git a/src/Update/Main.elm b/src/Update/Main.elm index 6713e49..ef07755 100644 --- a/src/Update/Main.elm +++ b/src/Update/Main.elm @@ -118,8 +118,8 @@ update msg model = LoadAccount accountId -> { model | accountTimeline = Update.Timeline.empty "account-timeline" - , accountFollowers = [] - , accountFollowing = [] + , accountFollowers = Update.Timeline.empty "account-followers" + , accountFollowing = Update.Timeline.empty "account-following" , accountRelationships = [] , accountRelationship = Nothing } @@ -130,12 +130,18 @@ update msg model = ! [ Command.loadNextTimeline model.client model.currentView id next ] ViewAccountFollowers account -> - { model | currentView = AccountFollowersView account model.accountFollowers } - ! [ Command.loadAccountFollowers model.client account.id ] + { model + | currentView = AccountFollowersView account model.accountFollowers + , accountRelationships = [] + } + ! [ Command.loadAccountFollowers model.client account.id Nothing ] ViewAccountFollowing account -> - { model | currentView = AccountFollowingView account model.accountFollowing } - ! [ Command.loadAccountFollowing model.client account.id ] + { model + | currentView = AccountFollowingView account model.accountFollowing + , accountRelationships = [] + } + ! [ Command.loadAccountFollowing model.client account.id Nothing ] ViewAccountStatuses account -> { model | currentView = AccountView account } ! [] @@ -151,8 +157,8 @@ update msg model = { model | currentView = Update.Timeline.preferred model , accountTimeline = Update.Timeline.empty "account-timeline" - , accountFollowing = [] - , accountFollowers = [] + , accountFollowing = Update.Timeline.empty "account-following" + , accountFollowers = Update.Timeline.empty "account-followers" } ! [] diff --git a/src/Update/Mastodon.elm b/src/Update/Mastodon.elm index fd5c498..7de1283 100644 --- a/src/Update/Mastodon.elm +++ b/src/Update/Mastodon.elm @@ -178,7 +178,10 @@ update msg model = AccountReceived result -> case result of Ok { decoded } -> - { model | currentView = AccountView decoded } + { model + | currentView = AccountView decoded + , accountRelationships = [] + } ! [ Command.loadAccountTimeline model.client decoded.id model.accountTimeline.links.next ] Err error -> @@ -196,21 +199,19 @@ update msg model = Err error -> { model | errors = addErrorNotification (errorText error) model } ! [] - AccountFollowers result -> + AccountFollowers append result -> case result of - Ok { decoded } -> - -- TODO: store next link - { model | accountFollowers = decoded } + Ok { decoded, links } -> + { model | accountFollowers = Update.Timeline.update append decoded links model.accountFollowers } ! [ Command.loadRelationships model.client <| List.map .id decoded ] Err error -> { model | errors = addErrorNotification (errorText error) model } ! [] - AccountFollowing result -> + AccountFollowing append result -> case result of - Ok { decoded } -> - -- TODO: store next link - { model | accountFollowing = decoded } + Ok { decoded, links } -> + { model | accountFollowing = Update.Timeline.update append decoded links model.accountFollowing } ! [ Command.loadRelationships model.client <| List.map .id decoded ] Err error -> @@ -232,8 +233,10 @@ update msg model = AccountRelationships result -> case result of Ok { decoded } -> - -- TODO: store next link - { model | accountRelationships = decoded } ! [] + { model + | accountRelationships = List.concat [ model.accountRelationships, decoded ] + } + ! [] Err error -> { model | errors = addErrorNotification (errorText error) model } ! [] diff --git a/src/View/Account.elm b/src/View/Account.elm index 9a8ff23..ec1f1df 100644 --- a/src/View/Account.elm +++ b/src/View/Account.elm @@ -100,12 +100,12 @@ followView currentUser relationship account = accountFollowView : CurrentUser - -> List Account + -> Timeline Account -> List Relationship -> CurrentUserRelation -> Account -> Html Msg -accountFollowView currentUser accounts relationships relationship account = +accountFollowView currentUser timeline relationships relationship account = let keyedEntry account = ( toString account.id @@ -116,10 +116,13 @@ accountFollowView currentUser accounts relationships relationship account = account ] ) + + entries = + List.map keyedEntry timeline.entries in accountView currentUser account relationship <| Keyed.ul [ class "list-group" ] <| - List.map keyedEntry accounts + (entries ++ [ ( "load-more", Common.loadMoreBtn timeline ) ]) accountTimelineView : CurrentUser -> Timeline Status -> CurrentUserRelation -> Account -> Html Msg