Skip to content

Commit

Permalink
Merge pull request #85 from RocketChat/features/avatar
Browse files Browse the repository at this point in the history
Features/avatar
  • Loading branch information
engelgabriel committed Jun 1, 2015
2 parents 32c5efc + 3ea8962 commit 566331b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 39 deletions.
3 changes: 0 additions & 3 deletions client/views/avatar/avatar.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ <h2>{{_ "avatar.Select_an_avatar"}}</h2>
{{> avatarSuggestion suggestions.avatars.google}}
{{> avatarSuggestion suggestions.avatars.github}}

{{#unless suggestions.avatars.gravatar}}
{{> avatarSuggestionLogin 'gravatar'}}
{{/unless}}
{{#unless suggestions.avatars.facebook}}
{{> avatarSuggestionLogin 'facebook'}}
{{/unless}}
Expand Down
76 changes: 40 additions & 36 deletions server/methods/getAvatarSuggestion.coffee
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
@getAvatarSuggestionForUser = (user) ->
avatars = []

if user.services.facebook?.id?
avatars.push
service: 'facebook'
url: "https://graph.facebook.com/#{user.services.facebook.id}/picture?type=large"

if user.services.google?.picture? and user.services.google.picture isnt "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg"
avatars.push
service: 'google'
url: user.services.google.picture

if user.services.github?.username?
avatars.push
service: 'github'
url: "https://avatars.githubusercontent.com/#{user.services.github.username}?s=200"

if user.emails?.length > 0
for email in user.emails when email.verified is true
avatars.push
service: 'gravatar'
url: Gravatar.imageUrl(email.address, {default: '404', size: 200, secure: true})

validAvatars = {}
for avatar in avatars
try
result = HTTP.get avatar.url, npmRequestOptions: {encoding: 'binary'}
if result.statusCode is 200
blob = "data:#{result.headers['content-type']};base64,"
blob += Buffer(result.content, 'binary').toString('base64')
avatar.blob = blob
validAvatars[avatar.service] = avatar
catch e
# ...

return validAvatars


Meteor.methods
getAvatarSuggestion: ->
if not Meteor.userId()
throw new Meteor.Error 203, '[methods] typingStatus -> Usuário não logado'

user = Meteor.user()

avatars = []

if user.services.facebook?.id?
avatars.push
service: 'facebook'
url: "https://graph.facebook.com/#{user.services.facebook.id}/picture?type=large"

if user.services.google?.picture?
avatars.push
service: 'google'
url: user.services.google.picture

if user.services.github?.username?
avatars.push
service: 'github'
url: "https://avatars.githubusercontent.com/#{user.services.github.username}?s=200"

if user.emails?.length > 0
for email in user.emails when email.verified is true
avatars.push
service: 'gravatar'
url: Gravatar.imageUrl email.address

validAvatars = {}
for avatar in avatars
try
result = HTTP.get avatar.url, npmRequestOptions: {encoding: null}
if result.statusCode is 200
blob = "data:#{result.headers['content-type']};base64,"
blob += Buffer(result.content, 'binary').toString('base64')
avatar.blob = blob
validAvatars[avatar.service] = avatar
catch e
# ...

return validAvatars
getAvatarSuggestionForUser user
23 changes: 23 additions & 0 deletions server/startup/migrations/v0.1.2.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Meteor.startup ->
Migrations.add
version: new Date("2015-06-01T00:26:05.197Z").getTime()
up: ->
Meteor.users.find({avatarOrigin: {$exists: false}, username: {$exists: true}}).forEach (user) ->
avatars = getAvatarSuggestionForUser user

services = Object.keys avatars

if services.length is 0
return

service = services[0]
console.log user.username, '->', service

blob = avatars[service].blob

file = new FS.File blob
file.attachData blob, ->
file.name user.username

Avatars.insert file, (err, fileObj) ->
Meteor.users.update {_id: user._id}, {$set: {avatarOrigin: service}}

0 comments on commit 566331b

Please sign in to comment.