oldhaven/app/assets/javascripts/vkontakte.coffee

92 lines
2.7 KiB
CoffeeScript
Raw Normal View History

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