-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add real-time comments to Reader full-post view #40396
Merged
Merged
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
5a63490
Add Lasagna websocket middleware (first pass)
mhsdef 74f6fa3
Step forward
mhsdef 3a41869
Sync to latest changes
mhsdef be2ee42
One other file
mhsdef c8f2ce2
Fix topics
mhsdef d8f2713
Check in a bunch of stuff
mhsdef f0e7ee9
Remove Presence for now
mhsdef e511e86
Fix logging path
mhsdef f496ca8
Remove Presence stragglers
mhsdef d02422c
More Presence stragglers
mhsdef 6e105c6
More cleanup
mhsdef d4324ba
Merge branch 'master' into try/realtime-comments
mhsdef 6e23185
Iterate
mhsdef fa97009
Push connection management back into the middleware
mhsdef f6ef74e
Add Reader full post view state subtree
mhsdef 9bbae1b
Use new full view post state logic
mhsdef 87b0a86
Switch to dynamic load of Phoenix.js
mhsdef 2c35393
Additional cleanup
mhsdef ed5149e
Merge branch 'master' into try/realtime-comments
unDemian aebbe1b
Merge branch 'master' into try/realtime-comments
mhsdef dde8e3b
Remove stray addition
mhsdef 76a83fa
Add config.isEnabled check back for lasagna
mhsdef 5790016
Merge branch 'master' into try/realtime-comments
unDemian 3be94fe
Merge branch 'master' into try/realtime-comments
mhsdef 7277061
Add webpackchunkname to dynamic import
mhsdef be75f4f
To be handled in a subsequent PR
mhsdef 0a3aa22
Merge branch 'master' into try/realtime-comments
unDemian 5b82179
Final adjustment
mhsdef 19b1dae
Merge branch 'master' into try/realtime-comments
mhsdef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const socketConnected = () => ( { type: 'LASAGNA_SOCKET_CONNECTED' } ); | ||
export const socketDisconnected = () => ( { type: 'LASAGNA_SOCKET_DISCONNECTED' } ); | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import wpcom from 'lib/wp'; | ||
import { getCurrentUser } from 'state/current-user/selectors'; | ||
import { socket, socketConnect, socketDisconnect } from './socket'; | ||
import privatePostChannelMiddleware from './private-post-channel/actions-to-events'; | ||
import publicPostChannelMiddleware from './public-post-channel/actions-to-events'; | ||
import userChannelMiddleware from './user-channel/actions-to-events'; | ||
|
||
let socketConnecting = false; | ||
|
||
/** | ||
* Compose a list of middleware into one middleware | ||
* Props @rhc3 | ||
* | ||
* @param m middlewares to compose | ||
*/ | ||
const combineMiddleware = ( ...m ) => { | ||
return store => { | ||
const initialized = m.map( middleware => middleware( store ) ); | ||
return next => initialized.reduce( ( chain, mw ) => mw( chain ), next ); | ||
}; | ||
}; | ||
|
||
/** | ||
* Connection management middleware | ||
* | ||
* @param store middleware store | ||
*/ | ||
const connectMiddleware = store => next => action => { | ||
// bail unless this is a section set with the section definition | ||
if ( action.type !== 'SECTION_SET' || ! action.section ) { | ||
return next( action ); | ||
} | ||
|
||
// connect if we are going to the reader without a socket | ||
if ( ! socket && ! socketConnecting && action.section.name === 'reader' ) { | ||
socketConnecting = true; | ||
const user = getCurrentUser( store.getState() ); | ||
|
||
wpcom | ||
.request( { | ||
method: 'POST', | ||
path: '/jwt/sign', | ||
body: { payload: JSON.stringify( { user } ) }, | ||
} ) | ||
.then( ( { jwt } ) => { | ||
socketConnect( store, jwt, user.ID ); | ||
socketConnecting = false; | ||
} ); | ||
} | ||
|
||
// disconnect if we are leaving the reader with a socket | ||
else if ( socket && action.section.name !== 'reader' ) { | ||
socketDisconnect( store ); | ||
} | ||
|
||
return next( action ); | ||
}; | ||
|
||
export default combineMiddleware( | ||
connectMiddleware, | ||
userChannelMiddleware, | ||
privatePostChannelMiddleware, | ||
publicPostChannelMiddleware | ||
); |
78 changes: 78 additions & 0 deletions
78
client/state/lasagna/private-post-channel/actions-to-events.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import debugFactory from 'debug'; | ||
|
||
/** | ||
* Internal Dependencies | ||
*/ | ||
import { LASAGNA_SOCKET_CONNECTED, ROUTE_SET } from 'state/action-types'; | ||
import { READER_POST_SEEN } from 'state/reader/action-types'; | ||
import { getReaderFullViewPostKey } from 'state/reader/full-view/selectors/get-reader-full-view-post-key'; | ||
import { getPostByKey } from 'state/reader/posts/selectors'; | ||
import { getSite } from 'state/reader/sites/selectors'; | ||
import registerEventHandlers from './events-to-actions'; | ||
import { socket } from '../socket'; | ||
|
||
let channel = null; | ||
|
||
const channelTopicPrefix = 'private:push:wp_post:'; | ||
const debug = debugFactory( 'lasagna:channel:private:push:wp_post' ); | ||
|
||
const joinChannel = ( store, site, post, postKey ) => { | ||
if ( ! socket || ! site.is_private ) { | ||
return; | ||
} | ||
|
||
channel = socket.channel( channelTopicPrefix + post.global_ID, { post_key: postKey } ); | ||
registerEventHandlers( channel, store ); | ||
|
||
channel | ||
.join() | ||
.receive( 'ok', () => debug( 'channel join ok' ) ) | ||
.receive( 'error', ( { reason } ) => { | ||
debug( 'channel join error', reason ); | ||
channel.leave(); | ||
channel = null; | ||
} ); | ||
}; | ||
|
||
const leaveChannel = () => { | ||
channel && channel.leave(); | ||
channel = null; | ||
}; | ||
|
||
export default store => next => action => { | ||
switch ( action.type ) { | ||
case LASAGNA_SOCKET_CONNECTED: { | ||
const state = store.getState(); | ||
const postKey = getReaderFullViewPostKey( state ); | ||
const post = getPostByKey( state, postKey ); | ||
|
||
if ( ! post ) { | ||
break; | ||
} | ||
|
||
const site = getSite( state, post.site_ID ); | ||
|
||
if ( ! site ) { | ||
break; | ||
} | ||
|
||
joinChannel( store, site, post, postKey ); | ||
break; | ||
} | ||
|
||
case READER_POST_SEEN: { | ||
const postKey = getReaderFullViewPostKey( store.getState() ); | ||
joinChannel( store, action.payload.site, action.payload.post, postKey ); | ||
break; | ||
} | ||
|
||
case ROUTE_SET: | ||
leaveChannel(); | ||
break; | ||
} | ||
|
||
return next( action ); | ||
}; |
30 changes: 30 additions & 0 deletions
30
client/state/lasagna/private-post-channel/events-to-actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import debugFactory from 'debug'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { receiveComments } from 'state/comments/actions'; | ||
|
||
const debug = debugFactory( 'lasagna:channel:private:push:wp_post' ); | ||
|
||
export default function( channel, store ) { | ||
channel.on( 'new_comment', ( { payload: comment } ) => { | ||
debug( 'New comment', comment ); | ||
|
||
if ( ! comment ) { | ||
return; | ||
} | ||
|
||
store.dispatch( | ||
receiveComments( { | ||
siteId: comment.post.site_ID, | ||
postId: comment.post.ID, | ||
comments: [ comment ], | ||
commentById: true, | ||
} ) | ||
); | ||
} ); | ||
} |
76 changes: 76 additions & 0 deletions
76
client/state/lasagna/public-post-channel/actions-to-events.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import debugFactory from 'debug'; | ||
|
||
/** | ||
* Internal Dependencies | ||
*/ | ||
import { LASAGNA_SOCKET_CONNECTED, ROUTE_SET } from 'state/action-types'; | ||
import { READER_POST_SEEN } from 'state/reader/action-types'; | ||
import { getPostByKey } from 'state/reader/posts/selectors'; | ||
import { getReaderFullViewPostKey } from 'state/reader/full-view/selectors/get-reader-full-view-post-key'; | ||
import { getSite } from 'state/reader/sites/selectors'; | ||
import registerEventHandlers from './events-to-actions'; | ||
import { socket } from '../socket'; | ||
|
||
let channel = null; | ||
|
||
const channelTopicPrefix = 'public:push:wp_post:'; | ||
const debug = debugFactory( 'lasagna:channel:public:push:wp_post' ); | ||
|
||
const joinChannel = ( store, site, post ) => { | ||
if ( ! socket || site.is_private ) { | ||
return; | ||
} | ||
|
||
channel = socket.channel( channelTopicPrefix + post.global_ID ); | ||
registerEventHandlers( channel, store ); | ||
|
||
channel | ||
.join() | ||
.receive( 'ok', () => debug( 'channel join ok' ) ) | ||
.receive( 'error', ( { reason } ) => { | ||
debug( 'channel join error', reason ); | ||
channel.leave(); | ||
channel = null; | ||
} ); | ||
}; | ||
|
||
const leaveChannel = () => { | ||
channel && channel.leave(); | ||
channel = null; | ||
}; | ||
|
||
export default store => next => action => { | ||
switch ( action.type ) { | ||
case LASAGNA_SOCKET_CONNECTED: { | ||
const state = store.getState(); | ||
const postKey = getReaderFullViewPostKey( state ); | ||
const post = getPostByKey( state, postKey ); | ||
|
||
if ( ! post ) { | ||
break; | ||
} | ||
|
||
const site = getSite( state, post.site_ID ); | ||
|
||
if ( ! site ) { | ||
break; | ||
} | ||
|
||
joinChannel( store, site, post ); | ||
break; | ||
} | ||
|
||
case READER_POST_SEEN: | ||
joinChannel( store, action.payload.site, action.payload.post ); | ||
break; | ||
|
||
case ROUTE_SET: | ||
leaveChannel(); | ||
break; | ||
} | ||
|
||
return next( action ); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The action types are added to
client/state/action-types
but plain strings are used here.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.
That should be fixed in the next PR #40720