Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Create broadcast, announcement, and notice #128

Merged
merged 9 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,7 @@ gem "foreman", "~> 0.87.2"
gem "mass_encryption", "~> 0.1.1"

gem "console1984"

gem "audits1984"

gem "local_time", "~> 3.0"
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ GEM
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
local_time (3.0.2)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand Down Expand Up @@ -490,6 +491,7 @@ DEPENDENCIES
foreman (~> 0.87.2)
importmap-rails (~> 1.2)
jbuilder
local_time (~> 3.0)
mass_encryption (~> 0.1.1)
mission_control-jobs (~> 0.1.1)
pg
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import "controllers"
import "@hotwired/turbo-rails"
import LocalTime from "local-time"
import "selectlist"
import("selectlist")

LocalTime.start()
15 changes: 15 additions & 0 deletions app/javascript/controllers/broadcast_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = [ "dialog" ]
connect() {
console.log("connected")
if (!localStorage.getItem(this.dialogTarget.id)) {
this.dialogTarget.addAttribute("open")
}
}

close() {
localStorage.setItem(this.dialogTarget.id, "closed")
}
}
6 changes: 6 additions & 0 deletions app/mailers/announcement_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AnnouncementMailer < ApplicationMailer
default from: "[email protected]", return_path: "[email protected]", message_steam: "broadcast"
def announce(user, subject, content)
mail(to: user.email, subject: subject, body: content)
end
end
5 changes: 5 additions & 0 deletions app/models/broadcast.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Broadcast < ApplicationRecord
def self.unexpired
select { |b| b.expires_at > DateTime.now }
end
end
9 changes: 9 additions & 0 deletions app/models/broadcast/announcement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Broadcast::Announcement < Broadcast
after_create_commit :announce

def announce
User::User.find_each do |u|
AnnouncementMailer.announce(u, subject, content).deliver_later
end
end
end
2 changes: 2 additions & 0 deletions app/models/broadcast/notice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Broadcast::Notice < Broadcast
end
34 changes: 34 additions & 0 deletions app/views/application/_broadcast.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<dialog <% if local_assigns[:open] %> open="" <% end %> class="broadcast" data-controller="broadcast" id="<%= dom_id(broadcast) %>" data-broadcast-target="dialog">
<p style="overflow-y: auto;flex-grow: 0.5;scrollbar-color: var(--lemon-glacier) rgba(251, 255, 18,0.1);word-wrap: break-word; font-size: larger;">
<%= local_time(broadcast.created_at) %>
<br>
<%= broadcast.content %>
</p>

<form method="dialog" style="width: fit-content;height: fit-content;" data-action="broadcast#close">
<button style="background: none;font-weight: bold;padding: 0;color: var(--lemon-glacier);">X</button>
</form>

</dialog>

<style>
.broadcast[open] {
position: initial;
padding: 1rem;
width: 50vw;
max-height: 15vh;
border-radius: 8px;
background: rgba(251, 255, 18,0.1);
border: 2px solid var(--lemon-glacier);
color: white;
display: flex;
justify-content: space-between;
gap: 1rem;
scrollbar-color: var(--lemon-glacier);
margin-bottom: 1rem;

time {
font-size: small;
}
}
</style>
11 changes: 11 additions & 0 deletions app/views/layouts/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
<% end %>
</div>
</div>

<noscript>
<% Broadcast.unexpired.each do |b| %>
<%= render partial: "application/broadcast", object: b, locals: { open: true } %>
<% end %>
</noscript>

<% Broadcast.unexpired.each do |b| %>
<%= render partial: "application/broadcast", object: b %>
<% end %>

<div class="px-8 md:px-16 lg:px-24 py-4">
<%= yield_nested %>
</div>
Expand Down
1 change: 1 addition & 0 deletions config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
pin_all_from "app/javascript"
pin "selectlist", to: "https://esm.sh/[email protected]", preload: true
pin "cursor-chat", to: "https://esm.sh/gh/obl-ong/cursor-chat-actioncable@9befe0089b/dist/cursor-chat.es.js"
pin "local-time", to: "https://ga.jspm.io/npm:[email protected]/app/assets/javascripts/local-time.es2017-esm.js"
11 changes: 11 additions & 0 deletions db/migrate/20240222163537_create_broadcasts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateBroadcasts < ActiveRecord::Migration[7.1]
def change
create_table :broadcasts do |t|
t.string :type
t.text :content
t.datetime :expires_at

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240222185700_add_broadcast_subject.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddBroadcastSubject < ActiveRecord::Migration[7.1]
def change
add_column :broadcasts, :subject, :string
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_02_20_173759) do
ActiveRecord::Schema[7.1].define(version: 2024_02_22_185700) do
create_table "audits1984_audits", force: :cascade do |t|
t.integer "status", default: 0, null: false
t.text "notes"
Expand All @@ -22,6 +22,15 @@
t.index ["session_id"], name: "index_audits1984_audits_on_session_id"
end

create_table "broadcasts", force: :cascade do |t|
t.string "type"
t.text "content"
t.datetime "expires_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "subject"
end

create_table "console1984_commands", force: :cascade do |t|
t.text "statements"
t.integer "sensitive_access_id"
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/broadcast/announcements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
11 changes: 11 additions & 0 deletions test/fixtures/broadcast/notices.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
11 changes: 11 additions & 0 deletions test/fixtures/broadcasts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
type:
content: MyText
expires_at: 2024-02-22 16:35:37

two:
type:
content: MyText
expires_at: 2024-02-22 16:35:37
7 changes: 7 additions & 0 deletions test/mailers/announcement_mailer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class AnnouncementMailerTest < ActionMailer::TestCase
# test "the truth" do
# assert true
# end
end
3 changes: 3 additions & 0 deletions test/mailers/previews/announcement_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Preview all emails at http://localhost:3000/rails/mailers/announcement_mailer
class AnnouncementMailerPreview < ActionMailer::Preview
end
7 changes: 7 additions & 0 deletions test/models/broadcast/announcement_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class Broadcast::AnnouncementTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/models/broadcast/notice_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class Broadcast::NoticeTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/models/broadcast_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class BroadcastTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
Loading