Перевести входящий поток источников на generic-контракт
Добавлен sources.proto с PushSource; приём в ядре вынесен в SourceIngressGrpcService с SourceProtoMapper и ISourceIngestObserver, тенант определяется IngressTenantResolver. Из telegram.proto удалён PushMessage, telegram-сервис шлёт generic-записи, превью сохраняет TelegramSourceIngestObserver.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using Deal.Grpc.Sources;
|
||||
using Deal.Modules.Cards.Application.Sources;
|
||||
|
||||
namespace Deal.Api.Sources;
|
||||
|
||||
/// <summary>
|
||||
/// Маппинг generic-контракта источника в доменную модель.
|
||||
/// </summary>
|
||||
public static class SourceProtoMapper
|
||||
{
|
||||
/// <summary>
|
||||
/// Преобразует protobuf-запись источника в доменную.
|
||||
/// </summary>
|
||||
/// <param name="request">Запрос PushSource.</param>
|
||||
/// <returns>Запись источника домена.</returns>
|
||||
public static SourceItem ToItem(PushSourceRequest request) => new()
|
||||
{
|
||||
Source = ToRef(request.Source),
|
||||
Content = ToContent(request.Content),
|
||||
};
|
||||
|
||||
private static SourceRef ToRef(SourceRefProto proto) => new()
|
||||
{
|
||||
Kind = proto.Kind,
|
||||
ExternalId = proto.HasExternalId ? proto.ExternalId : null,
|
||||
DisplayName = proto.HasDisplayName ? proto.DisplayName : null,
|
||||
OriginRef = proto.HasOriginRef ? proto.OriginRef : null,
|
||||
Author = proto.HasAuthor ? proto.Author : null,
|
||||
ReceivedAt = proto.ReceivedAt != 0 ? DateTimeOffset.FromUnixTimeMilliseconds(proto.ReceivedAt) : DateTimeOffset.UtcNow,
|
||||
Extra = proto.Extra.Count > 0 ? new Dictionary<string, string>(proto.Extra) : null,
|
||||
};
|
||||
|
||||
private static SourceContent ToContent(SourceContentProto proto)
|
||||
{
|
||||
var data = new List<DataRef>(proto.Data.Count);
|
||||
foreach (DataRefProto item in proto.Data)
|
||||
{
|
||||
data.Add(ToData(item));
|
||||
}
|
||||
|
||||
var contacts = new List<ContactRef>(proto.Contacts.Count);
|
||||
foreach (ContactRefProto contact in proto.Contacts)
|
||||
{
|
||||
contacts.Add(ToContact(contact));
|
||||
}
|
||||
|
||||
return new SourceContent
|
||||
{
|
||||
Text = proto.HasText ? proto.Text : null,
|
||||
Html = proto.HasHtml ? proto.Html : null,
|
||||
Author = proto.HasAuthor ? proto.Author : null,
|
||||
Subject = proto.HasSubject ? proto.Subject : null,
|
||||
Data = data,
|
||||
Links = proto.Links.Count > 0 ? proto.Links.ToList() : null,
|
||||
Contacts = contacts.Count > 0 ? contacts : null,
|
||||
Extra = proto.Extra.Count > 0 ? new Dictionary<string, string>(proto.Extra) : null,
|
||||
};
|
||||
}
|
||||
|
||||
private static DataRef ToData(DataRefProto proto) => new()
|
||||
{
|
||||
Id = proto.Id,
|
||||
Ref = proto.Ref,
|
||||
Kind = proto.HasKind ? proto.Kind : null,
|
||||
MimeType = proto.HasMimeType ? proto.MimeType : null,
|
||||
FileName = proto.HasFileName ? proto.FileName : null,
|
||||
Size = proto.HasSize ? proto.Size : null,
|
||||
Width = proto.HasWidth ? proto.Width : null,
|
||||
Height = proto.HasHeight ? proto.Height : null,
|
||||
DurationSec = proto.HasDurationSec ? proto.DurationSec : null,
|
||||
PreviewRef = proto.HasPreviewRef ? proto.PreviewRef : null,
|
||||
Caption = proto.HasCaption ? proto.Caption : null,
|
||||
Order = proto.HasOrder ? proto.Order : null,
|
||||
Meta = proto.Meta.Count > 0 ? new Dictionary<string, string>(proto.Meta) : null,
|
||||
};
|
||||
|
||||
private static ContactRef ToContact(ContactRefProto proto) => new()
|
||||
{
|
||||
Kind = proto.HasKind ? proto.Kind : null,
|
||||
Name = proto.HasName ? proto.Name : null,
|
||||
Phone = proto.HasPhone ? proto.Phone : null,
|
||||
Email = proto.HasEmail ? proto.Email : null,
|
||||
Url = proto.HasUrl ? proto.Url : null,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user