Ejemplo n.º 1
0
        private async Task ProcessTeamJoinEvent(SlackEventFullDto <TeamJoinEventDto> slackEventDto)
        {
            _userStorage = new UserEntity();

            if (slackEventDto.Event.User.IsBot)
            {
                return;
            }

            string workspaceId       = slackEventDto.Event.User.SlackTeamId;
            string workspaceMemberId = slackEventDto.Event.User.SlackId;

            SlackUserInfoDto slackUserInfoDto = await _slackService.GetSlackUserInfo(workspaceMemberId);

            string chatAppMemberEmail = slackUserInfoDto.User.Profile.Email;

            UserEntity existingUser = await _userStorage.FindAsync(u => u.Email == chatAppMemberEmail);

            if (existingUser == null)
            {
                // User email is not registered to frontend so
                // register user and also associate new chat app
                // user to new user entity
                string username = await GenerateUsername();

                string     password   = GenerateTemporaryPassword();
                User       user       = new User(username, slackUserInfoDto.User.Profile.Email, slackUserInfoDto.User.Timezone, slackUserInfoDto.User.Locale, true, "", slackEventDto.Event.User.Profile.Image192);
                UserEntity userEntity = _mapper.Map <UserEntity>(user);
                userEntity.HashedPassword = HashPassword(user, password);


                UserEntity newUser = await _userStorage.CreateAsync(userEntity);

                ChatAppUserEntity newChatAppUser = new ChatAppUserEntity()
                {
                    WorkspaceId       = workspaceId,
                    WorkspaceMemberId = workspaceMemberId,
                    UserId            = newUser.Id,
                };

                await _chatAppUserStorage.CreateAsync(newChatAppUser);

                string message = Messages.OnboardingMessage(workspaceMemberId, user.Email, password);
                await _slackService.ChatPostMessage(workspaceMemberId, message);

                await _slackService.ChatPostMessage(_privateRegistrationChannelId, $"<@{workspaceMemberId}>, `{username}`, `{user.Email}` joined the slack group. Send follow up.");
            }
            else
            {
                // User email is already register to frontend
                // associate new chat user record with existing user
                // TODO: Picture may be overwritten here if member
                // previously uploaded a profile pic through frontend,
                // remove once we have profile upload functionality on UI.
                existingUser.ProfilePictureUrl = slackEventDto.Event.User.Profile.Image192;

                ChatAppUserEntity newChatAppUser = new ChatAppUserEntity()
                {
                    WorkspaceId       = workspaceId,
                    WorkspaceMemberId = workspaceMemberId,
                    UserId            = existingUser.Id,
                };

                await _userStorage.UpdateAsync(existingUser);

                await _chatAppUserStorage.CreateAsync(newChatAppUser);

                string message = Messages.OnboardingRegisteredMessage(workspaceMemberId);
                await _slackService.ChatPostMessage(workspaceMemberId, message);

                await _slackService.ChatPostMessage(_privateRegistrationChannelId, $"<@{workspaceMemberId}>, `{existingUser.Username}`, `{existingUser.Username}` registered and joined the slack group. Send follow up.");
            }

            return;
        }