Skip to content
This repository was archived by the owner on Jul 3, 2023. It is now read-only.
Draft
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
3 changes: 1 addition & 2 deletions app/aggregates/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ def initialize(id, events)
end

apply MemberAdded do |event|
username = event.body['username']
write_attributes(
added: true,
handle: Handle.new(username),
handle: event.body['handle'],
email: event.body['email'],
name: event.body['name']
)
Expand Down
22 changes: 22 additions & 0 deletions app/aggregates/peer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Aggregates
##
# A +Peer+ is a remote actor.
class Peer
include EventSourcery::AggregateRoot

AWAIT_SYNCING_VALUE = 'Synching...'

attr_reader :name, :bio

def initialize(id, events)
@name = AWAIT_SYNCING_VALUE
@bio = AWAIT_SYNCING_VALUE
super(id, events)
end

apply PeerSynched do
end
end
end
6 changes: 3 additions & 3 deletions app/aggregates/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize(*arguments)
end

apply RegistrationRequested do |event|
write_attributes(event.body.slice('username', 'password', 'email'))
write_attributes(event.body.slice('handle', 'password', 'email'))
end

apply RegistrationConfirmed do
Expand Down Expand Up @@ -69,8 +69,8 @@ def password
attributes[:password]
end

def username
attributes[:username]
def handle
attributes[:handle]
end
end
end
5 changes: 1 addition & 4 deletions app/commands/application_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ def uuid_v5
if aggregate_id_name.empty?
''
else
UUIDTools::UUID.sha1_create(
aggregate_id_namespace,
aggregate_id_name
).to_s
UUIDGen.uuid(aggregate_id_namespace, aggregate_id_name).to_s
end
end

Expand Down
12 changes: 5 additions & 7 deletions app/commands/registration/new_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ module NewRegistration
class Command < ApplicationCommand
include BCrypt

UUID_EMAIL_NAMESPACE = UUIDTools::UUID.parse(
'2282b78c-85d6-419f-b240-0263d67ee6e6'
)

REQUIRED_PARAMS = %w[email username password].freeze
REQUIRED_PARAMS = %w[email handle password].freeze
ALLOWED_PARAMS = REQUIRED_PARAMS

# NewRegistration builds a UUIDv5 based on the mailaddress.
def initialize(params)
@payload = params.slice(*ALLOWED_PARAMS)

# overwrite the password
@payload['password'] = Password.create(@payload.delete('password'))
unless (@payload['password'] || '').empty?
@payload['password'] = Password.create(@payload.delete('password'))
end

@aggregate_id = aggregate_id
end
Expand All @@ -47,7 +45,7 @@ def aggregate_id_name
end

def aggregate_id_namespace
UUID_EMAIL_NAMESPACE
UUIDGen::NS_EMAIL
end
end

Expand Down
11 changes: 4 additions & 7 deletions app/commands/session/start.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ module Start
# with e.g. a rate limiter, or notification mail or so.
class Command < ApplicationCommand
include BCrypt
UUID_USERNAME_NAMESPACE = UUIDTools::UUID.parse(
'fb0f6f73-a16d-4032-b508-16519fb4a73a'
)
DEFAULT_PARAMS = { 'username' => '', 'password' => '' }.freeze
DEFAULT_PARAMS = { 'handle' => '', 'password' => '' }.freeze

def initialize(params, projection: Projections::Members::Query)
@payload = DEFAULT_PARAMS.merge(params).slice(*DEFAULT_PARAMS.keys)
Expand Down Expand Up @@ -47,15 +44,15 @@ def payload
attr_reader :projection

def aggregate_id_name
@payload['username']
@payload['handle']
end

def aggregate_id_namespace
UUID_USERNAME_NAMESPACE
UUIDGen::NS_USERNAME
end

def member
@member ||= projection.find_by(username: @payload['username']) || {}
@member ||= projection.find_by(handle: @payload['handle']) || {}
end
end

Expand Down
5 changes: 0 additions & 5 deletions app/events/confirmation_email_sent.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/events/contact_added.rb

This file was deleted.

57 changes: 57 additions & 0 deletions app/events/events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

##
# A confirmation email has been sent
ConfirmationEmailSent = Class.new(EventSourcery::Event)

##
# A Contact was added to a Member
# +aggregate_id+, UUID of the contact aggregate
# +body.owner_id+, UUID of the member owning the contact: the contact shows
# up in this member's contacts
# +body.handle+, Handle, the handle of the contact to be added.
ContactAdded = Class.new(EventSourcery::Event)

