Skip to content

Commit

Permalink
Merge pull request #775 from RocketChat/soonahn-user-avatar-in-desktop
Browse files Browse the repository at this point in the history
Avatar image in notification
  • Loading branch information
engelgabriel committed Sep 12, 2015
2 parents 0720c37 + a56544f commit 0a8af6a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
14 changes: 14 additions & 0 deletions client/lib/avatar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@

Blaze.registerHelper 'avatarUrlFromUsername', getAvatarUrlFromUsername

@getAvatarAsPng = (username, cb) ->
image = new Image
image.src = getAvatarUrlFromUsername(username)

image.onload = ->
canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
context = canvas.getContext('2d')
context.drawImage(image, 0, 0)
cb canvas.toDataURL('image/png')
image.onerror = ->
cb ''

@updateAvatarOfUsername = (username) ->
key = "avatar_random_#{username}"
Session.set key, Math.round(Math.random() * 1000)
Expand Down
33 changes: 17 additions & 16 deletions client/lib/notification.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@
showDesktop: (notification) ->
if not window.document.hasFocus?() and Meteor.user().status isnt 'busy'
if window.Notification && Notification.permission == "granted"
n = new Notification notification.title,
icon: '/images/logo/1024x1024-circle.png'
body: _.stripTags(notification.text)
getAvatarAsPng notification.payload.sender.username, (avatarImage) ->
n = new Notification notification.title,
icon: avatarImage
body: _.stripTags(notification.text)

if notification.payload?.rid?
n.onclick = ->
window.focus()
switch notification.payload.type
when 'd'
FlowRouter.go 'direct', {username: notification.payload.sender.username}
when 'c'
FlowRouter.go 'channel', {name: notification.payload.name}
when 'p'
FlowRouter.go 'group', {name: notification.payload.name}
if notification.payload?.rid?
n.onclick = ->
window.focus()
switch notification.payload.type
when 'd'
FlowRouter.go 'direct', {username: notification.payload.sender.username}
when 'c'
FlowRouter.go 'channel', {name: notification.payload.name}
when 'p'
FlowRouter.go 'group', {name: notification.payload.name}

setTimeout ->
n.close()
, 10000
setTimeout ->
n.close()
, 10000

newMessage: ->
unless Session.equals('user_' + Meteor.userId() + '_status', 'busy') or Meteor.user()?.settings?.preferences?.disableNewMessageNotification
Expand Down
4 changes: 2 additions & 2 deletions server/startup/avatar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ Meteor.startup ->
</text>
</svg>
"""

res.write svg
res.end()
return
return

res.setHeader 'content-type', 'image/jpeg'
res.setHeader 'Content-Length', file.length
Expand Down

0 comments on commit 0a8af6a

Please sign in to comment.