﷽
اولین کامیت پروژه پایاچت
This commit is contained in:
@@ -0,0 +1,302 @@
|
||||
{% extends "base.html" %}
|
||||
{% load static %}
|
||||
|
||||
{% block title %}{{ room.name }} - پایاچت{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<div class="app">
|
||||
|
||||
<aside class="sidebar mobile-hide">
|
||||
<div class="sidebar-header">
|
||||
<div style="display: flex; align-items: center; gap: 10px;">
|
||||
<img src="{% static 'icons/logo.svg' %}" alt="پایاچت"
|
||||
style="width: 28px; height: 28px; border-radius: 6px;">
|
||||
<span>پایاچت</span>
|
||||
</div>
|
||||
</div>
|
||||
<ul class="room-list">
|
||||
{% for r in rooms %}
|
||||
<li>
|
||||
<a class="room-item {% if r.id == room.id %}active{% endif %}"
|
||||
href="{% url 'room' r.id %}">
|
||||
<div class="room-item-avatar">
|
||||
{% if r.display_avatar %}
|
||||
<img src="{{ r.display_avatar }}" alt="{{ r.display_name }}">
|
||||
{% else %}
|
||||
{{ r.display_name|first }}
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="room-item-info">
|
||||
<span class="room-item-name">
|
||||
{{ r.display_name }}
|
||||
{% include "includes/_verified_badge.html" with is_verified=r.display_verified %}
|
||||
</span>
|
||||
<span class="room-item-meta">
|
||||
{% if r.room_type == 'channel' %}کانال
|
||||
{% elif r.room_type == 'private' %}چت خصوصی
|
||||
{% else %}گروه · {{ r.members.count }} عضو{% endif %}
|
||||
</span>
|
||||
</div>
|
||||
{% if r.unread_count %}
|
||||
<span class="unread-bubble">
|
||||
{% if r.unread_count > 99 %}+99{% else %}{{ r.unread_count }}{% endif %}
|
||||
</span>
|
||||
{% endif %}
|
||||
</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</aside>
|
||||
|
||||
<main class="chat mobile-show">
|
||||
<header class="chat-header">
|
||||
{% if other_user %}
|
||||
<a href="{% url 'user_profile' other_user.id %}" class="chat-header-link"></a>
|
||||
{% else %}
|
||||
<a href="{% url 'room_info' room.id %}" class="chat-header-link"></a>
|
||||
{% endif %}
|
||||
|
||||
<a class="back" href="{% url 'index' %}" title="بازگشت">→</a>
|
||||
|
||||
<div class="chat-avatar">
|
||||
{% if other_user %}
|
||||
{% if other_user.avatar %}<img src="{{ other_user.avatar.url }}" alt="{{ other_user.first_name }}">{% else %}{{ other_user.first_name|first }}{% endif %}
|
||||
{% elif room.avatar %}
|
||||
<img src="{{ room.avatar.url }}" alt="{{ room.name }}">
|
||||
{% else %}
|
||||
{{ room.name|first }}
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<div class="chat-header-info">
|
||||
<div class="chat-header-title">
|
||||
{% if other_user %}
|
||||
{{ other_user.first_name }}
|
||||
{% include "includes/_verified_badge.html" with is_verified=other_user.is_verified %}
|
||||
{% else %}
|
||||
{{ room.name }}
|
||||
{% include "includes/_verified_badge.html" with is_verified=room.is_verified %}
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="chat-header-meta">{% if not other_user %}{{ room.members.count }} عضو{% endif %}</div>
|
||||
</div>
|
||||
|
||||
<span style="flex:1"></span>
|
||||
|
||||
<a href="{% url 'settings' %}" style="font-size:1.2rem;text-decoration:none;color:var(--neon);position:relative;z-index:2;" title="تنظیمات">⚙️</a>
|
||||
</header>
|
||||
|
||||
<div id="messages" class="messages"></div>
|
||||
|
||||
|
||||
<div id="empty-chat" class="chat-empty" style="display: none;">
|
||||
<div class="chat-empty-icon">💬</div>
|
||||
<div class="chat-empty-text">اینجا پیامت رو بنویس...</div>
|
||||
</div>
|
||||
|
||||
<div class="composer">
|
||||
<input id="input" type="text" placeholder="پیامت را بنویس..." autocomplete="off">
|
||||
<button id="send">ارسال</button>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block extra_js %}
|
||||
<script>
|
||||
const myID = {{ request.user.id }};
|
||||
const messagesEl = document.getElementById('messages');
|
||||
const emptyChatEl = document.getElementById('empty-chat');
|
||||
const inputEl = document.getElementById('input');
|
||||
const ROOM_TYPE = "{{ room.room_type }}";
|
||||
// ===== متغیرهای لود بینهایت =====
|
||||
let oldestId = null; // قدیمیترین پیام لود شده
|
||||
let hasMore = true; // آیا پیام قدیمیتری هست؟
|
||||
let isLoadingMore = false; // آیا الان درخواست فرستادیم؟
|
||||
let firstLoadDone = false; // آیا لود اولیه تمام شد؟
|
||||
|
||||
let socket;
|
||||
|
||||
function connect() {
|
||||
const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
socket = new WebSocket(`${protocol}//${location.host}/ws/chat/{{ room.id }}/`);
|
||||
|
||||
socket.onmessage = (e) => {
|
||||
const data = JSON.parse(e.data);
|
||||
|
||||
if (data.action === 'history') {
|
||||
if (data.append) {
|
||||
addOldMessages(data.messages); // پیامهای قدیمی
|
||||
} else {
|
||||
showFirstMessages(data.messages); // پیامهای اول
|
||||
}
|
||||
} else {
|
||||
// پیام جدید
|
||||
show(data.id, data.text, data.author, data.author_name,
|
||||
data.author_avatar, data.created_at, data.author_verified);
|
||||
messagesEl.scrollTop = messagesEl.scrollHeight;
|
||||
}
|
||||
};
|
||||
|
||||
socket.onclose = () => setTimeout(connect, 2000);
|
||||
}
|
||||
|
||||
function show(id, text, author, author_name, author_avatar, created_at, author_verified, prepend = false) {
|
||||
const isMine = author === myID;
|
||||
const showMeta = ROOM_TYPE === 'group' && !isMine;
|
||||
const url = `/accounts/u/${author}/`;
|
||||
|
||||
// ۱. ساخت پوسته پیام
|
||||
const wrap = document.createElement('div');
|
||||
wrap.className = 'msg-wrap' + (isMine ? ' me' : '');
|
||||
wrap.dataset.id = id;
|
||||
|
||||
// ۲. آواتار (فقط برای گروه و پیام دیگران)
|
||||
if (showMeta) {
|
||||
const avatar = document.createElement('a');
|
||||
avatar.className = 'msg-avatar';
|
||||
avatar.href = url;
|
||||
if (author_avatar) {
|
||||
avatar.innerHTML = `<img src="${author_avatar}">`;
|
||||
} else {
|
||||
avatar.textContent = (author_name || '?')[0];
|
||||
}
|
||||
wrap.appendChild(avatar);
|
||||
}
|
||||
|
||||
// ۳. ساخت حباب پیام
|
||||
const bubble = document.createElement('div');
|
||||
bubble.className = 'msg' + (isMine ? ' me' : ''); // isMe درست شد
|
||||
bubble.textContent = text;
|
||||
|
||||
// ۴. اضافه کردن اسم و تیک آبی (فقط برای گروه و پیام دیگران)
|
||||
if (showMeta) {
|
||||
// ترتیب prepend برعکس است، پس از آخر به اول اضافه میکنیم
|
||||
bubble.prepend(': '); // [ : ][متن]
|
||||
|
||||
const name = document.createElement('a');
|
||||
name.href = url;
|
||||
name.className = 'msg-author-link';
|
||||
name.textContent = author_name;
|
||||
bubble.prepend(name); // [اسم][ : ][متن]
|
||||
|
||||
if (author_verified) {
|
||||
const badge = document.createElement('span');
|
||||
badge.className = 'verified-pro ' + (isMine ? 'verified-white' : 'verified-blue');
|
||||
badge.innerHTML = '<svg viewBox="0 0 24 24"><path d="M10.007 2.10377C8.60544 1.65006 7.08181 2.28116 6.41156 3.59306L5.60578 5.17023C5.51004 5.35763 5.35763 5.51004 5.17023 5.60578L3.59306 6.41156C2.28116 7.08181 1.65006 8.60544 2.10377 10.007L2.64923 11.692C2.71404 11.8922 2.71404 12.1078 2.64923 12.308L2.10377 13.993C1.65006 15.3946 2.28116 16.9182 3.59306 17.5885L5.17023 18.3942C5.35763 18.49 5.51004 18.6424 5.60578 18.8298L6.41156 20.407C7.08181 21.7189 8.60544 22.35 10.007 21.8963L11.692 21.3508C11.8922 21.286 12.1078 21.286 12.308 21.3508L13.993 21.8963C15.3946 22.35 16.9182 21.7189 17.5885 20.407L18.3942 18.8298C18.49 18.6424 18.6424 18.49 18.8298 18.3942L20.407 17.5885C21.7189 16.9182 22.35 15.3946 21.8963 13.993L21.3508 12.308C21.286 12.1078 21.286 11.8922 21.3508 11.692L21.8963 10.007C22.35 8.60544 21.7189 7.08181 20.407 6.41156L18.8298 5.60578C18.6424 5.51004 18.49 5.35763 18.3942 5.17023L17.5885 3.59306C16.9182 2.28116 15.3946 1.65006 13.993 2.10377L12.308 2.64923C12.1078 2.71403 11.8922 2.71404 11.692 2.64923L10.007 2.10377ZM6.75977 11.7573L8.17399 10.343L11.0024 13.1715L16.6593 7.51465L18.0735 8.92886L11.0024 15.9999L6.75977 11.7573Z"/></svg>';
|
||||
bubble.prepend(badge); // [تیک][اسم][ : ][متن]
|
||||
}
|
||||
}
|
||||
|
||||
// ۵. ساعت ارسال
|
||||
if (created_at) {
|
||||
const date = new Date(created_at);
|
||||
const timeStr = date.toLocaleTimeString('fa-IR', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
const timeEl = document.createElement('span');
|
||||
timeEl.className = 'msg-time';
|
||||
timeEl.textContent = timeStr;
|
||||
bubble.appendChild(timeEl); // [تیک][اسم][ : ][متن][ساعت]
|
||||
}
|
||||
|
||||
// ۶. اتصال به صفحه
|
||||
wrap.appendChild(bubble);
|
||||
// ← این بلاک کلیدی است
|
||||
if (prepend) {
|
||||
messagesEl.insertBefore(wrap, messagesEl.firstChild); // بالا
|
||||
} else {
|
||||
wrap.classList.add('msg-new');
|
||||
messagesEl.appendChild(wrap); // پایین
|
||||
messagesEl.scrollTop = messagesEl.scrollHeight; // اسکرول به پایین
|
||||
}
|
||||
|
||||
emptyChatEl.style.display = 'none';
|
||||
}
|
||||
|
||||
function send() {
|
||||
const text = inputEl.value.trim();
|
||||
if (!text || socket.readyState !== WebSocket.OPEN) return;
|
||||
socket.send(JSON.stringify({ type_event: 'message', text: text }));
|
||||
inputEl.value = '';
|
||||
}
|
||||
|
||||
function showFirstMessages(messages) {
|
||||
messagesEl.innerHTML = '';
|
||||
|
||||
messages.forEach(m => show(
|
||||
m.id, m.text, m.author, m.author_name,
|
||||
m.author_avatar, m.created_at, m.author_verified
|
||||
));
|
||||
|
||||
// ذخیرهی id قدیمیترین پیام
|
||||
if (messages.length > 0) {
|
||||
oldestId = messages[0].id;
|
||||
} else {
|
||||
hasMore = false;
|
||||
}
|
||||
|
||||
emptyChatEl.style.display = messages.length ? 'none' : 'block';
|
||||
|
||||
// اسکرول به پایین
|
||||
messagesEl.scrollTop = messagesEl.scrollHeight;
|
||||
|
||||
// بعد از ۱۰۰ms، قفل را باز کن
|
||||
setTimeout(() => {
|
||||
messagesEl.scrollTop = messagesEl.scrollHeight;
|
||||
firstLoadDone = true;
|
||||
}, 100);
|
||||
}
|
||||
|
||||
function addOldMessages(messages) {
|
||||
// اگر هیچ پیامی نبود → تمام
|
||||
if (messages.length === 0) {
|
||||
hasMore = false;
|
||||
isLoadingMore = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// ارتفاع قبل از اضافه کردن
|
||||
const heightBefore = messagesEl.scrollHeight;
|
||||
|
||||
// پیامهای قدیمی را به بالا اضافه کن (reverse چون prepend میکنیم)
|
||||
messages.slice().reverse().forEach(m => show(
|
||||
m.id, m.text, m.author, m.author_name,
|
||||
m.author_avatar, m.created_at, m.author_verified,
|
||||
true // ← prepend = true (بالا)
|
||||
));
|
||||
|
||||
// حفظ موقعیت اسکرول
|
||||
requestAnimationFrame(() => {
|
||||
const heightAfter = messagesEl.scrollHeight;
|
||||
messagesEl.scrollTop += (heightAfter - heightBefore);
|
||||
|
||||
// بهروزرسانی
|
||||
oldestId = messages[0].id;
|
||||
isLoadingMore = false;
|
||||
});
|
||||
}
|
||||
|
||||
document.getElementById('send').onclick = send;
|
||||
inputEl.onkeydown = (e) => { if (e.key === 'Enter') send(); };
|
||||
|
||||
connect();
|
||||
|
||||
messagesEl.addEventListener('scroll', () => {
|
||||
// ۴ قفل، به ترتیب
|
||||
if (!firstLoadDone) return; // لود اولیه تمام نشده
|
||||
if (isLoadingMore) return; // درخواست در جریان است
|
||||
if (!hasMore) return; // پیام قدیمیتری نیست
|
||||
if (messagesEl.scrollTop > 50) return; // به بالا نرسیده
|
||||
|
||||
// همهی شرطها درست → درخواست بفرست
|
||||
isLoadingMore = true;
|
||||
socket.send(JSON.stringify({
|
||||
type_event: 'fetch_more',
|
||||
before_id: oldestId,
|
||||
}));
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user