Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Database/Interfaces/IDefaultAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public interface IDefaultActionSetter

public bool SetAutoSendVideoActionToUser(int userId, string action, string type);

public bool SetDefaultActionIsActive(int userId, string type, bool isActive);
}

public interface IDefaultActionGetter
Expand All @@ -33,4 +34,5 @@ public interface IDefaultActionGetter

public int GetDefaultActionId(int userId, string type);
public string GetDefaultActionByUserIDAndType(int userID, string type);
public (string Action, string ActionCondition, bool IsActive) GetDefaultActionSettings(int userId, string type);
}
52 changes: 42 additions & 10 deletions Database/Repositories/SqLite/DefaultAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public SqliteDefaultActionSetter(string connectionString)
public bool SetAutoSendVideoConditionToUser(int userId, string actionCondition, string type)
{
const string query = @"
INSERT INTO DefaultUsersActions (UserId, Type, ActionCondition)
VALUES (@userId, @type, @actionCondition)
ON CONFLICT(UserId, Type)
INSERT INTO DefaultUsersActions (UserId, Type, ActionCondition, IsActive)
VALUES (@userId, @type, @actionCondition, 1)
ON CONFLICT(UserId, Type)
DO UPDATE SET ActionCondition = @actionCondition";

using var connection = new SqliteConnection(_connectionString);
Expand All @@ -70,14 +70,25 @@ ON CONFLICT(UserId, Type)
public bool SetAutoSendVideoActionToUser(int userId, string action, string type)
{
const string query = @"
INSERT INTO DefaultUsersActions (UserId, Type, Action)
VALUES (@userId, @type, @action)
ON CONFLICT(UserId, Type)
DO UPDATE SET Action = @action";
INSERT INTO DefaultUsersActions (UserId, Type, Action, IsActive)
VALUES (@userId, @type, @action, 1)
ON CONFLICT(UserId, Type)
DO UPDATE SET Action = @action, IsActive = 1";

using var connection = new SqliteConnection(_connectionString);
return connection.Execute(query, new {userId, type, action}) > 0;
}

public bool SetDefaultActionIsActive(int userId, string type, bool isActive)
{
const string query = @"
UPDATE DefaultUsersActions
SET IsActive = @isActive
WHERE UserId = @userId AND Type = @type";

using var connection = new SqliteConnection(_connectionString);
return connection.Execute(query, new {userId, type, isActive}) > 0;
}
}

public class SqliteDefaultActionGetter : IDefaultActionGetter
Expand Down Expand Up @@ -129,7 +140,7 @@ public string GetDefaultActionByUserIDAndType(int userID, string type)
try
{
using var connection = new SqliteConnection(_connectionString);

var result = connection.QueryFirstOrDefault<(string Action, string ActionCondition)>(
@"SELECT Action, ActionCondition
FROM DefaultUsersActions
Expand All @@ -138,8 +149,8 @@ FROM DefaultUsersActions
AND IsActive = 1",
new { userID, type });

return result != default
? $"{result.Action};{result.ActionCondition}"
return result != default
? $"{result.Action};{result.ActionCondition}"
: UsersAction.NO_VALUE;
}
catch (Exception ex)
Expand All @@ -148,4 +159,25 @@ FROM DefaultUsersActions
return "";
}
}

