mirror of
https://github.com/maker-community/ElectronBot.DotNet.git
synced 2026-10-25 10:39:50 +08:00
add data init
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Verdure.Braincase.Core.Contracts.Services;
|
||||
public interface IDataInitService
|
||||
{
|
||||
Task InitializeDataAsync();
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Abstraction.Plugins.Models;
|
||||
using BotSharp.Abstraction.Repositories.Filters;
|
||||
using BotSharp.Abstraction.Users;
|
||||
using BotSharp.Abstraction.Users.Enums;
|
||||
using BotSharp.Abstraction.Users.Models;
|
||||
using BotSharp.Abstraction.Utilities;
|
||||
using BotSharp.Core.Plugins;
|
||||
using CommunityToolkit.Mvvm.DependencyInjection;
|
||||
using Verdure.Braincase.Core.Contracts.Services;
|
||||
using Verdure.Braincase.Core.Models;
|
||||
|
||||
namespace Verdure.Braincase.Copilot.Services;
|
||||
public class CopilotDataInitService : IDataInitService
|
||||
{
|
||||
private readonly IAgentService _agentService;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IUserIdentity _userIdentity;
|
||||
private readonly IUserService _userService;
|
||||
private readonly ILocalSettingsService _localSettingsService;
|
||||
public CopilotDataInitService(IServiceProvider services,
|
||||
IAgentService agentService,
|
||||
IUserIdentity userIdentity,
|
||||
IUserService userService,
|
||||
ILocalSettingsService localSettingsService)
|
||||
{
|
||||
_services = services;
|
||||
_agentService = agentService;
|
||||
_userIdentity = userIdentity;
|
||||
_userService = userService;
|
||||
_localSettingsService = localSettingsService;
|
||||
}
|
||||
public async Task InitializeDataAsync()
|
||||
{
|
||||
var user = await _userService.GetUser(_userIdentity.Id);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
await _userService.CreateUser(new User
|
||||
{
|
||||
Id = _userIdentity.Id,
|
||||
Email = _userIdentity.Email,
|
||||
UserName = _userIdentity.UserName,
|
||||
FirstName = _userIdentity.FirstName,
|
||||
LastName = _userIdentity.LastName,
|
||||
Role = UserRole.Admin,
|
||||
Type = UserType.Client,
|
||||
});
|
||||
}
|
||||
|
||||
var agents = await _agentService.GetAgents(new AgentFilter
|
||||
{
|
||||
Pager = new Pagination
|
||||
{
|
||||
Page = 1,
|
||||
Size = 100
|
||||
}
|
||||
});
|
||||
if (!agents.Items.Any())
|
||||
{
|
||||
_ = await _agentService.RefreshAgents();
|
||||
|
||||
var loader = Ioc.Default.GetRequiredService<PluginLoader>();
|
||||
var plugins = loader.GetPagedPlugins(_services, new PluginFilter
|
||||
{
|
||||
Pager = new Pagination
|
||||
{
|
||||
Page = 1,
|
||||
Size = 100
|
||||
}
|
||||
}).Items.ToList();
|
||||
|
||||
foreach (var plugin in plugins)
|
||||
{
|
||||
_ = loader.UpdatePluginStatus(_services, plugin.Id, true);
|
||||
}
|
||||
await _localSettingsService.SaveSettingAsync(Constants.DefaultChatBotNameKey, new ComboxItemModel());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -56,21 +56,21 @@ public partial class AgentViewModel : ObservableRecipient, INavigationAware
|
||||
}
|
||||
public async void OnNavigatedTo(object parameter)
|
||||
{
|
||||
var user = await _userService.GetUser(_userIdentity.Id);
|
||||
//var user = await _userService.GetUser(_userIdentity.Id);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
await _userService.CreateUser(new User
|
||||
{
|
||||
Id = _userIdentity.Id,
|
||||
Email = _userIdentity.Email,
|
||||
UserName = _userIdentity.UserName,
|
||||
FirstName = _userIdentity.FirstName,
|
||||
LastName = _userIdentity.LastName,
|
||||
Role = UserRole.Admin,
|
||||
Type = UserType.Client,
|
||||
});
|
||||
}
|
||||
//if (user == null)
|
||||
//{
|
||||
// await _userService.CreateUser(new User
|
||||
// {
|
||||
// Id = _userIdentity.Id,
|
||||
// Email = _userIdentity.Email,
|
||||
// UserName = _userIdentity.UserName,
|
||||
// FirstName = _userIdentity.FirstName,
|
||||
// LastName = _userIdentity.LastName,
|
||||
// Role = UserRole.Admin,
|
||||
// Type = UserType.Client,
|
||||
// });
|
||||
//}
|
||||
|
||||
Agents = (await _agentService.GetAgents(new AgentFilter
|
||||
{
|
||||
|
||||
@@ -7,7 +7,6 @@ using BotSharp.Core;
|
||||
using BotSharp.Core.Crontab.Abstraction;
|
||||
using BotSharp.Core.Infrastructures;
|
||||
using BotSharp.Logger;
|
||||
using Contracts.Services;
|
||||
using Controls;
|
||||
using Controls.CompactOverlay;
|
||||
using ElectronBot.DotNet;
|
||||
@@ -26,6 +25,7 @@ using Verdure.Braincase.Activation;
|
||||
using Verdure.Braincase.ClockViews;
|
||||
using Verdure.Braincase.Contracts.Services;
|
||||
using Verdure.Braincase.Copilot.Hooks;
|
||||
using Verdure.Braincase.Copilot.Services;
|
||||
using Verdure.Braincase.Copilot.Services.BotSharp;
|
||||
using Verdure.Braincase.Copilot.ViewModels;
|
||||
using Verdure.Braincase.Copilot.Views;
|
||||
@@ -315,12 +315,13 @@ public static class ConfigureServicesExtensions
|
||||
// Add wake phrase listener
|
||||
.AddSingleton<IWakeWordListener, AzCognitiveServicesWakeWordListener>()
|
||||
//.AddSingleton<IBotSpeech, DefaultBotSpeech>()
|
||||
.AddSingleton<IBotSpeech, AzBotSpeech>()
|
||||
.AddSingleton<IBotSpeech, AzBotSpeech>()
|
||||
// Add the primary hosted service to start the loop.
|
||||
.AddHostedService<HostedService>()
|
||||
.AddMemoryCache()
|
||||
.AddScoped<ICrontabHook, NotifyCrontabHook>()
|
||||
.AddScoped<IWallpaperService, WallpaperService>()
|
||||
.AddScoped<IDataInitService, CopilotDataInitService>()
|
||||
// Configuration
|
||||
.BuildServiceProvider());
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
using Verdure.Braincase.Activation;
|
||||
using Verdure.Braincase.Contracts.Services;
|
||||
using Verdure.Braincase.Models;
|
||||
using Verdure.Braincase.Views;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Verdure.Braincase.Activation;
|
||||
using Verdure.Braincase.Views;
|
||||
|
||||
namespace Verdure.Braincase.Services;
|
||||
|
||||
@@ -19,14 +17,15 @@ public class ActivationService : IActivationService
|
||||
private readonly IdentityService _identityService;
|
||||
|
||||
private readonly ILocalSettingsService _localSettingsService;
|
||||
|
||||
private readonly IEnumerable<IDataInitService> _dataInitServices;
|
||||
|
||||
public ActivationService(ActivationHandler<LaunchActivatedEventArgs> defaultHandler,
|
||||
IEnumerable<IActivationHandler> activationHandlers,
|
||||
IThemeSelectorService themeSelectorService,
|
||||
UserDataService userDataService,
|
||||
IdentityService identityService,
|
||||
ILocalSettingsService localSettingsService)
|
||||
ILocalSettingsService localSettingsService,
|
||||
IEnumerable<IDataInitService> dataInitServices)
|
||||
{
|
||||
_defaultHandler = defaultHandler;
|
||||
_activationHandlers = activationHandlers;
|
||||
@@ -34,6 +33,7 @@ public class ActivationService : IActivationService
|
||||
_userDataService = userDataService;
|
||||
_identityService = identityService;
|
||||
_localSettingsService = localSettingsService;
|
||||
_dataInitServices = dataInitServices;
|
||||
}
|
||||
|
||||
public async Task ActivateAsync(object activationArgs)
|
||||
@@ -46,6 +46,11 @@ public class ActivationService : IActivationService
|
||||
await _identityService.AttachTokenCacheAsync();
|
||||
await _identityService.AcquireTokenSilentAsync();
|
||||
|
||||
foreach (var dataInitService in _dataInitServices)
|
||||
{
|
||||
await dataInitService.InitializeDataAsync();
|
||||
}
|
||||
|
||||
// Set the MainWindow Content.
|
||||
if (App.MainWindow.Content == null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user