Refactor ChatSubsystem, start to prototyping interfaces for chat internal managers classes

This commit is contained in:
ChronosX88 2021-02-03 22:09:37 +03:00
parent a489ff518e
commit eb11191c8f
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
15 changed files with 199 additions and 69 deletions

View File

@ -0,0 +1,43 @@
namespace Zirconium.Core.Models
{
public class EntityID
{
public string LocalPart { get; }
public string ServerPart { get; }
public bool OnlyServerPart { get; }
public char Type { get; }
public EntityID(string entityID)
{
Type = entityID[0];
entityID = entityID.Remove(0, 1); // prevent confusion with username entityID type when splitting
var entityIdSplitted = entityID.Split("@");
// if it is only server part
if (entityIdSplitted.Length == 1 && entityIdSplitted[0] == entityID)
{
ServerPart = entityIdSplitted[0];
OnlyServerPart = true;
}
LocalPart = entityIdSplitted[0];
ServerPart = entityIdSplitted[1];
}
public EntityID(char type, string localPart, string serverPart)
{
Type = type;
LocalPart = localPart;
ServerPart = serverPart;
}
public override string ToString()
{
if (OnlyServerPart)
{
return ServerPart;
}
return $"{Type}{LocalPart}@{ServerPart}";
}
}
}

View File

