From 940f62e1b4d14bcef7e09852c8cca06b5d8e0f16 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Sun, 31 May 2015 22:14:49 -0300 Subject: [PATCH] Create migration to add all avatars --- client/views/avatar/avatar.html | 3 - server/methods/getAvatarSuggestion.coffee | 76 ++++++++++++----------- server/startup/migrations/v0.1.2.coffee | 23 +++++++ 3 files changed, 63 insertions(+), 39 deletions(-) create mode 100644 server/startup/migrations/v0.1.2.coffee diff --git a/client/views/avatar/avatar.html b/client/views/avatar/avatar.html index 22cac8b4c9ff..b54e2e735da2 100644 --- a/client/views/avatar/avatar.html +++ b/client/views/avatar/avatar.html @@ -32,9 +32,6 @@

{{_ "avatar.Select_an_avatar"}}

{{> avatarSuggestion suggestions.avatars.google}} {{> avatarSuggestion suggestions.avatars.github}} - {{#unless suggestions.avatars.gravatar}} - {{> avatarSuggestionLogin 'gravatar'}} - {{/unless}} {{#unless suggestions.avatars.facebook}} {{> avatarSuggestionLogin 'facebook'}} {{/unless}} diff --git a/server/methods/getAvatarSuggestion.coffee b/server/methods/getAvatarSuggestion.coffee index dcb169577f1d..446a5d7b2134 100644 --- a/server/methods/getAvatarSuggestion.coffee +++ b/server/methods/getAvatarSuggestion.coffee @@ -1,3 +1,42 @@ +@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() @@ -5,39 +44,4 @@ Meteor.methods 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 diff --git a/server/startup/migrations/v0.1.2.coffee b/server/startup/migrations/v0.1.2.coffee new file mode 100644 index 000000000000..1f1b58c65bdf --- /dev/null +++ b/server/startup/migrations/v0.1.2.coffee @@ -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}} \ No newline at end of file