feat(chat): message bookmarks

This commit is contained in:
Daniel Kempkens 2023-08-21 13:40:48 +02:00
parent 8900987e2c
commit 4b4c01bb14
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM
3 changed files with 38 additions and 1 deletions

View file

@ -12,6 +12,7 @@ defmodule BdfrBrowser.Message do
field :author, :string field :author, :string
field :message, :string field :message, :string
field :posted_at, :utc_datetime field :posted_at, :utc_datetime
field :bookmark, :string
belongs_to :chat, Chat belongs_to :chat, Chat
end end

View file

@ -0,0 +1,9 @@
defmodule BdfrBrowser.Repo.Migrations.AddChatMessageBookmarks do
use Ecto.Migration
def change do
alter table("messages") do
add :bookmark, :string
end
end
end

View file

@ -1,7 +1,12 @@
<h2>Chats</h2> <h2>Chats</h2>
<%= for message <- messages do %> <%= for message <- messages do %>
<div class="row" style="margin-bottom: 2px;"> <div class="row" style="margin-bottom: 2px;"
id="msg-<%= message.id %>"
<%= unless is_nil(message.bookmark) do %>
data-bookmark="<%= message.bookmark %>"
<% end %>
>
<div class="card"> <div class="card">
<div class="card-body" style="padding: 8px;"> <div class="card-body" style="padding: 8px;">
<blockquote class="blockquote mb-0" style="font-size: 1rem;"> <blockquote class="blockquote mb-0" style="font-size: 1rem;">
@ -10,9 +15,31 @@
<footer class="blockquote-footer"> <footer class="blockquote-footer">
<a href="/user/<%= message.author %>"><%= message.author %></a>, <a href="/user/<%= message.author %>"><%= message.author %></a>,
<small><%= DateTime.to_iso8601(message.posted_at) %></small> <small><%= DateTime.to_iso8601(message.posted_at) %></small>
<%= unless is_nil(message.bookmark) do %>
<span class="badge text-bg-secondary"><%= message.bookmark %></span>
<% end %>
</footer> </footer>
</blockquote> </blockquote>
</div> </div>
</div> </div>
</div> </div>
<% end %> <% end %>
<script>
const bookmarks = document.querySelectorAll('[data-bookmark]');
const header = document.getElementsByTagName('h2')[0];
let container = document.createElement('p');
for (var i = 0; i < bookmarks.length; i++) {
let bookmarkElement = document.createElement('a');
bookmarkElement.href = `#${bookmarks[i].id}`;
bookmarkElement.innerText = bookmarks[i].dataset.bookmark;
bookmarkElement.className = "btn btn-secondary btn-sm";
bookmarkElement.setAttribute('role', 'button');
container.appendChild(bookmarkElement);
}
header.after(container);
</script>