From 8d5e7de18ece31217924ea67177214e71da9b3e1 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 3 Aug 2024 16:11:31 -0400 Subject: [PATCH] Add torrent help (#2650) * Updating translations. * Adding a link to view all a user's moderation history from an item. - Also making moderation history strings more detailed. * Fix. * Adding admin view moderation history to profile page. * Adding a torrent help message. * Updating translations. * Updating translations. --- lemmy-translations | 2 +- src/shared/components/post/post-listing.tsx | 62 ++++++++++++++++----- src/shared/config.ts | 1 + src/shared/utils/media/is-magnet-link.ts | 9 +++ 4 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 src/shared/utils/media/is-magnet-link.ts diff --git a/lemmy-translations b/lemmy-translations index 1f4e378f..aa39a764 160000 --- a/lemmy-translations +++ b/lemmy-translations @@ -1 +1 @@ -Subproject commit 1f4e378f2e18d843aec1e421b99a69a76ce633b7 +Subproject commit aa39a764aec120d781325dc8b55732aafffe0ac7 diff --git a/src/shared/components/post/post-listing.tsx b/src/shared/components/post/post-listing.tsx index f9261347..7c6ffab5 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -7,6 +7,7 @@ import { canAdmin, canMod } from "@utils/roles"; import classNames from "classnames"; import { Component, linkEvent } from "inferno"; import { Link } from "inferno-router"; +import { T } from "inferno-i18next-dess"; import { AddAdmin, AddModToCommunity, @@ -33,7 +34,7 @@ import { SavePost, TransferCommunity, } from "lemmy-js-client"; -import { relTags } from "../../config"; +import { relTags, torrentHelpUrl } from "../../config"; import { IsoDataOptionalSite, VoteContentType } from "../../interfaces"; import { mdToHtml, mdToHtmlInline } from "../../markdown"; import { I18NextService, UserService } from "../../services"; @@ -52,6 +53,9 @@ import PostActionDropdown from "../common/content-actions/post-action-dropdown"; import { CrossPostParams } from "@utils/types"; import { RequestState } from "../../services/HttpService"; import { toast } from "../../toast"; +import isMagnetLink, { + extractMagnetLinkDownloadName, +} from "@utils/media/is-magnet-link"; type PostListingState = { showEdit: boolean; @@ -176,6 +180,10 @@ export class PostListing extends Component { <> {this.listing()} {this.state.imageExpanded && !this.props.hideImage && this.img} + {this.showBody && + post.url && + isMagnetLink(post.url) && + this.torrentHelp()} {this.showBody && post.url && post.embed_title && ( )} @@ -217,6 +225,19 @@ export class PostListing extends Component { ); } + torrentHelp() { + return ( +
+ + + # + + # + + +
+ ); + } get img() { const { post } = this.postView; const { url } = post; @@ -542,20 +563,31 @@ export class PostListing extends Component { const post = this.postView.post; const url = post.url; - return ( -

- {url && !(hostname(url) === getExternalHost()) && ( - - {hostname(url)} - - )} -

- ); + if (url) { + // If its a torrent link, extract the download name + const linkName = isMagnetLink(url) + ? extractMagnetLinkDownloadName(url) + : !(hostname(url) === getExternalHost()) + ? hostname(url) + : null; + + if (linkName) { + return ( +

+ {url && !(hostname(url) === getExternalHost()) && ( + + {linkName} + + )} +

+ ); + } + } } duplicatesLine() { diff --git a/src/shared/config.ts b/src/shared/config.ts index b6313917..38e26613 100644 --- a/src/shared/config.ts +++ b/src/shared/config.ts @@ -9,6 +9,7 @@ export const donateLemmyUrl = `${joinLemmyUrl}/donate`; export const docsUrl = `${joinLemmyUrl}/docs/en/index.html`; export const helpGuideUrl = `${joinLemmyUrl}/docs/en/users/01-getting-started.html`; // TODO find a way to redirect to the non-en folder export const markdownHelpUrl = `${joinLemmyUrl}/docs/en/users/02-media.html`; +export const torrentHelpUrl = `${markdownHelpUrl}#torrents`; export const sortingHelpUrl = `${joinLemmyUrl}/docs/en/users/03-votes-and-ranking.html`; export const archiveTodayUrl = "https://archive.today"; export const ghostArchiveUrl = "https://ghostarchive.org"; diff --git a/src/shared/utils/media/is-magnet-link.ts b/src/shared/utils/media/is-magnet-link.ts new file mode 100644 index 00000000..084cd3c6 --- /dev/null +++ b/src/shared/utils/media/is-magnet-link.ts @@ -0,0 +1,9 @@ +const magnetLinkRegex = /^magnet:\?xt=urn:btih:[0-9a-fA-F]{40,}.*$/; + +export default function isMagnetLink(url: string) { + return magnetLinkRegex.test(url); +} + +export function extractMagnetLinkDownloadName(url: string) { + return new URLSearchParams(url).get("dn"); +}