Skip to content

Commit fcc0a14

Browse files
authored
Merge pull request #1334 from codidact/art/email-logs
Add email logs
2 parents 719a8f0 + c0b8911 commit fcc0a14

14 files changed

Lines changed: 98 additions & 9 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ gem 'will_paginate-bootstrap', '~> 1.0'
4141

4242
# AWS for S3 (image storage) and SES (emails).
4343
gem 'aws-sdk-s3', '~> 1.61', require: false
44+
gem 'aws-sdk-sns', '~> 1.72'
4445
gem 'aws-ses-v4', require: 'aws/ses'
4546

4647
# Task scheduler.

Gemfile.lock

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ GEM
7070
public_suffix (>= 2.0.2, < 6.0)
7171
ast (2.4.2)
7272
awesome_print (1.9.2)
73-
aws-eventstream (1.2.0)
74-
aws-partitions (1.628.0)
75-
aws-sdk-core (3.145.0)
76-
aws-eventstream (~> 1, >= 1.0.2)
77-
aws-partitions (~> 1, >= 1.525.0)
78-
aws-sigv4 (~> 1.1)
73+
aws-eventstream (1.3.0)
74+
aws-partitions (1.908.0)
75+
aws-sdk-core (3.191.6)
76+
aws-eventstream (~> 1, >= 1.3.0)
77+
aws-partitions (~> 1, >= 1.651.0)
78+
aws-sigv4 (~> 1.8)
7979
jmespath (~> 1, >= 1.6.1)
8080
aws-sdk-kms (1.58.0)
8181
aws-sdk-core (~> 3, >= 3.127.0)
@@ -84,12 +84,15 @@ GEM
8484
aws-sdk-core (~> 3, >= 3.127.0)
8585
aws-sdk-kms (~> 1)
8686
aws-sigv4 (~> 1.4)
87+
aws-sdk-sns (1.72.0)
88+
aws-sdk-core (~> 3, >= 3.191.0)
89+
aws-sigv4 (~> 1.1)
8790
aws-ses-v4 (0.8.1)
8891
builder
8992
mail (> 2.2.5)
9093
mime-types
9194
xml-simple
92-
aws-sigv4 (1.5.1)
95+
aws-sigv4 (1.8.0)
9396
aws-eventstream (~> 1, >= 1.0.2)
9497
bcrypt (3.1.18)
9598
bindex (0.8.1)
@@ -391,6 +394,7 @@ PLATFORMS
391394
DEPENDENCIES
392395
awesome_print (~> 1.9)
393396
aws-sdk-s3 (~> 1.61)
397+
aws-sdk-sns (~> 1.72)
394398
aws-ses-v4
395399
byebug (~> 11.1)
396400
capybara (~> 3.38)
@@ -452,4 +456,4 @@ RUBY VERSION
452456
ruby 2.7.6p219
453457

454458
BUNDLED WITH
455-
2.4.15
459+
2.4.13
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class EmailLogsController < ApplicationController
2+
skip_forgery_protection only: [:log]
3+
skip_before_action :set_globals, only: [:log]
4+
skip_before_action :distinguish_fake_community, only: [:log]
5+
skip_before_action :enforce_signed_in, only: [:log]
6+
7+
def log
8+
message_type = request.headers['X-Amz-SNS-Message-Type']
9+
if ['SubscriptionConfirmation', 'Notification'].include? message_type
10+
verifier = Aws::SNS::MessageVerifier.new
11+
body = request.body.read
12+
if verifier.authentic? body
13+
aws_data = JSON.parse body
14+
message_data = JSON.parse aws_data['Message']
15+
log_type = message_data['notificationType']
16+
destination = message_data['mail']['destination'].join(', ')
17+
EmailLog.create(log_type: log_type, destination: destination, data: aws_data['Message'])
18+
render plain: 'OK'
19+
else
20+
render plain: "You're not AWS. Go away.", status: 401
21+
end
22+
end
23+
end
24+
end

app/helpers/email_logs_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module EmailLogsHelper
2+
end

app/models/email_log.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class EmailLog < ApplicationRecord
2+
end

config/environments/development.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
# Store uploaded files on the local file system (see config/storage.yml for options).
3535
config.active_storage.service = :local
3636

37+
# Allow ngrok connections to dev server
38+
config.hosts << /[a-z0-9-.]+\.ngrok-free\.app/
39+
3740
# Don't care if the mailer can't send.
3841
config.action_mailer.raise_delivery_errors = false
3942
config.action_mailer.delivery_method = :ses

config/routes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@
348348
end
349349
end
350350

351+
scope 'emails' do
352+
post 'log', to: 'email_logs#log', as: :create_email_log
353+
end
354+
351355
get '403', to: 'errors#forbidden'
352356
get '404', to: 'errors#not_found'
353357
get '409', to: 'errors#conflict'

config/schedule.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
every 6.hours do
1010
runner 'scripts/recalc_abilities.rb'
1111
end
12+
13+
every 1.day, at: '02:10' do
14+
runner 'scripts/prune_email_logs.rb'
15+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateEmailLogs < ActiveRecord::Migration[7.0]
2+
def change
3+
create_table :email_logs do |t|
4+
t.string :log_type
5+
t.string :destination
6+
t.text :data
7+
8+
t.timestamps
9+
end
10+
end
11+
end

db/schema.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.0].define(version: 2023_08_17_213150) do
13+
ActiveRecord::Schema[7.0].define(version: 2024_04_05_113618) do
1414
create_table "abilities", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
1515
t.bigint "community_id"
1616
t.string "name"
@@ -231,6 +231,14 @@
231231
t.index ["user_id"], name: "index_community_users_on_user_id"
232232
end
233233

234+
create_table "email_logs", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
235+
t.string "log_type"
236+
t.string "destination"
237+
t.text "data"
238+
t.datetime "created_at", null: false
239+
t.datetime "updated_at", null: false
240+
end
241+
234242
create_table "error_logs", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
235243
t.bigint "community_id"
236244
t.bigint "user_id"

0 commit comments

Comments
 (0)