mirror of
https://gitlab.com/biskuteri-cafe/JKomasto2.git
synced 2024-11-20 06:44:51 +01:00
174 lines
3.8 KiB
Java
Executable File
174 lines
3.8 KiB
Java
Executable File
|
|
import java.util.List;
|
|
import java.util.ArrayList;
|
|
import java.io.IOException;
|
|
import cafe.biskuteri.hinoki.Tree;
|
|
|
|
class
|
|
TimelineWindowUpdater {
|
|
|
|
private JKomasto
|
|
primaire;
|
|
|
|
private MastodonApi
|
|
api;
|
|
|
|
// - -%- -
|
|
|
|
private List<TimelineWindow>
|
|
updatees;
|
|
|
|
private StringBuilder
|
|
event, data;
|
|
|
|
// - -%- -
|
|
|
|
private Thread
|
|
federated,
|
|
local,
|
|
home;
|
|
|
|
// ---%-@-%---
|
|
|
|
public void
|
|
addWindow(TimelineWindow updatee)
|
|
{
|
|
updatees.add(updatee);
|
|
|
|
Connection c = new Connection();
|
|
c.type = updatee.getTimelineType();
|
|
Thread t = new Thread(c);
|
|
switch (c.type) {
|
|
case FEDERATED:
|
|
if (federated != null) return;
|
|
federated = t; break;
|
|
case LOCAL:
|
|
if (local != null) return;
|
|
local = t; break;
|
|
case HOME:
|
|
if (home != null) return;
|
|
home = t; break;
|
|
default: return;
|
|
}
|
|
t.start();
|
|
|
|
System.err.println(t);
|
|
}
|
|
|
|
public void
|
|
removeWindow(TimelineWindow updatee)
|
|
{
|
|
updatees.remove(updatee);
|
|
}
|
|
|
|
// - -%- -
|
|
|
|
private void
|
|
handle(TimelineType type, String event, String data)
|
|
{
|
|
System.err.println("Handling " + event + ".");
|
|
|
|
assert !data.isEmpty();
|
|
if (event.isEmpty()) return;
|
|
|
|
boolean newPost = event.equals("update");
|
|
boolean newNotif = event.equals("notification");
|
|
if (!(newPost || newNotif)) return;
|
|
|
|
for (TimelineWindow updatee: filter(type)) {
|
|
System.err.println("Refreshing " + updatee);
|
|
updatee.showLatestPage();
|
|
}
|
|
}
|
|
|
|
private List<TimelineWindow>
|
|
filter(TimelineType type)
|
|
{
|
|
List<TimelineWindow> returnee = new ArrayList<>();
|
|
for (TimelineWindow updatee: updatees)
|
|
if (updatee.getTimelineType() == type)
|
|
returnee.add(updatee);
|
|
return returnee;
|
|
}
|
|
|
|
// ---%-@-%---
|
|
|
|
private class
|
|
Connection
|
|
implements Runnable, ServerSideEventsListener {
|
|
|
|
private TimelineType
|
|
type;
|
|
|
|
// -=-
|
|
|
|
private StringBuilder
|
|
event, data;
|
|
|
|
// -=%=-
|
|
|
|
public void
|
|
run()
|
|
{
|
|
event = new StringBuilder();
|
|
data = new StringBuilder();
|
|
api.monitorTimeline(type, this);
|
|
// monitorTimeline should not return
|
|
// until the connection is closed.
|
|
System.err.println("Finit.");
|
|
}
|
|
|
|
public void
|
|
lineReceived(String line)
|
|
{
|
|
System.err.println("Line: " + line);
|
|
|
|
if (line.startsWith(":")) return;
|
|
|
|
if (line.isEmpty()) {
|
|
handle(type, event.toString(), data.toString());
|
|
event.delete(0, event.length());
|
|
data.delete(0, event.length());
|
|
}
|
|
|
|
if (line.startsWith("data: "))
|
|
data.append(line.substring("data: ".length()));
|
|
|
|
if (line.startsWith("event: "))
|
|
event.append(line.substring("event: ".length()));
|
|
|
|
/*
|
|
* Note that I utterly ignore https://html.spec.whatwg.org
|
|
* /multipage/server-sent-events.html#dispatchMessage.
|
|
* That is because I am not a browser.
|
|
*/
|
|
}
|
|
|
|
public void
|
|
connectionFailed(IOException eIo)
|
|
{
|
|
// sais pas dois-je faire..
|
|
eIo.printStackTrace();
|
|
}
|
|
|
|
public void
|
|
requestFailed(int httpCode, Tree<String> json)
|
|
{
|
|
// mo shiranu
|
|
System.err.println(httpCode + ", " + json.get("error").value);
|
|
}
|
|
|
|
}
|
|
|
|
// ---%-@-%---
|
|
|
|
TimelineWindowUpdater(JKomasto primaire)
|
|
{
|
|
this.primaire = primaire;
|
|
this.api = primaire.getMastodonApi();
|
|
|
|
this.updatees = new ArrayList<>();
|
|
}
|
|
|
|
}
|