changing timeline filters works

This commit is contained in:
Iris Lightshard 2024-12-06 19:25:58 -07:00
parent 03ee058fe7
commit bbf26561f6
Signed by: Iris Lightshard
GPG key ID: 688407174966CAF3
10 changed files with 100 additions and 5 deletions

View file

@ -6,6 +6,7 @@ import (
type Adapter interface {
Init(Settings, *chan SocketData) error
Stop()
Name() string
Subscribe(string, *string) []error
Fetch(string, []string) error

View file

@ -101,13 +101,19 @@ func (self *HonkAdapter) Init(settings Settings, data *chan SocketData) error {
self.token = string(buf[:])
fmt.Println(self.token)
self.stop = make(chan bool)
return nil
}
func (self *HonkAdapter) Stop() {
close(self.stop)
}
func (self *HonkAdapter) Subscribe(filter string, target *string) []error {
if self.stop != nil {
close(self.stop)
self.maxId = 0
}
self.stop = make(chan bool)
@ -117,14 +123,18 @@ func (self *HonkAdapter) Subscribe(filter string, target *string) []error {
}
func (self *HonkAdapter) gethonks(filter string, target *string) {
x := self.stop
self.maxId = 0
for {
select {
case _, ok := <-self.stop:
case _, ok := <-x:
if !ok {
fmt.Println("stopping! filter was " + filter)
return
}
default:
fmt.Println("getting honks, filter is " + filter)
honkForm := url.Values{
"action": []string{"gethonks"},
"token": []string{self.token},

View file

@ -50,6 +50,10 @@ func (self *MastoAdapter) Init(settings Settings, data *chan SocketData) error {
return err
}
func (self *MastoAdapter) Stop() {
close(self.stop)
}
func (self *MastoAdapter) Subscribe(filter string, target *string) []error {
// TODO: decode separate timelines and hashtags
// for now, the filter is just the timeline
@ -71,7 +75,8 @@ func (self *MastoAdapter) Subscribe(filter string, target *string) []error {
return []error{err}
}
go func() {
for e := range self.events {
ee := self.events
for e := range ee {
log.Printf("event: %s !!!", e.Event)
switch e.Event {
case "error":

View file

@ -45,6 +45,10 @@ func (self *MisskeyAdapter) Name() string {
return self.nickname
}
func (self *MisskeyAdapter) Stop() {
close(self.stop)
}
func (self *MisskeyAdapter) Init(settings Settings, data *chan SocketData) error {
log.Print("initializing misskey adapter")
@ -102,9 +106,11 @@ func (self *MisskeyAdapter) poll() {
var notesService *n.Service
var timelineService *tl.Service
x := self.stop
for {
select {
case _, ok := <-self.stop:
case _, ok := <-x:
if !ok {
return
}

View file

@ -48,6 +48,11 @@ func (self *NostrAdapter) Init(settings Settings, data *chan SocketData) error {
return nil
}
func (self *NostrAdapter) Stop() {
// nostr has native streaming so we don't need to do anything in this method
return
}
func (self *NostrAdapter) Subscribe(filter string, target *string) []error {
var filters nostr.Filters
err := json.Unmarshal([]byte(filter), &filters)

View file

@ -16,6 +16,7 @@
<main>
<div class='uicolumn'>
<details open><summary>settings</summary><div id="settings_parent"></div></details>
<details open><summary>timeline-select</summary><div id="timeline_filter_parent"></div></details>
<details open><summary>timeline</summary><div id="timeline_parent"></div></details>
</div>
<div class='uicolumn'>

View file

@ -7,6 +7,7 @@ import { SettingsElement } from "./settings-element"
import { ProfileElement } from "./profile-element"
import { AuthorMessagesElement } from "./author-messages-element"
import { TimelineElement } from "./timeline-element"
import { TimelineFilterElement } from "./timeline-filter-element"
import {DatagramSocket} from "./websocket"
function main() {
@ -18,6 +19,7 @@ function main() {
customElements.define("underbbs-profile", ProfileElement);
customElements.define("underbbs-author-messages", AuthorMessagesElement);
customElements.define("underbbs-timeline", TimelineElement);
customElements.define("underbbs-timeline-filter", TimelineFilterElement);
util._("closeErr", util.closeErr);
@ -40,6 +42,11 @@ function main() {
if (timelineParent) {
timelineParent.innerHTML = "<underbbs-timeline id='honkstream' data-adapter='honk' data-target='home' data-gateway=''></underbbs-timeline>";
}
let timelineSelectParent = util.$("timeline_filter_parent");
if (timelineSelectParent) {
timelineSelectParent.innerHTML = "<underbbs-timeline-filter data-target='honkstream' data-filters='home::home/atme::atme'></underbbs-timeline-filter>";
}
}
main();

View file

@ -32,6 +32,7 @@ export class TimelineElement extends HTMLElement {
}
this._timeline = next;
this.innerHTML = `<ul class="messages_list"></ul>`;
this._messages = [];
if (this._subscriber) {
this._subscriber.subscribe(next);
}

View file

@ -0,0 +1,55 @@
export class TimelineFilter {
public name: string = "";
public filter: string = "";
}
export class TimelineFilterElement extends HTMLElement {
static observedAttributes = [ "data-filters", "data-target", "data-latest" ];
private _filters: TimelineFilter[] = [];
private _target: string = "";
constructor() {
super();
this.innerHTML = "<select class='filter_select'></select>"
}
connectedCallback() {
this._target = this.getAttribute("data-target") ?? "";
this._filters = (this.getAttribute("data-filters") ?? "").split("/").map(f=>{
const ff = f.split("::");
return <TimelineFilter>{ name: ff[0], filter: ff[1] };
});
let html = "<select class='filter_select'>";
html += this._filters.reduce((self: string, f: TimelineFilter)=>{
self += `<option value='${f.filter}'>${f.name}</option>`;
return self;
}, "");
html += "</select>";
this.innerHTML = html;
const select = this.querySelector(".filter_select");
if (select) {
select.addEventListener("change", this.onChanges.bind(this));
}
}
onChanges() {
const select = this.querySelector(".filter_select") as HTMLSelectElement;
// change target's target!
const target = document.getElementById(this._target);
if (target) {
target.setAttribute("data-target", select?.options[select.selectedIndex].value)
}
}
attributeChangedCallback(attr: string, prev: string, next: string) {
switch (attr) {
case "data-latest":
const select = this.querySelector(".filter_select") as HTMLSelectElement;
select.selectedIndex = this._filters.findIndex(f=>f.name == next);
break;
}
}
}

View file

@ -97,6 +97,10 @@ func (self *BBSServer) subscribeHandler(w http.ResponseWriter, r *http.Request)
case <-ctx.Done():
self.logf("subscriber has disconnected")
for _, a := range self.subscribers[s] {
// keeps any adapter's subscription from trying to send on the data channel after we close it
a.Stop()
}
close(s.data)
return //ctx.Err()
}