Skip to content

Commit

Permalink
Made stuff somewhat nicer ;)
Browse files Browse the repository at this point in the history
  • Loading branch information
lieuwex committed Feb 8, 2014
1 parent 57170b5 commit df1f538
Show file tree
Hide file tree
Showing 24 changed files with 118 additions and 38 deletions.
14 changes: 14 additions & 0 deletions bin/MataSharp.XML

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified bin/MataSharp.dll
Binary file not shown.
3 changes: 2 additions & 1 deletion src/MataSharp/Attachment.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Web;
using Newtonsoft.Json;

Expand Down Expand Up @@ -66,6 +65,8 @@ public int CompareTo(Attachment other)
var nameCompared = this.Name.CompareTo(other.Name);
return (nameCompared != 0) ? nameCompared : this.Date.CompareTo(other.Date);
}

public override string ToString() { return this.Name; }
}

internal enum AttachmentType
Expand Down
12 changes: 11 additions & 1 deletion src/MataSharp/Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static DateTime ToDateTime(this String original)
/// </summary>
/// <param name="AttachmentType">AttachmentType to give every attachment in the array.</param>
/// <returns>The array as list</returns>
public static List<Attachment> ToList(this Attachment[] currentArray, AttachmentType AttachmentType)
internal static List<Attachment> ToList(this Attachment[] currentArray, AttachmentType AttachmentType)
{
var tmpList = new List<Attachment>(currentArray);
tmpList.ForEach(a => a.Type = AttachmentType);
Expand Down Expand Up @@ -54,5 +54,15 @@ public static string DayOfWeekDutch(this DateTime Date)
default: return "";
}
}

/// <summary>
/// Converts the current DateTime instance to a string.
/// </summary>
/// <param name="dutch">If the day should be in Dutch or in English</param>
/// <returns>The current DateTime instance as a string.</returns>
public static string ToString(this DateTime Date, bool dutch)
{
return (dutch) ? (Date.DayOfWeekDutch() + " " + Date.ToString()) : (Date.DayOfWeek + " " + Date.ToString());
}
}
}
4 changes: 2 additions & 2 deletions src/MataSharp/HTTPClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public void Post(string URL, string Content)
this.Client.UploadData(URL, System.Text.Encoding.ASCII.GetBytes(Content));
}

public void Delete(string URL, string SessionID)
public void Delete(string URL, string Cookie)
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(URL);
req.Method = "DELETE";
req.Headers[HttpRequestHeader.Cookie] = "SESSION_ID=" + SessionID + "&fileDownload=true";
req.Headers[HttpRequestHeader.Cookie] = Cookie;
req.Timeout = 15000;
req.GetResponse();
}
Expand Down
19 changes: 11 additions & 8 deletions src/MataSharp/MagisterMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public bool CanSend
get { return (this._CanSend == true && this.Sender != null && this.Recipients != null && !string.IsNullOrEmpty(this.Body) && !string.IsNullOrEmpty(this.Subject)); }
}

public int Index { get; internal set; }

private Mata Mata { get; set; }
#endregion

Expand Down Expand Up @@ -143,7 +145,7 @@ public MagisterMessage CreateForwardMessage()
IDKey = this.IDKey,
IDOrginalReceiver = null,
IDOriginal = null,
Body = "<b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
Body = "<b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.ToString(true) + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
Deleted = false,
_IsRead = true,
Subject = tmpSubject,
Expand Down Expand Up @@ -175,7 +177,7 @@ public MagisterMessage CreateForwardMessage(string ContentAdd)
IDKey = this.IDKey,
IDOrginalReceiver = null,
IDOriginal = null,
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.ToString(true) + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
Deleted = false,
_IsRead = true,
Subject = tmpSubject,
Expand All @@ -197,7 +199,7 @@ public MagisterMessage CreateReplyToAllMessage(string ContentAdd)
var tmpSubject = (this.Subject[0] != 'R' || this.Subject[1] != 'E' || this.Subject[2] != ':' || this.Subject[3] != ' ') ? "RE: " + this.Subject : this.Subject;

