private async Task InsertMessageAsync(NotifyClientProfile profile, NotifyPropertyMessage message) { var messages = profile.MessageCollection; if (messages.Any(r => r.MessageId == message.MessageId)) { return; } messages.Add(message); var model = new MessageEntry { MessageId = message.MessageId, ProfileId = profile.ProfileId, DateTime = message.DateTime, Path = message.Path, Reference = message.Reference.ToString(), Object = message is NotifyTextMessage text ? (string)text.Object : ((NotifyImageMessage)message).ImageHash, }; await this.storage.StoreMessagesAsync(new[] { model }); if (message.Reference != MessageReference.Remote) { return; } var eventArgs = new MessageEventArgs(profile, message); NewMessage?.Invoke(this, eventArgs); if (eventArgs.IsHandled) { return; } profile.UnreadCount++; }
static bool NotOnline(NotifyClientProfile profile) => profile.OnlineStatus != ProfileOnlineStatus.Online;
public Client(Settings settings, IDispatcher dispatcher, IStorage storage) { void ProfileChanged(object sender, PropertyChangedEventArgs e) { var profile = this.profile; Debug.Assert(sender == profile); if (e.PropertyName == nameof(NotifyClientProfile.Name)) { settings.ClientName = profile.Name; } else if (e.PropertyName == nameof(NotifyClientProfile.Text)) { settings.ClientText = profile.Text; } else if (e.PropertyName == nameof(NotifyClientProfile.ImageHash)) { settings.ClientImageHash = profile.ImageHash; } } NotifyClientProfile InitializeProfile() { var imageHash = settings.ClientImageHash; var imagePath = default(FileInfo); var exists = !string.IsNullOrEmpty(imageHash) && this.cache.TryGetCache(imageHash, out imagePath); var profile = new NotifyClientProfile(settings.ClientId) { Name = settings.ClientName, Text = settings.ClientText, UdpPort = settings.UdpEndPoint.Port, TcpPort = settings.TcpEndPoint.Port, ImageHash = exists ? imageHash : string.Empty, }; profile.SetImagePath(exists ? imagePath.FullName : string.Empty); profile.SetIPAddress(IPAddress.Loopback); return(profile); } if (settings is null) { throw new ArgumentNullException(nameof(settings)); } if (dispatcher is null) { throw new ArgumentNullException(nameof(dispatcher)); } if (storage is null) { throw new ArgumentNullException(nameof(storage)); } this.cancellationToken = this.cancellation.Token; this.dispatcher = dispatcher; this.storage = storage; this.settings = settings; this.context = new Context(settings, this.generator, dispatcher, this, this.cancellation.Token); this.network = new Network(this.context); this.cache = new Cache(this.context, this.network); this.profile = InitializeProfile(); this.profile.PropertyChanged += ProfileChanged; this.network.RegisterHandler("link.message.text", this.HandleTextAsync); this.network.RegisterHandler("link.message.image-hash", this.HandleImageAsync); this.network.RegisterHandler("link.sharing.file", this.HandleFileAsync); this.network.RegisterHandler("link.sharing.directory", this.HandleDirectoryAsync); this.network.RegisterHandler("link.broadcast", this.HandleBroadcastAsync); }