Skip to content

Refactor message handling to use invitation_group_id #1485

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

Merged
merged 6 commits into from
Apr 17, 2025

Conversation

axif0
Copy link
Contributor

@axif0 axif0 commented Mar 23, 2025

  • Updated MessagesController and related views to replace group_id with invitation_group_id for better clarity and functionality.

  • Adjusted message_params to permit the new attribute and modified the schema to reflect this change. Ensured consistency across the application by updating references in the nh_messages_controller and views.

Related issue
#1037

Copy link
Member

@prioux prioux left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

The purpose of the pull request is really to distinguish the attribute "group_id" in the Message object from all other uses of "group_id" in other contexts.

The group_id attribute in Message is not related at all to the message creation interface. It is used by the "group invitation" mechanism, where a message contains the group_id of the Group that people are invited to join. That is why I wanted it renamed.

So the pieces that are missing are in the Invitation model, where the group_id is being actually used to make invitations.

The remaining files that need to be adjusted are in

app/models/invitation.rb
app/controllers/invitations_controller.rb
app/controllers/nh_invitations_controller.rb

In particular, care should be taken to adjust the active record relationship directive belongs_to :group in the model, given by convention it makes a link between tables using "group_id <-> Group". Then in the controller code, the method to go throught he relationship shouls also be adjusted (e.g. line 96 of invitations_controller.rb)

@@ -1,13 +0,0 @@
class AddGroupIdAndTypeAndActiveToMessages < ActiveRecord::Migration
Copy link
Member

Choose a reason for hiding this comment

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

This file should not be deleted or modified (see my general comments for more info)

@@ -0,0 +1,9 @@
class RenameMessageGroupIdToInvitationGroupId < ActiveRecord::Migration[5.0]
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 this migration's code is fine, but I feel you created it manually instead of using the rails generator ("rails generate migration DoSomethingOrOther" generates the template for the file). I can tell because the name of the file starts with "20240323000000" which is definitely an artificial timestamp. It should starts with 202503 and the main schema.rb should have the same ID recorded in its version (line 13 of schema.rb).

All of this is generally done automatically by creating and running the migration on your local dev database. Please make the adjustments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel you created it manually instead of using the rails generator

Ya, I created it manually. I’ll regenerate it using the Rails generator to ensure the correct timestamp and update the schema accordingly. Thanks for pointing it out!

@@ -0,0 +1,13 @@
class AddInvitationGroupIdAndTypeAndActiveToMessages < ActiveRecord::Migration
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand where this migration comes from, especially since it's from 2012. See my general comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't understand where this migration comes from, especially since it's from 2012. See my general comments.

Actually, I had renamed the file, which caused the confusion. I’ve reverted the files now.

@@ -247,7 +247,7 @@
t.datetime "last_sent"
t.boolean "critical", default: false, null: false
t.boolean "display", default: false, null: false
t.integer "group_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 schema.rb's version has not been updated to reflect the applied migration (see my comment about the slightly wrong migration 20240323000000)

@@ -59,7 +59,7 @@ def index #:nodoc:

def new #:nodoc:
@message = Message.new # blank object for new() form.
@group_id = nil # for new() form
@invitation_group_id = nil # for new() form
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this file requires any changes at all. See my general comments.

@@ -74,7 +75,7 @@ def self.send_message(destination, options = {})
expiry = options[:expiry] || options["expiry"]
critical = options[:critical] || options["critical"] || false
send_email = options[:send_email] || options["send_email"] || false
group_id = options[:group_id] || options["group_id"]
group_id = options[:group_id] || options["group_id"] # For invitation messages only.
Copy link
Member

Choose a reason for hiding this comment

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

So we should rename the variable and the key in the options hash to 'invitation_group_id' and make sure that the change is applied in places where this is called (which should only be when making Invitation objects)

@@ -44,10 +44,10 @@
</p>

