2017-05-07 08:31:51 -04:00
|
|
|
module Update.WebSocket exposing (update)
|
|
|
|
|
2017-06-02 08:11:50 -04:00
|
|
|
import Command
|
2017-05-07 08:31:51 -04:00
|
|
|
import Mastodon.Decoder
|
|
|
|
import Mastodon.Helper
|
2017-05-08 08:13:07 -04:00
|
|
|
import Mastodon.Model exposing (..)
|
2017-05-07 08:31:51 -04:00
|
|
|
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 ->
|
2017-05-08 08:13:07 -04:00
|
|
|
(model
|
|
|
|
|> (\m -> { m | homeTimeline = Update.Timeline.prepend status m.homeTimeline })
|
|
|
|
|> updateCurrentViewWithStatus status
|
|
|
|
)
|
|
|
|
! []
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
Err error ->
|
|
|
|
{ model | errors = addErrorNotification error model } ! []
|
|
|
|
|
2017-11-29 08:06:08 -05:00
|
|
|
Mastodon.WebSocket.StatusDeleteEvent id ->
|
|
|
|
Update.Timeline.deleteStatusFromAllTimelines id model ! []
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
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 08:11:50 -04:00
|
|
|
{ model | notifications = newNotifications }
|
|
|
|
! [ Command.notifyNotification notification ]
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
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 ->
|
2017-05-08 08:13:07 -04:00
|
|
|
(model
|
|
|
|
|> (\m -> { m | localTimeline = Update.Timeline.prepend status m.localTimeline })
|
|
|
|
|> updateCurrentViewWithStatus status
|
|
|
|
)
|
|
|
|
! []
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
Err error ->
|
|
|
|
{ model | errors = addErrorNotification error model } ! []
|
|
|
|
|
2017-11-29 08:06:08 -05:00
|
|
|
Mastodon.WebSocket.StatusDeleteEvent id ->
|
|
|
|
Update.Timeline.deleteStatusFromAllTimelines id model ! []
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
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 ->
|
2017-05-08 08:13:07 -04:00
|
|
|
(model
|
|
|
|
|> (\m -> { m | globalTimeline = Update.Timeline.prepend status m.globalTimeline })
|
|
|
|
|> updateCurrentViewWithStatus status
|
|
|
|
)
|
|
|
|
! []
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
Err error ->
|
|
|
|
{ model | errors = addErrorNotification error model } ! []
|
|
|
|
|
2017-11-29 08:06:08 -05:00
|
|
|
Mastodon.WebSocket.StatusDeleteEvent id ->
|
|
|
|
Update.Timeline.deleteStatusFromAllTimelines id model ! []
|
2017-05-07 08:31:51 -04:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
model ! []
|
2017-05-08 08:13:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
isThreadMember : Thread -> Status -> Bool
|
|
|
|
isThreadMember thread status =
|
2017-05-29 10:28:01 -04:00
|
|
|
case ( thread.status, thread.context ) of
|
2017-05-29 17:27:04 -04:00
|
|
|
( Just threadStatus, Just context ) ->
|
2017-05-29 10:28:01 -04:00
|
|
|
case status.in_reply_to_id of
|
|
|
|
Nothing ->
|
|
|
|
False
|
|
|
|
|
|
|
|
Just inReplyToId ->
|
|
|
|
let
|
|
|
|
threadStatusIds =
|
|
|
|
List.concat
|
2017-05-29 17:27:04 -04:00
|
|
|
[ [ threadStatus.id ]
|
2017-05-29 10:28:01 -04:00
|
|
|
, List.map .id context.ancestors
|
|
|
|
, List.map .id context.descendants
|
|
|
|
]
|
|
|
|
in
|
|
|
|
List.member inReplyToId threadStatusIds
|
2017-05-08 08:13:07 -04:00
|
|
|
|
2017-05-29 10:28:01 -04:00
|
|
|
_ ->
|
|
|
|
False
|
2017-05-08 08:13:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
appendToThreadDescendants : Thread -> Status -> Thread
|
|
|
|
appendToThreadDescendants ({ context } as thread) status =
|
2017-05-29 10:28:41 -04:00
|
|
|
case context of
|
2017-05-29 10:28:01 -04:00
|
|
|
Just context ->
|
|
|
|
{ thread
|
|
|
|
| context =
|
|
|
|
Just { context | descendants = List.append context.descendants [ status ] }
|
2017-05-08 08:13:07 -04:00
|
|
|
}
|
2017-05-29 10:28:01 -04:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
thread
|
2017-05-08 08:13:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
updateCurrentViewWithStatus : Status -> Model -> Model
|
2017-05-29 10:28:41 -04:00
|
|
|
updateCurrentViewWithStatus status ({ accountInfo } as model) =
|
2017-05-08 08:13:07 -04:00
|
|
|
case model.currentView of
|
2017-05-29 17:27:04 -04:00
|
|
|
ThreadView thread ->
|
2017-05-08 08:13:07 -04:00
|
|
|
if isThreadMember thread status then
|
|
|
|
{ model | currentView = ThreadView (appendToThreadDescendants thread status) }
|
|
|
|
else
|
|
|
|
model
|
|
|
|
|
2017-05-29 10:28:01 -04:00
|
|
|
AccountView _ ->
|
|
|
|
case model.accountInfo.account of
|
|
|
|
Just account ->
|
|
|
|
if Mastodon.Helper.sameAccount account status.account then
|
2017-05-29 10:28:41 -04:00
|
|
|
{ model
|
|
|
|
| accountInfo =
|
|
|
|
{ accountInfo
|
|
|
|
| timeline = Update.Timeline.prepend status accountInfo.timeline
|
|
|
|
}
|
|
|
|
}
|
2017-05-29 10:28:01 -04:00
|
|
|
else
|
|
|
|
model
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
model
|
2017-05-08 08:13:07 -04:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
model
|