Skip to content

Commit

Permalink
Merge branch 'next' of github.com:play/play into next
Browse files Browse the repository at this point in the history
  • Loading branch information
maddox committed Apr 19, 2012
2 parents 2349e04 + 2c1aba1 commit 463568f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
15 changes: 14 additions & 1 deletion app/models/history.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'time'

module Play
# Keeps track of the historical record of Play. Think plays and user votes and
# playcounts, oh my.
Expand Down Expand Up @@ -51,6 +53,7 @@ def self.add(song,user)

# Increment the song's own playcount counter.
$redis.incr "#{KEY}:songs:#{song.id}:count"
$redis.set "#{KEY}:songs:#{song.id}:last_played_at", Time.now.utc.iso8601

# Persist into the specific user's histories.
$redis.rpush "#{KEY}:#{user.login}:song_ids", song.id
Expand All @@ -72,6 +75,16 @@ def self.count_by_song(song)
$redis.get("#{KEY}:songs:#{song.id}:count").to_i || 0
end

# Public: The last time a song was played.
#
# song - The Song object to look up
#
# Returns a Time object, or nil if the song has never been played.
def self.song_last_played_at(song)
return nil unless (val = $redis.get("#{KEY}:songs:#{song.id}:last_played_at"))
Time.iso8601(val)
end

# Public: The last x listens, with latest on top.
#
# number - The Integer number of results we should return.
Expand All @@ -91,4 +104,4 @@ def self.now
Time.now.to_i
end
end
end
end
21 changes: 19 additions & 2 deletions app/models/song.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'time'

module Play
class Song

Expand All @@ -20,6 +22,9 @@ class Song
# the current_user has starred this song.
attr_accessor :starred

# The last time a song was played
attr_accessor :last_played

# Initializes a new Song.
#
# options - One of two possible arguments:
Expand All @@ -39,6 +44,7 @@ def initialize(options)
@name = options[:name]
@artist = options[:artist]
@album = options[:album]
@last_played = options[:last_played]
end
end

Expand Down Expand Up @@ -134,6 +140,16 @@ def path
record.location.get.to_s
end

def last_played
return @last_played ||= History.song_last_played_at(self)
end

def last_played_iso8601
ret = last_played
return "" unless ret
return ret.iso8601
end

# The hashed representation of a Song, suitable for API responses.
#
# Returns a Hash.
Expand All @@ -144,9 +160,10 @@ def to_hash
:artist => artist,
:album => album,
:starred => starred || false,
:queued => queued?
:queued => queued?,
:last_played => last_played_iso8601,
}
end

end
end
end

0 comments on commit 463568f

Please sign in to comment.