Update chat UI auth visibility, default global tab, and fast polling
This commit is contained in:
@@ -56,6 +56,10 @@ var notifications: Array = []
|
||||
# ── Private ───────────────────────────────────────────────────────────────────
|
||||
|
||||
var _overlay: CanvasLayer = null
|
||||
var _message_poll_timer: Timer = null
|
||||
var _message_poll_in_flight: Dictionary = {}
|
||||
|
||||
const MESSAGE_POLL_INTERVAL_SEC := 0.2
|
||||
|
||||
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -63,6 +67,7 @@ func _ready() -> void:
|
||||
Auth.auth_success.connect(_on_auth_success)
|
||||
Auth.session_expired.connect(_clear_state)
|
||||
Network.message_received.connect(_on_server_push)
|
||||
_setup_message_polling()
|
||||
# Defer adding the overlay so the scene tree is fully ready.
|
||||
call_deferred("_add_overlay")
|
||||
|
||||
@@ -73,18 +78,26 @@ func _add_overlay() -> void:
|
||||
push_error("Chat: could not load res://scenes/chat/ChatOverlay.tscn")
|
||||
return
|
||||
_overlay = scene.instantiate() as CanvasLayer
|
||||
# Chat UI should only be visible for authenticated users.
|
||||
_overlay.visible = false
|
||||
get_tree().get_root().add_child(_overlay)
|
||||
|
||||
# ── Auth hooks ────────────────────────────────────────────────────────────────
|
||||
|
||||
func _on_auth_success(_uid: String, _dn: String) -> void:
|
||||
if is_instance_valid(_overlay):
|
||||
_overlay.visible = true
|
||||
_fetch_social()
|
||||
_fetch_channels()
|
||||
_fetch_notifications()
|
||||
_set_own_presence("Online")
|
||||
_start_message_polling()
|
||||
|
||||
|
||||
func _clear_state() -> void:
|
||||
if is_instance_valid(_overlay):
|
||||
_overlay.visible = false
|
||||
_stop_message_polling()
|
||||
channels = []
|
||||
messages = {}
|
||||
friends = []
|
||||
@@ -92,6 +105,7 @@ func _clear_state() -> void:
|
||||
blocked_users = []
|
||||
presence = {}
|
||||
notifications = []
|
||||
_message_poll_in_flight.clear()
|
||||
channels_updated.emit()
|
||||
social_updated.emit()
|
||||
|
||||
@@ -228,12 +242,89 @@ func _fetch_notifications() -> void:
|
||||
)
|
||||
|
||||
|
||||
func _setup_message_polling() -> void:
|
||||
_message_poll_timer = Timer.new()
|
||||
_message_poll_timer.wait_time = MESSAGE_POLL_INTERVAL_SEC
|
||||
_message_poll_timer.one_shot = false
|
||||
_message_poll_timer.autostart = false
|
||||
_message_poll_timer.timeout.connect(_on_message_poll_tick)
|
||||
add_child(_message_poll_timer)
|
||||
|
||||
|
||||
func _start_message_polling() -> void:
|
||||
if is_instance_valid(_message_poll_timer):
|
||||
_message_poll_timer.start()
|
||||
|
||||
|
||||
func _stop_message_polling() -> void:
|
||||
if is_instance_valid(_message_poll_timer):
|
||||
_message_poll_timer.stop()
|
||||
|
||||
|
||||
func _on_message_poll_tick() -> void:
|
||||
if channels.is_empty():
|
||||
return
|
||||
|
||||
for c: Dictionary in channels:
|
||||
var channel_id: String = c.get("id", "")
|
||||
if channel_id.is_empty():
|
||||
continue
|
||||
if _message_poll_in_flight.get(channel_id, false):
|
||||
continue
|
||||
|
||||
_message_poll_in_flight[channel_id] = true
|
||||
Network.request(
|
||||
"chat.get_messages",
|
||||
{ "channelId": channel_id },
|
||||
_on_poll_fetch_messages_success.bind(channel_id),
|
||||
_on_poll_fetch_messages_error.bind(channel_id)
|
||||
)
|
||||
|
||||
|
||||
func _on_fetch_messages_success(_t: String, p: Variant, channel_id: String) -> void:
|
||||
var msgs: Array = (p as Dictionary).get("messages", [])
|
||||
var changed := _merge_messages(channel_id, msgs)
|
||||
if changed:
|
||||
message_received.emit(channel_id, {})
|
||||
|
||||
|
||||
func _on_poll_fetch_messages_success(t: String, p: Variant, channel_id: String) -> void:
|
||||
_message_poll_in_flight.erase(channel_id)
|
||||
_on_fetch_messages_success(t, p, channel_id)
|
||||
|
||||
|
||||
func _on_poll_fetch_messages_error(_c: String, _m: String, channel_id: String) -> void:
|
||||
_message_poll_in_flight.erase(channel_id)
|
||||
|
||||
|
||||
func _merge_messages(channel_id: String, incoming: Array) -> bool:
|
||||
if channel_id not in messages:
|
||||
messages[channel_id] = []
|
||||
messages[channel_id].append_array(msgs)
|
||||
message_received.emit(channel_id, {})
|
||||
|
||||
var channel_msgs: Array = messages[channel_id]
|
||||
var known_ids: Dictionary = {}
|
||||
for m: Dictionary in channel_msgs:
|
||||
var mid: String = m.get("id", "")
|
||||
if not mid.is_empty():
|
||||
known_ids[mid] = true
|
||||
|
||||
var changed := false
|
||||
for msg: Dictionary in incoming:
|
||||
var incoming_id: String = msg.get("id", "")
|
||||
if incoming_id.is_empty():
|
||||
channel_msgs.append(msg)
|
||||
changed = true
|
||||
continue
|
||||
if known_ids.has(incoming_id):
|
||||
continue
|
||||
known_ids[incoming_id] = true
|
||||
channel_msgs.append(msg)
|
||||
changed = true
|
||||
|
||||
channel_msgs.sort_custom(func(a: Dictionary, b: Dictionary) -> bool:
|
||||
return a.get("sentAt", "") > b.get("sentAt", ""))
|
||||
messages[channel_id] = channel_msgs
|
||||
return changed
|
||||
|
||||
|
||||
func _on_open_dm_success(_t: String, p: Variant) -> void:
|
||||
@@ -289,10 +380,8 @@ func _on_server_push(type: String, payload: Variant) -> void:
|
||||
var cid: String = msg.get("channelId", "")
|
||||
if cid.is_empty():
|
||||
return
|
||||
if cid not in messages:
|
||||
messages[cid] = []
|
||||
messages[cid].push_front(msg)
|
||||
message_received.emit(cid, msg)
|
||||
if _merge_messages(cid, [msg]):
|
||||
message_received.emit(cid, msg)
|
||||
|
||||
"chat.friend_request.push":
|
||||
_fetch_social()
|
||||
|
||||
Reference in New Issue
Block a user