12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622 |
- "use strict";
- const path = require("path");
- const envPaths = require("env-paths");
- const fs = require("fs");
- const homedir = require('os').homedir();
- const supportingPath = path.join(homedir, ".ssb/flume/contacts2.json");
- const offsetPath = path.join(homedir, ".ssb/flume/log.offset");
- const debug = require("debug")("oasis");
- const highlightJs = require("highlight.js");
- const MarkdownIt = require("markdown-it");
- const prettyMs = require("pretty-ms");
- const updater = require("../updater.js");
- global.updaterequired = "";
- global.ck = updater.getRemoteVersion(async function(checkversion){
- if (checkversion === "required"){
- ck = "required";
- }
- });
- const {
- a,
- article,
- br,
- body,
- button,
- details,
- div,
- em,
- footer,
- form,
- h1,
- h2,
- head,
- header,
- hr,
- html,
- img,
- input,
- label,
- li,
- link,
- main,
- meta,
- nav,
- option,
- p,
- pre,
- progress,
- section,
- select,
- span,
- summary,
- textarea,
- title,
- ul,
- } = require("hyperaxe");
- const { about, blob, vote } = require("../models")({});
- const lodash = require("lodash");
- const markdown = require("./markdown");
- const i18nBase = require("./i18n");
- let selectedLanguage = "en";
- let i18n = i18nBase[selectedLanguage];
- exports.setLanguage = (language) => {
- selectedLanguage = language;
- i18n = Object.assign({}, i18nBase.en, i18nBase[language]);
- };
- const markdownUrl = "https://commonmark.org/help/";
- const snhUrl = "https://solarnethub.com/";
- const doctypeString = "<!DOCTYPE html>";
- const THREAD_PREVIEW_LENGTH = 3;
- const toAttributes = (obj) =>
- Object.entries(obj)
- .map(([key, val]) => `${key}=${val}`)
- .join(", ");
- const nbsp = "\xa0";
- const template = (titlePrefix, ...elements) => {
- const navLink = ({ href, emoji, text }) =>
- li(
- a(
- { href, class: titlePrefix === text ? "current" : "" },
- span({ class: "emoji" }, emoji),
- nbsp,
- text
- )
- );
- const customCSS = (filename) => {
- const customStyleFile = path.join(
- envPaths("oasis", { suffix: "" }).config,
- filename
- );
- try {
- if (fs.existsSync(customStyleFile)) {
- return link({ rel: "stylesheet", href: filename });
- }
- } catch (error) {
- return "";
- }
- };
- const nodes = html(
- { lang: "en" },
- head(
- title(titlePrefix, " | SNH-Oasis"),
- link({ rel: "stylesheet", href: "/theme.css" }),
- link({ rel: "stylesheet", href: "/assets/style.css" }),
- link({ rel: "stylesheet", href: "/assets/highlight.css" }),
- customCSS("/custom-style.css"),
- link({ rel: "icon", type: "image/svg+xml", href: "/assets/favicon.svg" }),
- meta({ charset: "utf-8" }),
- meta({
- name: "description",
- content: i18n.oasisDescription,
- }),
- meta({
- name: "viewport",
- content: toAttributes({ width: "device-width", "initial-scale": 1 }),
- })
- ),
- body(
- nav(
- ul(
-
- navLink({ href: "/public/latest/extended", emoji: "∞", text: i18n.extended }),
- navLink({ href: "/public/popular/day", emoji: "⌘", text: i18n.popular }),
- navLink({ href: "/public/latest/threads", emoji: "♺", text: i18n.threads }),
- navLink({ href: "/public/latest", emoji: "☄", text: i18n.latest }),
- navLink({ href: "/public/latest/topics", emoji: "ϟ", text: i18n.topics }),
- navLink({ href: "/public/latest/summaries", emoji: "※", text: i18n.summaries }),
- navLink({ href: "/mentions", emoji: "✺", text: i18n.mentions }),
- )
- ),
- main({ id: "content" }, elements),
- nav(
- ul(
- navLink({ href: "/publish", emoji: "❂",text: i18n.publish }),
- navLink({ href: "/search", emoji: "✦", text: i18n.search }),
- navLink({ href: "/inbox", emoji: "☂", text: i18n.private }),
- navLink({ href: "/profile", emoji: "⚉", text: i18n.profile }),
- navLink({ href: "/invites", emoji: "❄", text: i18n.invites }),
- navLink({ href: "/peers", emoji: "⧖", text: i18n.peers }),
- navLink({ href: "/settings", emoji: "⚙", text: i18n.settings })
- )
- )
- )
- );
- const result = doctypeString + nodes.outerHTML;
- return result;
- };
- const thread = (messages) => {
-
-
- let lookingForTarget = true;
- let shallowest = Infinity;
- for (let i = messages.length - 1; i >= 0; i--) {
- const msg = messages[i];
- const depth = lodash.get(msg, "value.meta.thread.depth", 0);
- if (lookingForTarget) {
- const isThreadTarget = Boolean(
- lodash.get(msg, "value.meta.thread.target", false)
- );
- if (isThreadTarget) {
- lookingForTarget = false;
- }
- } else {
- if (depth < shallowest) {
- lodash.set(msg, "value.meta.thread.ancestorOfTarget", true);
- shallowest = depth;
- }
- }
- }
- const msgList = [];
- for (let i = 0; i < messages.length; i++) {
- const j = i + 1;
- const currentMsg = messages[i];
- const nextMsg = messages[j];
- const depth = (msg) => {
-
-
- if (msg === undefined) return 0;
- return lodash.get(msg, "value.meta.thread.depth", 0);
- };
- msgList.push(post({ msg: currentMsg }).outerHTML);
- if (depth(currentMsg) < depth(nextMsg)) {
- const isAncestor = Boolean(
- lodash.get(currentMsg, "value.meta.thread.ancestorOfTarget", false)
- );
- const isBlocked = Boolean(nextMsg.value.meta.blocking);
- msgList.push(`<div class="indent"><details ${isAncestor ? "open" : ""}>`);
- const nextAuthor = lodash.get(nextMsg, "value.meta.author.name");
- const nextSnippet = postSnippet(
- lodash.has(nextMsg, "value.content.contentWarning")
- ? lodash.get(nextMsg, "value.content.contentWarning")
- : lodash.get(nextMsg, "value.content.text")
- );
- msgList.push(
- summary(
- isBlocked
- ? i18n.relationshipBlockingPost
- : `${nextAuthor}: ${nextSnippet}`
- ).outerHTML
- );
- } else if (depth(currentMsg) > depth(nextMsg)) {
-
- const diffDepth = depth(currentMsg) - depth(nextMsg);
- const shallowList = [];
- for (let d = 0; d < diffDepth; d++) {
-
- shallowList.push("</details></div>");
- }
- msgList.push(shallowList);
- }
- }
- const htmlStrings = lodash.flatten(msgList);
- return div(
- {},
- { class: "thread-container", innerHTML: htmlStrings.join("") }
- );
- };
- const postSnippet = (text) => {
- const max = 40;
- text = text.trim().split("\n", 3).join("\n");
-
-
- text = text.replace(/_|`|\*|#|^\[@.*?]|\[|]|\(\S*?\)/g, "").trim();
- text = text.replace(/:$/, "");
- text = text.trim().split("\n", 1)[0].trim();
- if (text.length > max) {
- text = text.substring(0, max - 1) + "…";
- }
- return text;
- };
- const continueThreadComponent = (thread, isComment) => {
- const encoded = {
- next: encodeURIComponent(thread[THREAD_PREVIEW_LENGTH + 1].key),
- parent: encodeURIComponent(thread[0].key),
- };
- const left = thread.length - (THREAD_PREVIEW_LENGTH + 1);
- let continueLink;
- if (isComment == false) {
- continueLink = `/thread/${encoded.parent}#${encoded.next}`;
- return a(
- { href: continueLink },
- `continue reading ${left} more comment${left === 1 ? "" : "s"}`
- );
- } else {
- continueLink = `/thread/${encoded.parent}`;
- return a({ href: continueLink }, "read the rest of the thread");
- }
- };
- const postAside = ({ key, value }) => {
- const thread = value.meta.thread;
- if (thread == null) return null;
- const isComment = value.meta.postType === "comment";
- let postsToShow;
- if (isComment) {
- const commentPosition = thread.findIndex((msg) => msg.key === key);
- postsToShow = thread.slice(
- commentPosition + 1,
- Math.min(commentPosition + (THREAD_PREVIEW_LENGTH + 1), thread.length)
- );
- } else {
- postsToShow = thread.slice(
- 1,
- Math.min(thread.length, THREAD_PREVIEW_LENGTH + 1)
- );
- }
- const fragments = postsToShow.map((p) => post({ msg: p }));
- if (thread.length > THREAD_PREVIEW_LENGTH + 1) {
- fragments.push(section(continueThreadComponent(thread, isComment)));
- }
- return div({ class: "indent" }, fragments);
- };
- const post = ({ msg, aside = false }) => {
- const encoded = {
- key: encodeURIComponent(msg.key),
- author: encodeURIComponent(msg.value.author),
- parent: encodeURIComponent(msg.value.content.root),
- };
- const url = {
- author: `/author/${encoded.author}`,
- likeForm: `/like/${encoded.key}`,
- link: `/thread/${encoded.key}#${encoded.key}`,
- parent: `/thread/${encoded.parent}#${encoded.parent}`,
- avatar: msg.value.meta.author.avatar.url,
- json: `/json/${encoded.key}`,
- subtopic: `/subtopic/${encoded.key}`,
- comment: `/comment/${encoded.key}`,
- };
- const isPrivate = Boolean(msg.value.meta.private);
- const isBlocked = Boolean(msg.value.meta.blocking);
- const isRoot = msg.value.content.root == null;
- const isFork = msg.value.meta.postType === "subtopic";
- const hasContentWarning =
- typeof msg.value.content.contentWarning === "string";
- const isThreadTarget = Boolean(
- lodash.get(msg, "value.meta.thread.target", false)
- );
- const { name } = msg.value.meta.author;
- const ts_received = msg.value.meta.timestamp.received;
- const timeAgo = ts_received.since.replace("~", "");
- const timeAbsolute = ts_received.iso8601.split(".")[0].replace("T", " ");
- const markdownContent = markdown(
- msg.value.content.text,
- msg.value.content.mentions
- );
- const likeButton = msg.value.meta.voted
- ? { value: 0, class: "liked" }
- : { value: 1, class: null };
- const likeCount = msg.value.meta.votes.length;
- const maxLikedNameLength = 16;
- const maxLikedNames = 16;
- const likedByNames = msg.value.meta.votes
- .slice(0, maxLikedNames)
- .map((person) => person.name)
- .map((name) => name.slice(0, maxLikedNameLength))
- .join(", ");
- const additionalLikesMessage =
- likeCount > maxLikedNames ? `+${likeCount - maxLikedNames} more` : ``;
- const likedByMessage =
- likeCount > 0 ? `${likedByNames} ${additionalLikesMessage}` : null;
- const messageClasses = ["post"];
- const recps = [];
- const addRecps = (recpsInfo) => {
- recpsInfo.forEach(function (recp) {
- recps.push(
- a(
- { href: `/author/${encodeURIComponent(recp.feedId)}` },
- img({ class: "avatar", src: recp.avatarUrl, alt: "" })
- )
- );
- });
- };
- if (isPrivate) {
- messageClasses.push("private");
- addRecps(msg.value.meta.recpsInfo);
- }
- if (isThreadTarget) {
- messageClasses.push("thread-target");
- }
-
- const postOptions = {
- post: null,
- comment: i18n.commentDescription({ parentUrl: url.parent }),
- subtopic: i18n.subtopicDescription({ parentUrl: url.parent }),
- mystery: i18n.mysteryDescription,
- };
- const emptyContent = "<p>undefined</p>\n";
- const articleElement =
- markdownContent === emptyContent
- ? article(
- { class: "content" },
- pre({
- innerHTML: highlightJs.highlight(
- "json",
- JSON.stringify(msg, null, 2)
- ).value,
- })
- )
- : article({ class: "content", innerHTML: markdownContent });
- if (isBlocked) {
- messageClasses.push("blocked");
- return section(
- {
- id: msg.key,
- class: messageClasses.join(" "),
- },
- i18n.relationshipBlockingPost
- );
- }
- const articleContent = hasContentWarning
- ? details(summary(msg.value.content.contentWarning), articleElement)
- : articleElement;
- const fragment = section(
- {
- id: msg.key,
- class: messageClasses.join(" "),
- },
- header(
- div(
- span(
- { class: "author" },
- a(
- { href: url.author },
- img({ class: "avatar", src: url.avatar, alt: "" }),
- name
- )
- ),
- span({ class: "author-action" }, postOptions[msg.value.meta.postType]),
- span(
- {
- class: "time",
- title: timeAbsolute,
- },
- isPrivate ? "🔒" : null,
- isPrivate ? recps : null,
- a({ href: url.link }, nbsp, timeAgo)
- )
- )
- ),
- articleContent,
-
-
-
-
-
-
-
-
-
- div({ id: `centered-footer-${encoded.key}`, class: "centered-footer" }),
- footer(
- div(
- form(
- { action: url.likeForm, method: "post" },
- button(
- {
- name: "voteValue",
- type: "submit",
- value: likeButton.value,
- class: likeButton.class,
- title: likedByMessage,
- },
- `☉ ${likeCount}`
- )
- ),
- a({ href: url.comment }, i18n.comment),
- isPrivate || isRoot || isFork
- ? null
- : a({ href: url.subtopic }, nbsp, i18n.subtopic),
- a({ href: url.json }, nbsp, i18n.json)
- ),
- br()
- )
- );
- const threadSeparator = [div({ class: "text-browser" }, hr(), br())];
- if (aside) {
- return [fragment, postAside(msg), isRoot ? threadSeparator : null];
- } else {
- return fragment;
- }
- };
- exports.editProfileView = ({ name, description }) =>
- template(
- i18n.editProfile,
- section(
- h1(i18n.editProfile),
- p(i18n.editProfileDescription),
- form(
- {
- action: "/profile/edit",
- method: "POST",
- enctype: "multipart/form-data",
- },
- label(
- i18n.profileImage,
- input({ type: "file", name: "image", accept: "image/*" })
- ),
- label(i18n.profileName, input({ name: "name", value: name })),
- label(
- i18n.profileDescription,
- textarea(
- {
- autofocus: true,
- name: "description",
- },
- description
- )
- ),
- button(
- {
- type: "submit",
- },
- i18n.submit
- )
- )
- )
- );
- /**
- * @param {{avatarUrl: string, description: string, feedId: string, messages: any[], name: string, relationship: object, firstPost: object, lastPost: object}} input
- */
- exports.authorView = ({
- avatarUrl,
- description,
- feedId,
- messages,
- firstPost,
- lastPost,
- name,
- relationship,
- }) => {
- const mention = `[@${name}](${feedId})`;
- const markdownMention = highlightJs.highlight("markdown", mention).value;
- const contactForms = [];
- const addForm = ({ action }) =>
- contactForms.push(
- form(
- {
- action: `/${action}/${encodeURIComponent(feedId)}`,
- method: "post",
- },
- button(
- {
- type: "submit",
- },
- i18n[action]
- )
- )
- );
- if (relationship.me === false) {
- if (relationship.following) {
- addForm({ action: "unfollow" });
- } else if (relationship.blocking) {
- addForm({ action: "unblock" });
- } else {
- addForm({ action: "follow" });
- addForm({ action: "block" });
- }
- }
- const relationshipText = (() => {
- if (relationship.me === true) {
- return i18n.relationshipYou;
- } else if (
- relationship.following === true &&
- relationship.blocking === false
- ) {
- return i18n.relationshipFollowing;
- } else if (
- relationship.following === false &&
- relationship.blocking === true
- ) {
- return i18n.relationshipBlocking;
- } else if (
- relationship.following === false &&
- relationship.blocking === false
- ) {
- return i18n.relationshipNone;
- } else if (
- relationship.following === true &&
- relationship.blocking === true
- ) {
- return i18n.relationshipConflict;
- } else {
- throw new Error(`Unknown relationship ${JSON.stringify(relationship)}`);
- }
- })();
- const prefix = section(
- { class: "message" },
- div(
- { class: "profile" },
- img({ class: "avatar", src: avatarUrl }),
- h1(name)
- ),
- pre({
- class: "md-mention",
- innerHTML: markdownMention,
- }),
- description !== "" ? article({ innerHTML: markdown(description) }) : null,
- footer(
- div(
- a({ href: `/likes/${encodeURIComponent(feedId)}` }, i18n.viewLikes),
- span(nbsp, relationshipText),
- ...contactForms,
- relationship.me
- ? a({ href: `/profile/edit` }, nbsp, i18n.editProfile)
- : null
- ),
- br()
- )
- );
- const linkUrl = relationship.me
- ? "/profile/"
- : `/author/${encodeURIComponent(feedId)}/`;
- let items = messages.map((msg) => post({ msg }));
- if (items.length === 0) {
- if (lastPost === undefined) {
- items.push(section(div(span(i18n.feedEmpty))));
- } else {
- items.push(
- section(
- div(
- span(i18n.feedRangeEmpty),
- a({ href: `${linkUrl}` }, i18n.seeFullFeed)
- )
- )
- );
- }
- } else {
- const highestSeqNum = messages[0].value.sequence;
- const lowestSeqNum = messages[messages.length - 1].value.sequence;
- let newerPostsLink;
- if (lastPost !== undefined && highestSeqNum < lastPost.value.sequence)
- newerPostsLink = a(
- { href: `${linkUrl}?gt=${highestSeqNum}` },
- i18n.newerPosts
- );
- else newerPostsLink = span(i18n.newerPosts, { title: i18n.noNewerPosts });
- let olderPostsLink;
- if (lowestSeqNum > firstPost.value.sequence)
- olderPostsLink = a(
- { href: `${linkUrl}?lt=${lowestSeqNum}` },
- i18n.olderPosts
- );
- else
- olderPostsLink = span(i18n.olderPosts, { title: i18n.beginningOfFeed });
- const pagination = section(
- { class: "message" },
- footer(div(newerPostsLink, olderPostsLink), br())
- );
- items.unshift(pagination);
- items.push(pagination);
- }
- return template(i18n.profile, prefix, items);
- };
- exports.previewCommentView = async ({
- previewData,
- messages,
- myFeedId,
- parentMessage,
- contentWarning,
- }) => {
- const publishAction = `/comment/${encodeURIComponent(messages[0].key)}`;
- const preview = generatePreview({
- previewData,
- contentWarning,
- action: publishAction,
- });
- return exports.commentView(
- { messages, myFeedId, parentMessage },
- preview,
- previewData.text,
- contentWarning
- );
- };
- exports.commentView = async (
- { messages, myFeedId, parentMessage },
- preview,
- text,
- contentWarning
- ) => {
- let markdownMention;
- const messageElements = await Promise.all(
- messages.reverse().map((message) => {
- debug("%O", message);
- const authorName = message.value.meta.author.name;
- const authorFeedId = message.value.author;
- if (authorFeedId !== myFeedId) {
- if (message.key === parentMessage.key) {
- const x = `[@${authorName}](${authorFeedId})\n\n`;
- markdownMention = x;
- }
- }
- return post({ msg: message });
- })
- );
- const action = `/comment/preview/${encodeURIComponent(messages[0].key)}`;
- const method = "post";
- const isPrivate = parentMessage.value.meta.private;
- const authorName = parentMessage.value.meta.author.name;
- const publicOrPrivate = isPrivate ? i18n.commentPrivate : i18n.commentPublic;
- const maybeSubtopicText = isPrivate ? [null] : i18n.commentWarning;
- return template(
- i18n.commentTitle({ authorName }),
- div({ class: "thread-container" }, messageElements),
- preview !== undefined ? preview : "",
- p(
- ...i18n.commentLabel({ publicOrPrivate, markdownUrl }),
- ...maybeSubtopicText
- ),
- form(
- { action, method, enctype: "multipart/form-data" },
- label(
- i18n.contentWarningLabel,
- input({
- name: "contentWarning",
- type: "text",
- class: "contentWarning",
- value: contentWarning ? contentWarning : "",
- placeholder: i18n.contentWarningPlaceholder,
- })
- ),
- textarea(
- {
- autofocus: true,
- required: true,
- name: "text",
- },
- text ? text : isPrivate ? null : markdownMention
- ),
- button({ type: "submit" }, i18n.preview),
- label({ class: "file-button", for: "blob" }, i18n.attachFiles),
- input({ type: "file", id: "blob", name: "blob" })
- )
- );
- };
- exports.mentionsView = ({ messages }) => {
- return messageListView({
- messages,
- viewTitle: i18n.mentions,
- viewDescription: i18n.mentionsDescription,
- });
- };
- exports.privateView = ({ messages }) => {
- return messageListView({
- messages,
- viewTitle: i18n.private,
- viewDescription: i18n.privateDescription,
- });
- };
- exports.publishCustomView = async () => {
- const action = "/publish/custom";
- const method = "post";
- return template(
- i18n.publishCustom,
- section(
- h1(i18n.publishCustom),
- p(i18n.publishCustomDescription),
- form(
- { action, method },
- textarea(
- {
- autofocus: true,
- required: true,
- name: "text",
- },
- "{\n",
- ' "type": "test",\n',
- ' "hello": "world"\n',
- "}"
- ),
- button(
- {
- type: "submit",
- },
- i18n.submit
- )
- )
- ),
- p(i18n.publishBasicInfo({ href: "/publish" }))
- );
- };
- exports.threadView = ({ messages }) => {
- const rootMessage = messages[0];
- const rootAuthorName = rootMessage.value.meta.author.name;
- const rootSnippet = postSnippet(
- lodash.get(rootMessage, "value.content.text", i18n.mysteryDescription)
- );
- return template([`@${rootAuthorName}: `, rootSnippet], thread(messages));
- };
- exports.publishView = (preview, text, contentWarning) => {
- return template(
- i18n.publish,
- section(
- h1(i18n.publish),
- form(
- {
- action: "/publish/preview",
- method: "post",
- enctype: "multipart/form-data",
- },
- label(
- i18n.publishLabel({ markdownUrl, linkTarget: "_blank" }),
- label(
- i18n.contentWarningLabel,
- input({
- name: "contentWarning",
- type: "text",
- class: "contentWarning",
- value: contentWarning ? contentWarning : "",
- placeholder: i18n.contentWarningPlaceholder,
- })
- ),
- textarea({ required: true, name: "text", placeholder: i18n.publishWarningPlaceholder }, text ? text : "")
- ),
- button({ type: "submit" }, i18n.preview),
- label({ class: "file-button", for: "blob" }, i18n.attachFiles),
- input({ type: "file", id: "blob", name: "blob" })
- )
- ),
- preview ? preview : "",
- p(i18n.publishCustomInfo({ href: "/publish/custom" }))
- );
- };
- const generatePreview = ({ previewData, contentWarning, action }) => {
- const { authorMeta, text, mentions } = previewData;
- // craft message that looks like it came from the db
- // cb: this kinda fragile imo? this is for getting a proper post styling ya?
- const msg = {
- key: "%non-existent.preview",
- value: {
- author: authorMeta.id,
- // sequence: -1,
- content: {
- type: "post",
- text: text,
- },
- timestamp: Date.now(),
- meta: {
- isPrivate: true,
- votes: [],
- author: {
- name: authorMeta.name,
- avatar: {
- url: `/image/64/${encodeURIComponent(authorMeta.image)}`,
- },
- },
- },
- },
- };
- if (contentWarning) msg.value.content.contentWarning = contentWarning;
- const ts = new Date(msg.value.timestamp);
- lodash.set(msg, "value.meta.timestamp.received.iso8601", ts.toISOString());
- const ago = Date.now() - Number(ts);
- const prettyAgo = prettyMs(ago, { compact: true });
- lodash.set(msg, "value.meta.timestamp.received.since", prettyAgo);
- return div(
- Object.keys(mentions).length === 0
- ? ""
- : section(
- { class: "mention-suggestions" },
- h2(i18n.mentionsMatching),
- Object.keys(mentions).map((name) => {
- let matches = mentions[name];
- return div(
- matches.map((m) => {
- let relationship = { emoji: "", desc: "" };
- if (m.rel.followsMe && m.rel.following) {
- relationship.emoji = "☍";
- relationship.desc = i18n.relationshipMutuals;
- } else if (m.rel.following) {
- relationship.emoji = "☌";
- relationship.desc = i18n.relationshipFollowing;
- } else if (m.rel.followsMe) {
- relationship.emoji = "⚼";
- relationship.desc = i18n.relationshipTheyFollow;
- } else {
- relationship.emoji = "❓";
- relationship.desc = i18n.relationshipNotFollowing;
- }
- return div(
- { class: "mentions-container" },
- a(
- {
- class: "mentions-image",
- href: `/author/${encodeURIComponent(m.feed)}`,
- },
- img({ src: `/image/64/${encodeURIComponent(m.img)}` })
- ),
- a(
- {
- class: "mentions-name",
- href: `/author/${encodeURIComponent(m.feed)}`,
- },
- m.name
- ),
- div(
- { class: "emo-rel" },
- span(
- { class: "emoji", title: relationship.desc },
- relationship.emoji
- ),
- span(
- { class: "mentions-listing" },
- `[@${m.name}](${m.feed})`
- )
- )
- );
- })
- );
- })
- ),
- section(
- { class: "post-preview" },
- post({ msg }),
- // doesn't need blobs, preview adds them to the text
- form(
- { action, method: "post" },
- input({
- name: "contentWarning",
- type: "hidden",
- value: contentWarning,
- }),
- input({
- name: "text",
- type: "hidden",
- value: text,
- }),
- button({ type: "submit" }, i18n.publish)
- )
- )
- );
- };
- exports.previewView = ({ previewData, contentWarning }) => {
- const publishAction = "/publish";
- const preview = generatePreview({
- previewData,
- contentWarning,
- action: publishAction,
- });
- return exports.publishView(preview, previewData.text, contentWarning);
- };
- exports.peersView = async ({ peers }) => {
- const startButton = form(
- { action: "/settings/conn/start", method: "post" },
- button({ type: "submit" }, i18n.startNetworking)
- );
- const restartButton = form(
- { action: "/settings/conn/restart", method: "post" },
- button({ type: "submit" }, i18n.restartNetworking)
- );
- const stopButton = form(
- { action: "/settings/conn/stop", method: "post" },
- button({ type: "submit" }, i18n.stopNetworking)
- );
- const syncButton = form(
- { action: "/settings/conn/sync", method: "post" },
- button({ type: "submit" }, i18n.sync)
- );
- const connButtons = div({ class: "form-button-group" }, [
- startButton,
- restartButton,
- stopButton,
- syncButton,
- ]);
- const peerList = (peers || [])
- .filter(([, data]) => data.state === "connected")
- .map(([, data]) => {
- return li(
- a(
- { href: `/author/${encodeURIComponent(data.key)}` },
- data.name || data.host || data.key
- )
- );
- });
- const supportedList = (supporting)
- var supporting = JSON.parse(fs.readFileSync(supportingPath, "utf8")).value;
- var arr = [];
- var keys = Object.keys(supporting);
- var data = Object.entries(supporting[keys[0]]);
- Object.entries(data).forEach(([key, value]) => {
- if (value[1]===1){
- var supported = (value[0])
- if (!arr.includes(supported)) {
- arr.push(
- li(
- a(
- { href: `/author/${encodeURIComponent(supported)}` },
- supported
- )
- )
- );
- }
- }
- });
- var supports = arr;
- const blockedList = (supporting)
- var supporting = JSON.parse(fs.readFileSync(supportingPath, "utf8")).value;
- var arr = [];
- var keys = Object.keys(supporting);
- var data = Object.entries(supporting[keys[0]]);
- Object.entries(data).forEach(([key, value]) => {
- if (value[1]===-1){
- var blocked = (value[0])
- if (!arr.includes(blocked)) {
- arr.push(
- li(
- a(
- { href: `/author/${encodeURIComponent(blocked)}` },
- blocked
- )
- )
- );
- }
- }
- });
- var blocks = arr;
- const recommendedList = (supporting)
- var supporting = JSON.parse(fs.readFileSync(supportingPath, "utf8")).value;
- var arr = [];
- var keys = Object.keys(supporting);
- var data = Object.entries(supporting[keys[0]]);
- Object.entries(data).forEach(([key, value]) => {
- if (value[1]===-2){
- var recommended = (value[0])
- if (!arr.includes(recommended)) {
- arr.push(
- li(
- a(
- { href: `/author/${encodeURIComponent(recommended)}` },
- recommended
- )
- )
- );
- }
- }
- });
- var recommends = arr;
- return template(
- i18n.peers,
- section(
- { class: "message" },
- h1(i18n.peerConnections),
- connButtons,
- h1(i18n.online, " (", peerList.length, ")"),
- peerList.length > 0 ? ul(peerList) : i18n.noConnections,
- p(i18n.connectionActionIntro),
- h1(i18n.supported, " (", supports.length, ")"),
- supports.length > 0 ? ul(supports): i18n.noSupportedConnections,
- p(i18n.connectionActionIntro),
- h1(i18n.recommended, " (", recommends.length, ")"),
- recommends.length > 0 ? ul(recommends): i18n.noRecommendedConnections,
- p(i18n.connectionActionIntro),
- h1(i18n.blocked, " (", blocks.length, ")"),
- blocks.length > 0 ? ul(blocks): i18n.noBlockedConnections,
- p(i18n.connectionActionIntro),
- )
- );
- };
- exports.invitesView = ({ invites }) => {
- const pubsList = (pub)
- var pubs = fs.readFileSync(offsetPath, "utf8");
- var arr = pubs.split(/[{,}]/);
- const arr2 = [];
- const arr3 = [];
- var host = [];
- var id = [];
- for(var i in arr){
- arr.push(arr[i]);
- }
- for(var i=0; i<arr.length; i++){
- if (arr[i] === '"address":') {
- host = arr[i+1].split(':').pop().split(';')[0].split('"')[1];
- id = arr[i+3].split(':').pop().split(';')[0].split('"')[1];
- }
- if (!arr2.includes(host + ":" + id)){
- arr2.push(host + ":" + id)
- }
- var f = arr2.filter(function (el) {
- return el != "" & el != ":";
- });
- }
- for(const v of f){
- var h = v.split(":")[0];
- var i = v.split(":")[1];
- arr3.push(
- li(
- "PUB: " + h, br,
- a(
- { href: `/author/${encodeURIComponent(i)}` },
- i
- ), br, br
- )
- );
- }
- var pub = arr3;
- return template(
- i18n.invites,
- section(
- { class: "message" },
- h1(i18n.invites),
- p(i18n.invitesDescription),
- form(
- { action: "/settings/invite/accept", method: "post" },
- input({ name: "invite", type: "text", autofocus: true, required: true }),
- button({ type: "submit" }, i18n.acceptInvite),
- h1(i18n.acceptedInvites, " (", pub.length, ")"),
- pub.length > 0 ? ul(pub): i18n.noInvites,
- ),
- )
- );
- };
- exports.settingsView = ({ theme, themeNames, version }) => {
- const themeElements = themeNames.map((cur) => {
- const isCurrentTheme = cur === theme;
- if (isCurrentTheme) {
- return option({ value: cur, selected: true }, cur);
- } else {
- return option({ value: cur }, cur);
- }
- });
- const base16 = [
- // '00', removed because this is the background
- "01",
- "02",
- "03",
- "04",
- "05",
- "06",
- "07",
- "08",
- "09",
- "0A",
- "0B",
- "0C",
- "0D",
- "0E",
- "0F",
- ];
- const base16Elements = base16.map((base) =>
- div({
- class: `theme-preview theme-preview-${base}`,
- })
- );
- const languageOption = (longName, shortName) =>
- shortName === selectedLanguage
- ? option({ value: shortName, selected: true }, longName)
- : option({ value: shortName }, longName);
- const rebuildButton = form(
- { action: "/settings/rebuild", method: "post" },
- button({ type: "submit" }, i18n.rebuildName)
- );
- if (ck === "required"){
- updaterequired = form(
- { action: "/update", method: "post" },
- button({ type: "submit"}, i18n.updateit)
- );
- };
- return template(
- i18n.settings,
- section(
- { class: "message" },
- h1(i18n.settings),
- p(a({ href:snhUrl, target: "_blank" }, i18n.settingsIntro({ version }))),
- p(updaterequired),
- h2(i18n.theme),
- p(i18n.themeIntro),
- form(
- { action: "/theme.css", method: "post" },
- select({ name: "theme" }, ...themeElements),
- button({ type: "submit" }, i18n.setTheme)
- ),
- h2(i18n.language),
- p(i18n.languageDescription),
- form(
- { action: "/language", method: "post" },
- select({ name: "language" }, [
- // Languages are sorted alphabetically by their 'long name'.
- /* spell-checker:disable */
- languageOption("English", "en"),
- languageOption("Español", "es"),
- /* spell-checker:enable */
- ]),
- button({ type: "submit" }, i18n.setLanguage)
- ),
- h2(i18n.indexes),
- p(i18n.indexesDescription),
- rebuildButton,
- )
- );
- };
- /** @param {{ viewTitle: string, viewDescription: string }} input */
- const viewInfoBox = ({ viewTitle = null, viewDescription = null }) => {
- if (!viewTitle && !viewDescription) {
- return null;
- }
- return section(
- { class: "viewInfo" },
- viewTitle ? h1(viewTitle) : null,
- viewDescription ? em(viewDescription) : null
- );
- };
- exports.likesView = async ({ messages, feed, name }) => {
- const authorLink = a(
- { href: `/author/${encodeURIComponent(feed)}` },
- "@" + name
- );
- return template(
- ["@", name, i18n.likedBy],
- viewInfoBox({
- viewTitle: span(authorLink, i18n.likedBy),
- viewDescription: span(i18n.spreadedDescription)
- }),
- messages.map((msg) => post({ msg }))
- );
- };
- const messageListView = ({
- messages,
- viewTitle = null,
- viewDescription = null,
- viewElements = null,
- // If `aside = true`, it will show a few comments in the thread.
- aside = null,
- }) => {
- return template(
- viewTitle,
- section(h1(viewTitle), p(viewDescription), viewElements),
- messages.map((msg) => post({ msg, aside }))
- );
- };
- exports.popularView = ({ messages, prefix }) => {
- return messageListView({
- messages,
- viewElements: prefix,
- viewTitle: i18n.popular,
- viewDescription: i18n.popularDescription,
- });
- };
- exports.extendedView = ({ messages }) => {
- return messageListView({
- messages,
- viewTitle: i18n.extended,
- viewDescription: i18n.extendedDescription,
- });
- };
- exports.latestView = ({ messages }) => {
- return messageListView({
- messages,
- viewTitle: i18n.latest,
- viewDescription: i18n.latestDescription,
- });
- };
- exports.topicsView = ({ messages, prefix }) => {
- return messageListView({
- messages,
- viewTitle: i18n.topics,
- viewDescription: i18n.topicsDescription,
- viewElements: prefix,
- });
- };
- exports.summaryView = ({ messages }) => {
- return messageListView({
- messages,
- viewTitle: i18n.summaries,
- viewDescription: i18n.summariesDescription,
- aside: true,
- });
- };
- exports.spreadedView = ({ messages }) => {
- return spreadedListView({
- messages,
- viewTitle: i18n.spreaded,
- viewDescription: i18n.spreadedDescription,
- });
- };
- exports.threadsView = ({ messages }) => {
- return messageListView({
- messages,
- viewTitle: i18n.threads,
- viewDescription: i18n.threadsDescription,
- aside: true,
- });
- };
- exports.previewSubtopicView = async ({
- previewData,
- messages,
- myFeedId,
- contentWarning,
- }) => {
- const publishAction = `/subtopic/${encodeURIComponent(messages[0].key)}`;
- const preview = generatePreview({
- previewData,
- contentWarning,
- action: publishAction,
- });
- return exports.subtopicView(
- { messages, myFeedId },
- preview,
- previewData.text,
- contentWarning
- );
- };
- exports.subtopicView = async (
- { messages, myFeedId },
- preview,
- text,
- contentWarning
- ) => {
- const subtopicForm = `/subtopic/preview/${encodeURIComponent(
- messages[messages.length - 1].key
- )}`;
- let markdownMention;
- const messageElements = await Promise.all(
- messages.reverse().map((message) => {
- debug("%O", message);
- const authorName = message.value.meta.author.name;
- const authorFeedId = message.value.author;
- if (authorFeedId !== myFeedId) {
- if (message.key === messages[0].key) {
- const x = `[@${authorName}](${authorFeedId})\n\n`;
- markdownMention = x;
- }
- }
- return post({ msg: message });
- })
- );
- const authorName = messages[messages.length - 1].value.meta.author.name;
- return template(
- i18n.subtopicTitle({ authorName }),
- div({ class: "thread-container" }, messageElements),
- preview !== undefined ? preview : "",
- p(i18n.subtopicLabel({ markdownUrl })),
- form(
- { action: subtopicForm, method: "post", enctype: "multipart/form-data" },
- textarea(
- {
- autofocus: true,
- required: true,
- name: "text",
- },
- text ? text : markdownMention
- ),
- label(
- i18n.contentWarningLabel,
- input({
- name: "contentWarning",
- type: "text",
- class: "contentWarning",
- value: contentWarning ? contentWarning : "",
- placeholder: i18n.contentWarningPlaceholder,
- })
- ),
- button({ type: "submit" }, i18n.preview),
- label({ class: "file-button", for: "blob" }, i18n.attachFiles),
- input({ type: "file", id: "blob", name: "blob" })
- )
- );
- };
- exports.searchView = ({ messages, query }) => {
- const searchInput = input({
- name: "query",
- required: false,
- type: "search",
- value: query,
- });
- // - Minimum length of 3 because otherwise SSB-Search hangs forever. :)
- // https://github.com/ssbc/ssb-search/issues/8
- // - Using `setAttribute()` because HyperScript (the HyperAxe dependency has
- // a bug where the `minlength` property is being ignored. No idea why.
- // https://github.com/hyperhype/hyperscript/issues/91
- searchInput.setAttribute("minlength", 3);
- return template(
- i18n.search,
- section(
- h1(i18n.search),
- form(
- { action: "/search", method: "get" },
- label(i18n.searchLabel, searchInput),
- button(
- {
- type: "submit",
- },
- i18n.submit
- )
- )
- ),
- messages.map((msg) => post({ msg }))
- );
- };
- const imageResult = ({ id, infos }) => {
- const encodedBlobId = encodeURIComponent(id);
- // only rendering the first message result so far
- // todo: render links to the others as well
- const info = infos[0];
- const encodedMsgId = encodeURIComponent(info.msg);
- return div(
- {
- class: "image-result",
- },
- [
- a(
- {
- href: `/blob/${encodedBlobId}`,
- },
- img({ src: `/image/256/${encodedBlobId}` })
- ),
- a(
- {
- href: `/thread/${encodedMsgId}#${encodedMsgId}`,
- class: "result-text",
- },
- info.name
- ),
- ]
- );
- };
- exports.imageSearchView = ({ blobs, query }) => {
- const searchInput = input({
- name: "query",
- required: false,
- type: "search",
- value: query,
- });
- // - Minimum length of 3 because otherwise SSB-Search hangs forever. :)
- // https://github.com/ssbc/ssb-search/issues/8
- // - Using `setAttribute()` because HyperScript (the HyperAxe dependency has
- // a bug where the `minlength` property is being ignored. No idea why.
- // https://github.com/hyperhype/hyperscript/issues/91
- searchInput.setAttribute("minlength", 3);
- return template(
- i18n.imageSearch,
- section(
- h1(i18n.imageSearch),
- form(
- { action: "/imageSearch", method: "get" },
- label(i18n.imageSearchLabel, searchInput),
- button(
- {
- type: "submit",
- },
- i18n.submit
- )
- )
- ),
- div(
- {
- class: "image-search-grid",
- },
- Object.keys(blobs)
- // todo: add pagination
- .slice(0, 30)
- .map((blobId) => imageResult({ id: blobId, infos: blobs[blobId] }))
- )
- );
- };
- exports.hashtagView = ({ messages, hashtag }) => {
- return template(
- `#${hashtag}`,
- section(h1(`#${hashtag}`), p(i18n.hashtagDescription)),
- messages.map((msg) => post({ msg }))
- );
- };
- /** @param {{percent: number}} input */
- exports.indexingView = ({ percent }) => {
- // TODO: i18n
- const message = `Oasis has only processed ${percent}% of the messages and needs to catch up. This page will refresh every 10 seconds. Thanks for your patience! ❤`;
- const nodes = html(
- { lang: "en" },
- head(
- title("Oasis"),
- link({ rel: "icon", type: "image/svg+xml", href: "/assets/favicon.svg" }),
- meta({ charset: "utf-8" }),
- meta({
- name: "description",
- content: i18n.oasisDescription,
- }),
- meta({
- name: "viewport",
- content: toAttributes({ width: "device-width", "initial-scale": 1 }),
- }),
- meta({ "http-equiv": "refresh", content: 10 })
- ),
- body(
- main(
- { id: "content" },
- p(message),
- progress({ value: percent, max: 100 })
- )
- )
- );
- const result = doctypeString + nodes.outerHTML;
- return result;
- };
|