1
0
Fork 0
oldhaven/app/assets/javascripts/vkontakte.coffee

95 lines
2.9 KiB
CoffeeScript
Raw Normal View History

2011-09-07 23:47:11 +00:00
class window.Vkontakte
2011-09-09 22:13:02 +00:00
api_id: null
2011-11-27 23:13:14 +00:00
session_length: 3600 # seconds
2011-09-09 22:13:02 +00:00
constructor: (@api_id) ->
getApiId: ->
@api_id
init: ->
log "Initializing Vkontakte API ..."
2011-09-09 22:13:02 +00:00
window.vkAsyncInit = ->
VK.init apiId: _vkontakte.getApiId()
2011-11-27 09:56:10 +00:00
_vkontakte.auth()
2011-11-29 00:20:14 +00:00
VK.Widgets.Like("vk-like", {type: "mini", height: 20, pageUrl: "http://beathaven.org/", text: "Like"})
2011-06-29 04:11:54 +00:00
2011-09-09 22:13:02 +00:00
setTimeout ->
2011-10-20 16:42:38 +00:00
$('#vk_api_transport').append($('<script async="async" type="text/javascript" src="http://vkontakte.ru/js/api/openapi.js">'))
2011-09-09 22:13:02 +00:00
, 0
authInfo: (response) ->
2011-11-26 21:30:43 +00:00
if typeof response isnt 'undefined' and response.session?
2011-09-09 22:13:02 +00:00
_session = new Session(response.session)
_session.query '/user/auth', {}, (ar) ->
2011-11-26 21:30:43 +00:00
log "Authenticating user ..."
2011-11-29 17:03:28 +00:00
if ar.newbie or ar.user.name == ""
2011-11-29 23:08:00 +00:00
log "Requesting user name from Vkontakte ..."
2011-09-09 22:13:02 +00:00
VK.Api.call 'getVariable', key: 1281, (r) ->
_session.query '/user/update', params: { name: r.response }, (ar2) ->
2011-09-09 22:13:02 +00:00
_session.setUser ar2.user
window._session = _session
_ajax.detectPage()
2011-09-15 09:38:24 +00:00
$('#authorized').css display: 'block'
if ar.ok_reload
window.location.reload()
false
2011-11-29 23:08:00 +00:00
log "Requesting user tracks from Vkontakte ..."
VK.Api.call 'audio.get', uid: ar.user.vkid, (r) ->
_session.query "/user/set_first_favorites", tracks: r.response, (r2) ->
log "Sending your Vkontakte media collection to BeatHaven ..."
if r2.status?
log "Got error: #{r2.status}"
else
log "We believe your favorite artists are #{r2.join(', ')}"
false
false
else if ar.ok_reload
window.location.reload()
false
2011-09-09 22:13:02 +00:00
else
_session.setUser ar.user
window._session = _session
_ajax.detectPage()
$('.fullscreen').hide();
if response.session.expire?
2011-11-27 23:13:14 +00:00
# expire_in = response.session.expire * 1000 - new Date().getTime()
# the time is an illusion...
expire_in = @session_length * 1000
2011-11-27 09:56:10 +00:00
log "Session will expire in #{Math.round(expire_in / 1000)} seconds"
2011-09-09 22:13:02 +00:00
setTimeout ->
2011-11-27 09:56:10 +00:00
log "Session expired"
2011-09-09 22:13:02 +00:00
_vkontakte.auth()
false
2011-11-27 09:56:10 +00:00
, expire_in + 1000
2011-09-09 22:13:02 +00:00
else
_session = new Session({})
_session.setUser {}
window._session = _session
_ajax.detectPage()
$('.fullscreen').hide();
auth: ->
2011-11-27 09:56:10 +00:00
log "Requesting new Vkontakte session ..."
2011-09-09 22:13:02 +00:00
VK.Auth.getLoginStatus (response) ->
_vkontakte.authInfo(response)
false
, 8
false
2011-06-27 18:41:36 +00:00
2011-09-15 09:38:24 +00:00
$('#login').live 'click', ->
2011-09-09 22:13:02 +00:00
VK.Auth.login (response) ->
_vkontakte.authInfo(response)
false
, 8
false
2011-11-27 23:13:14 +00:00
2011-09-15 09:38:24 +00:00
$('#logout').live 'click', ->
2011-09-09 22:13:02 +00:00
VK.Auth.logout (response) ->
_vkontakte.authInfo(response)
2011-09-19 08:21:03 +00:00
$('.button-container').hide()
2011-09-09 22:13:02 +00:00
false
false