1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2024-10-17 09:42:45 +02:00
TweetDuck/lib/TweetLib.Core/Features/Notifications/DesktopNotification.cs
2022-02-05 23:34:00 +01:00

95 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TweetLib.Utils.Data;
namespace TweetLib.Core.Features.Notifications {
public sealed class DesktopNotification {
private const string DefaultHeadLayout = @"<html class=""scroll-v os-windows dark txt-size--14"" lang=""en-US"" id=""tduck"" data-td-font=""medium"" data-td-theme=""dark""><head><meta charset=""utf-8""><link href=""https://ton.twimg.com/tweetdeck-web/web/dist/bundle.4b1f87e09d.css"" rel=""stylesheet""><style type='text/css'>body { background: rgb(34, 36, 38) !important }</style>";
public enum Position {
TopLeft,
TopRight,
BottomLeft,
BottomRight,
Custom
}
public enum Size {
Auto,
Custom
}
public string ColumnId { get; }
public string ChirpId { get; }
public string ColumnTitle { get; }
public string TweetUrl { get; }
public string QuoteUrl { get; }
private readonly string html;
private readonly int characters;
public DesktopNotification(string columnId, string chirpId, string title, string html, int characters, string tweetUrl, string quoteUrl) {
this.ColumnId = columnId;
this.ChirpId = chirpId;
this.ColumnTitle = title;
this.TweetUrl = tweetUrl;
this.QuoteUrl = quoteUrl;
this.html = html;
this.characters = characters;
}
public int GetDisplayDuration(int value) {
return 2000 + Math.Max(1000, value * characters);
}
internal string GenerateHtml(string bodyClasses, string? headLayout, string? customStyles, IEnumerable<InjectedString> injections, string[] scripts) { // TODO
headLayout ??= DefaultHeadLayout;
customStyles ??= string.Empty;
StringBuilder build = new StringBuilder(1000);
build.Append("<!DOCTYPE html>");
build.Append(headLayout);
build.Append("<link rel='stylesheet' href='td://resources/notification/notification.css'>");
if (!string.IsNullOrWhiteSpace(customStyles)) {
build.Append("<style>").Append(customStyles).Append("</style>");
}
build.Append("</head><body class='scroll-styled-v");
if (!string.IsNullOrEmpty(bodyClasses)) {
build.Append(' ').Append(bodyClasses);
}
build.Append("'><div class='column' style='width:100%!important;min-height:100vh!important;height:auto!important;overflow:initial!important;'>");
build.Append(html);
build.Append("</div>");
build.Append("<tweetduck-script-placeholder></body></html>");
string result = build.ToString();
return injections.Aggregate(result, static (current, injection) => injection.InjectInto(current)).Replace("<tweetduck-script-placeholder>", GenerateScripts(scripts));
}
private string GenerateScripts(string[] scripts) {
if (scripts.Length == 0) {
return "";
}
StringBuilder build = new StringBuilder();
foreach (string script in scripts) {
build.Append("<script type='text/javascript'>");
build.Append(script);
build.Append("</script>");
}
return build.ToString();
}
}
}