Documentation
¶
Overview ¶
Package telemirror is an optional, removable backup feed. It fetches public Telegram channel widgets through Google Translate's public web-page proxy so users can browse public channels even when Telegram itself is blocked. The implementation is intentionally self-contained so the feature can be removed by deleting this package and the matching handlers in internal/web/telemirror.go.
Index ¶
- Constants
- Variables
- func IsDefault(username string) bool
- func ParseHTML(htmlBody string) (*Channel, []Post, error)
- func SanitizeUsername(s string) string
- type Cache
- type Channel
- type Client
- type FetchResult
- type Forward
- type ImageCache
- func (c *ImageCache) Clear()
- func (c *ImageCache) Get(rawURL string) ([]byte, string, bool)
- func (c *ImageCache) GetByKey(key string) ([]byte, string, bool)
- func (c *ImageCache) Put(rawURL, contentType string, body []byte) error
- func (c *ImageCache) PutByKey(key, contentType string, body []byte) error
- func (c *ImageCache) SetMaxBytes(n int64)
- func (c *ImageCache) Size() int64
- type Media
- type Post
- type Reaction
- type Reply
- type Store
Constants ¶
const ( FreshTTL = 1 * time.Minute StaleTTL = 24 * time.Hour )
Cache TTLs. FreshTTL is intentionally short so opening a channel a second time triggers a background refresh — without it, a "live" feed feels frozen for the first 10 minutes after every fetch.
const ImageStaleTTL = 30 * 24 * time.Hour
Variables ¶
var ( ErrEmptyUsername = errors.New("empty username") // ErrChannelNotFound means the username has no public web preview — it // doesn't exist, was renamed, or is private. The proxy signals this by // redirecting the /s/ request instead of serving the 200 widget. ErrChannelNotFound = errors.New("telemirror: channel not found or not available") )
Sentinel errors returned by Store.
var DefaultChannels = []string{"VahidOnline", "networkti", "thefeedconfig"}
DefaultChannels are listed first on a fresh install. Removable — a removal is recorded in the store's Hidden set.
Functions ¶
func ParseHTML ¶
ParseHTML extracts channel info and recent posts from a t.me/s/ widget. The HTML is the rendered output of Google Translate's proxy, which keeps the original Telegram class names but rewrites cross-domain URLs onto translate.goog so the browser can load them through the same proxy.
func SanitizeUsername ¶
SanitizeUsername strips @ / t.me/ prefixes and rejects characters not allowed by Telegram's username rules. Returns "" if the result is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a per-channel disk cache backed by a small in-memory map.
type Channel ¶
type Channel struct {
Username string `json:"username"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Photo string `json:"photo,omitempty"`
Subscribers string `json:"subscribers,omitempty"`
}
Channel describes the public channel header.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client mirrors HttpClient from teleMirror's http-client.js.
func (*Client) FetchHTMLBefore ¶ added in v0.17.4
func (c *Client) FetchHTMLBefore(ctx context.Context, username string, beforeID int) (string, error)
FetchHTMLBefore fetches the widget filtered to posts strictly older than beforeID. Used for "Load older" pagination.
func (*Client) FetchURL ¶
FetchURL fetches an arbitrary URL through the same fronting/uTLS attempts used for the channel widget, but with the request Host set to the URL's actual host (not proxyHost) so Google's edge routes it to the right backend. Returns the bytes plus the upstream Content-Type so the caller can stream it back to the browser.
Used to proxy translate.goog-rewritten image URLs (channel avatars, post photos/videos) without each one having to go through DNS.
type FetchResult ¶
type FetchResult struct {
Channel Channel `json:"channel"`
Posts []Post `json:"posts"`
FetchedAt time.Time `json:"fetchedAt"`
}
FetchResult is what we cache per channel.
type ImageCache ¶ added in v0.16.1
type ImageCache struct {
// contains filtered or unexported fields
}
ImageCache stores image bytes on disk under <dir>/<key>.bin with a <key>.json metadata sidecar. Use Put/Get for URL-hash keys or PutByKey/GetByKey for stable identifiers like channel usernames.
func NewImageCache ¶ added in v0.16.1
func NewImageCache(dir string) *ImageCache
func (*ImageCache) Clear ¶ added in v0.16.1
func (c *ImageCache) Clear()
func (*ImageCache) Get ¶ added in v0.16.1
func (c *ImageCache) Get(rawURL string) ([]byte, string, bool)
func (*ImageCache) GetByKey ¶ added in v0.16.1
func (c *ImageCache) GetByKey(key string) ([]byte, string, bool)
func (*ImageCache) Put ¶ added in v0.16.1
func (c *ImageCache) Put(rawURL, contentType string, body []byte) error
func (*ImageCache) PutByKey ¶ added in v0.16.1
func (c *ImageCache) PutByKey(key, contentType string, body []byte) error
func (*ImageCache) SetMaxBytes ¶ added in v0.27.0
func (c *ImageCache) SetMaxBytes(n int64)
SetMaxBytes sets the byte budget (0 = unlimited) and enforces immediately.
func (*ImageCache) Size ¶ added in v0.27.0
func (c *ImageCache) Size() int64
Size returns the total bytes on disk (.bin + .json). Scans once if unknown.
type Media ¶
type Media struct {
Type string `json:"type"` // photo | video | voice | audio | document | sticker | poll
URL string `json:"url,omitempty"`
Thumb string `json:"thumb,omitempty"`
Ratio float64 `json:"ratio,omitempty"` // width/height; lets the UI reserve the exact box (no scroll jump)
Duration string `json:"duration,omitempty"`
Title string `json:"title,omitempty"` // file name / poll question / audio title
Subtitle string `json:"subtitle,omitempty"`
Options []string `json:"options,omitempty"` // poll options
}
Media is one attachment on a post.
type Post ¶
type Post struct {
ID string `json:"id"` // "<channel>/<msgid>"
Author string `json:"author,omitempty"`
Text string `json:"text,omitempty"` // sanitised inner HTML
Reply *Reply `json:"reply,omitempty"`
Forward *Forward `json:"forward,omitempty"`
Media []Media `json:"media,omitempty"`
Reactions []Reaction `json:"reactions,omitempty"`
Time time.Time `json:"time,omitempty"`
Views string `json:"views,omitempty"`
Edited bool `json:"edited,omitempty"`
}
Post is a single message from the channel feed.
type Reply ¶ added in v0.16.0
type Reply struct {
Author string `json:"author,omitempty"`
Text string `json:"text,omitempty"` // inner HTML of the snippet
URL string `json:"url,omitempty"` // link to the replied-to post
}
Reply is the quoted preview shown above a reply.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store persists the user-added channel list. Defaults are listed first, and stay there unless the user removes one — List() re-adds them on every call, so a removal has to be recorded explicitly in Hidden.
func (*Store) List ¶
List returns the full channel list, defaults first, minus any the user removed.