8 Commits

Author SHA1 Message Date
Râu Cao
ad90fcd539 Add specs for xmpp default bookmarks, refactor xmpp job usage
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2023-04-08 16:37:21 +02:00
Râu Cao
705bd63b42 Add configurable default room bookmarks for new users
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-07 23:03:43 +02:00
Râu Cao
83e418cdee Update README
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-07 20:11:45 +02:00
Râu Cao
7a193d6647 Add comment
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-06 16:25:01 +02:00
Râu Cao
bb82b6b462 Update README 2023-04-06 16:24:46 +02:00
Râu Cao
a1b238e86b Fix email default URL options missing
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-04 09:11:06 +02:00
6f2160b479 Merge pull request 'Add solargraph in development, document usage with bundled gems' (#112) from feature/solargraph into master
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #112
2023-04-02 08:19:26 +00:00
Râu Cao
fe1dfd8ec8 Add solargraph in development, document usage with bundled gems
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Update release notes draft
2023-03-31 18:07:38 +02:00
14 changed files with 213 additions and 69 deletions

View File

@@ -1,3 +1,5 @@
AKKOUNTS_DOMAIN=accounts.example.com
SMTP_SERVER=smtp.example.com SMTP_SERVER=smtp.example.com
SMTP_PORT=587 SMTP_PORT=587
SMTP_LOGIN=accounts SMTP_LOGIN=accounts

View File

@@ -66,6 +66,7 @@ group :development do
gem 'letter_opener' gem 'letter_opener'
gem 'letter_opener_web' gem 'letter_opener_web'
gem 'faker' gem 'faker'
gem 'solargraph'
end end
group :test do group :test do

View File

@@ -68,7 +68,10 @@ GEM
tzinfo (~> 2.0) tzinfo (~> 2.0)
addressable (2.8.1) addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0) public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
backport (1.2.0)
bcrypt (3.1.18) bcrypt (3.1.18)
benchmark (0.2.1)
bindex (0.8.1) bindex (0.8.1)
builder (3.2.4) builder (3.2.4)
byebug (11.1.3) byebug (11.1.3)
@@ -109,6 +112,7 @@ GEM
dotenv-rails (2.8.1) dotenv-rails (2.8.1)
dotenv (= 2.8.1) dotenv (= 2.8.1)
railties (>= 3.2) railties (>= 3.2)
e2mmap (0.1.0)
erubi (1.11.0) erubi (1.11.0)
et-orbi (1.2.7) et-orbi (1.2.7)
tzinfo tzinfo
@@ -135,9 +139,15 @@ GEM
importmap-rails (1.1.5) importmap-rails (1.1.5)
actionpack (>= 6.0.0) actionpack (>= 6.0.0)
railties (>= 6.0.0) railties (>= 6.0.0)
jaro_winkler (1.5.4)
jbuilder (2.11.5) jbuilder (2.11.5)
actionview (>= 5.0.0) actionview (>= 5.0.0)
activesupport (>= 5.0.0) activesupport (>= 5.0.0)
json (2.6.3)
kramdown (2.4.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
launchy (2.5.0) launchy (2.5.0)
addressable (~> 2.7) addressable (~> 2.7)
letter_opener (1.8.1) letter_opener (1.8.1)
@@ -179,6 +189,9 @@ GEM
racc (~> 1.4) racc (~> 1.4)
orm_adapter (0.5.0) orm_adapter (0.5.0)
pagy (6.0.2) pagy (6.0.2)
parallel (1.22.1)
parser (3.2.1.1)
ast (~> 2.4.1)
pg (1.2.3) pg (1.2.3)
public_suffix (5.0.0) public_suffix (5.0.0)
puma (4.3.12) puma (4.3.12)
@@ -217,6 +230,7 @@ GEM
rake (>= 12.2) rake (>= 12.2)
thor (~> 1.0) thor (~> 1.0)
zeitwerk (~> 2.5) zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.0.6) rake (13.0.6)
rb-fsevent (0.11.2) rb-fsevent (0.11.2)
rb-inotify (0.10.1) rb-inotify (0.10.1)
@@ -229,6 +243,8 @@ GEM
responders (3.1.0) responders (3.1.0)
actionpack (>= 5.2) actionpack (>= 5.2)
railties (>= 5.2) railties (>= 5.2)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.5) rexml (3.2.5)
rqrcode (2.1.2) rqrcode (2.1.2)
chunky_png (~> 1.0) chunky_png (~> 1.0)
@@ -251,6 +267,19 @@ GEM
rspec-mocks (~> 3.11) rspec-mocks (~> 3.11)
rspec-support (~> 3.11) rspec-support (~> 3.11)
rspec-support (3.12.0) rspec-support (3.12.0)
rubocop (1.48.1)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.26.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.28.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5) ruby2_keywords (0.0.5)
rufus-scheduler (3.8.2) rufus-scheduler (3.8.2)
fugit (~> 1.1, >= 1.1.6) fugit (~> 1.1, >= 1.1.6)
@@ -268,6 +297,21 @@ GEM
rufus-scheduler (~> 3.2) rufus-scheduler (~> 3.2)
sidekiq (>= 4, < 7) sidekiq (>= 4, < 7)
tilt (>= 1.4.0) tilt (>= 1.4.0)
solargraph (0.48.0)
backport (~> 1.2)
benchmark
bundler (>= 1.17.2)
diff-lcs (~> 1.4)
e2mmap
jaro_winkler (~> 1.5)
kramdown (~> 2.3)
kramdown-parser-gfm (~> 1.1)
parser (~> 3.0)
reverse_markdown (>= 1.0.5, < 3)
rubocop (>= 0.52)
thor (~> 1.0)
tilt (~> 2.0)
yard (~> 0.9, >= 0.9.24)
sprockets (4.1.1) sprockets (4.1.1)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
rack (> 1, < 3) rack (> 1, < 3)
@@ -289,6 +333,7 @@ GEM
railties (>= 6.0.0) railties (>= 6.0.0)
tzinfo (2.0.5) tzinfo (2.0.5)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
view_component (2.78.0) view_component (2.78.0)
activesupport (>= 5.0.0, < 8.0) activesupport (>= 5.0.0, < 8.0)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
@@ -304,11 +349,14 @@ GEM
addressable (>= 2.8.0) addressable (>= 2.8.0)
crack (>= 0.3.2) crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0) hashdiff (>= 0.4.0, < 2.0.0)
webrick (1.7.0)
websocket-driver (0.7.5) websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0) websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5) websocket-extensions (0.1.5)
xpath (3.2.0) xpath (3.2.0)
nokogiri (~> 1.8) nokogiri (~> 1.8)
yard (0.9.28)
webrick (~> 1.7.0)
zeitwerk (2.6.6) zeitwerk (2.6.6)
PLATFORMS PLATFORMS
@@ -344,6 +392,7 @@ DEPENDENCIES
sentry-ruby sentry-ruby
sidekiq (< 7) sidekiq (< 7)
sidekiq-scheduler sidekiq-scheduler
solargraph
sprockets-rails sprockets-rails
sqlite3 (~> 1.4) sqlite3 (~> 1.4)
stimulus-rails stimulus-rails

