Skip to content

Commit

Permalink
Modify and Delete schedules
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Dec 13, 2024
1 parent f7896f0 commit afd31d9
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 69 deletions.
12 changes: 10 additions & 2 deletions src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// 表示一个通用的日程。
/// </summary>
public interface IGuildSchedule : IEntity<ulong>
public interface IGuildSchedule : IEntity<ulong>, IUpdateable, IDeletable
{
/// <summary>
/// 获取此日程所属的频道。
Expand Down Expand Up @@ -43,5 +43,13 @@ public interface IGuildSchedule : IEntity<ulong>
/// <summary>
/// 获取此日程的提醒类型。
/// </summary>
RemindType? RemindType { get; }
RemindType RemindType { get; }

/// <summary>
/// 修改此日程。
/// </summary>
/// <param name="func"> 一个包含修改日程属性的委托。 </param>
/// <param name="options"> 发送请求时要使用的选项。 </param>
/// <returns> 一个表示直日程性修改操作的异步任务。 </returns>
Task ModifyAsync(Action<ModifyGuildScheduleProperties> func, RequestOptions? options = null);

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macOS-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macOS-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macOS-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macOS-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macOS-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 54 in src/QQBot.Net.Core/Entities/Schedules/IGuildSchedule.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macOS-latest) / Build and Test

The type or namespace name 'ModifyGuildScheduleProperties' could not be found (are you missing a using directive or an assembly reference?)
}
9 changes: 7 additions & 2 deletions src/QQBot.Net.Core/QQBot.Net.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
<PackageId>QQBot.Net.Core</PackageId>
<Description>The core definition library for QQBot.Net.</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1"/>
</ItemGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('net9.0')) ">
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
</ItemGroup>

<ItemGroup Condition=" !$(TargetFramework.StartsWith('net9.0')) ">
<PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="PolySharp" Version="1.15.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion src/QQBot.Net.Rest/API/Common/Schedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ internal class Schedule

[JsonPropertyName("remind_type")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public required RemindType? RemindType { get; set; }
public required RemindType RemindType { get; set; }
}
5 changes: 4 additions & 1 deletion src/QQBot.Net.Rest/API/Rest/ModifyScheduleParams.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Text.Json.Serialization;

namespace QQBot.API.Rest;

internal class ModifyScheduleParams
{
[JsonPropertyName("schedule")]
public required ScheduleParams Schedule { get; set; }
}
}
43 changes: 36 additions & 7 deletions src/QQBot.Net.Rest/Entities/Channels/ChannelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,9 @@ public static async Task<IReadOnlyCollection<RestGuildSchedule>> GetSchedulesAsy
];
}

public static async Task<RestGuildSchedule> GetScheduleAsync(IScheduleChannel channel,
BaseQQBotClient client, ulong id, RequestOptions? options)
{
Schedule model = await client.ApiClient.GetScheduleAsync(channel.Id, id, options);
return RestGuildSchedule.Create(client, channel, model,
model.Creator.User is not null ? RestGuildMember.Create(client, channel.Guild, model.Creator.User, model.Creator) : null);
}
public static async Task<Schedule> GetScheduleAsync(IScheduleChannel channel,
BaseQQBotClient client, ulong id, RequestOptions? options) =>
await client.ApiClient.GetScheduleAsync(channel.Id, id, options).ConfigureAwait(false);

public static async Task<RestGuildSchedule> CreateScheduleAsync(IScheduleChannel channel, BaseQQBotClient client,
string name, DateTimeOffset startTime, DateTimeOffset endTime, string? description, IGuildChannel? jumpChannel,
Expand All @@ -567,5 +563,38 @@ public static async Task<RestGuildSchedule> CreateScheduleAsync(IScheduleChannel
model.Creator.User is not null ? RestGuildMember.Create(client, channel.Guild, model.Creator.User, model.Creator) : null);
}

public static async Task<Schedule> ModifyAsync(IGuildSchedule schedule, BaseQQBotClient client,
Action<ModifyGuildScheduleProperties> func, RequestOptions? options)
{
ModifyGuildScheduleProperties props = new()
{
Name = schedule.Name,
Description = schedule.Description,
StartTime = schedule.StartTime,
EndTime = schedule.EndTime,
JumpChannelId = schedule.JumpChannelId,
RemindType = schedule.RemindType
};
func(props);
ModifyScheduleParams args = new()
{
Schedule = new ScheduleParams
{
Name = props.Name,
Description = props.Description,
StartTimestamp = props.StartTime,
EndTimestamp = props.EndTime,
JumpChannelId = props.JumpChannelId,
RemindType = props.RemindType
}
};
return await client.ApiClient
.ModifyScheduleAsync(schedule.Channel.Id, schedule.Id, args, options)
.ConfigureAwait(false);
}

public static Task DeleteAsync(IGuildSchedule schedule, BaseQQBotClient client, RequestOptions? options) =>
client.ApiClient.DeleteScheduleAsync(schedule.Channel.Id, schedule.Id, options);

#endregion
}
11 changes: 9 additions & 2 deletions src/QQBot.Net.Rest/Entities/Channels/RestScheduleChannel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using QQBot.API;
using Model = QQBot.API.Channel;

