local session_user_id = SESSION_USER.Id -- текущий пользователь
local chat_subcat_id = 969 -- системная категория "Общение"
local pubParams = UTILS:json_decode(EVENTPARAMS["PublishedObjectParameters"])
--pubParams = {requestBody = {roomName = 'имя_комнаты_пользователя'}}
local roomName = pubParams.requestBody.roomName
-- Функция получает параметры ВКС
function get_jitsi_settings()
local res = SQL:query(
[[
SELECT TOP 1 jss.Domain, jss.UserRoomPrefix
FROM JitsiServiceSettings jss
]],
{}
)
return res[1];
end
-- Функция получает UserId по нику пользователя
function get_user_id_by_nick(nick)
local res = SQL:scalar(
[[
SELECT TOP 1 UserId
FROM Users WHERE Nick = @nick
]],
{
nick = nick
}
)
if res then return res else return nil end
end
-- Функция получает TaskId чата между пользователями
function get_chat_id(user1, user2)
local res = SQL:scalar (
[[
SELECT TOP 1 t.TaskId
FROM Tasks t
JOIN MailSubscribers ms ON ms.TaskId = t.TaskId AND ms.UserID = @user1
JOIN MailSubscribers ms2 ON ms2.TaskId = t.TaskId AND ms2.UserID = @user2
OUTER APPLY (
SELECT COUNT(*) as tot FROM MailSubscribers WHERE TaskId = t.TaskId
) ext
WHERE t.SubcatID = @chat_subcat_id AND t.IsClosed = 0 AND ext.tot = 2
]],
{
user1 = user1,
user2 = user2,
chat_subcat_id = chat_subcat_id
}
);
return res
end
-- Функция создает чат между пользователями
function create_chat(user1, user2)
local users = SQL:query (
[[
SELECT DisplayName AS name FROM Users WHERE UserID = @user1 OR UserID = @user2
]],
{
user1 = user1,
user2 = user2
}
);
local res = SMART:execute_action('CreateTask', nil, 'task', {
Owner = session_user_id,
Subcat = chat_subcat_id,
TaskText = users[1].name .. ', ' .. users[2].name,
CreateLink = false,
CreateSubtask = false,
NewTaskCopySubscribers = false,
CreateCopyFiles = false,
CopyParentText = false,
Priority = 1,
UsersToSubscribe = {user1, user2},
CreateCopiesForEachPerformer = false,
})
return res[0];
end
-- Логика публикации
local jitsi_settings = get_jitsi_settings()
local room_user_nick = string.sub(roomName, 5)
local room_user_id = get_user_id_by_nick(room_user_nick);
local jitsi_link = 'https://' .. jitsi_settings['Domain']:gsub("%s+", "") .. '/' .. jitsi_settings['UserRoomPrefix']:gsub("%s+", "") .. room_user_nick
local chat_id = get_chat_id(session_user_id, room_user_id)
if chat_id == nil then
chat_id = create_chat(room_user_id, session_user_id)
end
SMART:execute_action('PostComment', chat_id, 'task', {
CommentAuthor = session_user_id,
CommentText = 'ВКС: ожидаю в комнате ' .. jitsi_link,
Task = chat_id,
Recipients = {room_user_id},
ForcedEmail = false,
CommentSMS = false,
NoSubscription = false,
TextAsHTML = false,
})
-- Возвращает результат
RESULT = '{"success": true, "chatTaskId": ' .. chat_id .. '}'
do return end
|