private async Task UpdateAutoproxy(Context ctx, AutoproxyMode autoproxyMode, MemberId?autoproxyMember) { var patch = new AutoproxyPatch { AutoproxyMode = autoproxyMode, AutoproxyMember = autoproxyMember }; await ctx.Repository.UpdateAutoproxy(ctx.System.Id, ctx.Guild.Id, null, patch); }
private async Task <IActionResult> Patch(PKSystem system, ulong?guildId, ulong?channelId, JObject data, AutoproxySettings oldData) { var updateMember = data.ContainsKey("autoproxy_member"); PKMember?member = null; if (updateMember) { member = await ResolveMember(data.Value <string>("autoproxy_member")); } var patch = AutoproxyPatch.FromJson(data, member?.Id); patch.AssertIsValid(); if (updateMember && member == null) { patch.Errors.Add(new("autoproxy_member", "Member not found.")); } if (updateMember && ((patch.AutoproxyMode.IsPresent && patch.AutoproxyMode.Value == AutoproxyMode.Latch) || oldData.AutoproxyMode == AutoproxyMode.Latch)) { patch.Errors.Add(new("autoproxy_member", "Cannot update autoproxy member if autoproxy mode is set to latch")); } if (patch.Errors.Count > 0) { throw new ModelParseError(patch.Errors); } var res = await _repo.UpdateAutoproxy(system.Id, guildId, channelId, patch); if (!updateMember && oldData.AutoproxyMember != null) { member = await _repo.GetMember(oldData.AutoproxyMember.Value); } return(Ok(res.ToJson(member?.Hid))); }
public static AutoproxyPatch FromJson(JObject o, MemberId?autoproxyMember = null) { var p = new AutoproxyPatch(); if (o.ContainsKey("autoproxy_mode")) { var(autoproxyMode, error) = o.Value <JToken>("autoproxy_mode").ParseAutoproxyMode(); if (error != null) { p.AutoproxyMode = Partial <AutoproxyMode> .Present((AutoproxyMode)(-1)); } else { p.AutoproxyMode = autoproxyMode.Value; } } p.AutoproxyMember = autoproxyMember ?? Partial <MemberId?> .Absent; return(p); }
public async Task UpdateAutoproxy(SystemId system, ulong?guildId, ulong?channelId, AutoproxyPatch patch) { var locationStr = guildId != null ? "guild" : (channelId != null ? "channel" : "global"); _logger.Information("Updated autoproxy for {SystemId} in location {location}: {@AutoproxyPatch}", system, locationStr, patch); var query = patch.Apply(new Query("autoproxy") .Where("system", system) .Where("guild_id", guildId ?? 0) .Where("channel_id", channelId ?? 0) ); _ = _dispatch.Dispatch(system, guildId, channelId, patch); await _db.ExecuteQuery(query); }
public Task <AutoproxySettings> UpdateAutoproxy(SystemId system, ulong?guildId, ulong?channelId, AutoproxyPatch patch) { var locationStr = guildId != null ? "guild" : (channelId != null ? "channel" : "global"); _logger.Information("Updated autoproxy for {SystemId} in location {location}: {@AutoproxyPatch}", system, locationStr, patch); var query = patch.Apply(new Query("autoproxy") .Where("system", system) .Where("guild_id", guildId ?? 0) .Where("channel_id", channelId ?? 0) ); _ = _dispatch.Dispatch(system, guildId, channelId, patch); return(_db.QueryFirst <AutoproxySettings>(query, "returning *")); }