diff --git a/mod_startinblox_groups_muc_members_admin.lua b/mod_startinblox_groups_muc_members_admin.lua index d0453a0a39165b72e111de05b54fd143701bae80..5f828844d4f9fff16954105624b398f22a3a5669 100644 --- a/mod_startinblox_groups_muc_members_admin.lua +++ b/mod_startinblox_groups_muc_members_admin.lua @@ -44,17 +44,21 @@ function update_affiliations(room, affiliations) return false; end + -- module:log("debug", "TEST5_GROUP_DEBUG Updating affiliations for %s", room.jid); local user_errors; for user_jid, affiliation in pairs(affiliations) do + -- module:log("debug", "TEST5_GROUP_DEBUG Updating affiliations for %s : %s", room.jid, user_jid); if type(affiliation) == "string" then affiliation = { affiliation = affiliation }; end + -- module:log("debug", "TEST5_GROUP_DEBUG Updating affiliations for %s : %s with role %d and data %s", room.jid, user_jid, affiliation.affiliation, serialize(affiliation.data)); local ok, err = room:set_affiliation(true, user_jid, affiliation.affiliation, affiliation.reason, affiliation.data ); if not ok then if not user_errors then user_errors = {}; end + -- module:log("debug", "TEST5_GROUP_DEBUG Updating affiliations for %s raises error %s", room.jid, err); user_errors[user_jid] = err; end @@ -66,6 +70,7 @@ function update_affiliations(room, affiliations) or desired_affiliation.affiliation ~= affiliation or not data or desired_affiliation.data.ld_profile_url ~= data.ld_profile_url then + -- module:log("debug", "TEST5_GROUP_DEBUG Updating affiliation for %s due to change", user_jid); local ok, err = room:set_affiliation(true, jid.prep(user_jid), desired_affiliation.affiliation, desired_affiliation.reason, desired_affiliation.data @@ -76,6 +81,7 @@ function update_affiliations(room, affiliations) end end end + -- module:log("debug", "TEST5_GROUP_DEBUG updating affiliations finished"); return true, user_errors; end @@ -91,11 +97,13 @@ local function handle_change_notification(event) if notification["@context"] ~= "https://cdn.startinblox.com/owl/context.jsonld" or (notification.type ~= "update" and notification.type ~= "creation" and notification.type ~= "deletion") then + module:log("warn", "Unknown notification format, skipping"); -- module:log("debug", "TEST5_GROUP_DEBUG Received: %s", tostring(event.request.body)); return promise.reject(errors.new("invalid-data")); end local url = notification.object; + -- module:log("debug", "TEST5_GROUP_DEBUG [%s] Change notification for %s: %s", change_id, url, serialize(notification, "debug")); local headers = { ["depth"] = "3", ["accept-model-fields"] = '["user_set", "circle", "chatProfile", "jabberID", "username", "name"]' @@ -103,6 +111,8 @@ local function handle_change_notification(event) sib_util.log_object(headers); return sib_util.get_json_document(url, { headers = headers, insecure = true }):next(function (membership_data) + -- module:log("debug", "TEST5_GROUP_DEBUG Logging change data"); + -- module:log("debug", "TEST5_GROUP_DEBUG Logging membership data"); sib_util.log_object(membership_data); -- Is "group" local room_type, err = get_room_type(membership_data); @@ -110,31 +120,45 @@ local function handle_change_notification(event) return promise.reject(err); end + -- module:log("debug", "TEST5_GROUP_DEBUG Room type: %s", room_type); if membership_data["circle"] == nil or membership_data["circle"]["@id"] == nil then return promise.reject(errors.new("invalid-group-type")); - end + else + -- module:log("debug", "TEST5_GROUP_DEBUG Got some circle data"); + end -- --Todo: Parse the request response to get everything needed + -- module:log("debug", "TEST5_GROUP_DEBUG %s", membership_data["circle"]["@id"]); sib_util.log_object(membership_data.circle); local circle = membership_data.circle; local room_id = sib_util.extract_path(membership_data, "circle.@id") if not room_id then + -- module:log("debug", "TEST5_GROUP_DEBUG Room ID does not exist"); return promise.reject(errors.new("invalid-room-id")); - end + else + -- module:log("debug", "TEST5_GROUP_DEBUG Room ID: %s", room_id); + end + -- module:log("debug", "TEST5_GROUP_DEBUG %s", circle.jabberID); local room_jid = circle.jabberID; if not room_jid then + -- module:log("debug", "TEST5_GROUP_DEBUG Room JID does not exist"); return promise.reject(errors.new("invalid-room-jid")); - end + else + -- module:log("debug", "TEST5_GROUP_DEBUG Room JID: %s", room_jid); + end if jid.host(room_jid) ~= module.host then + -- module:log("debug", "TEST5_GROUP_DEBUG Invalid room host"); return promise.reject(errors.new("invalid-room-host")); end local room = mod_muc.get_room_from_jid(room_jid); if not room then + -- module:log("debug", "TEST5_GROUP_DEBUG Room not found"); return promise.reject(errors.new("room-not-found")); else + -- module:log("debug", "TEST5_GROUP_DEBUG Room found"); sib_util.log_object(room); end @@ -143,22 +167,27 @@ local function handle_change_notification(event) if type(circle_members) ~= "table" then error(errors.new("invalid-members-list")); else + -- module:log("debug", "TEST5_GROUP_DEBUG Got some members data"); -- Iterate on list of members and update the room with their affiliations data -- We need at list the username and jabber_id local members = array.map(circle_members, function (member) if member["@id"] then + -- module:log("debug", "TEST5_GROUP_DEBUG Member ID: %s", member["@id"]); user_id = member["@id"]; end if member["username"] then + -- module:log("debug", "TEST5_GROUP_DEBUG Member username: %s", member["username"]); username = member["username"]; end if member["chatProfile"]["jabberID"] then + -- module:log("debug", "TEST5_GROUP_DEBUG Member jabberID: %s", member["chatProfile"]["jabberID"]); user_jid = jid.prep(member["chatProfile"]["jabberID"]); end if member["chatProfile"]["@id"] then + -- module:log("debug", "TEST5_GROUP_DEBUG Member ChatProfile URI: %s", member["chatProfile"]["@id"]); chat_profile_url = jid.prep(member["chatProfile"]["@id"]); end @@ -175,8 +204,11 @@ local function handle_change_notification(event) ld_member_url = chat_profile_url; }; }; + -- module:log("debug", "TEST5_GROUP_DEBUG members: %s", serialize(formatted_member,"debug")) return formatted_member; end); + -- module:log("debug", "TEST5_GROUP_DEBUG Loggiing MEMEMEMBERS INFOOOO"); + -- module:log("debug", "TEST5_GROUP_DEBUG members: %s", serialize(members,"debug")) -- Add the @all user to the room local affiliations = { @@ -189,6 +221,7 @@ local function handle_change_notification(event) }; }; }; + -- module:log("debug", "TEST5_GROUP_DEBUG affiliations: %s", serialize(affiliations,"debug")) for _, member in ipairs(members) do affiliations[member.jid] = { @@ -196,9 +229,12 @@ local function handle_change_notification(event) data = member.data; }; end + -- module:log("debug", "TEST5_GROUP_DEBUG affiliations: %s", serialize(affiliations,"debug")) + -- module:log("debug", "TEST5_GROUP_DEBUG [%s] Updating room %s with members", change_id, room_jid); local ok, user_errors = update_affiliations(room, affiliations); if not ok then + -- module:log("debug", "TEST5_GROUP_DEBUG affiliations errors: %s", serialize(user_errors,"debug")) -- Total failure to apply changes to room error(errors.new("update-failed")); end @@ -209,18 +245,33 @@ local function handle_change_notification(event) error_strings = error_strings or {}; --luacheck: ignore 321/error_strings for user_jid, user_err in pairs(user_errors) do error_strings[user_jid] = user_err; - -- module:log("debug", "[%s] TEST5_GROUP_DEBUG Error updating room membership of %s: %s", + module:log("debug", "[%s] TEST5_GROUP_DEBUG Error updating room membership of %s: %s", change_id, user_err.instance_id, user_jid, user_err ); end end + -- if next(affiliations.errors) ~= nil then + -- error_strings = error_strings or {}; + -- for _, err in ipairs(affiliations_result.errors) do + -- local member_url = err.context.sib_member_url; + -- if not member_url then + -- member_url = ("<unknown member %s>"):format(id.short()); + -- end + -- error_strings[member_url] = tostring(err); + -- module:log("warn", "[%s] TEST5_GROUP_DEBUG Error fetching room member info for %s: %s (%s)", + -- change_id, member_url, err, err.context.wrapped_error or "<not native>" + -- ); + -- end + -- end + if error_strings then error(errors.new("partial-update", { sib_member_errors = error_strings })); end end end, function (err) sib_util.log_object(err) + -- module:log("debug", "TEST5_GROUP_DEBUG Error while requesting the content : %s", tostring(err), toString(err.code)); return { status_code = err.code or 500; headers = {