feat: import messages from go version
This commit is contained in:
parent
b56a71e105
commit
a50a8567dd
15 changed files with 503 additions and 769 deletions
|
|
@ -13,10 +13,12 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
using System.Data.Common;
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
using Catalogger.Backend.Database;
|
||||
using Catalogger.Backend.Database.Models;
|
||||
using Dapper;
|
||||
using NodaTime.Extensions;
|
||||
using Serilog;
|
||||
|
||||
|
|
@ -24,7 +26,7 @@ namespace Catalogger.GoImporter;
|
|||
|
||||
public static class GuildImport
|
||||
{
|
||||
public static async Task DoImportAsync(DatabaseContext db, string filename)
|
||||
public static async Task DoImportAsync(DatabaseConnection conn, string filename)
|
||||
{
|
||||
var rawData = await File.OpenText(filename).ReadToEndAsync();
|
||||
var data = JsonSerializer.Deserialize<GuildsExport>(rawData, Program.JsonOptions);
|
||||
|
|
@ -37,16 +39,15 @@ public static class GuildImport
|
|||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
|
||||
await using var tx = await db.Database.BeginTransactionAsync();
|
||||
await using var tx = await conn.BeginTransactionAsync();
|
||||
|
||||
foreach (var g in data.Guilds)
|
||||
ImportGuild(db, g);
|
||||
await ImportGuildAsync(conn, tx, g);
|
||||
foreach (var i in data.Invites)
|
||||
ImportInvite(db, i);
|
||||
await ImportInviteAsync(conn, tx, i);
|
||||
foreach (var w in data.Watchlist)
|
||||
ImportWatchlistEntry(db, w);
|
||||
await ImportWatchlistEntryAsync(conn, tx, w);
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
await tx.CommitAsync();
|
||||
|
||||
Log.Information(
|
||||
|
|
@ -55,7 +56,11 @@ public static class GuildImport
|
|||
);
|
||||
}
|
||||
|
||||
private static void ImportGuild(DatabaseContext db, GoGuild guild)
|
||||
private static async Task ImportGuildAsync(
|
||||
DatabaseConnection conn,
|
||||
DbTransaction tx,
|
||||
GoGuild guild
|
||||
)
|
||||
{
|
||||
var channels = new Guild.ChannelConfig
|
||||
{
|
||||
|
|
@ -90,38 +95,61 @@ public static class GuildImport
|
|||
if (ulong.TryParse(key, out var fromId) && ulong.TryParse(value, out var toId))
|
||||
channels.Redirects[fromId] = toId;
|
||||
|
||||
var dbGuild = new Guild
|
||||
{
|
||||
Id = guild.Id,
|
||||
BannedSystems = guild.BannedSystems,
|
||||
KeyRoles = guild.KeyRoles,
|
||||
Channels = channels,
|
||||
};
|
||||
|
||||
db.Guilds.Add(dbGuild);
|
||||
await conn.ExecuteAsync(
|
||||
"""
|
||||
insert into guilds (id, channels, banned_systems, key_roles)
|
||||
values (@Id, @Channels::jsonb, @BannedSystems, @KeyRoles)
|
||||
""",
|
||||
new
|
||||
{
|
||||
guild.Id,
|
||||
Channels = channels,
|
||||
guild.BannedSystems,
|
||||
guild.KeyRoles,
|
||||
},
|
||||
tx
|
||||
);
|
||||
}
|
||||
|
||||
private static void ImportInvite(DatabaseContext db, GoInvite invite)
|
||||
private static async Task ImportInviteAsync(
|
||||
DatabaseConnection conn,
|
||||
DbTransaction tx,
|
||||
GoInvite invite
|
||||
)
|
||||
{
|
||||
var dbInvite = new Invite
|
||||
{
|
||||
GuildId = invite.GuildId,
|
||||
Code = invite.Code,
|
||||
Name = invite.Name,
|
||||
};
|
||||
db.Invites.Add(dbInvite);
|
||||
await conn.ExecuteAsync(
|
||||
"insert into invites (code, guild_id, name) values (@Code, @GuildId, @Name)",
|
||||
new
|
||||
{
|
||||
invite.GuildId,
|
||||
invite.Code,
|
||||
invite.Name,
|
||||
},
|
||||
tx
|
||||
);
|
||||
}
|
||||
|
||||
private static void ImportWatchlistEntry(DatabaseContext db, GoWatchlistEntry watchlistEntry)
|
||||
private static async Task ImportWatchlistEntryAsync(
|
||||
DatabaseConnection conn,
|
||||
DbTransaction tx,
|
||||
GoWatchlistEntry watchlistEntry
|
||||
)
|
||||
{
|
||||
var dbWatchlist = new Watchlist
|
||||
{
|
||||
GuildId = watchlistEntry.GuildId,
|
||||
UserId = watchlistEntry.UserId,
|
||||
ModeratorId = watchlistEntry.ModeratorId,
|
||||
AddedAt = watchlistEntry.AddedAt,
|
||||
Reason = watchlistEntry.Reason,
|
||||
};
|
||||
await conn.ExecuteAsync(
|
||||
"""
|
||||
insert into watchlists (guild_id, user_id, added_at, moderator_id, reason)
|
||||
values (@GuildId, @UserId, @AddedAt, @ModeratorId, @Reason)
|
||||
""",
|
||||
new
|
||||
{
|
||||
watchlistEntry.GuildId,
|
||||
watchlistEntry.UserId,
|
||||
watchlistEntry.AddedAt,
|
||||
watchlistEntry.ModeratorId,
|
||||
watchlistEntry.Reason,
|
||||
},
|
||||
tx
|
||||
);
|
||||
}
|
||||
|
||||
private static ulong TryParse(this Dictionary<string, string> dict, string key) =>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue