Add notifications

This commit is contained in:
Danila Fedorin 2018-12-13 13:42:23 -08:00
parent b25e5d77af
commit 46352c429a
4 changed files with 35 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import Scylla.Http exposing (..)
import Scylla.Views exposing (viewFull) import Scylla.Views exposing (viewFull)
import Scylla.Route exposing (Route(..)) import Scylla.Route exposing (Route(..))
import Scylla.UserData exposing (..) import Scylla.UserData exposing (..)
import Scylla.Notification exposing (..)
import Url exposing (Url) import Url exposing (Url)
import Url.Parser exposing (parse) import Url.Parser exposing (parse)
import Url.Builder import Url.Builder
@ -90,7 +91,7 @@ updateTryUrl m ur = case ur of
updateLoginResponse : Model -> Result Http.Error LoginResponse -> (Model, Cmd Msg) updateLoginResponse : Model -> Result Http.Error LoginResponse -> (Model, Cmd Msg)
updateLoginResponse model r = case r of updateLoginResponse model r = case r of
Ok lr -> ( { model | token = Just lr.accessToken } , Cmd.batch Ok lr -> ( { model | token = Just lr.accessToken, loginUsername = lr.userId } , Cmd.batch
[ firstSync model.apiUrl lr.accessToken [ firstSync model.apiUrl lr.accessToken
, Nav.pushUrl model.key <| Url.Builder.absolute [] [] , Nav.pushUrl model.key <| Url.Builder.absolute [] []
] ) ] )
@ -104,10 +105,27 @@ updateSyncResponse model r notify =
<| Result.map .nextBatch r <| Result.map .nextBatch r
syncCmd = sync nextBatch model.apiUrl token syncCmd = sync nextBatch model.apiUrl token
newUsers sr = List.filter (\s -> not <| Dict.member s model.userData) <| roomsUsers sr newUsers sr = List.filter (\s -> not <| Dict.member s model.userData) <| roomsUsers sr
newUserCommands sr = Cmd.batch <| List.map (userData model.apiUrl <| Maybe.withDefault "" model.token) <| newUsers sr newUserCommands sr = Cmd.batch
<| List.map (userData model.apiUrl
<| Maybe.withDefault "" model.token)
<| newUsers sr
notification sr = List.head
<| List.filter (\(s, e) -> e.sender /= model.loginUsername)
<| notificationEvents sr
notificationCommand sr = Maybe.withDefault Cmd.none
<| Maybe.map (\(s, e) -> sendNotificationPort
{ name = displayName model e.sender
, text = notificationText e
, room = s
})
<| notification sr
in in
case r of case r of
Ok sr -> ({ model | sync = mergeSyncResponse model.sync sr }, Cmd.batch [ syncCmd, newUserCommands sr ]) Ok sr -> ({ model | sync = mergeSyncResponse model.sync sr }, Cmd.batch
[ syncCmd
, newUserCommands sr
, if notify then notificationCommand sr else Cmd.none
])
_ -> (model, syncCmd) _ -> (model, syncCmd)
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg

View File

@ -1,6 +1,6 @@
module Scylla.Model exposing (..) module Scylla.Model exposing (..)
import Scylla.Api exposing (..) import Scylla.Api exposing (..)
import Scylla.Sync exposing (SyncResponse, JoinedRoom) import Scylla.Sync exposing (SyncResponse, JoinedRoom, senderName)
import Scylla.Login exposing (LoginResponse, Username, Password) import Scylla.Login exposing (LoginResponse, Username, Password)
import Scylla.UserData exposing (UserData) import Scylla.UserData exposing (UserData)
import Scylla.Route exposing (Route) import Scylla.Route exposing (Route)
@ -39,3 +39,5 @@ type Msg =
| ReceiveLoginResponse (Result Http.Error LoginResponse) -- HTTP, Login has finished | ReceiveLoginResponse (Result Http.Error LoginResponse) -- HTTP, Login has finished
| ReceiveUserData Username (Result Http.Error UserData) | ReceiveUserData Username (Result Http.Error UserData)
displayName : Model -> Username -> String
displayName m s = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData

View File

@ -370,13 +370,17 @@ roomName jr =
Maybe.andThen name <| Maybe.andThen nameEvent <| Maybe.andThen .events <| state Maybe.andThen name <| Maybe.andThen nameEvent <| Maybe.andThen .events <| state
-- Business Logic: Event Extraction -- Business Logic: Event Extraction
notificationEvent : SyncResponse -> Maybe (String, RoomEvent) notificationText : RoomEvent -> String
notificationEvent s = notificationText re = case (Decode.decodeValue (field "msgtype" string) re.content) of
Ok "m.text" -> Result.withDefault "" <| (Decode.decodeValue (field "body" string) re.content)
_ -> ""
notificationEvents : SyncResponse -> List (String, RoomEvent)
notificationEvents s =
let let
applyPair k = List.map (\v -> (k, v)) applyPair k = List.map (\v -> (k, v))
in in
List.head List.sortBy (\(k, v) -> v.originServerTs)
<| List.sortBy (\(k, v) -> v.originServerTs)
<| Dict.foldl (\k v a -> a ++ applyPair k v) [] <| Dict.foldl (\k v a -> a ++ applyPair k v) []
<| joinedRoomsEvents s <| joinedRoomsEvents s

View File

@ -3,6 +3,7 @@ import Scylla.Model exposing (..)
import Scylla.Sync exposing (..) import Scylla.Sync exposing (..)
import Scylla.Route exposing (..) import Scylla.Route exposing (..)
import Scylla.Fnv as Fnv import Scylla.Fnv as Fnv
import Scylla.Login exposing (Username)
import Svg import Svg
import Svg.Attributes import Svg.Attributes
import Url.Builder import Url.Builder
@ -126,12 +127,9 @@ eventView m re =
Maybe.map createRow Maybe.map createRow
<| Maybe.andThen (\f -> f m re) viewFunction <| Maybe.andThen (\f -> f m re) viewFunction
eventSenderView : Model -> String -> Html Msg eventSenderView : Model -> Username -> Html Msg
eventSenderView m s = eventSenderView m s =
let span [ style "background-color" <| stringColor s, class "sender-wrapper" ] [ text <| displayName m s ]
displayName = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData
in
span [ style "background-color" <| stringColor s, class "sender-wrapper" ] [ text displayName ]
messageView : Model -> RoomEvent -> Maybe (Html Msg) messageView : Model -> RoomEvent -> Maybe (Html Msg)
messageView m re = messageView m re =