var tmpCC = this.Recipients.ToList().Where(p => p.ID != this.Mata.Person.ID).ToList(); //Should get the current receivers and pull itself out. :)
if (this.CC != null) tmpCC.AddRange(this.CC.ToList().Where(p => p.ID != this.Mata.Person.ID).ToList());
if (this.CC != null) tmpCC.AddRange(this.CC.ToList().Where(p => p.ID != this.Mata.Person.ID));
tmpCC.Sort();

return new MagisterMessage()
Expand All @@ -212,7 +214,7 @@ public MagisterMessage CreateReplyToAllMessage(string ContentAdd)
IDKey = this.IDKey,
IDOrginalReceiver = null,
IDOriginal = null,
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.ToString(true) + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
Deleted = false,
_IsRead = true,
Subject = tmpSubject,
Expand Down Expand Up @@ -245,7 +247,7 @@ public MagisterMessage CreateReplyMessage(string ContentAdd)
IDKey = this.IDKey,
IDOrginalReceiver = null,
IDOriginal = null,
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.ToString(true) + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
Deleted = false,
_IsRead = true,
Subject = tmpSubject,
Expand All @@ -265,7 +267,7 @@ public void Delete()
if (this.Deleted) return;

this.Deleted = true;
_Session.HttpClient.Delete(this.URL(), this.Mata.SessionID);
_Session.HttpClient.Delete(this.URL(), "SESSION_ID=" + this.Mata.SessionID + "&fileDownload=true");
}

/// <summary>
Expand Down Expand Up @@ -373,7 +375,7 @@ internal partial class MagisterStyleMessage
public int IdDeelNameSoort { get; set; }
#endregion

public MagisterMessage ToMagisterMessage()
public MagisterMessage ToMagisterMessage(int index)
{
var tmpReceivers = this.Ontvangers.ConvertAll(p => p.ToPerson(true));
tmpReceivers.Sort();
Expand Down Expand Up @@ -401,7 +403,8 @@ public MagisterMessage ToMagisterMessage()
Deleted = this.IsDefinitiefVerwijderd,
IDKey = this.IdKey,
SenderGroupID = this.IdDeelNameSoort,
_CanSend = false
_CanSend = false,
Index = index
};
}

Expand Down
10 changes: 5 additions & 5 deletions src/MataSharp/MagisterPerson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ internal partial struct MagisterStylePerson

private static List<MagisterStylePerson> GetPersons(string SearchFilter)
{
if (SearchFilter == null || !_Session.checkedPersons.ContainsKey(SearchFilter))
{
if (string.IsNullOrWhiteSpace(SearchFilter) || SearchFilter.Count() < 3) return new List<MagisterStylePerson>();
if (string.IsNullOrWhiteSpace(SearchFilter) || SearchFilter.Length < 3) return new List<MagisterStylePerson>();

if (!_Session.checkedPersons.ContainsKey(SearchFilter))
{
string URL = "https://" + _Session.School.URL + "/api/personen/" + _Session.Mata.UserID + "/communicatie/contactpersonen?q=" + SearchFilter;

string personsRAW = _Session.HttpClient.DownloadString(URL);
Expand All @@ -120,7 +120,7 @@ private static List<MagisterStylePerson> GetPersons(string SearchFilter)
_Session.checkedPersons.Add(SearchFilter, persons);
return persons;
}
else return _Session.checkedPersons.First(x => x.Key == SearchFilter).Value;
else return _Session.checkedPersons.First(x => x.Key.ToUpper() == SearchFilter.ToUpper()).Value;
}

public MagisterPerson ToPerson(bool download)
Expand Down Expand Up @@ -167,7 +167,7 @@ public MagisterPerson ToPerson(bool download)
Name = tmpName,
Description = tmpPerson.Omschrijving ?? tmpName,
Group = tmpPerson.Groep,
TeacherCode = tmpPerson.DocentCode,
TeacherCode = (!string.IsNullOrWhiteSpace(tmpPerson.DocentCode)) ? tmpPerson.DocentCode : null,
GroupID = tmpPerson.Type,
Initials = tmpPerson.Voorletters,
};
Expand Down
19 changes: 10 additions & 9 deletions src/MataSharp/Mata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,19 @@ public List<MagisterMessageFolder> GetMessageFolders()
var MessageFolders = JsonConvert.DeserializeObject<MagisterStyleMessageFolderListItem[]>(MessageFoldersRAW);

