Beispiel #1
0
        public async Task <FullUser> Register([FromBody] RegistrationData registrationData)
        {
            if (registrationData == null)
            {
                throw new ArgumentNullException("registrationData");
            }
            if (registrationData.Auth0Id == null)
            {
                throw new ArgumentNullException("registrationData.Auth0Id");
            }
            if (registrationData.PlatformId == null)
            {
                throw new ArgumentNullException("registrationData.PlatformId");
            }

            try
            {
                string auth0Id     = registrationData.Auth0Id;
                var    auth0DbUser = await _dbContext.Auth0Query(auth0Id).FirstOrDefaultAsync();

                var platformId = BitcornUtils.GetPlatformId(registrationData.PlatformId);
                switch (platformId.Platform)
                {
                case "twitch":
                    var twitchUser = await TwitchKraken.GetTwitchUser(platformId.Id);

                    var twitchDbUser = await _dbContext.TwitchQuery(platformId.Id).FirstOrDefaultAsync();

                    if (twitchDbUser != null && twitchDbUser.UserIdentity.Auth0Id == null)
                    {
                        //   _dbContext.UserIdentity.Remove(auth0DbUser);
                        auth0DbUser.UserIdentity.TwitchId = twitchDbUser.UserIdentity.TwitchId;
                        CopyIdentity(auth0DbUser.UserIdentity, twitchDbUser.UserIdentity);
                        twitchDbUser.UserIdentity.TwitchUsername = twitchUser.name;
                        twitchDbUser.UserIdentity.Auth0Id        = auth0Id;
                        twitchDbUser.UserIdentity.Auth0Nickname  = auth0DbUser.UserIdentity.Auth0Nickname;

                        await MigrateUser(auth0DbUser, twitchDbUser);


                        await TxUtils.TryClaimTx(platformId, null, _dbContext);

                        return(GetFullUser(twitchDbUser));
                    }
                    else if (twitchDbUser == null && auth0DbUser != null)
                    {
                        auth0DbUser.UserIdentity.TwitchId       = platformId.Id;
                        auth0DbUser.UserIdentity.TwitchUsername = twitchUser.name;
                        await _dbContext.SaveAsync();

                        await TxUtils.TryClaimTx(platformId, null, _dbContext);

                        return(GetFullUser(auth0DbUser));
                    }
                    else if (twitchDbUser != null)
                    {
                        var e = new Exception($"A login id already exists for this twitch id {platformId.Id}");
                        await BITCORNLogger.LogError(_dbContext, e);

                        throw e;
                    }
                    else
                    {
                        var e = new Exception(
                            $"Failed to register twitch {platformId.Id} {auth0Id}");
                        await BITCORNLogger.LogError(_dbContext, e);

                        throw e;
                    }

                case "discord":
                    try
                    {
                        var discordToken = DiscordApi.GetDiscordBotToken(_configuration);
                        var discordUser  = await DiscordApi.GetDiscordUser(discordToken, platformId.Id);

                        var discordDbUser = await _dbContext.DiscordQuery(platformId.Id).FirstOrDefaultAsync();

                        if (discordDbUser != null && discordDbUser.UserIdentity.Auth0Id == null)
                        {
                            //_dbContext.UserIdentity.Remove(auth0DbUser);
                            //await _dbContext.SaveAsync();
                            auth0DbUser.UserIdentity.DiscordId = discordDbUser.UserIdentity.DiscordId;
                            CopyIdentity(auth0DbUser.UserIdentity, discordDbUser.UserIdentity);

                            discordDbUser.UserIdentity.DiscordUsername = DiscordApi.GetUsernameString(discordUser);
                            discordDbUser.UserIdentity.Auth0Id         = auth0Id;
                            discordDbUser.UserIdentity.Auth0Nickname   = auth0DbUser.UserIdentity.Auth0Nickname;
                            await MigrateUser(auth0DbUser, discordDbUser);

                            await TxUtils.TryClaimTx(platformId, null, _dbContext);

                            return(GetFullUser(discordDbUser));
                        }
                        else if (discordDbUser == null && auth0DbUser != null)
                        {
                            auth0DbUser.UserIdentity.DiscordId       = platformId.Id;
                            auth0DbUser.UserIdentity.DiscordUsername = DiscordApi.GetUsernameString(discordUser);

                            await _dbContext.SaveAsync();

                            await TxUtils.TryClaimTx(platformId, null, _dbContext);

                            return(GetFullUser(auth0DbUser));
                        }
                        else if (discordDbUser?.UserIdentity.Auth0Id != null)
                        {
                            var e = new Exception($"A login id already exists for this discord id");
                            await BITCORNLogger.LogError(_dbContext, e, $"Auth0Id already exists for user {platformId.Id}");

                            throw e;
                        }
                        else
                        {
                            var e = new Exception($"Failed to register discord");
                            await BITCORNLogger.LogError(_dbContext, e, $"Failed to register discord id for user {platformId.Id} {auth0Id}");

                            throw e;
                        }
                    }
                    catch (Exception e)
                    {
                        await BITCORNLogger.LogError(_dbContext, e);

                        throw new Exception($"Failed to add user's discord");
                    }

                    throw new Exception($"HOW THE F**K DID YOU GET HERE");

                case "twitter":
                    try
                    {
                        var twitterUser = await TwitterApi.GetTwitterUser(_configuration, platformId.Id);

                        var twitterDbUser = await _dbContext.TwitterQuery(platformId.Id).FirstOrDefaultAsync();

                        if (twitterDbUser != null && twitterDbUser.UserIdentity.Auth0Id == null)
                        {
                            auth0DbUser.UserIdentity.TwitterId = twitterDbUser.UserIdentity.TwitterId;
                            //_dbContext.UserIdentity.Remove(auth0DbUser);
                            CopyIdentity(auth0DbUser.UserIdentity, twitterDbUser.UserIdentity);
                            twitterDbUser.UserIdentity.Auth0Id         = auth0Id;
                            twitterDbUser.UserIdentity.TwitterUsername = twitterUser.Name;
                            twitterDbUser.UserIdentity.Auth0Nickname   = auth0DbUser.UserIdentity.Auth0Nickname;
                            await MigrateUser(auth0DbUser, twitterDbUser);

                            await TxUtils.TryClaimTx(platformId, null, _dbContext);

                            return(GetFullUser(twitterDbUser));
                        }
                        if (twitterDbUser == null && auth0DbUser != null)
                        {
                            auth0DbUser.UserIdentity.TwitterId       = platformId.Id;
                            auth0DbUser.UserIdentity.TwitterUsername = twitterUser.Name;
                            await _dbContext.SaveAsync();

                            await TxUtils.TryClaimTx(platformId, null, _dbContext);

                            return(GetFullUser(auth0DbUser));
                        }
                        if (twitterDbUser?.UserIdentity.Auth0Id != null)
                        {
                            var e = new Exception($"Auth0Id already exists for user {platformId.Id}");
                            await BITCORNLogger.LogError(_dbContext, e);

                            throw e;
                        }
                        var ex = new Exception($"Failed to register twitter id for user {platformId.Id} {auth0Id}");
                        await BITCORNLogger.LogError(_dbContext, ex);

                        throw ex;
                    }
                    catch (Exception e)
                    {
                        await BITCORNLogger.LogError(_dbContext, e);

                        throw e;
                    }
                    throw new Exception($"HOW THE F**K DID YOU GET HERE");

                case "reddit":
                    try
                    {
                        var redditDbUser = await _dbContext.RedditQuery(platformId.Id).FirstOrDefaultAsync();

                        if (redditDbUser != null && redditDbUser.UserIdentity.Auth0Id == null)
                        {
                            auth0DbUser.UserIdentity.RedditId = redditDbUser.UserIdentity.RedditId;
                            CopyIdentity(auth0DbUser.UserIdentity, redditDbUser.UserIdentity);
                            //_dbContext.UserIdentity.Remove(auth0DbUser);
                            redditDbUser.UserIdentity.Auth0Id       = auth0Id;
                            redditDbUser.UserIdentity.Auth0Nickname = auth0DbUser.UserIdentity.Auth0Nickname;
                            await MigrateUser(auth0DbUser, redditDbUser);

                            await TxUtils.TryClaimTx(platformId, null, _dbContext);

                            return(GetFullUser(redditDbUser));
                        }
                        else if (redditDbUser == null && auth0DbUser != null)
                        {
                            auth0DbUser.UserIdentity.RedditId = platformId.Id;
                            await _dbContext.SaveAsync();

                            await TxUtils.TryClaimTx(platformId, null, _dbContext);

                            return(GetFullUser(auth0DbUser));
                        }
                        else if (redditDbUser?.UserIdentity.Auth0Id != null)
                        {
                            var e = new Exception($"Auth0Id already exists for user {platformId.Id}");
                            await BITCORNLogger.LogError(_dbContext, e);

                            throw e;
                        }
                        else
                        {
                            var e = new Exception($"Failed to register reddit id for user {platformId.Id} {platformId.Id}");
                            await BITCORNLogger.LogError(_dbContext, e);

                            throw e;
                        }
                    }
                    catch (Exception e)
                    {
                        await BITCORNLogger.LogError(_dbContext, e);

                        throw e;
                    }

                    throw new Exception($"HOW THE F**K DID YOU GET HERE");

                default:
                    throw new Exception("Invalid platform provided in the Id");
                }
            }
            catch (Exception e)
            {
                throw new Exception($"registration failed for {registrationData}");
            }
            throw new Exception("HOW THE F**K DID YOU GET HERE");
        }
        public override async Task <PlatformSyncResponse> SyncPlatform(RegistrationData registrationData, User auth0DbUser, PlatformId platformId, string auth0Id)
        {
            try
            {
                var discordToken = DiscordApi.GetDiscordBotToken(_configuration);
                var discordUser  = await DiscordApi.GetDiscordUser(discordToken, platformId.Id);

                var discordDbUser = await _dbContext.DiscordQuery(platformId.Id).FirstOrDefaultAsync();

                var creationTime = discordUser.GetCreatedTime();
                if (discordDbUser != null && discordDbUser.UserIdentity.Auth0Id == null)
                {
                    //_dbContext.UserIdentity.Remove(auth0DbUser);
                    //await _dbContext.SaveAsync();
                    auth0DbUser.UserIdentity.DiscordId = discordDbUser.UserIdentity.DiscordId;
                    MigrateOldIdentity(auth0DbUser.UserIdentity, discordDbUser.UserIdentity);

                    discordDbUser.UserIdentity.DiscordUsername = DiscordApi.GetUsernameString(discordUser);
                    discordDbUser.UserIdentity.Auth0Id         = auth0Id;
                    discordDbUser.UserIdentity.Auth0Nickname   = auth0DbUser.UserIdentity.Auth0Nickname;
                    await MigrateOldProfile(auth0DbUser, discordDbUser);

                    return(GetSyncOutput(creationTime, discordDbUser, true));
                }
                else if (discordDbUser == null && auth0DbUser != null)
                {
                    auth0DbUser.UserIdentity.DiscordId       = platformId.Id;
                    auth0DbUser.UserIdentity.DiscordUsername = DiscordApi.GetUsernameString(discordUser);

                    await _dbContext.SaveAsync();

                    return(GetSyncOutput(creationTime, auth0DbUser, false));
                }
                else if (discordDbUser?.UserIdentity.Auth0Id != null)
                {
                    var obj = GetSyncOutput(creationTime, discordDbUser, false);
                    obj.ProfileAlreadySynced = true;
                    return(obj);

                    /*
                     * var e = new Exception($"A login id already exists for this discord id");
                     * await BITCORNLogger.LogError(_dbContext, e, $"Auth0Id already exists for user {platformId.Id}");
                     * throw e;*/
                }
                else
                {
                    var e = new Exception($"Failed to register discord");
                    await BITCORNLogger.LogError(_dbContext, e, $"Failed to register discord id for user {platformId.Id} {auth0Id}");

                    throw e;
                }
            }
            catch (Exception e)
            {
                await BITCORNLogger.LogError(_dbContext, e, JsonConvert.SerializeObject(registrationData));

                throw new Exception($"Failed to add user's discord");
            }

            throw new Exception($"HOW THE F**K DID YOU GET HERE");
        }