-
Notifications
You must be signed in to change notification settings - Fork 3
feat: restore Truth Social support #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| export interface TruthSocialAccount { | ||
| display_name: string; | ||
| username: string; | ||
| url: string; | ||
| avatar: string; | ||
| } | ||
|
|
||
| export interface TruthSocialMedia { | ||
| type: string; | ||
| url: string; | ||
| preview_url?: string; | ||
| description?: string; | ||
| } | ||
|
|
||
| export interface TruthSocialStatus { | ||
| id: string; | ||
| created_at: string; | ||
| url: string; | ||
| content: string; | ||
| account: TruthSocialAccount; | ||
| replies_count: number; | ||
| reblogs_count: number; | ||
| favourites_count: number; | ||
| media_attachments: TruthSocialMedia[]; | ||
| quote?: TruthSocialStatus | null; | ||
| in_reply_to?: TruthSocialStatus | null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import he from "he"; | ||
|
|
||
| import type { Platform } from "../types"; | ||
| import type { TruthSocialStatus } from "./truth-social.d"; | ||
|
|
||
| const MATCH_RE = | ||
| /^(?:https?:\/\/)?(?:www\.)?truthsocial\.com\/@[^/?]+\/(?:posts\/)?(\d+)\/?(?:[?#].*)?$/; | ||
| const MAX_CONTEXT_DEPTH = 1; | ||
|
|
||
| function stripHtml(html: string) { | ||
| return he.decode( | ||
| html | ||
| .replace(/<br\s*\/?>/gi, "\n") | ||
| .replace(/<\/p>\s*<p>/gi, "\n\n") | ||
| .replace(/<[^>]+>/g, ""), | ||
| ); | ||
| } | ||
|
|
||
| export const TruthSocial: Platform<"TruthSocial", TruthSocialStatus, {}> = { | ||
| type: "TruthSocial", | ||
| async match(url) { | ||
| return url.match(MATCH_RE)?.[1] ?? null; | ||
| }, | ||
| async fetch(id, env) { | ||
| if (!/^\d+$/.test(id)) { | ||
| throw { code: 400, message: "Invalid Truth Social status ID" }; | ||
| } | ||
| if (!env?.TRUTH_SOCIAL_ACCESS_TOKEN) { | ||
| throw { code: 500, message: "TRUTH_SOCIAL_ACCESS_TOKEN is required" }; | ||
| } | ||
|
|
||
| const response = await fetch(`https://truthsocial.com/api/v1/statuses/${id}`, { | ||
| headers: { | ||
| Authorization: `Bearer ${env.TRUTH_SOCIAL_ACCESS_TOKEN}`, | ||
| Accept: "application/json", | ||
| "User-Agent": env.EMBED_USER_AGENT, | ||
| }, | ||
| }); | ||
|
|
||
| if (!response.ok) throw { code: response.status, message: response.statusText }; | ||
|
|
||
| // SAFETY: Truth Social's status endpoint follows its observed Mastodon-compatible shape. | ||
| return (await response.json()) as TruthSocialStatus; | ||
| }, | ||
| async transform(raw, options) { | ||
| const depth = options?.depth ?? 0; | ||
| const includeContext = depth < MAX_CONTEXT_DEPTH; | ||
| const text = stripHtml(raw.content); | ||
|
|
||
| return { | ||
| platform: this.type, | ||
| author: { | ||
| name: raw.account.display_name, | ||
| handle: raw.account.username, | ||
| url: raw.account.url, | ||
| avatar: raw.account.avatar, | ||
| }, | ||
| url: raw.url, | ||
| text: text || undefined, | ||
| timestamp: Math.floor(Date.parse(raw.created_at) / 1000), | ||
| stats: { | ||
| comments: raw.replies_count, | ||
| reposts: raw.reblogs_count, | ||
| likes: raw.favourites_count, | ||
| }, | ||
| media: raw.media_attachments.map((media) => ({ | ||
| url: media.url, | ||
| type: media.type, | ||
| description: media.description, | ||
| })), | ||
| quote: | ||
| includeContext && raw.quote | ||
| ? await this.transform(raw.quote, { depth: depth + 1 }) | ||
| : undefined, | ||
| reply_to: | ||
| includeContext && raw.in_reply_to | ||
| ? await this.transform(raw.in_reply_to, { depth: depth + 1 }) | ||
| : undefined, | ||
| }; | ||
| }, | ||
| } as const; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Truth Social posts now reach this footer, but
getEmojiByName(post.platform)looks up aTruthSocialapplication emoji while noTruthSocial.pngasset is synchronized. This supplies an undefined ID toformatEmoji, leaving every Truth Social embed with an invalid or visibly broken platform emoji reference. Add the corresponding emoji asset or provide a deliberate fallback.Prompt To Fix With AI