Ejemplo n.º 1
0
        internal async Task <ContentItemVariantModel <UserModel> > CreateUser(IntercomUser user)
        {
            string itemName;

            if (user.Type == "admin")
            {
                itemName = user.Name;
            }
            else
            {
                itemName = user.Id;
            }

            var userItem = await CreateUserItem(itemName, user.Id);

            UserModel userModel = new UserModel
            {
                Name         = user.Name,
                Type         = user.Type,
                Email        = user.Email,
                Id           = user.Id,
                IntercomLink = "<p><a href=\"" + IntercomFunctions.GetIntercomUserLink(user.Id, user.Type) + "\">Link to intercom</a></p>"
            };

            var userVariant = await CreateUserVariant(userModel, ContentItemIdentifier.ById(userItem.Id));

            var publishUser = await PublishItemVariant(userItem.Id.ToString());

            return(userVariant);
        }
Ejemplo n.º 2
0
        private ConversationModel CreateConversationModel(Conversation conversation)
        {
            var    messages = CreateRichTextCompatibleConversationString(conversation);
            var    tags     = CreateTagsString(conversation);
            string intercomConversationLinkRichText = "<p><a href=\"" + IntercomFunctions.GetIntercomConversationLink(conversation) + "\">Link to intercom</a></p>";

            List <ContentItemIdentifier> assignees = new List <ContentItemIdentifier>()
            {
            };
            List <ContentItemIdentifier> author = new List <ContentItemIdentifier>()
            {
            };

            if (conversation.assignee.id != null) //unassigned conversation
            {
                assignees.Add(ContentItemIdentifier.ByExternalId(currentConversationUsers
                                                                 .Where(x => x.Id.Equals(conversation.assignee.id, StringComparison.OrdinalIgnoreCase))
                                                                 .FirstOrDefault()
                                                                 .Id));
            }

            author.Add(ContentItemIdentifier.ByExternalId(currentConversationUsers
                                                          .Where(x => x.Id.Equals(conversation.conversation_message.author.id, StringComparison.OrdinalIgnoreCase))
                                                          .FirstOrDefault()
                                                          .Id));

            ConversationModel conversationModel = new ConversationModel
            {
                IntercomLink = intercomConversationLinkRichText,
                Author       = author,
                CreatedAt    = Tools.FromUnixTimestamp(conversation.created_at),
                LastUpdated  = Tools.FromUnixTimestamp(conversation.updated_at),
                Assignee     = assignees,
                Messages     = messages,
                RatingValue  = conversation.conversation_rating.rating,
                RatingNote   = conversation.conversation_rating.remark,
                Tags         = tags,
                SearchBody   = CreateSearchBody(conversation),

                /*Participants = userItemVariants
                 *  .Where(userReferences => participants.Any(participant => participant.Id.Equals(userReferences.Key, StringComparison.OrdinalIgnoreCase)))
                 *  .Select(userReference => userReference.Value)
                 *  .ToList(),*/
                Participants = currentConversationUsers
                               .Select(x => ContentItemIdentifier.ByExternalId(x.Id))
                               .ToList(),
                MessageCount   = IntercomFunctions.GetConversationMessageCount(conversation),
                ConversationId = conversation.id
            };

            return(conversationModel);
        }
Ejemplo n.º 3
0
        public SearchProjectClient(string intercomApiAuthKey, KontentFunctionsSettings kontentSettings, AlgoliaSettings algoliaSettings)
        {
            intercom       = new IntercomFunctions(intercomApiAuthKey);
            kontent        = new KontentFunctions(kontentSettings);
            algolia        = new AlgoliaFunctions(algoliaSettings);
            processedItems = new Dictionary <string, bool>();

            emptyProject        = kontentSettings.CleanProject;
            bannedConversations = kontentSettings.BannedConversations.Split(',');

            searchConversations = new Dictionary <string, SearchConversation>()
            {
            };
            searchUsers = new Dictionary <string, SearchUser>()
            {
            };
        }
Ejemplo n.º 4
0
        public async Task <bool> SyncSingle(string conversationId)
        {
            if (bannedConversations.Contains(conversationId))
            {
                // This is currently for conversations with big data, which fails in KK
                logger.Info("Conversation is banned: " + conversationId);
                return(false);
            }

            Conversation intercomConversation;

            // Get the conversation from Intercom
            try
            {
                intercomConversation = intercom.GetConversation(conversationId);
                if (intercomConversation == null)
                {
                    logger.Error("Conversation was returned empty: " + conversationId);
                    return(false);
                }
            }
            catch (Exception e)
            {
                logger.Error(e, "Wasn't able to get the conversation from intercom: " + conversationId);
                return(false);
            }

            // Try to get conversation from existing project
            ContentItemVariantModel <ConversationModel> conversationVariant = null;

            if (!emptyProject)
            {
                conversationVariant = await kontent.TryGetExistingConversationVariant(intercomConversation.id);
            }

            logger.Debug("Synchronizing conversation to Kentico Kontent: " + conversationId);
            if (!ConversationNeedsUpdate(intercomConversation, conversationVariant))
            {
                return(true);
            }
            else
            {
                if (conversationVariant != null)
                {
                    await kontent.UnpublishItemVariant(conversationVariant.Item.Id.ToString());
                }
            }

            // Extract all generic users participating in the conversation (id/type)
            var genericParticipants = IntercomFunctions.GetAllConversationParticipants(intercomConversation);
            List <ContentItemVariantModel <UserModel> > conversationUsers = new List <ContentItemVariantModel <UserModel> > {
            };

            logger.Debug("Trying to get existing user variants from Kontent.");
            foreach (var genericParticipant in genericParticipants)
            {
                ContentItemVariantModel <UserModel> userVariant;
                userVariant = await kontent.TryGetExistingUserVariant(genericParticipant.Id);

                if (userVariant == null)
                {
                    var intercomUser = intercom.GetIntercomUser(genericParticipant);
                    userVariant = await kontent.CreateUser(intercomUser);
                }
                conversationUsers.Add(userVariant);
            }

            var result = await kontent.SyncSingle(intercomConversation, conversationVariant, conversationUsers);

            if (!result.success)
            {
                return(false);
            }

            conversationVariant = result.variant;

            // synchronizovat do algolie
            logger.Debug("Synchronizing object to Algolia: " + conversationId);
            List <SearchUser> searchUserParticipants = new List <SearchUser>()
            {
            };
            SearchUser assignee;

            foreach (var user in conversationUsers)
            {
                searchUserParticipants.Add(new SearchUser(user.Elements.Name, user.Elements.Email));
            }
            if (intercomConversation.assignee.id != null)
            {
                var searchAssignee = intercom.GetIntercomUser(new GenericIntercomUser(intercomConversation.assignee.id, intercomConversation.assignee.type));
                assignee = new SearchUser(searchAssignee.Name, searchAssignee.Email);
            }
            else
            {
                assignee = new SearchUser("unassigned", "");
            }

            var searchConversation = new SearchConversation(conversationVariant.Elements, assignee, searchUserParticipants);

            await algolia.UpdateObject(searchConversation);

            return(true);
        }