Example #1
0
        public static void LoadRoomMessages(string personalAccessToken, long roomId)
        {
            var            client = new HipchatClient(personalAccessToken);
            DateTimeOffset until  = DateTimeOffset.Now;

            bool hitExistingMessage = false;

            while (true)
            {
                using (var ctx = new HipchatEntities())
                {
                    Task <HttpResponseMessage> response = client.GetAsync(
                        $"/v2/room/{roomId}/history?reverse=false&max-results=1000&date={until.ToUnixTimeSeconds()}");
                    response.Wait();
                    Task <string> content = response.Result.Content.ReadAsStringAsync();
                    content.Wait();
                    var    obj   = JsonConvert.DeserializeObject <JObject>(content.Result);
                    JArray items = (JArray)obj["items"];

                    if (items == null || items.Count == 0 || hitExistingMessage)
                    {
                        Console.WriteLine("Done");
                        break;
                    }

                    foreach (var item in items)
                    {
                        DateTimeOffset d = DateTimeOffset.Parse(item.Value <string>("date"));
                        if (d < until)
                        {
                            until = d;
                        }

                        if (item.Value <string>("type") == "message")
                        {
                            Message msg = ConvertToMessage(item, roomId);
                            if (ctx.Messages.Any(rm => rm.id == msg.id))
                            {
                                hitExistingMessage = true;
                                continue;
                            }

                            ctx.Messages.Add(msg);
                        }
                    }

                    Console.WriteLine($"Loaded history since {until.ToString()}");


                    // This fixes the condition where one message keeps getting returned at end
                    until = until.AddSeconds(-1);
                    ctx.SaveChanges();
                }
            }
        }
Example #2
0
        public static void LoadUsers(string url = "https://api.hipchat.com/v2/user?max-results=1000&startIndex=0&include-guests=false&include-deleted=false")
        {
            var client = new HipchatClient(_personalAccessToken);
            Task <HttpResponseMessage> response = client.GetAsync(url);

            response.Wait();
            Task <string> y = response.Result.Content.ReadAsStringAsync();

            y.Wait();
            var     resObj = JObject.Parse(y.Result);
            JObject links  = resObj.Value <JObject>("links");
            string  next   = links?.Value <string>("next");

            if (next != null)
            {
                LoadUsers(next);
            }

            JArray items = resObj.Value <JArray>("items");

            using (var ctx = new HipchatEntities())
            {
                foreach (var i in items)
                {
                    long   userId      = i.Value <long>("id");
                    string name        = i.Value <string>("name");
                    string mentionName = i.Value <string>("mention_name");

                    Console.WriteLine($"Loading user '{name}'...");

                    var user = ctx.Users.FirstOrDefault(r => r.id == userId);
                    if (user == null)
                    {
                        user = new User()
                        {
                            name = name, id = userId, mention_name = mentionName
                        };
                        ctx.Users.Add(user);
                    }

                    ctx.SaveChanges();

                    LoadUserMessages(userId);
                }
            }
        }
Example #3
0
        public static void LoadRooms(string url = "https://api.hipchat.com/v2/room?max-results=1000&startIndex=0")
        {
            var client = new HipchatClient(_personalAccessToken);
            Task <HttpResponseMessage> response = client.GetAsync(url);

            response.Wait();
            Task <string> y = response.Result.Content.ReadAsStringAsync();

            y.Wait();
            var resObj = JObject.Parse(y.Result);

            JObject links = resObj.Value <JObject>("links");
            string  next  = links?.Value <string>("next");

            if (next != null)
            {
                LoadRooms(next);
            }

            JArray items = resObj.Value <JArray>("items");

            using (var ctx = new HipchatEntities())
            {
                foreach (var i in items)
                {
                    long   roomId  = i.Value <long>("id");
                    string name    = i.Value <string>("name");
                    string privacy = i.Value <string>("privacy");

                    Console.WriteLine($"Loading room '{name}'...");

                    var room = ctx.Rooms.FirstOrDefault(r => r.room_id == roomId);
                    if (room == null)
                    {
                        room = new Room()
                        {
                            name = name, room_id = roomId, privacy = privacy
                        };
                        ctx.Rooms.Add(room);
                    }

                    response = client.GetAsync($"/v2/room/{room.room_id}/statistics");
                    response.Wait();

                    var content = response.Result.Content.ReadAsStringAsync();
                    content.Wait();
                    resObj             = JObject.Parse(content.Result);
                    room.messages_sent = resObj.Value <long>("messages_sent");
                    if (resObj["last_active"] != null && resObj.Value <string>("last_active") != null)
                    {
                        room.last_active = DateTimeOffset.Parse(resObj.Value <string>("last_active"));
                    }

                    ctx.SaveChanges();

                    if (room.messages_sent > 0)
                    {
                        bool anyMessages = ctx.Messages.Any(rm => rm.room_id == room.room_id);

                        if (!anyMessages)
                        {
                            LoadRoomMessages(_personalAccessToken, roomId);
                        }
                        else
                        {
                            DateTimeOffset latestMessage = ctx.Messages.Where(rm => rm.room_id == room.room_id).Max(m => m.date);
                            if (latestMessage < room.last_active)
                            {
                                LoadRoomMessages(_personalAccessToken, roomId);
                            }
                            else
                            {
                                Console.WriteLine($"No new messages since '{room.last_active}'");
                            }
                        }
                    }
                }
            }
        }