View File

@@ -23,9 +23,8 @@ so:
After these steps, you should have a working Rails app with a handful of test After these steps, you should have a working Rails app with a handful of test
users running on [http://localhost:3000](http://localhost:3000). users running on [http://localhost:3000](http://localhost:3000).
Log in with username "admin" and password "admin is admin". All users listed on Log in with username "admin" and password "admin is admin". All users listed on
[http://localhost:3000/admin/ldap_users](http://localhost:3000/admin/ldap_users) [http://localhost:3000/admin/users](http://localhost:3000/admin/users)
have the password "user is user". have the password "user is user".
### Rails app ### Rails app
@@ -79,6 +78,15 @@ The setup task will first delete any existing entries in the directory tree
Note that all 389ds data is stored in `tmp/389ds`. So if you want to start over Note that all 389ds data is stored in `tmp/389ds`. So if you want to start over
with a fresh installation, delete both that directory as well as the container. with a fresh installation, delete both that directory as well as the container.
### Solargraph
[Solargraph](https://solargraph.org/) is a Ruby language server, which you may
use with your editor to add features like auto-completion and syntax
validation. You can add inline documentation for bundled gems with this
command:
bundle exec yard gems
## Documentation ## Documentation
### Rails ### Rails

View File

@@ -1,18 +1,21 @@
class XmppExchangeContactsJob < ApplicationJob class XmppExchangeContactsJob < ApplicationJob
queue_as :default queue_as :default
def perform(inviter, username, domain) def perform(inviter, invitee)
return unless inviter.services_enabled.include?("ejabberd") &&
invitee.services_enabled.include?("ejabberd")
ejabberd = EjabberdApiClient.new ejabberd = EjabberdApiClient.new
ejabberd.add_rosteritem({ ejabberd.add_rosteritem({
"localuser": username, "localhost": domain, "localuser": invitee.cn, "localhost": invitee.ou,
"user": inviter.cn, "host": inviter.ou, "user": inviter.cn, "host": inviter.ou,
"nick": inviter.cn, "group": Setting.ejabberd_buddy_roster, "subs": "both" "nick": inviter.cn, "group": Setting.ejabberd_buddy_roster, "subs": "both"
}) })
ejabberd.add_rosteritem({ ejabberd.add_rosteritem({
"localuser": inviter.cn, "localhost": inviter.ou, "localuser": inviter.cn, "localhost": inviter.ou,
"user": username, "host": domain, "user": invitee.cn, "host": invitee.ou,
"nick": username, "group": Setting.ejabberd_buddy_roster, "subs": "both" "nick": invitee.cn, "group": Setting.ejabberd_buddy_roster, "subs": "both"
}) })
end end
end end

View File

@@ -0,0 +1,26 @@
class XmppSetDefaultBookmarksJob < ApplicationJob
queue_as :default
def perform(user)
return unless Setting.xmpp_default_rooms.any?
@user = user
ejabberd = EjabberdApiClient.new
ejabberd.private_set user, storage_content
end
def storage_content
bookmarks = ""
Setting.xmpp_default_rooms.each do |r|
bookmarks << conference_element(
jid: r[/<(.+)>/, 1], name: r[/^(.+)\s/, 1], nick: @user.cn,
autojoin: Setting.xmpp_autojoin_default_rooms
)
end
"<storage xmlns='storage:bookmarks'>#{bookmarks}</storage>"
end
def conference_element(jid:, name:, autojoin: false, nick:)
"<conference jid='#{jid}' name='#{name}' autojoin='#{autojoin.to_s}'><nick>#{nick}</nick></conference>"
end
end

View File

@@ -17,6 +17,13 @@ class Setting < RailsSettings::Base
account accounts donations mail webmaster support account accounts donations mail webmaster support
] ]
#
# XMPP
#
field :xmpp_default_rooms, type: :array, default: []
field :xmpp_autojoin_default_rooms, type: :boolean, default: false
# #
# Sentry # Sentry
# #

View File

@@ -22,6 +22,7 @@ class User < ApplicationRecord
validates_format_of :cn, without: /\A-/, validates_format_of :cn, without: /\A-/,
if: Proc.new{ |u| u.cn.present? }, if: Proc.new{ |u| u.cn.present? },
message: "is invalid. Usernames need to start with a letter." message: "is invalid. Usernames need to start with a letter."
# FIXME This needs a server restart to apply values
validates_format_of :cn, without: /\A(#{Setting.reserved_usernames.join('|')})\z/i, validates_format_of :cn, without: /\A(#{Setting.reserved_usernames.join('|')})\z/i,
message: "has already been taken" message: "has already been taken"
@@ -58,11 +59,10 @@ class User < ApplicationRecord
enable_service %w[ discourse ejabberd gitea mediawiki ] enable_service %w[ discourse ejabberd gitea mediawiki ]
#TODO enable in development when we have easy setup of ejabberd etc. #TODO enable in development when we have easy setup of ejabberd etc.
return if Rails.env.development? return if Rails.env.development? || !Setting.ejabberd_enabled?
if inviter.present? XmppExchangeContactsJob.perform_later(inviter, self) if inviter.present?
exchange_xmpp_contact_with_inviter if Setting.ejabberd_enabled? XmppSetDefaultBookmarksJob.perform_later(self)
end
end end
def send_devise_notification(notification, *args) def send_devise_notification(notification, *args)
@@ -133,12 +133,6 @@ class User < ApplicationRecord
ldap.delete_attribute(dn,:service) ldap.delete_attribute(dn,:service)
end end
def exchange_xmpp_contact_with_inviter
return unless inviter.services_enabled.include?("ejabberd") &&
services_enabled.include?("ejabberd")
XmppExchangeContactsJob.perform_later(inviter, self.cn, self.ou)
end
private private
def ldap def ldap

View File

@@ -10,7 +10,7 @@ class EjabberdApiClient
if res.status != 200 if res.status != 200
Rails.logger.error "[ejabberd] API request failed:" Rails.logger.error "[ejabberd] API request failed:"
Rails.logger.error res.body Rails.logger.error res.body
#TODO add some kind of exception tracking/notifications #TODO Send custom event to Sentry
end end
end end
@@ -21,4 +21,9 @@ class EjabberdApiClient
def send_message(payload) def send_message(payload)
post "send_message", payload post "send_message", payload
end end
def private_set(user, content)
payload = { user: user.cn, host: user.ou, element: content }
post "private_set", payload
end
end end

View File

@@ -7,24 +7,43 @@
title: "Enable ejabberd integration", title: "Enable ejabberd integration",
description: "ejabberd configuration present and features enabled" description: "ejabberd configuration present and features enabled"
) %> ) %>
<% if Setting.ejabberd_enabled? %> <% if Setting.ejabberd_enabled? %>
<%= render FormElements::FieldsetComponent.new(title: "API URL") do %> <%= render FormElements::FieldsetComponent.new(title: "API URL") do %>
<%= f.text_field :ejabberd_api_url, <%= f.text_field :ejabberd_api_url,
value: Setting.ejabberd_api_url, value: Setting.ejabberd_api_url,
class: "w-full", disabled: true %> class: "w-full", disabled: true %>
<% end %> <% end %>
<%= render FormElements::FieldsetComponent.new(title: "Admin URL") do %> <%= render FormElements::FieldsetComponent.new(title: "Admin URL") do %>
<%= f.text_field :ejabberd_admin_url, <%= f.text_field :ejabberd_admin_url,
value: Setting.ejabberd_admin_url, value: Setting.ejabberd_admin_url,
class: "w-full", disabled: true %> class: "w-full", disabled: true %>
<% end %>
<%= render FormElements::FieldsetComponent.new(
title: "Contact roster name",
description: "Used when exchanging contacts after signup from invitation"
) do %>
<%= f.text_field :ejabberd_buddy_roster,
value: Setting.ejabberd_buddy_roster,
class: "w-full" %>
<% end %>
<% end %> <% end %>
</ul> </ul>
<h3 class="mt-8">User default settings</h3>
<ul role="list">
<%= render FormElements::FieldsetComponent.new(
title: "Default rooms",
description: "Add these default rooms to new users' bookmarks"
) do %>
<%= f.text_area :xmpp_default_rooms,
value: Setting.xmpp_default_rooms.join("\n"),
placeholder: "Welcome <welcome@kosmos.chat>\nKosmos <kosmos@kosmos.chat>",
class: "h-24 w-full" %>
<% end %>
<%= render FormElements::FieldsetToggleComponent.new(
form: f,
attribute: :xmpp_autojoin_default_rooms,
enabled: Setting.xmpp_autojoin_default_rooms?,
title: "Auto-join default rooms",
description: "Automatically join above default rooms in chat clients"
) %>
<%= render FormElements::FieldsetComponent.new(
title: "Contact roster name",
description: "Used when exchanging contacts after signup from invitation"
) do %>
<%= f.text_field :ejabberd_buddy_roster,
value: Setting.ejabberd_buddy_roster,
class: "w-full" %>
<% end %>
<% end %>
</ul>

