Move hid generation to the database. Closes #157.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -69,7 +70,7 @@ namespace PluralKit.Core
|
||||
// if not, roll a new hid and we'll insert one with that
|
||||
// (we can't trust the hid given in the member, it might let us overwrite another system's members)
|
||||
var existingMember = FindExistingMemberInSystem(member.Hid, member.Name);
|
||||
string newHid = existingMember?.Hid ?? await FindFreeHid();
|
||||
string newHid = existingMember?.Hid ?? await _conn.QuerySingleAsync<string>("find_free_member_hid", commandType: CommandType.StoredProcedure);
|
||||
|
||||
// Upsert member data and return the ID
|
||||
QueryBuilder qb = QueryBuilder.Upsert("members", "hid")
|
||||
@@ -112,19 +113,6 @@ namespace PluralKit.Core
|
||||
return null;
|
||||
}
|
||||
|
||||
private async Task<string> FindFreeHid()
|
||||
{
|
||||
string hid;
|
||||
do
|
||||
{
|
||||
hid = await _conn.QuerySingleOrDefaultAsync<string>(
|
||||
"select @Hid where not exists (select id from members where hid = @Hid)",
|
||||
new {Hid = StringUtils.GenerateHid()});
|
||||
} while (hid == null);
|
||||
|
||||
return hid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register switches in bulk.
|
||||
/// </summary>
|
||||
|
@@ -6,18 +6,6 @@ namespace PluralKit.Core
|
||||
{
|
||||
public static class StringUtils
|
||||
{
|
||||
public static string GenerateHid()
|
||||
{
|
||||
var rnd = new Random();
|
||||
var charset = "abcdefghijklmnopqrstuvwxyz";
|
||||
string hid = "";
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
hid += charset[rnd.Next(charset.Length)];
|
||||
}
|
||||
return hid;
|
||||
}
|
||||
|
||||
public static string GenerateToken()
|
||||
{
|
||||
var buf = new byte[48]; // Results in a 64-byte Base64 string (no padding)
|
||||
|
Reference in New Issue
Block a user