-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPerson.cs
192 lines (169 loc) · 6.47 KB
/
Person.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections;
using System.Collections.Generic;
namespace Cisco.Spark
{
/// <summary>
/// A registered user of the Spark platform.
/// </summary>
[Serializable]
public class Person : SparkObject
{
/// <summary>
/// The SparkType the implementation represents.
/// </summary>
internal override SparkType SparkType
{
get { return SparkType.Person; }
}
/// <summary>
/// Full name of the Person.
/// </summary>
public string DisplayName { get; set; }
/// <summary>
/// Friendly name of the Person.
/// </summary>
public string NickName { get; set; }
/// <summary>
/// First name of the Person.
/// </summary>
public string FirstName { get; set; }
/// <summary>
/// Last name of the Person.
/// </summary>
public string LastName { get; set; }
/// <summary>
/// Display picture of the Person.
/// </summary>
public Avatar Avatar { get; set; }
/// <summary>
/// List of emails associated with this person.
/// </summary>
public List<string> Emails { get; set; }
/// <summary>
/// Holds a reference to the currently authenticated <see cref="Person"/> given
/// by the authentication token in Request.
/// </summary>
/// <returns>Currently authenticated <see cref="Person"/>.</returns>
public static Person AuthenticatedUser { get; internal set; }
// TODO: New Objects for these.
// public Organization Organization {get; set;}
// public List<Role> Roles {get; set;}
// public List<License> Licenses {get; set;}
// public TimeZone Timezone {get; set;}
// public string Status {get; set;}
/// <summary>
/// Builds representation of existing Spark-side
/// Person. Use <see cref="Load"/> to populate rest of properties from Spark.
/// </summary>
/// <param name="id">Spark UID of the Person.</param>
public static Person FromId(string id)
{
return (Person)SparkObjectFactory.Make(id, SparkType.Person);
}
/// <summary>
/// Returns a dictionary representation of the object.
/// </summary>
/// <returns>The Dictionary.</returns>
/// <param name="fields">A specific list of fields to serialise.</param>
protected override Dictionary<string, object> ToDict(List<string> fields = null)
{
var data = base.ToDict();
data["displayName"] = DisplayName;
data["nickName"] = NickName;
data["firstName"] = FirstName;
data["lastName"] = LastName;
data["avatar"] = Avatar.Uri.ToString();
data["emails"] = Emails;
return CleanDict(data, fields);
}
/// <summary>
/// Populates an object with data received from Spark.
/// </summary>
/// <param name="data">Data.</param>
protected override void LoadDict(Dictionary<string, object> data)
{
base.LoadDict(data);
object displayName;
if (data.TryGetValue("displayName", out displayName))
{
DisplayName = displayName as string;
}
object nickName;
if (data.TryGetValue("nickName", out nickName))
{
NickName = nickName as string;
}
object firstName;
if (data.TryGetValue("firstName", out firstName))
{
FirstName = firstName as string;
}
object lastName;
if (data.TryGetValue("lastName", out lastName))
{
LastName = lastName as string;
}
// Avatar.
object avatarString;
if (data.TryGetValue("avatar", out avatarString))
{
var uri = new Uri(avatarString as string);
Avatar = new Avatar(uri);
}
// Emails.
Emails = new List<string>();
foreach (var obj in data["emails"] as List<object>)
{
Emails.Add(obj as string);
}
}
/// <summary>
/// Sets <see cref="AuthenticatedUser"/> to the currently authenticated user.
/// </summary>
/// <param name="error">Error from Spark, if any.</param>
/// <param name="success">True if the operation completed successfully.</param>
public static IEnumerator GetMyself(Action<SparkMessage> error, Action<bool> success)
{
var getRecordRoutine = Request.Instance.GetRecord("me", SparkType.Person, error, dict =>
{
var id = dict["id"] as string;
AuthenticatedUser = FromId(id);
AuthenticatedUser.LoadDict(dict);
success(true);
});
yield return getRecordRoutine;
}
/// <summary>
/// Lists all Person objects found on Spark matching the given criteria.
/// </summary>
/// <param name="error">Error from Spark, if any.</param>
/// <param name="results">List of People found.</param>
/// <param name="email">An email address to filter on.</param>
/// <param name="displayName">A display name to filter on.</param>
/// <param name="max">Maximum number of results to return.</param>
public static IEnumerator ListPeople(Action<SparkMessage> error, Action<List<Person>> results, string email = null, string displayName = null, int max = 0)
{
// TODO: Admins are not bound by this rule.
if (email == null && displayName == null)
{
throw new ArgumentException("One of Email or Display Name must be provided when listing People.");
}
var constraints = new Dictionary<string, string>();
if (email != null)
{
constraints.Add("email", email);
}
else if (displayName != null)
{
constraints.Add("displayName", displayName);
}
if (max > 0)
{
constraints.Add("max", max.ToString());
}
var listObjects = ListObjects(constraints, SparkType.Person, error, results);
yield return Request.Instance.StartCoroutine(listObjects);
}
}
}