mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2024-11-24 20:42:46 +01:00
33 lines
1.0 KiB
C#
33 lines
1.0 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using DHT.Server.Data;
|
|
|
|
namespace DHT.Server.Database;
|
|
|
|
public static class DatabaseExtensions {
|
|
public static async Task AddFrom(this IDatabaseFile target, IDatabaseFile source) {
|
|
await target.Users.Add(await source.Users.Get().ToListAsync());
|
|
await target.Servers.Add(await source.Servers.Get().ToListAsync());
|
|
await target.Channels.Add(await source.Channels.Get().ToListAsync());
|
|
|
|
const int MessageBatchSize = 100;
|
|
List<Message> batchedMessages = new (MessageBatchSize);
|
|
|
|
await foreach (var message in source.Messages.Get()) {
|
|
batchedMessages.Add(message);
|
|
|
|
if (batchedMessages.Count >= MessageBatchSize) {
|
|
await target.Messages.Add(batchedMessages);
|
|
batchedMessages.Clear();
|
|
}
|
|
}
|
|
|
|
await target.Messages.Add(batchedMessages);
|
|
|
|
await foreach (var download in source.Downloads.GetWithoutData()) {
|
|
await target.Downloads.AddDownload(download.Status == DownloadStatus.Success ? await source.Downloads.HydrateWithData(download) : download);
|
|
}
|
|
}
|
|
}
|