Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
class MessagesController < ApplicationController
def backfill
@message = Message.find(params[:id])
before_action :set_message, only: [
:backfill,
:next,
:update_user_count
]

def backfill
url = "https://api-v2.soundcloud.com/explore/Popular+Music?tag=out-of-experiment&limit=100&client_id=#{ENV['SOUNDCLOUD_ID']}"
resp = Net::HTTP.get_response(URI.parse(url))
buffer = resp.body
Expand All @@ -20,15 +24,18 @@ def backfill
end
end

def create
@message = Message.create message_params
redirect_to @message
end

def index
@messages = Message.all.order('lower(title) ASC')
end

def next
if @message = Message.find_by(id: params[:id])
if @comment = @message.comments.playing.first
NextUpJob.perform_now(@message.id, @comment.id)
end
if @comment = @message.comments.playing.first
NextUpJob.perform_now(@message.id, @comment.id)
end
end

Expand Down Expand Up @@ -60,14 +67,24 @@ def show
end
end

def create
@message = Message.create message_params
redirect_to @message
def update_user_count
user_count = params[:user_count].presence || 0
user_count = user_count.to_i
if user_count > @message.most_users_count
@message.update_attributes(most_users_count: user_count)
end
render json: {user_count: @message.most_users_count}, status: :ok
end

private

def message_params
params.require(:message).permit(:title)
end

def set_message
unless @message = Message.find_by(id: params[:id])
render nothing: true, status: :bad_request
end
end
end
6 changes: 5 additions & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def blurred_album_art_url
ix_image_url(album_art_url, { blur: 200 })
end

def duration
(self.responses['duration'] / 1000).ceil
end

def palette_url
ix_image_url(album_art_url, { palette: 'css', colors: 4, class: 'album', zoom: 100 })
end
Expand All @@ -34,7 +38,7 @@ def reset!
def start_playing(should_push)
now = Time.now.utc
update_attributes(now_playing: true, aired_at: now)
next_track_at = now + (self.responses['duration'] / 1000).ceil
next_track_at = now + duration
NextUpJob.set(wait_until: next_track_at).perform_later(self.message.id, self.id)
PusherService.now_playing_track(self) if should_push
end
Expand Down
15 changes: 15 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@ class Message < ActiveRecord::Base
belongs_to :user
has_many :comments

def mvdj
grouped_track_count = self.comments.group(:user_id).count
sorted_group = grouped_track_count.sort_by { |user_id, count| -count }
User.find_by(id: sorted_group.first)
end

def played_tracks
self.comments.where.not(aired_at: nil)
end

def queue_count
self.comments.enqueued.count
end

def total_played_hours
seconds = played_tracks.reduce(0) { |sum, track| sum + track.duration }
(seconds.to_f / 60.0 / 60.0).round(2)
end
end
19 changes: 14 additions & 5 deletions app/views/messages/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,19 @@

<table class="my3">
<tr>
<td>Songs played</td><td>9,001</td>
<td>Songs played</td><td><%= @message.played_tracks.count %></td>
</tr>
<tr>
<td>Most users in room</td><td>A lot</td>
<td>Most users in room</td><td class="js-most-users-count"><%= @message.most_users_count %></td>
</tr>
<tr>
<td>Hours of music queued up</td><td>So many</td>
<td>Hours of music queued up</td><td><%= @message.total_played_hours %></td>
</tr>
<tr>
<td>Longest streak</td><td>Like, days</td>
</tr>
<tr>
<td>MVDJ</td><td>Baldwin</td>
<td>MVDJ</td><td><%= @message.mvdj.full_name %></td>
</tr>
</table>

Expand Down Expand Up @@ -365,6 +365,16 @@
$('.usersInRoom').append('<img src="' + member.info['avatar_url'] + '" data-member-id="' + member.id + '" title="' + member.info['name'] + '">');
});
$('.js-membersOnline').text(presenceChannel.members.count);
$.ajax({
url: "/messages/<%= @message.id %>/update_user_count",
method: 'post',
data: {
user_count: presenceChannel.members.count
},
success: function(data) {
$('.js-most-users-count').text(data.user_count);
}
});
});

// Listen for users entering the room
Expand Down Expand Up @@ -498,5 +508,4 @@
$('#new_comment').bind('ajax:complete', function(event,xhr,status){
$('#comment_content').val('');
});

</script>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
get :backfill
get :next
get :reload_all
post :update_user_count
end
end
resources :pusher, only: [] do
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20160309053318_add_most_users_count_to_messages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMostUsersCountToMessages < ActiveRecord::Migration[5.0]
def change
add_column :messages, :most_users_count, :integer, default: 0
end
end
18 changes: 8 additions & 10 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160220202815) do
ActiveRecord::Schema.define(version: 20160309053318) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -20,9 +20,9 @@
t.integer "message_id"
t.integer "user_id"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
t.jsonb "responses", default: {}, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "now_playing", default: false, null: false
t.datetime "aired_at"
t.index ["message_id"], name: "index_comments_on_message_id", using: :btree
Expand All @@ -33,21 +33,19 @@
t.integer "user_id"
t.string "title"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "most_users_count", default: 0
t.index ["user_id"], name: "index_messages_on_user_id", using: :btree
end

create_table "users", force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.jsonb "soundcloud_token"
t.jsonb "soundcloud_response"
t.boolean "boss", default: false
end

add_foreign_key "comments", "messages"
add_foreign_key "comments", "users"
add_foreign_key "messages", "users"
end