Skip to content
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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

lis2
Copy link
Contributor

@lis2 lis2 commented May 9, 2024

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.

@github-actions github-actions bot added the i18n PRs which update English locale files or i18n related code label May 9, 2024
Copy link
Contributor

@martin-brennan martin-brennan left a 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.

app/models/post_action_type.rb Outdated Show resolved Hide resolved
app/models/post_action_type.rb Outdated Show resolved Hide resolved
app/models/post_flag.rb Outdated Show resolved Hide resolved
app/models/post_flag.rb Outdated Show resolved Hide resolved
app/models/post_flag.rb Outdated Show resolved Hide resolved
app/models/post_flag.rb Outdated Show resolved Hide resolved
config/locales/client.en.yml Outdated Show resolved Hide resolved
This is preparation for a feature that will allow admins to define their custom flags. Current behaviour should stay untouched.
@lis2 lis2 force-pushed the custom-flags branch 2 times, most recently from d4f1a1f to 286d8ad Compare May 13, 2024 04:30
@github-actions github-actions bot added the chat PRs which include a change to Chat plugin label May 13, 2024
@lis2 lis2 marked this pull request as ready for review May 14, 2024 03:07
@lis2 lis2 requested a review from martin-brennan May 14, 2024 03:07
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
Copy link
Contributor Author

@lis2 lis2 May 14, 2024

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

@martin-brennan martin-brennan left a 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/flag.rb Show resolved Hide resolved
Flag
.enabled
.order(:position)
.each do |post_flag|
Copy link
Contributor

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.

Copy link
Contributor

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
Copy link
Contributor

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) }
Copy link
Contributor

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)
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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|
Copy link
Contributor

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?

@martin-brennan
Copy link
Contributor

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:

  • Add an applies_to column which would be either an array type or just a simple string type with a separator like Post|Topic|Chat::Message
  • Remove the topic_type since it is covered by the above
  • Do the name/name_key changes suggested, I think that will help us split the translation out and it will be good to cook the name key underscore replacement stuff on create
  • Add a description column

Then also just the few other suggestions here that are more minor then it can be good to go.

@lis2
Copy link
Contributor Author

lis2 commented May 16, 2024

@martin-brennan thank you for the review. Everything should be fixed 🤞

Copy link
Contributor

@martin-brennan martin-brennan left a 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
Copy link
Contributor

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chat PRs which include a change to Chat plugin i18n PRs which update English locale files or i18n related code
2 participants