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
DEV: move post flags into database #26951
base: main
Are you sure you want to change the base?
Conversation
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.
Just a few initial comments, will leave it there for now though.
This is preparation for a feature that will allow admins to define their custom flags. Current behaviour should stay untouched.
d4f1a1f
to
286d8ad
Compare
t.boolean :topic_type, default: false, null: false | ||
t.boolean :notify_type, default: false, null: false | ||
t.boolean :auto_action_type, default: false, null: false | ||
t.boolean :custom_type, default: false, null: false |
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.
@martin-brennan what do you think about adding chat_type
? Even if right now, all custom flags will be working with chat as well, later we will be easily able to modify UI to be more granular. It might be premature, we can always add a column later.
I would like to have something reasonably safe deployed and then make constant improvements.
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.
I think we need to rethink these type columns based on the discussions in /t/127494/29 internally, where we have landed on adding an applies_to
column (or something similar), which would indicate an array of polymorphic types that the flag applies to (e.g. Post
, Topic
, Chat::Message
).
Plugins would need to be able to register their own type so it can be chosen in the UI and stored in this column, and so we can tell via DiscoursePluginRegistry
whether to render flags of that type based on whether the plugin is enabled.
The other column names here (notify_type
, auto_action_type
, custom_type
) are all a bit confusing too, because they are just boolean columns indicating different configurations for the flag. I can see this has been partially informed by FlagSettings
, but I think they could be a bit more generic. Something like:
- notify_type - will_notify
- auto_action_type - performs_auto_action
- custom_type - ???
What I really struggle with is what all of these actually mean practically. Do you have a definition or idea of what each of them do, and if so can you give me a quick summary? Especially the notify_type
is quite confusing; if we wanted to expand the flag to notify certain groups per /t/127978 how would we do this? Why are notify_user
and notify_moderators
individual flag names rather than different configurations of the same flag? We need more clarity on this before proceeding.
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.
And what makes a flag be custom_type
true? It seems very arbitrary right now.
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 can mostly be disregarded now, see latest comment. We still do need to think about how notify/auto action stuff will work down the road though, which may require migrations.
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.
Okay there is a lot here, but we are building the foundation :) Sorry if some of this is a bit unclear, it's been a long day, please let me know if you need clarification on anything. The main thing I am concerned about is the various different "things" each flag can be used against (post, topic, chat message) and how the existing legacy post action type and reviewable score records tie into all of this.
app/models/post_action_type.rb
Outdated
Flag | ||
.enabled | ||
.order(:position) | ||
.each do |post_flag| |
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.
Still called post_flag
here, it should be just flag
.
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.
Also once we have applies_to
(or similar) column we would only want to get the applies_to: ["Post"]
flags here.
t.boolean :topic_type, default: false, null: false | ||
t.boolean :notify_type, default: false, null: false | ||
t.boolean :auto_action_type, default: false, null: false | ||
t.boolean :custom_type, default: false, null: false |
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.
I think we need to rethink these type columns based on the discussions in /t/127494/29 internally, where we have landed on adding an applies_to
column (or something similar), which would indicate an array of polymorphic types that the flag applies to (e.g. Post
, Topic
, Chat::Message
).
Plugins would need to be able to register their own type so it can be chosen in the UI and stored in this column, and so we can tell via DiscoursePluginRegistry
whether to render flags of that type based on whether the plugin is enabled.
The other column names here (notify_type
, auto_action_type
, custom_type
) are all a bit confusing too, because they are just boolean columns indicating different configurations for the flag. I can see this has been partially informed by FlagSettings
, but I think they could be a bit more generic. Something like:
- notify_type - will_notify
- auto_action_type - performs_auto_action
- custom_type - ???
What I really struggle with is what all of these actually mean practically. Do you have a definition or idea of what each of them do, and if so can you give me a quick summary? Especially the notify_type
is quite confusing; if we wanted to expand the flag to notify certain groups per /t/127978 how would we do this? Why are notify_user
and notify_moderators
individual flag names rather than different configurations of the same flag? We need more clarity on this before proceeding.
# frozen_string_literal: true | ||
|
||
class Flag < ActiveRecord::Base | ||
scope :enabled, -> { where(enabled: true) } |
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.
Might also want a scope to only return the system
flags.
end | ||
|
||
it "returns false when flag is system" do | ||
flag.update!(id: 5) |
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.
Rather than updating this one, we should query the system flag, since it will exist in the database from the seed. E.g. Flag.where(name: "off_topic")
, or if you had a scope you could do Flag.system.first
class CreateFlags < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :flags do |t| | ||
t.string :name, unique: true |
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.
I feel like the name
here should be the name_key
in the database, and we should have a separate name
column which would be the real, human-readable name for the custom flag. We would calculate the name_key
when the flag is created and ensure it does not overlap with any of the system flags (which would have an empty name
column since we have translations for them).
We also need a description
column here for the custom flags.
t.boolean :topic_type, default: false, null: false | ||
t.boolean :notify_type, default: false, null: false | ||
t.boolean :auto_action_type, default: false, null: false | ||
t.boolean :custom_type, default: false, null: false |
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.
And what makes a flag be custom_type
true? It seems very arbitrary right now.
end | ||
|
||
def self.reset_flag_settings! | ||
PostActionType.reload_types |
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.
I think it would be worth doing a writeup about how the PostActionType
and ReviewableScore
flag types differ. All of this intertwining with PostActionType
makes me nervous, but maybe we can't do much about it for historical reasons. I just want us to be super clear about how the flagging system fits together especially when it comes to custom flags defined by admins.
@@ -0,0 +1,56 @@ | |||
# frozen_string_literal: true | |||
|
|||
Flag.seed do |s| |
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.
Just double checking, these do get created on an existing site right?
OK re-evaluating based on your internal comments and guidance on the _type columns Kris, I think the main change we need to make here is to:
Then also just the few other suggestions here that are more minor then it can be good to go. |
@martin-brennan thank you for the review. Everything should be fixed 🤞 |
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.
I think this is a good foundation Kris, thanks for taking the time to address review comments :)
end | ||
|
||
def set_name_key | ||
self.name_key = self.name.gsub(" ", "_").gsub(/[^\w]/, "").downcase |
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.
Might be worth adding a couple of unit tests for the model with variations on the name to make sure that this name_key method works OK (e.g. with special characters, things like It's Illegal
)
This is preparation for a feature that will allow admins to define their custom flags. Current behaviour should stay untouched.
A basic guardian was also defined. In addition, default translations are handled. Old flags will use translations, but at this point, admin defined flags will not be translated.