Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Stree #1030

Merged
merged 1 commit into from
Jul 20, 2022
Merged

Stree #1030

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/plugin-linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ jobs:
- name: Rubocop
if: ${{ always() }}
run: bundle exec rubocop .

- name: Syntax Tree
if: ${{ always() }}
run: bundle exec stree check --print-width=100 --plugins=plugin/trailing_comma **/*.rb Gemfile **/*.rake
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

source 'https://rubygems.org'
source "https://rubygems.org"

group :development do
gem 'rubocop-discourse'
gem "rubocop-discourse", git: "https://github.com/discourse/rubocop-discourse/", branch: "stree"
gem "syntax_tree"
end
30 changes: 21 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
GIT
remote: https://github.com/discourse/rubocop-discourse/
revision: 8afca6460a423a11a2e0bf1f7051b18dd9a7231b
branch: stree
specs:
rubocop-discourse (2.5.0)
rubocop (>= 1.1.0)
rubocop-rspec (>= 2.0.0)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
json (2.6.2)
parallel (1.22.1)
parser (3.1.2.0)
ast (~> 2.4.1)
prettier_print (0.1.0)
rainbow (3.1.1)
regexp_parser (2.5.0)
rexml (3.2.5)
rubocop (1.30.1)
rubocop (1.31.2)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.1.0.0)
rainbow (>= 2.2.2, < 4.0)
Expand All @@ -17,15 +29,14 @@ GEM
rubocop-ast (>= 1.18.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.18.0)
rubocop-ast (1.19.1)
parser (>= 3.1.1.0)
rubocop-discourse (2.5.0)
rubocop (>= 1.1.0)
rubocop-rspec (>= 2.0.0)
rubocop-rspec (2.11.1)
rubocop (~> 1.19)
rubocop-rspec (2.12.1)
rubocop (~> 1.31)
ruby-progressbar (1.11.0)
unicode-display_width (2.1.0)
syntax_tree (3.2.0)
prettier_print
unicode-display_width (2.2.0)

PLATFORMS
arm64-darwin-20
Expand All @@ -36,7 +47,8 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
rubocop-discourse
rubocop-discourse!
syntax_tree

BUNDLED WITH
2.3.14
36 changes: 18 additions & 18 deletions app/controllers/admin/admin_incoming_chat_webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ class DiscourseChat::AdminIncomingChatWebhooksController < Admin::AdminControlle
requires_plugin DiscourseChat::PLUGIN_NAME

def index
render_serialized({
chat_channels: ChatChannel.public_channels,
incoming_chat_webhooks: IncomingChatWebhook.includes(:chat_channel).all
}, AdminChatIndexSerializer, root: false)
render_serialized(
{
chat_channels: ChatChannel.public_channels,
incoming_chat_webhooks: IncomingChatWebhook.includes(:chat_channel).all,
},
AdminChatIndexSerializer,
root: false,
)
end

def create
params.require([:name, :chat_channel_id])
params.require(%i[name chat_channel_id])

chat_channel = ChatChannel.find_by(id: params[:chat_channel_id])
if chat_channel.nil? || chat_channel.direct_message_channel?
raise Discourse::NotFound
end
raise Discourse::NotFound if chat_channel.nil? || chat_channel.direct_message_channel?

webhook = IncomingChatWebhook.new(name: params[:name], chat_channel: chat_channel)
if webhook.save
Expand All @@ -27,23 +29,21 @@ def create
end

def update
params.require([:incoming_chat_webhook_id, :name, :chat_channel_id])
params.require(%i[incoming_chat_webhook_id name chat_channel_id])

webhook = IncomingChatWebhook.find_by(id: params[:incoming_chat_webhook_id])
raise Discourse::NotFound unless webhook

chat_channel = ChatChannel.find_by(id: params[:chat_channel_id])
if chat_channel.nil? || chat_channel.direct_message_channel?
raise Discourse::NotFound
end
raise Discourse::NotFound if chat_channel.nil? || chat_channel.direct_message_channel?

if webhook.update(
name: params[:name],
description: params[:description],
emoji: params[:emoji],
username: params[:username],
chat_channel: chat_channel
)
name: params[:name],
description: params[:description],
emoji: params[:emoji],
username: params[:username],
chat_channel: chat_channel,
)
render json: success_json
else
render_json_error(webhook)
Expand Down
23 changes: 14 additions & 9 deletions app/controllers/api/category_chatables_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ def permissions
category = Category.find(params[:id])

if category.read_restricted?
permissions = Group
.joins(:category_groups)
.where(category_groups: { category_id: category.id })
.joins('LEFT OUTER JOIN group_users ON groups.id = group_users.group_id')
.group('groups.id', 'groups.name')
.pluck('groups.name', 'COUNT(group_users.user_id)')
permissions =
Group
.joins(:category_groups)
.where(category_groups: { category_id: category.id })
.joins("LEFT OUTER JOIN group_users ON groups.id = group_users.group_id")
.group("groups.id", "groups.name")
.pluck("groups.name", "COUNT(group_users.user_id)")

