- Links in Nachrichten klickbar

- Profilbilder werden in Gruppenchats (mehr als 2 Teilnehmer) angezeigt
- Profilbilder werden im Cache gespeichert
- Verbesserungen am Code
This commit is contained in:
Lyndon Jetten
2021-09-07 19:32:10 +02:00
parent 2dcca07725
commit afaf7a214c
14 changed files with 535 additions and 372 deletions

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
@@ -7,7 +9,9 @@ using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
@@ -38,9 +42,16 @@ namespace ChatController.Utilities
return;
}
var url = pathToProfilePicture;
var client = new RestClient(url);
DownloadImage(pathToProfilePicture, imageSource =>
{
cache.AddProfilePictureToCache(key, imageSource, pathToProfilePicture);
callback?.Invoke(imageSource);
});
}
private static void DownloadImage(string uri, Action<ImageSource> callback)
{
var client = new RestClient(uri);
var request = new RestRequest
{
ResponseWriter = stream =>
@@ -58,8 +69,6 @@ namespace ChatController.Utilities
bitmap.EndInit();
bitmap.Freeze();
cache.AddProfilePictureToCache(key, bitmap, pathToProfilePicture);
callback?.Invoke(bitmap);
}
};
@@ -70,6 +79,33 @@ namespace ChatController.Utilities
client.ExecuteAsync(request, response => { /*Das übernimmt der ResponseWriter von oben*/ });
}
public static void DownloadUserProfilePictures(Dictionary<long, string> userId2Uris)
{
var cache = OwnChatCache.GetInstance();
foreach(var id2Uri in userId2Uris)
{
var userProfilePicture = cache.GetUserProfilePictureFromCache(id2Uri.Key);
if(userProfilePicture == null)
{
try
{
var uri = id2Uri.Value;
DownloadImage(uri, profilePicture =>
{
cache.AddUserProfilePictureToCache(profilePicture, id2Uri.Key, uri);
});
}
catch(Exception)
{
}
}
}
}
public static void DownloadImageAsync(string uri, string key, CacheCategory cacheCategory, Action<ImageSource> callback)
{
if(string.IsNullOrEmpty(uri))
@@ -88,35 +124,11 @@ namespace ChatController.Utilities
return;
}
var url = uri;
var client = new RestClient(url);
var request = new RestRequest
DownloadImage(uri, imageSource =>
{
ResponseWriter = stream =>
{
var bitmap = new BitmapImage();
var localStream = new MemoryStream();
stream.CopyTo(localStream);
localStream.Position = 0;
bitmap.BeginInit();
bitmap.StreamSource = localStream;
bitmap.EndInit();
bitmap.Freeze();
cache.AddImageToCache(key, bitmap, cacheCategory, uri);
callback?.Invoke(bitmap);
}
};
request.AddHeader(Constants.Token, AuthToken);
request.AddHeader(Constants.CustomerId, Tenant);
client.ExecuteAsync(request, response => { });
cache.AddImageToCache(key, imageSource, cacheCategory, uri);
callback?.Invoke(imageSource);
});
}
private static ImageSource GetDefaultImageSource(bool pIsGroup, bool pIsEmployee)
@@ -438,5 +450,82 @@ namespace ChatController.Utilities
return 100;
}
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})*)(?:&amp;(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static ObservableCollection<Inline> ConvertLinksToHyperlinks(string messageText)
{
var result = new ObservableCollection<Inline>();
if(messageText == null)
{
return result;
}
var guidString = Guid.NewGuid().ToString();
var replacementString = Guid.NewGuid().ToString();
var matches = _UrlRegex.Matches(messageText);
var index2Link = new Dictionary<int, Hyperlink>();
for(var i = 0; i < matches.Count; i++)
{
var link = matches[i].Value;
if(!link.Contains("http"))
{
link = $"http://{link}";
}
var hyperlink = new Hyperlink(new Run(link))
{
NavigateUri = new Uri(link, UriKind.Absolute)
};
hyperlink.RequestNavigate += (s, e) =>
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
};
index2Link[i] = hyperlink;
}
var splitMessage = _UrlRegex.Replace(messageText, replacementString + guidString).Split(new[] {guidString}, StringSplitOptions.None);
var linkIndex = 0;
foreach(var text in splitMessage)
{
if(text.Equals(replacementString))
{
result.Add(index2Link[linkIndex]);
linkIndex++;
}
else if(text.StartsWith(replacementString) || text.EndsWith(replacementString))
{
var splitLine = text.Split(new[] { replacementString }, StringSplitOptions.None);
foreach(var partLine in splitLine)
{
if(string.Empty.Equals(partLine))
{
result.Add(index2Link[linkIndex]);
linkIndex++;
}
else
{
result.Add(new Run(partLine));
}
}
}
else
{
result.Add(new Run(text));
}
}
return result;
}
}
}