232 lines
7.6 KiB
C#
232 lines
7.6 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
|
|
{
|
|
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 pGroupId, long fileSize)
|
|
{
|
|
Id = Guid.NewGuid();
|
|
Username = username;
|
|
UserMessage = message;
|
|
SendTime = sendtime;
|
|
IsMyMessage = isme;
|
|
|
|
GroupId = pGroupId;
|
|
|
|
MessageType = IsHyperlink(message) ? ChatMessageType.Hyperlink : ChatMessageType.Message;
|
|
}
|
|
|
|
// Image-Ansicht
|
|
public ChatMessage(string username, string message, DateTime sendtime, bool isme, ImageSource image, string originalImage, string imageName, long pGroupId, long fileSize)
|
|
{
|
|
Id = Guid.NewGuid();
|
|
Username = username;
|
|
UserMessage = message;
|
|
SendTime = sendtime;
|
|
ImageSources = image;
|
|
IsMyMessage = isme;
|
|
|
|
OriginalImage = originalImage;
|
|
OriginalImageName = imageName;
|
|
|
|
GroupId = pGroupId;
|
|
|
|
MessageType = ChatMessageType.Image;
|
|
}
|
|
|
|
//Dokumenten-Ansicht
|
|
public ChatMessage(string pUsername, string pMessageText, DateTime pSendTime, string pSmallerImagePath, bool pIsLoggedInUsersMessage, string pUrl, long pGroupId, long fileSize)
|
|
{
|
|
Id = Guid.NewGuid();
|
|
Username = pUsername;
|
|
UserMessage = pMessageText;
|
|
SendTime = pSendTime;
|
|
IsMyMessage = pIsLoggedInUsersMessage;
|
|
|
|
FilePath = pUrl;
|
|
|
|
var cache = OwnChatCache.GetInstance();
|
|
|
|
var imgSrc = cache.GetImageSourceFromCache($"group-{pGroupId}");
|
|
|
|
if(imgSrc == null)
|
|
{
|
|
imgSrc = Utils.CreateImageSourceFromPath(pSmallerImagePath);
|
|
}
|
|
|
|
Thumbnail = imgSrc;
|
|
|
|
GroupId = pGroupId;
|
|
|
|
MessageType = ChatMessageType.Document;
|
|
}
|
|
|
|
public ChatMessageType MessageType { get; }
|
|
|
|
public ImageSource Thumbnail { get; }
|
|
|
|
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;
|
|
|
|
public ImageSource ImageSources { get; }
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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 message.Id.Equals(Id);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return SendTime.ToString("dd.MM.yyyy HH:mm:ss");
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return Id.GetHashCode() << 16;
|
|
}
|
|
}
|
|
}
|