Skip to content

Add cj pin and cj unpin#56

Merged
ChrisLovering merged 12 commits into
mainfrom
cj-pin
Jul 21, 2022
Merged

Add cj pin and cj unpin#56
ChrisLovering merged 12 commits into
mainfrom
cj-pin

Conversation

@D0rs4n
Copy link
Copy Markdown
Contributor

@D0rs4n D0rs4n commented Jul 18, 2022

Lets Code Jam participants pin messages in their own team channels. Check done via the Code Jam MGMT api.
Closes #53

D0rs4n added 2 commits July 19, 2022 00:08
- Add `cj pin` command which lets Code Jam participants pin messages in their team channel. The ID check is done with the help of the Code Jam Management API.
Comment thread bot/exts/code_jams/_cog.py Outdated
Copy link
Copy Markdown
Member

@ChrisLovering ChrisLovering left a comment

Choose a reason for hiding this comment

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

In my opinion, if this command is ran outside of the code jam team category, it should silently fail. Sir-Robin has no reason to respond to this command outside of that context.

If it is inside the code jam category, then you can assume the user running the command has permission to pin the message, as no other users will be in those channels.

If we really wanted to be safe, we can still check that the user invoking the command is a participant, but it's not 100% needed with the above solution.

@D0rs4n
Copy link
Copy Markdown
Contributor Author

D0rs4n commented Jul 19, 2022

In my opinion, if this command is ran outside of the code jam team category, it should silently fail. Sir-Robin has no reason to respond to this command outside of that context.

If it is inside the code jam category, then you can assume the user running the command has permission to pin the message, as no other users will be in those channels.

If we really wanted to be safe, we can still check that the user invoking the command is a participant, but it's not 100% needed with the above solution.

As for you first suggestion I agree, it should silently fail. However, I don't think we should rely on categories, those categories are created as we go, hence we don't add them as environment variables, so we would have to rely to their names, which feels like something that could easily break. (I think we discussed a similar issue when creating those categories). That's why I wanted to stick with the API.

brad90four
brad90four previously approved these changes Jul 19, 2022
Copy link
Copy Markdown
Contributor

@brad90four brad90four left a comment

Choose a reason for hiding this comment

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

LGTM content-wise, have not tested locally

@ChrisLovering
Copy link
Copy Markdown
Member

In my opinion, if this command is ran outside of the code jam team category, it should silently fail. Sir-Robin has no reason to respond to this command outside of that context.
If it is inside the code jam category, then you can assume the user running the command has permission to pin the message, as no other users will be in those channels.
If we really wanted to be safe, we can still check that the user invoking the command is a participant, but it's not 100% needed with the above solution.

As for you first suggestion I agree, it should silently fail. However, I don't think we should rely on categories, those categories are created as we go, hence we don't add them as environment variables, so we would have to rely to their names, which feels like something that could easily break. (I think we discussed a similar issue when creating those categories). That's why I wanted to stick with the API.

Using the API seems fine to me.

However, we are still giving error messages to users in other channels. EG if the user isn't a participant or if they didn't reply to a message.

IMO sir-robin should never respond to this command if it is outside of a codejam channel.

Also, on the code side of things, if you have a huge if statement, and then a tiny else statement, it is usually best to do the else statement first and return early.

EG for this it would be

if not isinstance(referenced_message, discord.Message):
    ctx.send( ... )
    return

...
the rest of the code
...

The check will silently fail, when invoked outside of one of the Code Jam Categories, since it raises a `CodeJamCategoryCheckFailure`, which is just logged, but does not send a response.
As of now, that check was applied on the pin and unpin command.
@D0rs4n
Copy link
Copy Markdown
Contributor Author

D0rs4n commented Jul 19, 2022

In my opinion, if this command is ran outside of the code jam team category, it should silently fail. Sir-Robin has no reason to respond to this command outside of that context.
If it is inside the code jam category, then you can assume the user running the command has permission to pin the message, as no other users will be in those channels.
If we really wanted to be safe, we can still check that the user invoking the command is a participant, but it's not 100% needed with the above solution.

