Example #1
0
 internal async Task <bool> UpdateObject(SearchConversation conversation)
 {
     try
     {
         if (conversation.ConversationSplit == "ConversationPart")
         {
             var parts = conversation.SplitConversation();
             await index.SaveObjectsAsync(parts);
         }
         else
         {
             await index.SaveObjectAsync(conversation);
         }
         return(true);
     }
     catch (Exception e)
     {
         logger.Error(e, "Unable to update object.");
         return(false);
     }
 }
        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);
        }