diff --git a/README.md b/README.md index 39b5039..a7e04e0 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ example: if i'm streaming on moth11.net, my did is did:plc:25z6ogppprfvijcnqo2fsfce` if you want to see the union of alice and bob's chats, and their dids are -`did:plc:12345` and `did:plc:abcde`, then do `go run . did:plc:12345 did:plc:abcde` +`did:plc:12345` and `did:plc:abcde`, then do `go run . did:plc:12345 +did:plc:abcde` pretty simple! diff --git a/beeper.go b/beeper.go index 3ba4656..b2c3370 100644 --- a/beeper.go +++ b/beeper.go @@ -32,7 +32,9 @@ type NotificationPlayer struct { } var ( - hasArgs bool + hasArgs bool + didToHandle map[syntax.DID]*syntax.Handle + noNeedForFrom bool ) func main() { @@ -40,6 +42,9 @@ func main() { if len(os.Args) != 1 { hasArgs = true } + if len(os.Args) == 2 { + noNeedForFrom = true + } f, err := os.Open("thread_notification.wav") if err != nil { panic(err) @@ -64,6 +69,7 @@ func main() { np, tp, } + didToHandle = make(map[syntax.DID]*syntax.Handle) go consumeLoop(context.Background(), ns) ns.tp.Run() } @@ -138,15 +144,10 @@ type ChatMessage struct { func (h *handler) HandleEvent(ctx context.Context, event *models.Event) error { if event.Commit != nil && event.Commit.Collection == "place.stream.chat.message" && event.Commit.Operation == "create" { - dd := identity.DefaultDirectory() - did, err := syntax.ParseDID(event.Did) + handle, err := getHandle(event.Did, ctx) if err != nil { panic(err) } - id, err := dd.LookupDID(ctx, did) - if err != nil { - return nil - } var v ChatMessage err = json.Unmarshal(event.Commit.Record, &v) if err != nil { @@ -161,18 +162,42 @@ func (h *handler) HandleEvent(ctx context.Context, event *models.Event) error { } } if shouldSend { - h.ns.Notify(v.Text, id.Handle) + var streamer string + if !noNeedForFrom { + streamer, err = getHandle(v.Streamer, ctx) + if err != nil { + panic(err) + } + } + h.ns.Notify(v.Text, handle, streamer) } } return nil } -func (ns *NotificationSystem) Notify(text string, handle syntax.Handle) { +func getHandle(did string, ctx context.Context) (string, error) { + sdid, err := syntax.ParseDID(did) + if err != nil { + return "", err + } + h, ok := didToHandle[sdid] + if ok { + return h.String(), nil + } + dd := identity.DefaultDirectory() + id, err := dd.LookupDID(ctx, sdid) + if err != nil { + return "failed.to.lookup", nil + } + didToHandle[sdid] = &id.Handle + return id.Handle.String(), nil +} + +func (ns *NotificationSystem) Notify(text string, handle string, streamer string) { - streamer := ns.np.audioData.Streamer(0, ns.np.audioData.Len()) - speaker.Play(streamer) - h := handle.String() - ns.tp.Send(ChatMsg{text: &text, handle: &h}) + noise := ns.np.audioData.Streamer(0, ns.np.audioData.Len()) + speaker.Play(noise) + ns.tp.Send(ChatMsg{text: &text, handle: &handle, streamer: &streamer}) } type model struct { @@ -182,8 +207,9 @@ type model struct { } type record struct { - handle *string - text *string + handle *string + text *string + streamer *string } func initialModel() model { @@ -200,8 +226,9 @@ func (m model) Init() tea.Cmd { //15191e type ChatMsg struct { - handle *string - text *string + handle *string + text *string + streamer *string } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { @@ -216,8 +243,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.height = msg.Height case ChatMsg: record := record{ - text: msg.text, - handle: msg.handle, + text: msg.text, + handle: msg.handle, + streamer: msg.streamer, } m.records = append(m.records, &record) } @@ -229,15 +257,20 @@ func (m model) View() string { for _, record := range m.records { str := "invalid handle" if record.handle != nil { - str = fmt.Sprintf("%s ", *record.handle) + str = fmt.Sprintf("%s", *record.handle) } bold := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color(hashStringToColor(str))) bdy := "" if record.text != nil { bdy = fmt.Sprintf("%s", *record.text) } + middleText := "\n" + if !noNeedForFrom { + boldStrmr := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color(hashStringToColor(*record.streamer))) + middleText = fmt.Sprintf(" in %s's chat\n", boldStrmr.Render(*record.streamer)) + } style := lipgloss.NewStyle().Width(m.width) - s = s + "\n" + bold.Render(str) + "\n" + style.Render(bdy) + "\n" + s = s + "\n" + bold.Render(str) + middleText + style.Render(bdy) + "\n" } return s }