- ChatGetChannelsHandler: for Direct channels, look up the other member's display name from identity DB and return it as the channel name (per-caller, no DB schema change) - ChatDmOpenHandler: same — returns recipient display name in the DTO for both new and existing DM channels - New ChatPartyRenameHandler (chat.party.rename): updates the party channel name, returns updated DTO - Chat.gd: rename_party() calls the new handler and upserts locally - chat_overlay.gd: "Rename" button in party action bar opens a dialog to set a new party name Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
523 lines
16 KiB
GDScript
523 lines
16 KiB
GDScript
extends Node
|
|
## Chat autoload — manages all chat state and server communication.
|
|
##
|
|
## On auth_success, fetches channels, social graph, and notifications,
|
|
## then sets own presence to Online. Adds the ChatOverlay CanvasLayer to
|
|
## the viewport root so it persists across scene changes.
|
|
##
|
|
## Public signals let the overlay (and any other UI) react to changes.
|
|
##
|
|
## Usage:
|
|
## Chat.send_message(channel_id, "Hello!")
|
|
## Chat.open_dm(other_account_id)
|
|
## Chat.social_updated ← emit when friends / requests / blocked change
|
|
|
|
# ── Signals ───────────────────────────────────────────────────────────────────
|
|
|
|
## Emitted when the channels array changes (initial load, DM opened, etc.)
|
|
signal channels_updated
|
|
|
|
## Emitted for every server-pushed or locally echoed message.
|
|
## msg is a ChatMessageDto dictionary.
|
|
signal message_received(channel_id: String, msg: Dictionary)
|
|
|
|
## Emitted after friends / pending_requests / blocked_users are refreshed.
|
|
signal social_updated
|
|
|
|
## Emitted after the presence dictionary is updated.
|
|
signal presence_updated
|
|
|
|
## Emitted after notifications array changes.
|
|
signal notifications_updated
|
|
|
|
## Emitted when the party channel is joined, left, or updated.
|
|
signal party_updated
|
|
|
|
## Emitted when a remote player invites the local player to their party.
|
|
## channel_id: the Party channel to accept into; inviter_name: display name of sender.
|
|
signal party_invite_received(channel_id: String, inviter_name: String)
|
|
|
|
# ── State ─────────────────────────────────────────────────────────────────────
|
|
|
|
## Array[Dictionary] — ChatChannelDto {id, name, channelType, createdAt}
|
|
var channels: Array = []
|
|
|
|
## Dictionary[channel_id → Array[Dictionary]] — messages newest-first
|
|
var messages: Dictionary = {}
|
|
|
|
## Array[Dictionary] — FriendDto {accountId, displayName, friendsSince}
|
|
var friends: Array = []
|
|
|
|
## Array[Dictionary] — FriendRequestDto {requestId, otherAccountId, otherDisplayName, createdAt}
|
|
var pending_requests: Array = []
|
|
|
|
## Array[Dictionary] — BlockedUserDto {accountId, displayName, blockedAt}
|
|
var blocked_users: Array = []
|
|
|
|
## Dictionary[account_id → Dictionary] — PresenceDto
|
|
var presence: Dictionary = {}
|
|
|
|
## Array[Dictionary] — NotificationDto
|
|
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 ─────────────────────────────────────────────────────────────────
|
|
|
|
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")
|
|
|
|
|
|
func _add_overlay() -> void:
|
|
var scene := load("res://scenes/chat/ChatOverlay.tscn") as PackedScene
|
|
if scene == null:
|
|
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:
|
|
# Overlay visibility is controlled by individual scenes via set_bubble_visible().
|
|
_fetch_social()
|
|
_fetch_channels()
|
|
_fetch_notifications()
|
|
_set_own_presence("Online")
|
|
_start_message_polling()
|
|
|
|
|
|
## Show or hide the chat bubble overlay. Call from scenes that want chat
|
|
## (e.g. MainScreen._ready / _exit_tree).
|
|
func set_bubble_visible(v: bool) -> void:
|
|
if is_instance_valid(_overlay):
|
|
_overlay.visible = v
|
|
|
|
|
|
func _clear_state() -> void:
|
|
if is_instance_valid(_overlay):
|
|
_overlay.visible = false
|
|
_stop_message_polling()
|
|
channels = []
|
|
messages = {}
|
|
friends = []
|
|
pending_requests = []
|
|
blocked_users = []
|
|
presence = {}
|
|
notifications = []
|
|
_message_poll_in_flight.clear()
|
|
channels_updated.emit()
|
|
social_updated.emit()
|
|
party_updated.emit()
|
|
|
|
# ── Public API ────────────────────────────────────────────────────────────────
|
|
|
|
func send_message(channel_id: String, content: String) -> void:
|
|
Network.send("chat.send", { "channelId": channel_id, "content": content.strip_edges() })
|
|
|
|
|
|
func fetch_messages(channel_id: String, before_id: String = "") -> void:
|
|
var payload: Dictionary = { "channelId": channel_id }
|
|
if not before_id.is_empty():
|
|
payload["beforeMessageId"] = before_id
|
|
Network.request(
|
|
"chat.get_messages",
|
|
payload,
|
|
_on_fetch_messages_success.bind(channel_id),
|
|
_noop_error
|
|
)
|
|
|
|
|
|
## Open or retrieve the Direct channel with recipient_account_id.
|
|
## Emits channels_updated on success so the DMs tab can refresh.
|
|
func open_dm(recipient_account_id: String) -> void:
|
|
Network.request(
|
|
"chat.dm_open",
|
|
{ "recipientAccountId": recipient_account_id },
|
|
_on_open_dm_success,
|
|
_noop_error
|
|
)
|
|
|
|
|
|
func send_friend_request(recipient_id: String) -> void:
|
|
Network.request("chat.friend_request",
|
|
{ "recipientAccountId": recipient_id },
|
|
func(_t: String, _p: Variant) -> void: _fetch_social(),
|
|
func(_c: String, _m: String) -> void: pass
|
|
)
|
|
|
|
|
|
func respond_friend_request(request_id: String, accept: bool) -> void:
|
|
Network.request("chat.friend_respond",
|
|
{ "requestId": request_id, "accept": accept },
|
|
func(_t: String, _p: Variant) -> void: _fetch_social(),
|
|
func(_c: String, _m: String) -> void: pass
|
|
)
|
|
|
|
|
|
func remove_friend(friend_account_id: String) -> void:
|
|
Network.request("chat.friend_remove",
|
|
{ "friendAccountId": friend_account_id },
|
|
func(_t: String, _p: Variant) -> void: _fetch_social(),
|
|
func(_c: String, _m: String) -> void: pass
|
|
)
|
|
|
|
|
|
func block_user(target_account_id: String) -> void:
|
|
Network.request("chat.block",
|
|
{ "targetAccountId": target_account_id },
|
|
func(_t: String, _p: Variant) -> void: _fetch_social(),
|
|
func(_c: String, _m: String) -> void: pass
|
|
)
|
|
|
|
|
|
func unblock_user(target_account_id: String) -> void:
|
|
Network.request("chat.unblock",
|
|
{ "targetAccountId": target_account_id },
|
|
func(_t: String, _p: Variant) -> void: _fetch_social(),
|
|
func(_c: String, _m: String) -> void: pass
|
|
)
|
|
|
|
|
|
func set_presence(status: String) -> void:
|
|
_set_own_presence(status)
|
|
|
|
|
|
func fetch_presence(account_ids: Array) -> void:
|
|
if account_ids.is_empty():
|
|
return
|
|
Network.request(
|
|
"chat.presence.get",
|
|
{ "accountIds": account_ids },
|
|
_on_fetch_presence_success,
|
|
_noop_error
|
|
)
|
|
|
|
|
|
## Invite a player to the caller's party (creates the party channel if needed).
|
|
func invite_to_party(account_id: String) -> void:
|
|
Network.request(
|
|
"chat.party.invite",
|
|
{ "inviteeAccountId": account_id },
|
|
_on_party_invite_sent_success,
|
|
_noop_error
|
|
)
|
|
|
|
|
|
## Accept or decline an incoming party invite.
|
|
func respond_party_invite(channel_id: String, accept: bool) -> void:
|
|
Network.request(
|
|
"chat.party.invite_respond",
|
|
{ "channelId": channel_id, "accept": accept },
|
|
_on_party_invite_respond_success,
|
|
_noop_error
|
|
)
|
|
|
|
|
|
## Rename the caller's current party channel.
|
|
func rename_party(name: String) -> void:
|
|
Network.request(
|
|
"chat.party.rename",
|
|
{ "name": name },
|
|
func(_t: String, p: Variant) -> void:
|
|
var chan: Dictionary = (p as Dictionary).get("channel", {})
|
|
if not chan.is_empty():
|
|
_upsert_channel(chan)
|
|
party_updated.emit()
|
|
channels_updated.emit(),
|
|
func(_c: String, _m: String) -> void: pass
|
|
)
|
|
|
|
|
|
## Leave the current party channel.
|
|
func leave_party() -> void:
|
|
Network.request(
|
|
"chat.party.leave",
|
|
{},
|
|
_on_party_leave_success,
|
|
_noop_error
|
|
)
|
|
|
|
|
|
func mark_notifications_read(ids: Array) -> void:
|
|
if ids.is_empty():
|
|
return
|
|
Network.send("chat.notifications.read", { "notificationIds": ids })
|
|
for n: Dictionary in notifications:
|
|
if n.get("id", "") in ids:
|
|
n["readAt"] = Time.get_datetime_string_from_system()
|
|
notifications_updated.emit()
|
|
|
|
|
|
func get_unread_notification_count() -> int:
|
|
return notifications.filter(
|
|
func(n: Dictionary) -> bool: return n.get("readAt") == null
|
|
).size()
|
|
|
|
|
|
## Returns the first channel of the given ChannelType, or {} if none.
|
|
func get_channel_by_type(type_name: String) -> Dictionary:
|
|
for c: Dictionary in channels:
|
|
if c.get("channelType", "") == type_name:
|
|
return c
|
|
return {}
|
|
|
|
|
|
## Returns all Direct-type channels.
|
|
func get_dm_channels() -> Array:
|
|
return channels.filter(
|
|
func(c: Dictionary) -> bool: return c.get("channelType", "") == "Direct"
|
|
)
|
|
|
|
|
|
## Returns the caller's current Party channel, or {} if not in one.
|
|
func get_party_channel() -> Dictionary:
|
|
for c: Dictionary in channels:
|
|
if c.get("channelType", "") == "Party":
|
|
return c
|
|
return {}
|
|
|
|
|
|
## Remove a Direct channel from local state (client-side only, no server call).
|
|
func close_dm(channel_id: String) -> void:
|
|
channels = channels.filter(func(c: Dictionary) -> bool:
|
|
return c.get("id", "") != channel_id)
|
|
messages.erase(channel_id)
|
|
channels_updated.emit()
|
|
|
|
# ── Private helpers ───────────────────────────────────────────────────────────
|
|
|
|
func _set_own_presence(status: String) -> void:
|
|
Network.send("chat.presence.set", { "status": status })
|
|
|
|
|
|
func _fetch_social() -> void:
|
|
Network.request("chat.get_social", {}, _on_fetch_social_success, _noop_error)
|
|
|
|
|
|
func _fetch_channels() -> void:
|
|
Network.request("chat.get_channels", {}, _on_fetch_channels_success, _noop_error)
|
|
|
|
|
|
func _fetch_notifications() -> void:
|
|
Network.request(
|
|
"chat.notifications.get",
|
|
{ "unreadOnly": false },
|
|
_on_fetch_notifications_success,
|
|
_noop_error
|
|
)
|
|
|
|
|
|
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] = []
|
|
|
|
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:
|
|
var chan: Dictionary = (p as Dictionary).get("channel", {})
|
|
if chan.is_empty():
|
|
return
|
|
var cid: String = chan.get("id", "")
|
|
var found := false
|
|
for c: Dictionary in channels:
|
|
if c.get("id", "") == cid:
|
|
found = true
|
|
break
|
|
if not found:
|
|
channels.push_front(chan)
|
|
channels_updated.emit()
|
|
|
|
|
|
func _on_fetch_presence_success(_t: String, p: Variant) -> void:
|
|
for pr: Dictionary in (p as Dictionary).get("presences", []):
|
|
presence[pr.get("accountId", "")] = pr
|
|
presence_updated.emit()
|
|
|
|
|
|
func _on_fetch_social_success(_t: String, p: Variant) -> void:
|
|
var d := p as Dictionary
|
|
friends = d.get("friends", [])
|
|
pending_requests = d.get("incomingRequests", [])
|
|
blocked_users = d.get("blocked", [])
|
|
social_updated.emit()
|
|
var ids: Array = friends.map(func(f: Dictionary) -> String:
|
|
return f.get("accountId", ""))
|
|
fetch_presence(ids)
|
|
|
|
|
|
func _on_fetch_channels_success(_t: String, p: Variant) -> void:
|
|
channels = (p as Dictionary).get("channels", [])
|
|
channels_updated.emit()
|
|
|
|
|
|
func _on_fetch_notifications_success(_t: String, p: Variant) -> void:
|
|
notifications = (p as Dictionary).get("notifications", [])
|
|
notifications_updated.emit()
|
|
|
|
|
|
func _on_party_invite_sent_success(_t: String, p: Variant) -> void:
|
|
var chan: Dictionary = (p as Dictionary).get("channel", {})
|
|
if not chan.is_empty():
|
|
_upsert_channel(chan)
|
|
party_updated.emit()
|
|
channels_updated.emit()
|
|
|
|
|
|
func _on_party_invite_respond_success(_t: String, p: Variant) -> void:
|
|
var d := p as Dictionary
|
|
if not d.get("accepted", false):
|
|
return
|
|
var chan: Dictionary = d.get("channel", {})
|
|
if not chan.is_empty():
|
|
_upsert_channel(chan)
|
|
party_updated.emit()
|
|
channels_updated.emit()
|
|
|
|
|
|
func _on_party_leave_success(_t: String, _p: Variant) -> void:
|
|
channels = channels.filter(func(c: Dictionary) -> bool:
|
|
return c.get("channelType", "") != "Party")
|
|
messages.erase(_get_party_channel_id())
|
|
party_updated.emit()
|
|
channels_updated.emit()
|
|
|
|
|
|
func _get_party_channel_id() -> String:
|
|
for c: Dictionary in channels:
|
|
if c.get("channelType", "") == "Party":
|
|
return c.get("id", "")
|
|
return ""
|
|
|
|
|
|
func _upsert_channel(chan: Dictionary) -> void:
|
|
var cid: String = chan.get("id", "")
|
|
for i in channels.size():
|
|
if (channels[i] as Dictionary).get("id", "") == cid:
|
|
channels[i] = chan
|
|
return
|
|
channels.push_front(chan)
|
|
|
|
|
|
func _noop_error(_c: String, _m: String) -> void:
|
|
pass
|
|
|
|
|
|
func _on_server_push(type: String, payload: Variant) -> void:
|
|
match type:
|
|
"chat.message.push":
|
|
var msg: Dictionary = (payload as Dictionary).get("message", {})
|
|
var cid: String = msg.get("channelId", "")
|
|
if cid.is_empty():
|
|
return
|
|
if _merge_messages(cid, [msg]):
|
|
message_received.emit(cid, msg)
|
|
|
|
"chat.friend_request.push":
|
|
_fetch_social()
|
|
|
|
"chat.notification.push":
|
|
var notif: Dictionary = (payload as Dictionary).get("notification", {})
|
|
if not notif.is_empty():
|
|
notifications.push_front(notif)
|
|
notifications_updated.emit()
|
|
|
|
"chat.party_invite.push":
|
|
var d := payload as Dictionary
|
|
var cid : String = d.get("channelId", "")
|
|
var inviter : String = d.get("inviterName", "Someone")
|
|
if not cid.is_empty():
|
|
party_invite_received.emit(cid, inviter)
|