(
api.macros/defendpoint
:put
"/settings"
"Update Slack related settings. You must be a superuser to do this. Also updates the slack-cache.
There are 3 cases where we alter the slack channel/user cache:
1. falsy token -> clear
2. invalid token -> clear
3. truthy, valid token -> refresh "
[
_route-params
_query-params
{
:keys
[
slack-app-token
slack-files-channel
slack-bug-report-channel
]
}
:-
[
:map
[
:slack-app-token
{
:optional
true
}
[
:maybe
ms/NonBlankString
]
]
[
:slack-files-channel
{
:optional
true
}
[
:maybe
ms/NonBlankString
]
]
[
:slack-bug-report-channel
{
:optional
true
}
[
:maybe
:string
]
]
]
]
(
validation/check-has-application-permission
:setting
)
(
try
(
when
(
nil?
slack-app-token
)
(
slack/slack-app-token!
nil
)
(
slack/clear-channel-cache!
)
)
(
when
(
nil?
slack-files-channel
)
(
slack/slack-files-channel!
"metabase_files"
)
)
(
when
(
and
slack-app-token
(
not
config/is-test?
)
(
not
(
slack/valid-token?
slack-app-token
)
)
)
(
slack/clear-channel-cache!
)
(
throw
(
ex-info
(
tru
"Invalid Slack token."
)
{
:errors
{
:slack-app-token
(
tru
"invalid token"
)
}
}
)
)
)
(
slack/slack-app-token!
slack-app-token
)
(
if
slack-app-token
(
do
(
slack/slack-token-valid?!
true
)
(
slack/slack-token!
nil
)
(
slack/refresh-channels-and-usernames-when-needed!
)
)
(
slack/clear-channel-cache!
)
)
(
when
slack-files-channel
(
let
[
processed-files-channel
(
slack/process-files-channel-name
slack-files-channel
)
]
(
when-not
(
slack/channel-exists?
processed-files-channel
)
(
slack/slack-token-valid?!
false
)
(
slack/slack-app-token!
nil
)
(
slack/clear-channel-cache!
)
(
throw
(
ex-info
(
tru
"Slack channel not found."
)
{
:errors
{
:slack-files-channel
(
tru
"channel not found"
)
}
}
)
)
)
(
slack/slack-files-channel!
processed-files-channel
)
)
)
(
when
slack-bug-report-channel
(
let
[
processed-bug-channel
(
slack/process-files-channel-name
slack-bug-report-channel
)
]
(
when
(
not
(
slack/channel-exists?
processed-bug-channel
)
)
(
throw
(
ex-info
(
tru
"Slack channel not found."
)
{
:errors
{
:slack-bug-report-channel
(
tru
"channel not found"
)
}
}
)
)
)
(
slack/slack-bug-report-channel!
processed-bug-channel
)
)
)
{
:ok
true
}
(
catch
clojure.lang.ExceptionInfo
info
{
:status
400
,
:body
(
ex-data
info
)
}
)
)
)