2020-12-17 14:01:19 -05:00
// Generated by CoffeeScript 2.5.1
2011-11-06 12:21:27 -08:00
( function ( ) {
2021-03-16 19:34:11 -04:00
var fs , log , redis , unread _counts ;
2011-11-06 12:21:27 -08:00
fs = require ( 'fs' ) ;
redis = require ( 'redis' ) ;
2013-05-01 12:51:59 -07:00
log = require ( './log.js' ) ;
2021-03-16 19:34:11 -04:00
unread _counts = ( server ) => {
var ENV _DEV , ENV _DOCKER , ENV _PROD , REDIS _PORT , REDIS _SERVER , SECURE , io ;
2021-03-19 11:16:29 -04:00
ENV _DEV = process . env . NODE _ENV === 'development' || process . env . NODE _ENV === 'debug' ;
2021-03-16 19:34:11 -04:00
ENV _PROD = process . env . NODE _ENV === 'production' ;
ENV _DOCKER = process . env . NODE _ENV === 'docker' ;
2021-03-16 20:00:55 -04:00
REDIS _SERVER = "db_redis" ;
2021-03-16 19:34:11 -04:00
if ( ENV _DEV ) {
REDIS _SERVER = 'localhost' ;
} else if ( ENV _PROD ) {
REDIS _SERVER = 'db-redis-user.service.nyc1.consul' ;
}
SECURE = ! ! process . env . NODE _SSL ;
REDIS _PORT = ENV _DOCKER ? 6579 : 6379 ;
// client = redis.createClient 6379, REDIS_SERVER
// RedisStore = require 'socket.io/lib/stores/redis'
// rpub = redis.createClient 6379, REDIS_SERVER
// rsub = redis.createClient 6379, REDIS_SERVER
// rclient = redis.createClient 6379, REDIS_SERVER
log . debug ( "Starting NewsBlur unread count server..." ) ;
if ( ! ENV _DEV && ! process . env . NODE _ENV ) {
log . debug ( "Specify NODE_ENV=<development,production>" ) ;
return ;
} else if ( ENV _DEV ) {
log . debug ( "Running as development server" ) ;
2021-03-16 20:07:46 -04:00
} else if ( ENV _DOCKER ) {
log . debug ( "Running as docker server" ) ;
2021-03-16 19:34:11 -04:00
} else {
log . debug ( "Running as production server" ) ;
}
io = require ( 'socket.io' ) ( server , {
2021-01-04 18:14:52 -05:00
path : "/v3/socket.io"
2016-11-30 15:26:42 -08:00
} ) ;
2021-03-16 19:34:11 -04:00
// io.set('transports', ['websocket'])
// io.set 'store', new RedisStore
// redisPub : rpub
// redisSub : rsub
// redisClient : rclient
io . on ( 'connection' , function ( socket ) {
var ip ;
ip = socket . handshake . headers [ 'X-Forwarded-For' ] || socket . handshake . address ;
socket . on ( 'subscribe:feeds' , ( feeds , username ) => {
var ref ;
this . feeds = feeds ;
this . username = username ;
2021-04-02 18:35:36 -04:00
log . info ( this . username , ` Connecting ( ${ this . feeds . length } feeds, ${ ip } ), ` + ` ( ${ io . engine . clientsCount } connected) ` + ` ${ SECURE ? "(SSL)" : "" } ` ) ;
2021-03-16 19:34:11 -04:00
if ( ! this . username ) {
return ;
}
socket . on ( "error" , function ( err ) {
return log . debug ( ` Error (socket): ${ err } ` ) ;
} ) ;
if ( ( ref = socket . subscribe ) != null ) {
ref . quit ( ) ;
}
socket . subscribe = redis . createClient ( REDIS _PORT , REDIS _SERVER ) ;
socket . subscribe . on ( "error" , ( err ) => {
var ref1 ;
log . info ( this . username , ` Error: ${ err } ( ${ this . feeds . length } feeds) ` ) ;
return ( ref1 = socket . subscribe ) != null ? ref1 . quit ( ) : void 0 ;
} ) ;
socket . subscribe . on ( "connect" , ( ) => {
var feeds _story ;
log . info ( this . username , ` Connected ( ${ this . feeds . length } feeds, ${ ip } ), ` + ` ( ${ io . engine . clientsCount } connected) ` + ` ${ SECURE ? "(SSL)" : "(non-SSL)" } ` ) ;
socket . subscribe . subscribe ( this . feeds ) ;
feeds _story = this . feeds . map ( function ( f ) {
return ` ${ f } :story ` ;
} ) ;
socket . subscribe . subscribe ( feeds _story ) ;
return socket . subscribe . subscribe ( this . username ) ;
} ) ;
return socket . subscribe . on ( 'message' , ( channel , message ) => {
var event _name ;
event _name = 'feed:update' ;
if ( channel === this . username ) {
event _name = 'user:update' ;
} else if ( channel . indexOf ( ':story' ) >= 0 ) {
event _name = 'feed:story:new' ;
}
log . info ( this . username , ` Update on ${ channel } : ${ event _name } - ${ message } ` ) ;
return socket . emit ( event _name , channel , message ) ;
2020-12-17 14:01:19 -05:00
} ) ;
} ) ;
2021-03-16 19:34:11 -04:00
return socket . on ( 'disconnect' , ( ) => {
var ref , ref1 ;
if ( ( ref = socket . subscribe ) != null ) {
ref . quit ( ) ;
2020-12-17 14:01:19 -05:00
}
2021-03-16 19:34:11 -04:00
return log . info ( this . username , ` Disconnect ( ${ ( ref1 = this . feeds ) != null ? ref1 . length : void 0 } feeds, ${ ip } ), ` + ` there are now ${ io . engine . clientsCount } users. ` + ` ${ SECURE ? "(SSL)" : "(non-SSL)" } ` ) ;
2020-12-17 14:01:19 -05:00
} ) ;
2011-11-06 12:21:27 -08:00
} ) ;
2021-03-16 19:34:11 -04:00
return io . sockets . on ( 'error' , function ( err ) {
return log . debug ( ` Error (sockets): ${ err } ` ) ;
2011-11-06 12:21:27 -08:00
} ) ;
2021-03-16 19:34:11 -04:00
} ;
2011-11-06 12:21:27 -08:00
2021-03-16 19:34:11 -04:00
exports . unread _counts = unread _counts ;
2013-08-12 11:52:29 -07:00
2011-11-06 12:21:27 -08:00
} ) . call ( this ) ;