<p>
To users of project: <%= group_select(:group_id,
To users of project: <%= group_select(:destination_group_id,
Copy link
Member

Choose a reason for hiding this comment

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

No changes needed in this file, this group_id is again the group of people to send a message to, not an invitation group ID.

@@ -44,8 +44,8 @@
] %>
</p>

<% # group_id is not the attribute of the Message object %>
<%= hidden_field_tag :group_id, current_user.own_group.id %>
<% # destination_group_id is not the attribute of the Message object %>
Copy link
Member

Choose a reason for hiding this comment

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

This should also stay as "group_id"

@prioux
Copy link
Member

prioux commented Mar 24, 2025

As an additional comment: yes I'm fully aware this is a tricky change to get right, we're working with some legacy code that originally was not using 'good' names for the invitation framework.

@prioux
Copy link
Member

prioux commented Mar 24, 2025

Also @axif0 let me know if you decide not to adjust the PR, I will then make the changes myself because I feel bad about creating the original issue without having described the associated technical challenges.

@prioux
Copy link
Member

prioux commented Mar 24, 2025

(I just made some edits to my messages because some of the sentences were moved from local code comments to the main review comments, and made no sense anymore)

@axif0
Copy link
Contributor Author

axif0 commented Mar 24, 2025

I updated rename_message_group_id_to_invitation_group_id as

  • If neither column exists, it will add the appropriate one
  • If the old column exists but new doesn't, it will perform the rename

Also, thanks for the detailed elaboration.

@axif0 axif0 requested a review from prioux March 24, 2025 17:03
@@ -0,0 +1,27 @@
class RenameMessageGroupIdToInvitationGroupId < ActiveRecord::Migration[5.0]
Copy link
Member

@prioux prioux Mar 24, 2025

Choose a reason for hiding this comment

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

All these checks are unnecessary. The migration you had in the previous commit was already perfectly fine. There is no need to check for the column and create it if needed, because when the migration is run, it is guaranteed that the column will be present. Just revert to the what it was:

  def up
    rename_column :messages, :group_id, :invitation_group_id
  end

  def down
    rename_column :messages, :invitation_group_id, :group_id
  end

@axif0
Copy link
Contributor Author

axif0 commented Apr 3, 2025

@prioux Is there any changes that needs to be made for this PR?

Also, can you suggest some good first issue that needs to be done ?

@prioux
Copy link
Member

prioux commented Apr 3, 2025

I'll gave to review it. I didn't realize you updated it following my comments, sorry. I'll have a look tomorrow.

@prioux
Copy link
Member

prioux commented Apr 4, 2025

@axif0 Also you asked for a suggestion of something to do. The issue #849 is pretty easy. There's only one file to change, BrainPortal/app/model/cluster_task.rb, in the method capture_job_out_err(). The method reads four files and store the content inside four object attributes. Two of them (cluster_stdout and cluster_stderr) need some filtering before storing. We need to remove ANSI sequences, these are special byte sequences that can be recognized by matching. E.g. the byte sequence ESCAPE (ascii 27), followed by [, 0 and m ("\e[0m" is another way of expressing all four bytes) is the code to reset color in a terminal. There are hundreds of such sequences, instead of identifying each one, remove them all in one efficient scan of the file. We probably only need to remove the very common ones, for color, underline, bold, background color, etc. Suggestion:

  <esc> '['  <any number of digits or semicolons> <any single letter>

This would need to be validated with an official ANSI sequence document.

Copy link
Member

@prioux prioux left a comment

Choose a reason for hiding this comment

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

Small style comments only.

I have a feeling other parts of the code need changing, but I will now test the PR in my dev environement.

def down
rename_column :messages, :invitation_group_id, :group_id
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Missing newline at the end of the file. Our convention in CBRAIN is that all lines must end with a newline, even the last one.

@@ -92,4 +92,4 @@
<%= f.submit "Submit" %>
</p>
</div>
<% end %>
<% end %>
Copy link
Member

Choose a reason for hiding this comment

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

Missing newline

@@ -10,4 +10,4 @@ def self.down
remove_column :messages, :group_id
remove_column :message, :active
end
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Missing terminal newline

@@ -178,4 +178,4 @@ def destroy #:nodoc:
def message_params
params.require(:message).permit(:header, :description, :variable_text, :message_type, :read, :user_id, :expiry, :last_sent, :critical, :display, :send_email, :group_id, :sender_id)
end
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Missing newline

Copy link
Member

Choose a reason for hiding this comment

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

This actually means no changed are supposed to happen to messages_controller.rb I think

@prioux
Copy link
Member

prioux commented Apr 8, 2025

Yep, I got a crash at line 144 of BrainPortal/app/views/groups/show.html.erb

So there are other places that need to be adjusted. Please make sure you can invite users and accept the invitation with dummy users in your dev environment.

@prioux
Copy link
Member

prioux commented Apr 8, 2025

Also line 43 of invitations_controller.rb

@prioux
Copy link
Member

prioux commented Apr 8, 2025

Also no invitation messages are created because the parameter for the group ID is set with key :invitation_group_id while in the message.rb code, it is still expected to be :group_id

@prioux
Copy link
Member

prioux commented Apr 8, 2025

Currently I have three required changes:

diff --git a/BrainPortal/app/controllers/invitations_controller.rb b/BrainPortal/app/controllers/invitations_controller.rb
index 968b4085a..de2970ae5 100644
--- a/BrainPortal/app/controllers/invitations_controller.rb
+++ b/BrainPortal/app/controllers/invitations_controller.rb
@@ -40,7 +40,7 @@ class InvitationsController < ApplicationController
        return
     end
 
-    already_sent_to = Invitation.where(sender_id: current_user.id, active: true, group_id: @group.id).all.map(&:user_id)
+    already_sent_to = Invitation.where(sender_id: current_user.id, active: true, invitation_group_id: @group.id).all.map(&:user_id)
     @users = current_user.visible_users.where("users.id NOT IN (?)", @group.users.map(&:id) | already_sent_to)
     render :partial => "new"
   end
diff --git a/BrainPortal/app/models/message.rb b/BrainPortal/app/models/message.rb
index 6bd6b7aed..6bf05c252 100644
--- a/BrainPortal/app/models/message.rb
+++ b/BrainPortal/app/models/message.rb
@@ -75,7 +75,7 @@ class Message < ApplicationRecord
     expiry       = options[:expiry]        || options["expiry"]
     critical     = options[:critical]      || options["critical"]      || false
     send_email   = options[:send_email]    || options["send_email"]    || false
-    group_id     = options[:group_id]      || options["group_id"]      # For invitation messages only.
+    group_id     = options[:invitation_group_id] || options["invitation_group_id"]
     sender_id    = options[:sender_id]     || options["sender_id"]
 
     # Affects how 'destination' is interpreted. Default: exclude locked users.

and

diff --git a/BrainPortal/app/views/groups/show.html.erb b/BrainPortal/app/views/groups/show.html.erb
index 325348692..8ee4f254e 100644
--- a/BrainPortal/app/views/groups/show.html.erb
+++ b/BrainPortal/app/views/groups/show.html.erb
@@ -141,7 +141,7 @@
 
   <%
     group_members  = @group.users
-    open_invites   = Invitation.where(sender_id: current_user.id, group_id: @group.id, active: true).to_a
+    open_invites   = Invitation.where(sender_id: current_user.id, invitation_group_id: @group.id, active: true).to_a
   %>
 
   <%= show_table(@group, :as => :group, :header => 'Members', :edit_condition => @group.can_be_edited_by?(current_user) && (!current_user.has_role?(:normal_user) || group_members.count > 1 || open_invites.count > 0 )) do |t| %>

@axif0
Copy link
Contributor Author

axif0 commented Apr 8, 2025

Thank you so much for the detailed explanation! @prioux I really appreciate your guidance. I'm still learning Ruby and getting familiar with the codebase, so apologies if I miss anything along the way.

@prioux
Copy link
Member

prioux commented Apr 17, 2025

I think this PR is ready now. I will do a revision.

@axif0
Copy link
Contributor Author

axif0 commented Apr 17, 2025

Thank you for the changes. @prioux

@prioux
Copy link
Member

prioux commented Apr 17, 2025

I'm going to merge as soon as the automated tests are finished.

@prioux prioux linked an issue Apr 17, 2025 that may be closed by this pull request
@prioux prioux merged commit 39ebc8c into aces:master Apr 17, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rename Message attribute group_id to "invitation_group_id"
2 participants