mirror of
https://github.com/sartoopjj/thefeed.git
synced 2026-05-19 08:14:36 +03:00
1184 lines
45 KiB
HTML
1184 lines
45 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="fa">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>thefeed</title>
|
|
<style>
|
|
@font-face {
|
|
font-family: 'Vazirmatn';
|
|
src: url('/static/Vazirmatn-Regular.woff2') format('woff2');
|
|
font-weight: normal;
|
|
font-style: normal;
|
|
font-display: swap;
|
|
}
|
|
|
|
:root {
|
|
--bg: #0b1020;
|
|
--bg2: #131a32;
|
|
--surface: rgba(18, 26, 48, 0.85);
|
|
--surface2: rgba(30, 42, 74, 0.9);
|
|
--border: #2d3d69;
|
|
--accent: #f59e0b;
|
|
--accent-dim: #d97706;
|
|
--text: #edf2ff;
|
|
--text-dim: #9aa8cf;
|
|
--success: #22c55e;
|
|
--error: #ef4444;
|
|
--send-color: #0ea5e9;
|
|
}
|
|
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
|
|
body {
|
|
font-family: 'Vazirmatn', -apple-system, BlinkMacSystemFont, sans-serif;
|
|
background: radial-gradient(1200px 600px at 85% -10%, #213162 0%, transparent 55%), linear-gradient(165deg, var(--bg) 0%, var(--bg2) 100%);
|
|
color: var(--text);
|
|
height: 100vh;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.header {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
padding: 10px 16px;
|
|
background: rgba(14, 20, 38, 0.92);
|
|
backdrop-filter: blur(8px);
|
|
border-bottom: 1px solid var(--border);
|
|
height: 58px;
|
|
position: relative;
|
|
z-index: 20;
|
|
}
|
|
.header-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
|
|
.header h1 { font-size: 17px; font-weight: 700; letter-spacing: 0.3px; }
|
|
.header-channel-name {
|
|
display: none;
|
|
font-size: 13px;
|
|
color: var(--text-dim);
|
|
white-space: nowrap;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
max-width: 160px;
|
|
}
|
|
.header-actions { display: flex; gap: 8px; align-items: center; }
|
|
.next-fetch-timer { font-size: 11px; color: var(--text-dim); }
|
|
|
|
.mobile-menu-btn {
|
|
display: none;
|
|
width: 38px;
|
|
height: 38px;
|
|
border-radius: 10px;
|
|
border: 1px solid var(--border);
|
|
background: var(--surface2);
|
|
color: var(--text);
|
|
font-size: 18px;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.status-dot {
|
|
width: 8px; height: 8px;
|
|
border-radius: 50%;
|
|
display: inline-block;
|
|
}
|
|
.status-dot.connected { background: var(--success); }
|
|
.status-dot.disconnected { background: var(--error); }
|
|
|
|
.btn {
|
|
padding: 6px 14px;
|
|
border: 1px solid var(--border);
|
|
border-radius: 10px;
|
|
background: var(--surface2);
|
|
color: var(--text);
|
|
cursor: pointer;
|
|
font-family: inherit;
|
|
font-size: 13px;
|
|
transition: background 0.15s, transform 0.12s;
|
|
}
|
|
.btn:hover { background: var(--border); }
|
|
.btn:active { transform: translateY(1px); }
|
|
.btn-primary { background: var(--accent); border-color: var(--accent); }
|
|
.btn-primary:hover { background: var(--accent-dim); }
|
|
|
|
.main {
|
|
display: flex;
|
|
height: calc(100vh - 58px);
|
|
}
|
|
|
|
.sidebar {
|
|
width: 260px;
|
|
min-width: 260px;
|
|
background: rgba(10, 16, 31, 0.82);
|
|
border-right: 1px solid var(--border);
|
|
display: flex;
|
|
flex-direction: column;
|
|
overflow-y: auto;
|
|
}
|
|
.sidebar-title {
|
|
padding: 12px 16px 8px;
|
|
font-size: 12px;
|
|
text-transform: uppercase;
|
|
color: var(--text-dim);
|
|
letter-spacing: 1px;
|
|
}
|
|
.channel-item {
|
|
padding: 12px 16px;
|
|
cursor: pointer;
|
|
border-left: 3px solid transparent;
|
|
transition: all 0.15s;
|
|
font-size: 14px;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
}
|
|
.channel-item:hover { background: var(--surface2); }
|
|
.channel-item.active {
|
|
background: var(--surface2);
|
|
border-left-color: var(--accent);
|
|
color: white;
|
|
}
|
|
.channel-type-badge {
|
|
font-size: 10px;
|
|
padding: 1px 5px;
|
|
border-radius: 3px;
|
|
background: var(--surface2);
|
|
color: var(--text-dim);
|
|
margin-left: 4px;
|
|
}
|
|
.channel-type-badge.private { background: #3b1f4e; color: #c084fc; }
|
|
.new-msg-badge {
|
|
background: var(--accent);
|
|
color: white;
|
|
font-size: 10px;
|
|
padding: 1px 6px;
|
|
border-radius: 10px;
|
|
min-width: 18px;
|
|
text-align: center;
|
|
}
|
|
|
|
.content {
|
|
flex: 1;
|
|
display: flex;
|
|
flex-direction: column;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.messages {
|
|
flex: 1;
|
|
overflow-y: auto;
|
|
padding: 18px;
|
|
direction: rtl;
|
|
text-align: right;
|
|
}
|
|
.message {
|
|
margin-bottom: 16px;
|
|
padding: 12px 14px;
|
|
background: var(--surface);
|
|
border-radius: 12px;
|
|
border: 1px solid var(--border);
|
|
box-shadow: 0 6px 20px rgba(5, 10, 22, 0.2);
|
|
max-width: 920px;
|
|
}
|
|
.message-header {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
margin-bottom: 8px;
|
|
font-size: 12px;
|
|
color: var(--text-dim);
|
|
direction: ltr;
|
|
}
|
|
.message-text {
|
|
line-height: 1.8;
|
|
white-space: pre-wrap;
|
|
word-break: break-word;
|
|
}
|
|
.media-tag {
|
|
display: inline-block;
|
|
padding: 2px 8px;
|
|
border-radius: 4px;
|
|
background: var(--surface2);
|
|
color: var(--text-dim);
|
|
font-size: 12px;
|
|
margin-bottom: 4px;
|
|
}
|
|
|
|
.send-panel {
|
|
display: none;
|
|
padding: 10px 12px;
|
|
background: var(--surface);
|
|
border-top: 1px solid var(--border);
|
|
direction: rtl;
|
|
}
|
|
.send-panel.visible { display: flex; gap: 8px; align-items: center; }
|
|
.send-input {
|
|
flex: 1;
|
|
padding: 8px 12px;
|
|
border: 1px solid var(--border);
|
|
border-radius: 6px;
|
|
background: var(--bg);
|
|
color: var(--text);
|
|
font-family: inherit;
|
|
font-size: 14px;
|
|
direction: rtl;
|
|
}
|
|
.send-btn {
|
|
padding: 8px 18px;
|
|
border: none;
|
|
border-radius: 10px;
|
|
background: var(--send-color);
|
|
color: white;
|
|
cursor: pointer;
|
|
font-family: inherit;
|
|
font-size: 13px;
|
|
}
|
|
.send-btn:hover { background: #0284c7; }
|
|
|
|
.progress-panel {
|
|
height: 56px;
|
|
min-height: 56px;
|
|
background: rgba(14, 20, 38, 0.95);
|
|
border-top: 1px solid var(--border);
|
|
border-bottom: 1px solid var(--border);
|
|
overflow-y: auto;
|
|
font-size: 12px;
|
|
font-family: 'Vazirmatn', monospace;
|
|
padding: 12px 12px;
|
|
direction: ltr;
|
|
text-align: left;
|
|
color: var(--text-dim);
|
|
}
|
|
.progress-item {
|
|
margin-bottom: 8px;
|
|
padding: 6px;
|
|
border-radius: 4px;
|
|
background: var(--bg);
|
|
}
|
|
.progress-label {
|
|
font-size: 11px;
|
|
margin-bottom: 3px;
|
|
color: var(--text-dim);
|
|
}
|
|
.progress-bar {
|
|
width: 100%;
|
|
height: 6px;
|
|
background: var(--border);
|
|
border-radius: 3px;
|
|
overflow: hidden;
|
|
}
|
|
.progress-fill {
|
|
height: 100%;
|
|
background: var(--accent);
|
|
transition: width 0.2s;
|
|
}
|
|
.progress-fill.send { background: var(--send-color); }
|
|
|
|
.log-panel {
|
|
height: 150px;
|
|
min-height: 100px;
|
|
background: rgba(14, 20, 38, 0.95);
|
|
border-top: 1px solid var(--border);
|
|
overflow-y: auto;
|
|
font-size: 12px;
|
|
font-family: monospace;
|
|
padding: 8px 12px;
|
|
direction: ltr;
|
|
text-align: left;
|
|
color: var(--text-dim);
|
|
}
|
|
.log-line {
|
|
padding: 2px 0;
|
|
line-height: 1.3;
|
|
}
|
|
.log-line.info { color: #60a5fa; }
|
|
.log-line.progress { color: #fbbf24; }
|
|
.log-line.error { color: #ef4444; }
|
|
.log-line.success { color: #22c55e; }
|
|
.log-line.warning { color: #f97316; }
|
|
|
|
.modal-overlay {
|
|
display: none;
|
|
position: fixed;
|
|
inset: 0;
|
|
background: rgba(0,0,0,0.7);
|
|
z-index: 100;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
.modal-overlay.active { display: flex; }
|
|
.modal {
|
|
background: var(--surface);
|
|
border: 1px solid var(--border);
|
|
border-radius: 12px;
|
|
padding: 24px;
|
|
width: 440px;
|
|
max-width: 95vw;
|
|
max-height: 90vh;
|
|
overflow-y: auto;
|
|
}
|
|
.modal h2 { margin-bottom: 20px; font-size: 18px; }
|
|
.form-group { margin-bottom: 16px; }
|
|
.form-group label {
|
|
display: block;
|
|
margin-bottom: 4px;
|
|
font-size: 13px;
|
|
color: var(--text-dim);
|
|
}
|
|
.form-group input,
|
|
.form-group textarea,
|
|
.form-group select {
|
|
width: 100%;
|
|
padding: 8px 12px;
|
|
border: 1px solid var(--border);
|
|
border-radius: 6px;
|
|
background: var(--bg);
|
|
color: var(--text);
|
|
font-family: inherit;
|
|
font-size: 14px;
|
|
direction: ltr;
|
|
text-align: left;
|
|
}
|
|
.form-group textarea { min-height: 80px; resize: vertical; }
|
|
|
|
.empty-state {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
height: 100%;
|
|
color: var(--text-dim);
|
|
gap: 16px;
|
|
}
|
|
.empty-state p { font-size: 14px; }
|
|
|
|
.footer-link {
|
|
padding: 12px 16px;
|
|
text-align: center;
|
|
font-size: 11px;
|
|
color: var(--text-dim);
|
|
border-top: 1px solid var(--border);
|
|
margin-top: auto;
|
|
}
|
|
.footer-link a { color: var(--accent); text-decoration: none; }
|
|
.footer-link a:hover { text-decoration: underline; }
|
|
.footer-link .free-iran { color: var(--success); font-weight: bold; }
|
|
|
|
.sidebar-overlay {
|
|
display: none;
|
|
position: fixed;
|
|
inset: 58px 0 0 0;
|
|
background: rgba(2, 6, 20, 0.55);
|
|
z-index: 9;
|
|
}
|
|
|
|
@media (max-width: 900px) {
|
|
.mobile-menu-btn { display: inline-flex; align-items: center; justify-content: center; }
|
|
.header {
|
|
padding: 10px 12px;
|
|
}
|
|
.header-left {
|
|
gap: 8px;
|
|
min-width: 0;
|
|
}
|
|
.header h1 {
|
|
font-size: 15px;
|
|
}
|
|
.next-fetch-timer {
|
|
display: none;
|
|
}
|
|
.header-channel-name {
|
|
display: block;
|
|
}
|
|
.header h1 {
|
|
flex-shrink: 0;
|
|
}
|
|
.header-actions .btn {
|
|
padding: 6px 10px;
|
|
font-size: 12px;
|
|
}
|
|
.sidebar {
|
|
position: fixed;
|
|
top: 58px;
|
|
bottom: 0;
|
|
right: 0;
|
|
width: min(82vw, 300px);
|
|
min-width: 0;
|
|
z-index: 10;
|
|
transform: translateX(100%);
|
|
transition: transform 0.22s ease;
|
|
border-left: 1px solid var(--border);
|
|
border-right: none;
|
|
}
|
|
.main.sidebar-open .sidebar {
|
|
transform: translateX(0);
|
|
}
|
|
.main.sidebar-open + .sidebar-overlay {
|
|
display: block;
|
|
}
|
|
.messages {
|
|
padding: 12px;
|
|
}
|
|
.message {
|
|
margin-bottom: 12px;
|
|
border-radius: 10px;
|
|
}
|
|
.progress-panel {
|
|
height: 52px;
|
|
min-height: 52px;
|
|
padding: 10px;
|
|
}
|
|
.log-panel {
|
|
height: 120px;
|
|
min-height: 90px;
|
|
padding: 8px 10px;
|
|
}
|
|
.modal {
|
|
width: 100%;
|
|
max-width: 100%;
|
|
height: 100%;
|
|
max-height: 100%;
|
|
border-radius: 0;
|
|
padding: 16px;
|
|
}
|
|
}
|
|
|
|
@media (max-width: 520px) {
|
|
.send-panel.visible {
|
|
flex-direction: column;
|
|
align-items: stretch;
|
|
}
|
|
.send-btn {
|
|
width: 100%;
|
|
}
|
|
.header-actions {
|
|
gap: 6px;
|
|
}
|
|
.header-actions .btn {
|
|
padding: 5px 8px;
|
|
font-size: 11px;
|
|
}
|
|
}
|
|
|
|
.tg-warning {
|
|
background: #3b1f1f;
|
|
border: 1px solid #7f1d1d;
|
|
border-radius: 6px;
|
|
padding: 8px 12px;
|
|
font-size: 12px;
|
|
color: #fca5a5;
|
|
margin-bottom: 16px;
|
|
display: none;
|
|
}
|
|
.tg-warning.visible { display: block; }
|
|
|
|
::-webkit-scrollbar { width: 6px; }
|
|
::-webkit-scrollbar-track { background: transparent; }
|
|
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
|
|
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="header">
|
|
<div class="header-left">
|
|
<button class="mobile-menu-btn" onclick="toggleSidebar()" aria-label="Open channels">☰</button>
|
|
<h1>thefeed</h1>
|
|
<span class="header-channel-name" id="headerChannelName"></span>
|
|
<span class="status-dot disconnected" id="statusDot"></span>
|
|
<span class="next-fetch-timer" id="nextFetchTimer"></span>
|
|
</div>
|
|
<div class="header-actions">
|
|
<button class="btn" onclick="doRefresh()">Refresh</button>
|
|
<button class="btn" onclick="openSettings()">Settings</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="main">
|
|
<div class="sidebar">
|
|
<div class="sidebar-title" style="display:flex;justify-content:space-between;align-items:center;">Channels
|
|
<button class="btn" style="padding:2px 8px;font-size:12px;" onclick="openChannelManager()" title="Manage channels">✎</button>
|
|
</div>
|
|
<div id="channelList">
|
|
<div style="padding:16px;color:var(--text-dim);font-size:13px;">No channels</div>
|
|
</div>
|
|
<div class="footer-link">
|
|
<a href="https://github.com/sartoopjj/thefeed" target="_blank">GitHub</a>
|
|
· <span class="free-iran">For FREE IRAN</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="content">
|
|
<div class="messages" id="messages">
|
|
<div class="empty-state" id="emptyState">
|
|
<p>No messages yet</p>
|
|
<button class="btn btn-primary" onclick="openSettings()">Configure</button>
|
|
</div>
|
|
</div>
|
|
<div class="send-panel" id="sendPanel">
|
|
<input type="text" class="send-input" id="sendInput" placeholder="Type a message..." maxlength="4000">
|
|
<button class="send-btn" onclick="sendMessage()">Send</button>
|
|
</div>
|
|
<div class="progress-panel" id="progressPanel"></div>
|
|
<div class="log-panel" id="logPanel"></div>
|
|
</div>
|
|
</div>
|
|
<div class="sidebar-overlay" onclick="toggleSidebar(true)"></div>
|
|
|
|
<div class="modal-overlay" id="channelModal">
|
|
<div class="modal">
|
|
<h2>Manage Channels</h2>
|
|
<div class="form-group">
|
|
<label>Add Channel (username without @)</label>
|
|
<div style="display:flex;gap:8px;">
|
|
<input type="text" id="addChannelInput" placeholder="channel_username">
|
|
<button class="btn btn-primary" onclick="addChannel()">Add</button>
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Current Channels</label>
|
|
<div id="channelManagerList" style="max-height:200px;overflow-y:auto;"></div>
|
|
</div>
|
|
<div id="channelManagerError" style="color:var(--error);margin-top:8px;font-size:13px;display:none;"></div>
|
|
<div style="display:flex;gap:8px;justify-content:flex-end;margin-top:20px;">
|
|
<button class="btn" onclick="closeChannelManager()">Close</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="modal-overlay" id="settingsModal">
|
|
<div class="modal">
|
|
<h2>Settings</h2>
|
|
<div id="tgWarning" class="tg-warning">
|
|
Server is running without Telegram login. Some features (sending messages, private chats) are unavailable.
|
|
To enable, restart the server with Telegram credentials.
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Domain</label>
|
|
<input type="text" id="cfgDomain" placeholder="t.example.com">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Passphrase</label>
|
|
<input type="password" id="cfgKey" placeholder="Encryption passphrase">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Resolvers (one per line)</label>
|
|
<textarea id="cfgResolvers" placeholder="8.8.8.8 1.1.1.1"></textarea>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Query Mode</label>
|
|
<select id="cfgQueryMode">
|
|
<option value="single">Single label (base32, stealthier)</option>
|
|
<option value="double">Multi-label (hex)</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Rate Limit (queries/sec, 0 = unlimited)</label>
|
|
<input type="number" id="cfgRateLimit" value="5" min="0" step="0.1">
|
|
</div>
|
|
<div class="form-group" style="flex-direction:row;align-items:center;gap:10px;">
|
|
<input type="checkbox" id="cfgDebug" checked style="width:auto;margin:0;">
|
|
<label for="cfgDebug" style="margin:0;cursor:pointer;">Debug mode (log generated query names)</label>
|
|
</div>
|
|
<div style="display:flex;gap:8px;justify-content:flex-end;margin-top:20px;">
|
|
<button class="btn" onclick="closeSettings()">Cancel</button>
|
|
<button class="btn btn-primary" onclick="saveConfig()">Save & Connect</button>
|
|
</div>
|
|
<div id="settingsError" style="color:var(--error);margin-top:12px;font-size:13px;display:none;"></div>
|
|
<div style="margin-top:20px;padding-top:16px;border-top:1px solid var(--border);text-align:center;">
|
|
<a href="https://github.com/sartoopjj/thefeed" target="_blank" style="color:var(--accent);font-size:12px;text-decoration:none;">github.com/sartoopjj/thefeed</a>
|
|
<div style="margin-top:6px;font-size:12px;" class="free-iran">For FREE IRAN</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
var selectedChannel = 0;
|
|
var channels = [];
|
|
var eventSource = null;
|
|
var autoRefreshTimer = null;
|
|
var telegramLoggedIn = false;
|
|
|
|
var serverNextFetch = 0;
|
|
var nextFetchInterval = null;
|
|
var previousMsgIDs = {};
|
|
var newMsgCount = 0;
|
|
var originalTitle = document.title;
|
|
|
|
function toggleSidebar(forceClose) {
|
|
var main = document.querySelector('.main');
|
|
if (!main) return;
|
|
if (forceClose) {
|
|
main.classList.remove('sidebar-open');
|
|
return;
|
|
}
|
|
main.classList.toggle('sidebar-open');
|
|
}
|
|
|
|
function getFirstVisibleChannel() {
|
|
if (channels.length > 0) return 1;
|
|
return 0;
|
|
}
|
|
|
|
async function init() {
|
|
try {
|
|
var resp = await fetch('/api/status');
|
|
var status = await resp.json();
|
|
if (!status.configured) {
|
|
openSettings();
|
|
} else {
|
|
document.getElementById('statusDot').className = 'status-dot connected';
|
|
telegramLoggedIn = !!status.telegramLoggedIn;
|
|
serverNextFetch = status.nextFetch || 0;
|
|
updateNextFetchDisplay();
|
|
await loadChannels();
|
|
if (channels && channels.length > 0) {
|
|
var firstVisible = getFirstVisibleChannel();
|
|
if (firstVisible > 0) {
|
|
await selectChannel(firstVisible);
|
|
}
|
|
} else {
|
|
showInitProgress();
|
|
await doRefresh();
|
|
}
|
|
startAutoRefresh();
|
|
}
|
|
} catch (e) {}
|
|
connectSSE();
|
|
}
|
|
|
|
function showInitProgress() {
|
|
var panel = document.getElementById('progressPanel');
|
|
var item = document.createElement('div');
|
|
item.id = 'progress-current';
|
|
item.className = 'progress-item';
|
|
item.innerHTML = '<div class="progress-label">Starting to get metadata...</div>' +
|
|
'<div class="progress-bar"><div class="progress-fill" style="width:0%"></div></div>';
|
|
panel.appendChild(item);
|
|
}
|
|
|
|
function startAutoRefresh() {
|
|
if (autoRefreshTimer) return;
|
|
autoRefreshTimer = setInterval(function() {
|
|
if (selectedChannel > 0) {
|
|
doRefresh(true);
|
|
}
|
|
}, 600000);
|
|
}
|
|
|
|
function updateNextFetchDisplay() {
|
|
if (nextFetchInterval) clearInterval(nextFetchInterval);
|
|
if (!serverNextFetch) {
|
|
document.getElementById('nextFetchTimer').textContent = '';
|
|
return;
|
|
}
|
|
function tick() {
|
|
var now = Math.floor(Date.now() / 1000);
|
|
var diff = serverNextFetch - now;
|
|
if (diff <= 0) {
|
|
document.getElementById('nextFetchTimer').textContent = 'Server refreshing...';
|
|
return;
|
|
}
|
|
var m = Math.floor(diff / 60);
|
|
var s = diff % 60;
|
|
document.getElementById('nextFetchTimer').textContent = 'Server refresh in ' + m + ':' + (s < 10 ? '0' : '') + s;
|
|
}
|
|
tick();
|
|
nextFetchInterval = setInterval(tick, 1000);
|
|
}
|
|
|
|
function connectSSE() {
|
|
if (eventSource) eventSource.close();
|
|
eventSource = new EventSource('/api/events');
|
|
eventSource.addEventListener('log', function(e) {
|
|
addLogLine(JSON.parse(e.data));
|
|
});
|
|
eventSource.addEventListener('update', async function(e) {
|
|
var wasEmpty = channels.length === 0;
|
|
var oldIDs = {};
|
|
for (var i = 0; i < channels.length; i++) {
|
|
oldIDs[i + 1] = channels[i].LastMsgID || channels[i].lastMsgID || 0;
|
|
}
|
|
await loadChannels();
|
|
var hasNew = false;
|
|
for (var i = 0; i < channels.length; i++) {
|
|
var newID = channels[i].LastMsgID || channels[i].lastMsgID || 0;
|
|
var oldID = oldIDs[i + 1] || 0;
|
|
if (oldID > 0 && newID > oldID && (i + 1) !== selectedChannel) {
|
|
hasNew = true;
|
|
}
|
|
}
|
|
if (hasNew) {
|
|
newMsgCount++;
|
|
document.title = '(' + newMsgCount + ') ' + originalTitle;
|
|
}
|
|
if (wasEmpty && channels.length > 0 && selectedChannel === 0) {
|
|
var firstVisible = getFirstVisibleChannel();
|
|
if (firstVisible > 0) {
|
|
selectChannel(firstVisible);
|
|
}
|
|
} else if (selectedChannel > 0) {
|
|
await loadMessages(selectedChannel);
|
|
updateSendPanel();
|
|
}
|
|
});
|
|
eventSource.onerror = function() {
|
|
document.getElementById('statusDot').className = 'status-dot disconnected';
|
|
if (eventSource.readyState === EventSource.CLOSED) {
|
|
eventSource.close();
|
|
setTimeout(connectSSE, 3000);
|
|
}
|
|
};
|
|
eventSource.onopen = function() {
|
|
document.getElementById('statusDot').className = 'status-dot connected';
|
|
};
|
|
}
|
|
|
|
function renderChannels() {
|
|
var el = document.getElementById('channelList');
|
|
if (!channels || channels.length === 0) {
|
|
el.innerHTML = '<div style="padding:16px;color:var(--text-dim);font-size:13px;">No channels</div>';
|
|
return;
|
|
}
|
|
var html = '';
|
|
var privates = [];
|
|
var publics = [];
|
|
for (var i = 0; i < channels.length; i++) {
|
|
var ch = channels[i];
|
|
var ct = ch.ChatType || ch.chatType || 0;
|
|
var entry = {ch: ch, idx: i};
|
|
if (ct === 1) privates.push(entry);
|
|
else publics.push(entry);
|
|
}
|
|
|
|
function renderSection(title, items) {
|
|
if (items.length === 0) return '';
|
|
var h = '';
|
|
if (title) h += '<div class="sidebar-title" style="padding-top:8px;">' + title + '</div>';
|
|
for (var j = 0; j < items.length; j++) {
|
|
var e = items[j];
|
|
var num = e.idx + 1;
|
|
var active = num === selectedChannel ? ' active' : '';
|
|
var name = e.ch.Name || e.ch.name || 'Channel ' + num;
|
|
var ct2 = e.ch.ChatType || e.ch.chatType || 0;
|
|
var badge = '';
|
|
if (ct2 === 1) badge = '<span class="channel-type-badge private">Private</span>';
|
|
var newBadge = '';
|
|
var lastID = e.ch.LastMsgID || e.ch.lastMsgID || 0;
|
|
var prevID = previousMsgIDs[num] || 0;
|
|
if (prevID > 0 && lastID > prevID) {
|
|
newBadge = '<span class="new-msg-badge">NEW</span>';
|
|
}
|
|
h += '<div class="channel-item' + active + '" onclick="selectChannel(' + num + ')">' +
|
|
'<span>' + escapeHtml(name) + badge + '</span>' +
|
|
newBadge + '</div>';
|
|
}
|
|
return h;
|
|
}
|
|
|
|
html += renderSection('', publics);
|
|
html += renderSection('Private', privates);
|
|
el.innerHTML = html;
|
|
}
|
|
|
|
async function selectChannel(num) {
|
|
selectedChannel = num;
|
|
newMsgCount = 0;
|
|
document.title = originalTitle;
|
|
toggleSidebar(true);
|
|
var chName = (channels[num - 1] && (channels[num - 1].Name || channels[num - 1].name)) || '';
|
|
var nameEl = document.getElementById('headerChannelName');
|
|
if (nameEl) nameEl.textContent = chName ? ('· ' + chName) : '';
|
|
renderChannels();
|
|
updateSendPanel();
|
|
document.getElementById('messages').innerHTML = '<div class="empty-state"><p>Loading...</p></div>';
|
|
|
|
var panel = document.getElementById('progressPanel');
|
|
var item = document.getElementById('progress-current');
|
|
if (!item) {
|
|
item = document.createElement('div');
|
|
item.id = 'progress-current';
|
|
item.className = 'progress-item';
|
|
panel.appendChild(item);
|
|
}
|
|
var chName = (channels[num - 1] && (channels[num - 1].Name || channels[num - 1].name)) || 'Channel ' + num;
|
|
item.innerHTML = '<div class="progress-label">' + escapeHtml(chName) + '</div>' +
|
|
'<div class="progress-bar"><div class="progress-fill" style="width:0%"></div></div>';
|
|
|
|
await loadMessages(num);
|
|
await doRefresh(true);
|
|
}
|
|
|
|
function updateSendPanel() {
|
|
var panel = document.getElementById('sendPanel');
|
|
var ch = channels[selectedChannel - 1];
|
|
var canSend = !!(ch && (ch.CanSend || ch.canSend));
|
|
if (selectedChannel > 0 && telegramLoggedIn && canSend) {
|
|
panel.classList.add('visible');
|
|
} else {
|
|
panel.classList.remove('visible');
|
|
}
|
|
}
|
|
|
|
async function loadChannels() {
|
|
try {
|
|
var resp = await fetch('/api/channels');
|
|
channels = await resp.json();
|
|
if (!channels) channels = [];
|
|
renderChannels();
|
|
updateSendPanel();
|
|
var sr = await fetch('/api/status');
|
|
var st = await sr.json();
|
|
telegramLoggedIn = !!st.telegramLoggedIn;
|
|
if (st.nextFetch) {
|
|
serverNextFetch = st.nextFetch;
|
|
updateNextFetchDisplay();
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
async function loadMessages(chNum) {
|
|
try {
|
|
var resp = await fetch('/api/messages/' + chNum);
|
|
if (chNum !== selectedChannel) return;
|
|
var msgs = await resp.json();
|
|
if (chNum !== selectedChannel) return;
|
|
renderMessages(msgs);
|
|
if (channels[chNum - 1]) {
|
|
previousMsgIDs[chNum] = channels[chNum - 1].LastMsgID || channels[chNum - 1].lastMsgID || 0;
|
|
renderChannels();
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
function renderMessages(msgs) {
|
|
var el = document.getElementById('messages');
|
|
if (!msgs || msgs.length === 0) {
|
|
el.innerHTML = '<div class="empty-state"><p>No messages in this channel</p></div>';
|
|
return;
|
|
}
|
|
var html = '';
|
|
for (var i = 0; i < msgs.length; i++) {
|
|
var msg = msgs[i];
|
|
var ts = new Date((msg.Timestamp || msg.timestamp) * 1000);
|
|
var timeStr = ts.toLocaleTimeString('fa-IR', {hour: '2-digit', minute: '2-digit'});
|
|
var dateStr = ts.toLocaleDateString('fa-IR', {month: 'short', day: 'numeric'});
|
|
var id = msg.ID || msg.id;
|
|
var text = msg.Text || msg.text || '';
|
|
var mediaHtml = '';
|
|
var textHtml = escapeHtml(text);
|
|
var mediaTypes = ['[IMAGE]', '[VIDEO]', '[FILE]', '[AUDIO]', '[STICKER]', '[GIF]', '[POLL]', '[CONTACT]', '[LOCATION]'];
|
|
for (var m = 0; m < mediaTypes.length; m++) {
|
|
if (text.indexOf(mediaTypes[m]) === 0) {
|
|
mediaHtml = '<div class="media-tag">' + mediaTypes[m] + '</div>';
|
|
textHtml = escapeHtml(text.substring(mediaTypes[m].length).replace(/^\n/, ''));
|
|
break;
|
|
}
|
|
}
|
|
html += '<div class="message">' +
|
|
'<div class="message-header">' +
|
|
'<span>#' + id + '</span>' +
|
|
'<span>' + timeStr + ' ' + dateStr + '</span>' +
|
|
'</div>' +
|
|
mediaHtml +
|
|
'<div class="message-text">' + textHtml + '</div>' +
|
|
'</div>';
|
|
}
|
|
el.innerHTML = html;
|
|
}
|
|
|
|
function addLogLine(line) {
|
|
var el = document.getElementById('logPanel');
|
|
var div = document.createElement('div');
|
|
div.className = 'log-line';
|
|
var level = 'info';
|
|
if (typeof line === 'string') {
|
|
if (line.includes('Invalid passphrase')) {
|
|
level = 'error';
|
|
} else if (line.includes('Error:') || line.includes('error') || line.includes('Error')) {
|
|
level = 'error';
|
|
} else if (line.includes('Warning:') || line.includes('warning') || line.includes('Warning')) {
|
|
level = 'warning';
|
|
} else if (line.includes('OK:') || line.includes('OK') || line.includes('success') || line.includes('done')) {
|
|
level = 'success';
|
|
} else if (line.match(/\[\d+%\]|\d+\s*%/)) {
|
|
level = 'progress';
|
|
updateProgressDisplay(line);
|
|
return;
|
|
} else if (line.includes('no new messages')) {
|
|
level = 'info';
|
|
}
|
|
}
|
|
div.className = 'log-line ' + level;
|
|
div.textContent = line;
|
|
el.appendChild(div);
|
|
el.scrollTop = el.scrollHeight;
|
|
while (el.children.length > 200) {
|
|
el.removeChild(el.firstChild);
|
|
}
|
|
}
|
|
|
|
function updateProgressDisplay(line) {
|
|
var match = line.match(/Channel\s+(\d+)\s*(?:\((\d+\/\d+)\))?/);
|
|
if (!match) return;
|
|
var channelNum = match[1];
|
|
var counts = match[2] || '';
|
|
var percentMatch = line.match(/(\d+)%/);
|
|
var percent = percentMatch ? parseInt(percentMatch[1]) : 0;
|
|
var panel = document.getElementById('progressPanel');
|
|
var itemId = 'progress-current';
|
|
var item = document.getElementById(itemId);
|
|
var label = 'Channel ' + channelNum;
|
|
if (counts) label += ' (' + counts + ')';
|
|
if (!item) {
|
|
item = document.createElement('div');
|
|
item.id = itemId;
|
|
item.className = 'progress-item';
|
|
item.innerHTML = '<div class="progress-label">' + label + '</div>' +
|
|
'<div class="progress-bar"><div class="progress-fill" style="width:0%"></div></div>';
|
|
panel.appendChild(item);
|
|
} else {
|
|
item.querySelector('.progress-label').textContent = label;
|
|
}
|
|
var fill = item.querySelector('.progress-fill');
|
|
fill.style.width = percent + '%';
|
|
if (percent >= 100) {
|
|
setTimeout(function() {
|
|
if (item.parentNode) item.parentNode.removeChild(item);
|
|
}, 1000);
|
|
}
|
|
}
|
|
|
|
async function doRefresh(quiet) {
|
|
try {
|
|
var url = '/api/refresh';
|
|
if (selectedChannel > 0) {
|
|
url += '?channel=' + selectedChannel;
|
|
}
|
|
if (quiet) {
|
|
url += (url.includes('?') ? '&' : '?') + 'quiet=1';
|
|
}
|
|
await fetch(url, {method: 'POST'});
|
|
// For manual refresh, reload messages after delay as fallback
|
|
if (!quiet && selectedChannel > 0) {
|
|
setTimeout(function() {
|
|
loadChannels();
|
|
loadMessages(selectedChannel);
|
|
}, 3000);
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
async function sendMessage() {
|
|
var input = document.getElementById('sendInput');
|
|
var text = input.value.trim();
|
|
if (!text || !selectedChannel) return;
|
|
try {
|
|
var resp = await fetch('/api/send', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({channel: selectedChannel, text: text})
|
|
});
|
|
if (resp.ok) {
|
|
input.value = '';
|
|
addLogLine('Message sent successfully');
|
|
} else {
|
|
var errText = await resp.text();
|
|
addLogLine('Error sending message: ' + errText);
|
|
}
|
|
} catch (e) {
|
|
addLogLine('Error: ' + e.message);
|
|
}
|
|
}
|
|
|
|
function openSettings() {
|
|
document.getElementById('settingsModal').classList.add('active');
|
|
document.getElementById('tgWarning').classList.toggle('visible', !telegramLoggedIn && channels.length > 0);
|
|
fetch('/api/config').then(function(r) { return r.json(); }).then(function(cfg) {
|
|
if (cfg.domain) document.getElementById('cfgDomain').value = cfg.domain;
|
|
if (cfg.key) document.getElementById('cfgKey').value = cfg.key;
|
|
if (cfg.resolvers) document.getElementById('cfgResolvers').value = cfg.resolvers.join('\n');
|
|
if (cfg.queryMode) document.getElementById('cfgQueryMode').value = cfg.queryMode;
|
|
if (typeof cfg.rateLimit === 'number') {
|
|
document.getElementById('cfgRateLimit').value = cfg.rateLimit;
|
|
} else {
|
|
document.getElementById('cfgRateLimit').value = 5;
|
|
}
|
|
document.getElementById('cfgDebug').checked = cfg.debug !== undefined ? !!cfg.debug : true;
|
|
}).catch(function() {});
|
|
}
|
|
|
|
function closeSettings() {
|
|
document.getElementById('settingsModal').classList.remove('active');
|
|
document.getElementById('settingsError').style.display = 'none';
|
|
}
|
|
|
|
async function saveConfig() {
|
|
var errEl = document.getElementById('settingsError');
|
|
errEl.style.display = 'none';
|
|
var saveBtn = document.querySelector('#settingsModal .btn-primary');
|
|
if (saveBtn && saveBtn.disabled) return;
|
|
if (saveBtn) { saveBtn.disabled = true; saveBtn.textContent = 'Connecting...'; }
|
|
var resolversText = document.getElementById('cfgResolvers').value.trim();
|
|
var resolvers = resolversText.split(/[\n,]+/).map(function(s) { return s.trim(); }).filter(function(s) { return s; });
|
|
var cfg = {
|
|
domain: document.getElementById('cfgDomain').value.trim(),
|
|
key: document.getElementById('cfgKey').value,
|
|
resolvers: resolvers,
|
|
queryMode: document.getElementById('cfgQueryMode').value,
|
|
rateLimit: parseFloat(document.getElementById('cfgRateLimit').value) || 5,
|
|
debug: document.getElementById('cfgDebug').checked
|
|
};
|
|
if (!cfg.domain || !cfg.key || resolvers.length === 0) {
|
|
errEl.textContent = 'Domain, passphrase, and at least one resolver are required.';
|
|
errEl.style.display = 'block';
|
|
return;
|
|
}
|
|
try {
|
|
var resp = await fetch('/api/config', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify(cfg)
|
|
});
|
|
if (!resp.ok) {
|
|
var text = await resp.text();
|
|
throw new Error(text);
|
|
}
|
|
closeSettings();
|
|
document.getElementById('statusDot').className = 'status-dot connected';
|
|
showInitProgress();
|
|
await doRefresh();
|
|
await loadChannels();
|
|
if (channels && channels.length > 0) {
|
|
var firstVisible = getFirstVisibleChannel();
|
|
if (firstVisible > 0) {
|
|
await selectChannel(firstVisible);
|
|
}
|
|
}
|
|
startAutoRefresh();
|
|
} catch (e) {
|
|
errEl.textContent = e.message;
|
|
errEl.style.display = 'block';
|
|
} finally {
|
|
if (saveBtn) { saveBtn.disabled = false; saveBtn.textContent = 'Save \u0026 Connect'; }
|
|
}
|
|
}
|
|
|
|
function escapeHtml(s) {
|
|
var div = document.createElement('div');
|
|
div.appendChild(document.createTextNode(s));
|
|
return div.innerHTML;
|
|
}
|
|
|
|
function openChannelManager() {
|
|
document.getElementById('channelModal').classList.add('active');
|
|
document.getElementById('channelManagerError').style.display = 'none';
|
|
loadChannelManagerList();
|
|
}
|
|
|
|
function closeChannelManager() {
|
|
document.getElementById('channelModal').classList.remove('active');
|
|
}
|
|
|
|
async function loadChannelManagerList() {
|
|
var el = document.getElementById('channelManagerList');
|
|
el.innerHTML = '<div style="color:var(--text-dim);font-size:13px;">Loading...</div>';
|
|
try {
|
|
var resp = await fetch('/api/admin', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({command: 'list_channels', arg: ''})
|
|
});
|
|
if (!resp.ok) {
|
|
var errorText = await resp.text();
|
|
throw new Error(errorText || 'admin command failed');
|
|
}
|
|
var data = await resp.json();
|
|
var result = data.result || '';
|
|
var chans = result.split('\n').filter(function(s) { return s.trim(); });
|
|
if (chans.length === 0) {
|
|
el.innerHTML = '<div style="color:var(--text-dim);font-size:13px;">No channels configured on server.</div>';
|
|
return;
|
|
}
|
|
var html = '';
|
|
for (var i = 0; i < chans.length; i++) {
|
|
html += '<div style="display:flex;justify-content:space-between;align-items:center;padding:6px 0;border-bottom:1px solid var(--border);">' +
|
|
'<span style="font-size:13px;">' + escapeHtml(chans[i]) + '</span>' +
|
|
'<button class="btn" style="padding:2px 8px;font-size:11px;color:var(--error);" onclick="removeChannel(\'' + escapeHtml(chans[i]).replace(/'/g, "\\'") + '\')">Remove</button>' +
|
|
'</div>';
|
|
}
|
|
el.innerHTML = html;
|
|
} catch (e) {
|
|
el.innerHTML = '<div style="color:var(--error);font-size:13px;">Error: ' + escapeHtml(e.message) + '</div>';
|
|
}
|
|
}
|
|
|
|
async function addChannel() {
|
|
var input = document.getElementById('addChannelInput');
|
|
var username = input.value.trim().replace(/^@/, '');
|
|
var errEl = document.getElementById('channelManagerError');
|
|
errEl.style.display = 'none';
|
|
if (!username) return;
|
|
try {
|
|
var resp = await fetch('/api/admin', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({command: 'add_channel', arg: username})
|
|
});
|
|
if (!resp.ok) {
|
|
errEl.textContent = await resp.text() || 'Failed to add channel';
|
|
errEl.style.display = 'block';
|
|
return;
|
|
}
|
|
await resp.json();
|
|
input.value = '';
|
|
addLogLine('Channel added: ' + username);
|
|
loadChannelManagerList();
|
|
} catch (e) {
|
|
errEl.textContent = e.message;
|
|
errEl.style.display = 'block';
|
|
}
|
|
}
|
|
|
|
async function removeChannel(username) {
|
|
var errEl = document.getElementById('channelManagerError');
|
|
errEl.style.display = 'none';
|
|
try {
|
|
var resp = await fetch('/api/admin', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({command: 'remove_channel', arg: username})
|
|
});
|
|
if (!resp.ok) {
|
|
errEl.textContent = await resp.text() || 'Failed to remove channel';
|
|
errEl.style.display = 'block';
|
|
return;
|
|
}
|
|
await resp.json();
|
|
addLogLine('Channel removed: ' + username);
|
|
loadChannelManagerList();
|
|
} catch (e) {
|
|
errEl.textContent = e.message;
|
|
errEl.style.display = 'block';
|
|
}
|
|
}
|
|
|
|
document.addEventListener('keydown', function(e) {
|
|
if (e.key === 'Enter' && document.activeElement === document.getElementById('sendInput')) {
|
|
e.preventDefault();
|
|
sendMessage();
|
|
}
|
|
if (e.key === 'Enter' && document.activeElement === document.getElementById('addChannelInput')) {
|
|
e.preventDefault();
|
|
addChannel();
|
|
}
|
|
});
|
|
|
|
window.addEventListener('resize', function() {
|
|
if (window.innerWidth > 900) {
|
|
toggleSidebar(true);
|
|
}
|
|
});
|
|
|
|
init();
|
|
</script>
|
|
</body>
|
|
</html>
|