List<MagisterMessageFolder> tmplst = new List<MagisterMessageFolder>();
foreach (var MessageFolder in MessageFolders)
foreach (var messageFolder in MessageFolders)
{
var tmpFolderType = (Enum.IsDefined(typeof(MessageFolder), messageFolder.Id)) ? (MessageFolder)messageFolder.Id : MessageFolder.Unknown;
tmplst.Add(new MagisterMessageFolder()
{
Name = MessageFolder.Naam,
UnreadMessagesCount = MessageFolder.OngelezenBerichten,
ID = MessageFolder.Id,
ParentID = MessageFolder.ParentId,
Ref = MessageFolder.Ref,
MessagesURI = MessageFolder.BerichtenUri,
Name = messageFolder.Naam,
UnreadMessagesCount = messageFolder.OngelezenBerichten,
ID = messageFolder.Id,
ParentID = messageFolder.ParentId,
Ref = messageFolder.Ref,
MessagesURI = messageFolder.BerichtenUri,
Mata = this,
FolderType = (MessageFolder)MessageFolder.Id
FolderType = tmpFolderType
});
}
return tmplst;
Expand All @@ -146,7 +147,7 @@ public List<MagisterPerson> GetPersons(string SearchFilter)
_Session.checkedPersons.Add(SearchFilter, personRaw);
return personRaw.ConvertAll(p => p.ToPerson(false));
}
else return _Session.checkedPersons.First(x => x.Key == SearchFilter).Value.ConvertAll(p => p.ToPerson(false));
else return _Session.checkedPersons.First(x => x.Key.ToUpper() == SearchFilter.ToUpper()).Value.ConvertAll(p => p.ToPerson(false));
}

public List<Homework> GetHomework()
Expand Down
3 changes: 2 additions & 1 deletion src/MataSharp/MessageFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public enum MessageFolder : int
{ //Defines the folders where messages can be in, server handles it as ID's. We? We handle it as an enum :D
Inbox = -101,
SentMessages = -103,
Bin = -102
Bin = -102,
Unknown = 0
}

internal partial struct MagisterStyleMessageFolderListItem
Expand Down
30 changes: 21 additions & 9 deletions src/MataSharp/MessageList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ public Message SingleOrDefault(int max, Func<Message, bool> predicate)
else return default(Message);
}

/// <summary>
/// Checks if the given messages exist on the servers.
/// </summary>
/// <param name="item">The item to check if it exists.</param>
/// <returns>A boolean value telling if the given message exists.</returns>
public bool Contains(Message item)
{
return (!item.Deleted && this.Sender.FolderType == item.Folder);
}

private class Enumerator<T> : IEnumerator<T>, IDisposable where T : MagisterMessage
{
private int Next = 0;
Expand All @@ -282,7 +292,7 @@ public T Current
URL = "https://" + Mata.School.URL + "/api/personen/" + Mata.UserID + "/communicatie/berichten/mappen/" + Sender.ID + "/berichten/" + CompactMessage.Id;
string MessageRAW = _Session.HttpClient.DownloadString(URL);
var MessageClean = JsonConvert.DeserializeObject<MagisterStyleMessage>(MessageRAW);
return (T)MessageClean.ToMagisterMessage();
return (T)MessageClean.ToMagisterMessage(this.Skip);
}
}

