| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Text.RegularExpressions; |
| | 3 | |
|
| | 4 | | namespace DCL.Chat.Channels |
| | 5 | | { |
| | 6 | | public static class ChannelUtils |
| | 7 | | { |
| | 8 | | private const string CHANNEL_MATCH_REGEX = "^#[a-zA-Z0-9-]{3,20}$"; |
| | 9 | | private const string NEAR_BY_CHANNEL = "~nearby"; |
| 1 | 10 | | private static readonly Regex filter = new (CHANNEL_MATCH_REGEX); |
| | 11 | |
|
| | 12 | | public static List<string> ExtractChannelIdsFromText(string text) |
| | 13 | | { |
| 2 | 14 | | List<string> channelsFound = new List<string>(); |
| | 15 | |
|
| 2 | 16 | | string[] separatedWords = text |
| | 17 | | .Replace("<noparse>", "") |
| | 18 | | .Replace("</noparse>", "") |
| | 19 | | .Replace('\n', ' ') |
| | 20 | | .Replace('.', ' ') |
| | 21 | | .Replace(',', ' ') |
| | 22 | | .Split(' '); |
| | 23 | |
|
| 76 | 24 | | for (int i = 0; i < separatedWords.Length; i++) |
| | 25 | | { |
| 36 | 26 | | if (IsAChannel(separatedWords[i])) |
| 8 | 27 | | channelsFound.Add(separatedWords[i]); |
| | 28 | | } |
| | 29 | |
|
| 2 | 30 | | return channelsFound; |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public static bool IsAChannel(string text) |
| | 34 | | { |
| 36 | 35 | | var match = filter.Match(text); |
| 36 | 36 | | return match.Success || text.ToLower() == NEAR_BY_CHANNEL; |
| | 37 | | } |
| | 38 | | } |
| | 39 | | } |