Skip to content

Commit cb4903c

Browse files
committed
Add staff recipients search and filters
1 parent f752b49 commit cb4903c

4 files changed

Lines changed: 158 additions & 7 deletions

File tree

API.Test/MessagesTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ public void GetReceptionPeople()
8888
Assert.Fail();
8989
}
9090

91+
[Test]
92+
public void GetStaffFilters()
93+
{
94+
Client.LoginAsync(s_Server, s_LoginName, s_UserName, s_Password).Wait();
95+
96+
Task<Dictionary<string, FilterItem[]>> filters = Client.GetStaffSearchFiltersAsync();
97+
filters.Wait();
98+
99+
if (filters.Result.Count > 0)
100+
Assert.Pass();
101+
else
102+
Assert.Fail();
103+
}
104+
91105
[Test]
92106
public void GetDrafts()
93107
{
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace WebUntisAPI.Client.Models.Messages
9+
{
10+
/// <summary>
11+
/// A filter item that can applied to a staff search
12+
/// </summary>
13+
public struct FilterItem
14+
{
15+
/// <summary>
16+
/// The id of this filter item
17+
/// </summary>
18+
[JsonProperty("referenceId")]
19+
public int ReferenceId { get; set; }
20+
21+
/// <summary>
22+
/// The displayed name of this filter item
23+
/// </summary>
24+
[JsonProperty("name")]
25+
public string Name { get; set; }
26+
}
27+
}

WebUntisAPI.Client/WebUntisClient.Messages.cs

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ public async Task<MessagePermissions> GetMessagePermissionsAsync(CancellationTok
4545
}
4646

4747
/// <summary>
48-
/// Get all available reception people
48+
/// Get all available reception teachers people
4949
/// </summary>
50+
/// <remarks>
51+
/// Use this method only when <see cref="MessagePermissions.RecipientOptions"/> contains "TEACHER"
52+
/// </remarks>
5053
/// <param name="ct">Cancellation token</param>
5154
/// <returns>The people (<see cref="KeyValuePair{TKey, TValue}.Key"/> is the type of people that are contained in <see cref="KeyValuePair{TKey, TValue}.Value"/>)</returns>
5255
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
@@ -64,6 +67,111 @@ public async Task<Dictionary<string, MessagePerson[]>> GetMessagePeopleAsync(Can
6467
return personTypes;
6568
}
6669

70+
/// <summary>
71+
/// Get all possible filters for the staff
72+
/// </summary>
73+
/// <remarks>
74+
/// Use this method only when <see cref="MessagePermissions.RecipientOptions"/> contains "STAFF"
75+
/// </remarks>
76+
/// <param name="ct">Cancellation token</param>
77+
/// <returns>The filters (the <see cref="KeyValuePair{TKey, TValue}.Key"/> is the type of the filter and <see cref="KeyValuePair{TKey, TValue}.Value"/> are the available filters for that type)</returns>
78+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
79+
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
80+
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
81+
public async Task<Dictionary<string, FilterItem[]>> GetStaffSearchFiltersAsync(CancellationToken ct = default)
82+
{
83+
string responseString = await MakeAPIGetRequestAsync("/WebUntis/api/rest/view/v2/messages/recipients/STAFF/filter", ct);
84+
85+
JObject obj = JObject.Parse(responseString);
86+
JArray types = obj["filters"].Value<JArray>();
87+
88+
Dictionary<string, FilterItem[]> filters = new Dictionary<string, FilterItem[]>();
89+
foreach (JObject filter in types.Cast<JObject>())
90+
filters.Add(filter["type"].ToObject<string>(), filter["items"].ToObject<List<FilterItem>>().ToArray());
91+
92+
return filters;
93+
}
94+
95+
/// <summary>
96+
/// Get all staff recipients for the applied filters
97+
/// </summary>
98+
/// <remarks>
99+
/// Use this method only when <see cref="MessagePermissions.RecipientOptions"/> contains "STAFF"
100+
/// </remarks>
101+
/// <param name="searchText">The search text</param>
102+
/// <param name="filters">The applied filters (use the data from <see cref="GetStaffSearchFiltersAsync(CancellationToken)"/>)</param>
103+
/// <param name="ct">Cancellation token</param>
104+
/// <returns>The recipients</returns>
105+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
106+
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
107+
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
108+
public async Task<MessagePerson[]> GetStaffFilterSearchResultAsync(string searchText, Dictionary<string, FilterItem[]> filters, CancellationToken ct = default)
109+
{
110+
// Check for disposing
111+
if (_disposedValue)
112+
throw new ObjectDisposedException(GetType().FullName);
113+
114+
// Check if you logged in
115+
if (!LoggedIn)
116+
throw new UnauthorizedAccessException("You're not logged in");
117+
118+
// Write request
119+
StringWriter sw = new StringWriter();
120+
using (JsonWriter writer = new JsonTextWriter(sw))
121+
{
122+
writer.WriteStartObject();
123+
124+
writer.WritePropertyName("filters");
125+
writer.WriteStartArray();
126+
foreach (KeyValuePair<string, FilterItem[]> filter in filters)
127+
{
128+
writer.WriteStartObject();
129+
130+
writer.WritePropertyName("type");
131+
writer.WriteValue(filter.Key);
132+
133+
writer.WritePropertyName("items");
134+
writer.WriteRawValue(JsonConvert.SerializeObject(filter.Value));
135+
136+
writer.WriteEndObject();
137+
}
138+
139+
writer.WriteEndArray();
140+
141+
writer.WritePropertyName("searchText");
142+
writer.WriteValue(searchText);
143+
144+
writer.WriteEndObject();
145+
}
146+
147+
HttpRequestMessage request = new HttpRequestMessage()
148+
{
149+
Method = HttpMethod.Post,
150+
RequestUri = new Uri(ServerUrl + $"/WebUntis/api/rest/view/v2/messages/recipients/STAFF/filter"),
151+
Content = new StringContent(sw.ToString())
152+
};
153+
request.Headers.Add("JSESSIONID", _sessionId);
154+
request.Headers.Add("schoolname", _schoolName);
155+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _bearerToken);
156+
157+
HttpResponseMessage response = await _client.SendAsync(request, ct);
158+
159+
// Verify response
160+
if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden)
161+
{
162+
_ = LogoutAsync();
163+
throw new UnauthorizedAccessException("You're not logged in");
164+
}
165+
166+
if (response.StatusCode != HttpStatusCode.OK)
167+
throw new HttpRequestException($"There was an error while the http request (Code: {response.StatusCode}).");
168+
169+
string responseString = await response.Content.ReadAsStringAsync();
170+
171+
JObject obj = JObject.Parse(responseString);
172+
return obj["users"].Value<List<MessagePerson>>().ToArray();
173+
}
174+
67175
/// <summary>
68176
/// Get the your message inbox
69177
/// </summary>

