From 71e0b3f64e327461b6415805cfef40b1aebf83b9 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 11 Sep 2019 01:11:08 -0700 Subject: [PATCH] Perform further cleanup to Sync --- src/Scylla/Model.elm | 2 +- src/Scylla/Room.elm | 11 ++++++ src/Scylla/Sync.elm | 79 -------------------------------------------- src/Scylla/Views.elm | 4 +-- 4 files changed, 14 insertions(+), 82 deletions(-) diff --git a/src/Scylla/Model.elm b/src/Scylla/Model.elm index e347b1b..05b14a6 100644 --- a/src/Scylla/Model.elm +++ b/src/Scylla/Model.elm @@ -1,6 +1,6 @@ module Scylla.Model exposing (..) import Scylla.Api exposing (..) -import Scylla.Sync exposing (SyncResponse, HistoryResponse, roomJoinedUsers) +import Scylla.Sync exposing (SyncResponse, HistoryResponse) import Scylla.ListUtils exposing (findFirst) import Scylla.Room exposing (OpenRooms) import Scylla.Sync.Rooms exposing (JoinedRoom) diff --git a/src/Scylla/Room.elm b/src/Scylla/Room.elm index e818809..bc46196 100644 --- a/src/Scylla/Room.elm +++ b/src/Scylla/Room.elm @@ -166,3 +166,14 @@ getTotalNotificationCountString or = case n of 0 -> Nothing _ -> Just <| "(" ++ String.fromInt n ++ suffix ++ ")" + +getHomeserver : String -> String +getHomeserver s = + let + colonIndex = Maybe.withDefault 0 + <| Maybe.map ((+) 1) + <| List.head + <| String.indexes ":" s + in + String.dropLeft colonIndex s + diff --git a/src/Scylla/Sync.elm b/src/Scylla/Sync.elm index cc3227b..1b9f021 100644 --- a/src/Scylla/Sync.elm +++ b/src/Scylla/Sync.elm @@ -58,27 +58,7 @@ findFirstEvent = findFirstBy .originServerTs findLastEvent : ({ a | originServerTs : Int } -> Bool) -> List { a | originServerTs : Int } -> Maybe { a | originServerTs : Int } findLastEvent = findLastBy .originServerTs --- Business Logic: Names -homeserver : String -> String -homeserver s = - let - colonIndex = Maybe.withDefault 0 - <| Maybe.map ((+) 1) - <| List.head - <| String.indexes ":" s - in - String.dropLeft colonIndex s - -- Business Logic: Events -allRoomStateEvents : JoinedRoom -> List StateEvent -allRoomStateEvents jr = - let - stateEvents = Maybe.withDefault [] <| Maybe.andThen .events jr.state - timelineEvents = Maybe.withDefault [] <| Maybe.andThen .events jr.timeline - allStateEvents = uniqueBy .eventId (stateEvents ++ (List.filterMap toStateEvent timelineEvents)) - in - allStateEvents - allRoomDictTimelineEvents : Dict String { a | timeline : Maybe Timeline } -> List RoomEvent allRoomDictTimelineEvents dict = List.concatMap (Maybe.withDefault [] << .events) <| List.filterMap .timeline @@ -104,65 +84,6 @@ joinedRoomsTimelineEvents s = <| Maybe.map (Dict.map (\k v -> Maybe.withDefault [] <| Maybe.andThen .events v.timeline)) <| Maybe.andThen .join s.rooms -totalNotificationCountString : SyncResponse -> Maybe String -totalNotificationCountString sr = - let - (h, n) = totalNotificationCounts sr - suffix = case h of - 0 -> "" - _ -> "!" - in - case n of - 0 -> Nothing - _ -> Just <| "(" ++ String.fromInt n ++ suffix ++ ")" - -totalNotificationCounts : SyncResponse -> (Int, Int) -totalNotificationCounts sr = - let - rooms = Maybe.withDefault [] - <| Maybe.map (Dict.values) - <| Maybe.andThen (.join) sr.rooms - zeroDefault = Maybe.withDefault 0 - getCounts = Maybe.map (\cs -> (zeroDefault cs.highlightCount, zeroDefault cs.notificationCount)) - << .unreadNotifications - sumCounts (h1, n1) (h2, n2) = (h1 + h2, n1 + n2) - in - List.foldl sumCounts (0, 0) - <| List.filterMap getCounts rooms - --- Business Logic: Room Info -roomAccountData : JoinedRoom -> String -> Maybe Decode.Value -roomAccountData jr et = - Maybe.map .content - <| Maybe.andThen (List.head << List.filter (((==) et) << .type_)) - <| Maybe.andThen .events jr.accountData - -roomName : JoinedRoom -> Maybe String -roomName jr = - let - name c = Result.toMaybe <| Decode.decodeValue (field "name" string) c - nameEvent = findLastEvent (((==) "m.room.name") << .type_) <| allRoomStateEvents jr - in - Maybe.andThen (name << .content) nameEvent - -roomTypingUsers : JoinedRoom -> List Username -roomTypingUsers jr = Maybe.withDefault [] - <| Maybe.andThen (Result.toMaybe << Decode.decodeValue (Decode.field "user_ids" (list string))) - <| Maybe.map .content - <| Maybe.andThen (findLast (((==) "m.typing") << .type_)) - <| Maybe.andThen .events jr.ephemeral - -- Business Logic: Users allUsers : SyncResponse -> List Username allUsers s = uniqueBy (\u -> u) <| List.map getSender <| allTimelineEvents s - -roomJoinedUsers : JoinedRoom -> List Username -roomJoinedUsers r = - let - contentDecoder = Decode.field "membership" Decode.string - isJoin e = Ok "join" == (Decode.decodeValue contentDecoder e.content) - in - List.map .sender - <| List.filter isJoin - <| List.filter (((==) "m.room.member") << .type_) - <| allRoomStateEvents r diff --git a/src/Scylla/Views.elm b/src/Scylla/Views.elm index 0411fe4..1b58e1e 100644 --- a/src/Scylla/Views.elm +++ b/src/Scylla/Views.elm @@ -3,7 +3,7 @@ import Scylla.Model exposing (..) import Scylla.Sync exposing (..) import Scylla.Sync.Events exposing (..) import Scylla.Sync.Rooms exposing (..) -import Scylla.Room exposing (RoomData, emptyOpenRooms, getRoomName, getRoomTypingUsers) +import Scylla.Room exposing (RoomData, emptyOpenRooms, getHomeserver, getRoomName, getRoomTypingUsers) import Scylla.Route exposing (..) import Scylla.Fnv as Fnv import Scylla.Messages exposing (..) @@ -104,7 +104,7 @@ roomListView m = ] roomGroups : List (String, RoomData) -> Dict String (List (String, RoomData)) -roomGroups jrs = groupBy (homeserver << Tuple.first) jrs +roomGroups jrs = groupBy (getHomeserver << Tuple.first) jrs homeserverView : Model -> String -> List (String, RoomData) -> Html Msg homeserverView m hs rs =