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

167 lines
6.2 KiB
Elm
Raw Normal View History

module Update.WebSocket exposing (update)
2017-06-02 12:11:50 +00:00
import Command
import Mastodon.Decoder
import Mastodon.Helper
import Mastodon.Model exposing (..)
import Mastodon.WebSocket
import Types exposing (..)
import Update.Error exposing (..)
import Update.Timeline
update : WebSocketMsg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NewWebsocketUserMessage message ->
case (Mastodon.Decoder.decodeWebSocketMessage message) of
Mastodon.WebSocket.ErrorEvent error ->
{ model | errors = addErrorNotification error model } ! []
Mastodon.WebSocket.StatusUpdateEvent result ->
case result of
Ok status ->
(model
|> (\m -> { m | homeTimeline = Update.Timeline.prepend status m.homeTimeline })
|> updateCurrentViewWithStatus status
)
! []
Err error ->
{ model | errors = addErrorNotification error model } ! []
Mastodon.WebSocket.StatusDeleteEvent id ->
Update.Timeline.deleteStatusFromAllTimelines id model ! []
Mastodon.WebSocket.NotificationEvent result ->
case result of
Ok notification ->
let
oldNotifications =
model.notifications
newNotifications =
{ oldNotifications
| entries =
Mastodon.Helper.addNotificationToAggregates
notification
oldNotifications.entries
}
in
2017-06-02 12:11:50 +00:00
{ model | notifications = newNotifications }
! [ Command.notifyNotification notification ]
Err error ->
{ model | errors = addErrorNotification error model } ! []
NewWebsocketLocalMessage message ->
case (Mastodon.Decoder.decodeWebSocketMessage message) of
Mastodon.WebSocket.ErrorEvent error ->
{ model | errors = addErrorNotification error model } ! []
Mastodon.WebSocket.StatusUpdateEvent result ->
case result of
Ok status ->
(model
|> (\m -> { m | localTimeline = Update.Timeline.prepend status m.localTimeline })
|> updateCurrentViewWithStatus status
)
! []
Err error ->
{ model | errors = addErrorNotification error model } ! []
Mastodon.WebSocket.StatusDeleteEvent id ->
Update.Timeline.deleteStatusFromAllTimelines id model ! []
_ ->
model ! []
NewWebsocketGlobalMessage message ->
case (Mastodon.Decoder.decodeWebSocketMessage message) of
Mastodon.WebSocket.ErrorEvent error ->
{ model | errors = addErrorNotification error model } ! []
Mastodon.WebSocket.StatusUpdateEvent result ->
case result of
Ok status ->
(model
|> (\m -> { m | globalTimeline = Update.Timeline.prepend status m.globalTimeline })
|> updateCurrentViewWithStatus status
)
! []
Err error ->
{ model | errors = addErrorNotification error model } ! []
Mastodon.WebSocket.StatusDeleteEvent id ->
Update.Timeline.deleteStatusFromAllTimelines id model ! []
_ ->
model ! []
isThreadMember : Thread -> Status -> Bool
isThreadMember thread status =
2017-05-29 14:28:01 +00:00
case ( thread.status, thread.context ) of
( Just threadStatus, Just context ) ->
2017-05-29 14:28:01 +00:00
case status.in_reply_to_id of
Nothing ->
False
Just inReplyToId ->
let
threadStatusIds =
List.concat
[ [ threadStatus.id ]
2017-05-29 14:28:01 +00:00
, List.map .id context.ancestors
, List.map .id context.descendants
]
in
List.member inReplyToId threadStatusIds
2017-05-29 14:28:01 +00:00
_ ->
False
appendToThreadDescendants : Thread -> Status -> Thread
appendToThreadDescendants ({ context } as thread) status =
2017-05-29 14:28:41 +00:00
case context of
2017-05-29 14:28:01 +00:00
Just context ->
{ thread
| context =
Just { context | descendants = List.append context.descendants [ status ] }
}
2017-05-29 14:28:01 +00:00
_ ->
thread
updateCurrentViewWithStatus : Status -> Model -> Model
2017-05-29 14:28:41 +00:00
updateCurrentViewWithStatus status ({ accountInfo } as model) =
case model.currentView of
ThreadView thread ->
if isThreadMember thread status then
{ model | currentView = ThreadView (appendToThreadDescendants thread status) }
else
model
2017-05-29 14:28:01 +00:00
AccountView _ ->
case model.accountInfo.account of
Just account ->
if Mastodon.Helper.sameAccount account status.account then
2017-05-29 14:28:41 +00:00
{ model
| accountInfo =
{ accountInfo
| timeline = Update.Timeline.prepend status accountInfo.timeline
}
}
2017-05-29 14:28:01 +00:00
else
model
Nothing ->
model
_ ->
model