Implement Godot API client with REST social helpers and WebSocket gateway client (task 264d84d07f28)
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
extends RefCounted
|
||||
class_name SocialApiClient
|
||||
## Helper class for AGP Social REST endpoints and WebSocket gateway interactions.
|
||||
##
|
||||
## Can be used as a standalone helper or via ApiClient.
|
||||
|
||||
static func get_api_client(node: Node) -> Node:
|
||||
if node.has_node("/root/ApiClient"):
|
||||
return node.get_node("/root/ApiClient")
|
||||
return null
|
||||
|
||||
# ── Friends & Follow & Block ──
|
||||
|
||||
static func send_friend_request(api: Node, user_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/friends/requests", JSON.stringify({"user_id": user_id}), "application/json", true)
|
||||
|
||||
static func get_friend_requests(api: Node) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_GET, "/api/friends/requests", "", "application/json", true)
|
||||
|
||||
static func accept_friend_request(api: Node, request_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/friends/requests/%d/accept" % request_id, "", "application/json", true)
|
||||
|
||||
static func decline_friend_request(api: Node, request_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/friends/requests/%d/decline" % request_id, "", "application/json", true)
|
||||
|
||||
static func unfriend(api: Node, user_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_DELETE, "/api/friends/%d" % user_id, "", "application/json", true)
|
||||
|
||||
static func get_friends(api: Node) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_GET, "/api/friends", "", "application/json", true)
|
||||
|
||||
static func follow_user(api: Node, user_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/friends/%d/follow" % user_id, "", "application/json", true)
|
||||
|
||||
static func unfollow_user(api: Node, user_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_DELETE, "/api/friends/%d/follow" % user_id, "", "application/json", true)
|
||||
|
||||
static func get_following(api: Node) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_GET, "/api/friends/following", "", "application/json", true)
|
||||
|
||||
static func block_user(api: Node, user_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/friends/%d/block" % user_id, "", "application/json", true)
|
||||
|
||||
static func unblock_user(api: Node, user_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_DELETE, "/api/friends/%d/block" % user_id, "", "application/json", true)
|
||||
|
||||
static func get_blocked(api: Node) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_GET, "/api/friends/blocked", "", "application/json", true)
|
||||
|
||||
# ── Presence ──
|
||||
|
||||
static func send_presence_heartbeat(api: Node, custom_status: String = "") -> Dictionary:
|
||||
var payload := {}
|
||||
if custom_status != "":
|
||||
payload["custom_status"] = custom_status
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/presence/heartbeat", JSON.stringify(payload), "application/json", true)
|
||||
|
||||
static func get_friends_presence(api: Node) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_GET, "/api/presence/friends", "", "application/json", true)
|
||||
|
||||
static func get_user_presence(api: Node, user_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_GET, "/api/presence/%d" % user_id, "", "application/json", true)
|
||||
|
||||
# ── Profiles ──
|
||||
|
||||
static func get_my_profile(api: Node) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_GET, "/api/profile/me", "", "application/json", true)
|
||||
|
||||
static func update_my_profile(api: Node, bio: Variant = null, avatar_asset_id: Variant = null, privacy: Variant = null) -> Dictionary:
|
||||
var payload := {}
|
||||
if bio != null:
|
||||
payload["bio"] = bio
|
||||
if avatar_asset_id != null:
|
||||
payload["avatar_asset_id"] = avatar_asset_id
|
||||
if privacy != null:
|
||||
payload["privacy"] = privacy
|
||||
return await api.request(HTTPClient.METHOD_PUT, "/api/profile/me", JSON.stringify(payload), "application/json", true)
|
||||
|
||||
static func get_my_favorites(api: Node) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_GET, "/api/profile/me/favorites", "", "application/json", true)
|
||||
|
||||
static func replace_my_favorites(api: Node, world_ids: Array) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_PUT, "/api/profile/me/favorites", JSON.stringify({"world_ids": world_ids}), "application/json", true)
|
||||
|
||||
static func get_public_profile(api: Node, user_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_GET, "/api/profile/%d" % user_id, "", "application/json", true)
|
||||
|
||||
# ── Groups ──
|
||||
|
||||
static func create_group(api: Node, name: String, description: String = "") -> Dictionary:
|
||||
var payload := {"name": name}
|
||||
if description != "":
|
||||
payload["description"] = description
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/groups", JSON.stringify(payload), "application/json", true)
|
||||
|
||||
static func list_groups(api: Node, page: int = 1, page_size: int = 50, mine: bool = false) -> Dictionary:
|
||||
var path := "/api/groups?page=%d&page_size=%d" % [page, page_size]
|
||||
if mine:
|
||||
path += "&mine=true"
|
||||
return await api.request(HTTPClient.METHOD_GET, path, "", "application/json", true)
|
||||
|
||||
static func get_group(api: Node, group_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_GET, "/api/groups/%d" % group_id, "", "application/json", true)
|
||||
|
||||
static func update_group(api: Node, group_id: int, name: Variant = null, description: Variant = null) -> Dictionary:
|
||||
var payload := {}
|
||||
if name != null:
|
||||
payload["name"] = name
|
||||
if description != null:
|
||||
payload["description"] = description
|
||||
return await api.request(HTTPClient.METHOD_PATCH, "/api/groups/%d" % group_id, JSON.stringify(payload), "application/json", true)
|
||||
|
||||
static func delete_group(api: Node, group_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_DELETE, "/api/groups/%d" % group_id, "", "application/json", true)
|
||||
|
||||
static func join_group(api: Node, group_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/groups/%d/join" % group_id, "", "application/json", true)
|
||||
|
||||
static func leave_group(api: Node, group_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/groups/%d/leave" % group_id, "", "application/json", true)
|
||||
|
||||
static func set_group_member_role(api: Node, group_id: int, user_id: int, role: String) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/groups/%d/members/%d/role" % [group_id, user_id], JSON.stringify({"role": role}), "application/json", true)
|
||||
|
||||
static func transfer_group_ownership(api: Node, group_id: int, new_owner_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/groups/%d/transfer" % group_id, JSON.stringify({"new_owner_id": new_owner_id}), "application/json", true)
|
||||
|
||||
# ── Chat & Moderation ──
|
||||
|
||||
static func get_dm_conversations(api: Node) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_GET, "/api/chat/dm/conversations", "", "application/json", true)
|
||||
|
||||
static func start_dm_conversation(api: Node, target_user_id: int) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/chat/dm/conversations", JSON.stringify({"target_user_id": target_user_id}), "application/json", true)
|
||||
|
||||
static func get_dm_messages(api: Node, conversation_id: int, before_id: int = 0, limit: int = 50) -> Dictionary:
|
||||
var path := "/api/chat/dm/conversations/%d/messages?limit=%d" % [conversation_id, limit]
|
||||
if before_id > 0:
|
||||
path += "&before_id=%d" % before_id
|
||||
return await api.request(HTTPClient.METHOD_GET, path, "", "application/json", true)
|
||||
|
||||
static func send_dm_message(api: Node, conversation_id: int, content: String) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/chat/dm/conversations/%d/messages" % conversation_id, JSON.stringify({"content": content}), "application/json", true)
|
||||
|
||||
static func get_world_chat_messages(api: Node, world_id: int, before_id: int = 0, limit: int = 50) -> Dictionary:
|
||||
var path := "/api/chat/world/%d/messages?limit=%d" % [world_id, limit]
|
||||
if before_id > 0:
|
||||
path += "&before_id=%d" % before_id
|
||||
return await api.request(HTTPClient.METHOD_GET, path, "", "application/json", true)
|
||||
|
||||
static func send_world_chat_message(api: Node, world_id: int, content: String) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/chat/world/%d/messages" % world_id, JSON.stringify({"content": content}), "application/json", true)
|
||||
|
||||
static func get_room_chat_messages(api: Node, room_id: String, before_id: int = 0, limit: int = 50) -> Dictionary:
|
||||
var path := "/api/chat/room/%s/messages?limit=%d" % [room_id.uri_encode(), limit]
|
||||
if before_id > 0:
|
||||
path += "&before_id=%d" % before_id
|
||||
return await api.request(HTTPClient.METHOD_GET, path, "", "application/json", true)
|
||||
|
||||
static func send_room_chat_message(api: Node, room_id: String, content: String) -> Dictionary:
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/chat/room/%s/messages" % room_id.uri_encode(), JSON.stringify({"content": content}), "application/json", true)
|
||||
|
||||
static func report_message(api: Node, message_id: int, conversation_type: String, reason: String, context_text: String = "") -> Dictionary:
|
||||
var payload := {
|
||||
"message_id": message_id,
|
||||
"conversation_type": conversation_type,
|
||||
"reason": reason
|
||||
}
|
||||
if context_text != "":
|
||||
payload["context_text"] = context_text
|
||||
return await api.request(HTTPClient.METHOD_POST, "/api/chat/reports/messages", JSON.stringify(payload), "application/json", true)
|
||||
Reference in New Issue
Block a user