Expand All @@ -296,7 +306,7 @@ public T GetAt(int index)
URL = "https://" + Mata.School.URL + "/api/personen/" + Mata.UserID + "/communicatie/berichten/mappen/" + Sender.ID + "/berichten/" + CompactMessage.Id;
string MessageRAW = _Session.HttpClient.DownloadString(URL);
var MessageClean = JsonConvert.DeserializeObject<MagisterStyleMessage>(MessageRAW);
return (T)MessageClean.ToMagisterMessage();
return (T)MessageClean.ToMagisterMessage(index);
}

public List<T> GetRange(int Ammount, int Skip)
Expand All @@ -306,13 +316,14 @@ public List<T> GetRange(int Ammount, int Skip)
string CompactMessagesRAW = _Session.HttpClient.DownloadString(URL);
var CompactMessages = JsonConvert.DeserializeObject<MagisterStyleMessageFolder>(CompactMessagesRAW);

var list = new List<T>();
var list = new List<T>(); int i = 0;
foreach (var CompactMessage in CompactMessages.Items)
{
URL = "https://" + Mata.School.URL + "/api/personen/" + this.Mata.UserID + "/communicatie/berichten/mappen/" + Sender.ID + "/berichten/" + CompactMessage.Id;
string MessageRAW = _Session.HttpClient.DownloadString(URL);
var MessageClean = JsonConvert.DeserializeObject<MagisterStyleMessage>(MessageRAW);
list.Add((T)MessageClean.ToMagisterMessage());
list.Add((T)MessageClean.ToMagisterMessage(i));
i++;
}
return list;
}
Expand All @@ -324,34 +335,35 @@ public List<T> GetUnread(uint Ammount, uint Skip = 0)
string CompactMessagesRAW = _Session.HttpClient.DownloadString(URL);
var CompactMessages = JsonConvert.DeserializeObject<MagisterStyleMessageFolder>(CompactMessagesRAW);

var list = new List<T>();
var list = new List<T>(); int i = 0;
foreach (var CompactMessage in CompactMessages.Items.Where(m => !m.IsGelezen))
{
URL = "https://" + Mata.School.URL + "/api/personen/" + this.Mata.UserID + "/communicatie/berichten/mappen/" + Sender.ID + "/berichten/" + CompactMessage.Id;
string MessageRAW = _Session.HttpClient.DownloadString(URL);
var MessageClean = JsonConvert.DeserializeObject<MagisterStyleMessage>(MessageRAW);
list.Add((T)MessageClean.ToMagisterMessage());
list.Add((T)MessageClean.ToMagisterMessage(i));
i++;
}
return list;
}

public List<T> GetUnread()
{
var list = new List<T>();
var list = new List<T>(); int index = 0;

for (uint i = 0; (list.Count != this.Sender.UnreadMessagesCount - 1); i++)
{
string URL = "https://" + Mata.School.URL + "/api/personen/" + this.Mata.UserID + "/communicatie/berichten/mappen/" + Sender.ID + "/berichten?$skip=" + (i * 25) + "&$top=" + ((i * 25) + 25);

string CompactMessagesRAW = _Session.HttpClient.DownloadString(URL);
var CompactMessages = JsonConvert.DeserializeObject<MagisterStyleMessageFolder>(CompactMessagesRAW);

foreach (var CompactMessage in CompactMessages.Items.Where(m => !m.IsGelezen))
{
URL = "https://" + Mata.School.URL + "/api/personen/" + this.Mata.UserID + "/communicatie/berichten/mappen/" + Sender.ID + "/berichten/" + CompactMessage.Id;
string MessageRAW = _Session.HttpClient.DownloadString(URL);
var MessageClean = JsonConvert.DeserializeObject<MagisterStyleMessage>(MessageRAW);
list.Add((T)MessageClean.ToMagisterMessage());
list.Add((T)MessageClean.ToMagisterMessage(index));
i++;
}
}
return list;
Expand Down
Loading

0 comments on commit df1f538

Please sign in to comment.