mirror of
https://github.com/NoiTheCat/WorldTime.git
synced 2024-11-24 01:14:13 +00:00
Merge pull request #10 from NoiTheCat/removelastseen
Remove 'last seen' filtering in list display, closes #7
This commit is contained in:
commit
4db6eb71e7
3 changed files with 4 additions and 36 deletions
22
Database.cs
22
Database.cs
|
@ -8,7 +8,6 @@ namespace WorldTime;
|
|||
/// </summary>
|
||||
public class Database {
|
||||
private const string UserDatabase = "userdata";
|
||||
private const string CutoffInterval = "INTERVAL '30 days'"; // TODO make configurable?
|
||||
|
||||
private readonly string _connectionString;
|
||||
|
||||
|
@ -49,7 +48,6 @@ public class Database {
|
|||
SELECT true FROM {UserDatabase}
|
||||
WHERE
|
||||
guild_id = @Gid
|
||||
AND last_active >= now() - {CutoffInterval}
|
||||
LIMIT 1
|
||||
";
|
||||
c.Parameters.Add("@Gid", NpgsqlDbType.Bigint).Value = (long)guild.Id;
|
||||
|
@ -68,23 +66,6 @@ LIMIT 1
|
|||
return (int)((long?)await c.ExecuteScalarAsync() ?? -1); // ExecuteScalarAsync returns a long here
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the last activity field for the specified guild user, if existing in the database.
|
||||
/// </summary>
|
||||
/// <returns>True if a value was updated, implying that the specified user exists in the database.</returns>
|
||||
internal async Task<bool> UpdateLastActivityAsync(SocketGuildUser user) {
|
||||
using var db = await OpenConnectionAsync().ConfigureAwait(false);
|
||||
using var c = db.CreateCommand();
|
||||
c.CommandText = $"UPDATE {UserDatabase} SET last_active = now() " +
|
||||
"WHERE guild_id = @Gid AND user_id = @Uid";
|
||||
c.Parameters.Add("@Gid", NpgsqlDbType.Bigint).Value = (long)user.Guild.Id;
|
||||
c.Parameters.Add("@Uid", NpgsqlDbType.Bigint).Value = (long)user.Id;
|
||||
await c.PrepareAsync().ConfigureAwait(false);
|
||||
return await c.ExecuteNonQueryAsync().ConfigureAwait(false) > 0;
|
||||
}
|
||||
|
||||
// TODO remove data from users with very distant last activity. how long ago?
|
||||
|
||||
/// <summary>
|
||||
/// Removes the specified user from the database.
|
||||
/// </summary>
|
||||
|
@ -132,7 +113,7 @@ LIMIT 1
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all known user time zones for the given guild. Filtered only by last-seen time.
|
||||
/// Retrieves all known user time zones for the given guild.
|
||||
/// Further filtering should be handled by the consumer.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
|
@ -145,7 +126,6 @@ LIMIT 1
|
|||
SELECT zone, user_id FROM {UserDatabase}
|
||||
WHERE
|
||||
guild_id = @Gid
|
||||
AND last_active >= now() - {CutoffInterval}
|
||||
ORDER BY RANDOM() -- Randomize results for display purposes";
|
||||
c.Parameters.Add("@Gid", NpgsqlDbType.Bigint).Value = (long)guildId;
|
||||
await c.PrepareAsync().ConfigureAwait(false);
|
||||
|
|
16
WorldTime.cs
16
WorldTime.cs
|
@ -187,24 +187,12 @@ internal class WorldTime : IDisposable {
|
|||
if (message.Type != MessageType.Default) return;
|
||||
if (message.Channel is not SocketTextChannel channel) return;
|
||||
|
||||
/*
|
||||
* From https://support-dev.discord.com/hc/en-us/articles/4404772028055:
|
||||
* "You will still receive the events and can call the same APIs, and you'll get other data about a message like
|
||||
* author and timestamp. To put it simply, you'll be able to know all the information about when someone sends a
|
||||
* message; you just won't know what they said."
|
||||
*
|
||||
* Assuming this stays true, it will be possible to maintain legacy behavior after this bot loses full access to incoming messages.
|
||||
*/
|
||||
// Attempt to update user's last_seen column
|
||||
// POTENTIAL BUG: If user does a list command, the list may be processed before their own time's refreshed, and they may be skipped.
|
||||
var hasMemberHint = await Database.UpdateLastActivityAsync((SocketGuildUser)message.Author).ConfigureAwait(false);
|
||||
|
||||
// Proactively fill guild user cache if the bot has any data for the respective guild
|
||||
// Can skip an extra query if the last_seen update is known to have been successful, otherwise query for any users
|
||||
var guild = channel.Guild;
|
||||
if (!guild.HasAllMembers && (hasMemberHint || await Database.HasAnyAsync(guild).ConfigureAwait(false))) {
|
||||
if (!guild.HasAllMembers && await Database.HasAnyAsync(guild)) {
|
||||
// Event handler hangs if awaited normally or used with Task.Run
|
||||
await Task.Factory.StartNew(guild.DownloadUsersAsync).ConfigureAwait(false);
|
||||
await Task.Factory.StartNew(guild.DownloadUsersAsync);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Version>2.1.1</Version>
|
||||
<Version>2.1.2</Version>
|
||||
<Authors>NoiTheCat</Authors>
|
||||
</PropertyGroup>
|
||||
|
||||
|
|
Loading…
Reference in a new issue