Skip to content

Commit c494893

Browse files
committed
Encrypt message content by default
Migrate unencrypted messages to encrypted message rich text content and drop old unencrypted column.
1 parent b83dc1c commit c494893

File tree

6 files changed

+42
-3
lines changed

6 files changed

+42
-3
lines changed

app/models/better_together/message.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Message < ApplicationRecord
66
belongs_to :conversation, touch: true
77
belongs_to :sender, class_name: 'BetterTogether::Person'
88

9+
has_rich_text :content, encrypted: true
10+
911
validates :content, presence: true
1012

1113
after_create_commit do
@@ -16,5 +18,9 @@ class Message < ApplicationRecord
1618
created_at: created_at.strftime('%H:%M %d-%m-%Y')
1719
})
1820
end
21+
22+
# def content
23+
# super || self[:content]
24+
# end
1925
end
2026
end

app/views/better_together/messages/_form.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!-- app/views/messages/_form.html.erb -->
22
<%= form_with(model: [conversation, message], local: false, html: { id: "new_message", data: { controller: "better_together--message-form" } }) do |form| %>
3-
<div class="input-group">
4-
<%= form.text_area :content, class: 'form-control', rows: 1, placeholder: t('better_together.messages.form.placeholder'), required: true, "data-better_together--message-form-target": "content" %>
5-
<button class="btn btn-primary" type="submit">
3+
<div class="message-box d-flex flex-column">
4+
<%= form.rich_text_area :content, class: 'form-control', rows: 1, placeholder: t('better_together.messages.form.placeholder'), required: true, "data-better_together--message-form-target": "content" %>
5+
<button class="btn btn-primary ms-auto mt-2" type="submit">
66
<%= t('better_together.messages.form.send') %> <i class="fas fa-paper-plane"></i>
77
</button>
88
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class RemoveMessagesContentColumnNull < ActiveRecord::Migration[7.1]
2+
def change
3+
change_column_null :better_together_messages, :content, true
4+
end
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class RemoveActionTextRichTextsLocaleColumnNull < ActiveRecord::Migration[7.1]
2+
def change
3+
change_column_null :action_text_rich_texts, :locale, true
4+
end
5+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class MigrateUnencryptedMessageContentAndDropColumn < ActiveRecord::Migration[7.1]
2+
def change
3+
reversible do |dir|
4+
dir.up do
5+
load 'tasks/data_migration.rake'
6+
Rake::Task['better_together:migrate_data:unencrypted_messages'].invoke
7+
8+
remove_column :better_together_messages, :content, if_exists: true
9+
end
10+
end
11+
end
12+
end

lib/tasks/data_migration.rake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,16 @@ namespace :better_together do
4444
end
4545
end
4646
end
47+
48+
desc 'migrates unencrypted message content to encrypted rich text'
49+
task unencrypted_messages: :environment do
50+
BetterTogether::Message.all.each do |message|
51+
next if message.content.persisted? || message[:content].nil?
52+
53+
message.content = message[:content]
54+
55+
message.save
56+
end
57+
end
4758
end
4859
end

0 commit comments

Comments
 (0)