Skip to content

Commit

Permalink
Feat/conversation conversations (#36)
Browse files Browse the repository at this point in the history
* feat: implement requests

create
list
list auto
stop
update
delete
inject

* fix(conversation/messages): use appropriate json structure

* fix(messages): include accept_time

* feat(tests): add e2e tests for conversations

* fix: typo in doc of ConversationChannel.cs

* chore: update AccessingPolymorphicType.cs with actual types
  • Loading branch information
Dovchik authored Feb 1, 2024
1 parent ce14a3a commit 3dd63db
Show file tree
Hide file tree
Showing 32 changed files with 1,192 additions and 255 deletions.
27 changes: 0 additions & 27 deletions examples/Console/AccessingPolymorhycType.cs

This file was deleted.

33 changes: 33 additions & 0 deletions examples/Console/AccessingPolymorphicType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Sinch;
using Sinch.Verification.Report.Request;
using Sinch.Verification.Report.Response;

namespace Examples
{
public class AccessingPolymorphicType
{
public static void Example()
{
var sinchClient = new SinchClient("KEY_ID", "KEY_SECRET", "PROJECT_ID");
var response = sinchClient.Verification("APP_KEY", "APP_SECRET").Verification
.ReportId("id", new SmsVerificationReportRequest()
{
Sms = new SmsVerify()
{
Code = "123",
Cli = "it's a cli"
}
}).Result;
var id = response switch
{
FlashCallVerificationReportResponse flashCallVerificationReportResponse =>
flashCallVerificationReportResponse.Id,
PhoneCallVerificationReportResponse phoneCallVerificationReportResponse =>
phoneCallVerificationReportResponse.Id,
SmsVerificationReportResponse smsVerificationReportResponse => smsVerificationReportResponse.Id,
_ => throw new ArgumentOutOfRangeException(nameof(response))
};
Console.WriteLine(id);
}
}
}
33 changes: 10 additions & 23 deletions src/Sinch/Conversation/Contacts/Contact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@

namespace Sinch.Conversation.Contacts
{
public sealed class Contact
public sealed class Contact : PropertyMaskQuery
{
/// <summary>
/// Tracks the fields which where initialized.
/// </summary>
private readonly ISet<string> _setFields = new HashSet<string>();

private List<ChannelIdentity> _channelIdentities;
private List<ConversationChannel> _channelPriority;
private string _displayName;
Expand All @@ -30,7 +25,7 @@ public List<ChannelIdentity> ChannelIdentities
get => _channelIdentities;
set
{
_setFields.Add(nameof(ChannelIdentities));
SetFields.Add(nameof(ChannelIdentities));
_channelIdentities = value;
}
}
Expand All @@ -44,7 +39,7 @@ public List<ConversationChannel> ChannelPriority
get => _channelPriority;
set
{
_setFields.Add(nameof(ChannelPriority));
SetFields.Add(nameof(ChannelPriority));
_channelPriority = value;
}
}
Expand All @@ -58,7 +53,7 @@ public string DisplayName
get => _displayName;
set
{
_setFields.Add(nameof(DisplayName));
SetFields.Add(nameof(DisplayName));
_displayName = value;
}
}
Expand All @@ -72,7 +67,7 @@ public string Email
get => _email;
set
{
_setFields.Add(nameof(Email));
SetFields.Add(nameof(Email));
_email = value;
}
}
Expand All @@ -86,7 +81,7 @@ public string ExternalId
get => _externalId;
set
{
_setFields.Add(nameof(ExternalId));
SetFields.Add(nameof(ExternalId));
_externalId = value;
}
}
Expand All @@ -100,7 +95,7 @@ public string Id
get => _id;
set
{
_setFields.Add(nameof(Id));
SetFields.Add(nameof(Id));
_id = value;
}
}
Expand All @@ -114,7 +109,7 @@ public ConversationLanguage Language
get => _language;
set
{
_setFields.Add(nameof(Language));
SetFields.Add(nameof(Language));
_language = value;
}
}
Expand All @@ -128,20 +123,12 @@ public string Metadata
get => _metadata;
set
{
_setFields.Add(nameof(Metadata));
SetFields.Add(nameof(Metadata));
_metadata = value;
}
}

