Watch
1
0
Fork
You've already forked nitter
0
mirror of https://github.com/zedeus/nitter synced 2026-11-22 12:57:19 +00:00

Merge branch 'zedeus:master' into master

This commit is contained in:
Salastil 2026-08-14 22:57:40 -04:00 • committed by GitHub
commit 162a15f7a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
40 changed files with 1552 additions and 178 deletions

View file

@ -62,6 +62,7 @@ jobs:
needs: [build-test]
name: Integration test
runs-on: ubuntu-24.04
timeout-minutes: 30
services:
redis:
@ -136,6 +137,7 @@ jobs:
cp nitter.example.conf nitter.conf
sed -i 's/enableDebug = false/enableDebug = true/g' nitter.conf
sed -i 's/maxRetries = 1/maxRetries = 10/g' nitter.conf
sed -i 's/hostname = "nitter.net"/hostname = "localhost:8080"/g' nitter.conf
nim r tools/rendermd.nim
nim r tools/gencss.nim
@ -147,4 +149,4 @@ jobs:
run: |
./nitter &
cd tests
poetry run pytest -n3 --reruns=5 --rs .
poetry run pytest -n3 --rs .

34
public/js/embedResize.js Normal file
View file

@ -0,0 +1,34 @@
(function () {
var embed = document.querySelector(".embed-wrapper, .embed-video");
if (!embed) return;
var video = embed.querySelector("video");
if (video) {
video.onplay = function () {
embed.classList.add("video-playing");
};
video.onpause = video.onended = function () {
embed.classList.remove("video-playing");
};
}
var lastHeight = 0;
function sendHeight() {
var h = embed.offsetHeight;
if (h !== lastHeight && h > 0) {
lastHeight = h;
window.parent.postMessage(["resizeIframe", { h: h }], "*");
}
}
// MessageChannel height request (used by oEmbed)
window.addEventListener("message", function (e) {
if (e.source === window.parent && e.ports && e.ports[0]) {
e.ports[0].postMessage(embed.offsetHeight);
}
});
window.addEventListener("load", sendHeight);
new ResizeObserver(sendHeight).observe(embed);
})();

221
public/js/widgets.js Normal file
View file

