diff --git a/Classes/Comm.lua b/Classes/Comm.lua index 1b5326bf..3a5d3d67 100644 --- a/Classes/Comm.lua +++ b/Classes/Comm.lua @@ -4,9 +4,6 @@ local _, GL = ...; ---@type Version local Version = GL.Version; ----@type CommMessage -local CommMessage = GL.CommMessage; - ---@class Comm GL.Comm = { initialized = false, @@ -146,6 +143,26 @@ function Comm:_init() self._initialized = true; end +--- Sending cross-realm/cross-faction addon messages via whisper doesn't work currently +--- This instead forces the message to be sent on a valid group chat (party/raid) if possible +--- +---@param playerName string +---@return string +function Comm:whisperOrGroup(playerName) + local distribution = "WHISPER"; + + if (GL.User.isInGroup + and not ( + GL:iEquals(GL.User.realm, GL:stripRealm(playerName)) + or UnitFactionGroup(playerName) ~= UnitFactionGroup("player") + ) + ) then + distribution = GL.User.isInRaid and "RAID" or "PARTY"; + end + + return distribution; +end + --- Send a CommMessage object --- ---@param CommMessage CommMessage @@ -159,6 +176,10 @@ function Comm:send(CommMessage, broadcastFinishedCallback, packageSentCallback) local recipient = CommMessage.recipient; local action = CommMessage.action; + if (distribution == "WHISPER") then + distribution = self:whisperOrGroup(recipient); + end + local compressedMessage = ""; -- If this is a fresh message, not a response, then CommMessage will @@ -271,7 +292,10 @@ function Comm:listen(payload, distribution, playerName) and GL.CommMessage.Box[correspondenceId].onConfirm(true); end - if (type(payload) ~= "table") then + if (type(payload) ~= "table" + -- This message is not meant for us + or (payload.recipient and not GL:iEquals(payload.recipient, GL.User.fqn)) + ) then return false; end @@ -298,7 +322,7 @@ function Comm:listen(payload, distribution, playerName) -- This empty message will trigger an out-of-date error on the recipient's side GL.CommMessage.new{ action = Actions.response, - channel = "WHISPER", + channel = self:whisperOrGroup(playerName), recipient = playerName, }:send(); return; diff --git a/Classes/CommMessage.lua b/Classes/CommMessage.lua index 632b17de..21d06aa6 100644 --- a/Classes/CommMessage.lua +++ b/Classes/CommMessage.lua @@ -254,9 +254,9 @@ function CommMessage:compress(Message) b = Message.content, -- Content c = FQN, -- Name of the sender d = Message.correspondenceId or Message.id, -- Response ID - mv = Message.minimumVersion, -- For backwards compatibility only @TODO: REMOVE IN LATER VERSION m = Message.minimumVersion, -- Minimum version recipient should have v = Message.version, -- Version of sender + r = Message.channel ~= "WHISPER" and Message.recipient or nil; }; local success, encoded = pcall(function () @@ -307,9 +307,6 @@ function CommMessage:decompress(encoded) Payload.c = nil; end - -- Old versions send their minimum version under the 'mv' key @TODO: REMOVE IN LATER VERSION - Payload.m = Payload.m or Payload.mv; - return { action = Payload.a or nil, -- Action content = Payload.b or nil, -- Content @@ -317,6 +314,7 @@ function CommMessage:decompress(encoded) correspondenceId = Payload.d or Payload.id, -- Response ID minimumVersion = Payload.m or nil, -- Minimum version recipient should have version = Payload.v or nil, -- Version of sender + recipient = Payload.r or nil, -- Recipient (in case we route whisper through group) }; end