private DataEntities.Chat CreateChat(CreateOrUpdateChatOptions options)
        {
            var chatMemberLogins = (IList <string>)options.ExtraData;

            var chat = mapper.Map <DataEntities.Chat>(options);

            chat.LastUpdate = DateTime.Now;
            chatRepository.SaveChat(chat);

            var initiator           = this.userService.GetUser(options.InitiatorLogin);
            var adminChatPermission = new AddOrUpdateChatPermissionOptions(
                userLogin: initiator.Login,
                chatId: chat.ChatId,
                permissionType: ChatPermissionTypes.Administrate,
                initiatorLogin: ChatPermissionService.SYSTEM_PERMISSION,
                lastUpdate: DateTime.Now);

            this.chatPermissionService.AddOrUpdateChatPermission(adminChatPermission);

            foreach (var userLogin in chatMemberLogins)
            {
                var userChatPermission = new AddOrUpdateChatPermissionOptions(
                    userLogin: userLogin,
                    chatId: chat.ChatId,
                    permissionType: chat.ChatType == (int)ChatTypes.PrivateChat
                        ? ChatPermissionTypes.Administrate
                        : ChatPermissionTypes.ReadWrite,
                    initiatorLogin: initiator.Login,
                    lastUpdate: DateTime.Now);
                this.chatPermissionService.AddOrUpdateChatPermission(userChatPermission);
            }

            return(chat);
        }
        private DataEntities.Chat UpdateChat(CreateOrUpdateChatOptions options)
        {
            var chat = this.chatRepository.GetChat(options.ChatId);

            chat.Title      = options.Title;
            chat.LastUpdate = DateTime.Now;
            this.chatRepository.SaveChat(chat);

            var chatMemberLogins    = (IList <string>)options.ExtraData;
            var existingChatMembers = chat.ChatPermissions
                                      .Where(chp => chp.Chat.ChatId == chat.ChatId)
                                      .Select(chp => chp.ChatUser.Login)
                                      .ToList();
            var newChatMembers = chatMemberLogins
                                 .Where(nchm => !existingChatMembers.Contains(nchm));

            foreach (var userLogin in newChatMembers)
            {
                var userChatPermission = new AddOrUpdateChatPermissionOptions(
                    userLogin: userLogin,
                    chatId: chat.ChatId,
                    permissionType: chat.ChatType == (int)ChatTypes.PrivateChat
                        ? ChatPermissionTypes.Administrate
                        : ChatPermissionTypes.ReadWrite,
                    initiatorLogin: options.InitiatorLogin,
                    lastUpdate: DateTime.Now);
                this.chatPermissionService.AddOrUpdateChatPermission(userChatPermission);
            }

            return(chat);
        }
        public OperationResult CreateOrUpdateChat(CreateOrUpdateChatOptions options)
        {
            OperationResult operationResult;

            try
            {
                var domainOptions = mapper.Map <DomainOptions.CreateOrUpdateChatOptions>(options);
                var domainChat    = this.chatService.CreateOrUpdateChat(domainOptions);
                var resultOptions = this.mapper.Map <CreateOrUpdateChatOptions>(domainChat);
                resultOptions.MembersLogins = options.MembersLogins;
                if (!resultOptions.MembersLogins.Contains(options.InitiatorLogin))
                {
                    resultOptions.MembersLogins.Add(options.InitiatorLogin);
                }

                foreach (var userLogin in resultOptions.MembersLogins)
                {
                    var chatMember = this.userService.GetUser(userLogin);
                    CreateOrUpdateChatForClient(
                        chatMember.Login,
                        resultOptions);
                }

                operationResult = new OperationResult(OperationResultTypes.Success);
            }
            catch (Exception exception)
            {
                operationResult = new OperationResult(
                    operationResultType: OperationResultTypes.Failure,
                    message: exception.ToString());
            }

            return(operationResult);
        }
Example #4
0
        public void AddOrUpdateChat(CreateOrUpdateChatOptions options)
        {
            var chat = this.mapper.Map <Chat>(options);

            this.chatService.AddOrUpdateChat(chat);

            var chatPermissionOptions = (AddOrUpdateChatPermissionOptions)options.ExtraData;
            var chatPermission        = this.mapper.Map <ChatPermission>(chatPermissionOptions);

            this.userService.SaveClientChatPermission(chatPermission);

            ChatEventsConnector.OnAddOrUpdateChat(chat);
            ChatEventsConnector.OnUpdateChatPermission(chatPermission);
        }
        private void CreateOrUpdateChatForClient(
            string userLogin,
            CreateOrUpdateChatOptions options)
        {
            try
            {
                if (this.chatServiceCallbacks.ContainsKey(userLogin))
                {
                    var domainChatPermission  = this.chatPermissionService.GetChatPermission(userLogin, options.ChatId);
                    var chatPermissionOptions = this.mapper.Map <AddOrUpdateChatPermissionOptions>(domainChatPermission);
                    options.ExtraData = chatPermissionOptions;

                    var usersChatServiceCallback = this.chatServiceCallbacks[userLogin];
                    usersChatServiceCallback.AddOrUpdateChat(options);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
        public Chat CreateOrUpdateChat(CreateOrUpdateChatOptions options)
        {
            _ = options ?? throw new ArgumentNullException("Can not create or update chat, because options are null");

            bool isNewChat = chatRepository.GetChat(options.ChatId) == null;

            if (!chatPermissionService.DoesUserHavePermission(
                    options.InitiatorLogin,
                    options.ChatId,
                    ChatPermissionTypes.Moderate) &&
                !isNewChat)
            {
                throw new ArgumentException($"Can not save chat, because initiator " +
                                            $"(user #{options.InitiatorLogin.ToString()}) does not have permissions");
            }

            var chat = isNewChat
                ? CreateChat(options)
                : UpdateChat(options);

            var domainChat = this.mapper.Map <Chat>(chat);

            return(domainChat);
        }