From 70d6eba42712cbe29deb67d447b76c12cd5367b5 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sun, 23 Dec 2018 21:17:03 -0800 Subject: [PATCH] Add basic notification priority filtering. --- src/Main.elm | 2 +- src/Scylla/AccountData.elm | 10 +++++++++- src/Scylla/Notification.elm | 27 ++++++++++++++++++++++++++- src/Scylla/Sync.elm | 15 --------------- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/Main.elm b/src/Main.elm index f0ca7c3..6379900 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -284,7 +284,7 @@ updateSyncResponse model r notify = notification sr = findFirstBy (\(s, e) -> e.originServerTs) (\(s, e) -> e.sender /= model.loginUsername) - <| notificationEvents sr + <| joinedRoomNotificationEvents sr notificationCmd sr = if notify then Maybe.withDefault Cmd.none <| Maybe.map (\(s, e) -> sendNotificationPort diff --git a/src/Scylla/AccountData.elm b/src/Scylla/AccountData.elm index 25cb133..24dc667 100644 --- a/src/Scylla/AccountData.elm +++ b/src/Scylla/AccountData.elm @@ -1,6 +1,7 @@ module Scylla.AccountData exposing (..) -import Scylla.Sync exposing (AccountData, JoinedRoom, roomAccountData) +import Scylla.Sync exposing (SyncResponse, AccountData, JoinedRoom, roomAccountData) import Json.Decode as Decode +import Dict type NotificationSetting = Normal | MentionsOnly | None @@ -20,3 +21,10 @@ roomNotificationSetting jr = Maybe.withDefault Normal <| Maybe.andThen Result.toMaybe <| Maybe.map (Decode.decodeValue notificationSettingDecoder) <| roomAccountData jr "com.danilafe.scylla.notifications" + +roomIdNotificationSetting : SyncResponse -> String -> NotificationSetting +roomIdNotificationSetting sr s = Maybe.withDefault Normal + <| Maybe.map roomNotificationSetting + <| Maybe.andThen (Dict.get s) + <| Maybe.andThen .join sr.rooms + diff --git a/src/Scylla/Notification.elm b/src/Scylla/Notification.elm index 0b33aad..f837093 100644 --- a/src/Scylla/Notification.elm +++ b/src/Scylla/Notification.elm @@ -1,5 +1,8 @@ port module Scylla.Notification exposing (..) -import Json.Decode +import Scylla.Sync exposing (SyncResponse, RoomEvent, joinedRoomsEvents) +import Scylla.AccountData exposing (..) +import Json.Decode as Decode exposing (string, field) +import Dict type alias Notification = { name : String @@ -9,3 +12,25 @@ type alias Notification = port sendNotificationPort : Notification -> Cmd msg port onNotificationClickPort : (String -> msg) -> Sub msg + +producesNotification : NotificationSetting -> RoomEvent -> Bool +producesNotification ns re = case ns of + Normal -> True + _ -> False + + +notificationText : RoomEvent -> String +notificationText re = case (Decode.decodeValue (field "msgtype" string) re.content) of + Ok "m.text" -> Result.withDefault "" <| (Decode.decodeValue (field "body" string) re.content) + _ -> "" + +joinedRoomNotificationEvents : SyncResponse -> List (String, RoomEvent) +joinedRoomNotificationEvents s = + let + applyPair k = List.map (\v -> (k, v)) + in + List.sortBy (\(k, v) -> v.originServerTs) + <| Dict.foldl (\k v a -> a ++ applyPair k v) [] + <| Dict.map (\k v -> List.filter (producesNotification (roomIdNotificationSetting s k)) v) + <| joinedRoomsEvents s + diff --git a/src/Scylla/Sync.elm b/src/Scylla/Sync.elm index 063cf58..104d392 100644 --- a/src/Scylla/Sync.elm +++ b/src/Scylla/Sync.elm @@ -1,6 +1,5 @@ module Scylla.Sync exposing (..) import Scylla.Api exposing (..) -import Scylla.Notification exposing (..) import Scylla.Login exposing (Username) import Scylla.Route exposing (RoomId) import Dict exposing (Dict) @@ -467,20 +466,6 @@ roomName jr = Maybe.andThen (name << .content) nameEvent -- Business Logic: Event Extraction -notificationText : RoomEvent -> String -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 - applyPair k = List.map (\v -> (k, v)) - in - List.sortBy (\(k, v) -> v.originServerTs) - <| Dict.foldl (\k v a -> a ++ applyPair k v) [] - <| joinedRoomsEvents s - joinedRoomsEvents : SyncResponse -> Dict String (List RoomEvent) joinedRoomsEvents s = Maybe.withDefault Dict.empty