2014-01-03 13:32:13 -05:00
|
|
|
GRAVATAR_URL = 'https://www.gravatar.com/'
|
|
|
|
cache = {}
|
|
|
|
CocoModel = require('./CocoModel')
|
|
|
|
|
|
|
|
module.exports = class User extends CocoModel
|
|
|
|
@className: "User"
|
|
|
|
urlRoot: "/db/user"
|
|
|
|
|
|
|
|
initialize: ->
|
|
|
|
super()
|
|
|
|
|
|
|
|
isAdmin: ->
|
|
|
|
permissions = @attributes['permissions'] or []
|
|
|
|
return 'admin' in permissions
|
|
|
|
|
|
|
|
displayName: ->
|
2014-04-09 19:46:44 -04:00
|
|
|
@get('name') or "Anoner"
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
lang: ->
|
|
|
|
@get('preferredLanguage') or "en-US"
|
|
|
|
|
2014-04-09 19:46:44 -04:00
|
|
|
getPhotoURL: (size=80) ->
|
|
|
|
if photoURL = @get('photoURL')
|
|
|
|
prefix = if photoURL.search(/\?/) is -1 then "?" else "&"
|
|
|
|
return "#{photoURL}#{prefix}s=#{size}" if photoURL.search('http') isnt -1 # legacy
|
|
|
|
return "/file/#{photoURL}#{prefix}s=#{size}"
|
|
|
|
return "/db/user/#{@id}/avatar?s=#{size}"
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
@getByID = (id, properties, force) ->
|
|
|
|
{me} = require('lib/auth')
|
|
|
|
return me if me.id is id
|
|
|
|
user = cache[id] or new module.exports({_id:id})
|
|
|
|
if force or not cache[id]
|
|
|
|
user.loading = true
|
|
|
|
user.fetch(
|
|
|
|
success: ->
|
|
|
|
user.loading = false
|
|
|
|
Backbone.Mediator.publish('user:fetched')
|
2014-04-09 21:10:30 -04:00
|
|
|
#user.trigger 'sync' # needed?
|
2014-01-03 13:32:13 -05:00
|
|
|
)
|
|
|
|
cache[id] = user
|
|
|
|
user
|