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

92 lines
2.6 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-27 23:13:14 +00:00
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-09-09 22:13:02 +00:00
if ar.newbie
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
2011-09-09 23:30:03 +00:00
$('#username')
.html (if _session.getUser().name then _session.getUser().name else '%username%')
2011-09-09 22:13:02 +00:00
window._session = _session
_ajax.detectPage()
_session.displayAuthorizedContent()
2011-09-15 09:38:24 +00:00
$('#authorized').css display: 'block'
if ar.ok_reload
window.location.reload()
false
else if ar.ok_reload
window.location.reload()
false
2011-09-09 22:13:02 +00:00
else
_session.setUser ar.user
2011-09-09 23:30:03 +00:00
$('#username')
.html (if _session.getUser().name then _session.getUser().name else '%username%')
_session.displayAuthorizedContent()
2011-09-09 22:13:02 +00:00
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 {}
_session.hideAuthorizedContent()
2011-09-09 22:13:02 +00:00
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