2014-02-12 05:23:40 -05:00
|
|
|
define [
|
2014-03-07 06:24:11 -05:00
|
|
|
"libs/md5"
|
2014-02-12 05:23:40 -05:00
|
|
|
"libs/backbone"
|
|
|
|
], () ->
|
2014-03-07 06:24:11 -05:00
|
|
|
User = Backbone.Model.extend {
|
|
|
|
gravatarUrl: (size = 32) ->
|
|
|
|
email = @get("email").trim().toLowerCase()
|
|
|
|
hash = CryptoJS.MD5(email)
|
|
|
|
return "//www.gravatar.com/avatar/#{hash}.jpg?size=#{size}&d=mm"
|
2014-03-07 07:00:31 -05:00
|
|
|
|
|
|
|
OWNER_HUE: 200
|
|
|
|
hue: () ->
|
|
|
|
if window.user.id == @get("id")
|
|
|
|
hue = @OWNER_HUE
|
|
|
|
else
|
|
|
|
hash = CryptoJS.MD5(@get("id"))
|
|
|
|
hue = parseInt(hash.toString().slice(0,8), 16) % 320
|
|
|
|
# Avoid 20 degrees either side of the owner
|
|
|
|
if hue > @OWNER_HUE - 20
|
|
|
|
hue = hue + 40
|
|
|
|
return hue
|
|
|
|
|
|
|
|
name: () ->
|
2014-03-07 07:54:08 -05:00
|
|
|
if window.user.id == @get("id")
|
|
|
|
return "you"
|
2014-03-07 07:00:31 -05:00
|
|
|
parts = []
|
|
|
|
first_name = @get("first_name")
|
|
|
|
if first_name? and first_name.length > 0
|
|
|
|
parts.push first_name
|
|
|
|
last_name = @get("last_name")
|
|
|
|
if last_name? and last_name.length > 0
|
|
|
|
parts.push last_name
|
|
|
|
return parts.join(" ")
|
|
|
|
|
2014-03-07 06:24:11 -05:00
|
|
|
}, {
|
2014-02-12 05:23:40 -05:00
|
|
|
findOrBuild : (id, attributes) ->
|
|
|
|
model = @find id
|
|
|
|
if !model?
|
|
|
|
model = @build id
|
|
|
|
model.set model.parse attributes
|
|
|
|
return model
|
|
|
|
|
|
|
|
build: (id) ->
|
|
|
|
model = new this(id : id)
|
|
|
|
@loadedModel ||= {}
|
|
|
|
@loadedModel[id] = model
|
|
|
|
return model
|
|
|
|
|
|
|
|
find: (id) ->
|
|
|
|
@loadedModel ||= {}
|
|
|
|
return @loadedModel[id]
|
2014-03-11 08:13:46 -04:00
|
|
|
|
|
|
|
getAnonymousUser: () ->
|
|
|
|
return User.findOrBuild("anonymous", { first_name: "Anonymous", email: "anon@sharelatex.com" })
|
2014-02-12 05:23:40 -05:00
|
|
|
}
|