public MastodonNotification(int id, string type, MastodonAccount account, MastodonStatus status) : base(id) { if (account != null) { this._accounts.Add(account); } if (status != null) { this.Status = status; } switch (type) { case "mention": this.Type = NotificationType.Mention; break; case "reblog": this.Type = NotificationType.Boost; break; case "favourite": this.Type = NotificationType.Favorite; break; case "follow": this.Type = NotificationType.Follow; break; default: this.Type = NotificationType.Unknown; break; } }
public Task ReceiveStatus(MastodonStatusData data, CancellationToken cancellationToken) { var status = new MastodonStatus( new AccountIdentifier(new Username(data.Username), new Instance(data.Instance)), data.AuthorDisplayName, data.AuthorUsername, data.Content, data.MediaDescriptions); var container = _readingTextContainerRepository.GetContainer(); container.Add(status.ConvertToReadingText()); return(Task.CompletedTask); }
public IEnumerable <TootSpan> Parse(MastodonStatus status) { var content = status.Content; var encoding = Encoding.UTF8; var parsedSpans = new List <TootSpan>(); // なんか一応 div で囲ってみてる(今のところ <p></p> で囲まれてるtootしかみたことないけど) using (var stream = new MemoryStream(encoding.GetBytes("<div>" + content.Replace("<br>", "<br/>") + "</div>"))) using (var reader = new SgmlReader(stream)) { XDocument doc = XDocument.Load(reader); //Console.WriteLine("start----"); //Console.WriteLine("raw--"); //Console.WriteLine(content); //Console.WriteLine("--raw"); // 最初に見つかった XText の Node の親の Node群を列挙する → いきなり @amay とかで始まるとだめだー //var firstTextNode = doc.DescendantNodes().FirstOrDefault(node => node is XText); //var contentRoot = firstTextNode?.Parent?.Nodes() // しょうがないので決め打ち;(SgmlReader にしたら勝手に <html></html> が付いた) var contentRoot = doc?.Element("html")?.Element("div")?.Element("p") ?? null; foreach (var node in contentRoot?.Nodes() ?? new XNode[0]) { if (node.NodeType == XmlNodeType.Text) { var xText = node as XText; parsedSpans.Add(TootSpan.MakeText(xText.Value)); //Console.WriteLine($"text - {xText.Value}"); } else if (node.NodeType == XmlNodeType.Element) { var element = node as XElement; switch (element.Name.LocalName.ToLower()) { case "a": { var span = ParseLink(element, status.MediaAttachments, status.Tags); parsedSpans.Add(span); break; } case "br": { parsedSpans.Add(TootSpan.MakeLineBreak()); break; } case "span": { // なんか @amay みたいなメンションは span になるらしいが判別できないのでただの Text とする parsedSpans.Add(TootSpan.MakeText(element.Value)); break; } default: { //Console.WriteLine("Unknown Element: [" + element.Name + "]" + element.Value); throw new NotSupportedException($"Unknown Element: [{element.Name}]{element.Value} - {status.Id}"); break; } } } else { // Unknown XNode //Console.WriteLine($"Unknown Node: {node}"); throw new NotSupportedException($"Unknown Node: {node} - {status.Id}"); } } //Console.WriteLine("----end"); } return(parsedSpans); }