Skip to content

Commit

Permalink
Making all more coffeescrpt-y. All now runs smoothly, would still be …
Browse files Browse the repository at this point in the history
…nice to polish up how we get channels and users, to avoid requesting all the times and not doing it in parallel
  • Loading branch information
samuele-mattiuzzo committed Jan 10, 2016
1 parent f192b0b commit 307fc39
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 81 deletions.
111 changes: 56 additions & 55 deletions lib/post-view.coffee
@@ -1,62 +1,63 @@
request = require 'request-promise'
{SelectListView} = require 'atom-space-pen-views'
request = require 'request-promise'
{SelectListView} = require 'atom-space-pen-views'


module.exports =
module.exports =

class PostView extends SelectListView
# fetches the selected text and posts to the channel (item)
# using token
initialize: (item, token)->
super
@target = item
@token = token
@txt = @_escapeSelection
class PostView extends SelectListView
# fetches the selected text and posts to the channel (item)
# using token
initialize: (item, token)->
super
@target = item
@token = token
@txt = @_escapeSelection()

if item.type == 'user'
# we need to open an IM channel with the user before sending to him
@_postToUser(item.id)
else
@_postToChannel(item.id)

_postToChannel: (channelId) ->
request({
uri: 'https://slack.com/api/chat.postMessage',
qs: {
'token': @token,
'text': "```#{ @txt }```",
'channel': channelId,
'as_user': true,
'parse': 'full'
},
json: true })
.then( (body)=>
if body['ok'] == false
# handle error message
console.log body['error']
if item.type == 'user'
# we need to open an IM channel with the user before sending to him
@_postToUser item.id
else
# handle success message
)
.catch( (err) => console.log err )
@_postToChannel item.id

_postToUser: (userId) ->
request({
uri: 'https://slack.com/api/im.open',
qs: { 'token': @token, 'user': userId },
json: true })
.then( (body)=>
if body['ok'] == false
# handle error message
console.log body['error']
else
@_postToChannel(body['channel']['id'])
)
.catch( (err) => console.log err )
# PRIVATE METHODS
_postToChannel: (channelId) ->
request({
uri: 'https://slack.com/api/chat.postMessage',
qs: {
'token': @token,
'text': "```#{ @txt }```",
'channel': channelId,
'as_user': true,
'parse': 'full'
},
json: true })
.then( (body)=>
if body['ok'] == false
# handle error message
console.log body['error']
else
# handle success message
)
.catch( (err) => console.log err )

_postToUser: (userId) ->
request({
uri: 'https://slack.com/api/im.open',
qs: { 'token': @token, 'user': userId },
json: true })
.then( (body)=>
if body['ok'] == false
# handle error message
console.log body['error']
else
@_postToChannel body['channel']['id']
)
.catch( (err) => console.log err )

_escapeSelection: ->
editor = atom.workspace.getActivePaneItem()
txt = editor.getSelectedText()
# removes incompatible ``` from selection
# avoids breaking out of the code block
txt = txt.replace /\`\`\`/g, ''
txt
_escapeSelection: ->
editor = atom.workspace.getActivePaneItem()
txt = editor.getSelectedText()
# removes incompatible ``` from selection
# avoids breaking out of the code block
txt = txt.replace /\`\`\`/g, ''
txt
34 changes: 12 additions & 22 deletions lib/select-channel-view.coffee
Expand Up @@ -10,38 +10,30 @@ class SelectChannelView extends SelectListView
# then spawns a PostView that handles the post
initialize: (token)->
super

@token = token
@channels = []
@users = []

@_create()


viewForItem: (item) -> "<li>#{ item.name }</li>"

getFilterKey: -> "name"

confirmed: (item) -> new PostView item, @token
confirmed: (item) ->
@panel.hide()
@view = new PostView item, @token

cancelled: -> @panel.hide()

destroy: ->
@view = null
@panel = null

# PRIVATE METHODS
_create: ->
@_getChannels()

_getAllItems: ->
# loops till we have channels and users to post to,
# then quits
# needs improvement or better logic elsewhere, otherwise blocks
# atom's loading for a bit too much
if @channels.length == 0 and @users.length == 0
@_getChannels()
@_getUsers()
timer = setTimeout @_getAllItems.bind(@), 5 * 1000
else
clearTimeout timer

_getChannels: ->
request({
uri: 'https://slack.com/api/channels.list',
Expand All @@ -50,13 +42,12 @@ class SelectChannelView extends SelectListView
.then( (body)=>
if body['ok'] == false
# handle error message
console.log(body['error'])
console.log body['error']
else
console.log('got the chans')
@channels = body['channels']
@_getUsers()
)
.catch( (err) => console.log(err) )
.catch( (err) => console.log err )

_getUsers: ->
request({
Expand All @@ -66,19 +57,18 @@ class SelectChannelView extends SelectListView
.then( (body)=>
if body['ok'] == false
# handle error message
console.log(body['error'])
console.log body['error']
else
console.log('got the users')
@users = body['members']
@_drawAndSet()
)
.catch( (err) => console.log(err) )
.catch( (err) => console.log err )

_drawAndSet:->
items = []
for i in [@channels..., @users...]
[v, t] = if i.profile? then [i.profile.real_name, 'user'] else [i.name, 'channel']
items.push({id:i.id, name:v, type:t})
items.push {id:i.id, name:v, type:t}
@setItems items

@addClass 'overlay from-top'
Expand Down
15 changes: 11 additions & 4 deletions lib/select-token-view.coffee
Expand Up @@ -21,14 +21,21 @@ class SelectTokenView extends SelectListView

getFilterKey: -> "name"

confirmed: (item) -> new SelectChannelView(item.token)
confirmed: (item) ->
@view = new SelectChannelView item.token

cancelled: -> @panel.hide()
cancelled: ->
@panel.hide()

destroy: ->
@view = null
@panel = null

# PRIVATE METHODS
_createItems: ->
tokens = atom.config.get('atom-slack-snippets.tokens')
tokens = atom.config.get 'atom-slack-snippets.tokens'
items = []
for item in tokens
[n, t] = item.split "|"
items.push({'name': n, 'token': t})
items.push {'name': n, 'token': t}
items

0 comments on commit 307fc39

Please sign in to comment.