@ -0,0 +1,221 @@
/**
* Drop-in replacement for Twitter's widgets.js
* Redirects twitter-tweet blockquotes to Nitter embeds
*/
(function () {
"use strict";
if (window.__nitterWidgets) return;
window.__nitterWidgets = true;
var NITTER = new URL(document.currentScript.src).origin;
var TWEET_RE = /(?:twitter\.com|x\.com)\/([^\/]+)\/status\/(\d+)/i;
var SELECTOR = "blockquote.twitter-tweet, blockquote.twitter-video";
var readyCallbacks = [];
var eventCallbacks = {};
var isReady = false;
function safeCall(fn, arg) {
try {
fn(arg);
} catch (e) { }
}
function fireEvent(name, data) {
(eventCallbacks[name] || []).forEach(function (cb) {
safeCall(cb, data);
});
}
function parseTweetUrl(url) {
if (!url) return null;
var m = TWEET_RE.exec(url);
if (m) return { user: m[1], id: m[2] };
m = url.match(/(\d{15,})/);
return m ? { user: null, id: m[1] } : null;
}
function createIframe(tweet, opts) {
var url;
if (opts.videoOnly) {
url = NITTER + "/i/videos/tweet/" + tweet.id;
} else {
var path = tweet.user ? "/" + tweet.user : "/i";
url = NITTER + path + "/status/" + tweet.id + "/embed";
if (opts.theme) {
var theme =
opts.theme === "dark"
? "nitter"
: opts.theme === "light"
? "twitter"
: opts.theme;
url += "?theme=" + encodeURIComponent(theme);
}
}
var iframe = document.createElement("iframe");
iframe.src = url;
iframe.className = "nitter-embed-frame";
iframe.loading = "lazy";
iframe.setAttribute("allowtransparency", "true");
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("scrolling", "no");
if (opts.videoOnly) iframe.setAttribute("allowfullscreen", "true");
var width = opts.width || 550;
var margin =
opts.align === "center"
? "10px auto"
: opts.align === "right"
? "10px 0 10px auto"
: "10px 0";
iframe.style.cssText =
"width:100%;max-width:" +
width +
"px;height:300px;" +
"border:none;display:block;margin:" +
margin;
iframe.addEventListener("load", function () {
fireEvent("rendered", { target: iframe });
});
return iframe;
}
function processBlockquote(bq) {
if (bq.dataset.nitterProcessed) return false;
bq.dataset.nitterProcessed = "true";
var tweet = null;
var links = bq.querySelectorAll("a[href]");
for (var i = 0; i < links.length && !tweet; i++) {
tweet = parseTweetUrl(links[i].href);
}
if (!tweet) return false;
var d = bq.dataset;
var iframe = createIframe(tweet, {
width: d.mediaMaxWidth || d.width,
align: d.align,
theme: d.theme,
videoOnly: d.mediaMaxWidth !== undefined,
});
bq.style.display = "none";
bq.parentNode.insertBefore(iframe, bq.nextSibling);
return true;
}
function processEmbeds(root) {
var bqs = (root || document).querySelectorAll(
SELECTOR + ":not([data-nitter-processed])",
);
for (var i = 0; i < bqs.length; i++) processBlockquote(bqs[i]);
}
function handleResize(e) {
if (!Array.isArray(e.data) || e.data[0] !== "resizeIframe") return;
var h = e.data[1] && e.data[1].h;
if (!h || h <= 0 || h > 10000) return; // Cap at 10000px for sanity
var frames = document.querySelectorAll("iframe.nitter-embed-frame");
for (var i = 0; i < frames.length; i++) {
if (frames[i].contentWindow === e.source) {
frames[i].style.height = h + "px";
return;
}
}
}
function observeDOM() {
if (!window.MutationObserver || !document.body) return;
function matches(el) {
return el.matches(SELECTOR) || el.querySelector(SELECTOR);
}
new MutationObserver(function (muts) {
var found = muts.some(function (mut) {
return Array.prototype.some.call(mut.addedNodes, function (n) {
return n.nodeType === 1 && matches(n);
});
});
if (found) processEmbeds();
}).observe(document.body, { childList: true, subtree: true });
}
function embedTweet(id, container, opts) {
if (!container) return Promise.reject("No container");
var iframe = createIframe({ id: id, user: null }, opts || {});
container.appendChild(iframe);
return Promise.resolve(iframe);
}
var prevTwttr = window.twttr;
window.twttr = {
widgets: {
load: processEmbeds,
createTweet: embedTweet,
createTweetEmbed: embedTweet,
createVideo: embedTweet,
loaded: true,
},
events: {
bind: function (name, cb) {
if (typeof cb !== "function") return;
if (!eventCallbacks[name]) eventCallbacks[name] = [];
eventCallbacks[name].push(cb);
},
unbind: function (name, cb) {
if (!eventCallbacks[name]) return;
eventCallbacks[name] = cb
? eventCallbacks[name].filter(function (f) {
return f !== cb;
})
: [];
},
},
ready: function (cb) {
if (typeof cb !== "function") return;
if (isReady) cb(window.twttr);
else readyCallbacks.push(cb);
},
_e: [],
};
// Process callbacks queued before load (twttr._e pattern)
if (prevTwttr && prevTwttr._e) {
prevTwttr._e.forEach(function (cb) {
safeCall(cb);
});
}
// Remove any Twitter scripts that snuck through
document
.querySelectorAll(
'script[src*="platform.twitter.com"], script[src*="platform.x.com"]',
)
.forEach(function (s) {
s.remove();
});
function init() {
window.addEventListener("message", handleResize);
processEmbeds();
observeDOM();
isReady = true;
readyCallbacks.forEach(function (cb) {
safeCall(cb, window.twttr);
});
readyCallbacks = [];
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
})();

View file

@ -35,8 +35,8 @@ proc userTweetsUrl(id: string; cursor: string): ApiReq =
proc userTweetsAndRepliesUrl(id: string; cursor: string): ApiReq =
return apiReq(graphUserTweetsAndRepliesV2, restIdVars % [id, cursor, "20"], userTweetsFieldToggles, skipTid=true)
proc tweetDetailUrl(id: string; cursor: string): ApiReq =
return apiReq(graphTweet, tweetVars % [id, cursor])
proc tweetDetailUrl(id, cursor: string; mode = Relevance): ApiReq =
return apiReq(graphTweet, tweetVars % [id, cursor, $mode])
# let cookieVars = tweetDetailVars % [id, cursor]
# result = ApiReq(
# cookie: apiUrl(graphTweetDetail, cookieVars, tweetDetailFieldToggles),
@ -230,21 +230,28 @@ proc getGraphTweetResult*(id: string): Future[Tweet] {.async.} =
js = await fetch(url)
result = parseGraphTweetResult(js)
proc getGraphTweet(id: string; after=""): Future[Conversation] {.async.} =
proc getTweetByRestId*(id: string): Future[Tweet] {.async.} =
if id.len == 0: return
let
url = apiReq(graphTweetResultByRestId, tweetByRestIdVars % id, articleFieldToggles)
js = await fetch(url)
result = parseTweetByRestId(js)
proc getGraphTweet(id: string; after=""; mode = Relevance): Future[Conversation] {.async.} =
if id.len == 0: return
let
cursor = cursorParam(after)
js = await fetch(tweetDetailUrl(id, cursor))
js = await fetch(tweetDetailUrl(id, cursor, mode))
result = parseGraphConversation(js, id)
proc getReplies*(id, after: string): Future[Result[Chain]] {.async.} =
result = (await getGraphTweet(id, after)).replies
proc getReplies*(id, after: string; mode = Relevance): Future[Result[Chain]] {.async.} =
result = (await getGraphTweet(id, after, mode)).replies
result.beginning = after.len == 0
proc getTweet*(id: string; after=""): Future[Conversation] {.async.} =
result = await getGraphTweet(id)
proc getTweet*(id: string; after=""; mode = Relevance): Future[Conversation] {.async.} =
result = await getGraphTweet(id, mode=mode)
if after.len > 0:
result.replies = await getReplies(id, after)
result.replies = await getReplies(id, after, mode)
proc getGraphEditHistory*(id: string): Future[EditHistory] {.async.} =
if id.len == 0: return
@ -263,12 +270,19 @@ proc getGraphTweetSearch*(query: Query; after=""): Future[Timeline] {.async.} =
if q.len == 0 or q == emptyQuery:
return Timeline(query: query, beginning: true)
let product =
case query.kind
of top: "Top"
# profile media feeds (RSS, multi-user timelines) must stay chronological
of media: (if query.fromUser.len == 0: "Media" else: "Latest")
else: "Latest"
var
variables = %*{
"rawQuery": q,
"count": 20,
"querySource": "typed_query",
"product": "Latest",
"product": product,
"withGrokTranslatedBio":true,
"withQuickPromoteEligibilityTweetFields":false
}
@ -283,33 +297,40 @@ proc getGraphTweetSearch*(query: Query; after=""): Future[Timeline] {.async.} =
# when no more items are available the API just returns the last page in
# full. this detects that and clears the page instead.
if after.len > 0 and result.bottom.len > 0 and maxId.len == 0 and
after[0..<64] == result.bottom[0..<64]:
let prefix = min(64, min(after.len, result.bottom.len))
if prefix > 0 and maxId.len == 0 and
after[0..<prefix] == result.bottom[0..<prefix]:
result.content.setLen(0)
proc getGraphUserSearch*(query: Query; after=""): Future[Result[User]] {.async.} =
proc getGraphProductSearch[T](query: Query; product: string;
after=""): Future[Result[T]] {.async.} =
if query.text.len == 0:
return Result[User](query: query, beginning: true)
return Result[T](query: query, beginning: true)
var
variables = %*{
"rawQuery": query.text,
"count": 20,
"querySource": "typed_query",
"product": "People",
"product": product,
"withGrokTranslatedBio":true,
"withQuickPromoteEligibilityTweetFields":false
}
if after.len > 0:
variables["cursor"] = % after
result.beginning = false
let
let
url = apiReq(graphSearchTimeline, $variables)
js = await fetch(url)
result = parseGraphSearch[User](js, after)
result = parseGraphSearch[T](js, after)
result.query = query
proc getGraphUserSearch*(query: Query; after=""): Future[Result[User]] =
getGraphProductSearch[User](query, "People", after)
proc getGraphListSearch*(query: Query; after=""): Future[Result[ListSearchResult]] =
getGraphProductSearch[ListSearchResult](query, "Lists", after)
proc getPhotoRail*(id: string): Future[PhotoRail] {.async.} =
if id.len == 0: return
let js = await fetch(mediaUrl(id, "", 30))

View file

@ -35,7 +35,7 @@ const
graphCommunityModerators* = "GBMT3GOWy5dYsYC4XJfvow/moderatorsSliceTimeline_Query"
graphCommunityHashtags* = "40DyrMxfCknGuZwE-keW_Q/CommunityHashtagsTimeline"
graphTweetResultByRestId* = "qtXMy1p5Y62uCskc_NUPJw/TweetResultByRestId"
graphTweetResultByRestId* = "4hhGRbehkcUVTKf8n0f0xw/TweetResultByRestId"
graphTweetResultsByRestIds* = "Sc9EUQTZNEH-wzegn-nHvQ/TweetResultsByRestIds"
graphBroadcast* = "FJLCzpXCLPM1jUZqmM7oEA/BroadcastQuery"
@ -92,6 +92,7 @@ const
tweetVars* = """{
"postId": "$1",
$2
"ranking_mode": "$3",
"includeHasBirdwatchNotes": false,
"includePromotedContent": false,
"withBirdwatchNotes": true,
@ -165,6 +166,14 @@ const
articleFieldToggles* = """{"withArticleRichContentState":true,"withArticlePlainText":false,"withArticleSummaryText":true,"withArticleVoiceOver":true}"""
tweetByRestIdVars* = """{
"tweetId": "$1",
"includePromotedContent": false,
"withBirdwatchNotes": false,
"withVoice": false,
"withCommunity": false
}""".replace(" ", "").replace("\n", "")
communityTweetsVars* = """{
"communityId": "$1", $2
"count": 20,

View file

@ -81,8 +81,8 @@ routes:
if request.path.len == 0 or request.path[0] != '/':
halt Http400
# skip all file URLs
cond "." notin request.path
# skip all file URLs (except Twitter widget compatibility)
cond "." notin request.path or request.path == "/embed/Tweet.html"
applyUrlPrefs()
get "/":

View file

@ -206,6 +206,20 @@ proc parseGraphCommunity*(js: JsonNode): Community =
if tag.len > 0:
result.hashtags.add tag
proc parseListObject(js: JsonNode; owner: User): List =
List(
id: js{"id_str"}.getStr,
name: js{"name"}.getStr,
username: owner.username,
userId: owner.id,
description: js{"description"}.getStr,
members: js{"member_count"}.getInt,
banner: select(
js{"custom_banner_media", "media_info", "original_img_url"},
js{"default_banner_media", "media_info", "original_img_url"}
).getImageStr
)
proc parseGraphList*(js: JsonNode): List =
if js.isNull: return
@ -215,15 +229,17 @@ proc parseGraphList*(js: JsonNode): List =
if list.isNull:
return
result = List(
id: list{"id_str"}.getStr,
name: list{"name"}.getStr,
username: list{"user_results", "result", "legacy", "screen_name"}.getStr,
userId: list{"user_results", "result", "rest_id"}.getStr,
description: list{"description"}.getStr,
members: list{"member_count"}.getInt,
banner: list{"custom_banner_media", "media_info", "original_img_url"}.getImageStr
result = parseListObject(list, parseGraphUser(list))
proc parseGraphSearchList(js: JsonNode): ListSearchResult =
let owner = parseGraphUser(js)
result = ListSearchResult(
list: parseListObject(js, owner),
owner: owner,
followersContext: js{"followers_context"}.getStr
)
for url in js{"facepile_urls"}:
result.facepiles.add url.getStr
proc parsePoll(js: JsonNode): Poll =
let vals = js{"binding_values"}
@ -694,10 +710,26 @@ proc parseGraphTweet*(js: JsonNode): Tweet =
with birdwatch, js{"birdwatch_pivot"}:
result.note = parseCommunityNote(birdwatch)
proc getConvSection(js: JsonNode): string =
let details = select(
js{"item", "client_event_info", "details"},
js{"item", "clientEventInfo", "details"}
)
select(
details{"conversation_details", "conversation_section"},
details{"conversationDetails", "conversationSection"}
).getStr
proc parseGraphThread(js: JsonNode): tuple[thread: Chain; self: bool] =
var checkedSection = false
for t in ? js{"content", "items"}:
let entryId = t.getEntryId
if "tweet-" in entryId and "promoted" notin entryId:
if not checkedSection:
checkedSection = true
if getConvSection(t) == "RelatedTweet":
result.thread.related = true
let tweet = t.getTweetResult("item")
if tweet.notNull:
result.thread.content.add parseGraphTweet(tweet)
@ -719,6 +751,10 @@ proc parseGraphTweetResult*(js: JsonNode): Tweet =
with tweet, js{"data", "tweet_result", "result"}:
result = parseGraphTweet(tweet)
proc parseTweetByRestId*(js: JsonNode): Tweet =
with tweet, js{"data", "tweetResult", "result"}:
result = parseGraphTweet(tweet)
proc parseGraphTweetResults*(js: JsonNode): seq[Tweet] =
let results = js{"data", "tweetResult"}
if results.kind != JArray: return
@ -758,7 +794,8 @@ proc parseGraphConversation*(js: JsonNode; tweetId: string): Conversation =
result.before.content.add tweet
elif not entryId.endsWith(tweetId):
result.before.content.add Tweet(id: entryId.getId)
elif entryId.startsWith("conversationthread"):
elif entryId.startsWith("conversationthread") or
entryId.startsWith("tweetdetailrelatedtweets"):
let (thread, self) = parseGraphThread(e)
if self:
result.after = thread
@ -808,20 +845,31 @@ proc parseGraphEditHistory*(js: JsonNode; tweetId: string): EditHistory =
if tweetResult.notNull:
result.history.add parseGraphTweet(tweetResult)
iterator extractTweetsFromModuleItems(items: JsonNode): Tweet =
for item in items:
with tweetResult, item.getTweetResult("item"):
let tweet = parseGraphTweet(tweetResult)
if not tweet.available:
tweet.id = item.getEntryId.getId
yield tweet
iterator extractListsFromItems(items: JsonNode): ListSearchResult =
for item in items:
with listJs, item{"item", "itemContent", "list"}:
let r = parseGraphSearchList(listJs)
if r.list.id.len > 0:
yield r
proc extractTweetsFromEntry*(e: JsonNode): seq[Tweet] =
with tweetResult, getTweetResult(e):
var tweet = parseGraphTweet(tweetResult)
let tweet = parseGraphTweet(tweetResult)
if not tweet.available:
tweet.id = e.getEntryId.getId
result.add tweet
return
for item in e{"content", "items"}:
with tweetResult, item.getTweetResult("item"):
var tweet = parseGraphTweet(tweetResult)
if not tweet.available:
tweet.id = item.getEntryId.getId
result.add tweet
for tweet in extractTweetsFromModuleItems(e{"content", "items"}):
result.add tweet
proc parseGraphTimeline*(js: JsonNode; after=""): Profile =
result = Profile(tweets: Timeline(beginning: after.len == 0))
@ -836,12 +884,8 @@ proc parseGraphTimeline*(js: JsonNode; after=""): Profile =
for i in instructions:
if i{"moduleItems"}.notNull:
for item in i{"moduleItems"}:
with tweetResult, item.getTweetResult("item"):
let tweet = parseGraphTweet(tweetResult)
if not tweet.available:
tweet.id = item.getEntryId.getId
result.tweets.content.add tweet
for tweet in extractTweetsFromModuleItems(i{"moduleItems"}):
result.tweets.content.add tweet
continue
if i{"entries"}.notNull:
@ -876,18 +920,13 @@ proc parseGraphPhotoRail*(js: JsonNode): PhotoRail =
for i in instructions:
if i{"moduleItems"}.notNull:
for item in i{"moduleItems"}:
with tweetResult, item.getTweetResult("item"):
let t = parseGraphTweet(tweetResult)
if not t.available:
t.id = item.getEntryId.getId
for t in extractTweetsFromModuleItems(i{"moduleItems"}):
let photo = extractGalleryPhoto(t)
if photo.url.len > 0:
result.add photo
let photo = extractGalleryPhoto(t)
if photo.url.len > 0:
result.add photo
if result.len == 16:
return
if result.len == 16:
return
continue
if i.getTypeName != "TimelineAddEntries":
@ -904,7 +943,7 @@ proc parseGraphPhotoRail*(js: JsonNode): PhotoRail =
if result.len == 16:
return
proc parseGraphSearch*[T: User | Tweets](js: JsonNode; after=""): Result[T] =
proc parseGraphSearch*[T: User | Tweets | ListSearchResult](js: JsonNode; after=""): Result[T] =
result = Result[T](beginning: after.len == 0)
let instructions = select(
@ -920,19 +959,27 @@ proc parseGraphSearch*[T: User | Tweets](js: JsonNode; after=""): Result[T] =
for e in instruction{"entries"}:
let entryId = e.getEntryId
when T is Tweets:
if entryId.startsWith("tweet"):
with tweetRes, getTweetResult(e):
let tweet = parseGraphTweet(tweetRes)
if not tweet.available:
tweet.id = entryId.getId
if entryId.startsWith("tweet") or entryId.startsWith("search-grid"):
for tweet in extractTweetsFromEntry(e):
result.content.add tweet
elif T is User:
if entryId.startsWith("user"):
with userRes, e{"content", "itemContent"}:
result.content.add parseGraphUser(userRes)
elif T is ListSearchResult:
if entryId.startsWith("list-search"):
for list in extractListsFromItems(e{"content", "items"}):
result.content.add list
if entryId.startsWith("cursor-bottom"):
result.bottom = e{"content", "value"}.getStr
elif typ == "TimelineAddToModule":
when T is Tweets:
for tweet in extractTweetsFromModuleItems(instruction{"moduleItems"}):
result.content.add tweet
elif T is ListSearchResult:
for list in extractListsFromItems(instruction{"moduleItems"}):
result.content.add list
elif typ == "TimelineReplaceEntry":
if instruction{"entry_id_to_replace"}.getStr.startsWith("cursor-bottom"):
result.bottom = instruction{"entry", "content", "value"}.getStr

View file

@ -98,9 +98,9 @@ proc getTimeFromMsStr*(js: JsonNode): DateTime =
proc getId*(id: string): int64 {.inline.} =
let start = id.rfind("-")
if start < 0:
return parseBiggestInt(id)
return parseBiggestInt(id[start + 1 ..< id.len])
try:
parseBiggestInt(if start < 0: id else: id[start + 1 ..< id.len])
except ValueError: 0'i64
proc getId*(js: JsonNode): int64 {.inline.} =
case js.kind
@ -233,9 +233,9 @@ proc replacedWith(runes: seq[Rune]; repls: openArray[ReplaceSlice];
symbol = $runes[rep.slice.a]
result.add a(symbol & name, href = "/search?f=tweets&q=%23" & name)
of rkMention:
result.add a($runes[rep.slice], href = rep.url, title = rep.display)
result.add a($runes[rep.slice], href = rep.url, title = escape(rep.display))
of rkUrl:
result.add a(rep.display, href = rep.url)
result.add a(escape(rep.display), href = rep.url)
of rkRemove:
discard

View file

@ -78,6 +78,9 @@ genPrefs:
hideReplies(checkbox, false):
"Hide tweet replies"
hideRelated(checkbox, true):
"Hide related tweets under replies"
hideCommunityNotes(checkbox, false):
"Hide community notes"

View file

@ -34,7 +34,7 @@ proc initQuery*(pms: Table[string, string]; name=""): Query =
proc getMediaQuery*(name: string): Query =
Query(
kind: media,
kind: QueryKind.media,
filters: @["twimg", "native_video"],
fromUser: @[name],
sep: "OR"
@ -64,7 +64,7 @@ proc genQueryParam*(query: Query; maxId=""): string =
else:
param &= ")"
if query.fromUser.len > 0 and query.kind in {posts, media}:
if query.fromUser.len > 0 and query.kind in {posts, QueryKind.media}:
param &= " (filter:self_threads OR -filter:replies)"
if "nativeretweets" notin query.excludes:
@ -104,7 +104,9 @@ proc genQueryUrl*(query: Query): string =
if query.view.len > 0:
params.add "view=" & encodeUrl(query.view)
if query.kind in {tweets, users}:
# media doubles as the profile media tab, where f isn't part of the URL scheme
if query.kind in {tweets, users, lists, top} or
(query.kind == QueryKind.media and query.fromUser.len == 0):
params.add &"f={query.kind}"
if query.text.len > 0:
params.add "q=" & encodeUrl(query.text)

View file

@ -1,29 +1,79 @@
# SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, strutils, strformat, options
import asyncdispatch, strutils, strformat, json
import jester, karax/vdom
import ".."/[types, api]
import ".."/[types, api, formatters]
import ../views/[embed, tweet, general]
include "../views/oembed.nimf"
import router_utils
export api, embed, vdom, tweet, general, router_utils
proc parseTweetPath(path: string): tuple[username, id: string] =
let parts = path.split('/')
if parts.len >= 3 and parts[1] in ["status", "statuses"]:
let tweetId = parts[2].split('?')[0].split('#')[0]
if tweetId.len > 0 and tweetId.allCharsInSet(Digits):
return (parts[0], tweetId)
return ("", "")
proc parseTweetUrl*(url: string; cfg: Config): tuple[username, id: string] =
var path = url
if path.startsWith("https://"):
path = path[8..^1]
elif path.startsWith("http://"):
path = path[7..^1]
const twitterPrefixes = ["twitter.com/", "x.com/", "mobile.twitter.com/",
"www.twitter.com/", "www.x.com/"]
for prefix in twitterPrefixes:
if path.startsWith(prefix):
return parseTweetPath(path[prefix.len..^1])
let nitterPrefix = cfg.hostname & "/"
if path.startsWith(nitterPrefix):
return parseTweetPath(path[nitterPrefix.len..^1])
# Fall back: strip any hostname and try to parse as a tweet path.
# Handles requests where the URL's host differs from cfg.hostname
# (e.g. localhost in dev/CI, or a reverse proxy with a different domain).
let slashPos = path.find('/')
if slashPos > 0:
let afterHost = path[slashPos + 1..^1]
let parsed = parseTweetPath(afterHost)
if parsed.username.len > 0:
return parsed
return ("", "")
proc createEmbedRouter*(cfg: Config) =
router embed:
get "/i/videos/tweet/@id":
let tweet = await getGraphTweetResult(@"id")
if tweet == nil or not tweet.hasVideos:
resp Http404
let
id = @"id"
tweet = await getTweetByRestId(id)
prefs = requestPrefs()
if tweet == nil:
resp renderErrorEmbed("Tweet not found", prefs, cfg, request, tweetId=id)
if not tweet.hasVideos:
resp renderErrorEmbed("No video in tweet", prefs, cfg, request,
tweetId=id, username=tweet.user.username)
resp renderVideoEmbed(tweet, cfg, request)
get "/@user/status/@id/embed":
let
tweet = await getGraphTweetResult(@"id")
id = @"id"
user = @"user"
tweet = await getTweetByRestId(id)
prefs = requestPrefs()
path = getPath()
if tweet == nil:
resp Http404
resp renderErrorEmbed("Tweet not found", prefs, cfg, request,
tweetId=id, username=user)
resp renderTweetEmbed(tweet, path, prefs, cfg, request)
@ -34,3 +84,57 @@ proc createEmbedRouter*(cfg: Config) =
redirect(&"/i/status/{id}/embed")
else:
resp Http404
get "/api/oembed":
responseHeaders().get.add(("Access-Control-Allow-Origin", "*"))
let
url = @"url"
format = @"format"
if format.len > 0 and format != "json":
resp Http501, "Only JSON format is supported"
if url.len == 0:
resp Http400, "Missing url parameter"
let (username, tweetId) = parseTweetUrl(url, cfg)
if username.len == 0 or tweetId.len == 0:
resp Http400, "Invalid tweet URL"
let tweet = await getTweetByRestId(tweetId)
if tweet == nil:
resp Http404
let
maxwidthParam = @"maxwidth"
maxwidth = if maxwidthParam.len > 0:
try: clamp(parseInt(maxwidthParam), 220, 550)
except ValueError: 550
else: 550
embedUrl = getUrlPrefix(cfg) & "/" & tweet.user.username & "/status/" & tweetId & "/embed"
authorUrl = getUrlPrefix(cfg) & "/" & tweet.user.username
title = stripHtml(tweet.text)
var response = %*{
"version": "1.0",
"type": "rich",
"provider_name": cfg.title,
"provider_url": getUrlPrefix(cfg),
"title": title,
"author_name": tweet.user.fullname,
"author_url": authorUrl,
"url": embedUrl,
"width": maxwidth,
"height": newJNull(),
"cache_age": "3153600000",
"html": renderOembedIframe(embedUrl, maxwidth)
}
if tweet.media.len > 0:
let thumbUrl = getUrlPrefix(cfg) & getPicUrl(tweet.media[0].getThumb)
response["thumbnail_url"] = %thumbUrl
response["thumbnail_width"] = %maxwidth
response["thumbnail_height"] = %maxwidth
respJson response

View file

@ -4,7 +4,7 @@ from jester import Request, cookies
import ../views/general
import ".."/[utils, prefs, types]
export utils, prefs, types, uri
export utils, prefs, types, uri, json
template savePref*(pref, value: string; req: Request; expire=false) =
if not expire or pref in cookies(req):

View file

@ -68,7 +68,7 @@ proc createRssRouter*(cfg: Config) =
let
prefs = requestPrefs()
query = initQuery(params(request))
if query.kind != tweets:
if query.kind notin {QueryKind.tweets, QueryKind.top, QueryKind.media}:
resp Http400, showError("Only Tweet searches are allowed for RSS feeds.", cfg)
let

View file

@ -20,9 +20,21 @@ proc createSearchRouter*(cfg: Config) =
let
prefs = requestPrefs()
query = initQuery(params(request))
title = "Search" & (if q.len > 0: " (" & q & ")" else: "")
var query = initQuery(params(request))
# x.com URL compat: f=user and f=list map to our kind names
# (f=live already falls back to tweets/Latest; f=media matches natively)
if @"f" == "user":
query.kind = users
elif @"f" == "list":
query.kind = lists
# media searches support view modes, defaulting like /user/media
if query.kind == QueryKind.media and
query.view notin ["timeline", "grid", "gallery"]:
query.view = prefs.mediaView.toLowerAscii
case query.kind
of users:
if "," in q:
@ -33,12 +45,16 @@ proc createSearchRouter*(cfg: Config) =
except InternalError:
users = Result[User](beginning: true, query: query)
resp renderMain(renderUserSearch(users, prefs), request, cfg, prefs, title)
of tweets:
of tweets, top, QueryKind.media:
let
tweets = await getGraphTweetSearch(query, getCursor())
rss = if cfg.enableRSSSearch: "/search/rss?" & genQueryUrl(query) else: ""
resp renderMain(renderTweetSearch(tweets, prefs, getPath()),
request, cfg, prefs, title, rss=rss)
of lists:
let listResults = await getGraphListSearch(query, getCursor())
resp renderMain(renderListSearch(listResults, prefs, getPath()),
request, cfg, prefs, title)
else:
resp Http404, showError("Invalid search", cfg)

View file

@ -21,16 +21,18 @@ proc createStatusRouter*(cfg: Config) =
if id.len > 19 or id.any(c => not c.isDigit):
resp Http404, showError("Invalid tweet ID", cfg)
let prefs = requestPrefs()
let
prefs = requestPrefs()
sort = parseEnum[RankingMode](@"sort".toLowerAscii.capitalizeAscii, Relevance)
# used for the infinite scroll feature
if @"scroll".len > 0:
let replies = await getReplies(id, getCursor())
let replies = await getReplies(id, getCursor(), sort)
if replies.content.len == 0:
resp Http204
resp $renderReplies(replies, prefs, getPath())
resp $renderReplies(replies, prefs, getPath(), sort=sort)
let conv = await getTweet(id, getCursor())
let conv = await getTweet(id, getCursor(), sort)
if conv == nil or conv.tweet == nil or conv.tweet.id == 0:
var error = "Tweet not found"
@ -64,9 +66,13 @@ proc createStatusRouter*(cfg: Config) =
elif card.video.isSome():
images = @[card.video.get().thumb]
let html = renderConversation(conv, prefs, getPath() & "#m")
let
tweetUrl = getUrlPrefix(cfg) & "/" & conv.tweet.user.username & "/status/" & $conv.tweet.id
oembedUrl = getUrlPrefix(cfg) & "/api/oembed?url=" & encodeUrl(tweetUrl)
let html = renderConversation(conv, prefs, getPath() & "#m", sort)
resp renderMain(html, request, cfg, prefs, title, desc, ogTitle,
images=images, video=video)
images=images, video=video, oembed=oembedUrl)
get "/@name/status/@id/history/?":
cond '.' notin @"name"

View file

@ -53,7 +53,7 @@ proc fetchProfile*(after: string; query: Query; skipRail=false): Future[Profile]
let
rail =
skipIf(skipRail or query.kind == media, @[]):
skipIf(skipRail or query.kind == QueryKind.media, @[]):
getCachedPhotoRail(userId)
user = getCachedUser(name)

View file

@ -107,6 +107,80 @@
grid-column-gap: 10px;
}
.list-result {
display: flex;
align-items: flex-start;
.list-result-banner {
flex-shrink: 0;
width: 56px;
height: 56px;
margin-right: 10px;
border-radius: 8px;
overflow: hidden;
background-color: var(--darker_grey);
// stay above the tweet-link overlay's hover background
z-index: 1;
img {
width: 100%;
height: 100%;
object-fit: cover;
}
}
.list-result-body {
min-width: 0;
pointer-events: none;
z-index: 1;
a {
pointer-events: all;
}
}
.list-result-title {
align-items: baseline;
}
.list-members {
flex-shrink: 0;
margin-left: 0.3em;
color: var(--fg_faded);
}
.list-result-context {
display: flex;
align-items: center;
flex-wrap: wrap;
margin-top: 2px;
color: var(--fg_faded);
a {
color: var(--fg_dark);
}
a.fullname {
color: var(--fg_color);
}
.list-facepile {
width: 20px;
height: 20px;
border-radius: 50%;
margin-right: 4px;
}
}
.list-result-description {
margin-top: 2px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
pointer-events: all;
}
}
.profile-tabs {
@include search-resize(820px, 5);
@include search-resize(715px, 4);

View file

@ -4,6 +4,26 @@
@include panel(100%, 600px);
}
.timeline-container.media-only {
max-width: none;
width: 100%;
padding: 0 10px;
box-sizing: border-box;
> .tab,
> .timeline-header {
max-width: 900px;
margin-left: auto;
margin-right: auto;
}
}
@media (max-width: 700px) {
.timeline-container.media-only {
padding: 0;
}
}
.timeline > div:not(:first-child) {
border-top: 1px solid var(--border_grey);
}

View file

@ -87,7 +87,6 @@
margin-top: 6px;
margin-bottom: 0px;
color: var(--grey);
pointer-events: all;
}
.tweet-avatar {
@ -118,32 +117,6 @@
}
}
.tweet-embed {
display: flex;
flex-direction: column;
justify-content: center;
height: 100%;
background-color: var(--bg_panel);
.tweet-content {
font-size: 18px;
}
.tweet-body {
display: flex;
flex-direction: column;
max-height: calc(100vh - 0.75em * 2);
}
.card-image img {
height: auto;
}
.avatar {
position: absolute;
}
}
.attribution {
display: flex;
pointer-events: all;

View file

@ -1,17 +1,159 @@
@import "_variables";
@import "_mixins";
.embed-video {
.gallery-video {
width: 100%;
height: 100%;
position: absolute;
background-color: black;
top: 0%;
left: 0%;
}
// Embed page: transparent background, no scrollbars
html:has(body > .embed-wrapper),
html:has(body > .embed-video) {
background: transparent;
overflow: hidden;
.gallery-video > .attachment {
max-height: unset;
body {
background: transparent;
overflow: hidden;
}
}
// Tweet embed wrapper
.embed-wrapper {
box-sizing: border-box;
border: 1px solid var(--border_grey);
border-radius: 12px;
overflow: hidden;
.embed-footer {
display: block;
padding: 12px 16px;
border-top: 1px solid var(--border_grey);
background: var(--bg_panel);
color: var(--accent);
font-size: 14px;
font-weight: 500;
text-align: center;
text-decoration: none;
transition: background-color 0.15s;
&:hover {
background: var(--bg_hover);
}
}
}
// Tweet embed content
.tweet-embed {
position: relative;
background-color: var(--bg_panel);
transition: background-color 0.15s;
&:hover {
background-color: var(--bg_hover);
}
.timeline-item {
pointer-events: none;
background-color: transparent;
}
.tweet-link:hover {
background-color: transparent;
}
.tweet-content {
font-size: 18px;
}
.avatar:not(.mini) {
position: absolute;
}
// Cap media height in embeds
.still-image img,
.quote-media-container img,
.quote-media-container video {
max-height: 600px;
}
&.error-embed {
display: flex;
align-items: center;
justify-content: center;
min-height: 120px;
padding: 20px;
.error-panel {
margin: 0;
}
}
}
// Video-only embed
.embed-video {
position: relative;
min-height: 300px;
background-color: black;
border: 1px solid var(--border_grey);
.attachments {
margin: 0;
border-radius: 0;
max-height: 560px;
background-color: unset;
}
.card {
margin: 0;
}
.gallery-video {
width: 100%;
}
.gallery-video>.attachment {
max-height: 560px;
width: 100%;
}
video {
width: 100%;
height: auto;
max-height: 560px;
object-fit: contain;
}
.video-download {
display: none;
}
.video-overlay-link {
position: absolute;
top: 12px;
right: 12px;
padding: 6px 12px;
background: rgba(30, 30, 30, 0.75);
backdrop-filter: blur(4px);
color: #fff;
font-size: 13px;
font-weight: 700;
text-decoration: none;
border-radius: 9999px;
border: 1px solid transparent;
transition:
background 0.15s,
opacity 0.15s;
z-index: 10;
&:hover {
background: rgba(60, 60, 60, 0.9);
}
}
// Hide button while playing, show on hover or when paused
&.video-playing .video-overlay-link {
opacity: 0;
pointer-events: none;
}
&.video-playing:hover .video-overlay-link {
opacity: 1;
pointer-events: auto;
}
}

Some files were not shown because too many files have changed in this diff Show more