fix a bug

This commit is contained in:
Michael 2017-05-25 15:32:38 -04:00
parent de6d1d15ab
commit d40c527da1

View file

@ -29,7 +29,7 @@ public static void NotifyFollowers(string uid, string title, string desc, string
var user = db.Users.FirstOrDefault(x => x.Id == uid);
if (user == null)
throw new Exception("Cannot find user with ID " + uid + ".");
foreach(var follower in user.Followers)
foreach (var follower in user.Followers)
{
NotifyUser(uid, follower.Follower, title, desc, url);
}
@ -41,7 +41,7 @@ public static void NotifyEveryone(string uid, string title, string desc, string
var user = db.Users.FirstOrDefault(x => x.Id == uid);
if (user == null)
throw new Exception("Cannot find user with ID " + uid + ".");
foreach (var usr in db.Users.Where(x=>x.Id!=uid).ToArray())
foreach (var usr in db.Users.Where(x => x.Id != uid).ToArray())
{
NotifyUser(uid, usr.Id, title, desc, url);
}
@ -117,40 +117,36 @@ internal static void ScreamToDiscord(string title, string desc, string url)
{
var db = new ApplicationDbContext();
var conf = db.Configs.FirstOrDefault();
if(conf != null)
if (conf != null)
{
if (!string.IsNullOrWhiteSpace(conf.WebhookUrl))
{
var wc = HttpWebRequest.Create(conf.WebhookUrl);
wc.Method = "POST";
wc.ContentType = "application/json";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(conf.WebhookUrl);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
content = $@"**{title}**
{desc}
Visit this URL to see more: {url}"
Visit this URL for more info: {url}"
});
wc.ContentLength = json.Length;
using (var s = wc.GetRequestStream())
{
using(var writer = new StreamWriter(s))
{
writer.Write(json);
using(var r = wc.GetResponse())
{
using(var rs = r.GetResponseStream())
{
using(var reader = new StreamReader(rs))
{
string result = reader.ReadToEnd();
db.AuditLogs.Add(new AuditLog("system", AuditLogLevel.Admin, "Discord webhook sent. Result: " + result));
}
}
}
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
}
}