partial void DeletePostHistory(PostHistory instance);
 partial void UpdatePostHistory(PostHistory instance);
 partial void InsertPostHistory(PostHistory instance);
		private void detach_PostHistories(PostHistory entity)
		{
			this.SendPropertyChanging();
			entity.User = null;
		}
        private void postStatusToPing()
        {
            GamervineDataContext dataContext = new GamervineDataContext();

            try
            {
                var users = from u in dataContext.Users
                            join up in dataContext.UserPrefs on u.UserId equals up.UserId
                            join gt in dataContext.Gamertags on u.UserId equals gt.UserId
                            where gt.State != 2
                            select new
                            {
                                u.UserId,
                                u.PingFmKey,
                                up.PublishGameFrequency,
                                up.PublishInterval,
                                up.PublishStatus,
                                up.PublishUniqueStatus,
                                up.StatusFormat,
                                gt.TagId,
                                gt.State,
                                gt.Type
                            };

                foreach (var user in users)
                {
                    var latestData = from xd in dataContext.XboxData
                                     where xd.TagId == user.TagId
                                     orderby xd.RetrieveDate descending
                                     select xd;

                    string status = bcXboxData.GetStatusString(user.StatusFormat, latestData.First());

                    var lastPost = from ph in dataContext.PostHistories
                                   where ph.UserId == user.UserId
                                   orderby ph.PostDate descending
                                   select ph;

                    string strLastPost = string.Empty;
                    if(lastPost.Count() > 0)
                        strLastPost = lastPost.First().PostString;

                    if (strLastPost.Equals(string.Empty) || !status.ToLower().Equals(strLastPost.ToLower()))
                    {
                        if ((strLastPost.ToLower().Contains(" is currently offline. last seen ") &&
                            !status.ToLower().Contains(" is currently offline. last seen ")) ||
                            (!strLastPost.ToLower().Contains(" is currently offline. last seen ") &&
                            status.ToLower().Contains(" is currently offline. last seen ")) ||
                            (!strLastPost.ToLower().Contains(" is currently offline. last seen ") &&
                            !status.ToLower().Contains(" is currently offline. last seen ")))
                        {
                            //Hit the Ping.fm service
                            PingFMApi.api_key = "29e2b905210c5a0cf77f74e2462f8ea4";
                            PingFMApi ping = new PingFMApi(user.PingFmKey);
                            PingFMApi.PingResponse pr = ping.Post(new PingFMApi.OutgoingMessage(status));

                            //Record the post history in the database
                            PostHistory ph = new PostHistory();
                            ph.PostDate = DateTime.UtcNow;
                            ph.PostHistoryId = _nextPostHistoryID++;
                            ph.PostString = status;
                            ph.UserId = user.UserId;

                            dataContext.PostHistories.InsertOnSubmit(ph);
                        }
                    }
                }
            }
            finally
            {
                dataContext.SubmitChanges();
            }
        }
Example #6
0
        public void DoWork()
        {
            GamervineDataContext dataContext = new GamervineDataContext();

            try
            {
                //Get all jobs where the user & gamertags are active and the next run time is in the past or now
                var jobs = from j in dataContext.Jobs
                           join gt in dataContext.Gamertags on j.TagId equals gt.TagId
                           join u in dataContext.Users on gt.UserId equals u.UserId
                           where gt.State == State.Active.GetHashCode()
                                && u.State == State.Active.GetHashCode()
                                && j.NextRunTime <= DateTime.UtcNow
                           select j;

                foreach (Job job in jobs)
                {
                    try
                    {
                        IJobHandler jobHandler = JobHandlerFactory.GetHandlerForType(job.Type);
                        string post = jobHandler.GetPost(dataContext, job);

                        if (!post.Equals(string.Empty))
                        {
                            var socialConnectors = from sc in dataContext.UserSocialConnections
                                                   join gtsc in dataContext.GamertagSocialConnections on sc.UserSocialConnectionId equals gtsc.UserSocialConnectionId
                                                   where gtsc.TagId == job.TagId
                                                   select sc;

                            if (socialConnectors.Count() > 0)
                            {
                                foreach (UserSocialConnection conn in socialConnectors)
                                {
                                    ISocialConnector socialConnector = SocialConnectorFactory.GetSocialConnectorForType(conn.Type);
                                    socialConnector.Post(conn, post);
                                }
                            }
                            else
                            {
                                //TODO: Perform logic to flag the account that it doesn't have social connectors and they should sign up
                            }

                            //Do any post work that the handler may need to do
                            jobHandler.PostWork(dataContext, job);

                            //Record the post history in the database
                            PostHistory ph = new PostHistory();
                            ph.PostDate = DateTime.UtcNow;
                            ph.PostHistoryId = Guid.NewGuid().ToString();
                            ph.PostString = post;
                            ph.PostType = job.Type;
                            ph.TagId = job.TagId;

                            dataContext.PostHistories.InsertOnSubmit(ph);
                        }

                        job.NextRunTime = job.CalculateNextRunTime();
                        job.LastRunTime = DateTime.UtcNow;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Exception occurred in PostService.DoWork processing Job ID #" + job.JobId + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
                    }
                    finally
                    {
                        try
                        {
                            dataContext.SubmitChanges();
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Exception occurred in PostService.DoWork -> dataContext.SubmitChanges for Job ID #" + job.JobId + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception occurred in PostService.DoWork:" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
            }
            finally
            {
                Debug.WriteLine("PostService DoWork completed at " + DateTime.Now.ToString());
            }
        }