* Use the most recent timestamp from sent digest messages
The original logic here had queried for up to 10 (unordered) messages
from the database for this user, perhaps leveraging the knowledge that
we purge old messages after 90 days to ensure 10 was enough.
Since the last message in the limited and unordered list could have
had any sent_at time in the past 90 days, it's possible users could
pass the "should send email" check multiple times in a day (and get
multiple digest emails, I'm not certain when the rake task runs but it
could be as frequently as once per deployment?)
Since we're only using this list of messages to find the most recently
sent message, select the maximum sent time.
* convert periodic_email_digest setting to days
Time.current - last_email_sent_at gives an integer count of elapsed
seconds
Settings::General.periodic_email_digest is an integer count of days
between digests.
Convert to days (so we're only sending digests when enough time has
elapsed) instead of defaulting to once every 2 seconds (or at least
several times per day).
* Clean up comparison
Rather than subtracting the current time from the last time, and
checking that the difference in time is greater than the periodic
setting, use days.ago to get the timestamp far enough in the past, and
ensure last email was sent before that ( `sent_at < n.days.ago` ).
This seems like it reads clearer than the original implementation.
* Prefer Time.before? to numeric comparison
Change the method name from last_email_sent_at to last_email_sent, so
that the comparison reads like English (the other callers use it as a
number)
And since the code reveals its intention clearly, there's no need for
an inline comment about the next line any more.