1- /*global shaka*/
2-
31const m = require ( 'mithril' ) ;
2+ const shaka = require ( 'shaka-player' ) ;
3+ const io = require ( 'socket.io-client' ) ;
44
5+ const CommentModel = require ( '../models/comment' ) ;
56const StreamModel = require ( '../models/stream' ) ;
67
78const datetime = require ( '../utils/dateFormat' ) ;
89
9- const Stream = module . exports = { } ;
10+ const Stream = module . exports = {
11+ stream : m . prop ( ) ,
12+ comments : m . prop ( [ ] ) ,
13+ socket : m . prop ( )
14+ } ;
1015
11- Stream . stream = m . prop ( ) ;
16+ const MAX_COMMENTS = 1000 ;
1217
1318const initPlayer = function ( ) {
1419 shaka . polyfill . installAll ( ) ;
@@ -34,10 +39,41 @@ const stopStream = function () {
3439 ) ;
3540} ;
3641
42+ const initComments = function ( ) {
43+ Stream . socket ( io ( ) ) ;
44+
45+ Stream . socket ( ) . emit ( 'identify' , document . cookie ) ;
46+ Stream . socket ( ) . emit ( 'join' , Stream . stream ( ) . room ( ) ) ;
47+ Stream . socket ( ) . on ( 'comment' , function ( res ) {
48+ m . startComputation ( ) ;
49+ Stream . comments ( ) . unshift ( new CommentModel ( {
50+ userId : res . userId ,
51+ alias : res . alias ,
52+ content : res . message ,
53+ createdAt : res . time
54+ } ) ) ;
55+ while ( Stream . comments ( ) . length > MAX_COMMENTS ) {
56+ Stream . comments ( ) . pop ( ) ;
57+ }
58+ m . endComputation ( ) ;
59+ } ) ;
60+ } ;
61+
62+ const destroyComments = function ( ) {
63+ Stream . socket ( ) . emit ( 'leave' , Stream . stream ( ) . room ( ) ) ;
64+ } ;
65+
3766Stream . controller = function ( ) {
3867 let id = m . route . param ( 'id' ) || - 1 ;
3968
40- StreamModel . get ( id ) . then ( Stream . stream ) ;
69+ m . sync ( [
70+ StreamModel . get ( id ) . then ( Stream . stream ) ,
71+ CommentModel . list ( id ) . then ( Stream . comments )
72+ ] ) . then ( initComments ) ;
73+
74+ return {
75+ onunload : destroyComments
76+ } ;
4177} ;
4278
4379Stream . view = function ( ) {
@@ -63,13 +99,20 @@ Stream.view = function () {
6399 m ( 'div.row col s9' , [
64100 m ( 'div.col s12' , stream . user ( ) . alias ( ) ) ,
65101 m ( 'div.col s12' , 'Start: ' + datetime . toShortDateTime ( stream . startDateTime ( ) ) ) ,
66- m ( 'div.col s12' , 'End: ' + datetime . toShortDateTime ( stream . endDateTime ( ) ) )
102+ stream . endDateTime ( ) ?
103+ m ( 'div.col s12' , 'End: ' + datetime . toShortDateTime ( stream . endDateTime ( ) ) ) :
104+ null
67105 ] )
68106 ] ) ,
69107 m ( 'div.row' , [
70108 m ( 'div.col s12' , stream . description ( ) ) ,
71109 m ( 'button.btn col s12' , { onclick : stopStream } , 'Stop Stream' ) ,
72- m ( 'div.col s12' , 'comment stream here' )
110+ m ( 'div#comments.col s12' ,
111+ Stream . comments ( ) . map ( ( c ) => m ( 'div.comment-row' , [
112+ '[' + datetime . toShortTime ( c . time ( ) ) + '] ' ,
113+ m ( 'a[href="/users/view/' + c . userId ( ) + '"]' , { config : m . route } , c . user ( ) + ':' ) ,
114+ ' ' + c . msg ( )
115+ ] ) ) )
73116 ] )
74117 ] )
75118 ] )
0 commit comments