Пример #1
0
        public virtual async Task OnSyncSuccess(DateTime?socialAccountCreationDate, PlatformId platformId)
        {
            await TxUtils.TryClaimTx(platformId, null, _dbContext);

            var key = $"{platformId.Platform}|{platformId.Id}";

            if (!(await _dbContext.SocialIdentity.AnyAsync(s => s.PlatformId == key)))
            {
                if (socialAccountCreationDate != null && DateTime.Now > socialAccountCreationDate.Value.AddDays(7))
                {
                    await ReferralUtils.UpdateReferralSync(_dbContext, platformId);
                }

                _dbContext.SocialIdentity.Add(new SocialIdentity()
                {
                    PlatformId = key,
                    Timestamp  = DateTime.Now
                });
                await _dbContext.SaveAsync();
            }
        }
Пример #2
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 async Task TestTipCornToUnregistered()
        {
            var dbContext = TestUtils.CreateDatabase();

            try
            {
                var startFromUser = dbContext.TwitchQuery(_configuration["Config:TestFromUserId"]).FirstOrDefault().UserWallet.Balance;
                var txController  = new TxController(dbContext);

                var request = new TipRequest();
                request.Columns = new string[] { };
                string toTwitchId = "123123";
                request.To       = "twitch|" + toTwitchId;
                request.From     = "twitch|" + _configuration["Config:TestFromUserId"];
                request.Platform = "twitch";
                request.Amount   = 100;

                var toUser = await dbContext.TwitchAsync(toTwitchId);

                dbContext.RemoveRange(dbContext.UnclaimedTx);
                TestUtils.RemoveUserIfExists(dbContext, toUser);

                var response = await txController.Tipcorn(request);

                var receipt = response.Value[0];
                Assert.Null(receipt.Tx);
                Assert.NotNull(receipt.From);
                Assert.Null(receipt.To);

                using (var dbContext2 = TestUtils.CreateDatabase())
                {
                    var endFromUser = dbContext2.TwitchQuery(_configuration["Config:TestFromUserId"]).FirstOrDefault().UserWallet.Balance;
                    Assert.Equal(startFromUser - request.Amount, endFromUser);

                    dbContext2.AddUser(new UserIdentity()
                    {
                        Auth0Id       = "temp",
                        Auth0Nickname = "temp",
                        TwitchId      = toTwitchId
                    }, new UserWallet());
                    var to = await dbContext2.TwitchAsync(toTwitchId);

                    await TxUtils.TryClaimTx(new PlatformId()
                    {
                        Platform = "twitch", Id = toTwitchId
                    }, to, dbContext2);
                }
                using (var dbContext2 = TestUtils.CreateDatabase())
                {
                    var to = await dbContext2.TwitchAsync(toTwitchId);

                    Assert.Equal(request.Amount, to.UserWallet.Balance);
                    var claim2 = await TxUtils.TryClaimTx(new PlatformId()
                    {
                        Platform = "twitch", Id = toTwitchId
                    }, to, dbContext2);;
                    Assert.Equal(0, claim2);
                }
            }
            finally
            {
                dbContext.Dispose();
            }
        }