namespace QQBot.Rest;
Expand Down Expand Up @@ -71,8 +72,14 @@ public Task<IReadOnlyCollection<RestGuildSchedule>> GetSchedulesAsync(DateTimeOf
ChannelHelper.GetSchedulesAsync(this, Client, since, options);

/// <inheritdoc cref="QQBot.IScheduleChannel.GetScheduleAsync(System.UInt64,QQBot.RequestOptions)" />
public Task<RestGuildSchedule> GetScheduleAsync(ulong id, RequestOptions? options = null) =>
ChannelHelper.GetScheduleAsync(this, Client, id, options);
public async Task<RestGuildSchedule> GetScheduleAsync(ulong id, RequestOptions? options = null)
{
Schedule model = await ChannelHelper.GetScheduleAsync(this, Client, id, options);
RestGuildMember? creator = model.Creator.User is not null
? RestGuildMember.Create(Client, Guild, model.Creator.User, model.Creator)
: null;
return RestGuildSchedule.Create(Client, this, model, creator);
}

/// <inheritdoc cref="QQBot.IScheduleChannel.CreateScheduleAsync(System.String,System.DateTimeOffset,System.DateTimeOffset,System.String,QQBot.IGuildChannel,QQBot.RemindType,QQBot.RequestOptions)" />
public Task<RestGuildSchedule> CreateScheduleAsync(string name, DateTimeOffset startTime, DateTimeOffset endTime,
Expand Down
82 changes: 82 additions & 0 deletions src/QQBot.Net.Rest/Entities/Schedules/RestGuildSchedule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
namespace QQBot.Rest;

/// <summary>
/// 表示一个基于 REST 的日程。
/// </summary>
public class RestGuildSchedule : RestEntity<ulong>, IGuildSchedule
{
/// <inheritdoc />
public IScheduleChannel Channel { get; }

/// <inheritdoc />
public string Name { get; private set; }

/// <inheritdoc />
public string? Description { get; private set; }

/// <inheritdoc />
public DateTimeOffset StartTime { get; private set; }

/// <inheritdoc />
public DateTimeOffset EndTime { get; private set; }

/// <inheritdoc />
public IGuildMember? Creator { get; private set; }

/// <inheritdoc />
public ulong? JumpChannelId { get; private set; }

/// <inheritdoc />
public RemindType RemindType { get; private set; }

internal RestGuildSchedule(BaseQQBotClient client, IScheduleChannel channel, ulong id,
string name, string? description, DateTimeOffset startTime, DateTimeOffset endTime,
IGuildMember? creator, ulong? jumpChannelId, RemindType remindType)
: base(client, id)
{
Channel = channel;
Name = name;
Description = description;
StartTime = startTime;
EndTime = endTime;
Creator = creator;
JumpChannelId = jumpChannelId;
RemindType = remindType;
}

internal static RestGuildSchedule Create(BaseQQBotClient client,
IScheduleChannel channel, API.Schedule model, IGuildMember? creator) =>
new(client, channel, model.Id,
model.Name, model.Description, model.StartTimestamp, model.EndTimestamp,
creator, model.JumpChannelId != 0 ? model.JumpChannelId : null, model.RemindType);

internal void Update(API.Schedule model)
{
Name = model.Name;
Description = model.Description;
StartTime = model.StartTimestamp;
EndTime = model.EndTimestamp;
if (model.Creator.User is { } userModel && Creator is RestGuildMember creator)
creator.Update(userModel, model.Creator);
JumpChannelId = model.JumpChannelId != 0 ? model.JumpChannelId : null;
RemindType = model.RemindType;
}

/// <inheritdoc />
public async Task UpdateAsync(RequestOptions? options = null)
{
API.Schedule model = await ChannelHelper.GetScheduleAsync(Channel, Client, Id, options).ConfigureAwait(false);
Update(model);
}

/// <inheritdoc />
public async Task ModifyAsync(Action<ModifyGuildScheduleProperties> func, RequestOptions? options = null)
{
API.Schedule model = await ChannelHelper.ModifyAsync(this, Client, func, options).ConfigureAwait(false);
Update(model);
}

/// <inheritdoc />
public Task DeleteAsync(RequestOptions? options = null) =>
ChannelHelper.DeleteAsync(this, Client, options);
}
52 changes: 0 additions & 52 deletions src/QQBot.Net.Rest/Entities/Schedules/RestSchedule.cs

This file was deleted.

19 changes: 17 additions & 2 deletions src/QQBot.Net.WebSocket/Entities/Channels/SocketScheduleChannel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using QQBot.API;
using QQBot.Rest;
using Model = QQBot.API.Channel;

Expand Down Expand Up @@ -72,8 +73,22 @@ public Task<IReadOnlyCollection<RestGuildSchedule>> GetSchedulesAsync(DateTimeOf
ChannelHelper.GetSchedulesAsync(this, Client, since, options);

/// <inheritdoc cref="QQBot.IScheduleChannel.GetScheduleAsync(System.UInt64,QQBot.RequestOptions)" />
public Task<RestGuildSchedule> GetScheduleAsync(ulong id, RequestOptions? options = null) =>
ChannelHelper.GetScheduleAsync(this, Client, id, options);
public async Task<RestGuildSchedule> GetScheduleAsync(ulong id, RequestOptions? options = null)
{
Schedule model = await ChannelHelper.GetScheduleAsync(this, Client, id, options);
IGuildMember? creator;
if (model.Creator.User is not { Id: var userId } userModel)
creator = null;
else if (Guild.GetUser(userId) is { } socketGuildMember)
{
socketGuildMember.Update(Client.State, userModel, model.Creator);
creator = socketGuildMember;
}
else
creator = RestGuildMember.Create(Client, Guild, userModel, model.Creator);

return RestGuildSchedule.Create(Client, this, model, creator);
}

/// <inheritdoc cref="QQBot.IScheduleChannel.CreateScheduleAsync(System.String,System.DateTimeOffset,System.DateTimeOffset,System.String,QQBot.IGuildChannel,QQBot.RemindType,QQBot.RequestOptions)" />
public Task<RestGuildSchedule> CreateScheduleAsync(string name, DateTimeOffset startTime, DateTimeOffset endTime,
Expand Down

0 comments on commit afd31d9

Please sign in to comment.