Esempio n. 1
0
		public static void SendNow(ChatMessage c, Random r)
		{
			try
			{
				Query q = new Query();
				q.QueryCondition = new Q(RoomPin.Columns.Pinned, true);
				RoomPinSet rps = new RoomPinSet(q);

				Guid roomGuid;
				int rand = r.Next(10);
				if (rand == 1)
					roomGuid = new Chat.RoomSpec(RoomType.RandomChat, Model.Entities.ObjectType.None, 0, Model.Entities.ObjectType.None, 0).Guid;
				else if (rand == 2)
					roomGuid = new Chat.RoomSpec(RoomType.Normal, Model.Entities.ObjectType.None, 0, Model.Entities.ObjectType.None, 0).Guid;
				else
					roomGuid = rps[r.Next(rps.Count)].RoomGuid;


				string chatRoomName = roomGuid.ToString();
				try
				{
					Chat.RoomSpec c1 = Chat.RoomSpec.FromGuid(roomGuid);
					if (c1.GetName().Length > 0)
						chatRoomName = c1.GetName();
					else
						chatRoomName = c1.RoomType.ToString();

					if (c1.RoomType != RoomType.Normal &&
						c1.RoomType != RoomType.PrivateChat)
						return;

				}
				catch { }


				MessageStub m = new MessageStub(
					Guid.NewGuid().Pack(),
					ItemType.PublicChatMessage,
					DateTime.Now.Ticks.ToString(),
					roomGuid.Pack(),
					c.FromUsr.NickName,
					c.FromUsr.StmuParams,
					c.FromUsr.K,
					c.FromUsr.HasPic ? c.FromUsr.Pic.ToString() : "0",
					c.FromUsr.HasChatPic ? c.FromUsr.ChatPic.Value.ToString() : "0",
					c.Text + " (" + chatRoomName + ")",
					"");

				Chat.SendJsonChatItem(m, c.FromUsr.K);
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex.ToString());
				throw ex;
			}
		}
