public async Task <bool> RegisterNotification(Models.Notification notification, bool isReminderAlreadyInserted = false) { try { Reminder reminder = new Reminder(); reminder.Body = notification.Body; reminder.Title = notification.Title; reminder.ETA = notification.UntilDate; if (!isReminderAlreadyInserted) { await ReminderLocalDbService.InsertReminderAsync(reminder); notification.ReminderID = reminder.ID; } else { reminder.ID = notification.ReminderID; } TimeSpan timeSpan = new TimeSpan(); timeSpan = notification.UntilDate.Subtract(DateTime.Now); // Todo: Extract to method that takes NotificationFrequency and builds a notification list based on that if (notification.FrequencyToSend == Enums.NotificationFrequency.DAILY) { for (int i = 0; i < timeSpan.Days; i++) { Plugin.Notifications.Notification notificationObject = new Plugin.Notifications.Notification { Title = notification.Title, Message = $"{notification.Body} ({timeSpan.Days - i} Day(s))", Vibrate = true, When = TimeSpan.FromDays(i) }; await CrossNotifications.Current.Send(notificationObject); if (notificationObject.Id != 0) { NotificationLink notificationLink = new NotificationLink(); notificationLink.ReminderID = reminder.ID; notificationLink.NotificationID = notificationObject.Id ?? 0; var a = await NotificationLinkLocalService.InsertNotificationLinkAsync(notificationLink); } else { return(false); } } } return(true); } catch (Exception) { return(false); } }
public static string NotificationLinkUrl(this NotificationLink link) { string url = link.Url; //enforce url rules if (link.IsExternal) { if (url.StartsWith("http://") || url.StartsWith("mailto:")) { //link start is good } else { url = "http://" + url; } } else { string appRoot = HttpContext.Current.Request.ApplicationPath; if (url.StartsWith("/") || url.StartsWith("~/")) { //starts with slash add root virtual path url = appRoot + url.TrimStart('~').TrimStart('/'); } else { url = appRoot + url; } } return(url); }
public static string FullNotificationLinkUrl(this NotificationLink link, string urlHostForLocal) { if (link.IsExternal) { return(link.NotificationLinkUrl()); } return("http://" + urlHostForLocal + link.NotificationLinkUrl()); }
public static void SetDefaultsForNew(this NotificationLink link, Notification notification) { link.Notification = notification; link.IsPending = false; link.StartDateTimeUtc = DateTime.UtcNow.AddMinutes(-1); link.EndDateTimeUtc = DateTime.UtcNow.AddYears(100); link.Client = notification.Client; link.ClientId = notification.ClientId; link.StoreFront = notification.StoreFront; link.StoreFrontId = notification.StoreFrontId; link.Order = 1; }
public static string NotificationLinkTag(this NotificationLink link, int counter) { string returnHtml = string.Empty; string url = link.NotificationLinkUrl(); returnHtml = "Link " + HttpUtility.HtmlEncode(counter) + ": " + "<a target=\"_blank\"" + " href=\"" + HttpUtility.HtmlAttributeEncode(url) + "\"" + " title=\"Go to " + HttpUtility.HtmlAttributeEncode(url) + "\"" + ">" + HttpUtility.HtmlEncode(link.LinkText) + " (Url: " + HttpUtility.HtmlEncode(url) + ")" + "</a>"; return(returnHtml); }
public async Task <bool> RemoveNotificationLinkAsync(NotificationLink notificationLink) { bool result = false; try { var tracking = _databaseContext.NotificationLinks.Remove(notificationLink); await _databaseContext.SaveChangesAsync(); result = tracking.State == EntityState.Deleted; return(result); } catch (Exception) { return(result); } }
public void Setup() { var noneLink = new NotificationLink(GameState.None, "..."); var humanTurnLink = new NotificationLink(GameState.HumanTurn, "Waiting for player action"); var computerTurnLink = new NotificationLink(GameState.ComputerTurn, "Computer turn"); var humanWonLink = new NotificationLink(GameState.HumanWon, "Hand won by player"); var computerWonLink = new NotificationLink(GameState.ComputerWon, "Hand won by computer"); var drawLink = new NotificationLink(GameState.Draw, "Draw"); noneLink.SetSuccessor(humanTurnLink); humanTurnLink.SetSuccessor(computerTurnLink); computerTurnLink.SetSuccessor(humanWonLink); humanWonLink.SetSuccessor(computerWonLink); computerWonLink.SetSuccessor(drawLink); drawLink.SetSuccessor(noneLink); _notificationChain = noneLink; }
public static string FullNotificationLinkTag(this NotificationLink link, int counter, string urlHostForLocal) { string returnHtml = string.Empty; string url = link.NotificationLinkUrl(); if (!link.IsExternal) { url = "http://" + urlHostForLocal + url; } returnHtml = "Link " + HttpUtility.HtmlEncode(counter) + ": " + "<a target=\"_blank\"" + " href=\"" + HttpUtility.HtmlAttributeEncode(url) + "\"" + " title=\"Go to " + HttpUtility.HtmlAttributeEncode(url) + "\"" + ">" + HttpUtility.HtmlEncode(link.LinkText) + " (Url: " + HttpUtility.HtmlEncode(url) + ")" + "</a>"; return(returnHtml); }
public void SetSuccessor(NotificationLink successor) { _successor = successor; }
public ActionResult Create(NotificationCreateViewModel data) { Order order = null; if (data.OrderId.HasValue) { //verify user has access to the order StoreFrontConfiguration storeFrontConfig = CurrentStoreFrontConfigOrThrow; StoreFront storeFront = storeFrontConfig.StoreFront; UserProfile profile = CurrentUserProfileOrNull; string trimmedEmail = (string.IsNullOrWhiteSpace(data.OrderEmail) ? "" : data.OrderEmail.Trim().ToLower()); if (profile == null) { order = storeFront.Orders.SingleOrDefault(o => o.OrderId == data.OrderId.Value && o.Email.ToLower() == trimmedEmail && o.UserProfileId == null); if (order == null) { return(HttpBadRequest("Anonymous order not found or not authorized for order id " + data.OrderId.Value + " with email: " + data.OrderEmail)); } } else { if (profile.AspNetIdentityUserIsInRoleSystemAdmin()) { order = storeFront.Orders.SingleOrDefault(o => o.OrderId == data.OrderId.Value); if (order == null) { return(HttpBadRequest("Sys Admin order not found order id " + data.OrderId.Value)); } } else if (storeFrontConfig.OrderAdmin_UserProfileId == profile.UserProfileId) { order = storeFront.Orders.SingleOrDefault(o => o.OrderId == data.OrderId.Value); if (order == null) { return(HttpBadRequest("Order Admin order not found order id " + data.OrderId.Value)); } } else { order = storeFront.Orders.SingleOrDefault(o => o.OrderId == data.OrderId.Value && o.UserProfileId == profile.UserProfileId); if (order == null) { return(HttpBadRequest("Logged in order not found or no access order id " + data.OrderId.Value)); } } } } UserProfile target = GStoreDb.UserProfiles.SingleOrDefault(prof => prof.UserProfileId == data.ToUserProfileId); if (target == null) { ModelState.AddModelError("", "Target recipient is not found. Please email the system administrator if you think this is an error."); } if (!User.IsInRole("SystemAdmin")) { if (!target.AllowUsersToSendSiteMessages) { ModelState.AddModelError("", "You are not authorized to send a message to the selected user. Please email the system administrator if you think this is an error."); } } if (!ModelState.IsValid) { ViewBag.Importance = ImportanceItems(); ViewBag.ToUserProfileId = AllowedToProfiles(); data.UpdateOrder(order); return(View(data)); } Notification notification = GStoreDb.Notifications.Create(); UserProfile sender = CurrentUserProfileOrThrow; notification.FromUserProfileId = sender.UserProfileId; notification.From = sender.FullName; notification.To = target.FullName; notification.Subject = data.Subject; notification.ToUserProfileId = data.ToUserProfileId; notification.Importance = data.Importance; notification.Message = data.Message; notification.UrlHost = Request.Url.Host; notification.Client = CurrentClientOrThrow; notification.StoreFront = CurrentStoreFrontOrThrow; notification.OrderId = data.OrderId; if (!Request.Url.IsDefaultPort) { notification.UrlHost += ":" + Request.Url.Port; } notification.BaseUrl = Url.Action("Details", "Notifications", new { id = "" }); List <NotificationLink> linkCollection = new List <NotificationLink>(); if (!string.IsNullOrWhiteSpace(data.Link1Url)) { if (string.IsNullOrWhiteSpace(data.Link1Text)) { data.Link1Text = data.Link1Url; } NotificationLink newLink1 = GStoreDb.NotificationLinks.Create(); newLink1.SetDefaultsForNew(notification); newLink1.Order = 1; newLink1.LinkText = data.Link1Text; newLink1.Url = data.Link1Url; if (data.Link1Url.StartsWith("/") || data.Link1Url.StartsWith("~/")) { newLink1.IsExternal = false; } else { newLink1.IsExternal = true; } linkCollection.Add(newLink1); } if (!string.IsNullOrWhiteSpace(data.Link2Url)) { if (string.IsNullOrWhiteSpace(data.Link2Text)) { data.Link2Text = data.Link2Url; } NotificationLink newLink2 = GStoreDb.NotificationLinks.Create(); newLink2.SetDefaultsForNew(notification); newLink2.Order = 2; newLink2.LinkText = data.Link2Text; newLink2.Url = data.Link2Url; if (data.Link2Url.StartsWith("/") || data.Link2Url.StartsWith("~/")) { newLink2.IsExternal = false; } else { newLink2.IsExternal = true; } linkCollection.Add(newLink2); } if (!string.IsNullOrWhiteSpace(data.Link3Url)) { if (string.IsNullOrWhiteSpace(data.Link3Text)) { data.Link3Text = data.Link3Url; } NotificationLink newLink3 = GStoreDb.NotificationLinks.Create(); newLink3.SetDefaultsForNew(notification); newLink3.Order = 3; newLink3.LinkText = data.Link3Text; newLink3.Url = data.Link3Url; if (data.Link3Url.StartsWith("/") || data.Link3Url.StartsWith("~/")) { newLink3.IsExternal = false; } else { newLink3.IsExternal = true; } linkCollection.Add(newLink3); } if (!string.IsNullOrWhiteSpace(data.Link4Url)) { if (string.IsNullOrWhiteSpace(data.Link4Text)) { data.Link4Text = data.Link4Url; } NotificationLink newLink4 = GStoreDb.NotificationLinks.Create(); newLink4.SetDefaultsForNew(notification); newLink4.Order = 4; newLink4.LinkText = data.Link4Text; newLink4.Url = data.Link4Url; if (data.Link4Url.StartsWith("/") || data.Link4Url.StartsWith("~/")) { newLink4.IsExternal = false; } else { newLink4.IsExternal = true; } linkCollection.Add(newLink4); } if (linkCollection.Count != 0) { notification.NotificationLinks = linkCollection; } notification.IsPending = false; notification.StartDateTimeUtc = DateTime.UtcNow; notification.EndDateTimeUtc = DateTime.UtcNow; GStoreDb.Notifications.Add(notification); GStoreDb.SaveChanges(); AddUserMessage("Message sent!", "Message sent to " + notification.To.ToHtml(), UserMessageType.Success); return(RedirectToAction("Index")); }