group_names = permissions.map { |p| "@#{p[0]}" }
members_count = permissions.sum { |p| p[1].to_i }
group_names = permissions.map { |p| "@#{p[0]}" }
members_count = permissions.sum { |p| p[1].to_i }

permissions_result = { allowed_groups: group_names, members_count: members_count, private: true }
permissions_result = {
allowed_groups: group_names,
members_count: members_count,
private: true,
}
else
everyone_group = Group.find(Group::AUTO_GROUPS[:everyone])

Expand Down
13 changes: 7 additions & 6 deletions app/controllers/api/chat_channel_memberships_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ def index
offset = (params[:offset] || 0).to_i
limit = (params[:limit] || 50).to_i.clamp(1, 50)

memberships = ChatChannelMembershipsQuery.call(
channel,
offset: offset,
limit: limit,
username: params[:username]
)
memberships =
ChatChannelMembershipsQuery.call(
channel,
offset: offset,
limit: limit,
username: params[:username],
)

render_serialized(memberships, UserChatChannelMembershipSerializer, root: false)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

MEMBERSHIP_EDITABLE_PARAMS = [:muted, :desktop_notification_level, :mobile_notification_level]
MEMBERSHIP_EDITABLE_PARAMS = %i[muted desktop_notification_level mobile_notification_level]

class DiscourseChat::Api::ChatChannelNotificationsSettingsController < DiscourseChat::Api::ChatChannelsController
def update
Expand Down
47 changes: 27 additions & 20 deletions app/controllers/api/chat_channels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

class DiscourseChat::Api::ChatChannelsController < DiscourseChat::Api
def index
options = {
status: params[:status] ? ChatChannel.statuses[params[:status]] : nil
}.merge(params.permit(:filter, :limit, :offset)).symbolize_keys!

channels = DiscourseChat::ChatChannelFetcher.secured_public_channels(
guardian,
UserChatChannelMembership.where(user: current_user),
options
)
options = { status: params[:status] ? ChatChannel.statuses[params[:status]] : nil }.merge(
params.permit(:filter, :limit, :offset),
).symbolize_keys!

channels =
DiscourseChat::ChatChannelFetcher.secured_public_channels(
guardian,
UserChatChannelMembership.where(user: current_user),
options,
)

render_serialized(channels, ChatChannelSerializer)
end
Expand All @@ -24,13 +25,13 @@ def update
chat_channel = find_chat_channel

if chat_channel.direct_message_channel?
raise Discourse::InvalidParameters.new(I18n.t("chat.errors.cant_update_direct_message_channel"))
raise Discourse::InvalidParameters.new(
I18n.t("chat.errors.cant_update_direct_message_channel"),
)
end

params_to_edit = editable_params(params, chat_channel)
params_to_edit.each do |k, v|
params_to_edit[k] = nil if params_to_edit[k].blank?
end
params_to_edit.each { |k, v| params_to_edit[k] = nil if params_to_edit[k].blank? }

if ActiveRecord::Type::Boolean.new.deserialize(params_to_edit[:auto_join_users])
auto_join_limiter(chat_channel).performed!
Expand All @@ -56,23 +57,29 @@ def find_chat_channel
end

def find_membership
membership = UserChatChannelMembership
.includes(:user, :chat_channel)
.find_by!(user: current_user, chat_channel_id: params.require(:chat_channel_id))
membership =
UserChatChannelMembership.includes(:user, :chat_channel).find_by!(
user: current_user,
chat_channel_id: params.require(:chat_channel_id),
)
guardian.ensure_can_see_chat_channel!(membership.chat_channel)
membership
end

def auto_join_limiter(chat_channel)
RateLimiter.new(current_user, "auto_join_users_channel_#{chat_channel.id}", 1, 3.minutes, apply_limit_to_staff: true)
RateLimiter.new(
current_user,
"auto_join_users_channel_#{chat_channel.id}",
1,
3.minutes,
apply_limit_to_staff: true,
)
end

def editable_params(params, chat_channel)
permitted_params = CHAT_CHANNEL_EDITABLE_PARAMS

if chat_channel.category_channel?
permitted_params += CATEGORY_CHAT_CHANNEL_EDITABLE_PARAMS
end
permitted_params += CATEGORY_CHAT_CHANNEL_EDITABLE_PARAMS if chat_channel.category_channel?

params.permit(*permitted_params)
end
Expand Down
8 changes: 2 additions & 6 deletions app/controllers/chat_base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ def ensure_can_chat
end

def set_channel_and_chatable_with_access_check(chat_channel_id: nil)
if chat_channel_id.blank?
params.require(:chat_channel_id)
end
params.require(:chat_channel_id) if chat_channel_id.blank?
id_or_name = chat_channel_id || params[:chat_channel_id]
@chat_channel = DiscourseChat::ChatChannelFetcher.find_with_access_check(
id_or_name, guardian
)
@chat_channel = DiscourseChat::ChatChannelFetcher.find_with_access_check(id_or_name, guardian)
@chatable = @chat_channel.chatable
end
end
Loading