-
Notifications
You must be signed in to change notification settings - Fork 717
feat: Add bot API endpoints for reading messages and adding reactions #190
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
Open
jpshackelford
wants to merge
6
commits into
basecamp:main
Choose a base branch
from
jpshackelford:feature/bot-read-messages
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d4a5678
feat: Add bot API endpoint for reading room messages
jpshackelford 5340f3d
fix: Ensure bot can only read messages from rooms it is a member of
jpshackelford 057d565
fix: Align create and index to both return 404 for non-member rooms
jpshackelford c82a8d3
fix: Use correct room messages in pagination tests
jpshackelford e8c1349
style: Use bot? predicate instead of role comparison
jpshackelford 20ab5b1
feat: Add bot API endpoint for adding reactions (boosts) to messages
jpshackelford File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| class Messages::Boosts::ByBotsController < ApplicationController | ||
| allow_bot_access only: :create | ||
|
|
||
| def create | ||
| set_message | ||
| @boost = @message.boosts.create!(content: read_body) | ||
|
|
||
| broadcast_create | ||
| head :created | ||
| rescue ActiveRecord::RecordNotFound | ||
| head :not_found | ||
| end | ||
|
|
||
| private | ||
| def set_message | ||
| @room = Current.user.rooms.find(params[:room_id]) | ||
| @message = @room.messages.find(params[:message_id]) | ||
| end | ||
|
|
||
| def read_body | ||
| request.body.rewind | ||
| request.body.read.force_encoding("UTF-8") | ||
| ensure | ||
| request.body.rewind | ||
| end | ||
|
|
||
| def broadcast_create | ||
| @boost.broadcast_append_to @boost.message.room, :messages, | ||
| target: "boosts_message_#{@boost.message.client_message_id}", | ||
| partial: "messages/boosts/boost", | ||
| attributes: { maintain_scroll: true } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
test/controllers/messages/boosts/by_bots_controller_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| require "test_helper" | ||
|
|
||
| class Messages::Boosts::ByBotsControllerTest < ActionDispatch::IntegrationTest | ||
| setup do | ||
| @room = rooms(:watercooler) | ||
| @message = messages(:fourth) # Message in watercooler room where bender bot is a member | ||
| @bot = users(:bender) | ||
| end | ||
|
|
||
| test "create adds a boost to the message" do | ||
| assert_difference -> { @message.boosts.count }, +1 do | ||
| post room_bot_message_boosts_url(@room, @bot.bot_key, @message), params: +"👀" | ||
| assert_response :created | ||
| end | ||
|
|
||
| assert_equal "👀", @message.boosts.last.content | ||
| end | ||
|
|
||
| test "create with emoji reaction" do | ||
| assert_difference -> { Boost.count }, +1 do | ||
| post room_bot_message_boosts_url(@room, @bot.bot_key, @message), params: +"🎉" | ||
| assert_response :created | ||
| end | ||
| end | ||
|
|
||
| test "create with text reaction" do | ||
| assert_difference -> { Boost.count }, +1 do | ||
| post room_bot_message_boosts_url(@room, @bot.bot_key, @message), params: +"Nice!" | ||
| assert_response :created | ||
| end | ||
|
|
||
| assert_equal "Nice!", @message.boosts.last.content | ||
| end | ||
|
|
||
| test "create broadcasts the boost" do | ||
| assert_turbo_stream_broadcasts [ @message.room, :messages ], count: 1 do | ||
| post room_bot_message_boosts_url(@room, @bot.bot_key, @message), params: +"👍" | ||
| end | ||
| end | ||
|
|
||
| test "create requires valid bot key" do | ||
| assert_no_difference -> { Boost.count } do | ||
| post room_bot_message_boosts_url(@room, "invalid-bot-key", @message), params: +"👀" | ||
| end | ||
| assert_response :redirect # Redirects to login | ||
| end | ||
|
|
||
| test "create returns not_found for room bot is not a member of" do | ||
| room_without_bot = rooms(:designers) | ||
| message_in_other_room = messages(:first) # Message in designers room | ||
|
|
||
| assert_no_difference -> { Boost.count } do | ||
| post room_bot_message_boosts_url(room_without_bot, @bot.bot_key, message_in_other_room), params: +"👀" | ||
| end | ||
| assert_response :not_found | ||
| end | ||
|
|
||
| test "create returns not_found for message not in the room" do | ||
| message_in_other_room = messages(:first) # Message in designers room, not watercooler | ||
|
|
||
| assert_no_difference -> { Boost.count } do | ||
| post room_bot_message_boosts_url(@room, @bot.bot_key, message_in_other_room), params: +"👀" | ||
| end | ||
| assert_response :not_found | ||
| end | ||
|
|
||
| test "create can't be abused to post boosts as regular user" do | ||
| user = users(:kevin) | ||
| bot_key = "#{user.id}-" | ||
|
|
||
| assert_no_difference -> { Boost.count } do | ||
| post room_bot_message_boosts_url(@room, bot_key, @message), params: +"👀" | ||
| end | ||
| assert_response :redirect | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test uses
Message.first.idwhich is 1, but message 1 belongs to the 'designers' room, not the 'watercooler' room (@room). When the controller tries to find this message in the watercooler room, it will raise aRecordNotFounderror. Use a message ID that actually belongs to the watercooler room, such as creating a message in setup or using a message ID from the watercooler fixtures.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit c82a8d3 - now using messages(:fourth) and messages(:thirteenth) which are fixtures belonging to the watercooler room where the bender bot is a member.