Skip to content

Commit

Permalink
Merge branch 'master' into passport
Browse files Browse the repository at this point in the history
  • Loading branch information
poulad committed Sep 1, 2018
2 parents 649af61 + c528ed5 commit 212d97e
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 82 deletions.
2 changes: 1 addition & 1 deletion deps/Telegram.Bot
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

// ReSharper disable CheckNamespace
Expand Down Expand Up @@ -35,11 +36,12 @@ public abstract class PassportElementError
/// <param name="source">Error source</param>
/// <param name="type">The section of the user's Telegram Passport which has the issue</param>
/// <param name="message">Error message</param>
/// <exception cref="ArgumentNullException">if any argument is null</exception>
protected PassportElementError(string source, string type, string message)
{
Type = type;
Source = source;
Message = message;
Type = type ?? throw new ArgumentNullException(nameof(type));
Source = source ?? throw new ArgumentNullException(nameof(source));
Message = message ?? throw new ArgumentNullException(nameof(message));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

// ReSharper disable CheckNamespace
Expand Down Expand Up @@ -33,6 +34,7 @@ public class PassportElementErrorDataField : PassportElementError
/// <param name="fieldName">Name of the data field which has the error</param>
/// <param name="dataHash">Base64-encoded data hash</param>
/// <param name="message">Error message</param>
/// <exception cref="ArgumentNullException">if any argument is null</exception>
public PassportElementErrorDataField(
string type,
string fieldName,
Expand All @@ -41,8 +43,8 @@ string message
)
: base("data", type, message)
{
FieldName = fieldName;
DataHash = dataHash;
FieldName = fieldName ?? throw new ArgumentNullException(nameof(fieldName));
DataHash = dataHash ?? throw new ArgumentNullException(nameof(dataHash));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

// ReSharper disable CheckNamespace
Expand Down Expand Up @@ -26,14 +27,15 @@ public class PassportElementErrorFile : PassportElementError
/// </param>
/// <param name="fileHash">Base64-encoded file hash</param>
/// <param name="message">Error message</param>
/// <exception cref="ArgumentNullException">if any argument is null</exception>
public PassportElementErrorFile(
string type,
string fileHash,
string message
)
: base("file", type, message)
{
FileHash = fileHash;
FileHash = fileHash ?? throw new ArgumentNullException(nameof(fileHash));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

Expand Down Expand Up @@ -27,14 +28,15 @@ public class PassportElementErrorFiles : PassportElementError
/// </param>
/// <param name="fileHashes">List of base64-encoded file hashes</param>
/// <param name="message">Error message</param>
/// <exception cref="ArgumentNullException">if any argument is null</exception>
public PassportElementErrorFiles(
string type,
IEnumerable<string> fileHashes,
string message
)
: base("files", type, message)
{
FileHashes = fileHashes;
FileHashes = fileHashes ?? throw new ArgumentNullException(nameof(fileHashes));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

// ReSharper disable CheckNamespace
Expand Down Expand Up @@ -26,14 +27,15 @@ public class PassportElementErrorFrontSide : PassportElementError
/// </param>
/// <param name="fileHash">Base64-encoded hash of the file with the front side of the document</param>
/// <param name="message">Error message</param>
/// <exception cref="ArgumentNullException">if any argument is null</exception>
public PassportElementErrorFrontSide(
string type,
string fileHash,
string message
)
: base("front_side", type, message)
{
FileHash = fileHash;
FileHash = fileHash ?? throw new ArgumentNullException(nameof(fileHash));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

// ReSharper disable CheckNamespace
Expand All @@ -25,14 +26,15 @@ public class PassportElementErrorReverseSide : PassportElementError
/// </param>
/// <param name="fileHash">Base64-encoded hash of the file with the reverse side of the document</param>
/// <param name="message">Error message</param>
/// <exception cref="ArgumentNullException">if any argument is null</exception>
public PassportElementErrorReverseSide(
string type,
string fileHash,
string message
)
: base("reverse_side", type, message)
{
FileHash = fileHash;
FileHash = fileHash ?? throw new ArgumentNullException(nameof(fileHash));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

// ReSharper disable CheckNamespace
Expand Down Expand Up @@ -26,14 +27,15 @@ public class PassportElementErrorSelfie : PassportElementError
/// </param>
/// <param name="fileHash">Base64-encoded hash of the file with the selfie</param>
/// <param name="message">Error message</param>
/// <exception cref="ArgumentNullException">if any argument is null</exception>
public PassportElementErrorSelfie(
string type,
string fileHash,
string message
)
: base("selfie", type, message)
{
FileHash = fileHash;
FileHash = fileHash ?? throw new ArgumentNullException(nameof(fileHash));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

// ReSharper disable CheckNamespace
Expand Down Expand Up @@ -27,14 +28,15 @@ public class PassportElementErrorTranslationFile : PassportElementError
/// </param>
/// <param name="fileHash">Base64-encoded file hash</param>
/// <param name="message">Error message</param>
/// <exception cref="ArgumentNullException">if any argument is null</exception>
public PassportElementErrorTranslationFile(
string type,
string fileHash,
string message
)
: base("translation_file", type, message)
{
FileHash = fileHash;
FileHash = fileHash ?? throw new ArgumentNullException(nameof(fileHash));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

Expand Down Expand Up @@ -28,14 +29,15 @@ public class PassportElementErrorTranslationFiles : PassportElementError
/// </param>
/// <param name="fileHashes">List of base64-encoded file hashes</param>
/// <param name="message">Error message</param>
/// <exception cref="ArgumentNullException">if any argument is null</exception>
public PassportElementErrorTranslationFiles(
string type,
IEnumerable<string> fileHashes,
string message
)
: base("translation_files", type, message)
{
FileHashes = fileHashes;
FileHashes = fileHashes ?? throw new ArgumentNullException(nameof(fileHashes));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

// ReSharper disable CheckNamespace
Expand All @@ -24,14 +25,15 @@ public class PassportElementErrorUnspecified : PassportElementError
/// </param>
/// <param name="elementHash">Base64-encoded element hash</param>
/// <param name="message">Error message</param>
/// <exception cref="ArgumentNullException">if any argument is null</exception>
public PassportElementErrorUnspecified(
string type,
string elementHash,
string message
)
: base("unspecified", type, message)
{
ElementHash = elementHash;
ElementHash = elementHash ?? throw new ArgumentNullException(nameof(elementHash));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Telegram.Bot.Types.Passport;
Expand Down Expand Up @@ -34,11 +35,14 @@ public class SetPassportDataErrorsRequest : RequestBase<bool>
/// </summary>
/// <param name="userId">User identifier</param>
/// <param name="errors">Descriptions of the errors</param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="errors"/> is null
/// </exception>
public SetPassportDataErrorsRequest(int userId, IEnumerable<PassportElementError> errors)
: base("setPassportDataErrors")
{
UserId = userId;
Errors = errors;
Errors = errors ?? throw new ArgumentNullException(nameof(errors));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
Expand All @@ -10,6 +11,9 @@
// ReSharper disable once CheckNamespace
namespace Telegram.Bot
{
/// <summary>
/// Contains extension methods for <see cref="ITelegramBotClient"/> instances
/// </summary>
public static class TelegramBotClientPassportExtensions
{
/// <summary>
Expand All @@ -23,8 +27,8 @@ public static class TelegramBotClientPassportExtensions
/// </summary>
/// <param name="botClient">Instance of bot client</param>
/// <param name="userId">User identifier</param>
/// <param name="errors">A JSON-serialized array describing the errors</param>
/// <param name="cancellationToken">Task cancellation token</param>
/// <param name="errors">Descriptions of the errors</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <see href="https://core.telegram.org/bots/api#setpassportdataerrors"/>
public static Task SetPassportDataErrorsAsync(
this ITelegramBotClient botClient,
Expand All @@ -34,6 +38,20 @@ public static Task SetPassportDataErrorsAsync(
) =>
botClient.MakeRequestAsync(new SetPassportDataErrorsRequest(userId, errors), cancellationToken);

/// <summary>
/// Downloads an ecnrypted Passport file, decrypts it, and writes the content to
/// <paramref name="destination"/> stream
/// </summary>
/// <param name="botClient">Instance of bot client</param>
/// <param name="passportFile"></param>
/// <param name="fileCredentials"></param>
/// <param name="destination"></param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>File information of the encrypted Passport file on Telegram servers.</returns>
/// <exception cref="ArgumentNullException">If null arguments are passed</exception>
/// <exception cref="ArgumentException">
/// If <paramref name="destination"/> stream is not writable.
/// </exception>
public static async Task<File> DownloadAndDecryptPassportFileAsync(
this ITelegramBotClient botClient,
PassportFile passportFile,
Expand All @@ -42,6 +60,16 @@ public static async Task<File> DownloadAndDecryptPassportFileAsync(
CancellationToken cancellationToken = default
)
{
if (passportFile == null)
{
throw new ArgumentNullException(nameof(passportFile));
}

if (!destination.CanWrite)
{
throw new ArgumentException("Stream msut be writable.", nameof(destination));
}

File fileInfo;

var encryptedContentStream = passportFile.FileSize > 0
Expand Down
Loading

0 comments on commit 212d97e

Please sign in to comment.