WebUntisAPI.Client/WebUntisclient.Profile.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public partial class WebUntisClient
3636
/// The rendered image will be rendered to 300px * 300px and a font size of 124
3737
/// </para>
3838
/// </remarks>
39-
/// <param name="person"></param>
40-
/// <param name="fontName"></param>
41-
/// <param name="ct"></param>
39+
/// <param name="person">The person for the image</param>
40+
/// <param name="fontName">The font size to render</param>
41+
/// <param name="ct">Cancellation token</param>
4242
/// <returns></returns>
43-
/// <exception cref="ObjectDisposedException"></exception>
44-
/// <exception cref="UnauthorizedAccessException"></exception>
45-
/// <exception cref="HttpRequestException"></exception>
43+
/// <exception cref="ObjectDisposedException">Thrown when the instance was disposed</exception>
44+
/// <exception cref="UnauthorizedAccessException">Thrown when you're logged in</exception>
45+
/// <exception cref="HttpRequestException">Thrown when an error happened while the http request</exception>
4646
#if NET47 || NET481
4747
public async Task<Image> GetMessagePersonProfileImageAsync(MessagePerson person, string fontName = "Segoe UI", CancellationToken ct = default)
4848
#elif NET6_0_OR_GREATER
@@ -172,5 +172,7 @@ public async Task<Image> GetMessagePersonProfileImageAsync(MessagePerson person,
172172
return image;
173173
#endif
174174
}
175+
176+
175177
}
176178
}

0 commit comments

Comments
 (0)