-
Notifications
You must be signed in to change notification settings - Fork 9
Private pusher channel #138
Changes from 15 commits
d3f5efb
a1348fc
ee7995c
0ade019
4f1c80a
b44ccbf
5b62d1b
d0b4b32
54475bf
9a79611
0a74252
a539ed1
95384b8
006bb53
fde3b02
7e77f7c
3babb2e
034394c
e6d7ef6
14507ff
47cc8ff
73b2f88
7dcf9be
6c49099
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module App.PageType exposing (Page(..)) | ||
|
||
{-| Prevent circula dependency. | ||
{-| Prevent circular dependency. | ||
-} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,13 @@ import App.PageType exposing (Page(..)) | |
import Config | ||
import Date | ||
import Dict | ||
import Http | ||
import ItemManager.Model | ||
import ItemManager.Update | ||
import Json.Decode exposing (bool, decodeValue) | ||
import Json.Encode exposing (Value) | ||
import Pages.Login.Update | ||
import Pusher.Model | ||
import Pusher.Utils exposing (getClusterName) | ||
import Pusher.Update | ||
import RemoteData exposing (RemoteData(..), WebData) | ||
import Task | ||
import Time exposing (minute) | ||
|
@@ -30,12 +29,7 @@ init flags = | |
Just config -> | ||
let | ||
defaultCmds = | ||
[ pusherKey | ||
( config.pusherKey.key | ||
, getClusterName config.pusherKey.cluster | ||
, Pusher.Model.eventNames | ||
) | ||
, Task.perform SetCurrentDate Date.now | ||
[ Task.perform SetCurrentDate Date.now | ||
] | ||
|
||
( cmds, activePage_ ) = | ||
|
@@ -87,13 +81,20 @@ update msg model = | |
model ! [] | ||
|
||
Logout -> | ||
( { emptyModel | ||
| accessToken = "" | ||
, activePage = Login | ||
, config = model.config | ||
} | ||
, accessTokenPort "" | ||
) | ||
let | ||
( _, pusherLogout ) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should probably be 1
--
( { emptyModel
| accessToken = ""
| pusher = pusherModelUpdated |
||
update (MsgPusher Pusher.Model.Logout) model | ||
in | ||
( { emptyModel | ||
| accessToken = "" | ||
, activePage = Login | ||
, config = model.config | ||
} | ||
, Cmd.batch | ||
[ accessTokenPort "" | ||
, pusherLogout | ||
] | ||
) | ||
|
||
MsgItemManager subMsg -> | ||
case model.user of | ||
|
@@ -124,6 +125,15 @@ update msg model = | |
-- If we don't have a user, we have nothing to do. | ||
model ! [] | ||
|
||
MsgPusher subMsg -> | ||
let | ||
( val, cmd ) = | ||
Pusher.Update.update backendUrl subMsg model.pusher | ||
in | ||
( { model | pusher = val } | ||
, Cmd.map MsgPusher cmd | ||
) | ||
|
||
PageLogin msg -> | ||
let | ||
( val, cmds, ( webDataUser, accessToken ) ) = | ||
|
@@ -166,6 +176,7 @@ update msg model = | |
[ Cmd.map PageLogin cmds | ||
, accessTokenPort accessToken | ||
, setActivePageCmds | ||
, pusherLogin model webDataUser accessToken | ||
] | ||
) | ||
|
||
|
@@ -205,6 +216,9 @@ update msg model = | |
ToggleSideBar -> | ||
{ model | sidebarOpen = not model.sidebarOpen } ! [] | ||
|
||
NoOp -> | ||
model ! [] | ||
|
||
|
||
{-| Determine is a page can be accessed by a user (anonymous or authenticated), | ||
and if not return a access denied page. | ||
|
@@ -240,6 +254,7 @@ subscriptions model = | |
[ Sub.map MsgItemManager <| ItemManager.Update.subscriptions model.pageItem model.activePage | ||
, Time.every minute Tick | ||
, offline (decodeValue bool >> HandleOfflineEvent) | ||
, Sub.map MsgPusher <| Pusher.Update.subscription | ||
] | ||
|
||
|
||
|
@@ -248,11 +263,35 @@ subscriptions model = | |
port accessTokenPort : String -> Cmd msg | ||
|
||
|
||
{-| Send Pusher key and cluster to JS. | ||
-} | ||
port pusherKey : ( String, String, List String ) -> Cmd msg | ||
|
||
|
||
{-| Get a singal if internet connection is lost. | ||
-} | ||
port offline : (Value -> msg) -> Sub msg | ||
|
||
|
||
pusherLogin : Model -> WebData User -> String -> Cmd Msg | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. worth a comment on the function, and a bit more comments in the function itself. |
||
pusherLogin model webDataUser accessToken = | ||
let | ||
pusherLoginMsg pusherKey pusherChannel = | ||
MsgPusher <| | ||
Pusher.Model.Login | ||
pusherKey | ||
pusherChannel | ||
(Pusher.Model.AccessToken accessToken) | ||
|
||
msg = | ||
case webDataUser of | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried doing it with a combination of RemoteData.map and RemoteData.withDefault, but it looks way more complicated to me. Probably I didn't understand what's the proper way to handle it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Untested, but could be shorten to: RemoteData.toMaybe webDataUser
|> Maybe.map (\user ->
RemoteData.toMaybe model.config
|> Maybe.map (\config -> pusherLoginMsg config.pusherKey user.pusherChannel)
|> Maybe.withDefault NoOp
)
|> Maybe.withDefault NoOp There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect. Thanks! |
||
Success user -> | ||
case model.config of | ||
Success config -> | ||
pusherLoginMsg config.pusherKey user.pusherChannel | ||
|
||
_ -> | ||
NoOp | ||
|
||
_ -> | ||
NoOp | ||
|
||
( _, pusherLogin ) = | ||
update msg model | ||
in | ||
pusherLogin |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ type alias ItemId = | |
type alias Item = | ||
{ name : String | ||
, image : String | ||
, privateNote : Maybe String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,23 +10,33 @@ import User.Model exposing (User) | |
|
||
view : Date -> User -> ItemId -> Item -> Html Msg | ||
view currentDate currentUser itemId item = | ||
div [] | ||
[ div | ||
[ class "ui secondary pointing fluid menu" ] | ||
[ h2 | ||
[ class "ui header" ] | ||
[ text item.name ] | ||
, div | ||
[ class "right menu" ] | ||
[ a | ||
[ class "ui active item" ] | ||
[ text "Overview" ] | ||
let | ||
privateNote = | ||
case item.privateNote of | ||
Just note -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
div [] [ text note ] | ||
|
||
_ -> | ||
text "" | ||
in | ||
div [] | ||
[ div | ||
[ class "ui secondary pointing fluid menu" ] | ||
[ h2 | ||
[ class "ui header" ] | ||
[ text item.name ] | ||
, div | ||
[ class "right menu" ] | ||
[ a | ||
[ class "ui active item" ] | ||
[ text "Overview" ] | ||
] | ||
] | ||
, div [] | ||
[ img [ src item.image, alt item.name ] [] | ||
] | ||
, div | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. copy divider |
||
[ class "ui divider" ] | ||
[] | ||
, privateNote | ||
] | ||
, div [] | ||
[ img [ src item.image, alt item.name ] [] | ||
] | ||
, div | ||
[ class "ui divider" ] | ||
[] | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,19 @@ module Pusher.Model exposing (..) | |
import Item.Model exposing (Item, ItemId) | ||
|
||
|
||
type alias Model = | ||
{ connectionStatus : ConnectionStatus | ||
, errors : List PusherError | ||
} | ||
|
||
|
||
emptyModel : Model | ||
emptyModel = | ||
{ connectionStatus = Initialized | ||
, errors = [] | ||
} | ||
|
||
|
||
type Cluster | ||
= ApSouthEast1 | ||
| EuWest1 | ||
|
@@ -25,8 +38,48 @@ type PusherEventData | |
= ItemUpdate Item | ||
|
||
|
||
type AccessToken | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
= AccessToken String | ||
|
||
|
||
type alias PusherChannel = | ||
String | ||
|
||
|
||
type alias PusherConfig = | ||
{ key : String | ||
, cluster : String | ||
, authEndpoint : String | ||
, channel : String | ||
, eventNames : List String | ||
} | ||
|
||
|
||
type ConnectionStatus | ||
= Initialized | ||
| Connecting (Maybe Int) | ||
| Connected | ||
| Unavailable (Maybe Int) | ||
| Failed | ||
| Disconnected | ||
| Other String | ||
|
||
|
||
type alias PusherError = | ||
{ code : Maybe Int | ||
, message : Maybe String | ||
} | ||
|
||
|
||
{-| Return the event names that should be added via JS. | ||
-} | ||
eventNames : List String | ||
eventNames = | ||
[ "item__update" ] | ||
|
||
|
||
type Msg | ||
= HandleError PusherError | ||
| HandleStateChange String | ||
| Login PusherAppKey PusherChannel AccessToken | ||
| Logout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
order by alpha bet