Skip to content

Commit

Permalink
Display warning if selected token does not support selected action
Browse files Browse the repository at this point in the history
  • Loading branch information
WarmUpTill committed Sep 11, 2023
1 parent a73ccb1 commit 41e47f3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ AdvSceneSwitcher.action.twitch.type.category="Set stream category"
AdvSceneSwitcher.action.twitch.type.commercial="Start commercial with duration"
AdvSceneSwitcher.action.twitch.categorySelectionDisabled="Cannot select category without selecting a Twitch account first!"
AdvSceneSwitcher.action.twitch.entry="On{{account}}{{actions}}{{text}}{{category}}{{manualCategorySearch}}{{duration}}"
AdvSceneSwitcher.action.twitch.tokenPermissionsInsufficient="Permissions of selected token are insufficient to perform selected action!"

; Hotkey
AdvSceneSwitcher.hotkey.startSwitcherHotkey="Start the Advanced Scene Switcher"
Expand Down
42 changes: 40 additions & 2 deletions src/macro-external/twitch/macro-action-twitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ std::string MacroActionTwitch::GetShortDesc() const
return GetWeakTwitchTokenName(_token);
}

bool MacroActionTwitch::ActionIsSupportedByToken()
{
static const std::unordered_map<Action, TokenOption> requiredOption = {
{Action::TITLE, {"channel:manage:broadcast"}},
{Action::CATEGORY, {"channel:manage:broadcast"}},
{Action::COMMERCIAL, {"channel:edit:commercial"}},
};
auto token = _token.lock();
if (!token) {
return false;
}
auto option = requiredOption.find(_action);
assert(option != requiredOption.end());
return token->OptionIsEnabled(option->second);
}

