1
0
Fork 0
tooty/src/Update/Main.elm

201 lines
6.4 KiB
Elm
Raw Normal View History

module Update.Main exposing (update)
import Command
import List.Extra exposing (removeAt)
import Mastodon.Model exposing (..)
2017-05-29 14:28:01 +00:00
import Navigation
import Types exposing (..)
2017-05-29 14:28:01 +00:00
import Update.AccountInfo
import Update.Draft
2017-05-10 07:46:04 +00:00
import Update.Error
import Update.Mastodon
2017-05-29 14:28:01 +00:00
import Update.Route
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 08:55:15 +00:00
, media_ids = List.map .id draft.attachments
}
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
model ! []
2017-05-29 14:28:01 +00:00
UrlChange location ->
2017-05-29 14:28:41 +00:00
Update.Route.update { model | location = location }
2017-05-29 14:28:01 +00:00
Back ->
model ! [ Navigation.back 1 ]
Navigate href ->
model ! [ Navigation.newUrl href ]
Tick newTime ->
{ model
| currentTime = newTime
2017-05-10 07:46:04 +00:00
, errors = Update.Error.cleanErrors newTime model.errors
}
! []
ClearError index ->
{ model | errors = removeAt index model.errors } ! []
2017-05-12 20:41:11 +00:00
AskConfirm message onClick onCancel ->
{ model | confirm = Just <| Confirm message onClick onCancel } ! []
ConfirmCancelled onCancel ->
update onCancel { model | confirm = Nothing }
Confirmed onConfirm ->
update onConfirm { model | confirm = Nothing }
SwitchClient client ->
let
newClients =
client :: (List.filter (\c -> c.token /= client.token) model.clients)
in
{ model
| clients = newClients
, homeTimeline = Update.Timeline.empty "home-timeline"
, localTimeline = Update.Timeline.empty "local-timeline"
, globalTimeline = Update.Timeline.empty "global-timeline"
, favoriteTimeline = Update.Timeline.empty "favorite-timeline"
2017-05-29 14:28:01 +00:00
, accountInfo = Update.AccountInfo.empty
2017-05-27 07:39:16 +00:00
, mutes = Update.Timeline.empty "mutes-timeline"
, blocks = Update.Timeline.empty "blocks-timeline"
, notifications = Update.Timeline.empty "notifications"
2017-05-29 14:28:01 +00:00
, currentView = AccountSelectorView
}
! [ Command.loadUserAccount <| Just client
, Command.loadTimelines <| Just client
]
2017-05-12 20:03:52 +00:00
LogoutClient client ->
let
newClients =
List.filter (\c -> c.token /= client.token) model.clients
newClient =
List.head newClients
in
{ model
| clients = newClients
, currentView = LocalTimelineView
2017-05-12 20:03:52 +00:00
}
! [ Command.saveClients newClients
, Command.loadUserAccount newClient
, Command.loadTimelines newClient
]
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 } ! []
Register ->
model ! [ Command.registerApp model ]
OpenThread status ->
2017-05-29 14:28:41 +00:00
{ model | currentView = ThreadView (Thread Nothing Nothing) }
2017-05-29 14:28:01 +00:00
! [ Navigation.newUrl <| "#thread/" ++ (toString status.id) ]
FollowAccount account ->
model ! [ Command.follow (List.head model.clients) account ]
UnfollowAccount account ->
model ! [ Command.unfollow (List.head model.clients) account ]
Mute account ->
model ! [ Command.mute (List.head model.clients) account ]
Unmute account ->
model ! [ Command.unmute (List.head model.clients) account ]
Block account ->
model ! [ Command.block (List.head model.clients) account ]
Unblock account ->
model ! [ Command.unblock (List.head model.clients) account ]
DeleteStatus id ->
model ! [ Command.deleteStatus (List.head model.clients) id ]
ReblogStatus status ->
Update.Timeline.processReblog status True model
! [ Command.reblogStatus (List.head model.clients) status.id ]
UnreblogStatus status ->
Update.Timeline.processReblog status False model
! [ Command.unreblogStatus (List.head model.clients) status.id ]
AddFavorite status ->
Update.Timeline.processFavourite status True model
! [ Command.favouriteStatus (List.head model.clients) status.id ]
RemoveFavorite status ->
Update.Timeline.processFavourite status False model
! [ Command.unfavouriteStatus (List.head model.clients) status.id ]
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 ->
model
! [ Command.postStatus (List.head model.clients) <|
toStatusRequestBody model.draft
]
TimelineLoadNext id next ->
Update.Timeline.markAsLoading True id model
2017-05-29 14:28:01 +00:00
! [ Command.loadNextTimeline model id next ]
FilterNotifications filter ->
{ model | notificationFilter = filter } ! []
ScrollColumn ScrollTop column ->
model ! [ Command.scrollColumnToTop column ]
ScrollColumn ScrollBottom column ->
model ! [ Command.scrollColumnToBottom column ]