'use strict'; const fs = require("fs"); const axios = require('axios'); const qs = require('qs'); /** * Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers) * to customize this controller */ //https://helpx.adobe.com/adobe-connect/webservices/topics.html //https://helpx.adobe.com/adobe-connect/webservices/launch-meetings-external-authentication.html //https://helpx.adobe.com/adobe-connect/webservices/create-meeting-room-urls.html#create_meeting_room_urls //https://helpx.adobe.com/adobe-connect/webservices/create-meetings.html module.exports = { async create(ctx) { const model = {...ctx.request.body}; if (model.isAdobe) { const instance = axios.create({ baseURL: `${process.env.AC_SERVER}`, headers: {'X-Custom-Header': 'foobar'}, withCredentials: true }); let apiCall = `/api/xml?action=login&login=${process.env.AC_USER}&password=${process.env.AC_PASSWORD}`; try { const tokenInfo = await instance.get(apiCall, {withCredentials: true}); if (tokenInfo && tokenInfo.data) { let tokenData = await strapi.services.helpers.xml2Json(tokenInfo.data); tokenData = JSON.parse(tokenData); if (tokenData && tokenData.results && tokenData.results.status[0].$.code === "ok") { instance.defaults.headers = { Cookie: tokenInfo.headers["set-cookie"][0] }; apiCall = `/api/xml?action=sco-update&session=${tokenData.results.OWASP_CSRFTOKEN[0].token[0]}&type=meeting&name=${model.title}&folder-id=${process.env.AC_MEETING_FOLDER}`; const meetingInfo = await instance.get(encodeURI(apiCall), {withCredentials: true}); if (meetingInfo && meetingInfo.data) { let meetingData = await strapi.services.helpers.xml2Json(meetingInfo.data); meetingData = JSON.parse(meetingData); let invalid = meetingData.results.status[0].$.code === "invalid"; if (invalid) { invalid = meetingData.results.status[0].invalid[0]; return ctx.badRequest(null, `${invalid.$.field} ${invalid.$.subcode}`); } //make public apiCall = `/api/xml?action=permissions-update&acl-id=${meetingData.results.sco[0].$["sco-id"]}&principal-id=public-access&permission-id=view-hidden`; let updatePrincipal = await instance.get(apiCall, {withCredentials: true}); if (updatePrincipal && updatePrincipal.data) { apiCall = `/api/xml?action=permissions-update&principal-id=${process.env.AC_ADMIN_ID}&acl-id=${meetingData.results.sco[0].$["sco-id"]}&permission-id=host`; updatePrincipal = await instance.get(apiCall, {withCredentials: true}); if (updatePrincipal && updatePrincipal.data) { let principal = await strapi.services.helpers.xml2Json(updatePrincipal.data); principal = JSON.parse(principal); if (principal && principal.results && principal.results.status[0].$.code === "ok") { model.internalMeetingID = meetingData.results.sco[0]["url-path"][0]; const entity = await strapi.services.session.create(model); const result = { joinUrl: `${process.env.AC_SERVER}${entity.internalMeetingID.replace(/\//g, '')}`, }; return ctx.send(result); } } } } } } } catch (ex) { console.error("create meeting error", ex); let msg = ex.message; if (msg && msg.indexOf(".") >= 0) { msg = msg.replace(/./g, "-"); } else if (!msg) { msg = "خطای نامشخص"; } return ctx.badRequest(null, msg); } } else { if (model.attendeePW) { model.attendeePW = model.attendeePW.toString(); } const {user} = ctx.state; model.meta_isPublic = model.guestPolicy === "ALWAYS_ACCEPT"; model.meta_isPublicRegistred = model.guestPolicy === "ALWAYS_ACCEPT_REGISTERED"; model.meta_isPrivate = model.guestPolicy === "ALWAYS_DENY"; if (model.guestPolicy === "ALWAYS_DENY") { model.guestPolicy = "ASK_MODERATOR"; } if (model.guestPolicy === "ALWAYS_ACCEPT_REGISTERED") { model.guestPolicy = "ALWAYS_ACCEPT"; } model.link = ""; let meetingID = model.course_level; if (model.course_level === "public") { model.course_level = null; } const entity = await strapi.services.session.create(model); if (meetingID === "public") { meetingID = entity.id; } delete model.desc; delete model.title; model.meta_disallowRecording = model.disallowRecording; //id is COURSE_LEVEL_ID const meetingInfo = await strapi.services.helpers.createBBBMeeting(entity.title, meetingID, model); meetingInfo.dialNumber = meetingInfo.dialNumber ? meetingInfo.dialNumber.toString() : ''; meetingInfo.voiceBridge = meetingInfo.voiceBridge ? meetingInfo.voiceBridge.toString() : ''; await strapi.services.session.update({id: entity.id}, meetingInfo); const fullName = "مدرس: " + user.name + " " + (user.family!=null ? user.family : ""); const result = { joinUrl: await strapi.services.helpers.joinUserToMeeting(fullName, meetingID, meetingInfo.moderatorPW, user.id, false) }; if (model.meta_isPrivate) { const data = fs.readFileSync(`resources/emailTemplate/PrivateSession.html`, 'utf8'); const html = data.replace(/SESSION_TITLE/g, entity.title) .replace(/PASSWORD/g, model.attendeePW); strapi.services.helpers.sendEmail("papinaser@gmail.com", "رمز عبور جلسه خصوصی", html).then(); } return ctx.send(result); } //return sanitizeEntity(entity, { model: strapi.models.session }); }, async getSessionResources(ctx) { const {id} = ctx.params;//course_level_id const {user} = ctx.state; const entity = await strapi.services["session"].findOne({id}); const levels = await strapi.services.helpers.getCourseLevels(entity.course_level); const ids = levels.map(x => x.id); const users = await strapi.query('user-course').find({ isConfirm: true, course_levels_in: ids }); const availableUsers = users.map(x => ({ id: x.user.id, name: x.user.name, family: x.user.family, phoneNo: x.user.phoneNo, nationalCode: x.user.nationalCode })); const availableResources = await strapi.query("session-resource").find({ teacher_in: [user.id, null] }); return { availableUsers, availableResources, session_resources: entity.session_resources.map(x => x.id), disallow_users: entity.disallow_users.map(x => x.id) }; }, async saveSessionResources(ctx) { const model = {...ctx.request.body}; const entity = await strapi.services.session.update({id: model.id}, {session_resources: model.session_resources, disallow_users: model.disallow_users}); ctx.send(true); }, async joinUserToMeeting(ctx) { const {id} = ctx.params;//course_level_id const {user} = ctx.state; const adobeInfo = await strapi.services.helpers.isAdobeRunning(id, true); if (adobeInfo !== false) { const result = { joinUrl: adobeInfo }; return ctx.send(result); } const info = await strapi.services.helpers.getBBBMeetingInfo(id); if (info.returncode !== "SUCCESS" || !info.running) { return ctx.badRequest(null, "buyCourse.errors.classIsNotLive"); } const fullName = "مدرس: " + user.name + " " + (user.family!=null ? user.family : ""); const result = { joinUrl: await strapi.services.helpers.joinUserToMeeting(fullName, id, info.moderatorPW, user.id, false) }; return ctx.send(result); }, async getResourcesForLevel(ctx) { const {id} = ctx.params;//course_level_id const {user} = ctx.state; const levels = await strapi.services.helpers.getCourseLevels(id); const canAccess= await strapi.services.session.canAccessResources(user,levels,id); const isAdmin = strapi.services.helpers.isAdmin(user); if (canAccess.result===false){ return ctx.badRequest(null,canAccess.entity); } const ids = levels.map(x => x.id); const userId = user.id; const sessions = await strapi.query('session').find({ isPublished: true, course_level_in: ids, disallow_users_nin: [userId] }); sessions.map(session=>{ session.checkLicense= !isAdmin && !canAccess.userCourse.inactiveLicense && session.checkLicense session.canDownload = isAdmin || canAccess.userCourse.canDownload }) return sessions; }, };