Exemple #1
0
        public string AddPost(PostInfo info, ResourceInfo resource1Info, ResourceInfo resource2Info, List<ContactInfo> lstContacts, bool isPicture, bool isVideo, bool isAudio, bool isWeb)
        {
            try
            {
                if (!String.IsNullOrWhiteSpace(info.Title) && !String.IsNullOrWhiteSpace(info.Caption1) && !String.IsNullOrWhiteSpace(info.Caption2)
                     && !String.IsNullOrWhiteSpace(resource1Info.DataUrl) && info.Duration >= 5 && info.Duration <= 60
                     && (info.IsPublic == true || info.ToContacts == true || (info.ToSelectedContacts == true && lstContacts.Count > 0))
                     && (info.TerritoryId != null || info.IsGlobal == true) && (isPicture || isVideo || isAudio) && (info.CategoryId == 1 || info.CategoryId == 2 || info.CategoryId == 3))
                {
                    PostManager mgr = new PostManager();

                    TokenInfo tokenInfo = new HelperMethods().GetUserToken<TokenInfo>(isWeb, HttpContext.Current);

                    resource1Info.Data = JsonWebToken.Base64UrlDecode(resource1Info.DataUrl);

                    if (resource2Info != null && !String.IsNullOrWhiteSpace(resource2Info.DataUrl))
                        resource2Info.Data = JsonWebToken.Base64UrlDecode(resource2Info.DataUrl);

                    // Sets user token from cookie
                    info.UserIdf = tokenInfo.Idf;
                    info.EndDate = DateTime.UtcNow.AddMinutes(info.Duration);

                    if (info.IsPublic)
                    {
                        info.ToContacts = false;
                        info.ToSelectedContacts = false;
                    }
                    else if (info.ToContacts)
                    {
                        info.ToSelectedContacts = false;
                        info.IsPublic = false;
                    }
                    else if (info.ToSelectedContacts)
                    {
                        info.IsPublic = false;
                        info.ToContacts = false;
                    }

                    if (isPicture)
                    {
                        isVideo = false;
                        isAudio = false;
                    }
                    else if (isVideo)
                    {
                        isPicture = false;
                        isAudio = false;
                    }
                    else if (isAudio)
                    {
                        isVideo = false;
                        isPicture = false;
                    }

                    EnumPostType postType = isPicture ? EnumPostType.Picture : (isVideo ? EnumPostType.Video : (isAudio ? EnumPostType.Audio : EnumPostType.Picture));

                    var guids = mgr.AddPostImages(resource1Info, resource2Info, tokenInfo.FolderPath, postType);
                    long postId = mgr.AddPost(info, guids[0], guids[1], postType, tokenInfo.FolderPath);
                    if (info.ToSelectedContacts)
                    {
                        List<ContactInfo> lstFilteredContacts = new List<ContactInfo>();
                        ContactManager conMgr = new ContactManager();
                        lstContacts.ForEach(x =>
                        {
                            if (!String.IsNullOrWhiteSpace(x.ContactToken))
                            {
                                try
                                {
                                    TokenInfo contactToken = JsonWebToken.DecodeToken<TokenInfo>(x.ContactToken, CodeHelper.SecretAccessKey, true, false);
                                    x.ContactIdf = contactToken.Idf;
                                    lstFilteredContacts.Add(x);
                                }
                                catch (Exception)
                                {
                                    // if contact does not have all details, ignore it
                                }
                            }

                        });
                        conMgr.SavePostContacts(info.UserIdf, info.TerritoryId, info.IsGlobal, postId, lstFilteredContacts);
                    }

                    // schedule job
                    mgr.SchedulePost(postId, info.EndDate);

                    // send notification to all users either GCM or SignalR stating your contact has posted something
                    var obj = GlobalHost.ConnectionManager.GetHubContext<HeyVoteHub>();
                    new PostManager().GetNotificationUserList(postId.ToString()).ForEach(x =>
                    {
                        var signalR = HeyVoteHub.MyUsers.Where(y => y.Value.Equals(x.NotifyUserIdf)).FirstOrDefault();
                        if (!signalR.Equals(default(KeyValuePair<string, string>)))
                            obj.Clients.Client(signalR.Key).notifyUsers(new
                            {
                                Id = x.Id,
                                Title = x.Title,
                                UserIdf = x.UserIdf,
                                ImageIdf = x.ImageIdf,
                                Image1Idf = x.Image1Idf,
                                FolderPath = x.FolderPath,
                                CreatedOn = x.CreatedOn,
                                Caption1 = x.Caption1,
                                Caption2 = x.Caption2,
                                DisplayName = x.DisplayName,
                                PostId = x.PostId,
                                EndDate = x.EndDate,
                                isPost = x.isPost,
                                isFollow = x.isFollow,
                                isContact = x.isContact,
                                hasRead = false,
                                isViewed = false
                            });
                    });

                    return true.ToString();
                }
                return false.ToString();
            }
            catch (Exception ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError);
            }
        }
Exemple #2
0
        // Add new post
        public long AddPost(PostInfo info, Guid? Image1Idf, Guid? Image2Idf, EnumPostType postType, string hierarchyId)
        {
            try
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter("[posts].[AddPost]", AppConfigManager.ConnectionString))
                {
                    adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

                    adapter.SelectCommand.Parameters.AddWithValue("@UserIdf", info.UserIdf);
                    adapter.SelectCommand.Parameters.AddWithValue("@Title", info.Title);
                    adapter.SelectCommand.Parameters.AddWithValue("@EndDate", info.EndDate);
                    adapter.SelectCommand.Parameters.AddWithValue("@Caption1", info.Caption1);
                    adapter.SelectCommand.Parameters.AddWithValue("@Caption2", info.Caption2);
                    adapter.SelectCommand.Parameters.AddWithValue("@Img1Idf", Image1Idf);
                    adapter.SelectCommand.Parameters.AddWithValue("@Img2Idf", Image2Idf);
                    adapter.SelectCommand.Parameters.AddWithValue("@NumberOfSubscribers", info.NumberOfSubscribers);
                    adapter.SelectCommand.Parameters.AddWithValue("@isPublic", info.IsPublic);
                    adapter.SelectCommand.Parameters.AddWithValue("@toContacts", info.ToContacts);
                    adapter.SelectCommand.Parameters.AddWithValue("@toSelectedContacts", info.ToSelectedContacts);
                    adapter.SelectCommand.Parameters.AddWithValue("@TerritoryId", info.IsGlobal ? (int?)null : info.TerritoryId);
                    adapter.SelectCommand.Parameters.AddWithValue("@isGlobal", info.IsGlobal);
                    adapter.SelectCommand.Parameters.AddWithValue("@TypeId", (int)postType);
                    adapter.SelectCommand.Parameters.AddWithValue("@CategoryId", info.CategoryId);
                    var nodeParam = adapter.SelectCommand.Parameters.Add("@FolderPath", SqlDbType.Udt);
                    nodeParam.Value = SqlHierarchyId.Parse(hierarchyId);
                    nodeParam.UdtTypeName = "HierarchyId";

                    // Gets postId of Inserted post
                    adapter.SelectCommand.Connection.Open();
                    var postId = adapter.SelectCommand.ExecuteScalar();
                    adapter.SelectCommand.Connection.Close();
                    return Convert.ToInt64(postId);
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                throw new Exception(CodeHelper.UnableToAddPost);
            }
        }