Skip to content
Snippets Groups Projects

Sprint/2020 12

Merged Marcel Nellesen requested to merge Sprint/2020-12 into master
18 files
+ 2305
0
Compare changes
  • Side-by-side
  • Inline
Files
18
 
using Coscine.ApiCommons;
 
using Coscine.ApiCommons.Factories;
 
using Coscine.Configuration;
 
using Coscine.Database.DataModel;
 
using Coscine.Database.Models;
 
using Coscine.NotificationChannelBase;
 
using Coscine.NotificationConfiguration;
 
using Microsoft.AspNetCore.Authorization;
 
using Microsoft.AspNetCore.Mvc;
 
using Newtonsoft.Json.Linq;
 
using System;
 
using System.Collections.Generic;
 
using System.Reflection;
 
using System.Threading.Tasks;
 
 
namespace Coscine.Api.NotificationBus.Controllers
 
{
 
public class NotificationBusController : Controller
 
{
 
 
private readonly UserModel _userModel;
 
 
private readonly ProjectModel _projectModel;
 
 
private readonly LanguageModel _languageModel;
 
 
private readonly IConfiguration _configuration;
 
 
private readonly Coscine.NotificationConfiguration.NotificationConfiguration _notificationConfiguration;
 
 
 
public NotificationBusController()
 
{
 
_userModel = new UserModel();
 
_projectModel = new ProjectModel();
 
_languageModel = new LanguageModel();
 
_configuration = Program.Configuration;
 
_notificationConfiguration = new Coscine.NotificationConfiguration.NotificationConfiguration();
 
}
 
 
[HttpPost("[controller]/send")]
 
public IActionResult Send([FromBody] NotificationParameterObject notificationParameterObject)
 
{
 
SendNotifications(notificationParameterObject).Wait();
 
return Ok();
 
}
 
 
[HttpPost("[controller]/sendAsync")]
 
public IActionResult SendAsync([FromBody] NotificationParameterObject notificationParameterObject)
 
{
 
SendNotifications(notificationParameterObject);
 
return Ok();
 
}
 
 
private async Task SendNotifications(NotificationParameterObject notificationParameterObject)
 
{
 
List<User> users = GetUsers(notificationParameterObject.UserIds);
 
Project project = GetProject(notificationParameterObject.ProjectId);
 
var action = notificationParameterObject.Action;
 
ActionObject actionObject = _notificationConfiguration.GetAction(action);
 
List<string> channelList = _notificationConfiguration.GetAllChannelsForAction(action);
 
 
string basePath = _configuration.GetStringAndWait("coscine/local/notifications/channels/folder");
 
 
// for every available channel
 
foreach (string channelName in channelList)
 
{
 
ChannelObject channelInfo = _notificationConfiguration.GetChannel(channelName);
 
string pathToDll = basePath + channelInfo.Path;
 
 
var mergeSettings = new JsonMergeSettings
 
{
 
MergeArrayHandling = MergeArrayHandling.Union
 
};
 
 
if(notificationParameterObject.Args != null)
 
{
 
notificationParameterObject.Args.Merge(channelInfo.Args, mergeSettings);
 
}
 
else
 
{
 
notificationParameterObject.Args = channelInfo.Args;
 
}
 
 
// for every available user
 
foreach (User user in users)
 
{
 
JObject messageData = FillTemplate(channelName, actionObject, notificationParameterObject.Args, user, project);
 
 
INotificationChannel notificationChannel = null;
 
 
var DLL = Assembly.LoadFile(pathToDll);
 
foreach (Type type in DLL.GetExportedTypes())
 
{
 
if (typeof(INotificationChannel).IsAssignableFrom(type))
 
{
 
notificationChannel = (INotificationChannel)Activator.CreateInstance(type);
 
}
 
}
 
 
await notificationChannel.SendAsync(Program.Configuration, new Message(messageData, notificationParameterObject.Args, user, project));
 
}
 
}
 
return;
 
}
 
 
private JObject FillTemplate(string channelName, ActionObject action, JObject requestArgs, User user, Project project)
 
{
 
string language = "en";
 
if (user.LanguageId != null)
 
{
 
language = _languageModel.GetById(new Guid(user.LanguageId.ToString())).Abbreviation;
 
}
 
return (JObject)(action.Template[channelName][language]);
 
}
 
 
private List<User> GetUsers(List<string> userIds)
 
{
 
var users = new List<User>();
 
foreach (var userId in userIds)
 
{
 
users.Add(_userModel.GetById(new Guid(userId.ToString())));
 
}
 
return users;
 
}
 
 
private Project GetProject(string projectId)
 
{
 
Project project = null;
 
if (projectId != null)
 
{
 
project = _projectModel.GetById(new Guid(projectId));
 
}
 
return project;
 
}
 
}
 
}
Loading