View File

@@ -62,6 +62,11 @@ Rails.application.configure do
outgoing_email_address = ENV.fetch('SMTP_FROM_ADDRESS', 'accounts@localhost') outgoing_email_address = ENV.fetch('SMTP_FROM_ADDRESS', 'accounts@localhost')
outgoing_email_domain = Mail::Address.new(outgoing_email_address).domain outgoing_email_domain = Mail::Address.new(outgoing_email_address).domain
config.action_mailer.default_url_options = {
host: ENV['AKKOUNTS_DOMAIN'],
protocol: "https",
}
config.action_mailer.default_options = { config.action_mailer.default_options = {
from: outgoing_email_address, from: outgoing_email_address,
message_id: -> { "<#{Mail.random_tag}@#{outgoing_email_domain}>" }, message_id: -> { "<#{Mail.random_tag}@#{outgoing_email_domain}>" },

View File

@@ -2,15 +2,18 @@ require 'rails_helper'
require 'webmock/rspec' require 'webmock/rspec'
RSpec.describe XmppExchangeContactsJob, type: :job do RSpec.describe XmppExchangeContactsJob, type: :job do
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" } let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
let(:guest) { create :user, cn: "isaacnewton", ou: "kosmos.org",
id: 2, email: "hotapple42@eol.com" }
subject(:job) { subject(:job) {
described_class.perform_later(user, 'isaacnewton', 'kosmos.org') described_class.perform_later(user, guest)
} }
before do before do
stub_request(:post, "http://xmpp.example.com/api/add_rosteritem") stub_request(:post, "http://xmpp.example.com/api/add_rosteritem")
.to_return(status: 200, body: "", headers: {}) .to_return(status: 200, body: "", headers: {})
allow_any_instance_of(User).to receive(:services_enabled).and_return(["ejabberd"])
end end
it "posts add_rosteritem commands to the ejabberd API" do it "posts add_rosteritem commands to the ejabberd API" do

View File

@@ -0,0 +1,34 @@
require 'rails_helper'
require 'webmock/rspec'
RSpec.describe XmppSetDefaultBookmarksJob, type: :job do
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
before do
Setting.xmpp_default_rooms = [
"Welcome <welcome@kosmos.chat>",
"Kosmos Dev <kosmos-dev@kosmos.chat>"
]
end
subject(:job) {
described_class.perform_later(user)
}
before do
stub_request(:post, "http://xmpp.example.com/api/private_set")
.to_return(status: 200, body: "", headers: {})
end
it "posts a private_set command to the ejabberd API" do
perform_enqueued_jobs { job }
expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/private_set")
.with { |req| req.body == '{"user":"willherschel","host":"kosmos.org","element":"\u003cstorage xmlns=\'storage:bookmarks\'\u003e\u003cconference jid=\'welcome@kosmos.chat\' name=\'Welcome\' autojoin=\'false\'\u003e\u003cnick\u003ewillherschel\u003c/nick\u003e\u003c/conference\u003e\u003cconference jid=\'kosmos-dev@kosmos.chat\' name=\'Kosmos Dev\' autojoin=\'false\'\u003e\u003cnick\u003ewillherschel\u003c/nick\u003e\u003c/conference\u003e\u003c/storage\u003e"}' }
end
after do
clear_enqueued_jobs
clear_performed_jobs
end
end

View File

@@ -101,38 +101,23 @@ RSpec.describe User, type: :model do
end end
end end
describe "#exchange_xmpp_contact_with_inviter" do
include ActiveJob::TestHelper
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
let(:guest) { create :user, id: 2, cn: "isaacnewton", ou: "kosmos.org", email: "newt@example.com" }
before do
Invitation.create! user: user, invited_user_id: guest.id, used_at: DateTime.now
allow_any_instance_of(User).to receive(:services_enabled).and_return(%w[ ejabberd ])
end
it "enqueues a job to exchange XMPP contacts between inviter and invitee" do
guest.send(:exchange_xmpp_contact_with_inviter)
expect(enqueued_jobs.size).to eq(1)
args = enqueued_jobs.first['arguments']
expect(args[0]['_aj_globalid']).to match('gid://akkounts/User')
expect(args[1]).to eq('isaacnewton')
expect(args[2]).to eq('kosmos.org')
end
after do
clear_enqueued_jobs
end
end
describe "#devise_after_confirmation" do describe "#devise_after_confirmation" do
include ActiveJob::TestHelper
after { clear_enqueued_jobs }
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" } let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
it "enables default services" do it "enables default services" do
expect(user).to receive(:enable_service).with(%w[ discourse ejabberd gitea mediawiki ]) expect(user).to receive(:enable_service).with(%w[ discourse ejabberd gitea mediawiki ])
user.send(:devise_after_confirmation) user.send :devise_after_confirmation
end
it "enqueues a job to set default chatroom bookmarks for XMPP" do
allow(user).to receive(:enable_service).and_return(true)
user.send :devise_after_confirmation
job = enqueued_jobs.select{|j| j['job_class'] == "XmppSetDefaultBookmarksJob"}.first
expect(job['arguments'][0]['_aj_globalid']).to eq('gid://akkounts/User/1')
end end
context "for invited user with ejabberd enabled" do context "for invited user with ejabberd enabled" do
@@ -140,12 +125,15 @@ RSpec.describe User, type: :model do
before do before do
Invitation.create! user: user, invited_user_id: guest.id, used_at: DateTime.now Invitation.create! user: user, invited_user_id: guest.id, used_at: DateTime.now
allow_any_instance_of(User).to receive(:enable_service).and_return(true) allow_any_instance_of(User).to receive(:enable_service)
end end
it "exchanges XMPP contacts with the inviter" do it "enqueues jobs to exchange XMPP contacts between inviter and invitee" do
expect(guest).to receive(:exchange_xmpp_contact_with_inviter) guest.send :devise_after_confirmation
guest.send(:devise_after_confirmation)
job = enqueued_jobs.select{|j| j['job_class'] == "XmppExchangeContactsJob"}.first
expect(job["arguments"][0]['_aj_globalid']).to eq('gid://akkounts/User/1')
expect(job["arguments"][1]['_aj_globalid']).to eq('gid://akkounts/User/2')
end end
end end
end end