mirror of
https://github.com/zedeus/nitter
synced 2026-09-06 07:09:31 +00:00
Merge branch 'zedeus:master' into master
This commit is contained in:
commit
162a15f7a8
40 changed files with 1552 additions and 178 deletions
4
.github/workflows/run-tests.yml
vendored
4
.github/workflows/run-tests.yml
vendored
|
|
@ -62,6 +62,7 @@ jobs:
|
||||||
needs: [build-test]
|
needs: [build-test]
|
||||||
name: Integration test
|
name: Integration test
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
|
timeout-minutes: 30
|
||||||
|
|
||||||
services:
|
services:
|
||||||
redis:
|
redis:
|
||||||
|
|
@ -136,6 +137,7 @@ jobs:
|
||||||
cp nitter.example.conf nitter.conf
|
cp nitter.example.conf nitter.conf
|
||||||
sed -i 's/enableDebug = false/enableDebug = true/g' 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/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/rendermd.nim
|
||||||
nim r tools/gencss.nim
|
nim r tools/gencss.nim
|
||||||
|
|
@ -147,4 +149,4 @@ jobs:
|
||||||
run: |
|
run: |
|
||||||
./nitter &
|
./nitter &
|
||||||
cd tests
|
cd tests
|
||||||
poetry run pytest -n3 --reruns=5 --rs .
|
poetry run pytest -n3 --rs .
|
||||||
|
|
|
||||||
34
public/js/embedResize.js
Normal file
34
public/js/embedResize.js
Normal 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
221
public/js/widgets.js
Normal 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();
|
||||||
|
}
|
||||||
|
})();
|
||||||
57
src/api.nim
57
src/api.nim
|
|
@ -35,8 +35,8 @@ proc userTweetsUrl(id: string; cursor: string): ApiReq =
|
||||||
proc userTweetsAndRepliesUrl(id: string; cursor: string): ApiReq =
|
proc userTweetsAndRepliesUrl(id: string; cursor: string): ApiReq =
|
||||||
return apiReq(graphUserTweetsAndRepliesV2, restIdVars % [id, cursor, "20"], userTweetsFieldToggles, skipTid=true)
|
return apiReq(graphUserTweetsAndRepliesV2, restIdVars % [id, cursor, "20"], userTweetsFieldToggles, skipTid=true)
|
||||||
|
|
||||||
proc tweetDetailUrl(id: string; cursor: string): ApiReq =
|
proc tweetDetailUrl(id, cursor: string; mode = Relevance): ApiReq =
|
||||||
return apiReq(graphTweet, tweetVars % [id, cursor])
|
return apiReq(graphTweet, tweetVars % [id, cursor, $mode])
|
||||||
# let cookieVars = tweetDetailVars % [id, cursor]
|
# let cookieVars = tweetDetailVars % [id, cursor]
|
||||||
# result = ApiReq(
|
# result = ApiReq(
|
||||||
# cookie: apiUrl(graphTweetDetail, cookieVars, tweetDetailFieldToggles),
|
# cookie: apiUrl(graphTweetDetail, cookieVars, tweetDetailFieldToggles),
|
||||||
|
|
@ -230,21 +230,28 @@ proc getGraphTweetResult*(id: string): Future[Tweet] {.async.} =
|
||||||
js = await fetch(url)
|
js = await fetch(url)
|
||||||
result = parseGraphTweetResult(js)
|
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
|
if id.len == 0: return
|
||||||
let
|
let
|
||||||
cursor = cursorParam(after)
|
cursor = cursorParam(after)
|
||||||
js = await fetch(tweetDetailUrl(id, cursor))
|
js = await fetch(tweetDetailUrl(id, cursor, mode))
|
||||||
result = parseGraphConversation(js, id)
|
result = parseGraphConversation(js, id)
|
||||||
|
|
||||||
proc getReplies*(id, after: string): Future[Result[Chain]] {.async.} =
|
proc getReplies*(id, after: string; mode = Relevance): Future[Result[Chain]] {.async.} =
|
||||||
result = (await getGraphTweet(id, after)).replies
|
result = (await getGraphTweet(id, after, mode)).replies
|
||||||
result.beginning = after.len == 0
|
result.beginning = after.len == 0
|
||||||
|
|
||||||
proc getTweet*(id: string; after=""): Future[Conversation] {.async.} =
|
proc getTweet*(id: string; after=""; mode = Relevance): Future[Conversation] {.async.} =
|
||||||
result = await getGraphTweet(id)
|
result = await getGraphTweet(id, mode=mode)
|
||||||
if after.len > 0:
|
if after.len > 0:
|
||||||
result.replies = await getReplies(id, after)
|
result.replies = await getReplies(id, after, mode)
|
||||||
|
|
||||||
proc getGraphEditHistory*(id: string): Future[EditHistory] {.async.} =
|
proc getGraphEditHistory*(id: string): Future[EditHistory] {.async.} =
|
||||||
if id.len == 0: return
|
if id.len == 0: return
|
||||||
|
|
@ -263,12 +270,19 @@ proc getGraphTweetSearch*(query: Query; after=""): Future[Timeline] {.async.} =
|
||||||
if q.len == 0 or q == emptyQuery:
|
if q.len == 0 or q == emptyQuery:
|
||||||
return Timeline(query: query, beginning: true)
|
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
|
var
|
||||||
variables = %*{
|
variables = %*{
|
||||||
"rawQuery": q,
|
"rawQuery": q,
|
||||||
"count": 20,
|
"count": 20,
|
||||||
"querySource": "typed_query",
|
"querySource": "typed_query",
|
||||||
"product": "Latest",
|
"product": product,
|
||||||
"withGrokTranslatedBio":true,
|
"withGrokTranslatedBio":true,
|
||||||
"withQuickPromoteEligibilityTweetFields":false
|
"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
|
# when no more items are available the API just returns the last page in
|
||||||
# full. this detects that and clears the page instead.
|
# full. this detects that and clears the page instead.
|
||||||
if after.len > 0 and result.bottom.len > 0 and maxId.len == 0 and
|
let prefix = min(64, min(after.len, result.bottom.len))
|
||||||
after[0..<64] == result.bottom[0..<64]:
|
if prefix > 0 and maxId.len == 0 and
|
||||||
|
after[0..<prefix] == result.bottom[0..<prefix]:
|
||||||
result.content.setLen(0)
|
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:
|
if query.text.len == 0:
|
||||||
return Result[User](query: query, beginning: true)
|
return Result[T](query: query, beginning: true)
|
||||||
|
|
||||||
var
|
var
|
||||||
variables = %*{
|
variables = %*{
|
||||||
"rawQuery": query.text,
|
"rawQuery": query.text,
|
||||||
"count": 20,
|
"count": 20,
|
||||||
"querySource": "typed_query",
|
"querySource": "typed_query",
|
||||||
"product": "People",
|
"product": product,
|
||||||
"withGrokTranslatedBio":true,
|
"withGrokTranslatedBio":true,
|
||||||
"withQuickPromoteEligibilityTweetFields":false
|
"withQuickPromoteEligibilityTweetFields":false
|
||||||
}
|
}
|
||||||
if after.len > 0:
|
if after.len > 0:
|
||||||
variables["cursor"] = % after
|
variables["cursor"] = % after
|
||||||
result.beginning = false
|
|
||||||
|
|
||||||
let
|
let
|
||||||
url = apiReq(graphSearchTimeline, $variables)
|
url = apiReq(graphSearchTimeline, $variables)
|
||||||
js = await fetch(url)
|
js = await fetch(url)
|
||||||
result = parseGraphSearch[User](js, after)
|
result = parseGraphSearch[T](js, after)
|
||||||
result.query = query
|
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.} =
|
proc getPhotoRail*(id: string): Future[PhotoRail] {.async.} =
|
||||||
if id.len == 0: return
|
if id.len == 0: return
|
||||||
let js = await fetch(mediaUrl(id, "", 30))
|
let js = await fetch(mediaUrl(id, "", 30))
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ const
|
||||||
graphCommunityModerators* = "GBMT3GOWy5dYsYC4XJfvow/moderatorsSliceTimeline_Query"
|
graphCommunityModerators* = "GBMT3GOWy5dYsYC4XJfvow/moderatorsSliceTimeline_Query"
|
||||||
graphCommunityHashtags* = "40DyrMxfCknGuZwE-keW_Q/CommunityHashtagsTimeline"
|
graphCommunityHashtags* = "40DyrMxfCknGuZwE-keW_Q/CommunityHashtagsTimeline"
|
||||||
|
|
||||||
graphTweetResultByRestId* = "qtXMy1p5Y62uCskc_NUPJw/TweetResultByRestId"
|
graphTweetResultByRestId* = "4hhGRbehkcUVTKf8n0f0xw/TweetResultByRestId"
|
||||||
graphTweetResultsByRestIds* = "Sc9EUQTZNEH-wzegn-nHvQ/TweetResultsByRestIds"
|
graphTweetResultsByRestIds* = "Sc9EUQTZNEH-wzegn-nHvQ/TweetResultsByRestIds"
|
||||||
|
|
||||||
graphBroadcast* = "FJLCzpXCLPM1jUZqmM7oEA/BroadcastQuery"
|
graphBroadcast* = "FJLCzpXCLPM1jUZqmM7oEA/BroadcastQuery"
|
||||||
|
|
@ -92,6 +92,7 @@ const
|
||||||
tweetVars* = """{
|
tweetVars* = """{
|
||||||
"postId": "$1",
|
"postId": "$1",
|
||||||
$2
|
$2
|
||||||
|
"ranking_mode": "$3",
|
||||||
"includeHasBirdwatchNotes": false,
|
"includeHasBirdwatchNotes": false,
|
||||||
"includePromotedContent": false,
|
"includePromotedContent": false,
|
||||||
"withBirdwatchNotes": true,
|
"withBirdwatchNotes": true,
|
||||||
|
|
@ -165,6 +166,14 @@ const
|
||||||
|
|
||||||
articleFieldToggles* = """{"withArticleRichContentState":true,"withArticlePlainText":false,"withArticleSummaryText":true,"withArticleVoiceOver":true}"""
|
articleFieldToggles* = """{"withArticleRichContentState":true,"withArticlePlainText":false,"withArticleSummaryText":true,"withArticleVoiceOver":true}"""
|
||||||
|
|
||||||
|
tweetByRestIdVars* = """{
|
||||||
|
"tweetId": "$1",
|
||||||
|
"includePromotedContent": false,
|
||||||
|
"withBirdwatchNotes": false,
|
||||||
|
"withVoice": false,
|
||||||
|
"withCommunity": false
|
||||||
|
}""".replace(" ", "").replace("\n", "")
|
||||||
|
|
||||||
communityTweetsVars* = """{
|
communityTweetsVars* = """{
|
||||||
"communityId": "$1", $2
|
"communityId": "$1", $2
|
||||||
"count": 20,
|
"count": 20,
|
||||||
|
|
|
||||||
|
|
@ -81,8 +81,8 @@ routes:
|
||||||
if request.path.len == 0 or request.path[0] != '/':
|
if request.path.len == 0 or request.path[0] != '/':
|
||||||
halt Http400
|
halt Http400
|
||||||
|
|
||||||
# skip all file URLs
|
# skip all file URLs (except Twitter widget compatibility)
|
||||||
cond "." notin request.path
|
cond "." notin request.path or request.path == "/embed/Tweet.html"
|
||||||
applyUrlPrefs()
|
applyUrlPrefs()
|
||||||
|
|
||||||
get "/":
|
get "/":
|
||||||
|
|
|
||||||
125
src/parser.nim
125
src/parser.nim
|
|
@ -206,6 +206,20 @@ proc parseGraphCommunity*(js: JsonNode): Community =
|
||||||
if tag.len > 0:
|
if tag.len > 0:
|
||||||
result.hashtags.add tag
|
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 =
|
proc parseGraphList*(js: JsonNode): List =
|
||||||
if js.isNull: return
|
if js.isNull: return
|
||||||
|
|
||||||
|
|
@ -215,15 +229,17 @@ proc parseGraphList*(js: JsonNode): List =
|
||||||
if list.isNull:
|
if list.isNull:
|
||||||
return
|
return
|
||||||
|
|
||||||
result = List(
|
result = parseListObject(list, parseGraphUser(list))
|
||||||
id: list{"id_str"}.getStr,
|
|
||||||
name: list{"name"}.getStr,
|
proc parseGraphSearchList(js: JsonNode): ListSearchResult =
|
||||||
username: list{"user_results", "result", "legacy", "screen_name"}.getStr,
|
let owner = parseGraphUser(js)
|
||||||
userId: list{"user_results", "result", "rest_id"}.getStr,
|
result = ListSearchResult(
|
||||||
description: list{"description"}.getStr,
|
list: parseListObject(js, owner),
|
||||||
members: list{"member_count"}.getInt,
|
owner: owner,
|
||||||
banner: list{"custom_banner_media", "media_info", "original_img_url"}.getImageStr
|
followersContext: js{"followers_context"}.getStr
|
||||||
)
|
)
|
||||||
|
for url in js{"facepile_urls"}:
|
||||||
|
result.facepiles.add url.getStr
|
||||||
|
|
||||||
proc parsePoll(js: JsonNode): Poll =
|
proc parsePoll(js: JsonNode): Poll =
|
||||||
let vals = js{"binding_values"}
|
let vals = js{"binding_values"}
|
||||||
|
|
@ -694,10 +710,26 @@ proc parseGraphTweet*(js: JsonNode): Tweet =
|
||||||
with birdwatch, js{"birdwatch_pivot"}:
|
with birdwatch, js{"birdwatch_pivot"}:
|
||||||
result.note = parseCommunityNote(birdwatch)
|
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] =
|
proc parseGraphThread(js: JsonNode): tuple[thread: Chain; self: bool] =
|
||||||
|
var checkedSection = false
|
||||||
for t in ? js{"content", "items"}:
|
for t in ? js{"content", "items"}:
|
||||||
let entryId = t.getEntryId
|
let entryId = t.getEntryId
|
||||||
if "tweet-" in entryId and "promoted" notin entryId:
|
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")
|
let tweet = t.getTweetResult("item")
|
||||||
if tweet.notNull:
|
if tweet.notNull:
|
||||||
result.thread.content.add parseGraphTweet(tweet)
|
result.thread.content.add parseGraphTweet(tweet)
|
||||||
|
|
@ -719,6 +751,10 @@ proc parseGraphTweetResult*(js: JsonNode): Tweet =
|
||||||
with tweet, js{"data", "tweet_result", "result"}:
|
with tweet, js{"data", "tweet_result", "result"}:
|
||||||
result = parseGraphTweet(tweet)
|
result = parseGraphTweet(tweet)
|
||||||
|
|
||||||
|
proc parseTweetByRestId*(js: JsonNode): Tweet =
|
||||||
|
with tweet, js{"data", "tweetResult", "result"}:
|
||||||
|
result = parseGraphTweet(tweet)
|
||||||
|
|
||||||
proc parseGraphTweetResults*(js: JsonNode): seq[Tweet] =
|
proc parseGraphTweetResults*(js: JsonNode): seq[Tweet] =
|
||||||
let results = js{"data", "tweetResult"}
|
let results = js{"data", "tweetResult"}
|
||||||
if results.kind != JArray: return
|
if results.kind != JArray: return
|
||||||
|
|
@ -758,7 +794,8 @@ proc parseGraphConversation*(js: JsonNode; tweetId: string): Conversation =
|
||||||
result.before.content.add tweet
|
result.before.content.add tweet
|
||||||
elif not entryId.endsWith(tweetId):
|
elif not entryId.endsWith(tweetId):
|
||||||
result.before.content.add Tweet(id: entryId.getId)
|
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)
|
let (thread, self) = parseGraphThread(e)
|
||||||
if self:
|
if self:
|
||||||
result.after = thread
|
result.after = thread
|
||||||
|
|
@ -808,20 +845,31 @@ proc parseGraphEditHistory*(js: JsonNode; tweetId: string): EditHistory =
|
||||||
if tweetResult.notNull:
|
if tweetResult.notNull:
|
||||||
result.history.add parseGraphTweet(tweetResult)
|
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] =
|
proc extractTweetsFromEntry*(e: JsonNode): seq[Tweet] =
|
||||||
with tweetResult, getTweetResult(e):
|
with tweetResult, getTweetResult(e):
|
||||||
var tweet = parseGraphTweet(tweetResult)
|
let tweet = parseGraphTweet(tweetResult)
|
||||||
if not tweet.available:
|
if not tweet.available:
|
||||||
tweet.id = e.getEntryId.getId
|
tweet.id = e.getEntryId.getId
|
||||||
result.add tweet
|
result.add tweet
|
||||||
return
|
return
|
||||||
|
|
||||||
for item in e{"content", "items"}:
|
for tweet in extractTweetsFromModuleItems(e{"content", "items"}):
|
||||||
with tweetResult, item.getTweetResult("item"):
|
result.add tweet
|
||||||
var tweet = parseGraphTweet(tweetResult)
|
|
||||||
if not tweet.available:
|
|
||||||
tweet.id = item.getEntryId.getId
|
|
||||||
result.add tweet
|
|
||||||
|
|
||||||
proc parseGraphTimeline*(js: JsonNode; after=""): Profile =
|
proc parseGraphTimeline*(js: JsonNode; after=""): Profile =
|
||||||
result = Profile(tweets: Timeline(beginning: after.len == 0))
|
result = Profile(tweets: Timeline(beginning: after.len == 0))
|
||||||
|
|
@ -836,12 +884,8 @@ proc parseGraphTimeline*(js: JsonNode; after=""): Profile =
|
||||||
|
|
||||||
for i in instructions:
|
for i in instructions:
|
||||||
if i{"moduleItems"}.notNull:
|
if i{"moduleItems"}.notNull:
|
||||||
for item in i{"moduleItems"}:
|
for tweet in extractTweetsFromModuleItems(i{"moduleItems"}):
|
||||||
with tweetResult, item.getTweetResult("item"):
|
result.tweets.content.add tweet
|
||||||
let tweet = parseGraphTweet(tweetResult)
|
|
||||||
if not tweet.available:
|
|
||||||
tweet.id = item.getEntryId.getId
|
|
||||||
result.tweets.content.add tweet
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if i{"entries"}.notNull:
|
if i{"entries"}.notNull:
|
||||||
|
|
@ -876,18 +920,13 @@ proc parseGraphPhotoRail*(js: JsonNode): PhotoRail =
|
||||||
|
|
||||||
for i in instructions:
|
for i in instructions:
|
||||||
if i{"moduleItems"}.notNull:
|
if i{"moduleItems"}.notNull:
|
||||||
for item in i{"moduleItems"}:
|
for t in extractTweetsFromModuleItems(i{"moduleItems"}):
|
||||||
with tweetResult, item.getTweetResult("item"):
|
let photo = extractGalleryPhoto(t)
|
||||||
let t = parseGraphTweet(tweetResult)
|
if photo.url.len > 0:
|
||||||
if not t.available:
|
result.add photo
|
||||||
t.id = item.getEntryId.getId
|
|
||||||
|
|
||||||
let photo = extractGalleryPhoto(t)
|
if result.len == 16:
|
||||||
if photo.url.len > 0:
|
return
|
||||||
result.add photo
|
|
||||||
|
|
||||||
if result.len == 16:
|
|
||||||
return
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if i.getTypeName != "TimelineAddEntries":
|
if i.getTypeName != "TimelineAddEntries":
|
||||||
|
|
@ -904,7 +943,7 @@ proc parseGraphPhotoRail*(js: JsonNode): PhotoRail =
|
||||||
if result.len == 16:
|
if result.len == 16:
|
||||||
return
|
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)
|
result = Result[T](beginning: after.len == 0)
|
||||||
|
|
||||||
let instructions = select(
|
let instructions = select(
|
||||||
|
|
@ -920,19 +959,27 @@ proc parseGraphSearch*[T: User | Tweets](js: JsonNode; after=""): Result[T] =
|
||||||
for e in instruction{"entries"}:
|
for e in instruction{"entries"}:
|
||||||
let entryId = e.getEntryId
|
let entryId = e.getEntryId
|
||||||
when T is Tweets:
|
when T is Tweets:
|
||||||
if entryId.startsWith("tweet"):
|
if entryId.startsWith("tweet") or entryId.startsWith("search-grid"):
|
||||||
with tweetRes, getTweetResult(e):
|
for tweet in extractTweetsFromEntry(e):
|
||||||
let tweet = parseGraphTweet(tweetRes)
|
|
||||||
if not tweet.available:
|
|
||||||
tweet.id = entryId.getId
|
|
||||||
result.content.add tweet
|
result.content.add tweet
|
||||||
elif T is User:
|
elif T is User:
|
||||||
if entryId.startsWith("user"):
|
if entryId.startsWith("user"):
|
||||||
with userRes, e{"content", "itemContent"}:
|
with userRes, e{"content", "itemContent"}:
|
||||||
result.content.add parseGraphUser(userRes)
|
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"):
|
if entryId.startsWith("cursor-bottom"):
|
||||||
result.bottom = e{"content", "value"}.getStr
|
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":
|
elif typ == "TimelineReplaceEntry":
|
||||||
if instruction{"entry_id_to_replace"}.getStr.startsWith("cursor-bottom"):
|
if instruction{"entry_id_to_replace"}.getStr.startsWith("cursor-bottom"):
|
||||||
result.bottom = instruction{"entry", "content", "value"}.getStr
|
result.bottom = instruction{"entry", "content", "value"}.getStr
|
||||||
|
|
|
||||||
|
|
@ -98,9 +98,9 @@ proc getTimeFromMsStr*(js: JsonNode): DateTime =
|
||||||
|
|
||||||
proc getId*(id: string): int64 {.inline.} =
|
proc getId*(id: string): int64 {.inline.} =
|
||||||
let start = id.rfind("-")
|
let start = id.rfind("-")
|
||||||
if start < 0:
|
try:
|
||||||
return parseBiggestInt(id)
|
parseBiggestInt(if start < 0: id else: id[start + 1 ..< id.len])
|
||||||
return parseBiggestInt(id[start + 1 ..< id.len])
|
except ValueError: 0'i64
|
||||||
|
|
||||||
proc getId*(js: JsonNode): int64 {.inline.} =
|
proc getId*(js: JsonNode): int64 {.inline.} =
|
||||||
case js.kind
|
case js.kind
|
||||||
|
|
@ -233,9 +233,9 @@ proc replacedWith(runes: seq[Rune]; repls: openArray[ReplaceSlice];
|
||||||
symbol = $runes[rep.slice.a]
|
symbol = $runes[rep.slice.a]
|
||||||
result.add a(symbol & name, href = "/search?f=tweets&q=%23" & name)
|
result.add a(symbol & name, href = "/search?f=tweets&q=%23" & name)
|
||||||
of rkMention:
|
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:
|
of rkUrl:
|
||||||
result.add a(rep.display, href = rep.url)
|
result.add a(escape(rep.display), href = rep.url)
|
||||||
of rkRemove:
|
of rkRemove:
|
||||||
discard
|
discard
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,9 @@ genPrefs:
|
||||||
hideReplies(checkbox, false):
|
hideReplies(checkbox, false):
|
||||||
"Hide tweet replies"
|
"Hide tweet replies"
|
||||||
|
|
||||||
|
hideRelated(checkbox, true):
|
||||||
|
"Hide related tweets under replies"
|
||||||
|
|
||||||
hideCommunityNotes(checkbox, false):
|
hideCommunityNotes(checkbox, false):
|
||||||
"Hide community notes"
|
"Hide community notes"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ proc initQuery*(pms: Table[string, string]; name=""): Query =
|
||||||
|
|
||||||
proc getMediaQuery*(name: string): Query =
|
proc getMediaQuery*(name: string): Query =
|
||||||
Query(
|
Query(
|
||||||
kind: media,
|
kind: QueryKind.media,
|
||||||
filters: @["twimg", "native_video"],
|
filters: @["twimg", "native_video"],
|
||||||
fromUser: @[name],
|
fromUser: @[name],
|
||||||
sep: "OR"
|
sep: "OR"
|
||||||
|
|
@ -64,7 +64,7 @@ proc genQueryParam*(query: Query; maxId=""): string =
|
||||||
else:
|
else:
|
||||||
param &= ")"
|
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)"
|
param &= " (filter:self_threads OR -filter:replies)"
|
||||||
|
|
||||||
if "nativeretweets" notin query.excludes:
|
if "nativeretweets" notin query.excludes:
|
||||||
|
|
@ -104,7 +104,9 @@ proc genQueryUrl*(query: Query): string =
|
||||||
if query.view.len > 0:
|
if query.view.len > 0:
|
||||||
params.add "view=" & encodeUrl(query.view)
|
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}"
|
params.add &"f={query.kind}"
|
||||||
if query.text.len > 0:
|
if query.text.len > 0:
|
||||||
params.add "q=" & encodeUrl(query.text)
|
params.add "q=" & encodeUrl(query.text)
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,79 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
import asyncdispatch, strutils, strformat, options
|
import asyncdispatch, strutils, strformat, json
|
||||||
import jester, karax/vdom
|
import jester, karax/vdom
|
||||||
import ".."/[types, api]
|
import ".."/[types, api, formatters]
|
||||||
import ../views/[embed, tweet, general]
|
import ../views/[embed, tweet, general]
|
||||||
|
include "../views/oembed.nimf"
|
||||||
import router_utils
|
import router_utils
|
||||||
|
|
||||||
export api, embed, vdom, tweet, general, 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) =
|
proc createEmbedRouter*(cfg: Config) =
|
||||||
router embed:
|
router embed:
|
||||||
get "/i/videos/tweet/@id":
|
get "/i/videos/tweet/@id":
|
||||||
let tweet = await getGraphTweetResult(@"id")
|
let
|
||||||
if tweet == nil or not tweet.hasVideos:
|
id = @"id"
|
||||||
resp Http404
|
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)
|
resp renderVideoEmbed(tweet, cfg, request)
|
||||||
|
|
||||||
get "/@user/status/@id/embed":
|
get "/@user/status/@id/embed":
|
||||||
let
|
let
|
||||||
tweet = await getGraphTweetResult(@"id")
|
id = @"id"
|
||||||
|
user = @"user"
|
||||||
|
tweet = await getTweetByRestId(id)
|
||||||
prefs = requestPrefs()
|
prefs = requestPrefs()
|
||||||
path = getPath()
|
path = getPath()
|
||||||
|
|
||||||
if tweet == nil:
|
if tweet == nil:
|
||||||
resp Http404
|
resp renderErrorEmbed("Tweet not found", prefs, cfg, request,
|
||||||
|
tweetId=id, username=user)
|
||||||
|
|
||||||
resp renderTweetEmbed(tweet, path, prefs, cfg, request)
|
resp renderTweetEmbed(tweet, path, prefs, cfg, request)
|
||||||
|
|
||||||
|
|
@ -34,3 +84,57 @@ proc createEmbedRouter*(cfg: Config) =
|
||||||
redirect(&"/i/status/{id}/embed")
|
redirect(&"/i/status/{id}/embed")
|
||||||
else:
|
else:
|
||||||
resp Http404
|
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
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ from jester import Request, cookies
|
||||||
|
|
||||||
import ../views/general
|
import ../views/general
|
||||||
import ".."/[utils, prefs, types]
|
import ".."/[utils, prefs, types]
|
||||||
export utils, prefs, types, uri
|
export utils, prefs, types, uri, json
|
||||||
|
|
||||||
template savePref*(pref, value: string; req: Request; expire=false) =
|
template savePref*(pref, value: string; req: Request; expire=false) =
|
||||||
if not expire or pref in cookies(req):
|
if not expire or pref in cookies(req):
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ proc createRssRouter*(cfg: Config) =
|
||||||
let
|
let
|
||||||
prefs = requestPrefs()
|
prefs = requestPrefs()
|
||||||
query = initQuery(params(request))
|
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)
|
resp Http400, showError("Only Tweet searches are allowed for RSS feeds.", cfg)
|
||||||
|
|
||||||
let
|
let
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,21 @@ proc createSearchRouter*(cfg: Config) =
|
||||||
|
|
||||||
let
|
let
|
||||||
prefs = requestPrefs()
|
prefs = requestPrefs()
|
||||||
query = initQuery(params(request))
|
|
||||||
title = "Search" & (if q.len > 0: " (" & q & ")" else: "")
|
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
|
case query.kind
|
||||||
of users:
|
of users:
|
||||||
if "," in q:
|
if "," in q:
|
||||||
|
|
@ -33,12 +45,16 @@ proc createSearchRouter*(cfg: Config) =
|
||||||
except InternalError:
|
except InternalError:
|
||||||
users = Result[User](beginning: true, query: query)
|
users = Result[User](beginning: true, query: query)
|
||||||
resp renderMain(renderUserSearch(users, prefs), request, cfg, prefs, title)
|
resp renderMain(renderUserSearch(users, prefs), request, cfg, prefs, title)
|
||||||
of tweets:
|
of tweets, top, QueryKind.media:
|
||||||
let
|
let
|
||||||
tweets = await getGraphTweetSearch(query, getCursor())
|
tweets = await getGraphTweetSearch(query, getCursor())
|
||||||
rss = if cfg.enableRSSSearch: "/search/rss?" & genQueryUrl(query) else: ""
|
rss = if cfg.enableRSSSearch: "/search/rss?" & genQueryUrl(query) else: ""
|
||||||
resp renderMain(renderTweetSearch(tweets, prefs, getPath()),
|
resp renderMain(renderTweetSearch(tweets, prefs, getPath()),
|
||||||
request, cfg, prefs, title, rss=rss)
|
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:
|
else:
|
||||||
resp Http404, showError("Invalid search", cfg)
|
resp Http404, showError("Invalid search", cfg)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,16 +21,18 @@ proc createStatusRouter*(cfg: Config) =
|
||||||
if id.len > 19 or id.any(c => not c.isDigit):
|
if id.len > 19 or id.any(c => not c.isDigit):
|
||||||
resp Http404, showError("Invalid tweet ID", cfg)
|
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
|
# used for the infinite scroll feature
|
||||||
if @"scroll".len > 0:
|
if @"scroll".len > 0:
|
||||||
let replies = await getReplies(id, getCursor())
|
let replies = await getReplies(id, getCursor(), sort)
|
||||||
if replies.content.len == 0:
|
if replies.content.len == 0:
|
||||||
resp Http204
|
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:
|
if conv == nil or conv.tweet == nil or conv.tweet.id == 0:
|
||||||
var error = "Tweet not found"
|
var error = "Tweet not found"
|
||||||
|
|
@ -64,9 +66,13 @@ proc createStatusRouter*(cfg: Config) =
|
||||||
elif card.video.isSome():
|
elif card.video.isSome():
|
||||||
images = @[card.video.get().thumb]
|
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,
|
resp renderMain(html, request, cfg, prefs, title, desc, ogTitle,
|
||||||
images=images, video=video)
|
images=images, video=video, oembed=oembedUrl)
|
||||||
|
|
||||||
get "/@name/status/@id/history/?":
|
get "/@name/status/@id/history/?":
|
||||||
cond '.' notin @"name"
|
cond '.' notin @"name"
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ proc fetchProfile*(after: string; query: Query; skipRail=false): Future[Profile]
|
||||||
|
|
||||||
let
|
let
|
||||||
rail =
|
rail =
|
||||||
skipIf(skipRail or query.kind == media, @[]):
|
skipIf(skipRail or query.kind == QueryKind.media, @[]):
|
||||||
getCachedPhotoRail(userId)
|
getCachedPhotoRail(userId)
|
||||||
|
|
||||||
user = getCachedUser(name)
|
user = getCachedUser(name)
|
||||||
|
|
|
||||||
|
|
@ -107,6 +107,80 @@
|
||||||
grid-column-gap: 10px;
|
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 {
|
.profile-tabs {
|
||||||
@include search-resize(820px, 5);
|
@include search-resize(820px, 5);
|
||||||
@include search-resize(715px, 4);
|
@include search-resize(715px, 4);
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,26 @@
|
||||||
@include panel(100%, 600px);
|
@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) {
|
.timeline > div:not(:first-child) {
|
||||||
border-top: 1px solid var(--border_grey);
|
border-top: 1px solid var(--border_grey);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,6 @@
|
||||||
margin-top: 6px;
|
margin-top: 6px;
|
||||||
margin-bottom: 0px;
|
margin-bottom: 0px;
|
||||||
color: var(--grey);
|
color: var(--grey);
|
||||||
pointer-events: all;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.tweet-avatar {
|
.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 {
|
.attribution {
|
||||||
display: flex;
|
display: flex;
|
||||||
pointer-events: all;
|
pointer-events: all;
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,159 @@
|
||||||
@import "_variables";
|
@import "_variables";
|
||||||
@import "_mixins";
|
@import "_mixins";
|
||||||
|
|
||||||
.embed-video {
|
// Embed page: transparent background, no scrollbars
|
||||||
.gallery-video {
|
html:has(body > .embed-wrapper),
|
||||||
width: 100%;
|
html:has(body > .embed-video) {
|
||||||
height: 100%;
|
background: transparent;
|
||||||
position: absolute;
|
overflow: hidden;
|
||||||
background-color: black;
|
|
||||||
top: 0%;
|
|
||||||
left: 0%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.gallery-video > .attachment {
|
body {
|
||||||
max-height: unset;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,39 @@
|
||||||
margin-bottom: 10px;
|
margin-bottom: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.reply-sort {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 2px 14px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
padding: 8px 12px;
|
||||||
|
background-color: var(--bg_panel);
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.reply-sort-label {
|
||||||
|
color: var(--fg_faded);
|
||||||
|
margin-right: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.reply-sort-option {
|
||||||
|
color: var(--tab);
|
||||||
|
font-weight: bold;
|
||||||
|
text-decoration: none;
|
||||||
|
border-bottom: 0.1rem solid transparent;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
color: var(--fg_color);
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
&.active {
|
||||||
|
color: var(--tab_selected);
|
||||||
|
border-bottom-color: var(--tab_selected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.main-tweet,
|
.main-tweet,
|
||||||
.replies,
|
.replies,
|
||||||
.edit-history > div {
|
.edit-history > div {
|
||||||
|
|
@ -152,3 +185,12 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.related-header {
|
||||||
|
padding: 8px 12px;
|
||||||
|
margin-top: 10px;
|
||||||
|
background-color: var(--bg_panel);
|
||||||
|
color: var(--fg_faded);
|
||||||
|
font-size: 14px;
|
||||||
|
border-bottom: 1px solid var(--border_grey);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,14 +16,16 @@ var
|
||||||
|
|
||||||
proc getPair(): Future[TidPair] {.async.} =
|
proc getPair(): Future[TidPair] {.async.} =
|
||||||
if cachedPairs.len == 0 or int(epochTime()) - lastCached > ttlSec:
|
if cachedPairs.len == 0 or int(epochTime()) - lastCached > ttlSec:
|
||||||
lastCached = int(epochTime())
|
|
||||||
|
|
||||||
let client = newAsyncHttpClient()
|
let client = newAsyncHttpClient()
|
||||||
defer: client.close()
|
defer: client.close()
|
||||||
|
|
||||||
let resp = await client.get(pairsUrl)
|
let resp = await client.get(pairsUrl)
|
||||||
if resp.status == $Http200:
|
if resp.status == $Http200:
|
||||||
cachedPairs = parseTidPairs(await resp.body)
|
cachedPairs = parseTidPairs(await resp.body)
|
||||||
|
lastCached = int(epochTime())
|
||||||
|
|
||||||
|
if cachedPairs.len == 0:
|
||||||
|
raise newException(ValueError, "Failed to fetch x-client-transaction-id pairs")
|
||||||
|
|
||||||
return sample(cachedPairs)
|
return sample(cachedPairs)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -174,7 +174,10 @@ type
|
||||||
variants*: seq[VideoVariant]
|
variants*: seq[VideoVariant]
|
||||||
|
|
||||||
QueryKind* = enum
|
QueryKind* = enum
|
||||||
posts, replies, media, users, tweets, userList, followers, following
|
posts, replies, media, users, tweets, userList, followers, following, lists, top
|
||||||
|
|
||||||
|
RankingMode* = enum
|
||||||
|
Relevance, Recency, Likes
|
||||||
|
|
||||||
Query* = object
|
Query* = object
|
||||||
kind*: QueryKind
|
kind*: QueryKind
|
||||||
|
|
@ -356,6 +359,7 @@ type
|
||||||
content*: Tweets
|
content*: Tweets
|
||||||
hasMore*: bool
|
hasMore*: bool
|
||||||
cursor*: string
|
cursor*: string
|
||||||
|
related*: bool
|
||||||
|
|
||||||
Conversation* = ref object
|
Conversation* = ref object
|
||||||
tweet*: Tweet
|
tweet*: Tweet
|
||||||
|
|
@ -385,6 +389,12 @@ type
|
||||||
members*: int
|
members*: int
|
||||||
banner*: string
|
banner*: string
|
||||||
|
|
||||||
|
ListSearchResult* = object
|
||||||
|
list*: List
|
||||||
|
owner*: User
|
||||||
|
followersContext*: string
|
||||||
|
facepiles*: seq[string]
|
||||||
|
|
||||||
CommunityRule* = object
|
CommunityRule* = object
|
||||||
name*: string
|
name*: string
|
||||||
description*: string
|
description*: string
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,76 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
import options
|
|
||||||
import karax/[karaxdsl, vdom]
|
import karax/[karaxdsl, vdom]
|
||||||
from jester import Request
|
from jester import Request
|
||||||
|
|
||||||
import ".."/[types, formatters]
|
import ".."/[types, formatters, prefs]
|
||||||
import general, tweet
|
import general, tweet
|
||||||
|
|
||||||
const doctype = "<!DOCTYPE html>\n"
|
const
|
||||||
|
doctype = "<!DOCTYPE html>\n"
|
||||||
|
embedResizeJs = staticRead("../../public/js/embedResize.js")
|
||||||
|
|
||||||
proc renderVideoEmbed*(tweet: Tweet; cfg: Config; req: Request): string =
|
proc renderVideoEmbed*(tweet: Tweet; cfg: Config; req: Request): string =
|
||||||
let
|
let
|
||||||
video = tweet.getVideos()[0]
|
video = tweet.getVideos()[0]
|
||||||
thumb = video.thumb
|
thumb = video.thumb
|
||||||
vidUrl = getVideoEmbed(cfg, tweet.id)
|
vidUrl = getVideoEmbed(cfg, tweet.id)
|
||||||
prefs = Prefs(hlsPlayback: true, mp4Playback: true)
|
prefs = Prefs(hlsPlayback: true, mp4Playback: true, proxyVideos: defaultPrefs.proxyVideos)
|
||||||
|
tweetUrl = getLink(tweet)
|
||||||
|
|
||||||
let node = buildHtml(html(lang="en")):
|
let node = buildHtml(html(lang="en")):
|
||||||
renderHead(prefs, cfg, req, video=vidUrl, images=(@[thumb]))
|
renderHead(prefs, cfg, req, video=vidUrl, images=(@[thumb]))
|
||||||
|
base(target="_blank")
|
||||||
|
|
||||||
body:
|
body:
|
||||||
tdiv(class="embed-video"):
|
tdiv(class="embed-video"):
|
||||||
renderVideo(video, prefs, "")
|
renderVideo(video, prefs, "")
|
||||||
|
a(class="video-overlay-link", href=tweetUrl):
|
||||||
|
text "Watch on " & cfg.hostname
|
||||||
|
|
||||||
|
script:
|
||||||
|
verbatim embedResizeJs
|
||||||
|
|
||||||
|
result = doctype & $node
|
||||||
|
|
||||||
|
proc renderTweetEmbed*(tweet: Tweet; path: string; prefs: Prefs; cfg: Config; req: Request): string =
|
||||||
|
let node = buildHtml(html(lang="en")):
|
||||||
|
renderHead(prefs, cfg, req)
|
||||||
|
base(target="_blank")
|
||||||
|
|
||||||
|
body:
|
||||||
|
tdiv(class="embed-wrapper"):
|
||||||
|
tdiv(class="tweet-embed"):
|
||||||
|
a(class="tweet-link", href=getLink(tweet))
|
||||||
|
renderTweet(tweet, prefs, path, mainTweet=true)
|
||||||
|
a(class="embed-footer", href=getLink(tweet)):
|
||||||
|
text "Read more on " & cfg.hostname
|
||||||
|
|
||||||
|
script:
|
||||||
|
verbatim embedResizeJs
|
||||||
|
|
||||||
|
result = doctype & $node
|
||||||
|
|
||||||
|
proc renderErrorEmbed*(error: string; prefs: Prefs; cfg: Config; req: Request;
|
||||||
|
tweetId = ""; username = ""): string =
|
||||||
|
let link = if tweetId.len > 0:
|
||||||
|
if username.len > 0: "/" & username & "/status/" & tweetId
|
||||||
|
else: "/i/status/" & tweetId
|
||||||
|
else: "/"
|
||||||
|
|
||||||
|
let node = buildHtml(html(lang="en")):
|
||||||
|
renderHead(prefs, cfg, req)
|
||||||
|
base(target="_blank")
|
||||||
|
|
||||||
|
body:
|
||||||
|
tdiv(class="embed-wrapper"):
|
||||||
|
tdiv(class="tweet-embed error-embed"):
|
||||||
|
a(class="tweet-link", href=link)
|
||||||
|
tdiv(class="error-panel"):
|
||||||
|
span: text error
|
||||||
|
a(class="embed-footer", href=link):
|
||||||
|
text "Read more on " & cfg.hostname
|
||||||
|
|
||||||
|
script:
|
||||||
|
verbatim embedResizeJs
|
||||||
|
|
||||||
result = doctype & $node
|
result = doctype & $node
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ proc renderNavbar(cfg: Config; req: Request; rss, canonical: string): VNode =
|
||||||
|
|
||||||
proc renderHead*(prefs: Prefs; cfg: Config; req: Request; titleText=""; desc="";
|
proc renderHead*(prefs: Prefs; cfg: Config; req: Request; titleText=""; desc="";
|
||||||
video=""; images: seq[string] = @[]; banner=""; ogTitle="";
|
video=""; images: seq[string] = @[]; banner=""; ogTitle="";
|
||||||
rss=""; alternate=""): VNode =
|
rss=""; alternate=""; oembed=""): VNode =
|
||||||
let theme = prefs.theme.toTheme
|
let theme = prefs.theme.toTheme
|
||||||
|
|
||||||
let ogType =
|
let ogType =
|
||||||
|
|
@ -50,7 +50,7 @@ proc renderHead*(prefs: Prefs; cfg: Config; req: Request; titleText=""; desc="";
|
||||||
let opensearchUrl = getUrlPrefix(cfg) & "/opensearch"
|
let opensearchUrl = getUrlPrefix(cfg) & "/opensearch"
|
||||||
|
|
||||||
buildHtml(head):
|
buildHtml(head):
|
||||||
link(rel="stylesheet", type="text/css", href="/css/style.css?v=45")
|
link(rel="stylesheet", type="text/css", href="/css/style.css?v=106")
|
||||||
link(rel="stylesheet", type="text/css", href="/css/fontello.css?v=7")
|
link(rel="stylesheet", type="text/css", href="/css/fontello.css?v=7")
|
||||||
|
|
||||||
if theme.len > 0:
|
if theme.len > 0:
|
||||||
|
|
@ -70,6 +70,10 @@ proc renderHead*(prefs: Prefs; cfg: Config; req: Request; titleText=""; desc="";
|
||||||
if rss.len > 0:
|
if rss.len > 0:
|
||||||
link(rel="alternate", type="application/rss+xml", href=rss, title="RSS feed")
|
link(rel="alternate", type="application/rss+xml", href=rss, title="RSS feed")
|
||||||
|
|
||||||
|
if oembed.len > 0:
|
||||||
|
let oembedTitle = if titleText.len > 0: titleText else: "oEmbed"
|
||||||
|
link(rel="alternate", type="application/json+oembed", href=oembed, title=oembedTitle)
|
||||||
|
|
||||||
if prefs.hlsPlayback:
|
if prefs.hlsPlayback:
|
||||||
script(src="/js/hls.min.js", `defer`="")
|
script(src="/js/hls.min.js", `defer`="")
|
||||||
script(src="/js/hlsPlayback.js?v=1", `defer`="")
|
script(src="/js/hlsPlayback.js?v=1", `defer`="")
|
||||||
|
|
@ -124,7 +128,7 @@ proc renderHead*(prefs: Prefs; cfg: Config; req: Request; titleText=""; desc="";
|
||||||
proc renderMain*(body: VNode; req: Request; cfg: Config; prefs=defaultPrefs;
|
proc renderMain*(body: VNode; req: Request; cfg: Config; prefs=defaultPrefs;
|
||||||
titleText=""; desc=""; ogTitle=""; rss=""; video="";
|
titleText=""; desc=""; ogTitle=""; rss=""; video="";
|
||||||
images: seq[string] = @[]; banner="";
|
images: seq[string] = @[]; banner="";
|
||||||
twitterLink=""): string =
|
twitterLink=""; oembed=""): string =
|
||||||
|
|
||||||
let twitterLink =
|
let twitterLink =
|
||||||
if twitterLink.len > 0: twitterLink
|
if twitterLink.len > 0: twitterLink
|
||||||
|
|
@ -132,7 +136,7 @@ proc renderMain*(body: VNode; req: Request; cfg: Config; prefs=defaultPrefs;
|
||||||
|
|
||||||
let node = buildHtml(html(lang="en")):
|
let node = buildHtml(html(lang="en")):
|
||||||
renderHead(prefs, cfg, req, titleText, desc, video, images, banner, ogTitle,
|
renderHead(prefs, cfg, req, titleText, desc, video, images, banner, ogTitle,
|
||||||
rss, twitterLink)
|
rss, twitterLink, oembed)
|
||||||
|
|
||||||
let bodyClass = if prefs.stickyNav: "fixed-nav" else: ""
|
let bodyClass = if prefs.stickyNav: "fixed-nav" else: ""
|
||||||
body(class=bodyClass):
|
body(class=bodyClass):
|
||||||
|
|
|
||||||
7
src/views/oembed.nimf
Normal file
7
src/views/oembed.nimf
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
#? stdtmpl(subsChar = '$', metaChar = '#')
|
||||||
|
## SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
#proc renderOembedIframe*(embedUrl: string; maxwidth = 550): string =
|
||||||
|
# result = ""
|
||||||
|
<iframe src="$embedUrl" style="width:100%;max-width:${maxwidth}px;height:250px;border:none" scrolling="no" loading="lazy" onload="let c=new MessageChannel;c.port1.onmessage=e=>this.style.height=e.data+'px';this.contentWindow.postMessage('','*',[c.port2])"></iframe>
|
||||||
|
# result = result.strip()
|
||||||
|
#end proc
|
||||||
|
|
@ -115,7 +115,7 @@ proc renderProtected*(username: string): VNode =
|
||||||
proc renderProfile*(profile: var Profile; prefs: Prefs; path: string): VNode =
|
proc renderProfile*(profile: var Profile; prefs: Prefs; path: string): VNode =
|
||||||
profile.tweets.query.fromUser = @[profile.user.username]
|
profile.tweets.query.fromUser = @[profile.user.username]
|
||||||
let
|
let
|
||||||
isGalleryView = profile.tweets.query.kind == media and
|
isGalleryView = profile.tweets.query.kind == QueryKind.media and
|
||||||
profile.tweets.query.view == "gallery"
|
profile.tweets.query.view == "gallery"
|
||||||
viewClass = if isGalleryView: " media-only" else: ""
|
viewClass = if isGalleryView: " media-only" else: ""
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,28 +40,45 @@ proc renderProfileTabs*(query: Query; username: string): VNode =
|
||||||
li(class=query.getTabClass(tweets)):
|
li(class=query.getTabClass(tweets)):
|
||||||
a(href=(link & "/search")): text "Search"
|
a(href=(link & "/search")): text "Search"
|
||||||
|
|
||||||
proc renderMediaViewTabs*(query: Query; username: string): VNode =
|
proc mediaViewUrl(query: Query; view: string): string =
|
||||||
|
var q = query
|
||||||
|
q.view = view
|
||||||
|
"?" & genQueryUrl(q)
|
||||||
|
|
||||||
|
proc renderMediaViewTabs*(query: Query): VNode =
|
||||||
let currentView = if query.view.len > 0: query.view else: "timeline"
|
let currentView = if query.view.len > 0: query.view else: "timeline"
|
||||||
let base = "/" & username & "/media?view="
|
|
||||||
func cls(view: string): string =
|
func cls(view: string): string =
|
||||||
if currentView == view: "tab-item active" else: "tab-item"
|
if currentView == view: "tab-item active" else: "tab-item"
|
||||||
buildHtml(ul(class="tab media-view-tabs")):
|
buildHtml(ul(class="tab media-view-tabs")):
|
||||||
li(class=cls("timeline")):
|
li(class=cls("timeline")):
|
||||||
a(href=(base & "timeline")): text "Timeline"
|
a(href=query.mediaViewUrl("timeline")): text "Timeline"
|
||||||
li(class=cls("grid")):
|
li(class=cls("grid")):
|
||||||
a(href=(base & "grid")): text "Grid"
|
a(href=query.mediaViewUrl("grid")): text "Grid"
|
||||||
li(class=cls("gallery")):
|
li(class=cls("gallery")):
|
||||||
a(href=(base & "gallery")): text "Gallery"
|
a(href=query.mediaViewUrl("gallery")): text "Gallery"
|
||||||
|
|
||||||
proc renderSearchTabs*(query: Query): VNode =
|
proc renderSearchTabs*(query: Query): VNode =
|
||||||
var q = query
|
var q = query
|
||||||
|
# the media view mode only applies to the Media tab
|
||||||
|
q.view = ""
|
||||||
buildHtml(ul(class="tab")):
|
buildHtml(ul(class="tab")):
|
||||||
|
li(class=query.getTabClass(top)):
|
||||||
|
q.kind = top
|
||||||
|
a(href=("?" & genQueryUrl(q))): text "Top"
|
||||||
li(class=query.getTabClass(tweets)):
|
li(class=query.getTabClass(tweets)):
|
||||||
q.kind = tweets
|
q.kind = tweets
|
||||||
a(href=("?" & genQueryUrl(q))): text "Tweets"
|
a(href=("?" & genQueryUrl(q))): text "Latest"
|
||||||
|
li(class=query.getTabClass(media)):
|
||||||
|
q.kind = media
|
||||||
|
q.view = query.view
|
||||||
|
a(href=("?" & genQueryUrl(q))): text "Media"
|
||||||
li(class=query.getTabClass(users)):
|
li(class=query.getTabClass(users)):
|
||||||
q.kind = users
|
q.kind = users
|
||||||
|
q.view = ""
|
||||||
a(href=("?" & genQueryUrl(q))): text "Users"
|
a(href=("?" & genQueryUrl(q))): text "Users"
|
||||||
|
li(class=query.getTabClass(lists)):
|
||||||
|
q.kind = lists
|
||||||
|
a(href=("?" & genQueryUrl(q))): text "Lists"
|
||||||
|
|
||||||
proc isPanelOpen(q: Query): bool =
|
proc isPanelOpen(q: Query): bool =
|
||||||
q.fromUser.len == 0 and (q.filters.len > 0 or q.excludes.len > 0 or
|
q.fromUser.len == 0 and (q.filters.len > 0 or q.excludes.len > 0 or
|
||||||
|
|
@ -72,7 +89,7 @@ proc renderSearchPanel*(query: Query): VNode =
|
||||||
let action = if user.len > 0: &"/{user}/search" else: "/search"
|
let action = if user.len > 0: &"/{user}/search" else: "/search"
|
||||||
buildHtml(form(`method`="get", action=action,
|
buildHtml(form(`method`="get", action=action,
|
||||||
class="search-field", autocomplete="off")):
|
class="search-field", autocomplete="off")):
|
||||||
hiddenField("f", "tweets")
|
hiddenField("f", $query.kind)
|
||||||
genInput("q", "", query.text, "Enter search...", class="pref-inline")
|
genInput("q", "", query.text, "Enter search...", class="pref-inline")
|
||||||
button(`type`="submit"): icon "search"
|
button(`type`="submit"): icon "search"
|
||||||
|
|
||||||
|
|
@ -103,33 +120,52 @@ proc renderSearchPanel*(query: Query): VNode =
|
||||||
proc renderTweetSearch*(results: Timeline; prefs: Prefs; path: string;
|
proc renderTweetSearch*(results: Timeline; prefs: Prefs; path: string;
|
||||||
pinned=none(Tweet)): VNode =
|
pinned=none(Tweet)): VNode =
|
||||||
let query = results.query
|
let query = results.query
|
||||||
buildHtml(tdiv(class="timeline-container")):
|
let containerClass =
|
||||||
|
if query.fromUser.len == 0 and query.kind == QueryKind.media and
|
||||||
|
query.view == "gallery": "timeline-container media-only"
|
||||||
|
else: "timeline-container"
|
||||||
|
buildHtml(tdiv(class=containerClass)):
|
||||||
if query.fromUser.len > 1:
|
if query.fromUser.len > 1:
|
||||||
tdiv(class="timeline-header"):
|
tdiv(class="timeline-header"):
|
||||||
text query.fromUser.join(" | ")
|
text query.fromUser.join(" | ")
|
||||||
|
|
||||||
if query.fromUser.len > 0:
|
if query.fromUser.len > 0:
|
||||||
if query.kind != media or query.view != "gallery":
|
if query.kind != QueryKind.media or query.view != "gallery":
|
||||||
renderProfileTabs(query, query.fromUser.join(","))
|
renderProfileTabs(query, query.fromUser.join(","))
|
||||||
if query.kind == media and query.fromUser.len == 1:
|
if query.kind == QueryKind.media and query.fromUser.len == 1:
|
||||||
renderMediaViewTabs(query, query.fromUser[0])
|
renderMediaViewTabs(query)
|
||||||
|
|
||||||
if query.fromUser.len == 0 or query.kind == tweets:
|
if query.fromUser.len == 0 or query.kind == QueryKind.tweets:
|
||||||
tdiv(class="timeline-header"):
|
tdiv(class="timeline-header"):
|
||||||
renderSearchPanel(query)
|
renderSearchPanel(query)
|
||||||
|
|
||||||
if query.fromUser.len == 0:
|
if query.fromUser.len == 0:
|
||||||
renderSearchTabs(query)
|
renderSearchTabs(query)
|
||||||
|
if query.kind == QueryKind.media:
|
||||||
|
renderMediaViewTabs(query)
|
||||||
|
|
||||||
renderTimelineTweets(results, prefs, path, pinned)
|
renderTimelineTweets(results, prefs, path, pinned)
|
||||||
|
|
||||||
|
proc renderSearchForm(kind, placeholder, value: string): VNode =
|
||||||
|
buildHtml(form(`method`="get", action="/search",
|
||||||
|
class="search-field", autocomplete="off")):
|
||||||
|
hiddenField("f", kind)
|
||||||
|
genInput("q", "", value, placeholder, class="pref-inline")
|
||||||
|
button(`type`="submit"): icon "search"
|
||||||
|
|
||||||
proc renderUserSearch*(results: Result[User]; prefs: Prefs): VNode =
|
proc renderUserSearch*(results: Result[User]; prefs: Prefs): VNode =
|
||||||
buildHtml(tdiv(class="timeline-container")):
|
buildHtml(tdiv(class="timeline-container")):
|
||||||
tdiv(class="timeline-header"):
|
tdiv(class="timeline-header"):
|
||||||
form(`method`="get", action="/search", class="search-field", autocomplete="off"):
|
renderSearchForm("users", "Enter username...", results.query.text)
|
||||||
hiddenField("f", "users")
|
|
||||||
genInput("q", "", results.query.text, "Enter username...", class="pref-inline")
|
|
||||||
button(`type`="submit"): icon "search"
|
|
||||||
|
|
||||||
renderSearchTabs(results.query)
|
renderSearchTabs(results.query)
|
||||||
renderTimelineUsers(results, prefs)
|
renderTimelineUsers(results, prefs)
|
||||||
|
|
||||||
|
proc renderListSearch*(results: Result[ListSearchResult]; prefs: Prefs;
|
||||||
|
path: string): VNode =
|
||||||
|
buildHtml(tdiv(class="timeline-container")):
|
||||||
|
tdiv(class="timeline-header"):
|
||||||
|
renderSearchForm("lists", "Enter search...", results.query.text)
|
||||||
|
|
||||||
|
renderSearchTabs(results.query)
|
||||||
|
renderTimelineLists(results, prefs, path)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
import sequtils
|
||||||
import karax/[karaxdsl, vdom]
|
import karax/[karaxdsl, vdom]
|
||||||
|
|
||||||
import ".."/[types, formatters]
|
import ".."/[types, formatters]
|
||||||
|
|
@ -28,21 +29,46 @@ proc renderReplyThread(thread: Chain; prefs: Prefs; path: string): VNode =
|
||||||
if thread.hasMore:
|
if thread.hasMore:
|
||||||
renderMoreReplies(thread)
|
renderMoreReplies(thread)
|
||||||
|
|
||||||
proc renderReplies*(replies: Result[Chain]; prefs: Prefs; path: string; tweet: Tweet = nil): VNode =
|
proc renderReplySort(sort: RankingMode): VNode =
|
||||||
|
buildHtml(tdiv(class="reply-sort")):
|
||||||
|
span(class="reply-sort-label"): text "Sort replies:"
|
||||||
|
for mode in RankingMode:
|
||||||
|
let
|
||||||
|
cls = if mode == sort: "reply-sort-option active"
|
||||||
|
else: "reply-sort-option"
|
||||||
|
label = case mode
|
||||||
|
of Relevance: "Relevant"
|
||||||
|
of Recency: "Recent"
|
||||||
|
of Likes: "Liked"
|
||||||
|
a(class=cls, href=("?sort=" & $mode & "#r")):
|
||||||
|
text label
|
||||||
|
|
||||||
|
proc renderReplies*(replies: Result[Chain]; prefs: Prefs; path: string;
|
||||||
|
tweet: Tweet = nil; sort = Relevance): VNode =
|
||||||
buildHtml(tdiv(class="replies", id="r")):
|
buildHtml(tdiv(class="replies", id="r")):
|
||||||
var hasReplies = false
|
var hasReplies = false
|
||||||
var replyCount = 0
|
var replyCount = 0
|
||||||
for thread in replies.content:
|
for thread in replies.content:
|
||||||
if thread.content.len == 0: continue
|
if thread.content.len == 0 or thread.related: continue
|
||||||
hasReplies = true
|
hasReplies = true
|
||||||
replyCount += thread.content.len
|
replyCount += thread.content.len
|
||||||
renderReplyThread(thread, prefs, path)
|
renderReplyThread(thread, prefs, path)
|
||||||
|
|
||||||
if hasReplies and replies.bottom.len > 0:
|
if hasReplies and replies.bottom.len > 0:
|
||||||
if tweet == nil or not replies.beginning or replyCount < tweet.stats.replies:
|
if tweet == nil or not replies.beginning or replyCount < tweet.stats.replies:
|
||||||
renderMore(Query(), replies.bottom, focus="#r")
|
let extra = if sort == Relevance: "" else: "sort=" & $sort & "&"
|
||||||
|
renderMore(Query(), replies.bottom, focus="#r", extra=extra)
|
||||||
|
|
||||||
proc renderConversation*(conv: Conversation; prefs: Prefs; path: string): VNode =
|
proc renderRelated(replies: Result[Chain]; prefs: Prefs; path: string): VNode =
|
||||||
|
buildHtml(tdiv(class="related-tweets")):
|
||||||
|
tdiv(class="related-header"):
|
||||||
|
text "Related tweets"
|
||||||
|
for thread in replies.content:
|
||||||
|
if thread.content.len == 0 or not thread.related: continue
|
||||||
|
renderReplyThread(thread, prefs, path)
|
||||||
|
|
||||||
|
proc renderConversation*(conv: Conversation; prefs: Prefs; path: string;
|
||||||
|
sort = Relevance): VNode =
|
||||||
let hasAfter = conv.after.content.len > 0
|
let hasAfter = conv.after.content.len > 0
|
||||||
let threadId = conv.tweet.threadId
|
let threadId = conv.tweet.threadId
|
||||||
buildHtml(tdiv(class="conversation")):
|
buildHtml(tdiv(class="conversation")):
|
||||||
|
|
@ -75,7 +101,12 @@ proc renderConversation*(conv: Conversation; prefs: Prefs; path: string): VNode
|
||||||
if not conv.replies.beginning:
|
if not conv.replies.beginning:
|
||||||
renderNewer(Query(), getLink(conv.tweet), focus="#r")
|
renderNewer(Query(), getLink(conv.tweet), focus="#r")
|
||||||
if conv.replies.content.len > 0 or conv.replies.bottom.len > 0:
|
if conv.replies.content.len > 0 or conv.replies.bottom.len > 0:
|
||||||
renderReplies(conv.replies, prefs, path, conv.tweet)
|
renderReplySort(sort)
|
||||||
|
renderReplies(conv.replies, prefs, path, conv.tweet, sort)
|
||||||
|
|
||||||
|
if not prefs.hideRelated:
|
||||||
|
if conv.replies.content.anyIt(it.related and it.content.len > 0):
|
||||||
|
renderRelated(conv.replies, prefs, path)
|
||||||
|
|
||||||
renderToTop(focus="#m")
|
renderToTop(focus="#m")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import ".."/[types, query, formatters]
|
||||||
import tweet, renderutils
|
import tweet, renderutils
|
||||||
|
|
||||||
proc timelineViewClass(query: Query): string =
|
proc timelineViewClass(query: Query): string =
|
||||||
if query.kind != media:
|
if query.kind != QueryKind.media:
|
||||||
return "timeline"
|
return "timeline"
|
||||||
|
|
||||||
case query.view
|
case query.view
|
||||||
|
|
@ -50,9 +50,9 @@ proc renderNewer*(query: Query; path: string; focus=""): VNode =
|
||||||
a(href=(p & url)):
|
a(href=(p & url)):
|
||||||
text "Load newest"
|
text "Load newest"
|
||||||
|
|
||||||
proc renderMore*(query: Query; cursor: string; focus=""): VNode =
|
proc renderMore*(query: Query; cursor: string; focus=""; extra=""): VNode =
|
||||||
buildHtml(tdiv(class="show-more")):
|
buildHtml(tdiv(class="show-more")):
|
||||||
a(href=(&"?{getQuery(query)}cursor={encodeUrl(cursor, usePlus=false)}{focus}")):
|
a(href=(&"?{extra}{getQuery(query)}cursor={encodeUrl(cursor, usePlus=false)}{focus}")):
|
||||||
text "Load more"
|
text "Load more"
|
||||||
|
|
||||||
proc renderNoMore(): VNode =
|
proc renderNoMore(): VNode =
|
||||||
|
|
@ -114,6 +114,84 @@ proc renderTimelineUsers*(results: Result[User]; prefs: Prefs; path=""): VNode =
|
||||||
else:
|
else:
|
||||||
renderNoMore()
|
renderNoMore()
|
||||||
|
|
||||||
|
proc mentionUsername(word: string): string =
|
||||||
|
# "@user" -> "user" for well-formed mentions, "" otherwise
|
||||||
|
if word.len > 1 and word[0] == '@' and
|
||||||
|
word[1 .. ^1].allCharsInSet({'A'..'Z', 'a'..'z', '0'..'9', '_'}):
|
||||||
|
word[1 .. ^1]
|
||||||
|
else: ""
|
||||||
|
|
||||||
|
proc mentionedUser(s: string): string =
|
||||||
|
# last @mention in strings like "65 followers including @user"
|
||||||
|
let words = s.split(' ')
|
||||||
|
for i in countdown(words.high, 0):
|
||||||
|
result = mentionUsername(words[i])
|
||||||
|
if result.len > 0: return
|
||||||
|
|
||||||
|
proc renderMentionedText(s: string): VNode =
|
||||||
|
# linkify @mentions in plain API strings like "65 followers including @user"
|
||||||
|
let words = s.split(' ')
|
||||||
|
buildHtml(span):
|
||||||
|
for i in 0 ..< words.len:
|
||||||
|
if i > 0: text " "
|
||||||
|
let username = mentionUsername(words[i])
|
||||||
|
if username.len > 0:
|
||||||
|
a(href=("/" & username)): text words[i]
|
||||||
|
else:
|
||||||
|
text words[i]
|
||||||
|
|
||||||
|
proc renderListCard(r: ListSearchResult): VNode =
|
||||||
|
let listUrl = "/i/lists/" & r.list.id
|
||||||
|
buildHtml(tdiv(class="timeline-item list-result")):
|
||||||
|
a(class="tweet-link", href=listUrl)
|
||||||
|
a(class="list-result-banner", href=listUrl):
|
||||||
|
if r.list.banner.len > 0:
|
||||||
|
genImg(r.list.banner)
|
||||||
|
tdiv(class="list-result-body"):
|
||||||
|
tdiv(class="list-result-title fullname-and-username"):
|
||||||
|
a(class="list-name fullname", href=listUrl): text r.list.name
|
||||||
|
span(class="list-members"):
|
||||||
|
text &"· {insertSep($r.list.members, ',')} members"
|
||||||
|
tdiv(class="list-result-context"):
|
||||||
|
if r.followersContext.len > 0:
|
||||||
|
# the first facepile belongs to the "including @user" account
|
||||||
|
let mentioned = mentionedUser(r.followersContext)
|
||||||
|
for i in 0 ..< r.facepiles.len:
|
||||||
|
if i == 0 and mentioned.len > 0:
|
||||||
|
a(class="facepile-link", href=("/" & mentioned)):
|
||||||
|
genImg(r.facepiles[i], class="list-facepile")
|
||||||
|
else:
|
||||||
|
genImg(r.facepiles[i], class="list-facepile")
|
||||||
|
renderMentionedText(r.followersContext)
|
||||||
|
else:
|
||||||
|
if r.owner.username.len > 0:
|
||||||
|
a(class="facepile-link", href=("/" & r.owner.username)):
|
||||||
|
genImg(r.owner.getUserPic("_mini"), class="list-facepile")
|
||||||
|
else:
|
||||||
|
genImg(r.owner.getUserPic("_mini"), class="list-facepile")
|
||||||
|
linkUser(r.owner, class="fullname")
|
||||||
|
linkUser(r.owner, class="username")
|
||||||
|
if r.list.description.len > 0:
|
||||||
|
tdiv(class="list-result-description"):
|
||||||
|
text r.list.description
|
||||||
|
|
||||||
|
proc renderTimelineLists*(results: Result[ListSearchResult]; prefs: Prefs;
|
||||||
|
path=""): VNode =
|
||||||
|
buildHtml(tdiv(class="timeline")):
|
||||||
|
if not results.beginning:
|
||||||
|
renderNewer(results.query, path)
|
||||||
|
|
||||||
|
if results.content.len > 0:
|
||||||
|
for list in results.content:
|
||||||
|
renderListCard(list)
|
||||||
|
if results.bottom.len > 0:
|
||||||
|
renderMore(results.query, results.bottom)
|
||||||
|
renderToTop()
|
||||||
|
elif results.beginning:
|
||||||
|
renderNoneFound()
|
||||||
|
else:
|
||||||
|
renderNoMore()
|
||||||
|
|
||||||
proc filterThreads(threads: seq[Tweets]; prefs: Prefs): seq[Tweets] =
|
proc filterThreads(threads: seq[Tweets]; prefs: Prefs): seq[Tweets] =
|
||||||
var retweets: seq[int64]
|
var retweets: seq[int64]
|
||||||
for thread in threads:
|
for thread in threads:
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ from jester import Request
|
||||||
|
|
||||||
import renderutils
|
import renderutils
|
||||||
import ".."/[types, utils, formatters]
|
import ".."/[types, utils, formatters]
|
||||||
import general
|
|
||||||
|
|
||||||
const doctype = "<!DOCTYPE html>\n"
|
const doctype = "<!DOCTYPE html>\n"
|
||||||
|
|
||||||
|
|
@ -464,13 +463,3 @@ proc renderTweet*(tweet: Tweet; prefs: Prefs; path: string; class=""; index=0;
|
||||||
|
|
||||||
if not prefs.hideTweetStats:
|
if not prefs.hideTweetStats:
|
||||||
renderStats(tweet.stats)
|
renderStats(tweet.stats)
|
||||||
|
|
||||||
proc renderTweetEmbed*(tweet: Tweet; path: string; prefs: Prefs; cfg: Config; req: Request): string =
|
|
||||||
let node = buildHtml(html(lang="en")):
|
|
||||||
renderHead(prefs, cfg, req)
|
|
||||||
|
|
||||||
body:
|
|
||||||
tdiv(class="tweet-embed"):
|
|
||||||
renderTweet(tweet, prefs, path, mainTweet=true)
|
|
||||||
|
|
||||||
result = doctype & $node
|
|
||||||
|
|
|
||||||
|
|
@ -55,14 +55,19 @@ class Timeline(object):
|
||||||
protected = '.timeline-protected'
|
protected = '.timeline-protected'
|
||||||
photo_rail = '.photo-rail-grid'
|
photo_rail = '.photo-rail-grid'
|
||||||
media_view_tabs = '.media-view-tabs'
|
media_view_tabs = '.media-view-tabs'
|
||||||
media_view_timeline = '.media-view-tabs a[href$="media?view=timeline"]'
|
media_view_timeline = '.media-view-tabs a[href*="view=timeline"]'
|
||||||
media_view_grid = '.media-view-tabs a[href$="media?view=grid"]'
|
media_view_grid = '.media-view-tabs a[href*="view=grid"]'
|
||||||
media_view_gallery = '.media-view-tabs a[href$="media?view=gallery"]'
|
media_view_gallery = '.media-view-tabs a[href*="view=gallery"]'
|
||||||
media_view_active = '.media-view-tabs .tab-item.active a'
|
media_view_active = '.media-view-tabs .tab-item.active a'
|
||||||
grid_view = '.timeline.media-grid-view'
|
grid_view = '.timeline.media-grid-view'
|
||||||
gallery_view = '.timeline.media-gallery-view'
|
gallery_view = '.timeline.media-gallery-view'
|
||||||
|
|
||||||
|
|
||||||
|
class Search(object):
|
||||||
|
tab_item = '.tab .tab-item'
|
||||||
|
tab_active = '.tab .tab-item.active a'
|
||||||
|
|
||||||
|
|
||||||
class Conversation(object):
|
class Conversation(object):
|
||||||
main = '.main-tweet'
|
main = '.main-tweet'
|
||||||
before = '.before-tweet'
|
before = '.before-tweet'
|
||||||
|
|
@ -71,6 +76,8 @@ class Conversation(object):
|
||||||
thread = '.reply'
|
thread = '.reply'
|
||||||
tweet = '.timeline-item'
|
tweet = '.timeline-item'
|
||||||
tweet_text = '.tweet-content'
|
tweet_text = '.tweet-content'
|
||||||
|
reply_sort = '.reply-sort'
|
||||||
|
reply_sort_active = '.reply-sort-option.active'
|
||||||
|
|
||||||
|
|
||||||
class Poll(object):
|
class Poll(object):
|
||||||
|
|
|
||||||
4
tests/conftest.py
Normal file
4
tests/conftest.py
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
from seleniumbase.config import settings
|
||||||
|
|
||||||
|
settings.SKIP_JS_WAITS = True
|
||||||
|
settings.WAIT_FOR_RSC_ON_PAGE_LOADS = False
|
||||||
|
|
@ -6,3 +6,6 @@ package-mode = false
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = "^3.14"
|
python = "^3.14"
|
||||||
seleniumbase = "4.46.5"
|
seleniumbase = "4.46.5"
|
||||||
|
|
||||||
|
[tool.pytest.ini_options]
|
||||||
|
addopts = "--pls=eager --rcs --reruns=2 --only-rerun=timeout --only-rerun=Timeout --only-rerun=Connection --only-rerun=WebDriverException --timeout_multiplier=5"
|
||||||
|
|
|
||||||
|
|
@ -291,6 +291,8 @@ class ArticleQuotedCardTest(BaseTestCase):
|
||||||
|
|
||||||
def test_quoted_card_has_cover_image(self):
|
def test_quoted_card_has_cover_image(self):
|
||||||
self.open_nitter(self.quoted_tweet)
|
self.open_nitter(self.quoted_tweet)
|
||||||
|
# Scroll to element to trigger lazy loading
|
||||||
|
self.scroll_to('.quote .article-card .card-image img')
|
||||||
self.assert_element_visible('.quote .article-card .card-image img')
|
self.assert_element_visible('.quote .article-card .card-image img')
|
||||||
src = self.get_attribute('.quote .article-card .card-image img', 'src')
|
src = self.get_attribute('.quote .article-card .card-image img', 'src')
|
||||||
self.assertIn('/pic/', src)
|
self.assertIn('/pic/', src)
|
||||||
|
|
|
||||||
276
tests/test_embed.py
Normal file
276
tests/test_embed.py
Normal file
|
|
@ -0,0 +1,276 @@
|
||||||
|
import requests
|
||||||
|
from base import BaseTestCase, Media
|
||||||
|
from parameterized import parameterized
|
||||||
|
|
||||||
|
|
||||||
|
class Embed:
|
||||||
|
container = '.tweet-embed'
|
||||||
|
footer = '.embed-footer'
|
||||||
|
tweet_content = '.tweet-content'
|
||||||
|
tweet_header = '.tweet-header'
|
||||||
|
fullname = '.fullname'
|
||||||
|
username = '.username'
|
||||||
|
avatar = '.avatar'
|
||||||
|
stats = '.tweet-stats'
|
||||||
|
quote = '.quote'
|
||||||
|
error_panel = '.error-panel'
|
||||||
|
|
||||||
|
|
||||||
|
class TweetEmbedTest(BaseTestCase):
|
||||||
|
"""Test tweet embed rendering."""
|
||||||
|
tweet = 'elonmusk/status/1141367104702038016'
|
||||||
|
|
||||||
|
def test_embed_container_visible(self):
|
||||||
|
self.open_nitter(self.tweet + '/embed')
|
||||||
|
self.assert_element_visible(Embed.container)
|
||||||
|
|
||||||
|
def test_embed_has_footer(self):
|
||||||
|
self.open_nitter(self.tweet + '/embed')
|
||||||
|
self.assert_element_visible(Embed.footer)
|
||||||
|
self.assert_text_visible('Read more on', Embed.footer)
|
||||||
|
|
||||||
|
def test_embed_has_tweet_content(self):
|
||||||
|
self.open_nitter(self.tweet + '/embed')
|
||||||
|
self.assert_element_visible(Embed.tweet_content)
|
||||||
|
|
||||||
|
def test_embed_has_avatar(self):
|
||||||
|
self.open_nitter(self.tweet + '/embed')
|
||||||
|
self.assert_element_visible(Embed.avatar)
|
||||||
|
|
||||||
|
def test_embed_has_username(self):
|
||||||
|
self.open_nitter(self.tweet + '/embed')
|
||||||
|
self.assert_element_visible(Embed.username)
|
||||||
|
|
||||||
|
def test_embed_has_stats(self):
|
||||||
|
self.open_nitter(self.tweet + '/embed')
|
||||||
|
self.assert_element_visible(Embed.stats)
|
||||||
|
|
||||||
|
def test_embed_footer_links_to_tweet(self):
|
||||||
|
self.open_nitter(self.tweet + '/embed')
|
||||||
|
href = self.get_attribute(Embed.footer, 'href')
|
||||||
|
self.assertIn('/elonmusk/status/1141367104702038016', href)
|
||||||
|
|
||||||
|
|
||||||
|
class TweetEmbedMediaTest(BaseTestCase):
|
||||||
|
"""Test embed rendering with various media types."""
|
||||||
|
|
||||||
|
def test_embed_with_image(self):
|
||||||
|
self.open_nitter('mobile_test/status/519364660823207936/embed')
|
||||||
|
self.assert_element_visible(Embed.container)
|
||||||
|
self.scroll_to(Media.container)
|
||||||
|
self.assert_element_visible(Media.image)
|
||||||
|
|
||||||
|
def test_embed_with_gif(self):
|
||||||
|
self.open_nitter('elonmusk/status/1141367104702038016/embed')
|
||||||
|
self.assert_element_visible(Embed.container)
|
||||||
|
self.scroll_to(Media.container)
|
||||||
|
self.assert_element_visible(Media.gif)
|
||||||
|
|
||||||
|
def test_embed_with_video(self):
|
||||||
|
self.open_nitter('d0m96/status/1078373829917974528/embed')
|
||||||
|
self.assert_element_visible(Embed.container)
|
||||||
|
self.scroll_to(Media.container)
|
||||||
|
self.assert_element_visible(Media.video)
|
||||||
|
|
||||||
|
def test_embed_with_gallery(self):
|
||||||
|
self.open_nitter('mobile_test/status/451108446603980803/embed')
|
||||||
|
self.assert_element_visible(Embed.container)
|
||||||
|
self.scroll_to(Media.container)
|
||||||
|
self.assert_element_visible(Media.row)
|
||||||
|
|
||||||
|
|
||||||
|
class TweetEmbedQuoteTest(BaseTestCase):
|
||||||
|
"""Test embed rendering with quoted tweets."""
|
||||||
|
|
||||||
|
def test_embed_with_quote_shows_quote(self):
|
||||||
|
self.open_nitter('elonmusk/status/1138827760107790336/embed')
|
||||||
|
self.assert_element_visible(Embed.container)
|
||||||
|
self.assert_element_visible(Embed.quote)
|
||||||
|
|
||||||
|
def test_embed_quote_has_content(self):
|
||||||
|
self.open_nitter('elonmusk/status/1138827760107790336/embed')
|
||||||
|
quote = self.find_element(Embed.quote)
|
||||||
|
self.assertIsNotNone(quote.text)
|
||||||
|
|
||||||
|
|
||||||
|
class EmbedErrorTest(BaseTestCase):
|
||||||
|
"""Test embed error handling."""
|
||||||
|
|
||||||
|
def test_nonexistent_tweet_shows_error(self):
|
||||||
|
self.open_nitter('nobody/status/1/embed')
|
||||||
|
self.assert_element_visible('.tweet-embed.error-embed')
|
||||||
|
self.assert_text_visible('not found', Embed.error_panel)
|
||||||
|
|
||||||
|
def test_protected_account_embed_shows_error(self):
|
||||||
|
self.open_nitter('mobile_test_7/status/1/embed')
|
||||||
|
self.assert_element_visible('.tweet-embed.error-embed')
|
||||||
|
|
||||||
|
def test_invalid_tweet_id_shows_error(self):
|
||||||
|
self.open_nitter('jack/status/notanumber/embed')
|
||||||
|
self.assert_element_visible('.tweet-embed.error-embed')
|
||||||
|
|
||||||
|
|
||||||
|
class OEmbedApiTest(BaseTestCase):
|
||||||
|
"""Test oEmbed API endpoint."""
|
||||||
|
base_url = 'http://localhost:8080'
|
||||||
|
tweet_url = 'https://twitter.com/elonmusk/status/1141367104702038016'
|
||||||
|
|
||||||
|
def test_oembed_returns_json(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
|
||||||
|
self.assertEqual(resp.status_code, 200)
|
||||||
|
self.assertEqual(resp.headers['Content-Type'], 'application/json')
|
||||||
|
|
||||||
|
def test_oembed_has_required_fields(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
|
||||||
|
data = resp.json()
|
||||||
|
self.assertEqual(data['type'], 'rich')
|
||||||
|
self.assertEqual(data['version'], '1.0')
|
||||||
|
self.assertIn('html', data)
|
||||||
|
self.assertIn('author_name', data)
|
||||||
|
self.assertIn('provider_name', data)
|
||||||
|
|
||||||
|
def test_oembed_html_contains_iframe(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
|
||||||
|
data = resp.json()
|
||||||
|
self.assertIn('<iframe', data['html'])
|
||||||
|
self.assertIn('/embed', data['html'])
|
||||||
|
|
||||||
|
def test_oembed_has_cors_header(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
|
||||||
|
self.assertEqual(resp.headers.get('Access-Control-Allow-Origin'), '*')
|
||||||
|
|
||||||
|
def test_oembed_missing_url_returns_400(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed')
|
||||||
|
self.assertEqual(resp.status_code, 400)
|
||||||
|
|
||||||
|
def test_oembed_invalid_url_returns_400(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url=https://example.com')
|
||||||
|
self.assertEqual(resp.status_code, 400)
|
||||||
|
|
||||||
|
def test_oembed_supports_x_com_url(self):
|
||||||
|
x_url = 'https://x.com/elonmusk/status/1141367104702038016'
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={x_url}')
|
||||||
|
self.assertEqual(resp.status_code, 200)
|
||||||
|
|
||||||
|
def test_oembed_strips_query_params(self):
|
||||||
|
url_with_params = 'https://twitter.com/elonmusk/status/1141367104702038016?s=20&t=abc'
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={url_with_params}')
|
||||||
|
self.assertEqual(resp.status_code, 200)
|
||||||
|
data = resp.json()
|
||||||
|
self.assertIn('html', data)
|
||||||
|
|
||||||
|
def test_oembed_handles_trailing_slash(self):
|
||||||
|
url_with_slash = 'https://twitter.com/elonmusk/status/1141367104702038016/'
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={url_with_slash}')
|
||||||
|
self.assertEqual(resp.status_code, 200)
|
||||||
|
|
||||||
|
def test_oembed_handles_mobile_url(self):
|
||||||
|
mobile_url = 'https://mobile.twitter.com/elonmusk/status/1141367104702038016'
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={mobile_url}')
|
||||||
|
self.assertEqual(resp.status_code, 200)
|
||||||
|
|
||||||
|
def test_oembed_rejects_malformed_tweet_id(self):
|
||||||
|
bad_url = 'https://twitter.com/elonmusk/status/notanumber'
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={bad_url}')
|
||||||
|
self.assertEqual(resp.status_code, 400)
|
||||||
|
|
||||||
|
def test_oembed_maxwidth_param_accepted(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&maxwidth=400')
|
||||||
|
self.assertEqual(resp.status_code, 200)
|
||||||
|
|
||||||
|
def test_oembed_maxwidth_clamps_to_range(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&maxwidth=100')
|
||||||
|
data = resp.json()
|
||||||
|
self.assertEqual(data['width'], 220)
|
||||||
|
self.assertIn('max-width:220px', data['html'])
|
||||||
|
|
||||||
|
def test_oembed_maxwidth_caps_at_550(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&maxwidth=9999')
|
||||||
|
data = resp.json()
|
||||||
|
self.assertEqual(data['width'], 550)
|
||||||
|
|
||||||
|
def test_oembed_maxwidth_invalid_uses_default(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&maxwidth=abc')
|
||||||
|
data = resp.json()
|
||||||
|
self.assertEqual(data['width'], 550)
|
||||||
|
|
||||||
|
def test_oembed_author_url_present(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
|
||||||
|
data = resp.json()
|
||||||
|
self.assertIn('author_url', data)
|
||||||
|
self.assertIn('elonmusk', data['author_url'])
|
||||||
|
|
||||||
|
def test_oembed_accepts_nitter_url(self):
|
||||||
|
nitter_url = f'{self.base_url}/elonmusk/status/1141367104702038016'
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={nitter_url}')
|
||||||
|
self.assertEqual(resp.status_code, 200)
|
||||||
|
data = resp.json()
|
||||||
|
self.assertIn('html', data)
|
||||||
|
|
||||||
|
def test_oembed_format_json_accepted(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&format=json')
|
||||||
|
self.assertEqual(resp.status_code, 200)
|
||||||
|
|
||||||
|
def test_oembed_format_xml_returns_501(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}&format=xml')
|
||||||
|
self.assertEqual(resp.status_code, 501)
|
||||||
|
|
||||||
|
def test_oembed_has_title(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
|
||||||
|
data = resp.json()
|
||||||
|
self.assertIn('title', data)
|
||||||
|
self.assertIsInstance(data['title'], str)
|
||||||
|
self.assertGreater(len(data['title']), 0)
|
||||||
|
|
||||||
|
def test_oembed_has_null_height(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/api/oembed?url={self.tweet_url}')
|
||||||
|
data = resp.json()
|
||||||
|
self.assertIsNone(data['height'])
|
||||||
|
|
||||||
|
|
||||||
|
class OEmbedDiscoveryTest(BaseTestCase):
|
||||||
|
"""Test oEmbed discovery link tags on tweet pages."""
|
||||||
|
base_url = 'http://localhost:8080'
|
||||||
|
|
||||||
|
def test_tweet_page_has_oembed_link_tag(self):
|
||||||
|
self.open_nitter('elonmusk/status/1141367104702038016')
|
||||||
|
self.assert_element_present('link[type="application/json+oembed"]')
|
||||||
|
|
||||||
|
def test_oembed_link_tag_points_to_api(self):
|
||||||
|
resp = requests.get(f'{self.base_url}/elonmusk/status/1141367104702038016')
|
||||||
|
self.assertIn('application/json+oembed', resp.text)
|
||||||
|
self.assertIn('/api/oembed?url=', resp.text)
|
||||||
|
self.assertIn('1141367104702038016', resp.text)
|
||||||
|
|
||||||
|
def test_oembed_discovery_roundtrip(self):
|
||||||
|
"""Fetch a tweet page, extract oEmbed URL, call it, verify response."""
|
||||||
|
import re
|
||||||
|
resp = requests.get(f'{self.base_url}/elonmusk/status/1141367104702038016')
|
||||||
|
match = re.search(
|
||||||
|
r'type="application/json\+oembed"\s+href="([^"]*)"', resp.text)
|
||||||
|
self.assertIsNotNone(match, "No oEmbed discovery link found in page")
|
||||||
|
oembed_url = match.group(1).replace('&', '&')
|
||||||
|
oembed_resp = requests.get(oembed_url)
|
||||||
|
self.assertEqual(oembed_resp.status_code, 200)
|
||||||
|
data = oembed_resp.json()
|
||||||
|
self.assertEqual(data['type'], 'rich')
|
||||||
|
self.assertIn('html', data)
|
||||||
|
|
||||||
|
|
||||||
|
class VideoEmbedTest(BaseTestCase):
|
||||||
|
"""Test video embed route (/i/videos/tweet/{id})."""
|
||||||
|
video_tweet_id = '1078373829917974528'
|
||||||
|
|
||||||
|
def test_video_embed_has_video_element(self):
|
||||||
|
self.open_nitter(f'i/videos/tweet/{self.video_tweet_id}')
|
||||||
|
self.assert_element_visible('video')
|
||||||
|
|
||||||
|
def test_video_embed_has_poster(self):
|
||||||
|
self.open_nitter(f'i/videos/tweet/{self.video_tweet_id}')
|
||||||
|
poster = self.get_attribute('video', 'poster')
|
||||||
|
self.assertIsNotNone(poster)
|
||||||
|
self.assertIn('pic/', poster)
|
||||||
|
|
||||||
|
def test_video_embed_nonexistent_returns_error(self):
|
||||||
|
self.open_nitter('i/videos/tweet/1')
|
||||||
|
self.assert_element_visible('.error-embed')
|
||||||
37
tests/test_reply_sort.py
Normal file
37
tests/test_reply_sort.py
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
from parameterized import parameterized
|
||||||
|
|
||||||
|
from base import BaseTestCase, Conversation
|
||||||
|
|
||||||
|
sort_modes = [
|
||||||
|
['jack/status/20', 'Relevant'],
|
||||||
|
['jack/status/20?sort=relevance', 'Relevant'],
|
||||||
|
['jack/status/20?sort=recency', 'Recent'],
|
||||||
|
['jack/status/20?sort=likes', 'Liked'],
|
||||||
|
['jack/status/20?sort=garbage', 'Relevant'],
|
||||||
|
['jack/status/20?sort=%3Cscript%3E', 'Relevant'],
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class ReplySortTest(BaseTestCase):
|
||||||
|
@parameterized.expand(sort_modes)
|
||||||
|
def test_active_mode(self, page, expected_active):
|
||||||
|
self.open_nitter(page)
|
||||||
|
self.assert_element_visible(Conversation.reply_sort)
|
||||||
|
active = self.get_text(Conversation.reply_sort_active)
|
||||||
|
self.assert_equal(active.strip(), expected_active)
|
||||||
|
|
||||||
|
def test_all_three_options_present(self):
|
||||||
|
self.open_nitter('jack/status/20')
|
||||||
|
options = self.find_elements('.reply-sort-option')
|
||||||
|
labels = [o.text.strip() for o in options]
|
||||||
|
self.assert_equal(labels, ['Relevant', 'Recent', 'Liked'])
|
||||||
|
|
||||||
|
def test_option_links_carry_sort_param(self):
|
||||||
|
self.open_nitter('jack/status/20')
|
||||||
|
for slug in ['Relevance', 'Recency', 'Likes']:
|
||||||
|
self.assert_element(f'.reply-sort-option[href="?sort={slug}#r"]')
|
||||||
|
|
||||||
|
def test_load_more_preserves_sort(self):
|
||||||
|
self.open_nitter('jack/status/20?sort=Likes')
|
||||||
|
href = self.get_attribute('.replies .show-more a', 'href')
|
||||||
|
self.assert_true('sort=Likes' in href, f'sort missing from: {href}')
|
||||||
|
|
@ -1,9 +1,129 @@
|
||||||
from base import BaseTestCase
|
|
||||||
from parameterized import parameterized
|
from parameterized import parameterized
|
||||||
|
|
||||||
|
from base import BaseTestCase, Search
|
||||||
|
|
||||||
#class SearchTest(BaseTestCase):
|
# [url, expected active tab label]
|
||||||
#@parameterized.expand([['@mobile_test'], ['@mobile_test_2']])
|
active_tabs = [
|
||||||
#def test_username_search(self, username):
|
['search?f=tweets&q=nasa', 'Latest'],
|
||||||
#self.search_username(username)
|
['search?f=top&q=nasa', 'Top'],
|
||||||
#self.assert_text(f'{username}')
|
['search?f=media&q=nasa', 'Media'],
|
||||||
|
['search?f=users&q=nasa', 'Users'],
|
||||||
|
['search?f=lists&q=test', 'Lists'],
|
||||||
|
# unknown/hostile values fall back to Latest
|
||||||
|
['search?f=garbage&q=nasa', 'Latest'],
|
||||||
|
['search?f=%3Cscript%3E&q=nasa', 'Latest'],
|
||||||
|
# x.com URL compat: f=live/user/list (f=media/top match natively)
|
||||||
|
['search?f=live&q=nasa', 'Latest'],
|
||||||
|
['search?f=user&q=nasa', 'Users'],
|
||||||
|
['search?f=list&q=test', 'Lists'],
|
||||||
|
]
|
||||||
|
|
||||||
|
results_pages = [
|
||||||
|
['search?f=tweets&q=nasa'],
|
||||||
|
['search?f=top&q=nasa'],
|
||||||
|
['search?f=media&q=nasa'],
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class SearchProductTest(BaseTestCase):
|
||||||
|
@parameterized.expand(active_tabs)
|
||||||
|
def test_active_tab(self, page, expected_active):
|
||||||
|
self.open_nitter(page)
|
||||||
|
active = self.get_text(Search.tab_active)
|
||||||
|
self.assert_equal(active.strip(), expected_active)
|
||||||
|
|
||||||
|
def test_all_tabs_present(self):
|
||||||
|
self.open_nitter('search?f=tweets&q=nasa')
|
||||||
|
tabs = self.find_elements(Search.tab_item)
|
||||||
|
labels = [t.text.strip() for t in tabs]
|
||||||
|
self.assert_equal(labels, ['Top', 'Latest', 'Media', 'Users', 'Lists'])
|
||||||
|
|
||||||
|
@parameterized.expand(results_pages)
|
||||||
|
def test_results_render(self, page):
|
||||||
|
self.open_nitter(page)
|
||||||
|
self.assert_element('.timeline .timeline-item')
|
||||||
|
|
||||||
|
def test_tab_links_carry_kind(self):
|
||||||
|
self.open_nitter('search?f=tweets&q=nasa')
|
||||||
|
self.assert_element('.tab-item a[href="?f=top&q=nasa"]')
|
||||||
|
self.assert_element('.tab-item a[href="?f=media&q=nasa"]')
|
||||||
|
self.assert_element('.tab-item a[href="?f=tweets&q=nasa"]')
|
||||||
|
self.assert_element('.tab-item a[href="?f=users&q=nasa"]')
|
||||||
|
self.assert_element('.tab-item a[href="?f=lists&q=nasa"]')
|
||||||
|
|
||||||
|
def test_show_more_preserves_kind(self):
|
||||||
|
self.open_nitter('search?f=media&q=nasa')
|
||||||
|
href = self.get_attribute('.show-more a', 'href')
|
||||||
|
self.assert_true('f=media' in href, f'f=media missing from: {href}')
|
||||||
|
|
||||||
|
def test_search_form_preserves_kind(self):
|
||||||
|
self.open_nitter('search?f=top&q=nasa')
|
||||||
|
self.assert_element_present('.search-field input[name="f"][value="top"]')
|
||||||
|
|
||||||
|
def test_media_operators_compose(self):
|
||||||
|
self.open_nitter('search?f=media&q=nasa&e-nativeretweets=on')
|
||||||
|
self.assert_element('.timeline .timeline-item')
|
||||||
|
|
||||||
|
@parameterized.expand([['DAAC'], ['AB'], ['maxid:'], ['maxid:abc']])
|
||||||
|
def test_garbage_cursor_no_crash(self, cursor):
|
||||||
|
# short/invalid cursors must render the page, not a 500 error
|
||||||
|
self.open_nitter(f'search?f=media&q=nasa&cursor={cursor}')
|
||||||
|
self.assert_element(Search.tab_active)
|
||||||
|
|
||||||
|
def test_no_results(self):
|
||||||
|
self.open_nitter('search?f=media&q=xkqzjwv_no_results_2026')
|
||||||
|
self.assert_text('No items found', '.timeline-none')
|
||||||
|
|
||||||
|
def test_list_results_render(self):
|
||||||
|
self.open_nitter('search?f=lists&q=test')
|
||||||
|
self.assert_element('.timeline-item.list-result')
|
||||||
|
self.assert_element('.list-result .list-name')
|
||||||
|
self.assert_element('.list-result .list-members')
|
||||||
|
|
||||||
|
def test_list_card_links_to_list(self):
|
||||||
|
self.open_nitter('search?f=lists&q=test')
|
||||||
|
href = self.get_attribute('.list-result .list-name', 'href')
|
||||||
|
self.assert_true('/i/lists/' in href, f'unexpected list link: {href}')
|
||||||
|
|
||||||
|
def test_list_row_clickable(self):
|
||||||
|
self.open_nitter('search?f=lists&q=test')
|
||||||
|
href = self.get_attribute('.list-result a.tweet-link', 'href')
|
||||||
|
self.assert_true('/i/lists/' in href, f'unexpected row link: {href}')
|
||||||
|
|
||||||
|
def test_list_avatar_links_to_user(self):
|
||||||
|
self.open_nitter('search?f=lists&q=test')
|
||||||
|
# the avatar link in a row must point at the user named in that row
|
||||||
|
row = '.list-result:has(a.facepile-link)'
|
||||||
|
self.assert_element(f'{row} a.facepile-link > img')
|
||||||
|
href = self.get_attribute(f'{row} a.facepile-link', 'href')
|
||||||
|
ctx = self.get_text(f'{row} .list-result-context')
|
||||||
|
mentioned = ctx.split('@')[-1].strip()
|
||||||
|
self.assert_true(href.endswith('/' + mentioned),
|
||||||
|
f'avatar link {href} does not match @{mentioned}')
|
||||||
|
|
||||||
|
def test_list_pagination_preserves_kind(self):
|
||||||
|
self.open_nitter('search?f=lists&q=test')
|
||||||
|
href = self.get_attribute('.show-more a', 'href')
|
||||||
|
self.assert_true('f=lists' in href, f'f=lists missing from: {href}')
|
||||||
|
|
||||||
|
def test_list_garbage_cursor_no_crash(self):
|
||||||
|
self.open_nitter('search?f=lists&q=test&cursor=DAAC')
|
||||||
|
self.assert_element(Search.tab_active)
|
||||||
|
|
||||||
|
def test_media_view_tabs_present(self):
|
||||||
|
self.open_nitter('search?f=media&q=nasa')
|
||||||
|
tabs = self.find_elements('.media-view-tabs .tab-item')
|
||||||
|
labels = [t.text.strip() for t in tabs]
|
||||||
|
self.assert_equal(labels, ['Timeline', 'Grid', 'Gallery'])
|
||||||
|
|
||||||
|
def test_media_view_grid(self):
|
||||||
|
self.open_nitter('search?f=media&q=nasa&view=grid')
|
||||||
|
self.assert_element('.timeline.media-grid-view')
|
||||||
|
|
||||||
|
def test_media_view_gallery(self):
|
||||||
|
self.open_nitter('search?f=media&q=nasa&view=gallery')
|
||||||
|
self.assert_element('.timeline.media-gallery-view .gallery-masonry')
|
||||||
|
|
||||||
|
def test_media_view_tabs_only_on_media(self):
|
||||||
|
self.open_nitter('search?f=tweets&q=nasa')
|
||||||
|
self.assert_element_not_present('.media-view-tabs')
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ def curl_status(url):
|
||||||
"""Get HTTP status code using curl to avoid URL normalization by Python libs."""
|
"""Get HTTP status code using curl to avoid URL normalization by Python libs."""
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
['curl', '-s', '-o', '/dev/null', '-w', '%{http_code}', url],
|
['curl', '-s', '-o', '/dev/null', '-w', '%{http_code}', url],
|
||||||
capture_output=True, text=True, timeout=10
|
capture_output=True, text=True, timeout=30
|
||||||
)
|
)
|
||||||
return int(result.stdout)
|
return int(result.stdout)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,7 @@ class MediaTest(BaseTestCase):
|
||||||
@parameterized.expand(gallery)
|
@parameterized.expand(gallery)
|
||||||
def test_gallery(self, tweet, rows):
|
def test_gallery(self, tweet, rows):
|
||||||
self.open_nitter(tweet)
|
self.open_nitter(tweet)
|
||||||
|
self.scroll_to(Media.container)
|
||||||
self.assert_element_visible(Media.container)
|
self.assert_element_visible(Media.container)
|
||||||
self.assert_element_visible(Media.row)
|
self.assert_element_visible(Media.row)
|
||||||
self.assert_element_visible(Media.image)
|
self.assert_element_visible(Media.image)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue