This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Post major events within a group to slack #130
Open
Connoropolous
wants to merge
6
commits into
cobudget-old:master
Choose a base branch
from
Connoropolous:post.to.slack
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8199291
everything but final wording/formatting
Connoropolous 66a1f18
finish up styling and architecture
Connoropolous e67df82
remove routing, unneeded
Connoropolous d0be6e9
remove sequenced
Connoropolous 55a1ab0
move appropriate things to env vars
Connoropolous bdde939
Merge branch 'master' into post.to.slack
Connoropolous File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class Event < ActiveRecord::Base | ||
KINDS = %w[bucket_created bucket_moved_to_funding bucket_funded] | ||
|
||
belongs_to :eventable, polymorphic: true | ||
belongs_to :group | ||
belongs_to :user | ||
|
||
scope :chronologically, -> { order('created_at asc') } | ||
|
||
after_create :notify_webhooks!, if: :group | ||
|
||
validates_inclusion_of :kind, :in => KINDS | ||
validates_presence_of :eventable | ||
|
||
def belongs_to?(this_user) | ||
self.user_id == this_user.id | ||
end | ||
|
||
def notify_webhooks! | ||
self.group.webhooks.each { |webhook| WebhookService.publish! webhook: webhook, event: self } | ||
end | ||
handle_asynchronously :notify_webhooks! | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Events::BucketCreated < Event | ||
|
||
def self.publish!(bucket, user) | ||
create!(kind: 'bucket_created', | ||
eventable: bucket, | ||
group: bucket.group, | ||
user: user) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Events::BucketFunded < Event | ||
|
||
def self.publish!(bucket, user) | ||
create!(kind: 'bucket_funded', | ||
eventable: bucket, | ||
group: bucket.group, | ||
user: user) | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Events::BucketMovedToFunding < Event | ||
|
||
def self.publish!(bucket, user) | ||
create!(kind: 'bucket_moved_to_funding', | ||
eventable: bucket, | ||
group: bucket.group, | ||
user: user) | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Webhook < ActiveRecord::Base | ||
belongs_to :hookable, polymorphic: true | ||
|
||
validates :uri, presence: true | ||
validates :hookable, presence: true | ||
validates_inclusion_of :kind, in: %w[slack] | ||
validates :event_types, length: { minimum: 1 } | ||
|
||
def headers | ||
{} | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Webhooks::Slack::Base = Struct.new(:event) do | ||
|
||
def username | ||
"Cobudget Bot" | ||
end | ||
|
||
def icon_url | ||
ENV["SLACK_ICON_URL"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be set to point to a 48x48 px image SLACK_ICON_URL=https://someimageurl.com/picture.png |
||
end | ||
|
||
def text | ||
"" | ||
end | ||
|
||
def attachments | ||
[{ | ||
title: attachment_title, | ||
color: '#00B8D4', | ||
text: attachment_text, | ||
fields: attachment_fields, | ||
fallback: attachment_fallback | ||
}] | ||
end | ||
|
||
alias :read_attribute_for_serialization :send | ||
|
||
private | ||
|
||
def view_group_on_cobudget(text = nil) | ||
"<#{url_root}groups/#{eventable.group.id}|#{text || eventable.group.name}>" | ||
end | ||
|
||
def bucket_link(text = nil) | ||
"<#{url_root}buckets/#{eventable.id}|#{text || eventable.name}>" | ||
end | ||
|
||
def url_root | ||
ENV["ROOT_URL"] + "/#/" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we will need to set this on the heroku app where the job worker will be running. should look like |
||
end | ||
|
||
def eventable | ||
@eventable ||= event.eventable | ||
end | ||
|
||
def author | ||
@author ||= eventable.author | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Webhooks::Slack::BucketCreated < Webhooks::Slack::Base | ||
|
||
def text | ||
"*#{eventable.user.name}* created a new idea in *#{view_group_on_cobudget}*" | ||
end | ||
|
||
def attachment_fallback | ||
"*#{eventable.name}*\n#{eventable.description}\n" | ||
end | ||
|
||
def attachment_title | ||
bucket_link | ||
end | ||
|
||
def attachment_text | ||
"#{eventable.description}\n#{bucket_link('Discuss it on Cobudget')}" | ||
end | ||
|
||
def attachment_fields | ||
[] | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Webhooks::Slack::BucketFunded < Webhooks::Slack::Base | ||
|
||
def text | ||
"#{eventable.group.currency_symbol}*#{eventable.target}* #{eventable.group.currency_code} bucket just got fully funded!" | ||
end | ||
|
||
def attachment_fallback | ||
"*#{eventable.name}*\n#{eventable.description}\n" | ||
end | ||
|
||
def attachment_title | ||
bucket_link | ||
end | ||
|
||
def attachment_text | ||
"#{eventable.description}\n#{bucket_link('View it on Cobudget')}" | ||
end | ||
|
||
def attachment_fields | ||
[] | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Webhooks::Slack::BucketMovedToFunding < Webhooks::Slack::Base | ||
|
||
def text | ||
"Bucket with #{eventable.group.currency_symbol}#{eventable.target} #{eventable.group.currency_code} target is now open for funding" | ||
end | ||
|
||
def attachment_fallback | ||
"*#{eventable.name}*\n#{eventable.description}\n" | ||
end | ||
|
||
def attachment_title | ||
bucket_link | ||
end | ||
|
||
def attachment_text | ||
"#{eventable.description}\n#{bucket_link('Comment or Fund it on Cobudget')}" | ||
end | ||
|
||
def attachment_fields | ||
[] | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class WebhookSerializer < ActiveModel::Serializer | ||
attributes :text, :username, :icon_url, :attachments | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class WebhookService | ||
|
||
def self.publish!(webhook:, event:) | ||
return false unless webhook.event_types.include? event.kind | ||
HTTParty.post webhook.uri, body: payload_for(webhook, event), headers: webhook.headers | ||
end | ||
|
||
private | ||
|
||
def self.payload_for(webhook, event) | ||
WebhookSerializer.new(webhook_object_for(webhook, event), root: false).to_json | ||
end | ||
|
||
def self.webhook_object_for(webhook, event) | ||
"Webhooks::#{webhook.kind.classify}::#{event.kind.classify}".constantize.new(event) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateWebhooks < ActiveRecord::Migration | ||
def change | ||
create_table :webhooks do |t| | ||
t.references :hookable, polymorphic: true, index: true | ||
t.string :kind, null: false | ||
t.string :uri, null: false | ||
t.text :event_types, array: true, default: [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. future self look |
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateEvents < ActiveRecord::Migration | ||
def change | ||
create_table :events do |t| | ||
t.string :kind, limit: 255 | ||
t.references :eventable, polymorphic: true, index: true | ||
t.references :user, index: true | ||
t.references :group, index: true | ||
t.timestamps | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 sure looks familiar :P
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.
Totally, I spun this off from loomio into metamaps land first, and then over to here. Absolutely worth noting that @gdpelican wrote a lot of this originally and should get a lot of the credit :)