As for you first suggestion I agree, it should silently fail. However, I don't think we should rely on categories, those categories are created as we go, hence we don't add them as environment variables, so we would have to rely to their names, which feels like something that could easily break. (I think we discussed a similar issue when creating those categories). That's why I wanted to stick with the API.

Using the API seems fine to me.

However, we are still giving error messages to users in other channels. EG if the user isn't a participant or if they didn't reply to a message.

IMO sir-robin should never respond to this command if it is outside of a codejam channel.

Also, on the code side of things, if you have a huge if statement, and then a tiny else statement, it is usually best to do the else statement first and return early.

EG for this it would be

if not isinstance(referenced_message, discord.Message):
    ctx.send( ... )
    return

...
the rest of the code
...

Resolved in db082ac

@D0rs4n D0rs4n requested a review from ChrisLovering July 19, 2022 13:28
@D0rs4n D0rs4n added High Priority For time-sensitive and otherwise urgent items Ready for Reviews Feature is ready for reviews Area: Code Jam labels Jul 19, 2022
@D0rs4n D0rs4n dismissed brad90four’s stale review July 19, 2022 16:50

Significant changes have been made

@D0rs4n D0rs4n requested review from brad90four and ichard26 July 19, 2022 16:51
Copy link
Copy Markdown
Contributor

@janine9vn janine9vn left a comment

Choose a reason for hiding this comment

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

Looks good! I have a request/comment about allowing pin/unpins from message links. I can be swayed either way though.

Also a small note about reverting an earlier decision about which role to lock commands to.

Comment thread bot/exts/code_jams/_cog.py Outdated
Comment thread bot/exts/code_jams/_cog.py Outdated
Comment thread bot/exts/code_jams/_cog.py Outdated
janine9vn
janine9vn previously approved these changes Jul 20, 2022
Copy link
Copy Markdown
Contributor

@janine9vn janine9vn left a comment

Choose a reason for hiding this comment

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

Very nice!

Copy link
Copy Markdown

@Kronifer Kronifer left a comment

Choose a reason for hiding this comment

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

looks good

brad90four
brad90four previously approved these changes Jul 20, 2022
Copy link
Copy Markdown
Contributor

@brad90four brad90four left a comment

Choose a reason for hiding this comment

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

