Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making all more coffeescrpt-y. All now runs smoothly, would still be …
…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
1 parent
f192b0b
commit 307fc39
Showing
3 changed files
with
79 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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