-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Реализация простого лонгпула для пользователей. Co-authored-by: inyutin-maxim <[email protected]>
- Loading branch information
1 parent
9cda8b2
commit 668329b
Showing
10 changed files
with
471 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#nullable enable | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace VkNet.Utils.UsersLongPool; | ||
|
||
/// <summary> | ||
/// Обработчик лонгпула пользовательских сообщений | ||
/// </summary> | ||
public interface IUsersLongPoolUpdatesHandler | ||
{ | ||
/// <summary> | ||
/// Запуск отслеживания событий | ||
/// </summary> | ||
/// <param name="token">Токен отмены операции</param> | ||
Task RunAsync(CancellationToken token = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#nullable enable | ||
using Newtonsoft.Json.Linq; | ||
using VkNet.Model; | ||
|
||
namespace VkNet.Utils.UsersLongPool; | ||
|
||
/// <summary> | ||
/// Обёртка для Message, в которой кроме самого сообщения есть и ошибки при парсинге. | ||
/// </summary> | ||
public class UserMessageEvent | ||
{ | ||
/// <summary> | ||
/// Сообщение | ||
/// </summary> | ||
public Message? Message = null; | ||
|
||
/// <summary> | ||
/// Ошибка парсинга сообщения | ||
/// </summary> | ||
public System.Exception? Exception = null; | ||
|
||
/// <summary> | ||
/// Сообщение в JObject | ||
/// </summary> | ||
public JObject RawMessage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#nullable enable | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json.Linq; | ||
using VkNet.Model; | ||
|
||
namespace VkNet.Utils.UsersLongPool; | ||
|
||
/// <summary> | ||
/// Методы для обработки событий лонгпула у сообществ. | ||
/// </summary> | ||
public static class UsersLongPoolHelpers | ||
{ | ||
/// <summary> | ||
/// Метод для получения сообщений из массива JObject, который не бросает исключений, но вместе с сообщениями возвращает ошибки при десериализации, если таковые имеются. | ||
/// </summary> | ||
/// <param name="jObjectMessages">Этот массив получается из метода api.Messages.GetLongPollHistory<LongPollHistoryResponse<JObject>>().Messages</param> | ||
/// <returns> | ||
/// Возвращает список сообщений пользователя | ||
/// </returns> | ||
public static List<UserMessageEvent> GetUserMessageEvents(IEnumerable<JObject> jObjectMessages) | ||
{ | ||
var userMessageEvents = new List<UserMessageEvent>(); | ||
|
||
foreach (var jObjectMessage in jObjectMessages) | ||
{ | ||
try | ||
{ | ||
var message = jObjectMessage.ToObject<Message>(); | ||
|
||
userMessageEvents.Add(new() | ||
{ | ||
Message = message, | ||
RawMessage = jObjectMessage | ||
}); | ||
} | ||
catch (System.Exception ex) | ||
{ | ||
userMessageEvents.Add(new() | ||
{ | ||
Exception = ex, | ||
RawMessage = jObjectMessage | ||
}); | ||
} | ||
} | ||
|
||
return userMessageEvents; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#nullable enable | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json.Linq; | ||
using VkNet.Model; | ||
|
||
namespace VkNet.Utils.UsersLongPool; | ||
|
||
/// <summary> | ||
/// Обёртка для UsersLongPoolUpdatesHandlerParams.OnUpdates, в которой содержится вся информация о текущем массиве событий лонгпула для пользователя. | ||
/// </summary> | ||
public class UsersLongPoolOnUpdatesEvent | ||
{ | ||
/// <summary> | ||
/// Обновление в событиях пользователя. | ||
/// </summary> | ||
public LongPollHistoryResponse<JObject> Response; | ||
|
||
/// <summary> | ||
/// Обработанные сообщения из Response | ||
/// </summary> | ||
public List<UserMessageEvent> Messages; | ||
} |
Oops, something went wrong.