diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/IWebPageResponseHook.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/IWebPageResponseHook.cs new file mode 100644 index 000000000..e8ec7494d --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/IWebPageResponseHook.cs @@ -0,0 +1,9 @@ +using BotSharp.Abstraction.Browsing.Models; + +namespace BotSharp.Abstraction.Browsing; + +public interface IWebPageResponseHook +{ + void OnDataFetched(MessageInfo message, string url, string postData, string responsData); + T? GetResponse(MessageInfo message, string url); +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs index 8bf09cb10..dcbe66f78 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs @@ -12,13 +12,6 @@ public class PageActionArgs public string Url { get; set; } = null!; public bool OpenNewTab { get; set; } = false; - public bool WaitForNetworkIdle = true; + public bool WaitForNetworkIdle { get; set; } = true; public float? Timeout { get; set; } - - /// - /// On page data fetched - /// - public DataFetched? OnDataFetched { get; set; } } - -public delegate void DataFetched(string url, string data); diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index d85b3cae8..0af440807 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -25,6 +25,8 @@ public interface IBotSharpRepository void UpdateUserVerified(string userId) => throw new NotImplementedException(); void UpdateUserVerificationCode(string userId, string verficationCode) => throw new NotImplementedException(); void UpdateUserPassword(string userId, string password) => throw new NotImplementedException(); + void UpdateUserEmail(string userId, string email)=> throw new NotImplementedException(); + void UpdateUserPhone(string userId, string Iphone) => throw new NotImplementedException(); #endregion #region Agent diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs index 02a7dd9e3..f35656ca2 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs @@ -14,4 +14,6 @@ public interface IUserService Task VerifyEmailExisting(string email); Task SendVerificationCodeResetPassword(User user); Task ResetUserPassword(User user); + Task ModifyUserEmail(string email); + Task ModifyUserPhone(string phone); } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 5fd9bc4cc..75ab61854 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -170,7 +170,7 @@ - + diff --git a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs index 2dd2dc7f9..61c45c585 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs @@ -28,7 +28,7 @@ public static IServiceCollection UsingSqlServer(this IServiceCollection services services.AddScoped(sp => { var myDatabaseSettings = sp.GetRequiredService(); - return DataContextHelper.GetDbContext(myDatabaseSettings, sp); + return DataContextHelper.GetSqlServerDbContext(myDatabaseSettings, sp); }); return services; diff --git a/src/Infrastructure/BotSharp.Core/Repository/DataContextHelper.cs b/src/Infrastructure/BotSharp.Core/Repository/DataContextHelper.cs index edcbced86..806e985f4 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/DataContextHelper.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/DataContextHelper.cs @@ -1,14 +1,12 @@ using Microsoft.Data.SqlClient; -using MySqlConnector; using System.Data.Common; namespace BotSharp.Core.Repository; public static class DataContextHelper { - public static T GetDbContext(BotSharpDatabaseSettings settings, IServiceProvider serviceProvider) + public static T GetSqlServerDbContext(BotSharpDatabaseSettings settings, IServiceProvider serviceProvider) where T : Database, new() - where Tdb : DataContext { if (settings.Assemblies == null) throw new Exception("Please set assemblies."); @@ -18,32 +16,40 @@ public static T GetDbContext(BotSharpDatabaseSettings settings, IService var dc = new T(); if (typeof(T) == typeof(BotSharpDbContext)) { - if (typeof(Tdb).Name.StartsWith("DbContext4SqlServer")) + dc.BindDbContext(new DatabaseBind { - dc.BindDbContext(new DatabaseBind - { - ServiceProvider = serviceProvider, - MasterConnection = new SqlConnection(settings.BotSharp.Master), - SlaveConnections = settings.BotSharp.Slavers.Length == 0 ? - new List { new SqlConnection(settings.BotSharp.Master) } : - settings.BotSharp.Slavers.Select(x => new SqlConnection(x) as DbConnection) - .ToList(), - CreateDbIfNotExist = true - }); - } - else if (typeof(Tdb).Name.StartsWith("DbContext4Aurora") || - typeof(Tdb).Name.StartsWith("DbContext4MySql")) - { - dc.BindDbContext(new DatabaseBind - { - ServiceProvider = serviceProvider, - MasterConnection = new MySqlConnection(settings.BotSharp.Master), - SlaveConnections = settings.BotSharp.Slavers - .Select(x => new MySqlConnection(x) as DbConnection).ToList(), - CreateDbIfNotExist = true - }); - } + ServiceProvider = serviceProvider, + MasterConnection = new SqlConnection(settings.BotSharp.Master), + SlaveConnections = settings.BotSharp.Slavers.Length == 0 ? + new List { new SqlConnection(settings.BotSharp.Master) } : + settings.BotSharp.Slavers.Select(x => new SqlConnection(x) as DbConnection) + .ToList(), + CreateDbIfNotExist = true + }); } return dc; } + + /*public static T GetMySqlDbContext(BotSharpDatabaseSettings settings, IServiceProvider serviceProvider) + where T : Database, new() + { + if (settings.Assemblies == null) + throw new Exception("Please set assemblies."); + + AppDomain.CurrentDomain.SetData("Assemblies", settings.Assemblies); + + var dc = new T(); + if (typeof(T) == typeof(BotSharpDbContext)) + { + dc.BindDbContext(new DatabaseBind + { + ServiceProvider = serviceProvider, + MasterConnection = new MySqlConnection(settings.BotSharp.Master), + SlaveConnections = settings.BotSharp.Slavers + .Select(x => new MySqlConnection(x) as DbConnection).ToList(), + CreateDbIfNotExist = true + }); + } + return dc; + }*/ } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs index ed953c178..78bd5cc0f 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs @@ -1,7 +1,6 @@ using System.IO; using FunctionDef = BotSharp.Abstraction.Functions.Models.FunctionDef; using BotSharp.Abstraction.Users.Models; -using MongoDB.Driver; using System.Text.Encodings.Web; using BotSharp.Abstraction.Plugins.Models; using BotSharp.Abstraction.Statistics.Settings; diff --git a/src/Infrastructure/BotSharp.Core/Routing/Planning/SequentialPlanner.cs b/src/Infrastructure/BotSharp.Core/Routing/Planning/SequentialPlanner.cs index b6f613e0a..ac201f042 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Planning/SequentialPlanner.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Planning/SequentialPlanner.cs @@ -1,12 +1,7 @@ -using Amazon.SecurityToken.Model.Internal.MarshallTransformations; -using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.MLTasks; -using BotSharp.Abstraction.Routing; using BotSharp.Abstraction.Routing.Models; using BotSharp.Abstraction.Routing.Planning; using BotSharp.Abstraction.Templating; -using System.Drawing; namespace BotSharp.Core.Routing.Planning; diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs index e289e1bcf..a8eaac5e7 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs @@ -20,7 +20,7 @@ public string Id [JsonPropertyName("user_name")] public string UserName - => _claims?.FirstOrDefault(x => x.Type == "name")?.Value!; + => _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value!; public string Email => _claims?.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value!; diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index cc6e98461..f643f9c58 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -364,4 +364,33 @@ public async Task ResetUserPassword(User user) db.UpdateUserPassword(record.Id, newPassword); return true; } + + public async Task ModifyUserEmail(string email) + { + var curUser = await GetMyProfile(); + var db = _services.GetRequiredService(); + var record = db.GetUserById(curUser.Id); + if (record == null) + { + return false; + } + + db.UpdateUserEmail(record.Id, email); + return true; + } + + public async Task ModifyUserPhone(string phone) + { + var curUser = await GetMyProfile(); + var db = _services.GetRequiredService(); + var record = db.GetUserById(curUser.Id); + + if (record == null) + { + return false; + } + + db.UpdateUserPhone(record.Id, phone); + return true; + } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs index 0b914635c..de07a4821 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs @@ -1,9 +1,5 @@ using BotSharp.Abstraction.Loggers.Models; using Microsoft.AspNetCore.Hosting; -using SharpCompress.Compressors.Xz; -using System; -using System.IO; -using System.Net.Mime; namespace BotSharp.OpenAPI.Controllers; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs index f260fd333..03b33a955 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs @@ -121,6 +121,18 @@ public async Task ResetUserPassword([FromBody] UserResetPasswordModel user return await _userService.ResetUserPassword(user.ToUser()); } + [HttpPost("/user/email/modify")] + public async Task ModifyUserEmail([FromQuery] string email) + { + return await _userService.ModifyUserEmail(email); + } + + [HttpPost("/user/phone/modify")] + public async Task ModifyUserPhone([FromQuery] string phone) + { + return await _userService.ModifyUserPhone(phone); + } + #region Avatar [HttpPost("/user/avatar")] public bool UploadUserAvatar([FromBody] BotSharpFile file) diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs index 4f9e50e41..17c9d0cd8 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs @@ -72,4 +72,20 @@ public void UpdateUserPassword(string userId, string password) .Set(x => x.UpdatedTime, DateTime.UtcNow); _dc.Users.UpdateOne(filter, update); } + + public void UpdateUserEmail(string userId, string email) + { + var filter = Builders.Filter.Eq(x => x.Id, userId); + var update = Builders.Update.Set(x => x.Email, email) + .Set(x => x.UpdatedTime, DateTime.UtcNow); + _dc.Users.UpdateOne(filter, update); + } + + public void UpdateUserPhone(string userId, string phone) + { + var filter = Builders.Filter.Eq(x => x.Id, userId); + var update = Builders.Update.Set(x => x.Phone, phone) + .Set(x => x.UpdatedTime, DateTime.UtcNow); + _dc.Users.UpdateOne(filter, update); + } } diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs index 94607662f..e09caa7b1 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs @@ -1,4 +1,3 @@ -using Amazon.Runtime.Internal.Transform; using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.MLTasks; using BotSharp.Core.Infrastructures; diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj b/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj index 892c3ca9e..fcaac28e4 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj +++ b/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj @@ -17,7 +17,7 @@ - + diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index a17169295..ebf1e5246 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -93,9 +93,9 @@ public async Task InitContext(string ctxId, BrowserActionArgs a return _contexts[ctxId]; } - public async Task NewPage(string ctxId, DataFetched? fetched) + public async Task NewPage(MessageInfo message, IServiceProvider services) { - var context = await GetContext(ctxId); + var context = await GetContext(message.ContextId); var page = await context.NewPageAsync(); // 许多网站为了防止信息被爬取,会添加一些防护手段。其中之一是检测 window.navigator.webdriver 属性。 @@ -103,35 +103,37 @@ public async Task NewPage(string ctxId, DataFetched? fetched) var js = @"Object.defineProperties(navigator, {webdriver:{get:()=>false}});"; await page.AddInitScriptAsync(js); - if (fetched != null) + page.Response += async (sender, e) => { - page.Response += async (sender, e) => + if (e.Headers.ContainsKey("content-type") && + e.Headers["content-type"].Contains("application/json") && + (e.Request.ResourceType == "fetch" || e.Request.ResourceType == "xhr")) { - if (e.Headers.ContainsKey("content-type") && - e.Headers["content-type"].Contains("application/json") && - e.Request.ResourceType == "fetch") + Serilog.Log.Information($"{e.Request.Method}: {e.Url}"); + JsonElement? json = null; + try { - Serilog.Log.Information($"fetched: {e.Url}"); - JsonElement? json = null; - try + if (e.Status == 200 && e.Ok) { - if (e.Status == 200 && e.Ok) - { - json = await e.JsonAsync(); - } - else - { - Serilog.Log.Warning($"Response status: {e.Status} {e.StatusText}, OK: {e.Ok}"); - } - fetched(e.Url.ToLower(), JsonSerializer.Serialize(json)); + json = await e.JsonAsync(); } - catch(Exception ex) + else { - Serilog.Log.Error(ex.ToString()); + Serilog.Log.Warning($"Response status: {e.Status} {e.StatusText}, OK: {e.Ok}"); + } + + var webPageResponseHooks = services.GetServices(); + foreach (var hook in webPageResponseHooks) + { + hook.OnDataFetched(message, e.Url.ToLower(), e.Request?.PostData ?? string.Empty, JsonSerializer.Serialize(json)); } } - }; - } + catch(Exception ex) + { + Serilog.Log.Error(ex.ToString()); + } + } + }; return page; } diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs index 8f1222135..458889f91 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.GoToPage.cs @@ -1,5 +1,3 @@ -using System.Linq; - namespace BotSharp.Plugin.WebDriver.Drivers.PlaywrightDriver; public partial class PlaywrightWebDriver @@ -26,7 +24,7 @@ public async Task GoToPage(MessageInfo message, PageActionA } }*/ - var page = args.OpenNewTab ? await _instance.NewPage(message.ContextId, fetched: args.OnDataFetched) : + var page = args.OpenNewTab ? await _instance.NewPage(message, _services) : _instance.GetPage(message.ContextId); Serilog.Log.Information($"goto page: {args.Url}"); @@ -64,14 +62,4 @@ public async Task GoToPage(MessageInfo message, PageActionA return result; } - - private void Page_Response1(object sender, IResponse e) - { - throw new NotImplementedException(); - } - - private void Page_Response(object sender, IResponse e) - { - throw new NotImplementedException(); - } }