Skip to content
Permalink
Browse files

adding stuff for tracking who is online

  • Loading branch information...
w1zeman1p committed Jan 28, 2015
1 parent 934802b commit c84e731e042345ab4a84d222c97e2520a33d5f13
@@ -15,6 +15,15 @@ window.CodeRacer = {

$('#header').html(header.render().$el);

// Add the current user to the list of online users
CodeRacer.onlineUsers.add(new CodeRacer.Models.User(window.CURRENT_RACER));

$.ajax({
url: '/api/online_user',
type: 'POST',
data: window.CURRENT_RACER
});

Backbone.history.start();
}
};
@@ -26,3 +35,19 @@ Pusher.log = function (message) {
};

CodeRacer.pusher = new Pusher('ec38d09303a657c3fd5e');
CodeRacer.presence = CodeRacer.pusher.subscribe('presence');

function cleanup() {
CodeRacer.pusher.disconnect();

$.ajax({
url: '/api/online_user',
type: 'DELETE',
data: window.CURRENT_RACER
});
}

$(window).on('beforeunload', function() {
var x = cleanup();
return x;
});
@@ -12,3 +12,22 @@ CodeRacer.Collections.Users = Backbone.Collection.extend({
return this.track.url() + '/leaders';
}
});

CodeRacer.onlineUsers = new CodeRacer.Collections.Users();

CodeRacer.onlineUsers.fetch({
url: "/api/online_user"
});

CodeRacer.presence.bind('add_user', function (data) {
console.log('ADDING USER', data);
CodeRacer.onlineUsers.add(data);
});

CodeRacer.presence.bind('remove_user', function (data) {
console.log('REMOVING USER', data);
var user = CodeRacer.onlineUsers.get(data.id);
if(user) {
CodeRacer.onlineUsers.remove(user);
}
});
@@ -0,0 +1,12 @@
CodeRacer.Views.Online = Backbone.View.extend({
template: JST['users/online'],

render: function () {
var content = this.template({
users: this.collection
});

this.$el.html(content);
return this;
},
});
@@ -0,0 +1,3 @@
// Place all the styles related to the Api/OnlineUsers controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,7 @@
<ul class="list-group">
<% users.each(function(user) { %>
<li class="list-group-item">
<%= user.escape("nickname") %>
</li>
<% }) %>
</ul>
@@ -0,0 +1,50 @@
require 'set'

module Api
class OnlineUsersController < ApplicationController
before_action :require_user!
before_action :get_users
after_action :set_users

def show
render json: @users
end

def create
add_user
Pusher['presence'].trigger('add_user', user_hash)
render json: @users
end

def destroy
remove_user
Pusher['presence'].trigger('remove_user', user_hash)
render json: @users
end

private

def user_hash
{
id: current_user.id,
nickname: current_user.nickname
}
end

def add_user
@users << user_hash unless @users.include?(user_hash)
end

def remove_user
@users.delete_if { |u| u[:id] == current_user.id }
end

def get_users
@users ||= Rails.cache.read('users') || Set.new
end

def set_users
Rails.cache.write('users', @users.to_a)
end
end
end
@@ -0,0 +1,2 @@
module Api::OnlineUsersHelper
end
@@ -7,6 +7,7 @@
resource :notification, only: [:show, :destroy]

namespace :api, defaults: { format: :json } do
resource :online_user, only: [:create, :destroy, :show]
resource :setting, only: [:update]
resources :stats, only: [:index]
resources :tracks, only: [:index, :show, :create, :update] do
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Api::OnlineUsersController, :type => :controller do

end

0 comments on commit c84e731

Please sign in to comment.
You can’t perform that action at this time.