This commit is contained in:
Morten Olsen
2021-11-07 23:12:18 +01:00
parent 0c52a449b4
commit 9ee2b1c358
2 changed files with 44 additions and 41 deletions

View File

@@ -3,9 +3,8 @@ local finders = require "telescope.finders"
local actions = require "telescope.actions" local actions = require "telescope.actions"
local action_state = require "telescope.actions.state" local action_state = require "telescope.actions.state"
local conf = require("telescope.config").values local conf = require("telescope.config").values
local curl = require "plenary.curl"
local M = {}
local M = {}
local providers = {} local providers = {}
local function get_visual_selection() local function get_visual_selection()
@@ -15,50 +14,14 @@ local function get_visual_selection()
return content return content
end end
M.providers = {}
M.providers.slack = function(slack_token)
local token = slack_token
print(vim.inspect(token))
local function post(receiver, content)
local body = {
text = "```\n" .. table.concat(content, "\n") .. "\n```",
channel = receiver.id,
as_user = true,
}
local res = curl.post("https://slack.com/api/chat.postMessage", {
body = vim.fn.json_encode(body),
headers = {
Authorization = "Bearer " .. token,
content_type = "application/json"
}
})
end
local function fetch_users()
local res = curl.get("https://slack.com/api/users.list", {
headers = {
Authorization = "Bearer " .. token
}
})
local users = vim.fn.json_decode(res.body).members
return users
end
return {
fetch_users = fetch_users,
post = post,
}
end
M.config = function(opts) M.config = function(opts)
providers = opts.providers or {} providers = opts.providers or {}
end end
M.share = function(provider_name) M.share = function(provider_name, opts)
local content = get_visual_selection() local content = get_visual_selection()
local provider = providers[provider_name] local provider = providers[provider_name]
local users = provider.fetch_users() local users = provider.fetch_users(opts)
pickers.new(nil, { pickers.new(nil, {
prompt_title = "Users", prompt_title = "Users",
finder = finders.new_table { finder = finders.new_table {
@@ -76,7 +39,7 @@ M.share = function(provider_name)
actions.select_default:replace(function() actions.select_default:replace(function()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
post_to(selection.value, content) provider.post(selection.value, table.concat(content, "\n"), opts)
end) end)
return true return true
end, end,

View File

@@ -0,0 +1,40 @@
local curl = require "plenary.curl"
local M = function(slack_token)
local token = slack_token
local function post(receiver, content, opts)
local text = content
if opts.format = "markdown" then
text = "```\n" .. text .. "```"
end
local body = {
text = text,
channel = receiver.id,
as_user = true,
}
local res = curl.post("https://slack.com/api/chat.postMessage", {
body = vim.fn.json_encode(body),
headers = {
Authorization = "Bearer " .. token,
content_type = "application/json"
}
})
end
local function fetch_users()
local res = curl.get("https://slack.com/api/users.list", {
headers = {
Authorization = "Bearer " .. token
}
})
local users = vim.fn.json_decode(res.body).members
return users
end
return {
fetch_users = fetch_users,
post = post,
}
end
return M