-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from RocketChat/features/avatar
Features/avatar
- Loading branch information
Showing
3 changed files
with
63 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |