2017-09-30 01:38:10 +02:00
|
|
|
|
using System;
|
2017-10-26 23:31:51 +02:00
|
|
|
|
using System.Collections.Generic;
|
2018-05-10 02:00:26 +02:00
|
|
|
|
using System.Linq;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord.WebSocket;
|
2018-05-10 02:00:26 +02:00
|
|
|
|
using Geekbot.net.Database;
|
|
|
|
|
using Geekbot.net.Database.Models;
|
|
|
|
|
using Geekbot.net.Lib.Extensions;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.Logger;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
namespace Geekbot.net.Lib.UserRepository
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
|
|
|
|
public class UserRepository : IUserRepository
|
|
|
|
|
{
|
2018-05-10 02:00:26 +02:00
|
|
|
|
private readonly DatabaseContext _database;
|
2018-01-20 01:38:49 +01:00
|
|
|
|
private readonly IGeekbotLogger _logger;
|
2018-05-10 02:00:26 +02:00
|
|
|
|
public UserRepository(DatabaseContext database, IGeekbotLogger logger)
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
2018-05-10 02:00:26 +02:00
|
|
|
|
_database = database;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 02:00:26 +02:00
|
|
|
|
public async Task<bool> Update(SocketUser user)
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-10-26 23:31:51 +02:00
|
|
|
|
var savedUser = Get(user.Id);
|
2018-05-10 02:00:26 +02:00
|
|
|
|
var isNew = false;
|
|
|
|
|
if (savedUser == null)
|
|
|
|
|
{
|
|
|
|
|
savedUser = new UserModel();
|
|
|
|
|
isNew = true;
|
|
|
|
|
}
|
|
|
|
|
savedUser.UserId = user.Id.AsLong();
|
2017-10-26 23:31:51 +02:00
|
|
|
|
savedUser.Username = user.Username;
|
|
|
|
|
savedUser.Discriminator = user.Discriminator;
|
2018-05-10 02:00:26 +02:00
|
|
|
|
savedUser.AvatarUrl = user.GetAvatarUrl() ?? "";
|
2017-10-26 23:31:51 +02:00
|
|
|
|
savedUser.IsBot = user.IsBot;
|
|
|
|
|
savedUser.Joined = user.CreatedAt;
|
2018-05-10 02:00:26 +02:00
|
|
|
|
if (savedUser.UsedNames == null) savedUser.UsedNames = Enumerable.Empty<string>().ToArray();
|
2017-10-26 23:31:51 +02:00
|
|
|
|
if (!savedUser.UsedNames.Contains(user.Username))
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
2018-05-10 02:00:26 +02:00
|
|
|
|
savedUser.UsedNames = savedUser.UsedNames.Concat(new[] {user.Username}).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
{
|
|
|
|
|
await _database.Users.AddAsync(savedUser);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_database.Users.Update(savedUser);
|
2017-10-26 23:31:51 +02:00
|
|
|
|
}
|
2018-05-10 02:00:26 +02:00
|
|
|
|
|
|
|
|
|
await _database.SaveChangesAsync();
|
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.UserRepository, "Updated User", savedUser);
|
2018-05-10 02:00:26 +02:00
|
|
|
|
await Task.Delay(500);
|
|
|
|
|
return true;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Warning(LogSource.UserRepository, $"Failed to update user: {user.Username}#{user.Discriminator} ({user.Id})", e);
|
2018-05-10 02:00:26 +02:00
|
|
|
|
return false;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 02:00:26 +02:00
|
|
|
|
public UserModel Get(ulong userId)
|
2017-10-26 23:31:51 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
2017-09-30 01:38:10 +02:00
|
|
|
|
{
|
2018-05-10 02:00:26 +02:00
|
|
|
|
return _database.Users.FirstOrDefault(u => u.UserId.Equals(userId.AsLong()));
|
2017-10-26 23:31:51 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Warning(LogSource.UserRepository, $"Failed to get {userId} from repository", e);
|
2018-05-10 02:00:26 +02:00
|
|
|
|
return null;
|
2017-09-30 01:38:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|