n8n
Version:
n8n Workflow Automation Tool
100 lines • 4.79 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollaborationService = void 0;
const typedi_1 = require("typedi");
const config_1 = __importDefault(require("../config"));
const push_1 = require("../push");
const Logger_1 = require("../Logger");
const collaboration_message_1 = require("./collaboration.message");
const user_service_1 = require("../services/user.service");
const collaboration_state_1 = require("../collaboration/collaboration.state");
const constants_1 = require("../constants");
const user_repository_1 = require("../databases/repositories/user.repository");
const INACTIVITY_CLEAN_UP_TIME_IN_MS = 15 * constants_1.TIME.MINUTE;
let CollaborationService = class CollaborationService {
constructor(logger, push, state, userService, userRepository) {
this.logger = logger;
this.push = push;
this.state = state;
this.userService = userService;
this.userRepository = userRepository;
if (!push.isBidirectional) {
logger.warn('Collaboration features are disabled because push is configured unidirectional. Use N8N_PUSH_BACKEND=websocket environment variable to enable them.');
return;
}
const isMultiMainSetup = config_1.default.get('multiMainSetup.enabled');
if (isMultiMainSetup) {
logger.warn('Collaboration features are disabled because multi-main setup is enabled.');
return;
}
this.push.on('message', async (event) => {
try {
await this.handleUserMessage(event.userId, event.msg);
}
catch (error) {
this.logger.error('Error handling user message', {
error: error,
msg: event.msg,
userId: event.userId,
});
}
});
}
async handleUserMessage(userId, msg) {
if ((0, collaboration_message_1.isWorkflowOpenedMessage)(msg)) {
await this.handleWorkflowOpened(userId, msg);
}
else if ((0, collaboration_message_1.isWorkflowClosedMessage)(msg)) {
await this.handleWorkflowClosed(userId, msg);
}
}
async handleWorkflowOpened(userId, msg) {
const { workflowId } = msg;
this.state.addActiveWorkflowUser(workflowId, userId);
this.state.cleanInactiveUsers(workflowId, INACTIVITY_CLEAN_UP_TIME_IN_MS);
await this.sendWorkflowUsersChangedMessage(workflowId);
}
async handleWorkflowClosed(userId, msg) {
const { workflowId } = msg;
this.state.removeActiveWorkflowUser(workflowId, userId);
await this.sendWorkflowUsersChangedMessage(workflowId);
}
async sendWorkflowUsersChangedMessage(workflowId) {
const activeWorkflowUsers = this.state.getActiveWorkflowUsers(workflowId);
const workflowUserIds = activeWorkflowUsers.map((user) => user.userId);
if (workflowUserIds.length === 0) {
return;
}
const users = await this.userRepository.getByIds(this.userService.getManager(), workflowUserIds);
const msgData = {
workflowId,
activeUsers: users.map((user) => ({
user,
lastSeen: activeWorkflowUsers.find((activeUser) => activeUser.userId === user.id).lastSeen,
})),
};
this.push.sendToUsers('activeWorkflowUsersChanged', msgData, workflowUserIds);
}
};
exports.CollaborationService = CollaborationService;
exports.CollaborationService = CollaborationService = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [Logger_1.Logger,
push_1.Push,
collaboration_state_1.CollaborationState,
user_service_1.UserService,
user_repository_1.UserRepository])
], CollaborationService);
//# sourceMappingURL=collaboration.service.js.map
;