| | | 1 | | using System.ServiceModel.Syndication; |
| | | 2 | | |
| | | 3 | | namespace GistBackend.Utils; |
| | | 4 | | |
| | | 5 | | public static class SyndicationItemExtensions { |
| | | 6 | | extension(SyndicationItem item) |
| | | 7 | | { |
| | | 8 | | public string ExtractAuthor() |
| | | 9 | | { |
| | | 10 | | if (item.Authors is null) return ""; |
| | | 11 | | if (item.Authors.Count != 0) return string.Join(", ", item.Authors.Select(person => person.ExtractName())); |
| | | 12 | | return item.ElementExtensions |
| | | 13 | | .Where(ext => ext.OuterName == "creator") |
| | | 14 | | .Select(ext => ext.GetObject<string>()) |
| | | 15 | | .FirstOrDefault()?.Trim() ?? ""; |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public DateTime ExtractUpdated() => |
| | | 19 | | item.LastUpdatedTime > DateTimeOffset.UnixEpoch |
| | | 20 | | ? item.LastUpdatedTime.UtcDateTime |
| | | 21 | | : item.PublishDate.UtcDateTime; |
| | | 22 | | |
| | | 23 | | public Uri ExtractUrl() => item.Links.First(link => link.RelationshipType != "enclosure").Uri; |
| | | 24 | | |
| | | 25 | | public IEnumerable<string> ExtractCategories() => |
| | | 26 | | item.Categories.Select(category => category.Name.Trim()); |
| | | 27 | | } |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public static class SyndicationPersonExtensions { |
| | | 31 | | public static string ExtractName(this SyndicationPerson person) => |
| | 177 | 32 | | string.IsNullOrWhiteSpace(person.Name) |
| | 177 | 33 | | ? string.IsNullOrWhiteSpace(person.Email) |
| | 177 | 34 | | ? "" |
| | 177 | 35 | | : person.Email.Trim().Replace("(", "").Replace(")", "") |
| | 177 | 36 | | : person.Name.Trim(); |
| | | 37 | | } |