New Feature: Direct Messages

This feature involved a lot of refactoring in order to get working
correctly. I wanted to continue using the timeline view for chats thus I
used alternative styling & structure for DM event kinds. This worked
create since the elements map does not care.

There is some queing that has to be done to decrypt message content thus
I allow viewing messages even if they haven't been decrypted yet. I
think this is good for transparency since you understand what is and is
not decrypted. I do think that the UX could improve, because even tho it
is fast, it's flashes on new messages.

I did not implement saving of latest messages. I will do this later, but
this feature is big enough to merge as is: an alpha state that works.

I further abstracted profile & name updating to work in a more global
manner. Additionally I rewrote code that had attribute scripts to use
addEventListener instead. This is needed to happen anyways for security
and made the codebase easier to manage.
This commit is contained in:
Thomas Mathews 2023-01-05 10:36:04 -08:00
parent 9badc35bf3
commit 077bf49fdb
17 changed files with 798 additions and 292 deletions

View file

@ -25,12 +25,13 @@ function linkify(text="", show_media=false) {
}
function format_content(ev, show_media) {
if (ev.kind === 7) {
if (ev.kind === KIND_REACTION) {
if (ev.content === "" || ev.content === "+")
return "❤️"
return html`${ev.content.trim()}`;
}
const content = ev.content.trim();
const content = (ev.kind == KIND_DM ? ev.decrypted || ev.content : ev.content)
.trim();
const body = fmt_body(content, show_media);
let cw = get_content_warning(ev.tags)
if (cw !== null) {
@ -72,8 +73,9 @@ function fmt_body(content, show_media) {
}, "")
}
/* format_profile_name takes in a profile and tries it's best to return a string
* that is best suited for the profile.
/* DEPRECATED: use fmt_name
* format_profile_name takes in a profile and tries it's best to
* return a string that is best suited for the profile.
*/
function fmt_profile_name(profile={}, fallback="Anonymous") {
const name = profile.display_name || profile.user || profile.name ||
@ -81,7 +83,23 @@ function fmt_profile_name(profile={}, fallback="Anonymous") {
return html`${name}`;
}
function fmt_name(profile={data:{}}) {
const { data } = profile;
const name = data.display_name || data.user || data.name ||
fmt_pubkey(profile.pubkey);
return html`${name}`;
}
function fmt_pubkey(pk) {
if (!pk)
return "Unknown";
return pk.slice(-8)
}
function fmt_datetime(d) {
return d.getFullYear() +
"/" + ("0" + (d.getMonth()+1)).slice(-2) +
"/" + ("0" + d.getDate()).slice(-2) +
" " + ("0" + d.getHours()).slice(-2) +
":" + ("0" + d.getMinutes()).slice(-2);
}