public (string Action, string ActionCondition, bool IsActive) GetDefaultActionSettings(int userId, string type)
{
try
{
using var connection = new SqliteConnection(_connectionString);

var result = connection.QueryFirstOrDefault<(string Action, string ActionCondition, bool IsActive)>(
@"SELECT Action, ActionCondition, IsActive
FROM DefaultUsersActions
WHERE UserId = @userId AND Type = @type",
new { userId, type });

return result;
}
catch (Exception ex)
{
Log.Error(ex, "An error occurred in the method {MethodName}", nameof(GetDefaultActionSettings));
return (string.Empty, string.Empty, false);
}
}
}
39 changes: 39 additions & 0 deletions Resources/texts.resx
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,42 @@ Are you sure you want to unmute them? If not, type /start</value>
<data name="DefaultActionChangedMessage" xml:space="preserve">
<value>Default action successfully changed.</value>
</data>
<data name="DefaultActionDisabledMessage" xml:space="preserve">
<value>Default action has been disabled.</value>
</data>
<data name="DefaultActionNotChangedMessage" xml:space="preserve">
<value>Default action not changed.</value>
</data>
<data name="SettingsCurrentStateText" xml:space="preserve">
<value>Current settings:</value>
</data>
<data name="SettingsActionLabel" xml:space="preserve">
<value>Action</value>
</data>
<data name="SettingsDelayLabel" xml:space="preserve">
<value>Delay</value>
</data>
<data name="SettingsStatusLabel" xml:space="preserve">
<value>Status</value>
</data>
<data name="SettingsNotConfigured" xml:space="preserve">
<value>not configured</value>
</data>
<data name="SettingsEnabled" xml:space="preserve">
<value>enabled</value>
</data>
<data name="SettingsDisabled" xml:space="preserve">
<value>disabled</value>
</data>
<data name="SettingsSecondsLabel" xml:space="preserve">
<value>sec.</value>
</data>
<data name="EnableAutoSendButtonText" xml:space="preserve">
<value>Enable Auto-Send</value>
</data>
<data name="DefaultActionGetGroupOrUserIDs" xml:space="preserve">
<value>Please specify the IDs of groups or users:</value>
</data>
<data name="DefaultActionTimeoutMessage" xml:space="preserve">
<value>⏳ Time is up, the default action will be performed...</value>
</data>
Expand Down Expand Up @@ -379,6 +412,12 @@ Are you sure you want to unmute them? If not, type /start</value>
<data name="OnlyMeButtonText" xml:space="preserve">
<value>Only Me</value>
</data>
<data name="Enable" xml:space="preserve">
<value>Enable</value>
</data>
<data name="Disable" xml:space="preserve">
<value>Disable</value>
</data>
<data name="DisableAutoSendButtonText" xml:space="preserve">
<value>Disable Auto-Send</value>
</data>
Expand Down
33 changes: 33 additions & 0 deletions Resources/texts.ru-RU.resx
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,42 @@ ID: {0}
<data name="DefaultActionChangedMessage" xml:space="preserve">
<value>Действие по умолчанию успешно изменено.</value>
</data>
<data name="DefaultActionDisabledMessage" xml:space="preserve">
<value>Действие по умолчанию отключено.</value>
</data>
<data name="DefaultActionNotChangedMessage" xml:space="preserve">
<value>Действие по умолчанию не изменено.</value>
</data>
<data name="SettingsCurrentStateText" xml:space="preserve">
<value>Текущие настройки:</value>
</data>
<data name="SettingsActionLabel" xml:space="preserve">
<value>Действие</value>
</data>
<data name="SettingsDelayLabel" xml:space="preserve">
<value>Задержка</value>
</data>
<data name="SettingsStatusLabel" xml:space="preserve">
<value>Статус</value>
</data>
<data name="SettingsNotConfigured" xml:space="preserve">
<value>не настроено</value>
</data>
<data name="SettingsEnabled" xml:space="preserve">
<value>включено</value>
</data>
<data name="SettingsDisabled" xml:space="preserve">
<value>отключено</value>
</data>
<data name="SettingsSecondsLabel" xml:space="preserve">
<value>сек.</value>
</data>
<data name="EnableAutoSendButtonText" xml:space="preserve">
<value>Включить авто-рассылку</value>
</data>
<data name="DefaultActionGetGroupOrUserIDs" xml:space="preserve">
<value>Укажите ID групп или пользователей:</value>
</data>
<data name="DefaultActionTimeoutMessage" xml:space="preserve">
<value>⏳ Время вышло, будет выполнено действие по умолчанию...</value>
</data>
Expand Down
54 changes: 50 additions & 4 deletions TelegramBot/Handlers/ICallBackQuery/IParameterizedCallBackQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,31 @@ public class SetAutoSendTimeCommand : IBotCallbackQueryHandlers
public string Name => "user_set_auto_send_video_time_to:";

private readonly IDefaultActionSetter _defaultActionSetter;
private readonly IDefaultActionGetter _defaultActionGetter;
private readonly IUserGetter _userGetter;

public SetAutoSendTimeCommand(
IUserGetter userGetter,
IDefaultActionSetter defaultActionSetter)
IDefaultActionSetter defaultActionSetter,
IDefaultActionGetter defaultActionGetter)
{
_userGetter = userGetter;
_defaultActionSetter = defaultActionSetter;
_defaultActionGetter = defaultActionGetter;
}

