<!-- Taken from the https://carlschwan.eu/2020/12/29/adding-comments-to-your-static-blog-with-mastodon/ -->

{% if page.extra.comments.host %}
	{% set host = page.extra.comments.host %}
{% else %}
	{% set host = config.extra.comments.host %}
{% endif %}

{% if page.extra.comments.user %}
	{% set username = page.extra.comments.user %}
{% else %}
	{% set username = config.extra.comments.user %}
{% endif %}

{% set id = page.extra.comments.id %}

<section id="comments">
	{% if config.extra.comments.show_qr %}
		<img id="qrcode" class="no-hover pixels" alt="QR code to a Mastodon post" src="https://api.qrserver.com/v1/create-qr-code/?data=https://{{ host }}/@{{ username }}/{{ id }}&format=gif" />
	{% endif %}

	<h2>Comments</h2>
	<p>
		You can comment on this blog post by publicly replying to this <a href="https://{{ host }}/@{{ username }}/{{ id }}">post</a> using a Mastodon or other ActivityPub/Fediverse account. Known non-private replies are displayed below.
	</p>

	<p>
		<a id="load-comments" class="inline-button" onclick="loadComments()">Load Comments</a>
	</p>

	<div id="comments-wrapper">
		<noscript>
			<p>
				Loading comments relies on JavaScript. Try enabling JavaScript and
				reloading, or visit
				<a href="https://{{ host }}/@{{ username }}/{{ id }}">
					the original post
				</a>
				on Mastodon.
			</p>
		</noscript>
	</div>
	<script type="text/javascript">
		function escapeHtml(unsafe) {
			return unsafe
				.replace(/&/g, "&amp;")
				.replace(/</g, "&lt;")
				.replace(/>/g, "&gt;")
				.replace(/"/g, "&quot;")
				.replace(/'/g, "&#039;");
		}
		function emojify(input, emojis) {
			let output = input;

			emojis.forEach((emoji) => {
				let picture = document.createElement("picture");

				let source = document.createElement("source");
				source.setAttribute("srcset", escapeHtml(emoji.url));
				source.setAttribute("media", "(prefers-reduced-motion: no-preference)");

				let img = document.createElement("img");
				img.className = "emoji";
				img.setAttribute("src", escapeHtml(emoji.static_url));
				img.setAttribute("alt", `:${emoji.shortcode}:`);
				img.setAttribute("title", `:${emoji.shortcode}:`);
				img.setAttribute("width", "20");
				img.setAttribute("height", "20");

				picture.appendChild(source);
				picture.appendChild(img);

				output = output.replace(`:${emoji.shortcode}:`, picture.outerHTML);
			});

			return output;
		}

		function loadComments() {
			let commentsWrapper = document.getElementById("comments-wrapper");
			document.getElementById("load-comments").innerHTML = "Loading…";
			fetch("https://{{ host }}/api/v1/statuses/{{ id }}/context")
				.then(function (response) {
					return response.json();
				})
				.then(function (data) {
					let descendants = data["descendants"];
					if (
						descendants &&
						Array.isArray(descendants) &&
						descendants.length > 0
					) {
						commentsWrapper.innerHTML = "";

						descendants.forEach(function (status) {
							console.log(descendants);
							if (status.account.display_name.length > 0) {
								status.account.display_name = escapeHtml(
									status.account.display_name
								);
								status.account.display_name = emojify(
									status.account.display_name,
									status.account.emojis
								);
							} else {
								status.account.display_name = status.account.username;
							}

							let instance = "";
							if (status.account.acct.includes("@")) {
								instance = status.account.acct.split("@")[1];
							} else {
								instance = "{{ host }}";
							}

							const isReply = status.in_reply_to_id !== "{{ id }}";

							let op = false;
							if (status.account.acct == "{{ username }}") {
								op = true;
							}

							status.content = emojify(status.content, status.emojis);

							let avatarSource = document.createElement("source");
							avatarSource.setAttribute(
								"srcset",
								escapeHtml(status.account.avatar)
							);
							avatarSource.setAttribute(
								"media",
								"(prefers-reduced-motion: no-preference)"
							);

							let avatarImg = document.createElement("img");
							avatarImg.className = "avatar";
							avatarImg.setAttribute(
								"src",
								escapeHtml(status.account.avatar_static)
							);
							avatarImg.setAttribute(
								"alt",
								`@${status.account.username}@${instance} avatar`
							);

							let avatarPicture = document.createElement("picture");
							avatarPicture.appendChild(avatarSource);
							avatarPicture.appendChild(avatarImg);

							let avatar = document.createElement("a");
							avatar.className = "avatar-link";
							avatar.setAttribute("href", status.account.url);
							avatar.setAttribute("rel", "external nofollow");
							avatar.setAttribute(
								"title",
								`View profile at @${status.account.username}@${instance}`
							);
							avatar.appendChild(avatarPicture);

							let instanceBadge = document.createElement("a");
							instanceBadge.className = "instance";
							instanceBadge.setAttribute("href", status.account.url);
							instanceBadge.setAttribute(
								"title",
								`@${status.account.username}@${instance}`
							);
							instanceBadge.setAttribute("rel", "external nofollow");
							instanceBadge.textContent = instance;

							let display = document.createElement("span");
							display.className = "display";
							display.setAttribute("itemprop", "author");
							display.setAttribute("itemtype", "http://schema.org/Person");
							display.innerHTML = status.account.display_name;

							let header = document.createElement("header");
							header.className = "author";
							header.appendChild(display);
							header.appendChild(instanceBadge);

							let permalink = document.createElement("a");
							permalink.setAttribute("href", status.url);
							permalink.setAttribute("itemprop", "url");
							permalink.setAttribute("title", `View comment at ${instance}`);
							permalink.setAttribute("rel", "external nofollow");
							permalink.textContent = new Date(
								status.created_at
							).toLocaleString("{{ config.extra.comments.locale | default(value='en-IE') }}", {
								dateStyle: "long",
								timeStyle: "short",
							});

							let timestamp = document.createElement("time");
							timestamp.setAttribute("datetime", status.created_at);
							timestamp.appendChild(permalink);

							let main = document.createElement("main");
							main.setAttribute("itemprop", "text");
							main.innerHTML = status.content;

							let interactions = document.createElement("footer");

							let boosts = document.createElement("a");
							boosts.className = "boosts";
							boosts.setAttribute("href", `${status.url}/reblogs`);
							boosts.setAttribute("title", `Favorites from ${instance}`);

							let boostsIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
							boostsIcon.setAttribute("xmlns", "http://www.w3.org/2000/svg");
							boostsIcon.setAttribute("width", "16");
							boostsIcon.setAttribute("height", "16");
							boostsIcon.setAttribute("viewBox", "0 0 16 16");

							let boostsPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
							boostsPath.setAttribute("fill", "currentColor");
							boostsPath.setAttribute("d", "M8 1v2H4C1.8 3 0 4.8 0 7v2a4 4 0 0 0 1.02 2.672 1 1 0 1 0 1.488-1.336A1.97 1.97 0 0 1 2 9V7c0-1.125.875-2 2-2h4v2h1v-.008a1 1 0 0 0 .707-.285l2-2a1 1 0 0 0 0-1.414l-2-2A1 1 0 0 0 9 1.008V1zm6.29 3a1 1 0 0 0-.72.258.993.993 0 0 0-.078 1.41c.317.355.508.816.508 1.34v2c0 1.125-.875 2-2 2H8V9H7v.008a1 1 0 0 0-.707.285l-2 2a1 1 0 0 0 0 1.414l2 2c.187.184.441.29.707.285V15h1v-1.992h4c2.2 0 4-1.805 4-4v-2a4 4 0 0 0-1.02-2.676A1 1 0 0 0 14.29 4m0 0");
							boostsIcon.appendChild(boostsPath);
							boosts.appendChild(boostsIcon);
							boosts.insertAdjacentHTML('beforeend', ` ${status.reblogs_count}`);
							interactions.appendChild(boosts);

							let faves = document.createElement("a");
							faves.className = "faves";
							faves.setAttribute("href", `${status.url}/favourites`);
							faves.setAttribute("title", `Favorites from ${instance}`);

							let favesIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
							favesIcon.setAttribute("xmlns", "http://www.w3.org/2000/svg");
							favesIcon.setAttribute("width", "16");
							favesIcon.setAttribute("height", "16");
							favesIcon.setAttribute("viewBox", "0 0 16 16");

							let favesPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
							favesPath.setAttribute("fill", "currentColor");
							favesPath.setAttribute("d", "M8 0a1 1 0 0 0-.95.684l-1.448 4.34-4.59-.016C.032 5.004-.371 6.266.43 6.828l3.625 2.555-1.5 4.285c-.317.902.687 1.691 1.492 1.172l4.004-2.594 3.894 2.586c.801.531 1.817-.258 1.5-1.16l-1.504-4.29 3.645-2.577c.789-.563.394-1.809-.574-1.813l-4.66-.015L8.949.69A1 1 0 0 0 8 0m0 0");
							favesIcon.appendChild(favesPath);
							faves.appendChild(favesIcon);
							faves.insertAdjacentHTML('beforeend', ` ${status.favourites_count}`);
							interactions.appendChild(faves);

							let comment = document.createElement("article");
							comment.id = `comment-${status.id}`;
							comment.className = isReply ? "comment comment-reply" : "comment";
							comment.setAttribute("itemprop", "comment");
							comment.setAttribute("itemtype", "http://schema.org/Comment");
							comment.appendChild(avatar);
							comment.appendChild(header);
							comment.appendChild(timestamp);
							comment.appendChild(main);
							comment.appendChild(interactions);

							if (op === true) {
								comment.classList.add("op");

								avatar.classList.add("op");
								avatar.setAttribute(
									"title",
									"Blog post author; " + avatar.getAttribute("title")
								);

								instanceBadge.classList.add("op");
								instanceBadge.setAttribute(
									"title",
									"Blog post author: " + instanceBadge.getAttribute("title")
								);
							}

							commentsWrapper.innerHTML += comment.outerHTML;
						});
					}
					let loadCommentsButton = document.getElementById("load-comments");
					loadCommentsButton.remove();
				});
		}
	</script>
</section>
