Skip to content

Commit

Permalink
v1.0.10
Browse files Browse the repository at this point in the history
Fixed bug in ApiList
Added User.SetPreference
Fix json conversion of DateTime?
  • Loading branch information
nikkilocke committed Aug 22, 2019
1 parent e6f2c53 commit 99c4a21
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
12 changes: 7 additions & 5 deletions MattermostApi/ApiEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public class UnixMsecDateTimeConverter : Newtonsoft.Json.JsonConverter {
static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public override bool CanConvert(Type objectType) {
return objectType == typeof(DateTime);
return objectType == typeof(DateTime) || objectType == typeof(DateTime?);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
var t = (long)reader.Value;
return t == 0 ? DateTime.MinValue : epoch.AddMilliseconds(t);
var t = (long?)reader.Value;
return t == null ? (DateTime?)null : t == 0 ? DateTime.MinValue : epoch.AddMilliseconds((long)t);
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
long msec = value == null ? 0 : (long)((DateTime)value - epoch).TotalMilliseconds;
long? msec = value == null ? (long?)null : (long)((DateTime)value - epoch).TotalMilliseconds;
writer.WriteValue(msec);
}
}
Expand Down Expand Up @@ -253,7 +253,9 @@ public IEnumerable<T> All(Api api) {
/// Override for the odd list return that doesn't use pages.
/// </summary>
public virtual ApiList<T> Convert(JObject j) {
return j.ConvertToObject<ApiList<T>>();
ApiList<T> result = j.ConvertToObject<ApiList<T>>();
result.Request = Request;
return result;
}

}
Expand Down
4 changes: 2 additions & 2 deletions MattermostApi/MattermostApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<PackageProjectUrl>https://github.com/nikkilocke/MattermostApi</PackageProjectUrl>
<RepositoryUrl>https://github.com/nikkilocke/MattermostApi</RepositoryUrl>
<RepositoryType>GitHub</RepositoryType>
<PackageReleaseNotes>Added static AddUser methods to Team and Channel</PackageReleaseNotes>
<Version>1.0.6</Version>
<PackageReleaseNotes>Fixed bug in ApiList</PackageReleaseNotes>
<Version>1.0.10</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
44 changes: 34 additions & 10 deletions MattermostApi/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public class SearchUsersQuery : ListRequest {

public class TeamForUser : ApiEntryBase {
public string id;
public DateTime create_at;
public DateTime update_at;
public DateTime delete_at;
public DateTime? create_at;
public DateTime? update_at;
public DateTime? delete_at;
public string display_name;
public string name;
public string description;
public string email;
public string type;
public string allowed_domains;
public string invite_id;
public bool allow_open_invite;
public bool? allow_open_invite;
public string company_name;
public string scheme_id;
public string group_constrained;
Expand All @@ -78,25 +78,41 @@ public class UserCreateInfo : ApiEntryBase {
public NotifyProps notify_props;
}

public class Preference : ApiEntryBase {
public string user_id;
public string category;
public string name;
public string value;

public Preference() {
}

public Preference(string category, string name, string value) {
this.category = category;
this.name = name;
this.value = value;
}
}

public class User : ApiEntryWithId {
public string username;
public string first_name;
public string last_name;
public string nickname;
public string email;
public bool email_verified;
public bool? email_verified;
public string auth_service;
public string roles;
public string locale;
public NotifyProps notify_props;
public JToken props;
public DateTime last_password_update;
public DateTime last_picture_update;
public int failed_attempts;
public bool mfa_active;
public DateTime? last_password_update;
public DateTime? last_picture_update;
public int? failed_attempts;
public bool? mfa_active;
public TimeZone timezone;
public string terms_of_service_id;
public DateTime terms_of_service_create_at;
public DateTime? terms_of_service_create_at;

public static async Task<ApiList<User>> GetAll(Api api, GetUsersQuery query = null) {
return await api.GetAsync<ApiList<User>>("users", query);
Expand Down Expand Up @@ -162,5 +178,13 @@ public async Task<ApiList<Channel>> ChannelsForTeam(Api api, string team_id) {
public async Task<UnreadMessageCount> GetUnreadMessages(Api api, string channel_id) {
return await api.GetAsync<UnreadMessageCount>(Api.Combine("users", id, "channels", channel_id, "unread"));
}

static public async Task SetPreference(Api api, string user_id, params Preference [] preferences) {
foreach (Preference p in preferences)
p.user_id = user_id;
await api.PutAsync(Api.Combine("users", user_id, "preferences"), null, preferences);
}


}
}

0 comments on commit 99c4a21

Please sign in to comment.