public async Task ExecuteAsync(Update update, ITelegramBotClient botClient, CancellationToken ct)
{
string callbackQueryData = update.CallbackQuery!.Data!.Split(':')[1];
var chatId = update.CallbackQuery!.Message!.Chat.Id;
int userId = _userGetter.GetUserIDbyTelegramID(chatId);
bool result = Users.SetAutoSendVideoTimeToUser(chatId, callbackQueryData, _defaultActionSetter, _userGetter);

var message = result
? Config.GetResourceString("AutoSendTimeChangedMessage") + callbackQueryData
string statusText = Users.FormatSettingsStatus(_defaultActionGetter, userId);
var message = result
? Config.GetResourceString("AutoSendTimeChangedMessage") + callbackQueryData + " " + Config.GetResourceString("SettingsSecondsLabel")
: Config.GetResourceString("AutoSendTimeNotChangedMessage");
message += "\n\n" + statusText;

await CommonUtilities.SendMessage(
botClient,
Expand Down Expand Up @@ -112,6 +118,45 @@ public async Task ExecuteAsync(Update update, ITelegramBotClient botClient, Canc
{
string action = update.CallbackQuery!.Data!.Split(':')[1];
long chatId = update.CallbackQuery!.Message!.Chat.Id;
int userId = _userGetter.GetUserIDbyTelegramID(chatId);

// Handle "enable" action: re-enable existing settings without changing them
if (action == "enable")
{
bool enableResult = _defaultActionSetter.SetDefaultActionIsActive(userId, UsersActionTypes.DEFAULT_MEDIA_DISTRIBUTION, true);
string enableMessage = enableResult
? Config.GetResourceString("DefaultActionChangedMessage")
: Config.GetResourceString("DefaultActionNotChangedMessage");

string statusText = Users.FormatSettingsStatus(_defaultActionGetter, userId);
await CommonUtilities.SendMessage(
botClient,
update,
KeyboardUtils.GetReturnButtonMarkup("user_set_video_send_users"),
cancellationToken,
enableMessage + "\n\n" + statusText
);
return;
}

// Handle "off" action: disable the default action
if (action == UsersAction.OFF)
{
bool disableResult = _defaultActionSetter.SetDefaultActionIsActive(userId, UsersActionTypes.DEFAULT_MEDIA_DISTRIBUTION, false);
string disableMessage = disableResult
? Config.GetResourceString("DefaultActionDisabledMessage")
: Config.GetResourceString("DefaultActionNotChangedMessage");

string statusText = Users.FormatSettingsStatus(_defaultActionGetter, userId);
await CommonUtilities.SendMessage(
botClient,
update,
KeyboardUtils.GetReturnButtonMarkup("user_set_video_send_users"),
cancellationToken,
disableMessage + "\n\n" + statusText
);
return;
}

List<string> extendActions = new List<string>
{
Expand Down Expand Up @@ -148,12 +193,13 @@ await CommonUtilities.SendMessage(

if (result)
{
string confirmStatusText = Users.FormatSettingsStatus(_defaultActionGetter, userId);
await CommonUtilities.SendMessage(
botClient,
update,
KeyboardUtils.GetReturnButtonMarkup("user_set_video_send_users"),
cancellationToken,
Config.GetResourceString("DefaultActionChangedMessage")
Config.GetResourceString("DefaultActionChangedMessage") + "\n\n" + confirmStatusText
);
return;
}
Expand Down
39 changes: 36 additions & 3 deletions TelegramBot/Handlers/ICallBackQuery/ISettingsCallbackQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,64 @@ public async Task ExecuteAsync(Update update, ITelegramBotClient botClient, Canc

public class VideoDefaultActionsMenuCommand : IBotCallbackQueryHandlers
{
private readonly IDefaultActionGetter _defaultActionGetter;
private readonly IUserGetter _userGetter;

public VideoDefaultActionsMenuCommand(
IDefaultActionGetter defaultActionGetter,
IUserGetter userGetter)
{
_defaultActionGetter = defaultActionGetter;
_userGetter = userGetter;
}

public string Name => "video_default_actions_menu";

public async Task ExecuteAsync(Update update, ITelegramBotClient botClient, CancellationToken ct)
{
await Users.ViewVideoDefaultActionsMenu(botClient, update);
await Users.ViewVideoDefaultActionsMenu(botClient, update, _defaultActionGetter, _userGetter);
}
}

public class UserSetAutoSendVideoTimeCommand : IBotCallbackQueryHandlers
{
private readonly IDefaultActionGetter _defaultActionGetter;
private readonly IUserGetter _userGetter;

public UserSetAutoSendVideoTimeCommand(
IDefaultActionGetter defaultActionGetter,
IUserGetter userGetter)
{
_defaultActionGetter = defaultActionGetter;
_userGetter = userGetter;
}

public string Name => "user_set_auto_send_video_time";

public async Task ExecuteAsync(Update update, ITelegramBotClient botClient, CancellationToken ct)
{
await Users.ViewAutoSendVideoTimeMenu(botClient, update);
await Users.ViewAutoSendVideoTimeMenu(botClient, update, _defaultActionGetter, _userGetter);
}
}

public class UserSetVideoSendUsersCommand : IBotCallbackQueryHandlers
{
private readonly IDefaultActionGetter _defaultActionGetter;
private readonly IUserGetter _userGetter;

public UserSetVideoSendUsersCommand(
IDefaultActionGetter defaultActionGetter,
IUserGetter userGetter)
{
_defaultActionGetter = defaultActionGetter;
_userGetter = userGetter;
}

public string Name => "user_set_video_send_users";

public async Task ExecuteAsync(Update update, ITelegramBotClient botClient, CancellationToken ct)
{
await Users.ViewUsersVideoSentUsersActionsMenu(botClient, update);
await Users.ViewUsersVideoSentUsersActionsMenu(botClient, update, _defaultActionGetter, _userGetter);
}
}

Expand Down
Loading
Loading