private static void AddMentionedEntities(List <Entity> entities, EventNotificationCardPayload notificationPayload) { bool ifEntityExits = entities.Any(x => x.GetAs <Mention>().Mentioned.Id == notificationPayload.UserTeamsId); if (!ifEntityExits) { var entity = new Entity(); entity.SetAs(new Mention() { Text = $"<at>{notificationPayload.UserName}</at>", Mentioned = new ChannelAccount() { Name = notificationPayload.UserName, Id = notificationPayload.UserTeamsId, Role = "link", }, Type = "mention", }); entities.Add(entity); } }
/// <summary> /// Process and send all the due events in a team. /// </summary> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> private async Task ProcessEvents() { foreach (var teamId in this.teamsEventsDictionary.Keys) { List <string> eventIds = new List <string>(); this.teamsEventsDictionary.TryGetValue(teamId, out eventIds); List <Attachment> cardAttachments = new List <Attachment>(); List <EventMessage> eventMessages = new List <EventMessage>(); const int maxEventPerCarousel = 6; const int minEventToSendIndividually = 3; int counter = 0; List <EventNotificationCardPayload> eventNotificationCardPayloadList = new List <EventNotificationCardPayload>(); List <Entity> entities = new List <Entity>(); foreach (var eventId in eventIds) { counter++; var recurringEvent = this.recurringEvents.Where(x => x.EventId == eventId).FirstOrDefault(); var celebrationEvent = this.celebrationEvents.Where(x => x.Id == eventId).FirstOrDefault(); // Get event owner information. var user = this.users.Where(x => x.AadObjectId == celebrationEvent.OwnerAadObjectId).FirstOrDefault(); // Add an Entry to Messages collection. EventMessage eventMessage = await this.AddEntryToEventMessagesCollection(teamId, recurringEvent, celebrationEvent, user); eventMessages.Add(eventMessage); // Get Hero card for event. HeroCard card = CelebrationCard.GetEventCard(eventMessage.Activity); EventNotificationCardPayload eventNotificationCardPayload = new EventNotificationCardPayload() { UserName = user.UserName, UserTeamsId = user.TeamsId, Message = $"<at>{user.UserName}</at> is celebrating {celebrationEvent.Title}", Attachment = card.ToAttachment(), }; eventNotificationCardPayloadList.Add(eventNotificationCardPayload); if (eventIds.Count > minEventToSendIndividually && (counter % maxEventPerCarousel == 0 || counter == eventIds.Count)) { eventNotificationCardPayloadList = eventNotificationCardPayloadList.OrderBy(x => x.UserName).ToList(); string message = "Stop the presses! Today "; foreach (var notificationPayload in eventNotificationCardPayloadList) { message = message + notificationPayload.Message + ","; cardAttachments.Add(notificationPayload.Attachment); AddMentionedEntities(entities, notificationPayload); } message = message.TrimEnd(','); int position = message.LastIndexOf(','); message = (message.Substring(0, position) + " and " + message.Substring(position + 1)).Replace(",", ", ") + ". That’s a lot of merrymaking for one day—pace yourselves! \n\n"; // Do not send separate message in case of 1 event. if (eventNotificationCardPayloadList.Count == 1) { message = string.Empty; } // send event notification in team. this.logProvider.LogInfo("Sending event message in team", new Dictionary <string, string>() { { "EventId", celebrationEvent.Id }, { "TeamId", teamId }, { "Attachment", cardAttachments.ToString() }, { "Message", message }, }); await this.SendEventCard(message, cardAttachments, teamId, eventMessages, entities); // Reset list cardAttachments = new List <Attachment>(); eventMessages = new List <EventMessage>(); eventNotificationCardPayloadList = new List <EventNotificationCardPayload>(); entities = new List <Entity>(); } else if (eventIds.Count <= minEventToSendIndividually) { this.logProvider.LogInfo("Sending event message in team", new Dictionary <string, string>() { { "EventId", celebrationEvent.Id }, { "TeamId", teamId }, { "Attachment", cardAttachments.ToString() }, { "Message", string.Empty }, }); await this.SendEventCard(string.Empty, new List <Attachment> { eventNotificationCardPayload.Attachment }, teamId, eventMessages); // Reset list cardAttachments = new List <Attachment>(); eventMessages = new List <EventMessage>(); eventNotificationCardPayloadList = new List <EventNotificationCardPayload>(); entities = new List <Entity>(); } } } // Delete entry from occurrences collection. foreach (var recurringEvent in this.recurringEvents) { this.logProvider.LogInfo("Deleting recurring event", new Dictionary <string, string>() { { "EventId", recurringEvent.EventId }, { "RecurringEventId", recurringEvent.Id }, }); await this.eventHelper.DeleteRecurringEventAsync(recurringEvent.Id, recurringEvent.EventId); } }