codecombat/app/models/User.coffee

103 lines
3 KiB
CoffeeScript
Raw Normal View History

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'
@schema: require 'schemas/models/user'
2014-06-30 22:16:26 -04:00
urlRoot: '/db/user'
notyErrors: false
2014-01-03 13:32:13 -05:00
defaults:
points: 0
2014-01-03 13:32:13 -05:00
initialize: ->
super()
@migrateEmails()
2014-01-03 13:32:13 -05:00
onLoaded: ->
CocoModel.pollAchievements() # Check for achievements on login
super arguments...
2014-01-03 13:32:13 -05:00
isAdmin: ->
permissions = @attributes['permissions'] or []
return 'admin' in permissions
isAnonymous: ->
@get 'anonymous'
2014-01-03 13:32:13 -05:00
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
getPhotoURL: (size=80, useJobProfilePhoto=false, useEmployerPageAvatar=false) ->
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 '&'
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}&employerPageAvatar=#{useEmployerPageAvatar}"
2014-01-03 13:32:13 -05:00
getSlugOrID: -> @get('slug') or @get('_id')
2014-07-16 13:51:44 -04:00
set: ->
if arguments[0] is 'jobProfileApproved' and @get("jobProfileApproved") is false and not @get("jobProfileApprovedDate")
@set "jobProfileApprovedDate", (new Date()).toISOString()
super arguments...
2014-06-30 22:16:26 -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
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
setEmailSubscription: (name, enabled) ->
newSubs = _.clone(@get('emails')) or {}
(newSubs[name] ?= {}).enabled = enabled
@set 'emails', newSubs
2014-06-30 22:16:26 -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
@set('emails', newSubs)
2014-06-30 22:16:26 -04:00
isEmailSubscriptionEnabled: (name) -> (@get('emails') or {})[name]?.enabled
a = 5
b = 100
c = b
# y = a * ln(1/b * (x + c)) + 1
@levelFromExp: (xp) ->
if xp > 0 then Math.floor(a * Math.log((1/b) * (xp + c))) + 1 else 1
# x = b * e^((y-1)/a) - c
@expForLevel: (level) ->
if level > 1 then Math.ceil Math.exp((level - 1)/ a) * b - c else 0
level: ->
User.levelFromExp(@get('points'))