2017-05-07 08:31:51 -04:00
|
|
|
module Update.Main exposing (update)
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import List.Extra exposing (removeAt)
|
|
|
|
import Mastodon.Model exposing (..)
|
|
|
|
import Types exposing (..)
|
|
|
|
import Update.Draft
|
2017-05-10 03:46:04 -04:00
|
|
|
import Update.Error
|
2017-05-07 08:31:51 -04:00
|
|
|
import Update.Mastodon
|
|
|
|
import Update.Timeline
|
|
|
|
import Update.Viewer
|
|
|
|
import Update.WebSocket
|
|
|
|
|
|
|
|
|
|
|
|
toStatusRequestBody : Draft -> StatusRequestBody
|
|
|
|
toStatusRequestBody draft =
|
|
|
|
{ status = draft.status
|
|
|
|
, in_reply_to_id =
|
|
|
|
case draft.inReplyTo of
|
|
|
|
Just status ->
|
|
|
|
Just status.id
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Nothing
|
|
|
|
, spoiler_text = draft.spoilerText
|
|
|
|
, sensitive = draft.sensitive
|
|
|
|
, visibility = draft.visibility
|
2017-05-11 04:55:15 -04:00
|
|
|
, media_ids = List.map .id draft.attachments
|
2017-05-07 08:31:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
|
|
|
update msg model =
|
|
|
|
case msg of
|
|
|
|
NoOp ->
|
|
|
|
model ! []
|
|
|
|
|
|
|
|
Tick newTime ->
|
|
|
|
{ model
|
|
|
|
| currentTime = newTime
|
2017-05-10 03:46:04 -04:00
|
|
|
, errors = Update.Error.cleanErrors newTime model.errors
|
2017-05-07 08:31:51 -04:00
|
|
|
}
|
|
|
|
! []
|
|
|
|
|
|
|
|
ClearError index ->
|
|
|
|
{ model | errors = removeAt index model.errors } ! []
|
|
|
|
|
2017-05-09 12:43:12 -04:00
|
|
|
SwitchClient client ->
|
|
|
|
let
|
|
|
|
newClients =
|
|
|
|
client :: (List.filter (\c -> c.token /= client.token) model.clients)
|
|
|
|
in
|
|
|
|
{ model
|
|
|
|
| clients = newClients
|
|
|
|
, currentView = Update.Timeline.preferred model
|
|
|
|
}
|
|
|
|
! [ Command.loadUserAccount <| Just client
|
|
|
|
, Command.loadTimelines <| Just client
|
|
|
|
]
|
|
|
|
|
2017-05-07 08:31:51 -04:00
|
|
|
MastodonEvent msg ->
|
|
|
|
let
|
|
|
|
( newModel, commands ) =
|
|
|
|
Update.Mastodon.update msg model
|
|
|
|
in
|
|
|
|
newModel ! [ commands ]
|
|
|
|
|
|
|
|
WebSocketEvent msg ->
|
|
|
|
let
|
|
|
|
( newModel, commands ) =
|
|
|
|
Update.WebSocket.update msg model
|
|
|
|
in
|
|
|
|
newModel ! [ commands ]
|
|
|
|
|
|
|
|
ServerChange server ->
|
|
|
|
{ model | server = server } ! []
|
|
|
|
|
|
|
|
UrlChange location ->
|
|
|
|
model ! []
|
|
|
|
|
|
|
|
Register ->
|
|
|
|
model ! [ Command.registerApp model ]
|
|
|
|
|
|
|
|
OpenThread status ->
|
2017-05-09 12:43:12 -04:00
|
|
|
model ! [ Command.loadThread (List.head model.clients) status ]
|
|
|
|
|
|
|
|
OpenAccountSelector ->
|
|
|
|
{ model | currentView = AccountSelectorView, server = "" } ! []
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
CloseThread ->
|
|
|
|
{ model | currentView = Update.Timeline.preferred model } ! []
|
|
|
|
|
|
|
|
FollowAccount id ->
|
2017-05-09 12:43:12 -04:00
|
|
|
model ! [ Command.follow (List.head model.clients) id ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
UnfollowAccount id ->
|
2017-05-09 12:43:12 -04:00
|
|
|
model ! [ Command.unfollow (List.head model.clients) id ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
DeleteStatus id ->
|
2017-05-09 12:43:12 -04:00
|
|
|
model ! [ Command.deleteStatus (List.head model.clients) id ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
ReblogStatus id ->
|
|
|
|
Update.Timeline.processReblog id True model
|
2017-05-09 12:43:12 -04:00
|
|
|
! [ Command.reblogStatus (List.head model.clients) id ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
UnreblogStatus id ->
|
|
|
|
Update.Timeline.processReblog id False model
|
2017-05-09 12:43:12 -04:00
|
|
|
! [ Command.unreblogStatus (List.head model.clients) id ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
AddFavorite id ->
|
|
|
|
Update.Timeline.processFavourite id True model
|
2017-05-09 12:43:12 -04:00
|
|
|
! [ Command.favouriteStatus (List.head model.clients) id ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
RemoveFavorite id ->
|
|
|
|
Update.Timeline.processFavourite id False model
|
2017-05-09 12:43:12 -04:00
|
|
|
! [ Command.unfavouriteStatus (List.head model.clients) id ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
DraftEvent draftMsg ->
|
|
|
|
case model.currentUser of
|
|
|
|
Just user ->
|
|
|
|
Update.Draft.update draftMsg user model
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
model ! []
|
|
|
|
|
|
|
|
ViewerEvent viewerMsg ->
|
|
|
|
let
|
|
|
|
( viewer, commands ) =
|
|
|
|
Update.Viewer.update viewerMsg model.viewer
|
|
|
|
in
|
|
|
|
{ model | viewer = viewer } ! [ commands ]
|
|
|
|
|
|
|
|
SubmitDraft ->
|
2017-05-09 12:43:12 -04:00
|
|
|
model ! [ Command.postStatus (List.head model.clients) <| toStatusRequestBody model.draft ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
LoadAccount accountId ->
|
|
|
|
{ model
|
|
|
|
| accountTimeline = Update.Timeline.empty "account-timeline"
|
2017-05-08 06:22:20 -04:00
|
|
|
, accountFollowers = Update.Timeline.empty "account-followers"
|
|
|
|
, accountFollowing = Update.Timeline.empty "account-following"
|
2017-05-07 08:31:51 -04:00
|
|
|
, accountRelationships = []
|
|
|
|
, accountRelationship = Nothing
|
|
|
|
}
|
2017-05-09 12:43:12 -04:00
|
|
|
! [ Command.loadAccount (List.head model.clients) accountId ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
TimelineLoadNext id next ->
|
|
|
|
Update.Timeline.markAsLoading True id model
|
2017-05-09 12:43:12 -04:00
|
|
|
! [ Command.loadNextTimeline (List.head model.clients) model.currentView id next ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
ViewAccountFollowers account ->
|
2017-05-08 06:22:20 -04:00
|
|
|
{ model
|
|
|
|
| currentView = AccountFollowersView account model.accountFollowers
|
|
|
|
, accountRelationships = []
|
|
|
|
}
|
2017-05-09 12:43:12 -04:00
|
|
|
! [ Command.loadAccountFollowers (List.head model.clients) account.id Nothing ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
ViewAccountFollowing account ->
|
2017-05-08 06:22:20 -04:00
|
|
|
{ model
|
|
|
|
| currentView = AccountFollowingView account model.accountFollowing
|
|
|
|
, accountRelationships = []
|
|
|
|
}
|
2017-05-09 12:43:12 -04:00
|
|
|
! [ Command.loadAccountFollowing (List.head model.clients) account.id Nothing ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
ViewAccountStatuses account ->
|
|
|
|
{ model | currentView = AccountView account } ! []
|
|
|
|
|
|
|
|
UseGlobalTimeline flag ->
|
|
|
|
let
|
|
|
|
newModel =
|
|
|
|
{ model | useGlobalTimeline = flag }
|
|
|
|
in
|
|
|
|
{ newModel | currentView = Update.Timeline.preferred newModel } ! []
|
|
|
|
|
|
|
|
CloseAccount ->
|
|
|
|
{ model
|
|
|
|
| currentView = Update.Timeline.preferred model
|
|
|
|
, accountTimeline = Update.Timeline.empty "account-timeline"
|
2017-05-08 06:22:20 -04:00
|
|
|
, accountFollowing = Update.Timeline.empty "account-following"
|
|
|
|
, accountFollowers = Update.Timeline.empty "account-followers"
|
2017-05-07 08:31:51 -04:00
|
|
|
}
|
|
|
|
! []
|
|
|
|
|
2017-05-09 12:43:12 -04:00
|
|
|
CloseAccountSelector ->
|
|
|
|
{ model | currentView = Update.Timeline.preferred model } ! []
|
|
|
|
|
2017-05-07 08:31:51 -04:00
|
|
|
FilterNotifications filter ->
|
|
|
|
{ model | notificationFilter = filter } ! []
|
|
|
|
|
|
|
|
ScrollColumn ScrollTop column ->
|
|
|
|
model ! [ Command.scrollColumnToTop column ]
|
|
|
|
|
|
|
|
ScrollColumn ScrollBottom column ->
|
|
|
|
model ! [ Command.scrollColumnToBottom column ]
|