Esempio n. 2
0
		public RefreshStub Send(string message, string roomGuidString, string lastItemGuidString, int sessionID, string pageUrl, StateStub[] roomState)
		{
			WaitIfDevEnv();

			if (Usr.Current == null || Usr.Current.IsSkeleton || Usr.Current.Banned)
				throw new LoginPermissionException();

			Guid guid = roomGuidString.UnPackGuid();
			Chat.RoomSpec spec = Chat.RoomSpec.FromGuid(guid);
			Guid chatItemGuid = Guid.NewGuid();

			if (spec == null)
				throw new InvalidRoomException();

			if (!spec.CheckPermission(Usr.Current, true))
				throw new WritePermissionException();

			storeRoomState(roomState, Usr.Current.K);

			string txt = Chat.GetMessageFromChatBox(message);

			#region Create a ChatMessage database entry
			if (!spec.IsThreadRoom)
			{
				ChatMessage c = new ChatMessage();
				c.ChatItemGuid = chatItemGuid;
				c.DateTime = DateTime.Now;
				c.UsrK = Usr.Current.K;
				c.Text = txt;
				c.RoomGuid = spec.Guid;
				c.Update();
			}
			#endregion

			Usr.Current.ChatMessageCount++;
			Usr.Current.DateTimeLastChatMessage = DateTime.Now;
			string lastActionTicks = resetLastActionAndSessionID(sessionID); //will also fire Usr.Current.Update()

			MessageStub m;
			if (spec.IsPrivateChatRoom)
			{
				#region Private chat message
				int recipientUsrK = Usr.Current.K == spec.ObjectK ? spec.SecondObjectK : spec.ObjectK;
				bool isBuddy;
				try
				{
					Buddy b = new Buddy(recipientUsrK, Usr.Current.K);
					isBuddy = true;
				}
				catch
				{
					isBuddy = false;
				}
				
				m = new PrivateStub(
					chatItemGuid.Pack(),
					ItemType.PrivateChatMessage,
					lastActionTicks,
					roomGuidString.UnPackGuid().Pack(),
					Usr.Current.NickName,
					Usr.Current.StmuParams,
					Usr.Current.K,
					Usr.Current.HasPic ? Usr.Current.PicOrFacebookUID : "0",
					Usr.Current.HasChatPic ? Usr.Current.ChatPic.Value.ToString() : "0",
					txt,
					"",
					isBuddy);

				Chat.SendJsonChatItem(m, new int[]{Usr.Current.K, recipientUsrK});
				#endregion
			}
			else if (spec.IsThreadRoom || spec.IsHasPrimaryThreadObject)
			{
				#region Thread rooms
				Thread t = null;
				bool alreadyCreatedComment = false;
				bool createNewThread = false;


				if (spec.IsHasPrimaryThreadObject)
				{
					//if the object doesn't have a primary thread...
					IHasPrimaryThread primaryThreadObject = (IHasPrimaryThread)spec.ObjectBob;

					if (primaryThreadObject.ThreadK.IsNullOrZero())
					{
						createNewThread = true;
					}
					else
					{
						try
						{
							t = new Thread(primaryThreadObject.ThreadK.Value);
						}
						catch (BobNotFound)
						{
							createNewThread = true;
						}
					}

					if (createNewThread)
					{
						Thread.MakerReturn threadMakerReturn = Thread.CreatePublicThread((IDiscussable)spec.ObjectBob, Guid.NewGuid(), txt, false, null, true);

						alreadyCreatedComment = true;

						if (threadMakerReturn.Success || threadMakerReturn.Duplicate)
							t = threadMakerReturn.Thread;
						else
							throw new Exception("Couldn't create new thread!");
					}
				}
				else
				{
					t = new Thread(spec.ObjectK);
				}

				string subject = t.Subject.TruncateWithDots(30);
				int threadK = t.K;
				bool isPublic = !t.Private && !t.PrivateGroup && !t.GroupPrivate;
				string url = "";
				if (createNewThread)
				{
					url = t.UrlRefresher() + "#Comments";
				}
				else
				{
					if (t.LastPage == 1)
						url = t.UrlRefresher() + "#Comments";
					else
						url = t.UrlRefresher("c", t.LastPage.ToString()) + "#Comments";

					try
					{
						if (t.TotalComments % 20 == 0)
							url = t.UrlRefresher("c", (t.LastPage + 1).ToString()) + "#Comments";
						else if (t.LastComment != null)
							url = t.LastComment.UrlRefresherAnchorAfter();
					}
					catch { }
				}

				m = new CommentMessageStub(
					chatItemGuid.Pack(),
					ItemType.CommentChatMessage,
					lastActionTicks,
					roomGuidString.UnPackGuid().Pack(),
					Usr.Current.NickName,
					Usr.Current.StmuParams,
					Usr.Current.K,
					Usr.Current.HasPic ? Usr.Current.PicOrFacebookUID : "0",
					Usr.Current.HasChatPic ? Usr.Current.ChatPic.Value.ToString() : "0",
					txt,
					"",
					url,
					subject);

				List<int> usrKs = Thread.GetAllLoggedInParticipants(t).ToList().ConvertAll(u => u.K);
				try
				{
					usrKs.Add(Usr.Current.K);
				}
				catch { }
				
				Chat.SendJsonChatItem(m, usrKs.ToArray());

				if (isPublic && threadK > 0)
				{
					m.guid = Guid.NewGuid().Pack();
					m.roomGuid = new Chat.RoomSpec(RoomType.RandomChat).Guid.Pack();
					m.pinRoomGuid = t.GetRoomSpec().Guid.Pack();
					Chat.SendJsonChatItem(m);
				}

				if (!alreadyCreatedComment)
				{
					AddCommentJob job = new AddCommentJob(t.K, txt, Usr.Current.K, chatItemGuid);
					job.ExecuteAsynchronously();
				}
				#endregion
			}
			else
			{
				#region Public chat rooms
				m = new MessageStub(
					chatItemGuid.Pack(),
					ItemType.PublicChatMessage,
					lastActionTicks,
					roomGuidString.UnPackGuid().Pack(),
					Usr.Current.NickName,
					Usr.Current.StmuParams,
					Usr.Current.K,
					Usr.Current.HasPic ? Usr.Current.PicOrFacebookUID : "0",
					Usr.Current.HasChatPic ? Usr.Current.ChatPic.ToString() : "0",
					txt,
					"");

				Chat.SendJsonChatItem(m, Usr.Current.K);
				#endregion
			}

			SendStub s = new SendStub();
			s.itemGuid = chatItemGuid.Pack();

			RefreshStub r = refreshPrivate(false, lastItemGuidString, sessionID, lastActionTicks, pageUrl, Usr.Current.K, roomState);
			s.guestRefreshStubs = r.guestRefreshStubs;
			s.itemsJson = r.itemsJson;
			s.lastActionTicks = r.lastActionTicks;
			s.lastItemGuidReturned = r.lastItemGuidReturned;
			return s;
		}