Example #1
0
		public int Invite(
			List<int> inviteKs,
			Usr invitingUsr,
			DateTime inviteDateTime,
			List<int> alertedUsrs,
			bool isNewThread,
			Comment postedComment,
			bool joinChatRoom)
		{
			IDiscussable Parent = this.ParentForumObject;
			Query qInvites = new Query();

			List<Q> qList = new List<Q>();
			qList.Add(new Q(false));
			if (inviteKs != null && inviteKs.Count > 0)
				qList.Add(new InListQ(Usr.Columns.K, inviteKs));
			
			qInvites.QueryCondition = new Or(qList.ToArray());
			qInvites.Columns = new ColumnSet(
				Usr.EmailColumns,
				Usr.LinkColumns,
				Usr.Columns.IsLoggedOn,
				Usr.Columns.DateTimeLastPageRequest,
				Usr.Columns.AddedByGroupK);
			UsrSet usInvites = new UsrSet(qInvites);

			int count = 0;

			GroupUsr guInvitingUsr = null;
			if (this.GroupK > 0)
				guInvitingUsr = this.Group.GetGroupUsr(invitingUsr);

			List<int> addedThreadUsrsUsrKs = new List<int>();

			foreach (Usr u in usInvites)
			{
				if (!alertedUsrs.Contains(u.K))
				{
					try
					{
						ThreadUsr tuInvite = new ThreadUsr(this.K, u.K);
					}
					catch (Bobs.BobNotFound)
					{
						bool sendInviteAlerts = true;
						bool addThreadUsr = true;

						if (this.GroupK > 0)
						{
							GroupUsr gu = this.Group.GetGroupUsr(u);

							if (this.GroupPrivate || this.PrivateGroup)
							{
								//if this thread is private to the group or in a private group, there's a danger that we're 
								//inviting someone to a thread that they can't see. Also - if the group is made public, many 
								//threads may appear in peoples inboxes. This bit stops us sending these dodgy invites out...
								if (gu == null)
								{
									//if they don't have a GroupUsr record, lets send a group invite, but hold off on the thread alert
									sendInviteAlerts = false;
									this.Group.Invite(u, gu, invitingUsr, guInvitingUsr, "", false);
								}
								else if (gu.IsMember)
								{
									//if they are a member, we can send the thread invite
								}
								else if (gu.Status.Equals(GroupUsr.StatusEnum.Recommend) ||
									gu.Status.Equals(GroupUsr.StatusEnum.RecommendRejected) ||
									gu.Status.Equals(GroupUsr.StatusEnum.Request) ||
									gu.Status.Equals(GroupUsr.StatusEnum.RequestRejected))
								{
									//if we might be able to invite or join them to the group, we give it a try (the outcome will depend on some rather complex code)
									this.Group.Invite(u, gu, invitingUsr, guInvitingUsr, "", false);

									if (!gu.IsMember)
									{
										//if we didn't join them straight up, they still won't be able to see the thread, so we hold off on the thread alert email
										sendInviteAlerts = false;

										if (!gu.Status.Equals(GroupUsr.StatusEnum.Invite))
										{
											//if we didn't join them straight up AND we didn't invite them, lets not bother inviting them to the thread
											addThreadUsr = false;
										}
									}
								}
								else
								{
									//ok so they have a GroupUsr, but they're not a member, and we can't invite or join them, so we're not going to bother inviting them to the thread
									addThreadUsr = false;
								}
							}
						}

						//We still add this user to the alertedusers list (even if !addThreadUsr) - this just makes sure we don't attempt to send them an invite a second time.
						alertedUsrs.Add(u.K);

						if (addThreadUsr)
						{
							ThreadUsr tu = this.GetThreadUsr(u);
							tu.ChangeStatus(ThreadUsr.StatusEnum.NewInvite, DateTime.Now, false, false);
							try
							{
								addedThreadUsrsUsrKs.Add(u.K);
							}
							catch { }
							tu.InvitingUsrK = invitingUsr.K;
							tu.StatusChangeObjectK = invitingUsr.K;
							tu.StatusChangeObjectType = Model.Entities.ObjectType.Usr;
							tu.Update();

							count++;

							#region sendInviteAlerts
							if (sendInviteAlerts)
							{
								try
								{
									Mailer usrMail = new Mailer();
									if (isNewThread && this.Private && this.ParentObjectType.Equals(Model.Entities.ObjectType.Photo))
									{
										usrMail.Subject = invitingUsr.NickName + " sent you a photo";
										usrMail.Body += "<h1>" + invitingUsr.NickName + " sent you a photo</h1>";
										usrMail.Body += "<p align=\"center\"><a href=\"[LOGIN]\"><img border=\"0\" src=\"" + this.ParentPhoto.WebPath + "\" width=\"" + this.ParentPhoto.WebWidth + "\" height=\"" + this.ParentPhoto.WebHeight + "\" class=\"BorderBlack All\" /></a></p>";
									}
									else if (isNewThread)
									{
										usrMail.Subject = invitingUsr.NickName + " posts: \"" + this.SubjectSnip(40) + "\"";
										usrMail.Body += "<h1>" + invitingUsr.NickName + " has posted a new topic</h1>";
									}
									else
									{
										usrMail.Subject = invitingUsr.NickName + " invites you to: \"" + this.SubjectSnip(40) + "\"";
										usrMail.Body += "<h1>" + invitingUsr.NickName + " invites you to a topic</h1>";
									}

									usrMail.Body += "<p>The subject is: \"" + this.Subject + "\"</p>";
									usrMail.Body += "<p>To read " + invitingUsr.HisString(false) + " message, check out the <a href=\"[LOGIN]\">topic page</a>.</p>";
									usrMail.Body += "<p>If you want to stop " + invitingUsr.HimString(false) + " inviting you to topics, click the <i>Stop " + invitingUsr.NickName + " inviting me to chat topics</i> button on <a href=\"[LOGIN(" + invitingUsr.Url() + ")]\">" + invitingUsr.HisString(false) + " profile page</a>.</p>";
									usrMail.TemplateType = Mailer.TemplateTypes.AnotherSiteUser;
									usrMail.RedirectUrl = this.UrlDiscussion();
									usrMail.UsrRecipient = u;
									//usrMail.Bulk=usInvites.Count>5;
									usrMail.Bulk = false; // This is interesting... if people are invited by email, they should get the invite really...
									usrMail.Inbox = true;
									usrMail.Send();
								}
								catch (Exception ex) { Global.Log("1d3726bf-0715-4404-9059-4e53ca9e3dc5", ex); }

								//try
								//{
								//    if (u.IsLoggedOn && u.DateTimeLastPageRequest > DateTime.Now.AddMinutes(-5))
								//    {
								//        XmlDocument xmlDoc = new XmlDocument();
								//        XmlNode n = xmlDoc.CreateElement("privateMessageAlert");
								//        n.AddAttribute("nickName", invitingUsr.NickNameSafe);
								//        n.AddAttribute("stmu", invitingUsr.StmuParams);
								//        n.AddAttribute("usrK", invitingUsr.K.ToString());
								//        if (invitingUsr.HasPic)
								//            n.AddAttribute("pic", invitingUsr.Pic.ToString());
								//        else
								//            n.AddAttribute("pic", "0");
								//        n.AddAttribute("k", this.K.ToString());
								//        if (postedComment == null)
								//        {
								//            n.InnerText = this.Url();
								//        }
								//        else
								//        {
								//            n.InnerText = postedComment.Url(this);
								//        }
								//        Chat.SendChatItem(ItemType.Invite, n, DateTime.Now.Ticks, u.K, Guid.NewGuid());

								//    }
								//}
								//catch (Exception ex) { Global.Log("2902fd82-e8a4-49c2-9e33-ccfca01ca1f9", ex); }
							}
							#endregion
						}
					}
				}
			}

			if (joinChatRoom)
			{
				Guid room = this.GetRoomSpec().Guid;
				Chat.JoinRoom(room, addedThreadUsrsUsrKs.ToArray());
			}

			return count;
		}