OverviewFilesCommitsIssuesDocsReleasesPeers

godot-keet / addons / keet_social / keet_dm.gd

145 lines · gd ref:
class_name KeetDM
extends Node
## DM management for keet-daemon via UDS.
## List DMs, accept/reject requests, initiate DMs, share rooms.
## DMs are RoomType=2 in keet-core — rooms with 2 members.

signal dms_loaded(dms: Array)
signal dm_requests_loaded(requests: Array)
signal dm_sent(room_id: String)
signal dm_accepted(room_id: String)
signal dm_rejected(room_id: String)
signal room_shared(dm_room_id: String)
signal shared_rooms_loaded(dm_room_id: String, shares: Array)
signal error_occurred(message: String)

var client: KeetUdsClient


## List accepted DMs (status=1).
func list_dms(callback: Callable = Callable()) -> void:
	_require_client()
	client.call_rpc("dm.list", {}, func(result, err):
		if err != null:
			error_occurred.emit("list_dms: %s" % str(err))
			return
		var dms: Array = result if result is Array else []
		dms_loaded.emit(dms)
		if callback.is_valid():
			callback.call(dms)
	)


## List pending DM requests (status=3 by default).
func list_requests(status: int = 3, callback: Callable = Callable()) -> void:
	_require_client()
	client.call_rpc("dm.requests", {"status": status}, func(result, err):
		if err != null:
			error_occurred.emit("list_requests: %s" % str(err))
			return
		var requests: Array = result if result is Array else []
		dm_requests_loaded.emit(requests)
		if callback.is_valid():
			callback.call(requests)
	)


## Accept a DM request.
func accept(room_id: String, callback: Callable = Callable()) -> void:
	_require_client()
	client.call_rpc("dm.accept", {"roomId": room_id}, func(result, err):
		if err != null:
			error_occurred.emit("accept: %s" % str(err))
			return
		dm_accepted.emit(room_id)
		if callback.is_valid():
			callback.call(result)
	)


## Reject a DM request.
func reject(room_id: String, callback: Callable = Callable()) -> void:
	_require_client()
	client.call_rpc("dm.reject", {"roomId": room_id}, func(result, err):
		if err != null:
			error_occurred.emit("reject: %s" % str(err))
			return
		dm_rejected.emit(room_id)
		if callback.is_valid():
			callback.call(result)
	)


## Send DM request from a shared room (requires being in the same room).
func request_from_room(room_id: String, member_id: String, message: String = "", callback: Callable = Callable()) -> void:
	_require_client()
	client.call_rpc("dm.request", {"roomId": room_id, "memberId": member_id, "message": message}, func(result, err):
		if err != null:
			error_occurred.emit("request_from_room: %s" % str(err))
			return
		var dm_rid: String = ""
		if result is Dictionary:
			dm_rid = result.get("roomId", result.get("id", ""))
		elif result is String:
			dm_rid = result
		dm_sent.emit(dm_rid)
		if callback.is_valid():
			callback.call(result)
	)


## Send DM from contact (cold-contact — no shared room needed).
func request_from_contact(member_id: String, callback: Callable = Callable()) -> void:
	_require_client()
	client.call_rpc("dm.fromContact", {"memberId": member_id}, func(result, err):
		if err != null:
			error_occurred.emit("request_from_contact: %s" % str(err))
			return
		var dm_rid: String = ""
		if result is Dictionary:
			dm_rid = result.get("roomId", result.get("id", ""))
		elif result is String:
			dm_rid = result
		dm_sent.emit(dm_rid)
		if callback.is_valid():
			callback.call(result)
	)


## Share a room in a DM as an _hs:room-share message.
func share_room(dm_room_id: String, room_id: String, title: String, topics: Array = [], invite_token: String = "", callback: Callable = Callable()) -> void:
	_require_client()
	client.call_rpc("dm.share", {
		"dmRoomId": dm_room_id,
		"roomId": room_id,
		"title": title,
		"topics": topics,
		"inviteToken": invite_token
	}, func(result, err):
		if err != null:
			error_occurred.emit("share_room: %s" % str(err))
			return
		room_shared.emit(dm_room_id)
		if callback.is_valid():
			callback.call(result)
	)


## Get rooms shared in a DM conversation.
func get_shared_rooms(dm_room_id: String, callback: Callable = Callable()) -> void:
	_require_client()
	client.call_rpc("dm.shares", {"dmRoomId": dm_room_id}, func(result, err):
		if err != null:
			error_occurred.emit("get_shared_rooms: %s" % str(err))
			return
		var shares: Array = result if result is Array else []
		shared_rooms_loaded.emit(dm_room_id, shares)
		if callback.is_valid():
			callback.call(shares)
	)


func _require_client() -> void:
	if client == null:
		push_error("KeetDM: client not set")