示例#1
0
        public void PublishToFeed(Core core, User owner, IActionableItem item, List<NumberedItem> subItems, string description)
        {
            core.Console.AppendLine("0x0000");
            ItemKey itemKey = item.ItemKey;
            List<ItemKey> subItemKeys = new List<Internals.ItemKey>();

            foreach (NumberedItem i in subItems)
            {
                subItemKeys.Add(i.ItemKey);
            }

            core.Console.AppendLine("0x0001");
            SelectQuery query = new SelectQuery(typeof(Action));
            query.AddField(new QueryFunction("action_id", QueryFunctions.Count, "twentyfour"));
            query.AddCondition("action_primitive_id", owner.ItemKey.Id);
            query.AddCondition("action_primitive_type_id", owner.ItemKey.TypeId);
            query.AddCondition("action_application", applicationId);
            query.AddCondition("action_time_ut", ConditionEquality.GreaterThan, UnixTime.UnixTimeStamp() - 60 * 60 * 24);

            core.Console.AppendLine("0x0002");
            // maximum 48 per application per day
            if ((long)core.Db.Query(query).Rows[0]["twentyfour"] < 48)
            {
                core.Console.AppendLine("0x0003");
                /* Post to Twitter, Facebook, individual */
                string sharePrefix = (core.Http != null ? core.Http.Form["share"] : null);
                // Either the share prefix has been set XOR fallback to the default share settings
                if ((!string.IsNullOrEmpty(sharePrefix)) || ((owner.UserInfo.TwitterSyndicate && owner.UserInfo.TwitterAuthenticated) || (owner.UserInfo.FacebookSyndicate && owner.UserInfo.FacebookAuthenticated) || (owner.UserInfo.TumblrSyndicate && owner.UserInfo.TumblrAuthenticated)))
                {
                    core.Console.AppendLine("0x0004");
                    ItemInfo info = item.Info;
                    IActionableItem sharedItem = item;
                    ItemKey sharedItemKey = item.ItemKey;

                    core.Console.AppendLine("0x0005");
                    if (subItems.Count == 1)
                    {
                        sharedItemKey = subItems[0].ItemKey;
                        try
                        {
                            info = new ItemInfo(core, subItems[0]);
                        }
                        catch (InvalidIteminfoException)
                        {
                            info = ItemInfo.Create(core, subItems[0]);
                        }

                        if (subItems[0] is IActionableItem)
                        {
                            sharedItem = (IActionableItem)subItems[0];
                        }
                    }

                    core.Console.AppendLine("0x0006");
                    if (sharedItemKey.GetType(core).Shareable)
                    {
                        bool publicItem = true;

                        string debug = string.Empty;

                        IPermissibleItem pitem = item as IPermissibleItem;
                        if (pitem != null)
                        {
                            publicItem = pitem.Access.IsPublic();
                            //debug += ":p+" + item.ItemKey.ToString();
                        }

                        IPermissibleSubItem psitem = item as IPermissibleSubItem;
                        if (psitem != null)
                        {
                            publicItem = psitem.PermissiveParent.Access.IsPublic();
                            //debug += ":ps+" + publicItem.ToString();
                        }

                        core.Console.AppendLine("0x0007");
                        if (publicItem)
                        {
                            core.Console.AppendLine("0x0008");
                            if (owner.UserInfo.TwitterAuthenticated)
                            {
                                core.Console.AppendLine("0x0009");
                                int maxLength = 140;
                                if (!string.IsNullOrEmpty(sharedItem.DataContentType))
                                {
                                    maxLength -= 24; // twitter now considers the image link as part of the tweet
                                }
                                string twitterDescription = Functions.TrimStringToWord(description, maxLength - 7 - Hyperlink.Domain.Length - 3 - 11 - 1, true);

                                core.Console.AppendLine("0x0010");
                                string twitterShare = (core.Http != null ? core.Http.Form[sharePrefix + "-share-twitter"] : string.Empty);
                                core.Console.AppendLine("0x0011");
                                if ((sharePrefix == null && owner.UserInfo.TwitterSyndicate) || ((!string.IsNullOrEmpty(sharePrefix)) && (!string.IsNullOrEmpty(twitterShare))))
                                {
                                    core.Console.AppendLine("0x0012");
                                    core.Queue.PushJob(new Job(core.Settings.QueueDefaultPriority, 0, core.LoggedInMemberId, sharedItemKey.TypeId, sharedItemKey.Id, "publishTweet", twitterDescription + debug));
                                }
                            }

                            if (owner.UserInfo.TumblrAuthenticated)
                            {
                                Uri shareUri = new Uri(info.ShareUri);

                                string tumblrShare = (core.Http != null ? core.Http.Form[sharePrefix + "-share-tumblr"] : string.Empty);
                                if ((sharePrefix == null && owner.UserInfo.TumblrSyndicate) || ((!string.IsNullOrEmpty(sharePrefix)) && (!string.IsNullOrEmpty(tumblrShare))))
                                {
                                    core.Queue.PushJob(new Job(core.Settings.QueueDefaultPriority, 0, core.LoggedInMemberId, sharedItemKey.TypeId, sharedItemKey.Id, "publishTumblr", core.Bbcode.Parse(HttpUtility.HtmlEncode(sharedItem.PostType == ActionableItemType.Photo ? sharedItem.Caption : sharedItem.GetActionBody(subItemKeys)), owner, true, string.Empty, string.Empty) + "<p><a href=\"" + info.ShareUri + "\">" + shareUri.Authority + shareUri.PathAndQuery + "</a></p>"));
                                }
                            }

                            if (owner.UserInfo.FacebookAuthenticated)
                            {
                                string facebookShare = (core.Http != null ? core.Http.Form[sharePrefix + "-share-facebook"] : string.Empty);
                                if ((sharePrefix == null && owner.UserInfo.FacebookSyndicate) || ((!string.IsNullOrEmpty(sharePrefix)) && (!string.IsNullOrEmpty(facebookShare))))
                                {
                                    core.Queue.PushJob(new Job(core.Settings.QueueDefaultPriority, 0, core.LoggedInMemberId, sharedItemKey.TypeId, sharedItemKey.Id, "publishFacebook", description));
                                }
                            }
                        }
                    }
                }

                core.Console.AppendLine("0x0013");
                /* Post to Box Social feed, coalesce */
                SelectQuery query2 = Action.GetSelectQueryStub(core, typeof(Action));
                query2.AddCondition("action_primitive_id", owner.ItemKey.Id);
                query2.AddCondition("action_primitive_type_id", owner.ItemKey.TypeId);
                query2.AddCondition("action_item_id", itemKey.Id);
                query2.AddCondition("action_item_type_id", itemKey.TypeId);
                query2.AddCondition("action_application", applicationId);
                query2.AddCondition("action_time_ut", ConditionEquality.GreaterThan, UnixTime.UnixTimeStamp() - 60 * 60 * 24);
                query2.AddSort(SortOrder.Descending, "action_time_ut");

                DataTable actionDataTable = core.Db.Query(query2);

                if (actionDataTable.Rows.Count > 0)
                {
                    List<ItemKey> subItemsShortList = new List<ItemKey>();

                    Action action = new Action(core, owner, actionDataTable.Rows[0]);

                    SelectQuery query3 = ActionItem.GetSelectQueryStub(core, typeof(ActionItem));
                    query3.AddCondition("action_id", action.Id);
                    query3.LimitCount = 3;

                    DataTable actionItemDataTable = core.Db.Query(query3);

                    if (actionItemDataTable.Rows.Count < 3)
                    {
                        for (int i = 0; i < actionItemDataTable.Rows.Count; i++)
                        {
                            subItemsShortList.Add(new ItemKey((long)actionItemDataTable.Rows[i]["item_id"], (long)actionItemDataTable.Rows[i]["item_type_id"]));
                        }

                        for (int i = 0; i < subItems.Count; i++)
                        {
                            if (subItemsShortList.Count == 3) break;
                            subItemsShortList.Add(subItemKeys[i]);
                        }

                        string body = item.GetActionBody(subItemsShortList);
                        string bodyCache = string.Empty;

                        if (!body.Contains("[user") && !body.Contains("sid=true]"))
                        {
                            bodyCache = Bbcode.Parse(HttpUtility.HtmlEncode(body), null, owner, true, string.Empty, string.Empty);
                        }

                        UpdateQuery uquery = new UpdateQuery(typeof(Action));
                        uquery.AddField("action_time_ut", UnixTime.UnixTimeStamp());
                        uquery.AddField("action_title", item.Action);
                        uquery.AddField("action_body", body);
                        uquery.AddField("action_body_cache", bodyCache);

                        if (subItemsShortList != null && subItemsShortList.Count == 1)
                        {
                            uquery.AddField("interact_item_id", subItemsShortList[0].Id);
                            uquery.AddField("interact_item_type_id", subItemsShortList[0].TypeId);
                        }
                        else
                        {
                            uquery.AddField("interact_item_id", itemKey.Id);
                            uquery.AddField("interact_item_type_id", itemKey.TypeId);
                        }

                        uquery.AddCondition("action_id", action.Id);

                        core.Db.Query(uquery);

                        if (subItems != null)
                        {
                            foreach (ItemKey subItem in subItemKeys)
                            {
                                InsertQuery iquery = new InsertQuery(typeof(ActionItem));
                                iquery.AddField("action_id", action.Id);
                                iquery.AddField("item_id", subItem.Id);
                                iquery.AddField("item_type_id", subItem.TypeId);

                                core.Db.Query(iquery);
                            }
                        }
                    }
                }
                else
                {
                    string body = item.GetActionBody(subItemKeys);
                    string bodyCache = string.Empty;

                    if (!body.Contains("[user") && !body.Contains("sid=true]"))
                    {
                        bodyCache = Bbcode.Parse(HttpUtility.HtmlEncode(body), null, owner, true, string.Empty, string.Empty);
                    }

                    InsertQuery iquery = new InsertQuery(typeof(Action));
                    iquery.AddField("action_primitive_id", owner.ItemKey.Id);
                    iquery.AddField("action_primitive_type_id", owner.ItemKey.TypeId);
                    iquery.AddField("action_item_id", itemKey.Id);
                    iquery.AddField("action_item_type_id", itemKey.TypeId);
                    iquery.AddField("action_title", item.Action);
                    iquery.AddField("action_body", body);
                    iquery.AddField("action_body_cache", bodyCache);
                    iquery.AddField("action_application", applicationId);
                    iquery.AddField("action_time_ut", UnixTime.UnixTimeStamp());

                    if (subItems != null && subItems.Count == 1)
                    {
                        iquery.AddField("interact_item_id", subItemKeys[0].Id);
                        iquery.AddField("interact_item_type_id", subItemKeys[0].TypeId);
                    }
                    else
                    {
                        iquery.AddField("interact_item_id", itemKey.Id);
                        iquery.AddField("interact_item_type_id", itemKey.TypeId);
                    }

                    long actionId = core.Db.Query(iquery);

                    if (subItems != null)
                    {
                        foreach (ItemKey subItem in subItemKeys)
                        {
                            iquery = new InsertQuery(typeof(ActionItem));
                            iquery.AddField("action_id", actionId);
                            iquery.AddField("item_id", subItem.Id);
                            iquery.AddField("item_type_id", subItem.TypeId);

                            core.Db.Query(iquery);
                        }
                    }
                }
            }
        }