private static void AddMessage(SlackHistoryViewerDbContext dbContext, Message message, string channelId)
        {
            var newMessage = new Messages();

            newMessage.MessageId = MD5Hasher.GetMd5Hash(message.User + message.TimeStamp);

            var idUser = dbContext.Users
                         .Where(u => u.UserId == message.User)
                         .Select(u => u.Id)
                         .FirstOrDefault();

            if (idUser == 0)
            {
                var botId = dbContext.Users
                            .Where(u => u.UserId == AppSettings.Instance.UnknownBotId)
                            .Select(u => u.Id)
                            .FirstOrDefault();

                idUser = botId;
            }

            newMessage.UserId = idUser;

            var idChannel = dbContext.Channels
                            .Where(c => c.ChannelId == channelId)
                            .Select(c => c.Id)
                            .FirstOrDefault();

            newMessage.ChannelId = idChannel;
            newMessage.JsonData  = JsonConvert.SerializeObject(message);

            dbContext.Messages.Add(newMessage);
        }
        private static bool CheckDuplicateMessage(SlackHistoryViewerDbContext dbContext, string hash)
        {
            var result = dbContext.Messages
                         .Where(m => m.MessageId == hash)
                         .FirstOrDefault();

            return(result != null);
        }
        private static bool CheckDuplicateUser(SlackHistoryViewerDbContext dbContext, string id)
        {
            var result = dbContext.Users
                         .Where(u => u.UserId == id)
                         .FirstOrDefault();

            return(result != null);
        }
        private static bool CheckDuplicateChannel(SlackHistoryViewerDbContext dbContext, string id)
        {
            var result = dbContext.Channels
                         .Where(c => c.ChannelId == id)
                         .FirstOrDefault();

            return(result != null);
        }
Esempio n. 5
0
        public static void InsertChannels(string path)
        {
            using (var dbContext = new SlackHistoryViewerDbContext())
            {
                var data = GetData <Channel>(path, AppSettings.Instance.ChannelsFile);

                dbContext.Channels.AddRange(data.Select(c => new Channels(c.Id, c.Name)));

                dbContext.SaveChanges();
            }
        }
        public static void InsertChannelsWithoutDuplicates(IEnumerable <Channel> channels)
        {
            using (var dbContext = new SlackHistoryViewerDbContext())
            {
                var newChannels = channels
                                  .Where(c => !CheckDuplicateUser(dbContext, c.Id))
                                  .Select(c => new Channels(c.Id, c.Name));

                dbContext.Channels.AddRange(newChannels);

                dbContext.SaveChanges();
            }
        }
        public static void InsertUsersWithoutDuplicates(IEnumerable <Member> users)
        {
            using (var dbContext = new SlackHistoryViewerDbContext())
            {
                var newUsers = users
                               .Where(u => !CheckDuplicateUser(dbContext, u.Id))
                               .Select(u => new Users(u.Id, u.Name));

                dbContext.Users.AddRange(newUsers);

                dbContext.SaveChanges();
            }
        }
Esempio n. 8
0
        public static void InsertUsers(string path)
        {
            using (var dbContext = new SlackHistoryViewerDbContext())
            {
                var data = GetData <Member>(path, AppSettings.Instance.UsersFile);

                dbContext.Users.AddRange(data.Select(u => new Users(u.Id, u.Name)));
                dbContext.Users.Add(new Users(AppSettings.Instance.SlackBotId, AppSettings.Instance.SlackBotName));
                dbContext.Users.Add(new Users(AppSettings.Instance.UnknownBotId, AppSettings.Instance.UnknownBotName));

                dbContext.SaveChanges();
            }
        }
Esempio n. 9
0
        public static void TruncateTables(params string[] tableNames)
        {
            using (var dbContext = new SlackHistoryViewerDbContext())
            {
                var sql = string.Empty;

                foreach (var tableName in tableNames)
                {
                    sql = string.Format("DELETE FROM {0}", tableName);
                    dbContext.Database.ExecuteSqlCommand(sql);

                    sql = string.Format("DBCC CHECKIDENT ({0}, RESEED, 0)", tableName);
                    dbContext.Database.ExecuteSqlCommand(sql);
                }
            }
        }
        public static void InsertMessagesWithoutDuplicates(IEnumerable <Message> messages, string channelId)
        {
            using (var dbContext = new SlackHistoryViewerDbContext())
            {
                foreach (Message message in messages)
                {
                    var key  = message.User + message.TimeStamp;
                    var hash = MD5Hasher.GetMd5Hash(key);

                    if (!CheckDuplicateMessage(dbContext, hash))
                    {
                        AddMessage(dbContext, message, channelId);
                    }
                }

                dbContext.SaveChanges();
            }
        }
Esempio n. 11
0
        public static void InsertMessages(string path)
        {
            using (var dbContext = new SlackHistoryViewerDbContext())
            {
                var files = Directory.GetFiles(@path, "*.*", SearchOption.AllDirectories);

                foreach (string file in files)
                {
                    if (IsFileIgnored(file))
                    {
                        continue;
                    }

                    var words     = file.Split('\\');
                    var channel   = words[words.Length - 2];
                    var channelId = dbContext.Channels
                                    .Where(c => c.Name == channel)
                                    .Select(c => c.ChannelId)
                                    .FirstOrDefault();

                    var text = File.ReadAllText(file);
                    var data = JsonConvert.DeserializeObject <List <Message> >(text);

                    foreach (var message in data)
                    {
                        if (IsAttachment(message))
                        {
                            continue;
                        }

                        AddMessage(dbContext, message, channelId);
                    }

                    dbContext.SaveChanges();
                }
            }
        }