static inline void populateActionSelection(QComboBox *list)
{
for (const auto &[_, name] : actionTypes) {
Expand All @@ -169,7 +185,9 @@ MacroActionTwitchEdit::MacroActionTwitchEdit(
_category(new TwitchCategorySelection(this)),
_manualCategorySearch(new TwitchCategorySearchButton()),
_duration(new DurationSelection(this, false, 0)),
_layout(new QHBoxLayout())
_layout(new QHBoxLayout()),
_tokenPermissionWarning(new QLabel(obs_module_text(
"AdvSceneSwitcher.action.twitch.tokenPermissionsInsufficient")))
{
_text->setSizePolicy(QSizePolicy::MinimumExpanding,
QSizePolicy::Preferred);
Expand All @@ -190,6 +208,8 @@ MacroActionTwitchEdit::MacroActionTwitchEdit(
SLOT(CategoreyChanged(const TwitchCategory &)));
QObject::connect(_duration, SIGNAL(DurationChanged(const Duration &)),
this, SLOT(DurationChanged(const Duration &)));
QWidget::connect(&_tokenPermissionCheckTimer, SIGNAL(timeout()), this,
SLOT(CheckTokenPermissions()));

PlaceWidgets(obs_module_text("AdvSceneSwitcher.action.twitch.entry"),
_layout,
Expand All @@ -199,7 +219,14 @@ MacroActionTwitchEdit::MacroActionTwitchEdit(
{"{{category}}", _category},
{"{{manualCategorySearch}}", _manualCategorySearch},
{"{{duration}}", _duration}});
setLayout(_layout);
_layout->setContentsMargins(0, 0, 0, 0);

auto mainLayout = new QVBoxLayout();
mainLayout->addLayout(_layout);
mainLayout->addWidget(_tokenPermissionWarning);
setLayout(mainLayout);

_tokenPermissionCheckTimer.start(1000);

_entryData = entryData;
UpdateEntryData();
Expand Down Expand Up @@ -250,6 +277,14 @@ void MacroActionTwitchEdit::DurationChanged(const Duration &duration)
_entryData->_duration = duration;
}

void MacroActionTwitchEdit::CheckTokenPermissions()
{
_tokenPermissionWarning->setVisible(
_entryData && !_entryData->ActionIsSupportedByToken());
adjustSize();
updateGeometry();
}

void MacroActionTwitchEdit::SetupWidgetVisibility()
{
_text->setVisible(_entryData->_action ==
Expand All @@ -266,6 +301,9 @@ void MacroActionTwitchEdit::SetupWidgetVisibility()
AddStretchIfNecessary(_layout);
}

_tokenPermissionWarning->setVisible(
!_entryData->ActionIsSupportedByToken());

adjustSize();
updateGeometry();
}
Expand Down
4 changes: 4 additions & 0 deletions src/macro-external/twitch/macro-action-twitch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MacroActionTwitch : public MacroAction {
{
return std::make_shared<MacroActionTwitch>(m);
}
bool ActionIsSupportedByToken();

enum class Action {
TITLE,
Expand Down Expand Up @@ -65,6 +66,7 @@ private slots:
void TextChanged();
void CategoreyChanged(const TwitchCategory &);
void DurationChanged(const Duration &);
void CheckTokenPermissions();

signals:
void HeaderInfoChanged(const QString &);
Expand All @@ -82,6 +84,8 @@ private slots:
TwitchCategorySearchButton *_manualCategorySearch;
DurationSelection *_duration;
QHBoxLayout *_layout;
QLabel *_tokenPermissionWarning;
QTimer _tokenPermissionCheckTimer;
bool _loading = true;
};

Expand Down
20 changes: 17 additions & 3 deletions src/macro-external/twitch/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ std::string TokenOption::GetLocale() const
return apiIdToLocale.at(apiId);
}

const std::unordered_map<std::string, std::string> &TokenOption::GetTokenMap()
const std::unordered_map<std::string, std::string> &
TokenOption::GetTokenOptionMap()
{
return apiIdToLocale;
}
Expand Down Expand Up @@ -329,8 +330,10 @@ TwitchTokenSettingsDialog::TwitchTokenSettingsDialog(
row = 0;
auto optionsBox = new QGroupBox(
obs_module_text("AdvSceneSwitcher.twitchToken.permissions"));
for (const auto &[id, _] : TokenOption::GetTokenMap()) {
for (const auto &[id, _] : TokenOption::GetTokenOptionMap()) {
auto checkBox = addOption({id}, settings, optionsGrid, row);
QWidget::connect(checkBox, SIGNAL(stateChanged(int)), this,
SLOT(TokenOptionChanged(int)));
_optionWidgets[id] = checkBox;
}
MinimizeSizeOfColumn(optionsGrid, 0);
Expand Down Expand Up @@ -359,7 +362,7 @@ TwitchTokenSettingsDialog::TwitchTokenSettingsDialog(
}
HideToken();

if (_name->text() == "") {
if (_name->text().isEmpty()) {
PulseWidget(_requestToken, Qt::green, QColor(0, 0, 0, 0), true);
}

Expand All @@ -378,6 +381,17 @@ void TwitchTokenSettingsDialog::HideToken()
_currentTokenValue->setEchoMode(QLineEdit::PasswordEchoOnEdit);
}

void TwitchTokenSettingsDialog::TokenOptionChanged(int)
{
if (!_name->text().isEmpty()) {
PulseWidget(_requestToken, Qt::green, QColor(0, 0, 0, 0), true);
}
_name->setText("");
QMetaObject::invokeMethod(this, "NameChanged",
Q_ARG(const QString &, ""));
_currentTokenValue->setText("");
}

static std::string generateStateString()
{
const char *chars =
Expand Down
3 changes: 2 additions & 1 deletion src/macro-external/twitch/token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TokenOption {
std::string GetLocale() const;

static const std::unordered_map<std::string, std::string> &
GetTokenMap();
GetTokenOptionMap();
bool operator<(const TokenOption &other) const;
std::string apiId = "";

Expand Down Expand Up @@ -90,6 +90,7 @@ class TwitchTokenSettingsDialog : public ItemSettingsDialog {
private slots:
void ShowToken();
void HideToken();
void TokenOptionChanged(int);
void RequestToken();
void GotToken(const std::optional<QString> &);

Expand Down

0 comments on commit 41e47f3

Please sign in to comment.