public override void Install() { // settings _settingService.SaveSetting(new RChatSettings { numberOfHistoryMessages = 100, messageCharacterLimit = 1000, nameCharacterLimit = 10, throttleMilliseconds = 500, bannerText = "Welcome to RChat" }); _permissionService.InstallPermissions(new RChatPermissions()); RChatHistory.Resize(100); // locales _localizationService.AddOrUpdatePluginLocaleResource("Plugins.Widgets.RChat.Name", "RChat"); _localizationService.AddOrUpdatePluginLocaleResource("Plugins.Widgets.RChat.NumberOfHistoryMessages", "Number of History Messages"); _localizationService.AddOrUpdatePluginLocaleResource("Plugins.Widgets.RChat.MessageCharacterLimit", "Message Character Limit"); _localizationService.AddOrUpdatePluginLocaleResource("Plugins.Widgets.RChat.NameCharacterLimit", "Name Character Limit"); _localizationService.AddOrUpdatePluginLocaleResource("Plugins.Widgets.RChat.ThrottleMilliseconds", "Throttle Milliseconds"); _localizationService.AddOrUpdatePluginLocaleResource("Plugins.Widgets.RChat.BannerText", "Banner Text"); base.Install(); }
public RChatPlugin (ILocalizationService localizationService , ISettingService settingService , IStoreContext storeContext , IWebHelper webHelper , IPermissionService permissionService ) { _localizationService = localizationService; _settingService = settingService; _webHelper = webHelper; _permissionService = permissionService; RChatSettings rChatSettings = _settingService.LoadSetting <RChatSettings>(storeContext.CurrentStore.Id); RChatHistory.Resize(rChatSettings.numberOfHistoryMessages); }
public async Task Send(string message) { if (!_permissions.Authorize(RChatPermissions.Send)) { return; } message = message.Trim(); if (message == "") { return; } RChatSettings rChatSettings = _settingService.LoadSetting <RChatSettings>(_storeContext.CurrentStore.Id); Customer customer = _workContext.CurrentCustomer; string name = customer.Username; int id = customer.Id; bool throttle = _throttleTracker.TryGetValue(id, out long millis) && millis > DateTime.Now.Ticks; _throttleTracker[id] = TimeSpan.TicksPerMillisecond * rChatSettings.throttleMilliseconds + DateTime.Now.Ticks; if (throttle) { return; } if (message.Length > rChatSettings.messageCharacterLimit) { message = message.Remove(rChatSettings.messageCharacterLimit); } if (name.Length > rChatSettings.nameCharacterLimit) { name = name.Remove(rChatSettings.nameCharacterLimit); } message = Sanitize(message); RChatHistory.AddMessage(id, name, message); await Clients.All.SendAsync("Send", id, name, message); }