##
# A follower was added to a member
FollowerAdded = Class.new(EventSourcery::Event)

##
# A Member was added
MemberAdded = Class.new(EventSourcery::Event)

##
# A profile bio was updated
MemberBioUpdated = Class.new(EventSourcery::Event)

##
# A Member was invited
MemberInvited = Class.new(EventSourcery::Event)

##
# A profile bio was updated
MemberNameUpdated = Class.new(EventSourcery::Event)

##
# A Member was Tagged
MemberTagAdded = Class.new(EventSourcery::Event)

##
# Fetching of Details for peer from remote server requested.
PeerFetchRequested = Class.new(EventSourcery::Event)

##
# Fetching of Details for peer finished
PeerSynched = Class.new(EventSourcery::Event)

##
# A Registration is confirmed
RegistrationConfirmed = Class.new(EventSourcery::Event)

##
# A visitor has registered: reguested a new registration
RegistrationRequested = Class.new(EventSourcery::Event)

##
# A Logged in session was started
SessionStarted = Class.new(EventSourcery::Event)
5 changes: 0 additions & 5 deletions app/events/follower_added.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/events/member_added.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/events/member_bio_updated.rb

This file was deleted.

6 changes: 0 additions & 6 deletions app/events/member_invited.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/events/member_name_updated.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/events/member_tag_added.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/events/registration_confirmed.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/events/registration_requested.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/events/session_started.rb

This file was deleted.

6 changes: 1 addition & 5 deletions app/projections/members/projector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ class Projector
table :members do
column :member_id, 'UUID NOT NULL'
column :handle, :text, null: false
column :username, :text
column :password, :text
end

project MemberAdded do |event|
username = event.body['username']

table.insert(
member_id: event.aggregate_id,
handle: Handle.new(username).to_s,
username: username,
handle: event.body['handle'],
password: event.body['password']
)
end
Expand Down
4 changes: 2 additions & 2 deletions app/projections/members/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Members
# Query the Members projection with helpers that return
# Sequel collection objects.
class Query
def self.find_by(username:)
collection.first(username: username)
def self.find_by(handle:)
collection.first(handle: handle)
end

def self.find(id)
Expand Down
23 changes: 23 additions & 0 deletions app/reactors/contact_fetcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Reactors
##
# Adds an actor to a members' followers on various Events
class ContactFetcher
include EventSourcery::Postgres::Reactor

processor_name :contact_fetcher

emits_events PeerSynched

process PeerFetchRequested do |event|
emit_event(
PeerSynched.new(
aggregate_id: event.aggregate_id,
body: event.body,
causation_id: event.uuid
)
)
end
end
end
10 changes: 10 additions & 0 deletions app/web/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class ApplicationController < Sinatra::Base
# :nocov:
end

helpers do
def domain
Roost.config.domain
end
end

protected

def requires_authorization
Expand All @@ -29,6 +35,10 @@ def authorize(&block)
raise Unauthorized unless block.call
end

def authorized?
current_member.active?
end

def current_member
return OpenStruct.new(active?: false) unless member_id

Expand Down
22 changes: 15 additions & 7 deletions app/web/controllers/web/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Web
##
# Handles contacts index and addition
class ContactsController < WebController
include LoadHelpers
include PolicyHelpers

# Index
Expand All @@ -16,27 +17,34 @@ class ContactsController < WebController

# Add
post '/contacts' do
requires_authorization
redirect '/remote' unless authorized?

authorize { may_add_contact? }

Commands.handle(
'Contact', 'Add',
{ 'handle' => contact.handle, 'owner_id' => current_member.member_id }
{ 'handle' => handle, 'owner_id' => current_member.member_id }
)

flash[:success] = "#{contact.handle} was added to your contacts"
redirect "/m/#{contact.handle}"
flash[:success] = "#{handle} was added to your contacts"
redirect "/m/#{handle}"
end

private

def contact
aggregate_id = Projections::Members::Query.aggregate_id_for(handle)
Roost.repository.load(Aggregates::Member, aggregate_id)
decorate(
load(
Aggregates::Member,
Projections::Members::Query.aggregate_id_for(handle.to_s)
),
ViewModels::Profile,
ViewModels::Profile::NullProfile
)
end

def handle
Handle.parse(params['handle'])
Handle.parse(params[:handle])
end
end
end
4 changes: 3 additions & 1 deletion app/web/controllers/web/login_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class LoginController < WebController
private

def post_params
params.slice('username', 'password')
post_params = params.slice(:password)
post_params[:handle] = Handle.new(params[:username]).to_s
post_params
end
end
end
Loading