2017-05-07 08:31:51 -04:00
|
|
|
module Update.Main exposing (update)
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import List.Extra exposing (removeAt)
|
2017-11-29 08:06:08 -05:00
|
|
|
import Mastodon.Helper exposing (extractStatusId)
|
2017-05-07 08:31:51 -04:00
|
|
|
import Mastodon.Model exposing (..)
|
2017-05-29 10:28:01 -04:00
|
|
|
import Navigation
|
2017-05-07 08:31:51 -04:00
|
|
|
import Types exposing (..)
|
2017-05-29 10:28:01 -04:00
|
|
|
import Update.AccountInfo
|
2017-05-07 08:31:51 -04:00
|
|
|
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
|
2017-05-29 10:28:01 -04:00
|
|
|
import Update.Route
|
2017-05-30 18:27:30 -04:00
|
|
|
import Update.Search
|
2017-05-07 08:31:51 -04:00
|
|
|
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 ! []
|
|
|
|
|
2017-05-29 10:28:01 -04:00
|
|
|
UrlChange location ->
|
2017-05-29 10:28:41 -04:00
|
|
|
Update.Route.update { model | location = location }
|
2017-05-29 10:28:01 -04:00
|
|
|
|
|
|
|
Back ->
|
|
|
|
model ! [ Navigation.back 1 ]
|
|
|
|
|
|
|
|
Navigate href ->
|
|
|
|
model ! [ Navigation.newUrl href ]
|
|
|
|
|
2017-05-07 08:31:51 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
! []
|
|
|
|
|
2017-07-17 09:34:42 -04:00
|
|
|
KeyMsg event code ->
|
|
|
|
case ( event, code, model.viewer ) of
|
|
|
|
( KeyDown, 27, Just _ ) ->
|
2017-07-13 11:34:00 -04:00
|
|
|
-- Esc
|
|
|
|
update (ViewerEvent CloseViewer) model
|
|
|
|
|
2017-07-17 09:34:42 -04:00
|
|
|
( KeyDown, 37, Just _ ) ->
|
2017-07-13 11:34:00 -04:00
|
|
|
-- Left arrow
|
|
|
|
update (ViewerEvent PrevAttachment) model
|
|
|
|
|
2017-07-17 09:34:42 -04:00
|
|
|
( KeyDown, 39, Just _ ) ->
|
2017-07-13 11:34:00 -04:00
|
|
|
-- Right arrow
|
|
|
|
update (ViewerEvent NextAttachment) model
|
|
|
|
|
2017-07-17 09:34:42 -04:00
|
|
|
( KeyDown, 17, _ ) ->
|
|
|
|
-- Ctrl key down
|
|
|
|
{ model | ctrlPressed = True } ! []
|
|
|
|
|
|
|
|
( KeyUp, 17, _ ) ->
|
|
|
|
-- Ctrl key up
|
|
|
|
{ model | ctrlPressed = False } ! []
|
|
|
|
|
2017-07-13 11:34:00 -04:00
|
|
|
_ ->
|
|
|
|
model ! []
|
|
|
|
|
2017-05-07 08:31:51 -04:00
|
|
|
ClearError index ->
|
|
|
|
{ model | errors = removeAt index model.errors } ! []
|
|
|
|
|
2017-05-12 16:41:11 -04: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 }
|
|
|
|
|
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
|
2017-05-13 05:54:35 -04:00
|
|
|
, homeTimeline = Update.Timeline.empty "home-timeline"
|
|
|
|
, localTimeline = Update.Timeline.empty "local-timeline"
|
|
|
|
, globalTimeline = Update.Timeline.empty "global-timeline"
|
2017-05-13 09:55:46 -04:00
|
|
|
, favoriteTimeline = Update.Timeline.empty "favorite-timeline"
|
2017-05-29 10:28:01 -04:00
|
|
|
, accountInfo = Update.AccountInfo.empty
|
2017-05-27 03:39:16 -04:00
|
|
|
, mutes = Update.Timeline.empty "mutes-timeline"
|
|
|
|
, blocks = Update.Timeline.empty "blocks-timeline"
|
2017-05-13 05:54:35 -04:00
|
|
|
, notifications = Update.Timeline.empty "notifications"
|
2017-05-29 10:28:01 -04:00
|
|
|
, currentView = AccountSelectorView
|
2017-05-09 12:43:12 -04:00
|
|
|
}
|
|
|
|
! [ Command.loadUserAccount <| Just client
|
|
|
|
, Command.loadTimelines <| Just client
|
|
|
|
]
|
|
|
|
|
2017-05-12 16:03:52 -04:00
|
|
|
LogoutClient client ->
|
|
|
|
let
|
|
|
|
newClients =
|
|
|
|
List.filter (\c -> c.token /= client.token) model.clients
|
|
|
|
|
|
|
|
newClient =
|
|
|
|
List.head newClients
|
|
|
|
in
|
|
|
|
{ model
|
|
|
|
| clients = newClients
|
2017-05-13 09:55:46 -04:00
|
|
|
, currentView = LocalTimelineView
|
2017-05-12 16:03:52 -04:00
|
|
|
}
|
|
|
|
! [ Command.saveClients newClients
|
|
|
|
, Command.loadUserAccount newClient
|
|
|
|
, Command.loadTimelines newClient
|
|
|
|
]
|
|
|
|
|
2017-05-07 08:31:51 -04:00
|
|
|
MastodonEvent msg ->
|
|
|
|
let
|
|
|
|
( newModel, commands ) =
|
|
|
|
Update.Mastodon.update msg model
|
|
|
|
in
|
|
|
|
newModel ! [ commands ]
|
|
|
|
|
2017-05-30 18:27:30 -04:00
|
|
|
SearchEvent msg ->
|
|
|
|
Update.Search.update msg model
|
|
|
|
|
2017-05-07 08:31:51 -04:00
|
|
|
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 10:28:41 -04:00
|
|
|
{ model | currentView = ThreadView (Thread Nothing Nothing) }
|
2017-11-29 08:06:08 -05:00
|
|
|
! [ Navigation.newUrl <| "#thread/" ++ extractStatusId status.id ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
2017-05-25 04:25:43 -04:00
|
|
|
FollowAccount account ->
|
|
|
|
model ! [ Command.follow (List.head model.clients) account ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
2017-05-25 04:25:43 -04:00
|
|
|
UnfollowAccount account ->
|
|
|
|
model ! [ Command.unfollow (List.head model.clients) account ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
2017-05-25 17:07:17 -04:00
|
|
|
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 ]
|
|
|
|
|
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
|
|
|
|
2017-05-13 09:55:46 -04:00
|
|
|
ReblogStatus status ->
|
|
|
|
Update.Timeline.processReblog status True model
|
|
|
|
! [ Command.reblogStatus (List.head model.clients) status.id ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
2017-05-13 09:55:46 -04:00
|
|
|
UnreblogStatus status ->
|
|
|
|
Update.Timeline.processReblog status False model
|
|
|
|
! [ Command.unreblogStatus (List.head model.clients) status.id ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
2017-05-13 09:55:46 -04:00
|
|
|
AddFavorite status ->
|
|
|
|
Update.Timeline.processFavourite status True model
|
|
|
|
! [ Command.favouriteStatus (List.head model.clients) status.id ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
2017-05-13 09:55:46 -04:00
|
|
|
RemoveFavorite status ->
|
|
|
|
Update.Timeline.processFavourite status False model
|
|
|
|
! [ Command.unfavouriteStatus (List.head model.clients) status.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-14 05:25:34 -04:00
|
|
|
model
|
|
|
|
! [ Command.postStatus (List.head model.clients) <|
|
|
|
|
toStatusRequestBody model.draft
|
|
|
|
]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
TimelineLoadNext id next ->
|
|
|
|
Update.Timeline.markAsLoading True id model
|
2017-05-29 10:28:01 -04:00
|
|
|
! [ Command.loadNextTimeline model id next ]
|
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 ]
|