@ -36,11 +36,11 @@ namespace Zirconium.Core.Plugins.IPC
{ {
return Task.Factory.StartNew<dynamic>(() => return Task.Factory.StartNew<dynamic>(() =>
{ {
var method = methodTable[pluginName].Where(x => x.MethodName == methodName).FirstOrDefault(); var method = methodTable[pluginName].FirstOrDefault(x => x.MethodName == methodName);
object returnValue = null; object returnValue = null;
try try
{ {
returnValue = method.Method.Invoke(method.Service, new object[] { paramsObject }); returnValue = method?.Method.Invoke(method.Service, new object[] { paramsObject });
} }
catch (TargetInvocationException e) catch (TargetInvocationException e)
{ {

View File

@ -3,6 +3,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<BuildProjectReferences>false</BuildProjectReferences> <BuildProjectReferences>false</BuildProjectReferences>
<RootNamespace>ChatSubsystem</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -6,11 +6,11 @@ using Zirconium.Core.Plugins.Interfaces;
using Zirconium.Utils; using Zirconium.Utils;
using Log4Sharp; using Log4Sharp;
namespace BasicChat namespace ChatSubsystem
{ {
internal class BasicChat : IPluginAPI internal class ChatSubsystemPlugin : IPluginAPI
{ {
public string GetPluginUniqueName() => "BasicChat"; public string GetPluginUniqueName() => "ChatSubsystem";
public void Initialize(IPluginHostAPI pluginHost) public void Initialize(IPluginHostAPI pluginHost)
{ {

View File

@ -0,0 +1,13 @@
using System.Threading.Tasks;
using ChatSubsystem.Storage.Models;
using Zirconium.Core.Models;
namespace ChatSubsystem.Storage.Interfaces
{
public interface IChatStorageManager
{
Task<Chat> GetPrivateChat(EntityID u1, EntityID u2);
Task<Chat> GetById(EntityID chatId);
Task<Event> GetEventById(EntityID id);
}
}

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using ChatSubsystem.Storage.Models;
using Zirconium.Core.Models;
namespace ChatSubsystem.Storage.Interfaces
{
public interface IEventStorageManager
{
IList<Event> GetEventsForUser(EntityID user, EntityID token, int limit);
Task<Event> GetEventById(EntityID id);
}
}

View File

@ -0,0 +1,25 @@
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace ChatSubsystem.Storage.Models
{
public class Chat
{
[BsonId]
public ObjectId Id { get; set; }
public string ChatID { get; set; }
public string AvatarID { get; set; }
public string Description { get; set; }
public string[] Members { get; set; }
public bool IsPrivateChat { get; set; }
public IList<string> Banned { get; set; }
public IList<string> PinnedMessages { get; set; } // id of pinned messages
public IDictionary<string, string> RoleNames { get; set; }
public IDictionary<string, byte[]> Permissions { get; set; }
public IList<string> LocalAliases { get; set; }
public IList<string> RemoveAliases { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using Zirconium.Core.Models;
namespace ChatSubsystem.Storage.Models
{
public class Event
{
[BsonId]
public ObjectId Id { get; set; }
public EntityID From { get; set; }
public EntityID ChatId { get; set; }
public string Type { get; set; }
public long Timestamp { get; set; }
public EntityID OriginServer { get; set; }
public EventContent Content { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using MongoDB.Bson.Serialization.Attributes;
namespace ChatSubsystem.Storage.Models
{
[BsonDiscriminator(RootClass = true)]
[BsonKnownTypes(typeof(GeneralMessageEventContent))]
public abstract class EventContent {}
public class GeneralMessageEventContent : EventContent
{
public string Text { get; set; }
public Media[] Media { get; set; }
public Reaction[] Reactions { get; set; }
}
}

View File

@ -0,0 +1,11 @@
namespace ChatSubsystem.Storage.Models
{
public class Media
{
public string ID { get; set; }
public string Type { get; set; }
public int Size { get; set; }
public string FileName { get; set; }
public MediaAttrs Attrs { get; set; }
}
}

View File

@ -0,0 +1,46 @@
using MongoDB.Bson.Serialization.Attributes;
namespace ChatSubsystem.Storage.Models
{
[BsonDiscriminator(RootClass = true)]
[BsonKnownTypes(typeof(AudioAttrs),typeof(StickerAttrs), typeof(VideoAttrs), typeof(PhotoAttrs), typeof(GeolocationAttrs))]
public abstract class MediaAttrs
{
}
public class AudioAttrs : MediaAttrs
{
public bool Voice { get; set; } // whether it is voice message
public decimal Duration { get; set; } // duration of audio
public string? Title { get; set; } // audio title
public string? Artist { get; set; } // audio artist
public byte[] VoiceWaveForm { get; set; } // voice wave form
}
public class StickerAttrs : MediaAttrs
{
public string AssociatedEmoji { get; set; } // the associated emoji with this
public string StickerSetID { get; set; } // id of sticker set which is associated with this sticker
public bool Animated { get; set; } // is client need to animate this sticker
}
public class VideoAttrs : MediaAttrs
{
public bool IsVideoMessage { get; set; } // is this a rounded video message
public decimal Duration { get; set; } // duration of video
public decimal Width { get; set; } // width of video
public decimal Height { get; set; } // height of video
}
public class PhotoAttrs : MediaAttrs
{
public decimal Width { get; set; } // width of photo
public decimal Height { get; set; } // height of photo
}
public class GeolocationAttrs : MediaAttrs
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
}

View File

@ -0,0 +1,7 @@
namespace ChatSubsystem.Storage.Models
{
public class Reaction
{
public string ID { get; set; }
}
}

View File

@ -1,32 +0,0 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace MessageStorage
{
public class Message
{
public ObjectId Id { get; set; }
public string From { get; set; }
public string To { get; set; }
public string RoomId { get; set; }
public string Type { get; set; }
public long Timestamp { get; set; }
public string Content { get; set; }
public Media[] Media { get; set; }
public Reaction[] Reactions { get; set; }
[BsonExtraElements]
public BsonDocument OtherMetadata { get; set; }
}
public class Media
{
public string MimeType;
public string Url;
}
public class Reaction
{
public string ID { get; set; }
}
}

View File

@ -1,14 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../../Zirconium/Zirconium.csproj">
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>

View File

@ -1,18 +0,0 @@
using System;
using Zirconium.Core.Plugins.Interfaces;
namespace MessageStorage
{
public class Plugin : IPluginAPI
{
public void Initialize(IPluginHostAPI pluginHost)
{
throw new NotImplementedException();
}
public string GetPluginUniqueName()
{
return "MessageStoragePlugin";
}
}
}