Skip to content

Commit

Permalink
Merge pull request #21 from CodersTV/fix-online-counter
Browse files Browse the repository at this point in the history
Merge online counter and chatroom on coder channel
  • Loading branch information
gabrielhpugliese committed Jan 31, 2015
2 parents 9db0bfb + fac75b5 commit c23658b
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 151 deletions.
81 changes: 31 additions & 50 deletions client/lib/controllers/channel.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,38 @@
function getCurrentCoder (coderId) {
if (! coderId) {
return {
userId: coderId
};
}

var user = Meteor.users.findOne({'profile.username': coderId}),
currentCoder = {coderId: coderId};

if (user) {
return {
userId: user._id,
username: coderId,
name: user.profile.name
};
CoderController = RouteController.extend({
template: 'channel',
waitOn: function () {
return Meteor.subscribe('ChannelWithOwnerAndFollowers', this.params.coderId);
},
onAfterAction: function () {
var coder = Meteor.users.findOneFromCoderId(this.params.coderId);
if (! _.isEmpty(coder)) {
Session.set('coder', coder);
Meteor.subscribe(
'userPresenceWithProfile',
coder.profile.username,
coder._id,
function () {
Session.set('chatSubsReady', true);
}
);
Session.set('currentCoder', coder._id);
}
}
});

return {
userId: coderId
};
}


ChannelController = RouteController.extend({
VideoController = RouteController.extend({
template: 'channel',
waitOn: function () {
var path = Path.get(true);

return [
Meteor.subscribe('userPresence', path),
Meteor.subscribe('Channels', Session.get('channelSearchQuery')),
Meteor.subscribe('Users'),
Meteor.subscribe('Followers'),
];
return Meteor.subscribe('VideoWithOwnerAndFollowers', this.params.videoId);
},
onBeforeAction: function () {
var coderId = this.params.coderId;
var videoId = this.params.videoId;
var currentCoder = getCurrentCoder(coderId);
var currentVideo = Channel.get(currentCoder.userId, videoId);
var title = TITLE;

Session.set('channelSearchQuery', this.params.keyword);
Session.set('currentCoder', currentCoder.userId);
Session.set('currentUsername', currentCoder.username);
Session.set('currentVideo', videoId);
if (currentVideo) {
title = currentVideo.title + ' by ' + currentCoder.name + ' coding with ' + currentVideo.language + title;
} else if (currentCoder.name) {
title = currentCoder.name + title;
} else {
title = 'Coders' + title;
}
data: function () {
return Channels.findOne({_id: this.params.videoId});
}
});

document.title = title;
CoderListController = RouteController.extend({
template: 'channels',
waitOn: function () {
return Meteor.subscribe('ChannelsWithOwner');
}
});
9 changes: 8 additions & 1 deletion client/lib/controllers/logged_user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LoggedUserController = BasicController.extend({
LoggedUserController = RouteController.extend({
onBeforeAction: function () {
if (! Meteor.user()) {
this.redirect(Router.routes.login.path());
Expand All @@ -8,3 +8,10 @@ LoggedUserController = BasicController.extend({
}
}
});

DashboardController = LoggedUserController.extend({
template: 'dashboard',
waitOn: function () {
return Meteor.subscribe('SelfVideos');
}
});
8 changes: 3 additions & 5 deletions client/lib/controllers/search.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
SearchController = ChannelController.extend({
SearchController = CoderListController.extend({
template: 'channels',
onBeforeAction: function () {
var keyword = this.params.keyword;
Session.set('channelSearchQuery', keyword);
document.title = 'Searching for ' + keyword + TITLE;
waitOn: function () {
return Meteor.subscribe('ChannelsSearchWithUsers', this.params.keyword);
}
});

29 changes: 9 additions & 20 deletions client/lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Router.onAfterAction(function () {
}
});

Router.waitOn(function subscribeToSelf () {
Meteor.subscribe('SelfUser');
});

Router.onBeforeAction(function setTitle () {
document.title = 'Watch programming videos or broadcast your coder skills with the world | CodersTV';
});
Expand Down Expand Up @@ -84,27 +88,24 @@ Router.map(function () {
path: '/',
waitOn: function () {
return [
Meteor.subscribe('ChannelsWithUsers', 3),
Meteor.subscribe('ChannelsWithOwner', 3),
Meteor.subscribe('FeaturedChannelWithUser')
];
}
});

this.route('coder', {
controller: ChannelController,
template: 'channel',
controller: CoderController,
path: '/coder/:coderId'
});

this.route('codersList', {
controller: ChannelController,
template: 'channels',
controller: CoderListController,
path: '/coders'
});

this.route('video', {
controller: ChannelController,
template: 'channel',
controller: VideoController,
path: '/video/:coderId/:videoId'
});

Expand Down Expand Up @@ -133,19 +134,7 @@ Router.map(function () {
this.route('loading');

this.route('dashboard', {
controller: LoggedUserController,
onBeforeAction: function () {
if (Meteor.isClient) {
document.title = 'Dashboard' + TITLE;
}
},
waitOn: function () {
return [
Meteor.subscribe('Languages'),
Meteor.subscribe('Users'),
Meteor.subscribe('Channels')
];
}
controller: DashboardController
});

this.route('preferences', {
Expand Down
41 changes: 16 additions & 25 deletions client/views/channels/channel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This file is a mess :(

// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
// player = null;
Expand Down Expand Up @@ -104,12 +106,17 @@ Template.channel.LoadDisqusJS = function () {
})();
};

Template.channel.getChannel = function() {
return Channels.findOne({});
};

Deps.autorun(function disqus () {
var user = Meteor.user();
var channel = Template.channel.getChannel();

if (Session.equals('channelRendered', false) ||
Session.equals('disqusSSO', false) ||
Session.equals('currentVideo', undefined)) {
! channel) {
return;
}

Expand All @@ -125,39 +132,19 @@ Deps.autorun(function disqus () {
Template.channel.LoadDisqusJS();
});


Template.channel.getChannel = function() {
var currentCoder = Session.get('currentCoder'),
currentVideo = Session.get('currentVideo'),
filter = {};

if (currentCoder) {
filter.owner = currentCoder;
}
if (currentVideo) {
filter._id = currentVideo;
return Channels.findOne(filter);
}
filter.isLive = true;
return Channels.findOne(filter);

};

Deps.autorun(function youtube (c) {
var channel = Template.channel.getChannel();
if (Session.equals('YTApiReady', false) ||
Session.equals('channelRendered', false)) {
Session.equals('channelRendered', false) ||
! channel) {
return;
}

var interval = Meteor.setInterval(function () {
if(! document.getElementById('player-div')) {
return;
}
var
playerDiv = document.createElement('div'),
channel = Template.channel.getChannel()
;
var playerDiv = document.createElement('div');

playerDiv.id = 'player-div';
document.getElementById('player-parent').innerHTML = '';
Expand All @@ -172,6 +159,10 @@ Deps.autorun(function youtube (c) {

Meteor.clearInterval(interval);
}, 100);

c.onInvalidate(function () {
Meteor.clearInterval(interval);
});
});

Template.info_row.watchers = function () {
Expand All @@ -180,7 +171,7 @@ Template.info_row.watchers = function () {

Template.channel.getCoder = function() {
return Meteor.users.findOne({
_id : Session.get('currentCoder')
_id: Session.get('currentCoder')
});
};

Expand Down
18 changes: 18 additions & 0 deletions lib/both/collections/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,21 @@ Meteor.users.setDescription = function (description) {
}});
};

Meteor.users.findOneFromCoderId = function (coderId) {
// coderId can be its username or _id
return this.findOne({$or: [
{'profile.username': coderId},
{_id: coderId}
]});
};

Meteor.users.findOneFromPath = function (path) {
var coderId;

if (_.isEmpty(path)) {
return {};
}

coderId = path.split('/')[2];
return this.findOneFromCoderId(coderPath);
};
1 change: 0 additions & 1 deletion packages/chatroom/client/lib/startup.js

This file was deleted.

16 changes: 9 additions & 7 deletions packages/chatroom/client/views/chatroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Template.chatroom.removeLastMessage = function () {

Template.chatroom.sendMsg = function () {
var user = Meteor.user();
if (! user ||
if (! user ||
(user.superchat && typeof user.superchat.canChat !== 'undefined' && !user.superchat.canChat)) {
return;
}
Expand All @@ -59,8 +59,9 @@ Template.chatroom.sendMsg = function () {
}

var owner = Meteor.userId(),
host = Path(true),
action = 'says';
action = 'says',
coder = Session.get('coder'),
host = coder._id;

Template.chatroom.removeLastMessage();
superChatMsgs.insert({
Expand All @@ -72,7 +73,7 @@ Template.chatroom.sendMsg = function () {
});
superChatStream.emit('chat', message, host);
Template.chatroom.scrollToBottom();

};

Template.chatroom.insertAtCaret = function(txtarea, text) {
Expand Down Expand Up @@ -119,7 +120,7 @@ Template.chatroom.rendered = function () {
});

self.banDeps = Deps.autorun(function banUserWhenFlood () {
if (! usersSubs.ready()) {
if (! Session.equals('chatSubsReady', true)) {
return;
}

Expand Down Expand Up @@ -149,7 +150,8 @@ Template.chatroom.destroyed = function () {

Template.chatroom.helpers({
msgs: function () {
return superChatMsgs.find({host: Path(true)}, {limit: Superchat.messageLimitOnScreen});
var coder = Session.get('coder');
return superChatMsgs.find({host: coder._id}, {limit: Superchat.messageLimitOnScreen});
},
getProfile: function (userId) {
return userId && Meteor.users.findOne({_id: userId}).superchat;
Expand All @@ -161,7 +163,7 @@ Template.chatroom.helpers({

if (_.isEmpty(ids))
return;

query = ids.map(function (id) {
return { _id: id };
});
Expand Down
4 changes: 2 additions & 2 deletions packages/chatroom/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Package.on_use(function (api, where) {
api.use([
'meteor',
'standard-app-packages',
'presence',
'presence',
'streams'
], both);

Expand All @@ -26,7 +26,6 @@ Package.on_use(function (api, where) {
], both);

api.add_files([
'client/lib/startup.js',
'client/lib/helpers.js',
'client/stylesheets/superchat.less',
'client/views/chatroom.html',
Expand All @@ -44,6 +43,7 @@ Package.on_use(function (api, where) {

if (typeof api.export !== 'undefined') {
api.imply('reactive-path');
api.imply('publish-composite');
api.export('Superchat', both);
}

Expand Down
Loading

0 comments on commit c23658b

Please sign in to comment.