Looks good, just the one thing on the conditional error message (I don't think it should have a dangling comma)

Comment thread bot/exts/code_jams/_creation_utils.py Outdated
@D0rs4n
Copy link
Copy Markdown
Contributor Author

D0rs4n commented Jul 20, 2022

These two commands have an authorization issue and maaaybe another one too:

1. The Events Lead cannot use `&cj pin` or `&cj unpin` (poor Kat)

2. Any helper part of the Events Team can abuse `&cj pin` and `&cj unpin` to (un)pin any messages in channels Robin has permission to do so. I have no idea what permissions Robin will be given in the actual server during normal operations, but let's verify this won't be an issue (if not block this route entirely).

1.:The Events Lead can use the command, since it's enabled for the whole Events Team. (which includes the Events Lead)
2.: No the staff cannot abuse this, since it's limited to the Code Jam categories, hence the category, if invoked outside of the Code Jam categories, it'll silently fail.

Copy link
Copy Markdown
Contributor

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

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

Re 1. right makes sense. Sorry about that. Re 2. I'm talking about a staff member who is on the Events Team invoking &cj pin in a CJ team channel to pin a message outside of the CJ categories. A bit of an edge case I'll admit, but I thought it was worth mentioning.

I'll dismiss my review so a merge is still possible since 2 is definitely an edge case.

@ichard26 ichard26 dismissed their stale review July 20, 2022 20:52

I am an idiot re. 1

@D0rs4n D0rs4n added the Stalled Feature is stalled due to external reasons label Jul 20, 2022
@D0rs4n
Copy link
Copy Markdown
Contributor Author

D0rs4n commented Jul 20, 2022

2 is valid, Stalling the PR while figuring it out.

@ichard26 ichard26 added review: do not merge Prevent pull requests from being merged and removed Stalled Feature is stalled due to external reasons labels Jul 20, 2022
This commit introduces a check which checks whether the message is located inside the Code Jam category.
Additionally if the message is inside the Code Jam category, but is not the participant's team's channel, the permission to pin/unpin that message will be denied.
@D0rs4n D0rs4n removed the review: do not merge Prevent pull requests from being merged label Jul 20, 2022
@D0rs4n D0rs4n requested review from brad90four and janine9vn July 20, 2022 21:38
@D0rs4n D0rs4n dismissed stale reviews from brad90four and janine9vn July 20, 2022 21:38

Significant changes have been made.

@D0rs4n D0rs4n requested a review from ichard26 July 20, 2022 21:39
Copy link
Copy Markdown
Contributor

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

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

I'm sorry, but some more changes are needed.

Technically the Events Team and Admins can (un)pin a message from a CJ team channel that IS NOT the channel the command was invoked in, but honestly I don't think this matters much and isn't a permission bypass (just confusing). So I suggest ignoring this unless someone feels very strongly. I'm kind of scared of making any more changes than necessary 😅

Comment thread bot/exts/code_jams/_cog.py Outdated
Comment thread bot/exts/code_jams/_cog.py Outdated
Comment thread bot/exts/code_jams/_cog.py Outdated
Comment thread bot/exts/code_jams/_cog.py Outdated
Comment thread bot/exts/code_jams/_cog.py Outdated
Comment thread bot/exts/code_jams/_cog.py Outdated
Copy link
Copy Markdown
Member

@ChrisLovering ChrisLovering left a comment

Choose a reason for hiding this comment

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

Both the pin and unpin command have a lot of duplicated code. IMO this tends to mean that it should be abstracted to a helper func, where the two commands just call this function with pin or unpin and the helper func does all this work in a single place. (see the help channel implementation.)

There also seems to be a very common pattern in sir-robin where we get a team, do some API error handling and then do something with the team.

IMO this fetching of the team and error handling should be moved to a helper function, to further remove this code duplication.
Extra points if you return the team in a data class, so we're not doing dict slicing everywhere.

Comment thread bot/utils/checks.py Outdated
Comment thread bot/utils/checks.py Outdated
Comment thread bot/exts/code_jams/_creation_utils.py Outdated
To avoid code duplication an additional flow was introduced, the `pin_flow`.

Additionally, the `message_is_in_code_jam_category` was deleted, and an extra check was integrated into the `pin_flow` to avoid permission problems, by checking that the referenced message's channel is the same as the context's channel.
@D0rs4n D0rs4n requested review from ChrisLovering and ichard26 July 21, 2022 01:04
@D0rs4n
Copy link
Copy Markdown
Contributor Author

D0rs4n commented Jul 21, 2022

Resolved all. 🤞
Except for the helper to handle team related MGMT requests, that'll be included in a separate PR.

Copy link
Copy Markdown
Contributor

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

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

Wheee this looks so much nicer. Can confirm it works and has no permission issues. Great work @D0rs4n, and thank you @ChrisLovering for bringing some sensibility back into this mess :)

Comment thread bot/exts/code_jams/_flows.py Outdated
@D0rs4n
Copy link
Copy Markdown
Contributor Author

D0rs4n commented Jul 21, 2022

Wheee this looks so much nicer. Can confirm it works and has no permission issues. Great work @D0rs4n, and thank you @ChrisLovering for bringing some sensibility back into this mess :)

Thank you for your thorough review! :)

@ChrisLovering ChrisLovering merged commit ddc059d into main Jul 21, 2022
@ChrisLovering ChrisLovering deleted the cj-pin branch July 21, 2022 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Code Jam High Priority For time-sensitive and otherwise urgent items Ready for Reviews Feature is ready for reviews

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add &cj pin command for participants to use

6 participants