Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reorder db interactions and add types to starboard.py #123

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

andrewj-brown
Copy link
Member

@peclarke prompted a galaxy-brain idea - by reordering the DB interactions (and adding a fake-blacklist-entry to the DB, temporarily), we should be able to fix the starboard doubleposting.

This is a "oh shit cool idea" PR, and as such isn't tested and I haven't thought about side effects. I'm just getting a quick thing out there so I don't forget in the middle of exam fog!

Potential problems/other notes:

  • what does the second 'racer' see? Probably a blacklisted message, which means it'll fail to count that react until another update happens. Alternatively, it might just error-out when it attempts to add?
  • what happens now if the posting fails? The db's record of a blacklisted post will still be there, which will need manual cleanup (ew)
  • now that _add just creates a no-message-entry, it could be moved further up and used by the blacklist commands

@andrewj-brown
Copy link
Member Author

Also, added a fallback that watches for message deletions in the starboard channel, and adds blacklist entries for them.

@andrewj-brown andrewj-brown marked this pull request as ready for review July 1, 2023 03:41
Copy link
Member

@49Indium 49Indium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still uncertain of a few things, so take all comments with a pinch of salt, I think it would be really nice to have the wiki, as there are a few terms I keep encountering that would be nice to define ("starboard update" in particular). It would also be nice to have a description of the of the starboard database and what None means for certain entries.

The typing helps quite a bit for understanding the overall program structure, so a win for Pyright. I haven't full gotten my head around the overall flow of adding starboarded messages, but from what I understand, the _starboard_db_add and _starboard_db_finish methods seem to tighten the race issue, though two almost simultaneous calls to on_raw_reaction_add could still cause _process_sb_updates to run twice. Also the fallback for deleted messages is great.

I'm also going to piggyback on this review and ask a list of questions about starboard that weren't changed by this pull request, but would help me understand the code:

  • Why are runtime errors used and what would be the symptoms if one of them occurred?
  • Why is there time.sleep(5) in cleanup_starboard (in the latest version, line 130)?

class StarboardMsgPair(object):
def __init__(
self,
recv: Union[discord.Message, None],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I think Optional[discord.Message] is a little bit more clear, but I'm also happy as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Isaac here.

self,
recv: Union[discord.Message, None],
sent: Union[discord.Message, None],
recv_channel: Union[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I find that Optional[discord.abc.GuildChannel | discord.Thread | discord.abc.PrivateChannel] is more readable, but again, not too fussed.

class BlacklistedMessageError(Exception):
# always caught. used to differentiate "starboard message doesn't exist" and "starboard message is blacklisted"
pass
class StarboardMsgPair(object):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to have a docstring to explain the difference between recv and sent. It took me some time to understand what they meant.

)
else:
if message_id == 1076779482637144105:
# This is Isaac's initial-starboard message. I know, IDs are bad. BUT
# consider that this doesn't cause any of the usual ID-related issues
# like breaking lookups in other servers.
return
return StarboardMsgPair(None, None, None, True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using keywords for the arguments here would be useful. Similarly when this pattern reoccurs.

self, payload: discord.RawMessageDeleteEvent
) -> None:
"""
Fallback that blacklists messages whenever a starboard message is deleted. See documentation for context blacklist commands.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a great idea


def dangerous_recv(self) -> discord.Message:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't understand the dangerous commands on first run through; a doc string would be nice but not necessary.

db_session.close()

def _starboard_db_finish(self, recv: int, recv_location: int, sent: int) -> None:
"""Finalises a starboard DB entry. Finishes the job that _starboard_db_add starts - it adds the sent-message-id."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring was very helpful

@@ -232,13 +275,37 @@ def _rm_big_ratelimit(self, id: int) -> None:
"""Callback to remove a message from the big-ratelimited list"""
self.big_blocked_messages.remove(id)

def _starboard_db_add(self, recv: int, recv_location: int, sent: int) -> None:
def _starboard_db_add(self, recv: int, recv_location: int) -> None:
"""Creates a starboard DB entry. Only called from _process_updates when the messages are not None, so doesn't
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially mention that _starboard_db_finish should be run at some point after this method.

@49Indium 49Indium added the bug Something isn't working label Jul 2, 2023
@JamesDearlove JamesDearlove changed the title reorder db interactions in starboard.py reorder db interactions and add types to starboard.py Jul 2, 2023
Copy link
Member

@JamesDearlove JamesDearlove left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No major issues from me

class StarboardMsgPair(object):
def __init__(
self,
recv: Union[discord.Message, None],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Isaac here.

@andrewj-brown andrewj-brown mentioned this pull request Aug 27, 2023
8 tasks
@andrewj-brown andrewj-brown linked an issue Oct 9, 2023 that may be closed by this pull request
8 tasks
@49Indium 49Indium mentioned this pull request Mar 7, 2024
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Starboard Mega-Issue
3 participants