/// <summary>
/// Get the comma separated snake_case list of properties which were directly initialized in this object.
/// If, for example, DisplayName and Metadata were set, will return <example>display_name,metadata</example>
/// </summary>
/// <returns></returns>
internal string GetPropertiesMask()
{
return string.Join(',', _setFields.Select(StringUtils.ToSnakeCase));
}


/// <summary>
/// Returns the string presentation of the object
Expand Down
5 changes: 5 additions & 0 deletions src/Sinch/Conversation/ConversationChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,10 @@ public record ConversationChannel(string Value) : EnumRecord(Value)
/// WeChat channel.
/// </summary>
public static readonly ConversationChannel WeChat = new("WECHAT");

/// <summary>
/// Channel has not been specified
/// </summary>
public static readonly ConversationChannel Unspecified = new("CHANNEL_UNSPECIFIED");
}
}
147 changes: 147 additions & 0 deletions src/Sinch/Conversation/Conversations/Conversation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using System;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Sinch.Core;

namespace Sinch.Conversation.Conversations
{
/// <summary>
/// A collection of messages exchanged between a contact and an app. Conversations are normally created on the fly by
/// Conversation API once a message is sent and there is no active conversation already. There can be only one active
/// conversation at any given time between a particular contact and an app.
/// </summary>
public sealed class Conversation : PropertyMaskQuery
{
private bool? _active;
private ConversationChannel _activeChannel;
private string _appId;
private string _contactId;
private string _correlationId;
private string _metadata;
private JsonObject _metadataJson;

/// <summary>
/// Gets or Sets ActiveChannel
/// </summary>
public ConversationChannel ActiveChannel
{
get => _activeChannel;
set
{
SetFields.Add(nameof(ActiveChannel));
_activeChannel = value;
}
}


/// <summary>
/// Flag for whether this conversation is active.
/// </summary>
public bool? Active
{
get => _active;
set
{
SetFields.Add(nameof(Active));
_active = value;
}
}

/// <summary>
/// The ID of the participating app.
/// </summary>
public string AppId
{
get => _appId;
set
{
SetFields.Add(nameof(AppId));
_appId = value;
}
}

/// <summary>
/// The ID of the participating contact.
/// </summary>
public string ContactId
{
get => _contactId;
set
{
SetFields.Add(nameof(ContactId));
_contactId = value;
}
}

/// <summary>
/// The ID of the conversation.
/// </summary>
public string Id { get; set; }

/// <summary>
/// The timestamp of the latest message in the conversation.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public DateTime LastReceived { get; set; }

/// <summary>
/// Arbitrary data set by the Conversation API clients. Up to 1024 characters long.
/// </summary>
public string Metadata
{
get => _metadata;
set
{
SetFields.Add(nameof(Metadata));
_metadata = value;
}
}

/// <summary>
/// Arbitrary data set by the Conversation API clients and/or provided in the conversation_metadata field of a
/// SendMessageRequest. A valid JSON object.
/// </summary>
public JsonObject MetadataJson
{
get => _metadataJson;
set
{
SetFields.Add(nameof(MetadataJson));
_metadataJson = value;
}
}

public string CorrelationId
{
get => _correlationId;
set
{
SetFields.Add(nameof(CorrelationId));
_correlationId = value;
}
}


/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("class Conversation {\n");
sb.Append(" Active: ").Append(Active).Append("\n");
sb.Append(" ActiveChannel: ").Append(ActiveChannel).Append("\n");
sb.Append(" AppId: ").Append(AppId).Append("\n");
sb.Append(" ContactId: ").Append(ContactId).Append("\n");
sb.Append(" Id: ").Append(Id).Append("\n");
sb.Append(" LastReceived: ").Append(LastReceived).Append("\n");
sb.Append(" Metadata: ").Append(Metadata).Append("\n");
sb.Append(" MetadataJson: ").Append(MetadataJson).Append("\n");
sb.Append(" CorrelationId: ").Append(CorrelationId).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
}
}
Loading

0 comments on commit 3dd63db

Please sign in to comment.