Merge pull request #10 from NoiTheCat/removelastseen

Remove 'last seen' filtering in list display, closes #7
This commit is contained in:
Noi 2022-06-28 21:22:30 -07:00 committed by GitHub
commit 4db6eb71e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 36 deletions

View file

@ -8,7 +8,6 @@ namespace WorldTime;
/// </summary> /// </summary>
public class Database { public class Database {
private const string UserDatabase = "userdata"; private const string UserDatabase = "userdata";
private const string CutoffInterval = "INTERVAL '30 days'"; // TODO make configurable?
private readonly string _connectionString; private readonly string _connectionString;
@ -49,7 +48,6 @@ public class Database {
SELECT true FROM {UserDatabase} SELECT true FROM {UserDatabase}
WHERE WHERE
guild_id = @Gid guild_id = @Gid
AND last_active >= now() - {CutoffInterval}
LIMIT 1 LIMIT 1
"; ";
c.Parameters.Add("@Gid", NpgsqlDbType.Bigint).Value = (long)guild.Id; 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 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> /// <summary>
/// Removes the specified user from the database. /// Removes the specified user from the database.
/// </summary> /// </summary>
@ -132,7 +113,7 @@ LIMIT 1
} }
/// <summary> /// <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. /// Further filtering should be handled by the consumer.
/// </summary> /// </summary>
/// <returns> /// <returns>
@ -145,7 +126,6 @@ LIMIT 1
SELECT zone, user_id FROM {UserDatabase} SELECT zone, user_id FROM {UserDatabase}
WHERE WHERE
guild_id = @Gid guild_id = @Gid
AND last_active >= now() - {CutoffInterval}
ORDER BY RANDOM() -- Randomize results for display purposes"; ORDER BY RANDOM() -- Randomize results for display purposes";
c.Parameters.Add("@Gid", NpgsqlDbType.Bigint).Value = (long)guildId; c.Parameters.Add("@Gid", NpgsqlDbType.Bigint).Value = (long)guildId;
await c.PrepareAsync().ConfigureAwait(false); await c.PrepareAsync().ConfigureAwait(false);

View file

@ -187,24 +187,12 @@ internal class WorldTime : IDisposable {
if (message.Type != MessageType.Default) return; if (message.Type != MessageType.Default) return;
if (message.Channel is not SocketTextChannel channel) 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 // 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 // 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; 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 // 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);
} }
} }

View file

@ -5,7 +5,7 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Version>2.1.1</Version> <Version>2.1.2</Version>
<Authors>NoiTheCat</Authors> <Authors>NoiTheCat</Authors>
</PropertyGroup> </PropertyGroup>