From 75aa841ceb5cfc0b828849610c3bd21e1f140653 Mon Sep 17 00:00:00 2001 From: Nicolas Perriault Date: Thu, 27 Apr 2017 22:01:51 +0200 Subject: [PATCH] Fix #26: Load account timeline. (#77) --- src/Mastodon/ApiUrl.elm | 6 ++++++ src/Mastodon/Http.elm | 7 +++++++ src/Model.elm | 25 +++++++++++++++++++------ src/View.elm | 8 ++++---- src/ViewHelper.elm | 4 ++-- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/Mastodon/ApiUrl.elm b/src/Mastodon/ApiUrl.elm index 2143ed8..b495b22 100644 --- a/src/Mastodon/ApiUrl.elm +++ b/src/Mastodon/ApiUrl.elm @@ -4,6 +4,7 @@ module Mastodon.ApiUrl , oauthAuthorize , oauthToken , account + , accountTimeline , homeTimeline , publicTimeline , notifications @@ -65,6 +66,11 @@ publicTimeline local = "/api/v1/timelines/public" ++ isLocal +accountTimeline : Int -> String +accountTimeline id = + (account id) ++ "/statuses" + + notifications : String notifications = "/api/v1/notifications" diff --git a/src/Mastodon/Http.elm b/src/Mastodon/Http.elm index 243fedb..c56f76d 100644 --- a/src/Mastodon/Http.elm +++ b/src/Mastodon/Http.elm @@ -10,6 +10,7 @@ module Mastodon.Http , getAuthorizationUrl , getAccessToken , fetchAccount + , fetchAccountTimeline , fetchLocalTimeline , fetchNotifications , fetchGlobalTimeline @@ -21,6 +22,7 @@ module Mastodon.Http import Http import HttpBuilder import Json.Decode as Decode +import Task import Mastodon.ApiUrl as ApiUrl import Mastodon.Decoder exposing (..) import Mastodon.Encoder exposing (..) @@ -121,6 +123,11 @@ fetchGlobalTimeline client = fetch client (ApiUrl.publicTimeline (Nothing)) <| Decode.list statusDecoder +fetchAccountTimeline : Client -> Int -> Request (List Status) +fetchAccountTimeline client id = + fetch client (ApiUrl.accountTimeline id) <| Decode.list statusDecoder + + fetchNotifications : Client -> Request (List Notification) fetchNotifications client = fetch client (ApiUrl.notifications) <| Decode.list notificationDecoder diff --git a/src/Model.elm b/src/Model.elm index 8924437..135daca 100644 --- a/src/Model.elm +++ b/src/Model.elm @@ -47,7 +47,8 @@ type MastodonMsg | Reblogged (Result Mastodon.Model.Error Mastodon.Model.Status) | StatusPosted (Result Mastodon.Model.Error Mastodon.Model.Status) | Unreblogged (Result Mastodon.Model.Error Mastodon.Model.Status) - | UserAccount (Result Mastodon.Model.Error Mastodon.Model.Account) + | Account (Result Mastodon.Model.Error Mastodon.Model.Account) + | AccountTimeline (Result Mastodon.Model.Error (List Mastodon.Model.Status)) | UserTimeline (Result Mastodon.Model.Error (List Mastodon.Model.Status)) @@ -62,7 +63,7 @@ type Msg | ClearOpenedAccount | CloseThread | DraftEvent DraftMsg - | LoadUserAccount Int + | LoadAccount Int | MastodonEvent MastodonMsg | NoOp | OpenThread Mastodon.Model.Status @@ -114,6 +115,7 @@ type alias Model = , userTimeline : List Mastodon.Model.Status , localTimeline : List Mastodon.Model.Status , globalTimeline : List Mastodon.Model.Status + , accountTimeline : List Mastodon.Model.Status , notifications : List Mastodon.Model.NotificationAggregate , draft : Draft , errors : List String @@ -156,6 +158,7 @@ init flags location = , userTimeline = [] , localTimeline = [] , globalTimeline = [] + , accountTimeline = [] , notifications = [] , draft = defaultDraft , errors = [] @@ -502,7 +505,7 @@ processMastodonEvent msg model = Err error -> { model | errors = (errorText error) :: model.errors } ! [] - UserAccount result -> + Account result -> case result of Ok account -> { model | currentView = AccountView account } ! [] @@ -514,6 +517,14 @@ processMastodonEvent msg model = } ! [] + AccountTimeline result -> + case result of + Ok statuses -> + { model | accountTimeline = statuses } ! [] + + Err error -> + { model | errors = (errorText error) :: model.errors } ! [] + UserTimeline result -> case result of Ok userTimeline -> @@ -723,17 +734,19 @@ update msg model = Nothing -> [] - LoadUserAccount accountId -> + LoadAccount accountId -> {- @TODO When requesting a user profile, we should load a new "page" so that the URL in the browser matches the user displayed -} - model + { model | currentView = preferredTimeline model } ! case model.client of Just client -> [ Mastodon.Http.fetchAccount client accountId - |> Mastodon.Http.send (MastodonEvent << UserAccount) + |> Mastodon.Http.send (MastodonEvent << Account) + , Mastodon.Http.fetchAccountTimeline client accountId + |> Mastodon.Http.send (MastodonEvent << AccountTimeline) ] Nothing -> diff --git a/src/View.elm b/src/View.elm index c3488fd..820d789 100644 --- a/src/View.elm +++ b/src/View.elm @@ -70,7 +70,7 @@ accountLink : Mastodon.Model.Account -> Html Msg accountLink account = a [ href account.url - , ViewHelper.onClickWithPreventAndStop (LoadUserAccount account.id) + , ViewHelper.onClickWithPreventAndStop (LoadAccount account.id) ] [ text <| "@" ++ account.username ] @@ -79,7 +79,7 @@ accountAvatarLink : Mastodon.Model.Account -> Html Msg accountAvatarLink account = a [ href account.url - , ViewHelper.onClickWithPreventAndStop (LoadUserAccount account.id) + , ViewHelper.onClickWithPreventAndStop (LoadAccount account.id) , title <| "@" ++ account.username ] [ img [ class "avatar", src account.avatar ] [] ] @@ -174,7 +174,7 @@ statusView context ({ account, content, media_attachments, reblog, mentions } as -- When clicking on a status, we should not let the browser -- redirect to a new page. That's why we're preventing the default -- behavior here - , ViewHelper.onClickWithPreventAndStop (LoadUserAccount account.id) + , ViewHelper.onClickWithPreventAndStop (LoadAccount account.id) ] in case reblog of @@ -611,7 +611,7 @@ homepageView model = Model.AccountView account -> -- Todo: Load the user timeline - accountTimelineView account [] "Account" "user" + accountTimelineView account model.accountTimeline "Account" "user" Model.ThreadView thread -> threadView thread diff --git a/src/ViewHelper.elm b/src/ViewHelper.elm index e25a5c0..8cbc3d0 100644 --- a/src/ViewHelper.elm +++ b/src/ViewHelper.elm @@ -13,7 +13,7 @@ import HtmlParser import Json.Decode as Decode import String.Extra exposing (replace) import Mastodon.Model -import Model exposing (Msg(LoadUserAccount)) +import Model exposing (Msg(LoadAccount)) -- Custom Events @@ -58,7 +58,7 @@ createLinkNode attrs children mentions = Just mention -> Html.node "a" ((List.map toAttribute attrs) - ++ [ onClickWithPreventAndStop (LoadUserAccount mention.id) ] + ++ [ onClickWithPreventAndStop (LoadAccount mention.id) ] ) (toVirtualDom mentions children)