@@ -521,12 +521,19 @@ Plugged.prototype._wsaprocessor = function(self, msg) {
521
521
self . state . chatcache . shift ( ) ;
522
522
}
523
523
524
- if ( chat . message . indexOf ( '@' + self . state . self . username ) > - 1 )
525
- self . emit ( self . CHAT_MENTION , chat ) ;
526
- else if ( chat . message . charAt ( 0 ) == '/' )
527
- self . emit ( self . CHAT_COMMAND , chat ) ;
528
-
529
- self . emit ( self . CHAT , chat ) ;
524
+ // guests who log in whilst in the room don't emit USER_JOIN
525
+ // messages, so they won't be in the user list yet. that means that
526
+ // plug.dj might send CHAT events from nonexistent users, so we
527
+ // first grab the user info, and then emit the chat event later
528
+ var user = self . getUserByID ( chat . id ) ;
529
+ if ( ! user || user . guest ) {
530
+ self . getUser ( chat . id , function ( e , userData ) {
531
+ self . _pushUser ( userData ) ;
532
+ self . _emitChat ( chat ) ;
533
+ } ) ;
534
+ }
535
+ else
536
+ self . _emitChat ( chat ) ;
530
537
break ;
531
538
532
539
case self . CHAT_DELETE :
@@ -682,13 +689,7 @@ Plugged.prototype._wsaprocessor = function(self, msg) {
682
689
683
690
case self . USER_JOIN :
684
691
var user = models . parseUser ( data . p ) ;
685
- self . state . room . users . push ( user ) ;
686
- self . state . room . meta . population ++ ;
687
-
688
- if ( self . isFriend ( user . id ) )
689
- self . emit ( self . FRIEND_JOIN , user ) ;
690
- else
691
- self . emit ( self . USER_JOIN , user ) ;
692
+ self . _pushUser ( user ) ;
692
693
break ;
693
694
694
695
case self . USER_UPDATE :
@@ -765,6 +766,27 @@ Plugged.prototype._keepAliveCheck = function() {
765
766
this . keepAliveID = setTimeout ( this . _keepAlive , 30 * 1000 ) ;
766
767
} ;
767
768
769
+ Plugged . prototype . _emitChat = function ( chat ) {
770
+ if ( chat . message . indexOf ( '@' + this . state . self . username ) > - 1 )
771
+ this . emit ( this . CHAT_MENTION , chat ) ;
772
+ else if ( chat . message . charAt ( 0 ) == '/' )
773
+ this . emit ( this . CHAT_COMMAND , chat ) ;
774
+
775
+ this . emit ( this . CHAT , chat ) ;
776
+ } ;
777
+
778
+ Plugged . prototype . _pushUser = function ( user ) {
779
+ if ( user . guest )
780
+ return ;
781
+
782
+ this . state . room . users . push ( user ) ;
783
+ this . state . room . population = this . state . room . users . length ;
784
+ if ( this . isFriend ( user . id ) )
785
+ this . emit ( this . FRIEND_JOIN , user ) ;
786
+ else
787
+ this . emit ( this . USER_JOIN , user ) ;
788
+ } ;
789
+
768
790
Plugged . prototype . sendChat = function ( message , deleteTimeout ) {
769
791
deleteTimeout = deleteTimeout || - 1 ;
770
792
0 commit comments