public bool Send(ClientMessage message) { bool outOfSync = OutOfSync; SetVersion(); // See if this is a valid command (starts with /) if (TryHandleCommand(message.Content, message.Room)) { return(outOfSync); } var userId = Context.User.Identity.Name; ChatUser user = _repository.VerifyUserId(userId); ChatRoom room = _repository.VerifyUserRoom(_cache, user, message.Room); // REVIEW: Is it better to use _repository.VerifyRoom(message.Room, mustBeOpen: false) // here? if (room.Closed) { throw new InvalidOperationException(String.Format("You cannot post messages to '{0}'. The room is closed.", message.Room)); } // Update activity *after* ensuring the user, this forces them to be active UpdateActivity(user, room); ChatMessage chatMessage = _service.AddMessage(user, room, message.Id, message.Content); var messageViewModel = new MessageViewModel(chatMessage); Clients.Group(room.Name).addMessage(messageViewModel, room.Name); _repository.CommitChanges(); string clientMessageId = chatMessage.Id; // Update the id on the message chatMessage.Id = Guid.NewGuid().ToString("d"); _repository.CommitChanges(); var urls = UrlExtractor.ExtractUrls(chatMessage.Content); if (urls.Count > 0) { ProcessUrls(urls, room.Name, clientMessageId, message.Id); } return(outOfSync); }
public static void Main(string[] args) { Console.WriteLine("Launch at " + DateTime.Now.ToString()); Console.WriteLine("Version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()); // 実施判定 if (!hasToCheckUpdate()) { return; } // URL管理 UrlManager manager = new UrlManager(); // URL展開 UrlExtractor extractor = new UrlExtractor(); List <string> urls = extractor.ExtractUrls(); // ご新規さんを抽出 List <string> newUrls = manager.selectNonExists(urls); Console.WriteLine(" total: " + urls.Count + ", new: " + newUrls.Count); // ダウンロード PageDownloader downloader = new PageDownloader(); foreach (string url in newUrls) { System.Threading.Thread.Sleep(1000); Console.WriteLine(" new address: " + url); string fileName = downloader.Download(url); if (fileName != null) { manager.addUrl(url); } else { Console.WriteLine(" ...fail!!"); } } Console.WriteLine("Finish at " + DateTime.Now.ToString()); }
public void PostNotification(ClientNotification notification, bool executeContentProviders) { string userId = Context.User.GetUserId(); ChatUser user = _repository.GetUserById(userId); ChatRoom room = _repository.VerifyUserRoom(_cache, user, notification.Room); // User must be an owner if (room == null || !room.Owners.Contains(user) || (room.Private && !user.AllowedRooms.Contains(room))) { throw new InvalidOperationException("You're not allowed to post a notification"); } var chatMessage = new ChatMessage { Id = Guid.NewGuid().ToString("d"), Content = notification.Content, User = user, Room = room, HtmlEncoded = false, ImageUrl = notification.ImageUrl, Source = notification.Source, When = DateTimeOffset.UtcNow, MessageType = (int)MessageType.Notification }; _repository.Add(chatMessage); _repository.CommitChanges(); Clients.Group(room.Name).addMessage(new MessageViewModel(chatMessage), room.Name); if (executeContentProviders) { var urls = UrlExtractor.ExtractUrls(chatMessage.Content); if (urls.Count > 0) { _resourceProcessor.ProcessUrls(urls, Clients, room.Name, chatMessage.Id); } } }
public bool Send(ClientMessage clientMessage) { CheckStatus(); // See if this is a valid command (starts with /) if (TryHandleCommand(clientMessage.Content, clientMessage.Room)) { return(true); } var userId = Context.User.Identity.Name; ChatUser user = _repository.VerifyUserId(userId); ChatRoom room = _repository.VerifyUserRoom(_cache, user, clientMessage.Room); if (room == null || (room.Private && !user.AllowedRooms.Contains(room))) { return(false); } // REVIEW: Is it better to use _repository.VerifyRoom(message.Room, mustBeOpen: false) // here? if (room.Closed) { throw new InvalidOperationException(String.Format("You cannot post messages to '{0}'. The room is closed.", clientMessage.Room)); } // Update activity *after* ensuring the user, this forces them to be active UpdateActivity(user, room); // Create a true unique id and save the message to the db string id = Guid.NewGuid().ToString("d"); ChatMessage chatMessage = _service.AddMessage(user, room, id, clientMessage.Content); _repository.CommitChanges(); var messageViewModel = new MessageViewModel(chatMessage); if (clientMessage.Id == null) { // If the client didn't generate an id for the message then just // send it to everyone. The assumption is that the client has some ui // that it wanted to update immediately showing the message and // then when the actual message is roundtripped it would "solidify it". Clients.Group(room.Name).addMessage(messageViewModel, room.Name); } else { // If the client did set an id then we need to give everyone the real id first Clients.OthersInGroup(room.Name).addMessage(messageViewModel, room.Name); // Now tell the caller to replace the message Clients.Caller.replaceMessage(clientMessage.Id, messageViewModel, room.Name); } // Add mentions AddMentions(chatMessage); var urls = UrlExtractor.ExtractUrls(chatMessage.Content); if (urls.Count > 0) { _resourceProcessor.ProcessUrls(urls, Clients, room.Name, chatMessage.Id); } return(true); }
public bool Send(ClientMessage clientMessage) { CheckStatus(); // reject it if it's too long if (_settings.MaxMessageLength > 0 && clientMessage.Content.Length > _settings.MaxMessageLength) { throw new HubException(String.Format(LanguageResources.SendMessageTooLong, _settings.MaxMessageLength)); } // See if this is a valid command (starts with /) //if (TryHandleCommand(clientMessage.Content, clientMessage.Room)) //{ // return true; //} var userId = Context.User.GetUserId(); ChatUser user = _repository.VerifyUserId(userId); ChatRoom room = _chatRoomRepository.VerifyUserRoom(_repository, _cache, user, clientMessage.Room); if (room == null || (room.Private && !user.AllowedRooms.Contains(room))) { return(false); } // REVIEW: Is it better to use the extension method room.EnsureOpen here? if (room.Closed) { throw new HubException(String.Format(LanguageResources.SendMessageRoomClosed, clientMessage.Room)); } // Update activity *after* ensuring the user, this forces them to be active UpdateActivity(user, room); // Create a true unique id and save the message to the db string id = Guid.NewGuid().ToString("d"); ChatMessage chatMessage = _service.AddMessage(user, room, id, clientMessage.Content); var messageViewModel = new MessageViewModel(chatMessage); if (clientMessage.Id == null) { // If the client didn't generate an id for the message then just // send it to everyone. The assumption is that the client has some ui // that it wanted to update immediately showing the message and // then when the actual message is roundtripped it would "solidify it". Clients.Group(room.Name).addMessage(messageViewModel, room.Name); } else { // If the client did set an id then we need to give everyone the real id first Clients.OthersInGroup(room.Name).addMessage(messageViewModel, room.Name); // Now tell the caller to replace the message Clients.Caller.replaceMessage(clientMessage.Id, messageViewModel, room.Name); } // Add mentions AddMentions(chatMessage); var urls = UrlExtractor.ExtractUrls(chatMessage.Content); if (urls.Count > 0) { // _resourceProcessor.ProcessUrls(urls, Clients, room.Name, chatMessage.Id); } return(true); }