Skip to content

Commit

Permalink
Show warning if no relays readable. Relay connection indicator in sta…
Browse files Browse the repository at this point in the history
…tus bar. Note times are no longer static.
  • Loading branch information
ahanniga committed May 8, 2023
1 parent 0e96ac4 commit ab5be71
Show file tree
Hide file tree
Showing 27 changed files with 200 additions and 60 deletions.
48 changes: 42 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/dustin/go-humanize"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -64,6 +63,16 @@ func (a *App) startup(ctx context.Context) {
}
}
}

// Maintenance loop
go func() {
for {
a.CheckRelays()
a.PingTimer()
time.Sleep(time.Second * 10)
}
}()

log.Info().Msg("...start up done")
}

Expand Down Expand Up @@ -128,6 +137,7 @@ func (a *App) OnDomReady(ctx context.Context) {
}()
}
}
a.CheckRelays()
}

func (a *App) BeginSubscriptions() {
Expand Down Expand Up @@ -348,10 +358,20 @@ func (a *App) GetContactProfile(pk string) *Profile {
}
}

func (a *App) GetReadableRelays() []*string {
rs := []*string{}
for _, r := range a.relayPool.pool {
if r.Enabled && r.Read && (r.conn.ConnectionError == nil) {
rs = append(rs, &r.Url)
}
}
return rs
}

