2021-08-26 01:43:31 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Threading.Tasks ;
using Newtonsoft.Json.Linq ;
using NodaTime ;
namespace PluralKit.Core
{
public partial class BulkImporter
{
private async Task < ImportResultNew > ImportTupperbox ( JObject importFile )
{
var tuppers = importFile . Value < JArray > ( "tuppers" ) ;
var newMembers = tuppers . Count ( t = > ! _existingMemberNames . TryGetValue ( "name" , out var memberId ) ) ;
2021-09-22 01:42:41 +00:00
await AssertMemberLimitNotReached ( newMembers ) ;
2021-08-26 01:43:31 +00:00
string lastSetTag = null ;
bool multipleTags = false ;
bool hasGroup = false ;
foreach ( JObject tupper in tuppers )
( lastSetTag , multipleTags , hasGroup ) = await ImportTupper ( tupper , lastSetTag ) ;
if ( multipleTags | | hasGroup )
{
var issueStr =
$"{Emojis.Warn} The following potential issues were detected converting your Tupperbox input file:" ;
if ( hasGroup )
issueStr + =
"\n- PluralKit does not support member groups. Members will be imported without groups." ;
if ( multipleTags )
issueStr + =
"\n- PluralKit does not support per-member system tags. Since you had multiple members with distinct tags, those tags will be applied to the members' *display names*/nicknames instead." ;
await _confirmFunc ( issueStr ) ;
_result . Success = true ;
}
return _result ;
}
private async Task < ( string lastSetTag , bool multipleTags , bool hasGroup ) > ImportTupper ( JObject tupper , string lastSetTag )
{
if ( ! tupper . ContainsKey ( "name" ) | | tupper [ "name" ] . Type = = JTokenType . Null )
throw new ImportException ( "Field 'name' cannot be null." ) ;
var hasGroup = tupper . ContainsKey ( "group_id" ) & & tupper [ "group_id" ] . Type ! = JTokenType . Null ;
var multipleTags = false ;
2021-08-27 15:03:47 +00:00
2021-08-26 01:43:31 +00:00
var name = tupper . Value < string > ( "name" ) ;
var patch = new MemberPatch ( ) ;
patch . Name = name ;
if ( tupper . ContainsKey ( "avatar_url" ) & & tupper [ "avatar_url" ] . Type ! = JTokenType . Null ) patch . AvatarUrl = tupper . Value < string > ( "avatar_url" ) . NullIfEmpty ( ) ;
if ( tupper . ContainsKey ( "brackets" ) )
{
var brackets = tupper . Value < JArray > ( "brackets" ) ;
if ( brackets . Count % 2 ! = 0 )
throw new ImportException ( $"Field 'brackets' in tupper {name} is invalid." ) ;
var tags = new List < ProxyTag > ( ) ;
2021-08-27 15:03:47 +00:00
for ( var i = 0 ; i < brackets . Count / 2 ; i + + )
tags . Add ( new ProxyTag ( ( string ) brackets [ i * 2 ] , ( string ) brackets [ i * 2 + 1 ] ) ) ;
2021-08-26 01:43:31 +00:00
patch . ProxyTags = tags . ToArray ( ) ;
}
// todo: && if is new member
if ( tupper . ContainsKey ( "posts" ) ) patch . MessageCount = tupper . Value < int > ( "posts" ) ;
if ( tupper . ContainsKey ( "show_brackets" ) ) patch . KeepProxy = tupper . Value < bool > ( "show_brackets" ) ;
if ( tupper . ContainsKey ( "birthday" ) & & tupper [ "birthday" ] . Type ! = JTokenType . Null )
{
var parsed = DateTimeFormats . TimestampExportFormat . Parse ( tupper . Value < string > ( "birthday" ) ) ;
if ( ! parsed . Success )
throw new ImportException ( $"Field 'birthday' in tupper {name} is invalid." ) ;
patch . Birthday = LocalDate . FromDateTime ( parsed . Value . ToDateTimeUtc ( ) ) ;
}
if ( tupper . ContainsKey ( "description" ) ) patch . Description = tupper . Value < string > ( "description" ) ;
2021-10-29 16:59:19 +00:00
if ( tupper . ContainsKey ( "nick" ) ) patch . DisplayName = tupper . Value < string > ( "nick" ) ;
2021-08-26 01:43:31 +00:00
if ( tupper . ContainsKey ( "tag" ) & & tupper [ "tag" ] . Type ! = JTokenType . Null )
{
var tag = tupper . Value < string > ( "tag" ) ;
if ( tag ! = lastSetTag )
{
lastSetTag = tag ;
multipleTags = true ;
}
patch . DisplayName = $"{name} {tag}" ;
}
2021-10-13 12:37:34 +00:00
patch . AssertIsValid ( ) ;
if ( patch . Errors . Count > 0 )
{
var err = patch . Errors [ 0 ] ;
if ( err is FieldTooLongError )
throw new ImportException ( $"Field {err.Key} in tupper {name} is too long "
+ $"({(err as FieldTooLongError).ActualLength} > {(err as FieldTooLongError).MaxLength})." ) ;
else if ( err . Text ! = null )
throw new ImportException ( $"tupper {name}: {err.Text}" ) ;
else
throw new ImportException ( $"Field {err.Key} in tupper {name} is invalid." ) ;
}
2021-08-26 01:43:31 +00:00
var isNewMember = false ;
if ( ! _existingMemberNames . TryGetValue ( name , out var memberId ) )
{
2021-09-30 01:51:38 +00:00
var newMember = await _repo . CreateMember ( _system . Id , name , _conn ) ;
2021-08-26 01:43:31 +00:00
memberId = newMember . Id ;
isNewMember = true ;
_result . Added + + ;
}
else
_result . Modified + + ;
2021-08-27 15:03:47 +00:00
2021-08-26 01:43:31 +00:00
_logger . Debug ( "Importing member with identifier {FileId} to system {System} (is creating new member? {IsCreatingNewMember})" ,
name , _system . Id , isNewMember ) ;
2021-09-30 01:51:38 +00:00
await _repo . UpdateMember ( memberId , patch , _conn ) ;
2021-08-27 15:03:47 +00:00
2021-08-26 01:43:31 +00:00
return ( lastSetTag , multipleTags , hasGroup ) ;
}
}
}