325 lines
10 KiB
C#
325 lines
10 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Text.RegularExpressions;
|
|
using System.Windows;
|
|
using System.Windows.Media;
|
|
using ChatController.Core;
|
|
using ChatController.Utilities;
|
|
using static System.Windows.HorizontalAlignment;
|
|
|
|
namespace ChatController.ChatKlassen
|
|
{
|
|
public class ChatMessage : INotifyPropertyChanged
|
|
{
|
|
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; }
|
|
|
|
public long FileSize { get; set; }
|
|
|
|
// Separator
|
|
public ChatMessage(DateTime sendTime)
|
|
{
|
|
Id = Guid.NewGuid();
|
|
IsSeparator = true;
|
|
SendTime = sendTime;
|
|
|
|
MessageType = ChatMessageType.Separator;
|
|
}
|
|
|
|
//Chat-Ansicht
|
|
public ChatMessage(string username, string message, DateTime sendtime, bool isme, long groupId, long fileSize, long messageId)
|
|
{
|
|
MessageId = messageId;
|
|
|
|
Id = Guid.NewGuid();
|
|
Username = username;
|
|
UserMessage = message;
|
|
SendTime = sendtime;
|
|
IsMyMessage = isme;
|
|
|
|
GroupId = groupId;
|
|
|
|
MessageType = IsHyperlink(message) ? ChatMessageType.Hyperlink : ChatMessageType.Message;
|
|
}
|
|
|
|
// Image-Ansicht
|
|
public ChatMessage(string username, string message, DateTime sendtime, bool isme, string filePath, string originalImage, string imageName, long groupId, long fileSize, long messageId)
|
|
{
|
|
PicturePlaceholderHeight = Utils.GetHeightFromThumbnailUri(filePath);
|
|
PictureSource = Utils.GetPicturePlaceholder();
|
|
|
|
MessageId = messageId;
|
|
|
|
Id = Guid.NewGuid();
|
|
Username = username;
|
|
UserMessage = message;
|
|
SendTime = sendtime;
|
|
|
|
IsMyMessage = isme;
|
|
|
|
OriginalImage = originalImage;
|
|
OriginalImageName = imageName;
|
|
|
|
GroupId = groupId;
|
|
|
|
MessageType = ChatMessageType.Image;
|
|
|
|
Utils.DownloadImageAsync(filePath, $"group-{groupId}", CacheCategory.Thumbnail, imageSource =>
|
|
{
|
|
PictureSource = imageSource;
|
|
});
|
|
}
|
|
|
|
//Dokumenten-Ansicht
|
|
public ChatMessage(string username, string message, DateTime sendtime, string thumbnailPath, bool isme, string filePath, long groupId, long fileSize, long messageId)
|
|
{
|
|
ThumbnailPlaceholderHeight = Utils.GetHeightFromThumbnailUri(thumbnailPath);
|
|
Thumbnail = Utils.GetPicturePlaceholder();
|
|
|
|
MessageId = messageId;
|
|
|
|
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;
|
|
}
|
|
|
|
// 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));
|
|
}
|
|
}
|
|
}
|
|
|
|
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})*)?");
|
|
|
|
public static bool IsHyperlink(string word)
|
|
{
|
|
if(word == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (word.IndexOfAny(@":.\/".ToCharArray()) != -1)
|
|
{
|
|
if (_UrlRegex.IsMatch(word))
|
|
{
|
|
var uri = new Uri(word, UriKind.RelativeOrAbsolute);
|
|
|
|
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; }
|
|
|
|
public string Username { get; }
|
|
|
|
public string UserMessage { get; }
|
|
|
|
public DateTime SendTime { get; }
|
|
|
|
public string UserTimeStringLeft => IsMyMessage ? null : Username;
|
|
|
|
public string UserTimeStringRight => SendTimeString;
|
|
|
|
public string SendTimeString => SendTime.Date == DateTime.Today ? $"Heute {SendTime:HH:mm}" : SendTime.ToString("dd. MMM HH:mm");
|
|
|
|
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;
|
|
|
|
public string OriginalImage { get; }
|
|
|
|
public string OriginalImageName { get; }
|
|
|
|
public Visibility ChatVisibility => MessageType == ChatMessageType.Message ? Visibility.Visible : Visibility.Collapsed;
|
|
|
|
public Visibility HyperlinkVisibility => MessageType == ChatMessageType.Hyperlink ? Visibility.Visible : Visibility.Collapsed;
|
|
|
|
public Visibility PictureVisibility => MessageType == ChatMessageType.Image ? Visibility.Visible : Visibility.Collapsed;
|
|
|
|
public Visibility PicturePlaceHolderVisibility => MessageType == ChatMessageType.DefaultImage ? Visibility.Visible : Visibility.Collapsed;
|
|
|
|
public Visibility DokumentVisibility => MessageType == ChatMessageType.Document ? Visibility.Visible : Visibility.Collapsed;
|
|
|
|
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; }
|
|
|
|
public long GroupId { get; }
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
public Guid Id { get; }
|
|
|
|
public Uri UserMessageAsUri
|
|
{
|
|
get
|
|
{
|
|
if(IsHyperlink(UserMessage))
|
|
{
|
|
if(!UserMessage.Contains("http"))
|
|
{
|
|
return new Uri("http://" + UserMessage);
|
|
}
|
|
|
|
if(UserMessage.Contains("https") || UserMessage.Contains("http"))
|
|
{
|
|
return new Uri(UserMessage);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
protected virtual void OnPropertyChanged(string propertyName)
|
|
{
|
|
var handler = PropertyChanged;
|
|
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
if(obj is ChatMessage message)
|
|
{
|
|
if(message.IsSeparator && IsSeparator)
|
|
{
|
|
return SendTime == message.SendTime;
|
|
}
|
|
|
|
return MessageId.Equals(message.MessageId);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return SendTime.ToString("dd.MM.yyyy HH:mm:ss");
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return Id.GetHashCode() << 16;
|
|
}
|
|
}
|
|
}
|