コード例 #1
0
        public static async Task <List <CSNotification> > RequestNotification(bool isReturn)
        {
            if (MainWindow.IsLoggedIn)
            {
                string requestURI = "https://story.kakao.com/a/notifications";

                HttpWebRequest webRequest = GenerateDefaultProfile(requestURI);
                webRequest.Timeout = NotificationRefreshTime;

                DateTime?lastTime = null;
                try
                {
                    string response = await GetResponseFromRequest(webRequest);

                    List <CSNotification> obj = JsonConvert.DeserializeObject <List <CSNotification> >(response);
                    if (isReturn)
                    {
                        return(obj);
                    }
                    int countTemp = 0;

                    if (obj.Count > 0)
                    {
                        lastTime = obj[0].created_at;
                    }

                    for (int count = 0; count < obj.Count; count++)
                    {
                        countTemp = count;
                        DateTime CurrentMessage = obj[count].created_at;

                        if (LastMessageTime != null && GlobalHelper.ToUnixTime(CurrentMessage) > GlobalHelper.ToUnixTime((DateTime)LastMessageTime))
                        {
                            string Message = obj[count].message;
                            string Content = obj[count].content ?? "내용 없음";

                            string Profile  = null;
                            string Identity = null;
                            string Uri      = "https://story.kakao.com/";
                            if (obj[count].scheme != null && (obj[count].scheme.Contains("?profile_id=")))
                            {
                                var ObjStr = obj[count].scheme?.Split(new string[] { "?profile_id=" }, StringSplitOptions.None);
                                Profile  = ObjStr[1];
                                Identity = ObjStr[0].Split('.')[1];
                                Uri     += Profile + "/" + Identity + "!" + ObjStr[0];
                            }
                            if (obj[count].scheme != null && (obj[count].scheme.Contains("kakaostory://profiles/")))
                            {
                                Uri += obj[count].scheme.Replace("kakaostory://profiles/", "");
                            }
                            bool willShow = true;

                            if (Properties.Settings.Default.Disable_Like && obj[count].emotion != null)
                            {
                                willShow = false;
                            }
                            else if (Properties.Settings.Default.Disable_VIP && obj[count].decorators != null && obj[count].decorators[0] != null && obj[count].decorators[0].text != null &&
                                     obj[count].decorators[0].text.StartsWith("관심친구"))
                            {
                                willShow = false;
                            }

                            if (willShow && obj[count].is_new)
                            {
                                if (Environment.OSVersion.Version.Major == 10)
                                {
                                    GlobalHelper.ShowNotification(Message, Content, Uri, obj[count].comment_id, obj[count].actor?.id, obj[count].actor?.display_name, Profile, Identity, obj[count].thumbnail_url ?? obj[count].actor?.profile_thumbnail_url);
                                }
                                else
                                {
                                    GlobalHelper.ShowNotification(Message, Content, Uri);
                                }
                            }
                            try
                            {
                                string activityID = Uri.Split(new string[] { "activities/" }, StringSplitOptions.None)[1];
                                if (MainWindow.Posts.ContainsKey(activityID))
                                {
                                    MainWindow.Posts[activityID].Refresh();
                                    MainWindow.Posts[activityID].SV_Comment.ScrollToEnd();
                                }
                            }
                            catch (Exception) {}
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (MainWindow.IsOffline)
                    {
                        if (!Properties.Settings.Default.Disable_Message)
                        {
                            GlobalHelper.ShowNotification("안내", "인터넷 연결이 복구됐습니다.", null);
                        }
                        MainWindow.Instance.Title = MainWindow.Instance.Title.Replace(OfflineStr, "");
                        MainWindow.IsOffline      = false;
                    }
                }
                catch (WebException)
                {
                    if (!MainWindow.IsOffline && Environment.OSVersion.Version.Major == 10)
                    {
                        MainWindow.Instance.Title = MainWindow.Instance.Title.Replace(OfflineStr, "") + OfflineStr;
                        MainWindow.IsOffline      = true;
                        if (!Properties.Settings.Default.Disable_Message)
                        {
                            GlobalHelper.ShowNotification("일반 오류", "인터넷 연결에 문제가 발생했습니다. 자동으로 복구를 시도합니다.", null);
                        }
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (lastTime != null)
                    {
                        LastMessageTime = lastTime;
                    }
                }
            }
            if (isReturn == false)
            {
                await Task.Delay(NotificationRefreshTime);
                await RequestNotification(isReturn);
            }
            return(null);
        }