public static void WritePushedMessageToTestFile(GoogleNotifyMessage message) { try { var path = HttpRuntime.AppDomainAppPath; var fileName = "testPush.txt"; using (var fs = new FileStream(path.PathFormatter() + fileName, FileMode.Append, FileAccess.Write)) { byte[] info = null; info = new UTF8Encoding(true).GetBytes( $"data: {message.Message.Data}{Environment.NewLine}messageId: {message.Message.MessageId}{Environment.NewLine}subscription: {message.Subscription}{Environment.NewLine}"); //info = new UTF8Encoding(true).GetBytes("test"); fs.Write(info, 0, info.Length); } } catch (Exception ex) { var path = HttpRuntime.AppDomainAppPath; var fileName = "error.txt"; using (var fs = new FileStream(path.PathFormatter() + fileName, FileMode.Append, FileAccess.Write)) { byte[] info = null; info = new UTF8Encoding(true).GetBytes(ex.ToString()); fs.Write(info, 0, info.Length); } } }
public bool HandleGoogleNotifyMessage(GoogleNotifyMessage message) { bool result = true; if (message?.Message == null) { result = false; } else if (message.Subscription != BotInitializer.Instance.BotSettings.Subscription) { result = false; } if (result) { var decodedData = JsonConvert.DeserializeObject <EncodedMessageData>(Base64.DecodeUrlSafe(message.Message.Data)); if (decodedData == null) { result = false; } else { LogMaker.Log(Logger, $"Received push notification from account {decodedData.Email} with historyId={decodedData.HistoryId}.", false); UserSettingsModel userSettings = null; try { var userModel = _dbWorker.FindUserByEmail(decodedData.Email); if (userModel == null) { throw new DbDataStoreException( $"Can't find user data in database. User record with email {decodedData.Email} is absent in the database."); } userSettings = _dbWorker.FindUserSettings(userModel.UserId); if (userSettings == null) { throw new DbDataStoreException( $"Can't find user settings data in database. User record with id {userModel.UserId} is absent in the database."); } var service = Methods.SearchServiceByUserId(userModel.UserId.ToString()); var query = service.GmailService.Users.History.List("me"); query.HistoryTypes = UsersResource.HistoryResource.ListRequest.HistoryTypesEnum.MessageAdded; query.LabelId = Label.Unread; query.StartHistoryId = Convert.ToUInt64(userSettings.HistoryId); var listRequest = query.Execute(); var historyList = listRequest.History?.ToList(); if (historyList != null) { var addedMessageCollection = historyList.FindAll(h => h.MessagesAdded != null) .SelectMany(m => m.MessagesAdded) .ToList(); foreach (var addedMessage in addedMessageCollection) { var getRequest = service.GmailService.Users.Messages.Get("me", addedMessage.Message.Id); var messageResponse = getRequest.Execute(); var formattedMessage = new FormattedMessage(messageResponse); if (userSettings.IgnoreList.Any(ignoreModel => ignoreModel.Address == formattedMessage.From.Email)) { continue; } if (userSettings.UseWhitelist) { if (!userSettings.Whitelist.Any(label => formattedMessage.LabelIds.Contains(label.LabelId))) { continue; } } if (userSettings.Blacklist.Any(label => formattedMessage.LabelIds.Contains(label.LabelId))) { continue; } _botActions.ShowShortMessage(userModel.UserId.ToString(), formattedMessage); if (userSettings.ReadAfterReceiving) { Methods.ModifyMessageLabels(service, formattedMessage.MessageId, null, new List <string> { "UNREAD" }); } } } } catch (Exception ex) { LogMaker.Log(Logger, ex); } finally { if (userSettings != null) { userSettings.HistoryId = long.Parse(decodedData.HistoryId); _dbWorker.UpdateUserSettingsRecord(userSettings); } } } } if (!result) { LogMaker.Log(Logger, $"Unauthorized attempt to notify.", false); } return(result); }