Skip to content

Commit

Permalink
Sending cross-realm/cross-faction addon message via whisper doesn't c…
Browse files Browse the repository at this point in the history
…urrently work in WoW. This aims to resolve that issue
  • Loading branch information
papa-smurf committed Mar 9, 2024
1 parent 84288dd commit e9c1d35
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
34 changes: 29 additions & 5 deletions Classes/Comm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ local _, GL = ...;
---@type Version
local Version = GL.Version;

---@type CommMessage
local CommMessage = GL.CommMessage;

---@class Comm
GL.Comm = {
initialized = false,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions Classes/CommMessage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down Expand Up @@ -307,16 +307,14 @@ 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
senderFqn = Payload.c or nil, -- Name of the sender
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

Expand Down

0 comments on commit e9c1d35

Please sign in to comment.