<commit_before>/*
Copyright 2016 OpenMarket Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
Listens for incoming postMessage requests from the integrations UI URL. The following API is exposed:
{
    action: "invite" | "membership_state",
    room_id: $ROOM_ID,
    user_id: $USER_ID
}

The complete request object is returned to the caller with an additional "response" key like so:
{
    action: "invite" | "membership_state",
    room_id: $ROOM_ID,
    user_id: $USER_ID,
    response: { ... }
}

"response" objects can consist of a sole "error" key to indicate an error. These look like:
{
    error: {
        message: "Unable to invite user into room.",
        _error: <Original Error Object>
    }
}
The "message" key should be a human-friendly string.

The response object for "membership_state" looks like:
{
    membership_state: "join" | "leave" | "invite" | "ban"
}

The response object for "invite" looks like:
{
    invite: true
}

*/

const SdkConfig = require('./SdkConfig');
const MatrixClientPeg = require("./MatrixClientPeg");

function sendResponse(event, res) {
    const data = JSON.parse(JSON.stringify(event.data));
    data.response = res;
    event.source.postMessage(data, event.origin);
}

function sendError(event, msg, nestedError) {
    console.error("Action:" + event.data.action + " failed with message: " + msg);
    const data = event.data;
    data.response = {
        error: {
            message: msg,
        },
    };
    if (nestedError) {
        data.response.error._error = nestedError;
    }
    event.source.postMessage(data, event.origin);
}

function inviteUser(event) {
    const roomId = event.data.room_id;
    const userId = event.data.user_id;
    if (!userId) {
        sendError(event, "Missing user_id in request");<commit_msg>Consistency with sendResponse<commit_after>/*
Copyright 2016 OpenMarket Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/*
Listens for incoming postMessage requests from the integrations UI URL. The following API is exposed:
{
    action: "invite" | "membership_state",
    room_id: $ROOM_ID,
    user_id: $USER_ID
}

The complete request object is returned to the caller with an additional "response" key like so:
{
    action: "invite" | "membership_state",
    room_id: $ROOM_ID,
    user_id: $USER_ID,
    response: { ... }
}

"response" objects can consist of a sole "error" key to indicate an error. These look like:
{
    error: {
        message: "Unable to invite user into room.",
        _error: <Original Error Object>
    }
}
The "message" key should be a human-friendly string.

The response object for "membership_state" looks like:
{
    membership_state: "join" | "leave" | "invite" | "ban"
}

The response object for "invite" looks like:
{
    invite: true
}

*/

const SdkConfig = require('./SdkConfig');
const MatrixClientPeg = require("./MatrixClientPeg");

function sendResponse(event, res) {
    const data = JSON.parse(JSON.stringify(event.data));
    data.response = res;
    event.source.postMessage(data, event.origin);
}

function sendError(event, msg, nestedError) {
    console.error("Action:" + event.data.action + " failed with message: " + msg);
    const data = JSON.parse(JSON.stringify(event.data);
    data.response = {
        error: {
            message: msg,
        },
    };
    if (nestedError) {
        data.response.error._error = nestedError;
    }
    event.source.postMessage(data, event.origin);
}

function inviteUser(event) {
    const roomId = event.data.room_id;
    const userId = event.data.user_id;
    if (!userId) {
        sendError(event, "Missing user_id in request");
{'fraction_non_alphanumeric': 0.10263674003050774, 'fraction_numerical': 0.003486598387448246, 'mean_word_length': 3.469328140214216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '477099', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1345, 'n_words': 481}