2014-01-03 13:32:13 -05:00
|
|
|
GRAVATAR_URL = 'https://www.gravatar.com/'
|
|
|
|
cache = {}
|
2014-06-30 22:16:26 -04:00
|
|
|
CocoModel = require './CocoModel'
|
2014-07-09 14:23:05 -04:00
|
|
|
util = require 'lib/utils'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
module.exports = class User extends CocoModel
|
2014-06-30 22:16:26 -04:00
|
|
|
@className: 'User'
|
2014-04-22 14:11:08 -04:00
|
|
|
@schema: require 'schemas/models/user'
|
2014-06-30 22:16:26 -04:00
|
|
|
urlRoot: '/db/user'
|
2014-07-10 04:30:23 -04:00
|
|
|
notyErrors: false
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
initialize: ->
|
|
|
|
super()
|
2014-04-21 19:15:23 -04:00
|
|
|
@migrateEmails()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
isAdmin: ->
|
|
|
|
permissions = @attributes['permissions'] or []
|
|
|
|
return 'admin' in permissions
|
|
|
|
|
|
|
|
displayName: ->
|
2014-06-30 22:16:26 -04:00
|
|
|
@get('name') or 'Anoner'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
lang: ->
|
2014-06-30 22:16:26 -04:00
|
|
|
@get('preferredLanguage') or 'en-US'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-07-07 13:29:34 -04:00
|
|
|
getPhotoURL: (size=80, useJobProfilePhoto=false, useEmployerPageAvatar=false) ->
|
2014-04-10 20:54:28 -04:00
|
|
|
photoURL = if useJobProfilePhoto then @get('jobProfile')?.photoURL else null
|
|
|
|
photoURL ||= @get('photoURL')
|
|
|
|
if photoURL
|
2014-06-30 22:16:26 -04:00
|
|
|
prefix = if photoURL.search(/\?/) is -1 then '?' else '&'
|
2014-04-09 19:46:44 -04:00
|
|
|
return "#{photoURL}#{prefix}s=#{size}" if photoURL.search('http') isnt -1 # legacy
|
|
|
|
return "/file/#{photoURL}#{prefix}s=#{size}"
|
2014-07-07 13:29:34 -04:00
|
|
|
return "/db/user/#{@id}/avatar?s=#{size}&employerPageAvatar=#{useEmployerPageAvatar}"
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
@getByID = (id, properties, force) ->
|
2014-06-30 22:16:26 -04:00
|
|
|
{me} = require 'lib/auth'
|
2014-01-03 13:32:13 -05:00
|
|
|
return me if me.id is id
|
2014-06-30 22:16:26 -04:00
|
|
|
user = cache[id] or new module.exports({_id: id})
|
2014-01-03 13:32:13 -05:00
|
|
|
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
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-07-09 14:23:05 -04:00
|
|
|
# callbacks can be either success or error
|
|
|
|
@getByIDOrSlug: (idOrSlug, force, callbacks={}) ->
|
|
|
|
{me} = require 'lib/auth'
|
|
|
|
isID = util.isID idOrSlug
|
|
|
|
if me.id is idOrSlug or me.slug is idOrSlug
|
|
|
|
callbacks.success me if callbacks.success?
|
|
|
|
return me
|
|
|
|
cached = cache[idOrSlug]
|
|
|
|
user = cached or new @ _id: idOrSlug
|
|
|
|
if force or not cached
|
|
|
|
user.loading = true
|
|
|
|
user.fetch
|
|
|
|
success: ->
|
|
|
|
user.loading = false
|
|
|
|
Backbone.Mediator.publish 'user:fetched'
|
|
|
|
callbacks.success user if callbacks.success?
|
|
|
|
error: ->
|
|
|
|
user.loading = false
|
|
|
|
callbacks.error user if callbacks.error?
|
|
|
|
cache[idOrSlug] = user
|
|
|
|
user
|
|
|
|
|
2014-07-10 14:50:16 -04:00
|
|
|
@getUnconflictedName: (name, done) ->
|
|
|
|
$.ajax "/auth/name/#{name}",
|
|
|
|
success: (data) -> done data.name
|
|
|
|
statusCode: 409: (data) ->
|
|
|
|
response = JSON.parse data.responseText
|
|
|
|
done response.name
|
|
|
|
|
2014-04-21 19:15:23 -04:00
|
|
|
getEnabledEmails: ->
|
|
|
|
@migrateEmails()
|
|
|
|
emails = _.clone(@get('emails')) or {}
|
|
|
|
emails = _.defaults emails, @schema().properties.emails.default
|
|
|
|
(emailName for emailName, emailDoc of emails when emailDoc.enabled)
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-04-21 19:15:23 -04:00
|
|
|
setEmailSubscription: (name, enabled) ->
|
|
|
|
newSubs = _.clone(@get('emails')) or {}
|
|
|
|
(newSubs[name] ?= {}).enabled = enabled
|
|
|
|
@set 'emails', newSubs
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-04-21 19:15:23 -04:00
|
|
|
emailMap:
|
|
|
|
announcement: 'generalNews'
|
|
|
|
developer: 'archmageNews'
|
|
|
|
tester: 'adventurerNews'
|
|
|
|
level_creator: 'artisanNews'
|
|
|
|
article_editor: 'scribeNews'
|
|
|
|
translator: 'diplomatNews'
|
|
|
|
support: 'ambassadorNews'
|
|
|
|
notification: 'anyNotes'
|
|
|
|
|
|
|
|
migrateEmails: ->
|
|
|
|
return if @attributes.emails or not @attributes.emailSubscriptions
|
|
|
|
oldSubs = @get('emailSubscriptions') or []
|
|
|
|
newSubs = {}
|
2014-06-30 22:16:26 -04:00
|
|
|
newSubs[newSubName] = {enabled: oldSubName in oldSubs} for oldSubName, newSubName of @emailMap
|
2014-04-21 19:15:23 -04:00
|
|
|
@set('emails', newSubs)
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-04-22 14:11:10 -04:00
|
|
|
isEmailSubscriptionEnabled: (name) -> (@get('emails') or {})[name]?.enabled
|
2014-05-26 12:21:56 -04:00
|
|
|
|
|
|
|
a = 5
|
|
|
|
b = 40
|
|
|
|
|
2014-05-31 17:19:55 -04:00
|
|
|
# y = a * ln(1/b * (x + b)) + 1
|
2014-05-26 12:21:56 -04:00
|
|
|
@levelFromExp: (xp) ->
|
2014-05-31 17:19:55 -04:00
|
|
|
if xp > 0 then Math.floor(a * Math.log((1/b) * (xp + b))) + 1 else 1
|
2014-05-26 12:21:56 -04:00
|
|
|
|
2014-05-31 17:19:55 -04:00
|
|
|
# x = (e^((y-1)/a) - 1) * b
|
2014-05-26 12:21:56 -04:00
|
|
|
@expForLevel: (level) ->
|
2014-05-31 17:19:55 -04:00
|
|
|
Math.ceil((Math.exp((level - 1)/ a) - 1) * b)
|
2014-05-26 12:21:56 -04:00
|
|
|
|
|
|
|
level: ->
|
2014-05-31 17:19:55 -04:00
|
|
|
User.levelFromExp(@get('points'))
|
|
|
|
|
|
|
|
levelFromExp: (xp) -> User.levelFromExp(xp)
|
|
|
|
|
|
|
|
expForLevel: (level) -> User.expForLevel(level)
|