index.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. #!/usr/bin/env node
  2. "use strict";
  3. // Minimum required to get config
  4. const path = require("path");
  5. const envPaths = require("env-paths");
  6. const {cli} = require("./cli");
  7. const fs = require("fs");
  8. const promisesFs = require("fs").promises;
  9. const supports = require("./supports.js").supporting;
  10. const blocks = require("./supports.js").blocking;
  11. const recommends = require("./supports.js").recommending;
  12. const defaultConfig = {};
  13. const defaultConfigFile = path.join(
  14. envPaths("oasis", { suffix: "" }).config,
  15. "/default.json"
  16. );
  17. let haveConfig;
  18. try {
  19. const defaultConfigOverride = fs.readFileSync(defaultConfigFile, "utf8");
  20. Object.entries(JSON.parse(defaultConfigOverride)).forEach(([key, value]) => {
  21. defaultConfig[key] = value;
  22. });
  23. haveConfig = true;
  24. } catch (e) {
  25. if (e.code === "ENOENT") {
  26. haveConfig = false;
  27. } else {
  28. console.log(`There was a problem loading ${defaultConfigFile}`);
  29. throw e;
  30. }
  31. }
  32. const config = cli(defaultConfig, defaultConfigFile);
  33. if (config.debug) {
  34. process.env.DEBUG = "oasis,oasis:*";
  35. }
  36. const customStyleFile = path.join(
  37. envPaths("oasis", { suffix: "" }).config,
  38. "/custom-style.css"
  39. );
  40. let haveCustomStyle;
  41. try {
  42. fs.readFileSync(customStyleFile, "utf8");
  43. haveCustomStyle = true;
  44. } catch (e) {
  45. if (e.code === "ENOENT") {
  46. haveCustomStyle = false;
  47. } else {
  48. console.log(`There was a problem loading ${customStyleFile}`);
  49. throw e;
  50. }
  51. }
  52. const { get } = require("node:http");
  53. const debug = require("debug")("oasis");
  54. const log = (formatter, ...args) => {
  55. const isDebugEnabled = debug.enabled;
  56. debug.enabled = true;
  57. debug(formatter, ...args);
  58. debug.enabled = isDebugEnabled;
  59. };
  60. delete config._;
  61. delete config.$0;
  62. const { host } = config;
  63. const { port } = config;
  64. const url = `http://${host}:${port}`;
  65. debug("Current configuration: %O", config);
  66. debug(`You can save the above to ${defaultConfigFile} to make \
  67. these settings the default. See the readme for details.`);
  68. const { saveConfig, getConfig } = require('./modules-config');
  69. const oasisCheckPath = "/.well-known/oasis";
  70. process.on("uncaughtException", function (err) {
  71. // This isn't `err.code` because TypeScript doesn't like that.
  72. if (err["code"] === "EADDRINUSE") {
  73. get(url + oasisCheckPath, (res) => {
  74. let rawData = "";
  75. res.on("data", (chunk) => {
  76. rawData += chunk;
  77. });
  78. res.on("end", () => {
  79. log(rawData);
  80. if (rawData === "oasis") {
  81. log(`Oasis is already running on host ${host} and port ${port}`);
  82. if (config.open === true) {
  83. log("Opening link to existing instance of Oasis");
  84. open(url);
  85. } else {
  86. log(
  87. "Not opening your browser because opening is disabled by your config"
  88. );
  89. }
  90. process.exit(0);
  91. } else {
  92. throw new Error(`Another server is already running at ${url}.
  93. It might be another copy of Oasis or another program on your computer.
  94. You can run Oasis on a different port number with this option:
  95. oasis --port ${config.port + 1}
  96. Alternatively, you can set the default port in ${defaultConfigFile} with:
  97. {
  98. "port": ${config.port + 1}
  99. }
  100. `);
  101. }
  102. });
  103. });
  104. } else {
  105. throw err;
  106. }
  107. });
  108. process.argv = [];
  109. const http = require("./http");
  110. const {koaBody} = require("koa-body");
  111. const { nav, ul, li, a } = require("hyperaxe");
  112. const open = require("open");
  113. const pull = require("pull-stream");
  114. const koaRouter = require("@koa/router");
  115. const ssbMentions = require("ssb-mentions");
  116. const isSvg = require('is-svg');
  117. const { themeNames } = require("@fraction/base16-css");
  118. const { isFeed, isMsg, isBlob } = require("ssb-ref");
  119. const ssb = require("./ssb");
  120. const router = new koaRouter();
  121. // Create "cooler"-style interface from SSB connection.
  122. // This handle is passed to the models for their convenience.
  123. const cooler = ssb({ offline: config.offline });
  124. const models = require("./models");
  125. const { about, blob, friend, meta, post, vote, wallet } = models({
  126. cooler,
  127. isPublic: config.public,
  128. });
  129. const nameWarmup = about._startNameWarmup();
  130. // enhance the users' input text by expanding @name to [@name](@feedPub.key)
  131. // and slurps up blob uploads and appends a markdown link for it to the text (see handleBlobUpload)
  132. const preparePreview = async function (ctx) {
  133. let text = String(ctx.request.body.text);
  134. // find all the @mentions that are not inside a link already
  135. // stores name:[matches...]
  136. // TODO: sort by relationship
  137. const mentions = {};
  138. // This matches for @string followed by a space or other punctuations like ! , or .
  139. // The idea here is to match a plain @name but not [@name](...)
  140. // also: re.exec has state => regex is consumed and thus needs to be re-instantiated for each call
  141. //
  142. // Change this link when the regex changes: https://regex101.com/r/j5rzSv/2
  143. const rex = /(^|\s)(?!\[)@([a-zA-Z0-9-]+)([\s.,!?)~]{1}|$)/g;
  144. // ^ sentence ^
  145. // delimiters
  146. // find @mentions using rex and use about.named() to get the info for them
  147. let m;
  148. while ((m = rex.exec(text)) !== null) {
  149. const name = m[2];
  150. let matches = about.named(name);
  151. for (const feed of matches) {
  152. let found = mentions[name] || [];
  153. found.push(feed);
  154. mentions[name] = found;
  155. }
  156. }
  157. // filter the matches depending on the follow relation
  158. Object.keys(mentions).forEach((name) => {
  159. let matches = mentions[name];
  160. // if we find mention matches for a name, and we follow them / they follow us,
  161. // then use those matches as suggestions
  162. const meaningfulMatches = matches.filter((m) => {
  163. return (m.rel.followsMe || m.rel.following) && m.rel.blocking === false;
  164. });
  165. if (meaningfulMatches.length > 0) {
  166. matches = meaningfulMatches;
  167. }
  168. mentions[name] = matches;
  169. });
  170. // replace the text with a markdown link if we have unambiguous match
  171. const replacer = (match, name, sign) => {
  172. let matches = mentions[name];
  173. if (matches && matches.length === 1) {
  174. // we found an exact match, don't send it to frontend as a suggestion
  175. delete mentions[name];
  176. // format markdown link and put the correct sign back at the end
  177. return `[@${matches[0].name}](${matches[0].feed})${sign ? sign : ""}`;
  178. }
  179. return match;
  180. };
  181. text = text.replace(rex, replacer);
  182. // add blob new blob to the end of the document.
  183. text += await handleBlobUpload(ctx);
  184. // author metadata for the preview-post
  185. const ssb = await cooler.open();
  186. const authorMeta = {
  187. id: ssb.id,
  188. name: await about.name(ssb.id),
  189. image: await about.image(ssb.id),
  190. };
  191. return { authorMeta, text, mentions };
  192. };
  193. // handleBlobUpload ingests an uploaded form file.
  194. // it takes care of maximum blob size (5meg), exif stripping and mime detection.
  195. // finally it returns the correct markdown link for the blob depending on the mime-type.
  196. // it supports plain, image and also audio: and video: as understood by ssbMarkdown.
  197. const handleBlobUpload = async function (ctx) {
  198. if (!ctx.request.files) return "";
  199. const ssb = await cooler.open();
  200. const blobUpload = ctx.request.files.blob;
  201. if (typeof blobUpload === "undefined") {
  202. return "";
  203. }
  204. let data = await promisesFs.readFile(blobUpload.filepath);
  205. if (data.length == 0) {
  206. return "";
  207. }
  208. // 5 MiB check
  209. const mebibyte = Math.pow(2, 20);
  210. const maxSize = 5 * mebibyte;
  211. if (data.length > maxSize) {
  212. throw new Error("Blob file is too big, maximum size is 5 mebibytes");
  213. }
  214. try {
  215. const removeExif = (fileData) => {
  216. const exifOrientation = load(fileData);
  217. const orientation = exifOrientation["0th"][ImageIFD.Orientation];
  218. const clean = remove(fileData);
  219. if (orientation !== undefined) {
  220. // preserve img orientation
  221. const exifData = { "0th": {} };
  222. exifData["0th"][ImageIFD.Orientation] = orientation;
  223. const exifStr = dump(exifData);
  224. return insert(exifStr, clean);
  225. } else {
  226. return clean;
  227. }
  228. };
  229. const dataString = data.toString("binary");
  230. // implementation borrowed from ssb-blob-files
  231. // (which operates on a slightly different data structure, sadly)
  232. // https://github.com/ssbc/ssb-blob-files/blob/master/async/image-process.js
  233. data = Buffer.from(removeExif(dataString), "binary");
  234. } catch (e) {
  235. // blob was likely not a jpeg -- no exif data to remove. proceeding with blob upload
  236. }
  237. const addBlob = new Promise((resolve, reject) => {
  238. pull(
  239. pull.values([data]),
  240. ssb.blobs.add((err, hashedBlobRef) => {
  241. if (err) return reject(err);
  242. resolve(hashedBlobRef);
  243. })
  244. );
  245. });
  246. let blob = {
  247. id: await addBlob,
  248. name: blobUpload.name,
  249. };
  250. // determine encoding to add the correct markdown link
  251. const FileType = require("file-type");
  252. try {
  253. let fileType = await FileType.fromBuffer(data);
  254. blob.mime = fileType.mime;
  255. } catch (error) {
  256. console.warn(error);
  257. blob.mime = "application/octet-stream";
  258. }
  259. // append uploaded blob as markdown to the end of the input text
  260. if (blob.mime.startsWith("image/")) {
  261. return `\n![${blob.name}](${blob.id})`;
  262. } else if (blob.mime.startsWith("audio/")) {
  263. return `\n![audio:${blob.name}](${blob.id})`;
  264. } else if (blob.mime.startsWith("video/")) {
  265. return `\n![video:${blob.name}](${blob.id})`;
  266. } else {
  267. return `\n[${blob.name}](${blob.id})`;
  268. }
  269. };
  270. const resolveCommentComponents = async function (ctx) {
  271. const { message } = ctx.params;
  272. const parentId = message;
  273. const parentMessage = await post.get(parentId);
  274. const myFeedId = await meta.myFeedId();
  275. const hasRoot =
  276. typeof parentMessage.value.content.root === "string" &&
  277. ssbRef.isMsg(parentMessage.value.content.root);
  278. const hasFork =
  279. typeof parentMessage.value.content.fork === "string" &&
  280. ssbRef.isMsg(parentMessage.value.content.fork);
  281. const rootMessage = hasRoot
  282. ? hasFork
  283. ? parentMessage
  284. : await post.get(parentMessage.value.content.root)
  285. : parentMessage;
  286. const messages = await post.topicComments(rootMessage.key);
  287. messages.push(rootMessage);
  288. let contentWarning;
  289. if (ctx.request.body) {
  290. const rawContentWarning = String(ctx.request.body.contentWarning).trim();
  291. contentWarning =
  292. rawContentWarning.length > 0 ? rawContentWarning : undefined;
  293. }
  294. return { messages, myFeedId, parentMessage, contentWarning };
  295. };
  296. const {
  297. authorView,
  298. previewCommentView,
  299. commentView,
  300. editProfileView,
  301. indexingView,
  302. extendedView,
  303. latestView,
  304. likesView,
  305. threadView,
  306. hashtagView,
  307. mentionsView,
  308. popularView,
  309. previewView,
  310. privateView,
  311. publishCustomView,
  312. publishView,
  313. previewSubtopicView,
  314. subtopicView,
  315. searchView,
  316. imageSearchView,
  317. setLanguage,
  318. settingsView,
  319. modulesView,
  320. peersView,
  321. invitesView,
  322. topicsView,
  323. summaryView,
  324. threadsView,
  325. walletView,
  326. walletErrorView,
  327. walletHistoryView,
  328. walletReceiveView,
  329. walletSendFormView,
  330. walletSendConfirmView,
  331. walletSendResultView,
  332. } = require("./views/index.js");
  333. const ssbRef = require("ssb-ref");
  334. const markdownView = require("./views/markdown.js");
  335. let sharp;
  336. try {
  337. sharp = require("sharp");
  338. } catch (e) {
  339. // Optional dependency
  340. }
  341. const readmePath = path.join(__dirname, "..", "README.md");
  342. const packagePath = path.join(__dirname, "..", "package.json");
  343. const readme = fs.readFileSync(readmePath, "utf8");
  344. const version = JSON.parse(fs.readFileSync(packagePath, "utf8")).version;
  345. router
  346. .param("imageSize", (imageSize, ctx, next) => {
  347. const size = Number(imageSize);
  348. const isInteger = size % 1 === 0;
  349. const overMinSize = size > 2;
  350. const underMaxSize = size <= 256;
  351. ctx.assert(
  352. isInteger && overMinSize && underMaxSize,
  353. 400,
  354. "Invalid image size"
  355. );
  356. return next();
  357. })
  358. .param("blobId", (blobId, ctx, next) => {
  359. ctx.assert(ssbRef.isBlob(blobId), 400, "Invalid blob link");
  360. return next();
  361. })
  362. .param("message", (message, ctx, next) => {
  363. ctx.assert(ssbRef.isMsg(message), 400, "Invalid message link");
  364. return next();
  365. })
  366. .param("feed", (message, ctx, next) => {
  367. ctx.assert(ssbRef.isFeedId(message), 400, "Invalid feed link");
  368. return next();
  369. })
  370. .get("/", async (ctx) => {
  371. ctx.redirect("/mentions");
  372. })
  373. .get("/robots.txt", (ctx) => {
  374. ctx.body = "User-agent: *\nDisallow: /";
  375. })
  376. .get(oasisCheckPath, (ctx) => {
  377. ctx.body = "oasis";
  378. })
  379. .get("/public/popular/:period", async (ctx) => {
  380. const { period } = ctx.params;
  381. const popularMod = ctx.cookies.get("popularMod") || 'on';
  382. if (popularMod !== 'on') {
  383. ctx.redirect('/modules');
  384. return;
  385. }
  386. const publicPopular = async ({ period }) => {
  387. const messages = await post.popular({ period });
  388. const selectedLanguage = ctx.cookies.get("language") || "en";
  389. const i18nBase = require("./views/i18n");
  390. let i18n = i18nBase[selectedLanguage];
  391. exports.setLanguage = (language) => {
  392. selectedLanguage = language;
  393. i18n = Object.assign({}, i18nBase.en, i18nBase[language]);
  394. };
  395. const prefix = nav(
  396. ul(
  397. a({ href: "./day" }, i18n.day),
  398. a({ href: "./week" }, i18n.week),
  399. a({ href: "./month" }, i18n.month),
  400. a({ href: "./year" }, i18n.year)
  401. )
  402. );
  403. return popularView({
  404. messages,
  405. prefix,
  406. });
  407. };
  408. ctx.body = await publicPopular({ period });
  409. })
  410. .get("/public/latest", async (ctx) => {
  411. const latestMod = ctx.cookies.get("latestMod") || 'on';
  412. if (latestMod !== 'on') {
  413. ctx.redirect('/modules');
  414. return;
  415. }
  416. const messages = await post.latest();
  417. ctx.body = await latestView({ messages });
  418. })
  419. .get("/public/latest/extended", async (ctx) => {
  420. const extendedMod = ctx.cookies.get("extendedMod") || 'on';
  421. if (extendedMod !== 'on') {
  422. ctx.redirect('/modules');
  423. return;
  424. }
  425. const messages = await post.latestExtended();
  426. ctx.body = await extendedView({ messages });
  427. })
  428. .get("/public/latest/topics", async (ctx) => {
  429. const topicsMod = ctx.cookies.get("topicsMod") || 'on';
  430. if (topicsMod !== 'on') {
  431. ctx.redirect('/modules');
  432. return;
  433. }
  434. const messages = await post.latestTopics();
  435. const channels = await post.channels();
  436. const list = channels.map((c) => {
  437. return li(a({ href: `/hashtag/${c}` }, `#${c}`));
  438. });
  439. const prefix = nav(ul(list));
  440. ctx.body = await topicsView({ messages, prefix });
  441. })
  442. .get("/public/latest/summaries", async (ctx) => {
  443. const summariesMod = ctx.cookies.get("summariesMod") || 'on';
  444. if (summariesMod !== 'on') {
  445. ctx.redirect('/modules');
  446. return;
  447. }
  448. const messages = await post.latestSummaries();
  449. ctx.body = await summaryView({ messages });
  450. })
  451. .get("/public/latest/threads", async (ctx) => {
  452. const threadsMod = ctx.cookies.get("threadsMod") || 'on';
  453. if (threadsMod !== 'on') {
  454. ctx.redirect('/modules');
  455. return;
  456. }
  457. const messages = await post.latestThreads();
  458. ctx.body = await threadsView({ messages });
  459. })
  460. .get("/author/:feed", async (ctx) => {
  461. const { feed } = ctx.params;
  462. const gt = Number(ctx.request.query["gt"] || -1);
  463. const lt = Number(ctx.request.query["lt"] || -1);
  464. if (lt > 0 && gt > 0 && gt >= lt)
  465. throw new Error("Given search range is empty");
  466. const author = async (feedId) => {
  467. const description = await about.description(feedId);
  468. const name = await about.name(feedId);
  469. const image = await about.image(feedId);
  470. const messages = await post.fromPublicFeed(feedId, gt, lt);
  471. const firstPost = await post.firstBy(feedId);
  472. const lastPost = await post.latestBy(feedId);
  473. const relationship = await friend.getRelationship(feedId);
  474. const avatarUrl = `/image/256/${encodeURIComponent(image)}`;
  475. return authorView({
  476. feedId,
  477. messages,
  478. firstPost,
  479. lastPost,
  480. name,
  481. description,
  482. avatarUrl,
  483. relationship,
  484. });
  485. };
  486. ctx.body = await author(feed);
  487. })
  488. .get("/search", async (ctx) => {
  489. let { query } = ctx.query;
  490. if (isMsg(query)) {
  491. return ctx.redirect(`/thread/${encodeURIComponent(query)}`);
  492. }
  493. if (isFeed(query)) {
  494. return ctx.redirect(`/author/${encodeURIComponent(query)}`);
  495. }
  496. if (isBlob(query)) {
  497. return ctx.redirect(`/blob/${encodeURIComponent(query)}`);
  498. }
  499. if (typeof query === "string") {
  500. // https://github.com/ssbc/ssb-search/issues/7
  501. query = query.toLowerCase();
  502. if (query.length > 1 && query.startsWith("#")) {
  503. const hashtag = query.slice(1);
  504. return ctx.redirect(`/hashtag/${encodeURIComponent(hashtag)}`);
  505. }
  506. }
  507. const messages = await post.search({ query });
  508. ctx.body = await searchView({ messages, query });
  509. })
  510. .get("/imageSearch", async (ctx) => {
  511. const { query } = ctx.query;
  512. const blobs = query ? await blob.search({ query }) : {};
  513. ctx.body = await imageSearchView({ blobs, query });
  514. })
  515. .get("/inbox", async (ctx) => {
  516. const theme = ctx.cookies.get("theme") || config.theme;
  517. const inboxMod = ctx.cookies.get("inboxMod") || 'on';
  518. if (inboxMod !== 'on') {
  519. ctx.redirect('/modules');
  520. return;
  521. }
  522. const inboxMessages = async () => {
  523. const messages = await post.inbox();
  524. return privateView({ messages });
  525. };
  526. ctx.body = await inboxMessages();
  527. })
  528. .get("/hashtag/:hashtag", async (ctx) => {
  529. const { hashtag } = ctx.params;
  530. const messages = await post.fromHashtag(hashtag);
  531. ctx.body = await hashtagView({ hashtag, messages });
  532. })
  533. .get("/theme.css", async (ctx) => {
  534. const theme = ctx.cookies.get("theme") || config.theme;
  535. const packageName = "@fraction/base16-css";
  536. const filePath = path.resolve(
  537. "node_modules",
  538. packageName,
  539. "src",
  540. `base16-${theme}.css`
  541. );
  542. try {
  543. // await the css content
  544. const cssContent = await promisesFs.readFile(filePath, { encoding: "utf8" });
  545. ctx.type = "text/css"; // Set the Content-Type header
  546. ctx.body = cssContent; // Serve the CSS content
  547. } catch (err) {
  548. console.error("Error reading CSS file:", err.message);
  549. ctx.status = 404; // Return a 404 status if the file is not found
  550. ctx.body = "Theme not found.";
  551. }
  552. })
  553. .get("/custom-style.css", async (ctx) => {
  554. ctx.type = "text/css";
  555. try {
  556. // Read the CSS file
  557. const cssContent = await fs.readFileSync(customStyleFile, "utf8");
  558. ctx.type = "text/css"; // Set the Content-Type header
  559. ctx.body = cssContent; // Serve the CSS content
  560. } catch (err) {
  561. console.error("Error reading custom style file:", err.message);
  562. ctx.status = 404; // Return a 404 status if the file is not found
  563. ctx.body = "Custom style not found.";
  564. }
  565. })
  566. .get("/profile", async (ctx) => {
  567. const myFeedId = await meta.myFeedId();
  568. const gt = Number(ctx.request.query["gt"] || -1);
  569. const lt = Number(ctx.request.query["lt"] || -1);
  570. if (lt > 0 && gt > 0 && gt >= lt)
  571. throw new Error("Given search range is empty");
  572. const description = await about.description(myFeedId);
  573. const name = await about.name(myFeedId);
  574. const image = await about.image(myFeedId);
  575. const messages = await post.fromPublicFeed(myFeedId, gt, lt);
  576. const firstPost = await post.firstBy(myFeedId);
  577. const lastPost = await post.latestBy(myFeedId);
  578. const avatarUrl = `/image/256/${encodeURIComponent(image)}`;
  579. ctx.body = await authorView({
  580. feedId: myFeedId,
  581. messages,
  582. firstPost,
  583. lastPost,
  584. name,
  585. description,
  586. avatarUrl,
  587. relationship: { me: true },
  588. });
  589. })
  590. .get("/profile/edit", async (ctx) => {
  591. const myFeedId = await meta.myFeedId();
  592. const description = await about.description(myFeedId);
  593. const name = await about.name(myFeedId);
  594. ctx.body = await editProfileView({
  595. name,
  596. description,
  597. });
  598. })
  599. .post("/profile/edit", koaBody({ multipart: true }), async (ctx) => {
  600. const name = String(ctx.request.body.name);
  601. const description = String(ctx.request.body.description);
  602. const image = await promisesFs.readFile(ctx.request.files.image.filepath);
  603. ctx.body = await post.publishProfileEdit({
  604. name,
  605. description,
  606. image,
  607. });
  608. ctx.redirect("/profile");
  609. })
  610. .get("/publish/custom", async (ctx) => {
  611. ctx.body = await publishCustomView();
  612. })
  613. .get("/json/:message", async (ctx) => {
  614. if (config.public) {
  615. throw new Error(
  616. "Sorry, many actions are unavailable when Oasis is running in public mode. Please run Oasis in the default mode and try again."
  617. );
  618. }
  619. const { message } = ctx.params;
  620. ctx.type = "application/json";
  621. const json = async (message) => {
  622. const json = await meta.get(message);
  623. return JSON.stringify(json, null, 2);
  624. };
  625. ctx.body = await json(message);
  626. })
  627. .get("/blob/:blobId", async (ctx) => {
  628. const { blobId } = ctx.params;
  629. const buffer = await blob.getResolved({ blobId });
  630. ctx.body = buffer;
  631. if (ctx.body.length === 0) {
  632. ctx.response.status = 404;
  633. } else {
  634. ctx.set("Cache-Control", "public,max-age=31536000,immutable");
  635. }
  636. // This prevents an auto-download when visiting the URL.
  637. ctx.attachment(blobId, { type: "inline" });
  638. // If we don't do this explicitly the browser downloads the SVG and thinks
  639. // that it's plain XML, so it doesn't render SVG files correctly. Note that
  640. // this library is **not a full SVG parser**, and may cause false positives
  641. // in the case of malformed XML like `<svg><div></svg>`.
  642. if (isSvg(buffer)) {
  643. ctx.type = "image/svg+xml";
  644. }
  645. })
  646. .get("/image/:imageSize/:blobId", async (ctx) => {
  647. const { blobId, imageSize } = ctx.params;
  648. if (sharp) {
  649. ctx.type = "image/png";
  650. }
  651. const fakePixel = Buffer.from(
  652. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=",
  653. "base64"
  654. );
  655. const fakeImage = (imageSize) =>
  656. sharp
  657. ? sharp({
  658. create: {
  659. width: imageSize,
  660. height: imageSize,
  661. channels: 4,
  662. background: {
  663. r: 0,
  664. g: 0,
  665. b: 0,
  666. alpha: 0.5,
  667. },
  668. },
  669. })
  670. .png()
  671. .toBuffer()
  672. : new Promise((resolve) => resolve(fakePixel));
  673. const image = async ({ blobId, imageSize }) => {
  674. const bufferSource = await blob.get({ blobId });
  675. const fakeId = "&0000000000000000000000000000000000000000000=.sha256";
  676. debug("got buffer source");
  677. return new Promise((resolve) => {
  678. if (blobId === fakeId) {
  679. debug("fake image");
  680. fakeImage(imageSize).then((result) => resolve(result));
  681. } else {
  682. debug("not fake image");
  683. pull(
  684. bufferSource,
  685. pull.collect(async (err, bufferArray) => {
  686. if (err) {
  687. await blob.want({ blobId });
  688. const result = fakeImage(imageSize);
  689. debug({ result });
  690. resolve(result);
  691. } else {
  692. const buffer = Buffer.concat(bufferArray);
  693. if (sharp) {
  694. sharp(buffer)
  695. .resize(imageSize, imageSize)
  696. .png()
  697. .toBuffer()
  698. .then((data) => {
  699. resolve(data);
  700. });
  701. } else {
  702. resolve(buffer);
  703. }
  704. }
  705. })
  706. );
  707. }
  708. });
  709. };
  710. ctx.body = await image({ blobId, imageSize: Number(imageSize) });
  711. })
  712. .get("/modules", async (ctx) => {
  713. const configMods = getConfig();
  714. const popularMod = ctx.cookies.get('popularMod', { signed: false }) || configMods.popularMod;
  715. const topicsMod = ctx.cookies.get('topicsMod', { signed: false }) || configMods.topicsMod;
  716. const summariesMod = ctx.cookies.get('summariesMod', { signed: false }) || configMods.summariesMod;
  717. const latestMod = ctx.cookies.get('latestMod', { signed: false }) || configMods.latestMod;
  718. const threadsMod = ctx.cookies.get('threadsMod', { signed: false }) || configMods.threadsMod;
  719. const multiverseMod = ctx.cookies.get('multiverseMod', { signed: false }) || configMods.multiverseMod;
  720. const inboxMod = ctx.cookies.get('inboxMod', { signed: false }) || configMods.inboxMod;
  721. const invitesMod = ctx.cookies.get('invitesMod', { signed: false }) || configMods.invitesMod;
  722. const walletMod = ctx.cookies.get('walletMod', { signed: false }) || configMods.walletMod;
  723. ctx.body = modulesView({ popularMod, topicsMod, summariesMod, latestMod, threadsMod, multiverseMod, inboxMod, invitesMod, walletMod });
  724. })
  725. .get("/settings", async (ctx) => {
  726. const theme = ctx.cookies.get("theme") || config.theme;
  727. const walletUrl = ctx.cookies.get("wallet_url") || config.walletUrl;
  728. const walletUser = ctx.cookies.get("wallet_user") || config.walletUser;
  729. const walletFee = ctx.cookies.get("wallet_fee") || config.walletFee;
  730. const getMeta = async ({ theme }) => {
  731. return settingsView({
  732. theme,
  733. themeNames,
  734. version: version.toString(),
  735. walletUrl,
  736. walletUser,
  737. walletFee
  738. });
  739. };
  740. ctx.body = await getMeta({ theme });
  741. })
  742. .get("/peers", async (ctx) => {
  743. const theme = ctx.cookies.get("theme") || config.theme;
  744. const getMeta = async ({ theme }) => {
  745. const peers = await meta.connectedPeers();
  746. const peersWithNames = await Promise.all(
  747. peers.map(async ([key, value]) => {
  748. value.name = await about.name(value.key);
  749. return [key, value];
  750. })
  751. );
  752. return peersView({
  753. peers: peersWithNames,
  754. supports: supports,
  755. blocks: blocks,
  756. recommends: recommends,
  757. });
  758. };
  759. ctx.body = await getMeta({ theme });
  760. })
  761. .get("/invites", async (ctx) => {
  762. const theme = ctx.cookies.get("theme") || config.theme;
  763. const invitesMod = ctx.cookies.get("invitesMod") || 'on';
  764. if (invitesMod !== 'on') {
  765. ctx.redirect('/modules');
  766. return;
  767. }
  768. const getMeta = async ({ theme }) => {
  769. return invitesView({});
  770. };
  771. ctx.body = await getMeta({ theme });
  772. })
  773. .get("/likes/:feed", async (ctx) => {
  774. const { feed } = ctx.params;
  775. const likes = async ({ feed }) => {
  776. const pendingMessages = post.likes({ feed });
  777. const pendingName = about.name(feed);
  778. return likesView({
  779. messages: await pendingMessages,
  780. feed,
  781. name: await pendingName,
  782. });
  783. };
  784. ctx.body = await likes({ feed });
  785. })
  786. .get("/settings/readme", async (ctx) => {
  787. const status = async (text) => {
  788. return markdownView({ text });
  789. };
  790. ctx.body = await status(readme);
  791. })
  792. .get("/mentions", async (ctx) => {
  793. const mentions = async () => {
  794. const messages = await post.mentionsMe();
  795. return mentionsView({ messages });
  796. };
  797. ctx.body = await mentions();
  798. })
  799. .get("/thread/:message", async (ctx) => {
  800. const { message } = ctx.params;
  801. const thread = async (message) => {
  802. const messages = await post.fromThread(message);
  803. debug("got %i messages", messages.length);
  804. return threadView({ messages });
  805. };
  806. ctx.body = await thread(message);
  807. })
  808. .get("/subtopic/:message", async (ctx) => {
  809. const { message } = ctx.params;
  810. const rootMessage = await post.get(message);
  811. const myFeedId = await meta.myFeedId();
  812. debug("%O", rootMessage);
  813. const messages = [rootMessage];
  814. ctx.body = await subtopicView({ messages, myFeedId });
  815. })
  816. .get("/publish", async (ctx) => {
  817. ctx.body = await publishView();
  818. })
  819. .get("/comment/:message", async (ctx) => {
  820. const { messages, myFeedId, parentMessage } =
  821. await resolveCommentComponents(ctx);
  822. ctx.body = await commentView({ messages, myFeedId, parentMessage });
  823. })
  824. .get("/wallet", async (ctx) => {
  825. const walletMod = ctx.cookies.get("walletMod") || 'on';
  826. if (walletMod !== 'on') {
  827. ctx.redirect('/modules');
  828. return;
  829. }
  830. const url = ctx.cookies.get("wallet_url") || config.walletUrl;
  831. const user = ctx.cookies.get("wallet_user") || config.walletUser;
  832. const pass = ctx.cookies.get("wallet_pass") || config.walletPass;
  833. try {
  834. const balance = await wallet.getBalance(url, user, pass);
  835. ctx.body = await walletView(balance);
  836. } catch (error) {
  837. ctx.body = await walletErrorView(error);
  838. }
  839. })
  840. .get("/wallet/history", async (ctx) => {
  841. const url = ctx.cookies.get("wallet_url") || config.walletUrl;
  842. const user = ctx.cookies.get("wallet_user") || config.walletUser;
  843. const pass = ctx.cookies.get("wallet_pass") || config.walletPass;
  844. try {
  845. const balance = await wallet.getBalance(url, user, pass);
  846. const transactions = await wallet.listTransactions(url, user, pass);
  847. ctx.body = await walletHistoryView(balance, transactions);
  848. } catch (error) {
  849. ctx.body = await walletErrorView(error);
  850. }
  851. })
  852. .get("/wallet/receive", async (ctx) => {
  853. const url = ctx.cookies.get("wallet_url") || config.walletUrl;
  854. const user = ctx.cookies.get("wallet_user") || config.walletUser;
  855. const pass = ctx.cookies.get("wallet_pass") || config.walletPass;
  856. try {
  857. const balance = await wallet.getBalance(url, user, pass);
  858. const address = await wallet.getAddress(url, user, pass);
  859. ctx.body = await walletReceiveView(balance, address);
  860. } catch (error) {
  861. ctx.body = await walletErrorView(error);
  862. }
  863. })
  864. .get("/wallet/send", async (ctx) => {
  865. const url = ctx.cookies.get("wallet_url") || config.walletUrl;
  866. const user = ctx.cookies.get("wallet_user") || config.walletUser;
  867. const pass = ctx.cookies.get("wallet_pass") || config.walletPass;
  868. const fee = ctx.cookies.get("wallet_fee") || config.walletFee;
  869. try {
  870. const balance = await wallet.getBalance(url, user, pass);
  871. ctx.body = await walletSendFormView(balance, null, null, fee, null);
  872. } catch (error) {
  873. ctx.body = await walletErrorView(error);
  874. }
  875. })
  876. .post(
  877. "/subtopic/preview/:message",
  878. koaBody({ multipart: true }),
  879. async (ctx) => {
  880. const { message } = ctx.params;
  881. const rootMessage = await post.get(message);
  882. const myFeedId = await meta.myFeedId();
  883. const rawContentWarning = String(ctx.request.body.contentWarning).trim();
  884. const contentWarning =
  885. rawContentWarning.length > 0 ? rawContentWarning : undefined;
  886. const messages = [rootMessage];
  887. const previewData = await preparePreview(ctx);
  888. ctx.body = await previewSubtopicView({
  889. messages,
  890. myFeedId,
  891. previewData,
  892. contentWarning,
  893. });
  894. }
  895. )
  896. .post("/subtopic/:message", koaBody(), async (ctx) => {
  897. const { message } = ctx.params;
  898. const text = String(ctx.request.body.text);
  899. const rawContentWarning = String(ctx.request.body.contentWarning).trim();
  900. const contentWarning =
  901. rawContentWarning.length > 0 ? rawContentWarning : undefined;
  902. const publishSubtopic = async ({ message, text }) => {
  903. // TODO: rename `message` to `parent` or `ancestor` or similar
  904. const mentions = ssbMentions(text) || undefined;
  905. const parent = await post.get(message);
  906. return post.subtopic({
  907. parent,
  908. message: { text, mentions, contentWarning },
  909. });
  910. };
  911. ctx.body = await publishSubtopic({ message, text });
  912. ctx.redirect(`/thread/${encodeURIComponent(message)}`);
  913. })
  914. .post(
  915. "/comment/preview/:message",
  916. koaBody({ multipart: true }),
  917. async (ctx) => {
  918. const { messages, contentWarning, myFeedId, parentMessage } =
  919. await resolveCommentComponents(ctx);
  920. const previewData = await preparePreview(ctx);
  921. ctx.body = await previewCommentView({
  922. messages,
  923. myFeedId,
  924. contentWarning,
  925. parentMessage,
  926. previewData,
  927. });
  928. }
  929. )
  930. .post("/comment/:message", koaBody(), async (ctx) => {
  931. const { message } = ctx.params;
  932. const text = String(ctx.request.body.text);
  933. const rawContentWarning = String(ctx.request.body.contentWarning);
  934. const contentWarning =
  935. rawContentWarning.length > 0 ? rawContentWarning : undefined;
  936. const publishComment = async ({ message, text }) => {
  937. // TODO: rename `message` to `parent` or `ancestor` or similar
  938. const mentions = ssbMentions(text) || undefined;
  939. const parent = await meta.get(message);
  940. return post.comment({
  941. parent,
  942. message: { text, mentions, contentWarning },
  943. });
  944. };
  945. ctx.body = await publishComment({ message, text });
  946. ctx.redirect(`/thread/${encodeURIComponent(message)}`);
  947. })
  948. .post("/publish/preview", koaBody({ multipart: true }), async (ctx) => {
  949. const rawContentWarning = String(ctx.request.body.contentWarning).trim();
  950. // Only submit content warning if it's a string with non-zero length.
  951. const contentWarning =
  952. rawContentWarning.length > 0 ? rawContentWarning : undefined;
  953. const previewData = await preparePreview(ctx);
  954. ctx.body = await previewView({ previewData, contentWarning });
  955. })
  956. .post("/publish", koaBody(), async (ctx) => {
  957. const text = String(ctx.request.body.text);
  958. const rawContentWarning = String(ctx.request.body.contentWarning);
  959. // Only submit content warning if it's a string with non-zero length.
  960. const contentWarning =
  961. rawContentWarning.length > 0 ? rawContentWarning : undefined;
  962. const publish = async ({ text, contentWarning }) => {
  963. const mentions = ssbMentions(text) || undefined;
  964. return post.root({
  965. text,
  966. mentions,
  967. contentWarning,
  968. });
  969. };
  970. ctx.body = await publish({ text, contentWarning });
  971. ctx.redirect("/public/latest");
  972. })
  973. .post("/publish/custom", koaBody(), async (ctx) => {
  974. const text = String(ctx.request.body.text);
  975. const obj = JSON.parse(text);
  976. ctx.body = await post.publishCustom(obj);
  977. ctx.redirect(`/public/latest`);
  978. })
  979. .post("/follow/:feed", koaBody(), async (ctx) => {
  980. const { feed } = ctx.params;
  981. const referer = new URL(ctx.request.header.referer);
  982. ctx.body = await friend.follow(feed);
  983. ctx.redirect(referer.href);
  984. })
  985. .post("/unfollow/:feed", koaBody(), async (ctx) => {
  986. const { feed } = ctx.params;
  987. const referer = new URL(ctx.request.header.referer);
  988. ctx.body = await friend.unfollow(feed);
  989. ctx.redirect(referer.href);
  990. })
  991. .post("/block/:feed", koaBody(), async (ctx) => {
  992. const { feed } = ctx.params;
  993. const referer = new URL(ctx.request.header.referer);
  994. ctx.body = await friend.block(feed);
  995. ctx.redirect(referer.href);
  996. })
  997. .post("/unblock/:feed", koaBody(), async (ctx) => {
  998. const { feed } = ctx.params;
  999. const referer = new URL(ctx.request.header.referer);
  1000. ctx.body = await friend.unblock(feed);
  1001. ctx.redirect(referer.href);
  1002. })
  1003. .post("/like/:message", koaBody(), async (ctx) => {
  1004. const { message } = ctx.params;
  1005. // TODO: convert all so `message` is full message and `messageKey` is key
  1006. const messageKey = message;
  1007. const voteValue = Number(ctx.request.body.voteValue);
  1008. const encoded = {
  1009. message: encodeURIComponent(message),
  1010. };
  1011. const referer = new URL(ctx.request.header.referer);
  1012. referer.hash = `centered-footer-${encoded.message}`;
  1013. const like = async ({ messageKey, voteValue }) => {
  1014. const value = Number(voteValue);
  1015. const message = await post.get(messageKey);
  1016. const isPrivate = message.value.meta.private === true;
  1017. const messageRecipients = isPrivate ? message.value.content.recps : [];
  1018. const normalized = messageRecipients.map((recipient) => {
  1019. if (typeof recipient === "string") {
  1020. return recipient;
  1021. }
  1022. if (typeof recipient.link === "string") {
  1023. return recipient.link;
  1024. }
  1025. return null;
  1026. });
  1027. const recipients = normalized.length > 0 ? normalized : undefined;
  1028. return vote.publish({ messageKey, value, recps: recipients });
  1029. };
  1030. ctx.body = await like({ messageKey, voteValue });
  1031. ctx.redirect(referer.href);
  1032. })
  1033. .post("/update", koaBody(), async (ctx) => {
  1034. const util = require("node:util");
  1035. const exec = util.promisify(require("node:child_process").exec);
  1036. async function updateTool() {
  1037. const { stdout, stderr } = await exec("git reset --hard && git pull && npm install .");
  1038. console.log("oasis@version: updating Oasis...");
  1039. console.log(stdout);
  1040. console.log(stderr);
  1041. }
  1042. await updateTool();
  1043. const referer = new URL(ctx.request.header.referer);
  1044. ctx.redirect(referer.href);
  1045. })
  1046. .post("/theme.css", koaBody(), async (ctx) => {
  1047. const theme = String(ctx.request.body.theme);
  1048. ctx.cookies.set("theme", theme);
  1049. const referer = new URL(ctx.request.header.referer);
  1050. ctx.redirect(referer.href);
  1051. })
  1052. .post("/language", koaBody(), async (ctx) => {
  1053. const language = String(ctx.request.body.language);
  1054. ctx.cookies.set("language", language);
  1055. const referer = new URL(ctx.request.header.referer);
  1056. ctx.redirect(referer.href);
  1057. })
  1058. .post("/settings/conn/start", koaBody(), async (ctx) => {
  1059. await meta.connStart();
  1060. ctx.redirect("/peers");
  1061. })
  1062. .post("/settings/conn/stop", koaBody(), async (ctx) => {
  1063. await meta.connStop();
  1064. ctx.redirect("/peers");
  1065. })
  1066. .post("/settings/conn/sync", koaBody(), async (ctx) => {
  1067. await meta.sync();
  1068. ctx.redirect("/peers");
  1069. })
  1070. .post("/settings/conn/restart", koaBody(), async (ctx) => {
  1071. await meta.connRestart();
  1072. ctx.redirect("/peers");
  1073. })
  1074. .post("/settings/invite/accept", koaBody(), async (ctx) => {
  1075. try {
  1076. const invite = String(ctx.request.body.invite);
  1077. await meta.acceptInvite(invite);
  1078. } catch (e) {
  1079. // Just in case it's an invalid invite code. :(
  1080. debug(e);
  1081. }
  1082. ctx.redirect("/invites");
  1083. })
  1084. .post("/settings/rebuild", async (ctx) => {
  1085. meta.rebuild();
  1086. ctx.redirect("/settings");
  1087. })
  1088. .post("/save-modules", koaBody(), async (ctx) => {
  1089. const popularMod = ctx.request.body.popularForm === 'on' ? 'on' : 'off';
  1090. const topicsMod = ctx.request.body.topicsForm === 'on' ? 'on' : 'off';
  1091. const summariesMod = ctx.request.body.summariesForm === 'on' ? 'on' : 'off';
  1092. const latestMod = ctx.request.body.latestForm === 'on' ? 'on' : 'off';
  1093. const threadsMod = ctx.request.body.threadsForm === 'on' ? 'on' : 'off';
  1094. const multiverseMod = ctx.request.body.multiverseForm === 'on' ? 'on' : 'off';
  1095. const inboxMod = ctx.request.body.inboxForm === 'on' ? 'on' : 'off';
  1096. const invitesMod = ctx.request.body.invitesForm === 'on' ? 'on' : 'off';
  1097. const walletMod = ctx.request.body.walletForm === 'on' ? 'on' : 'off';
  1098. ctx.cookies.set("popularMod", popularMod, { httpOnly: true, maxAge: 86400000, path: '/' });
  1099. ctx.cookies.set("topicsMod", topicsMod, { httpOnly: true, maxAge: 86400000, path: '/' });
  1100. ctx.cookies.set("summariesMod", summariesMod, { httpOnly: true, maxAge: 86400000, path: '/' });
  1101. ctx.cookies.set("latestMod", latestMod, { httpOnly: true, maxAge: 86400000, path: '/' });
  1102. ctx.cookies.set("threadsMod", threadsMod, { httpOnly: true, maxAge: 86400000, path: '/' });
  1103. ctx.cookies.set("multiverseMod", multiverseMod, { httpOnly: true, maxAge: 86400000, path: '/' });
  1104. ctx.cookies.set("inboxMod", inboxMod, { httpOnly: true, maxAge: 86400000, path: '/' });
  1105. ctx.cookies.set("invitesMod", invitesMod, { httpOnly: true, maxAge: 86400000, path: '/' });
  1106. ctx.cookies.set("walletMod", walletMod, { httpOnly: true, maxAge: 86400000, path: '/' });
  1107. const currentConfig = getConfig();
  1108. currentConfig.popularMod = popularMod;
  1109. currentConfig.topicsMod = topicsMod;
  1110. currentConfig.summariesMod = summariesMod;
  1111. currentConfig.latestMod = latestMod;
  1112. currentConfig.threadsMod = threadsMod;
  1113. currentConfig.multiverseMod = multiverseMod;
  1114. currentConfig.inboxMod = inboxMod;
  1115. currentConfig.invitesMod = invitesMod;
  1116. currentConfig.walletMod = walletMod;
  1117. saveConfig(currentConfig);
  1118. ctx.redirect(`/modules`);
  1119. })
  1120. .post("/settings/wallet", koaBody(), async (ctx) => {
  1121. const url = String(ctx.request.body.wallet_url);
  1122. const user = String(ctx.request.body.wallet_user);
  1123. const pass = String(ctx.request.body.wallet_pass);
  1124. const fee = String(ctx.request.body.wallet_fee);
  1125. url && url.trim() !== "" && ctx.cookies.set("wallet_url", url);
  1126. user && user.trim() !== "" && ctx.cookies.set("wallet_user", user);
  1127. pass && pass.trim() !== "" && ctx.cookies.set("wallet_pass", pass);
  1128. fee && fee > 0 && ctx.cookies.set("wallet_fee", fee);
  1129. const referer = new URL(ctx.request.header.referer);
  1130. ctx.redirect(referer.href);
  1131. })
  1132. .post("/wallet/send", koaBody(), async (ctx) => {
  1133. const action = String(ctx.request.body.action);
  1134. const destination = String(ctx.request.body.destination);
  1135. const amount = Number(ctx.request.body.amount);
  1136. const fee = Number(ctx.request.body.fee);
  1137. const url = ctx.cookies.get("wallet_url") || config.walletUrl;
  1138. const user = ctx.cookies.get("wallet_user") || config.walletUser;
  1139. const pass = ctx.cookies.get("wallet_pass") || config.walletPass;
  1140. let balance = null
  1141. try {
  1142. balance = await wallet.getBalance(url, user, pass);
  1143. } catch (error) {
  1144. ctx.body = await walletErrorView(error);
  1145. }
  1146. switch (action) {
  1147. case 'confirm':
  1148. const validation = await wallet.validateSend(url, user, pass, destination, amount, fee);
  1149. if (validation.isValid) {
  1150. try {
  1151. ctx.body = await walletSendConfirmView(balance, destination, amount, fee);
  1152. } catch (error) {
  1153. ctx.body = await walletErrorView(error);
  1154. }
  1155. } else {
  1156. try {
  1157. const statusMessages = {
  1158. type: 'error',
  1159. title: 'validation_errors',
  1160. messages: validation.errors,
  1161. }
  1162. ctx.body = await walletSendFormView(balance, destination, amount, fee, statusMessages);
  1163. } catch (error) {
  1164. ctx.body = await walletErrorView(error);
  1165. }
  1166. }
  1167. break;
  1168. case 'send':
  1169. try {
  1170. const txId = await wallet.sendToAddress(url, user, pass, destination, amount);
  1171. ctx.body = await walletSendResultView(balance, destination, amount, txId);
  1172. } catch (error) {
  1173. ctx.body = await walletErrorView(error);
  1174. }
  1175. break;
  1176. }
  1177. });
  1178. const routes = router.routes();
  1179. const middleware = [
  1180. async (ctx, next) => {
  1181. if (config.public && ctx.method !== "GET") {
  1182. throw new Error(
  1183. "Sorry, many actions are unavailable when Oasis is running in public mode. Please run Oasis in the default mode and try again."
  1184. );
  1185. }
  1186. await next();
  1187. },
  1188. async (ctx, next) => {
  1189. const selectedLanguage = ctx.cookies.get("language") || "en";
  1190. setLanguage(selectedLanguage);
  1191. await next();
  1192. },
  1193. async (ctx, next) => {
  1194. const ssb = await cooler.open();
  1195. const status = await ssb.status();
  1196. const values = Object.values(status.sync.plugins);
  1197. const totalCurrent = Object.values(status.sync.plugins).reduce(
  1198. (acc, cur) => acc + cur,
  1199. 0
  1200. );
  1201. const totalTarget = status.sync.since * values.length;
  1202. const left = totalTarget - totalCurrent;
  1203. // Weird trick to get percentage with 1 decimal place (e.g. 78.9)
  1204. const percent = Math.floor((totalCurrent / totalTarget) * 1000) / 10;
  1205. const mebibyte = 1024 * 1024;
  1206. if (left > mebibyte) {
  1207. ctx.response.body = indexingView({ percent });
  1208. } else {
  1209. await next();
  1210. }
  1211. },
  1212. routes,
  1213. ];
  1214. const { allowHost } = config;
  1215. const app = http({ host, port, middleware, allowHost });
  1216. // HACK: This lets us close the database once tests finish.
  1217. // If we close the database after each test it throws lots of really fun "parent
  1218. // stream closing" errors everywhere and breaks the tests. :/
  1219. app._close = () => {
  1220. nameWarmup.close();
  1221. cooler.close();
  1222. };
  1223. module.exports = app;
  1224. if (config.open === true) {
  1225. open(url);
  1226. }