Files
Chat/ChatController/ChatKlassen/ChatMessage.cs

328 lines
10 KiB
C#
Raw Normal View History

2017-12-11 14:32:56 +01:00
using System;
using System.ComponentModel;
using System.Diagnostics;
2017-12-11 14:32:56 +01:00
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Media;
using ChatController.Core;
using ChatController.Utilities;
using static System.Windows.HorizontalAlignment;
2017-12-11 14:32:56 +01:00
namespace ChatController.ChatKlassen
{
public class ChatMessage : INotifyPropertyChanged
2017-12-11 14:32:56 +01:00
{
public long MessageId { get; set; }
public int TimeStringMaxWidth => IsSeparator ? int.MaxValue : 350;
public int MessageMaxWidth => IsSeparator ? int.MaxValue : 500;
public string SeparatorTimeString => SendTime.ToString("dd. MMMM yyyy");
public bool IsSeparator { get; set; }
2020-04-17 13:01:23 +02:00
public long FileSize { get; set; }
// Separator
public ChatMessage(DateTime sendTime)
{
Id = Guid.NewGuid();
IsSeparator = true;
SendTime = sendTime;
MessageType = ChatMessageType.Separator;
}
2019-04-02 13:43:30 +02:00
//Chat-Ansicht
public ChatMessage(string username, string message, DateTime sendtime, bool isme, long groupId, long fileSize, long messageId)
2017-12-11 14:32:56 +01:00
{
MessageId = messageId;
Id = Guid.NewGuid();
2017-12-11 14:32:56 +01:00
Username = username;
UserMessage = message;
SendTime = sendtime;
IsMyMessage = isme;
GroupId = groupId;
MessageType = IsHyperlink(message) ? ChatMessageType.Hyperlink : ChatMessageType.Message;
2017-12-11 14:32:56 +01:00
}
2019-04-02 13:43:30 +02:00
// Image-Ansicht
public ChatMessage(string username, string message, DateTime sendtime, bool isme, string filePath, string originalImage, string imageName, long groupId, long fileSize, long messageId)
2017-12-11 14:32:56 +01:00
{
PicturePlaceholderHeight = Utils.GetHeightFromThumbnailUri(filePath);
PictureSource = Utils.GetPicturePlaceholder();
MessageId = messageId;
Id = Guid.NewGuid();
2017-12-11 14:32:56 +01:00
Username = username;
UserMessage = message;
SendTime = sendtime;
2017-12-11 14:32:56 +01:00
IsMyMessage = isme;
OriginalImage = originalImage;
OriginalImageName = imageName;
GroupId = groupId;
MessageType = ChatMessageType.Image;
Utils.DownloadImageAsync(filePath, $"group-{groupId}", CacheCategory.Thumbnail, imageSource =>
{
PictureSource = imageSource;
});
2017-12-11 14:32:56 +01:00
}
2019-04-02 13:43:30 +02:00
//Dokumenten-Ansicht
public ChatMessage(string username, string message, DateTime sendtime, string thumbnailPath, bool isme, string filePath, long groupId, long fileSize, long messageId)
2017-12-11 14:32:56 +01:00
{
ThumbnailPlaceholderHeight = Utils.GetHeightFromThumbnailUri(thumbnailPath);
Thumbnail = Utils.GetPicturePlaceholder();
2017-12-11 14:32:56 +01:00
MessageId = messageId;
2017-12-11 14:32:56 +01:00
Id = Guid.NewGuid();
Username = username;
UserMessage = message;
SendTime = sendtime;
IsMyMessage = isme;
FilePath = filePath;
Utils.DownloadImageAsync(thumbnailPath, $"group-{groupId}", CacheCategory.Thumbnail, imageSource =>
{
Thumbnail = imageSource;
});
GroupId = groupId;
MessageType = ChatMessageType.Document;
2017-12-11 14:32:56 +01:00
}
// Mit Logo
public ChatMessage(string username, string message, DateTime sendtime, bool isme, ImageSource logo, string originalImage, string imageName, long groupId, long fileSize, long messageId)
{
MessageId = messageId;
Id = Guid.NewGuid();
Username = username;
UserMessage = message;
SendTime = sendtime;
IsMyMessage = isme;
OriginalImage = originalImage;
OriginalImageName = imageName;
GroupId = groupId;
MessageType = ChatMessageType.Image;
PictureSource = logo;
}
public ChatMessageType MessageType { get; }
private ImageSource _Thumbnail;
public ImageSource Thumbnail
{
get => _Thumbnail;
set
{
if(!Equals(_Thumbnail, value))
{
_Thumbnail = value;
OnPropertyChanged(nameof(Thumbnail));
}
}
}
private int _ThumbnailPlaceholderHeight;
public int ThumbnailPlaceholderHeight
{
get => _ThumbnailPlaceholderHeight;
set
{
if(!Equals(_ThumbnailPlaceholderHeight, value))
{
_ThumbnailPlaceholderHeight = value;
OnPropertyChanged(nameof(ThumbnailPlaceholderHeight));
}
}
}
2017-12-11 14:32:56 +01:00
private static readonly Regex _UrlRegex = new Regex(@"(?#Protocol)^(http(?:s?)\:(\/\/|\\\\)|(w){3}(2|3)?\.{1})(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?");
2017-12-11 14:32:56 +01:00
public static bool IsHyperlink(string word)
{
2020-04-17 13:01:23 +02:00
if(word == null)
{
return false;
}
2017-12-11 14:32:56 +01:00
try
{
if (word.IndexOfAny(@":.\/".ToCharArray()) != -1)
{
if (_UrlRegex.IsMatch(word))
2017-12-11 14:32:56 +01:00
{
2019-04-02 13:43:30 +02:00
var uri = new Uri(word, UriKind.RelativeOrAbsolute);
2017-12-11 14:32:56 +01:00
if (!uri.IsAbsoluteUri)
{
uri = new Uri(@"http://" + word, UriKind.Absolute);
}
if (uri.IsAbsoluteUri)
{
return true;
}
}
else
{
return false;
}
}
}
catch (Exception e)
{
return false;
}
return false;
}
public bool IsMyMessage { get; set; }
2019-04-02 13:43:30 +02:00
public string Username { get; }
2019-04-02 13:43:30 +02:00
public string UserMessage { get; }
2019-04-02 13:43:30 +02:00
public DateTime SendTime { get; }
2017-12-11 14:32:56 +01:00
2019-04-02 13:43:30 +02:00
public string UserTimeStringLeft => IsMyMessage ? null : Username;
2017-12-11 14:32:56 +01:00
2019-04-02 13:43:30 +02:00
public string UserTimeStringRight => SendTimeString;
2017-12-11 14:32:56 +01:00
public string SendTimeString => SendTime.Date == DateTime.Today ? $"Heute {SendTime:HH:mm}" : SendTime.ToString("dd. MMM HH:mm");
2017-12-11 14:32:56 +01:00
public Brush BorderBrush => IsSeparator ? new SolidColorBrush(Colors.Transparent) : IsMyMessage ? new SolidColorBrush(Color.FromRgb(255, 90, 0)) : new SolidColorBrush(Color.FromRgb(229, 229, 229));
public Brush HighlightedBorderBrush => IsSeparator ? new SolidColorBrush(Colors.Transparent) : IsMyMessage ? new SolidColorBrush(Color.FromRgb(255, 153, 153)) : new SolidColorBrush(Color.FromRgb(247, 247, 247));
public Brush Foreground => new BrushConverter().ConvertFromString("#505050") as SolidColorBrush;
public HorizontalAlignment HorizontalAlignmentValue => IsSeparator ? HorizontalAlignment.Stretch : IsMyMessage ? Right : Left;
2017-12-11 14:32:56 +01:00
2019-04-02 13:43:30 +02:00
public string OriginalImage { get; }
2019-04-02 13:43:30 +02:00
public string OriginalImageName { get; }
2017-12-11 14:32:56 +01:00
public Visibility ChatVisibility => MessageType == ChatMessageType.Message ? Visibility.Visible : Visibility.Collapsed;
2017-12-11 14:32:56 +01:00
public Visibility HyperlinkVisibility => MessageType == ChatMessageType.Hyperlink ? Visibility.Visible : Visibility.Collapsed;
2017-12-11 14:32:56 +01:00
public Visibility PictureVisibility => MessageType == ChatMessageType.Image ? Visibility.Visible : Visibility.Collapsed;
2017-12-11 14:32:56 +01:00
public Visibility PicturePlaceHolderVisibility => MessageType == ChatMessageType.DefaultImage ? Visibility.Visible : Visibility.Collapsed;
public Visibility DokumentVisibility => MessageType == ChatMessageType.Document ? Visibility.Visible : Visibility.Collapsed;
2017-12-11 14:32:56 +01:00
private ImageSource _PictureSource;
public ImageSource PictureSource
{
get => _PictureSource;
set
{
if(!Equals(_PictureSource, value))
{
_PictureSource = value;
OnPropertyChanged(nameof(PictureSource));
}
}
}
private int _PicturePlaceholderHeight;
public int PicturePlaceholderHeight
{
get => _PicturePlaceholderHeight;
set
{
if (!Equals(_PicturePlaceholderHeight, value))
{
_PicturePlaceholderHeight = value;
OnPropertyChanged(nameof(PicturePlaceholderHeight));
}
}
}
public object FilePath { get; }
2017-12-11 14:32:56 +01:00
public long GroupId { get; }
public event PropertyChangedEventHandler PropertyChanged;
public Guid Id { get; }
2017-12-11 14:32:56 +01:00
public Uri UserMessageAsUri
2017-12-11 14:32:56 +01:00
{
get
2017-12-11 14:32:56 +01:00
{
if(IsHyperlink(UserMessage))
{
Debug.WriteLine($"Link: {UserMessage}");
if(!UserMessage.Contains("http"))
{
return new Uri("http://" + UserMessage);
}
2021-02-12 14:58:31 +01:00
if(UserMessage.Contains("https") || UserMessage.Contains("http"))
2021-02-12 14:58:31 +01:00
{
return new Uri(UserMessage);
}
}
2017-12-11 14:32:56 +01:00
return null;
2017-12-11 14:32:56 +01:00
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public override bool Equals(object obj)
2017-12-11 14:32:56 +01:00
{
if(obj is ChatMessage message)
2017-12-11 14:32:56 +01:00
{
if(message.IsSeparator && IsSeparator)
{
return SendTime == message.SendTime;
}
2017-12-11 14:32:56 +01:00
return MessageId.Equals(message.MessageId);
2017-12-11 14:32:56 +01:00
}
return false;
}
public override string ToString()
{
return SendTime.ToString("dd.MM.yyyy HH:mm:ss");
}
2017-12-11 14:32:56 +01:00
public override int GetHashCode()
2017-12-11 14:32:56 +01:00
{
return Id.GetHashCode() << 16;
2017-12-11 14:32:56 +01:00
}
}
}