Esempio n. 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="core"></param>
        public static void UpdateView(Core core, bool response)
        {
            if (core == null)
            {
                throw new NullCoreException();
            }

            long viewId = core.Functions.FormLong("vid", core.Functions.RequestLong("vid", 0));
            string mode = core.Http.Form["view-mode"]; // tick, background, foreground, unload
            if (string.IsNullOrEmpty(mode))
            {
                mode = core.Http["view-mode"];
            }
            long timestamp = UnixTime.UnixTimeStamp();

            if (viewId > 0)
            {
                ItemView view = new ItemView(core, viewId);

                if (view.viewSessionId == core.Session.SessionId)
                {
                    switch (mode.ToLower())
                    {
                        case "tick":
                            if (view.viewState != (int)ItemViewState.Exited && view.viewState != (int)ItemViewState.Inactive)
                            {
                                if (timestamp - view.viewUpdateTimeRaw < 120) // ticks happen every 60 seconds with a 60 second page timeout
                                {
                                    UpdateQuery uQuery = new UpdateQuery(typeof(ItemView));
                                    uQuery.AddField("view_timespan", new QueryOperation("view_timespan", QueryOperations.Addition, timestamp - view.viewUpdateTimeRaw));
                                    uQuery.AddField("view_update_time_ut", timestamp);
                                    uQuery.AddField("view_cookies", core.Session.SessionMethod == SessionMethods.Cookie);
                                    uQuery.AddField("view_javascript", true);
                                    uQuery.AddField("view_state", (int)ItemViewState.Foreground);
                                    uQuery.AddCondition("view_id", viewId);

                                    core.Db.Query(uQuery);
                                }
                                else
                                {
                                    UpdateQuery uQuery = new UpdateQuery(typeof(ItemView));
                                    uQuery.AddField("view_update_time_ut", timestamp);
                                    uQuery.AddField("view_state", (int)ItemViewState.Foreground);
                                    uQuery.AddCondition("view_id", viewId);

                                    core.Db.Query(uQuery);
                                }
                            }
                            break;
                        case "background":
                        case "unload":
                        case "inactive":
                            {
                                UpdateQuery uQuery = new UpdateQuery(typeof(ItemView));
                                uQuery.AddField("view_javascript", true);
                                if (timestamp - view.viewUpdateTimeRaw < 120)
                                {
                                    uQuery.AddField("view_timespan", new QueryOperation("view_timespan", QueryOperations.Addition, timestamp - view.viewUpdateTimeRaw));
                                }
                                uQuery.AddField("view_update_time_ut", timestamp);
                                if (mode.ToLower() == "unload")
                                {
                                    uQuery.AddField("view_state", (int)ItemViewState.Exited);
                                }
                                else if (mode.ToLower() == "inactive")
                                {
                                    uQuery.AddField("view_state", (int)ItemViewState.Inactive);
                                }
                                else
                                {
                                    uQuery.AddField("view_state", (int)ItemViewState.Background);
                                }
                                uQuery.AddCondition("view_id", viewId);

                                core.Db.Query(uQuery);
                            }
                            break;
                        case "foreground":
                            {
                                UpdateQuery uQuery = new UpdateQuery(typeof(ItemView));
                                uQuery.AddField("view_update_time_ut", timestamp);
                                uQuery.AddField("view_state", (int)ItemViewState.Foreground);
                                uQuery.AddCondition("view_id", viewId);

                                core.Db.Query(uQuery);
                            }
                            break;
                        case "active":
                            {
                                UpdateQuery uQuery = new UpdateQuery(typeof(ItemView));
                                uQuery.AddField("view_update_time_ut", timestamp);
                                uQuery.AddField("view_state", (int)ItemViewState.Active);
                                uQuery.AddCondition("view_id", viewId);

                                core.Db.Query(uQuery);
                            }
                            break;
                    }
                }
                else
                {
                    // probably a view bot
                    UpdateQuery uQuery = new UpdateQuery(typeof(ItemView));
                    uQuery.AddField("view_discounted", true);
                    uQuery.AddField("view_processed", true);
                    uQuery.SetBitField("view_discounted_reason", (int)ItemViewDiscountedReason.InvalidSession);
                    uQuery.AddCondition("view_id", viewId);

                    core.Db.Query(uQuery);
                }
            }

            if (response)
            {
                core.Response.SendStatus("viewLogged");
            }
        }