func (a *App) GetWritableRelays() []*string {
rs := []*string{}
for _, r := range a.relayPool.pool {
if r.Enabled && r.Write {
if r.Enabled && r.Write && (r.conn.ConnectionError == nil) {
rs = append(rs, &r.Url)
}
}
Expand Down Expand Up @@ -389,7 +409,6 @@ func (a *App) GetTextNotesForPubkeys(pks []string, postEvent string, repost bool
ch := make(chan *nostr.Event)
go func() {
for ev := range ch {
ev.SetExtra("when", humanize.Time(ev.CreatedAt.Time()))
existingEvent := db.GetEvent(ev.ID)
db.AddEvent(ev.ID, ev)
if existingEvent == nil || repost {
Expand All @@ -415,7 +434,6 @@ func (a *App) SubscribeToFeedForPubkeys(pks []string, repost bool) {
ch1 := make(chan *nostr.Event)
go func() {
for ev := range ch {
ev.SetExtra("when", humanize.Time(ev.CreatedAt.Time()))
existingEvent := db.GetEvent(ev.ID)
db.AddEvent(ev.ID, ev)
if existingEvent == nil || repost {
Expand All @@ -425,7 +443,6 @@ func (a *App) SubscribeToFeedForPubkeys(pks []string, repost bool) {
}()
go func() {
for ev := range ch1 {
ev.SetExtra("when", humanize.Time(ev.CreatedAt.Time()))
existingEvent := db.GetEvent(ev.ID)
db.AddEvent(ev.ID, ev)
if existingEvent == nil || repost {
Expand Down Expand Up @@ -456,7 +473,6 @@ func (a *App) GetTextNotesByEventIds(ids []string) []*nostr.Event {
ch := make(chan *nostr.Event)
go func() {
for ev := range ch {
ev.SetExtra("when", humanize.Time(ev.CreatedAt.Time()))
db.AddEvent(ev.ID, ev)
events = append(events, ev)
}
Expand Down Expand Up @@ -790,3 +806,23 @@ func (a *App) SaveProfile(metadata ProfileMetadata) error {
a.PostEvent(nostr.KindSetMetadata, nostr.Tags{}, string(content))
return nil
}

func (a *App) CheckRelays() {
readable := a.GetReadableRelays()
writable := a.GetWritableRelays()
numSubs := 0
for _, url := range readable {
relay := a.relayPool.GetRelayByUrl(*url)
numSubs += len(relay.subs)
}

opts := make(map[string]int)
opts["readable"] = len(readable)
opts["writable"] = len(writable)
opts["subs"] = numSubs
runtime.EventsEmit(a.ctx, "evRelayStatus", opts)
}

func (a *App) PingTimer() {
runtime.EventsEmit(a.ctx, "evTimer", time.Now().UnixMilli())
}
6 changes: 6 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"vite": "^3.0.0"
},
"dependencies": {
"humanize-duration": "^3.28.0",
"lodash": "^4.17.21"
}
}
2 changes: 1 addition & 1 deletion frontend/package.json.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
85d34d58ecc427335e7de2b1f57c5e5b
ce91643ecdbb4e57cab88302cac35cc1
48 changes: 34 additions & 14 deletions frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* caught, the user icon set, contacts refreshed and the feed loaded.
*/
import {EventsOn} from "../wailsjs/runtime/runtime.js";
import Follow from "./Follow.svelte";
import EventPost from "./EventPost.svelte";
import {
Expand All @@ -17,14 +18,16 @@
GetContactProfile,
SaveContacts,
RestoreContacts,
BeginSubscriptions
BeginSubscriptions,
GetReadableRelays
} from '../wailsjs/go/main/App.js'
import { contactStore } from './ContactStore.js'
import { eventStore, sortedEvents } from "./EventStore.js";
import nostrIcon from "./assets/images/nostr.png";
import loadingGif from "./assets/images/loading.gif";
import Dialogs from "./Dialogs.svelte";
import {EventsEmit} from "../wailsjs/runtime/runtime.js";
import StatusBar from "./StatusBar.svelte";
let pendingNotes = [];
let myPk = false;
Expand All @@ -35,13 +38,28 @@
let autoRefresh = false;
const onPkChange = (pk) => {
GetReadableRelays().then((relays)=>{
if(relays.length === 0) {
EventsEmit("evMessageDialog", {
title: "No Available Relays",
message: "No relays able to read from the network. Click OK to configure, or cancel",
iconClass: "bi-exclamation-circle",
cancelable: true,
callback: ()=>{
console.log("Callback...");
EventsEmit("evRelayDialog");
}
});
}
});
myPk = pk;
GetContactProfile(pk).then((p)=>{
myProfile = p;
BeginSubscriptions();
});
}
window.runtime.EventsOn('evPkChange', onPkChange);
EventsOn('evPkChange', onPkChange);
const onRefreshNote = (event) => {
if(autoRefresh) {
Expand All @@ -51,12 +69,12 @@
pendingNotes = pendingNotes;
}
}
window.runtime.EventsOn('evRefreshNote', onRefreshNote);
EventsOn('evRefreshNote', onRefreshNote);
const onFollowEventNote = (event) => {
addOrUpdateEvent(event);
}
window.runtime.EventsOn('evFollowEventNote', onFollowEventNote);
EventsOn('evFollowEventNote', onFollowEventNote);
const addOrUpdateEvent = (event) => {
let ev = getEventIndex(event);
Expand All @@ -71,11 +89,9 @@
for(let a = 0; a < $sortedEvents.length; a++) {
let c = $sortedEvents[a];
if(c.id === event.id) {
console.log("Got event index " + a);
return a;
}
}
console.log("Got event index -1");
return -1
}
Expand Down Expand Up @@ -105,7 +121,7 @@
$contactStore.push(profile);
}
}
window.runtime.EventsOn('evMetadata', onMetadata);
EventsOn('evMetadata', onMetadata);
const refreshFeed = () => {
if(pendingNotes.length > 0 && !filtering) {
Expand All @@ -124,16 +140,16 @@
$contactStore = [];
RefreshContactProfiles();
}
window.runtime.EventsOn('evRefreshContacts', onRefreshContacts);
EventsOn('evRefreshContacts', onRefreshContacts);
const launchPostDialog = () => {
window.runtime.EventsEmit("evPostDialog");
EventsEmit("evPostDialog");
}
const launchRelayDialog = () => {
window.runtime.EventsEmit("evRelayDialog");
EventsEmit("evRelayDialog");
}
const launchSearchContact = () => {
window.runtime.EventsEmit("evFindContactDialog");
EventsEmit("evFindContactDialog");
}
const actionQuit = (e) => {
Expand All @@ -143,7 +159,7 @@
const launchProfileCard = () => {
GetMyPubkey().then((pk)=>{
GetContactProfile(pk).then((p)=>{
window.runtime.EventsEmit("evProfileCard", p);
EventsEmit("evProfileCard", p);
});
});
}
Expand All @@ -164,7 +180,7 @@
eventStore.deleteAll();
GetTextNotesForPubkeys([profile.pk], "evFollowEventNote", true);
}
window.runtime.EventsOn('evFilterByProfile', onFilterByProfile);
EventsOn('evFilterByProfile', onFilterByProfile);
const resetFilterAndRefresh = () => {
filtering = false;
Expand Down Expand Up @@ -356,7 +372,11 @@
</div>
</div>
</div>
<div class="row flex-shrink-0 ">
<div class="row">
<!-- <div class="col-12" style="background: #0d6efd">-->
<div class="col-12 border-top mt-2 p-1 px-3">
<StatusBar />
</div>
</div>
</div>
</main>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/EventDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import {GetMyPubkey, GetTextNotesByEventIds, Nip19Decode} from "../wailsjs/go/main/App.js";
import {EventsOn} from "../wailsjs/runtime/runtime.js";
import EventPost from "./EventPost.svelte";
let myPk;
Expand Down Expand Up @@ -35,7 +36,7 @@
});
}
window.runtime.EventsOn('evEventDialog', onEventDialog);
EventsOn('evEventDialog', onEventDialog);
</script>
<style></style>
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/EventInfo.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script>
import {EventsOn} from "../wailsjs/runtime/runtime.js";
/**
* Display the raw note event in a dialog
*/
Expand All @@ -8,7 +10,7 @@
const onEventInfo = (ev) => {
event = ev;
}
window.runtime.EventsOn('evEventInfo', onEventInfo);
EventsOn('evEventInfo', onEventInfo);
</script>
<style></style>
Expand Down
17 changes: 14 additions & 3 deletions frontend/src/EventPost.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* The note text is parsed for links, images and nostr: links.
*/
import {EventsEmit} from "../wailsjs/runtime/runtime.js";
import {EventsEmit, EventsOn} from "../wailsjs/runtime/runtime.js";
import {
GetTaggedProfiles,
GetTaggedEvents,
Expand All @@ -18,6 +18,7 @@
import LookupPk from "./LookupPk.svelte";
import LookupEvent from "./LookupEvent.svelte";
import loadingGif from "./assets/images/loading.gif"
import humanizeDuration from "humanize-duration"
export let event;
Expand All @@ -27,6 +28,16 @@
let showWaiting = false;
let notFound = false;
const getWhen = (millis) => {
return humanizeDuration(Math.floor(millis - event.created_at*1000), { round: true, units: ["y", "mo", "d", "h", "m"] });
}
let when = getWhen(Date.now());
const updateWhen = (now) => {
when = getWhen(now)
}
EventsOn("evTimer", updateWhen);
const parseContent = (txt) => {
return imageParse(newlineParse(httpLinkParse(nostrNpubLinkParse(nostrEventLinkParse(txt)))));
}
Expand Down Expand Up @@ -147,7 +158,7 @@
}
const openReplyDialog = (ev) => {
window.runtime.EventsEmit("evReplyDialog", ev);
EventsEmit("evReplyDialog", ev);
}
</script>
Expand All @@ -161,7 +172,7 @@
<div class="card-body p-1 pt-3 pe-0">
<img src="{p.meta.picture}" alt="" style="width: 36px !important; height: 36px !important; min-width: 36px; min-height: 36px">
<span class="d-inline ms-2 text-body">{getDisplayName(p)}</span>
<span class="d-inline me-2 ms-3 float-end small text-body">{event.when}</span>
<span class="d-inline me-2 ms-3 float-end small text-body">{when} ago</span>

<div id="tooltip-container" style="" class="float-end text-muted">
<a href="#" data-bs-toggle="modal" data-bs-target="#profileCard" data-bs-placement="bottom" title="Contact Info" class="d-inline-block pe-2 nav-link" on:click={() => profileCard(p)}>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/FindContact.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { Nip19Decode, GetContactProfile } from "../wailsjs/go/main/App.js";
import { EventsEmit } from "../wailsjs/runtime/runtime.js";
import {EventsOn} from "../wailsjs/runtime/runtime.js";
let input;
Expand All @@ -16,7 +17,7 @@
document.getElementById('searchContact').focus();
}, 500);
}
window.runtime.EventsOn('evFindContactDialog', onFindContactDialog);
EventsOn('evFindContactDialog', onFindContactDialog);
async function getProfile(pk) {
return await GetContactProfile(pk);
Expand Down
Loading

0 comments on commit ab5be71

Please sign in to comment.