public DiscourseMigration(DiscourseService discourse, RsPeerContext db, NodeBbService nodebb, IMediator mediator, IServiceScopeFactory factory)
 {
     _discourse = discourse;
     _db        = db;
     _nodebb    = nodebb;
     _mediator  = mediator;
     _factory   = factory;
 }
        private async Task OnTopic(NodeBBTopic topic, NodeBbService nodebb, DiscourseService discouse, RsPeerContext db)
        {
            var post = await nodebb.GetPost(topic.PostId);

            if (post == null)
            {
                return;
            }

            var user = await nodebb.GetUser(topic.UserId);

            if (user == null)
            {
                return;
            }

            var createdAt = DateTimeOffset.FromUnixTimeMilliseconds((long)topic.Timestamp).ToString("O");

            Console.WriteLine("Creating: " + topic.Title + " " + createdAt);

            var topicId = await discouse.CreateTopic(new DiscourseTopicRequest
            {
                Content   = post.Content,
                Category  = discouse.GetCategory(topic.Category),
                CreatedAt = createdAt,
                Title     = topic.Title
            }, user);

            Console.WriteLine("Updating forum thread with nodebb topic id: " + topic.TopicId);
            var sql = $"UPDATE scripts SET discoursethread = {topicId} where forumthread like '%/topic/{topic.TopicId}/%'";

            var update = await db.Database.GetDbConnection().ExecuteAsync(sql);

            Console.WriteLine(update);

            File.AppendAllText("topics_created.txt", $"{topic.TopicId}{Environment.NewLine}");

            await CreatePostsForTopic(topic.TopicId, topicId, nodebb, discouse);
        }
        private async Task CreatePostsForTopic(int nodeTopicId, int newTopicId, NodeBbService nodebb, DiscourseService discourse)
        {
            var posts = await nodebb.GetPostsForTopic(nodeTopicId);

            foreach (var post in posts)
            {
                try
                {
                    var createdAt = DateTimeOffset.FromUnixTimeMilliseconds((long)post.Timestamp).ToString("O");
                    var created   = await discourse.CreatePost(new DiscoursePostRequest
                    {
                        TopicId   = newTopicId,
                        CreatedAt = createdAt,
                        Content   = post.Content
                    }, await nodebb.GetUser(post.UserId));

                    Console.WriteLine($"Created post {created} for topic {nodeTopicId}");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    var user = await nodebb.GetUser(post.UserId);

                    Console.WriteLine($"Failed to create post: {post.Content} with user: " + user?.Email);
                }
            }
        }