-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathChatModel.java
More file actions
114 lines (99 loc) · 4.15 KB
/
Copy pathChatModel.java
File metadata and controls
114 lines (99 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.example.model;
import com.example.network.ChatNetworkClient;
import com.example.network.NtfyHttpClient;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.io.File;
public class ChatModel {
private final ObservableList<NtfyMessage> messages = FXCollections.observableArrayList();
private final ChatNetworkClient networkClient;
private ChatNetworkClient.Subscription subscription;
private final String baseUrl;
private final String topic;
/**
* Constructs a ChatModel, initializing the network client, selecting the NTFY base URL,
* setting the topic to "myChatTopic", and establishing the subscription.
*
* <p>The base URL is chosen in this order of precedence: the system property
* "NTFY_BASE_URL", the environment variable "NTFY_BASE_URL", and finally the default
* "http://localhost:8080". This constructor prints the chosen URL and calls {@code connect()}
* to begin receiving messages.
*/
public ChatModel() {
this.networkClient = new NtfyHttpClient();
String url = System.getProperty("NTFY_BASE_URL");
if (url == null || url.isBlank()) {
url = System.getenv("NTFY_BASE_URL");
}
if (url == null || url.isBlank()) {
url = "http://localhost:8080";
}
this.baseUrl = url;
this.topic = "myChatTopic";
System.out.println("Using NTFY URL: " + this.baseUrl);
connect();
}
/**
* Provides the observable list of chat messages.
*
* @return the observable list of NtfyMessage instances; changes to this list are observable and reflect the model's current messages
*/
public ObservableList<NtfyMessage> getMessages() {
return messages;
}
/**
* Establishes a subscription to the configured Ntfy topic and begins receiving messages.
*
* Incoming messages are appended to the model's observable messages list on the JavaFX
* application thread. Subscription errors are written to standard error.
*/
public void connect() {
this.subscription = networkClient.subscribe(
baseUrl,
topic,
msg -> Platform.runLater(() -> messages.add(msg)),
error -> System.err.println("Error: " + error.getMessage())
);
}
/**
* Publish a text message to the configured chat topic.
*
* @param text the message content to send
* @throws Exception if sending the message fails
*/
public void sendMessage(String text) throws Exception {
NtfyMessage message = new NtfyMessage(topic, text);
networkClient.send(baseUrl, message);
}
/**
* Sends the given file to the configured chat topic and posts a chat message describing the file.
*
* Validates that the file exists, uploads it via the network client, then sends a message containing
* the file name and a human-readable file size.
*
* @param file the file to send; must be non-null and exist on disk
* @throws IllegalArgumentException if {@code file} is null or does not exist
* @throws Exception if the upload or subsequent message send fails
*/
public void sendFile(File file) throws Exception {
if (file == null || !file.exists()) {
throw new IllegalArgumentException("File not found");
}
networkClient.sendFile(baseUrl, topic, file);
String fileMessage = "📎 " + file.getName() + " (" + formatFileSize(file.length()) + ")";
sendMessage(fileMessage);
}
/**
* Format a byte count into a human-readable string using B, KB, MB, or GB.
*
* @param size the size in bytes
* @return a formatted size string (e.g., "512 B", "1.5 KB", "2.0 MB", or "3.2 GB")
*/
private String formatFileSize(long size) {
if (size < 1024) return size + " B";
if (size < 1024 * 1024) return String.format("%.1f KB", size / 1024.0);
if (size < 1024 * 1024 * 1024) return String.format("%.1f MB", size / (1024.0 * 1024));
return String.format("%.1f GB", size / (1024.0 * 1024 * 1024));
}
}