Share pictures and files in chat, the way TeamSpeak 6 does
A shared file goes to the sender's myTeamSpeak cloud storage, and an ordinary text message carries TeamSpeak 6's ts.file.myts pointer to it, so TeamSpeak 6 users see the file and we see theirs. Pictures show inline through the description picture cache, other files as a link to save; logged history shows them too. Sharing and fetching need a myTeamSpeak sign-in. Desktop: share button, drop or paste files and pictures, a picture viewer. Android: attach from the system picker, tap to view or save. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package com.ts3client.myts;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class ChatAttachmentTest {
|
||||
|
||||
/** As TeamSpeak 6 sends it: JSON.stringify of its object, keys in this order. */
|
||||
private static final String TS6_MESSAGE = "{\"msg_type\":\"ts.file.myts\",\"file_id\":\"YWx1aW1hZWJp\","
|
||||
+ "\"chat_user_id\":\"@agl7x5qxz7h7pqrfz7xyzedmlkbpstkbbkxw7gxltefsa3p7eexri===:chat-1.tmspk.net\","
|
||||
+ "\"file_name\":\"cat picture.PNG\",\"file_size\":532,\"body\":\"cat picture.PNG\",\"v2\":true}";
|
||||
|
||||
@Test
|
||||
void readsTheMessageTeamSpeak6Sends() {
|
||||
ChatAttachment a = ChatAttachment.parse(TS6_MESSAGE);
|
||||
assertEquals("YWx1aW1hZWJp", a.fileId());
|
||||
assertEquals("agl7x5qxz7h7pqrfz7xyzedmlkbpstkbbkxw7gxltefsa3p7eexri===", a.senderUserId());
|
||||
assertEquals("cat picture.PNG", a.fileName());
|
||||
assertEquals(532, a.fileSize());
|
||||
assertTrue(a.isImage());
|
||||
assertEquals("", a.caption());
|
||||
}
|
||||
|
||||
@Test
|
||||
void writesWhatItReads() {
|
||||
assertEquals(TS6_MESSAGE, ChatAttachment.parse(TS6_MESSAGE).toMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void namesItsSenderAsTeamSpeak6Does() {
|
||||
ChatAttachment a = ChatAttachment.of("id", "aeqlaxceldn5xjqkgnzdd3bjsxety4k46mzr7v5sd4xwtpyv5yfm2", "a.txt", 3);
|
||||
assertEquals("@aeqlaxceldn5xjqkgnzdd3bjsxety4k46mzr7v5sd4xwtpyv5yfm2===:chat-1.tmspk.net", a.senderChatId());
|
||||
assertFalse(a.isImage());
|
||||
ChatAttachment back = ChatAttachment.parse(a.toMessage());
|
||||
assertEquals(a, back);
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepsQuotesAndBracketsInNames() {
|
||||
ChatAttachment a = new ChatAttachment("f", "@u:h", "say \"hi\" [b]x[/b].gif", 1, "line\nbreak");
|
||||
assertEquals(a, ChatAttachment.parse(a.toMessage()));
|
||||
assertEquals("line\nbreak", a.caption());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoresOtherMessages() {
|
||||
assertNull(ChatAttachment.parse("hello"));
|
||||
assertNull(ChatAttachment.parse("{\"msg_type\":\"ts.file.myts\"}"));
|
||||
assertNull(ChatAttachment.parse("{\"msg_type\":\"other\",\"file_id\":\"a\",\"chat_user_id\":\"@b:c\"} ts.file.myts"));
|
||||
assertNull(ChatAttachment.parse("{\"msg_type\":\"ts.file.myts\", broken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hrefNamesEverythingNeededToFetchIt() {
|
||||
ChatAttachment a = ChatAttachment.parse(TS6_MESSAGE);
|
||||
String href = a.href("+Tyg2JtxE8vRNZp/JiUBnmBh0MY=");
|
||||
assertTrue(ChatAttachment.isHref(href));
|
||||
ChatAttachment.Ref ref = ChatAttachment.Ref.parse(href);
|
||||
assertEquals("+Tyg2JtxE8vRNZp/JiUBnmBh0MY=", ref.room());
|
||||
assertEquals(a.senderUserId(), ref.owner());
|
||||
assertEquals("YWx1aW1hZWJp", ref.fileId());
|
||||
assertEquals("cat picture.PNG", ref.fileName());
|
||||
assertFalse(href.contains(" "));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.ts3client.myts;
|
||||
|
||||
import com.ts3client.proto.ProtobufReader;
|
||||
import com.ts3client.proto.ProtobufWriter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class MyTsFilesTest {
|
||||
|
||||
/** The account replay, plus the file service: hands out made-up addresses. */
|
||||
static final class FileServer implements MyTsTransport {
|
||||
final MyTeamSpeakTest.ReplayServer accounts = new MyTeamSpeakTest.ReplayServer();
|
||||
final List<String> requests = new ArrayList<>();
|
||||
int expireNext;
|
||||
|
||||
@Override
|
||||
public byte[] call(String endpoint, String method, byte[] request) throws IOException, MyTsException {
|
||||
if (!endpoint.equals("user")) return accounts.call(endpoint, method, request);
|
||||
accounts.calls.add(endpoint + "/" + method);
|
||||
if (expireNext > 0) {
|
||||
expireNext--;
|
||||
return new ProtobufWriter().varint(2, 109).toByteArray();
|
||||
}
|
||||
ProtobufReader r = new ProtobufReader(request);
|
||||
StringBuilder fields = new StringBuilder(method);
|
||||
while (r.next()) fields.append(' ').append(r.fieldNumber()).append('=').append(r.readString());
|
||||
requests.add(fields.toString());
|
||||
return new ProtobufWriter().varint(1, 1).string(5, "file1").string(6, "https://store/" + method)
|
||||
.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
private static MyTsFiles signedIn(File dir, FileServer server) throws Exception {
|
||||
MyTeamSpeakLogin login = new MyTeamSpeakLogin(new File(dir, "myts.properties"), new MyTeamSpeak(server));
|
||||
login.signIn(MyTsCryptoTest.EMAIL, MyTsCryptoTest.PASSWORD);
|
||||
server.accounts.calls.clear();
|
||||
return new MyTsFiles(login, new MyTeamSpeak(server));
|
||||
}
|
||||
|
||||
@Test
|
||||
void asksUnderTheAccountsIdAndKeepsTheSession(@TempDir File dir) throws Exception {
|
||||
FileServer server = new FileServer();
|
||||
MyTsFiles files = signedIn(dir, server);
|
||||
|
||||
MyTsFiles.Upload upload = files.requestUpload("room=");
|
||||
assertEquals("file1", upload.fileId());
|
||||
assertEquals("aeqlaxcldiwt4t2qmfzihfffw3d5r2pqaejcgncfkztxrcm2vo6m2===", upload.senderUserId());
|
||||
assertEquals("https://store/requestDownloadFile", files.requestDownload("owner", "room=", "file9"));
|
||||
|
||||
String session = "00000000-1111-2222-3333-444444444444";
|
||||
assertEquals(List.of("requestUploadFile 1=" + session + " 2=" + upload.senderUserId() + " 3=room=",
|
||||
"requestDownloadFile 1=" + session + " 2=owner 3=room= 4=file9"), server.requests);
|
||||
assertEquals(List.of("authentication/login", "user/requestUploadFile", "user/requestDownloadFile"),
|
||||
server.accounts.calls);
|
||||
|
||||
files.close();
|
||||
assertEquals("authentication/deleteSession", server.accounts.calls.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void signsInAgainWhenTheSessionRanOut(@TempDir File dir) throws Exception {
|
||||
FileServer server = new FileServer();
|
||||
MyTsFiles files = signedIn(dir, server);
|
||||
files.requestUpload("room");
|
||||
server.expireNext = 1;
|
||||
server.accounts.calls.clear();
|
||||
|
||||
files.requestUpload("room");
|
||||
assertEquals(List.of("user/requestUploadFile", "authentication/login", "user/requestUploadFile"),
|
||||
server.accounts.calls);
|
||||
}
|
||||
|
||||
@Test
|
||||
void needsASignIn(@TempDir File dir) {
|
||||
FileServer server = new FileServer();
|
||||
MyTeamSpeakLogin login = new MyTeamSpeakLogin(new File(dir, "myts.properties"), new MyTeamSpeak(server));
|
||||
MyTsFiles files = new MyTsFiles(login, new MyTeamSpeak(server));
|
||||
assertThrows(MyTsException.class, () -> files.requestUpload("room"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.ts3client.text;
|
||||
|
||||
import com.ts3client.chatlog.ChatLogScope;
|
||||
import com.ts3client.chatlog.ChatLogStore;
|
||||
import com.ts3client.myts.ChatAttachment;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class ChatHtmlAttachmentTest {
|
||||
|
||||
private static final String ROOM = "+Tyg2JtxE8vRNZp+JiUBnmBh0MY=";
|
||||
private static final ChatAttachment PICTURE = ChatAttachment.of("f1", "abc", "cat.png", 532);
|
||||
private static final ChatAttachment FILE = ChatAttachment.of("f2", "abc", "notes <1>.txt", 3 << 20);
|
||||
|
||||
@Test
|
||||
void showsAPictureAsOne() {
|
||||
String html = ChatHtml.message(5, "uid", "alice", PICTURE.toMessage(), ROOM);
|
||||
assertTrue(html.contains("<img src=\"" + PICTURE.href(ROOM) + "\">"), html);
|
||||
assertFalse(html.contains("msg_type"), html);
|
||||
}
|
||||
|
||||
@Test
|
||||
void showsAnyOtherFileAsALinkWithItsSize() {
|
||||
String html = ChatHtml.message(5, "uid", "alice", FILE.toMessage(), ROOM);
|
||||
assertTrue(html.contains("href=\"" + FILE.href(ROOM) + "\">notes <1>.txt</a>"), html);
|
||||
assertTrue(html.contains("(3.0 MB)"), html);
|
||||
}
|
||||
|
||||
@Test
|
||||
void namesTheFileWhereItCannotBeFetched() {
|
||||
String html = ChatHtml.message(5, "uid", "alice", PICTURE.toMessage());
|
||||
assertFalse(html.contains("<img"), html);
|
||||
assertTrue(html.contains("cat.png"), html);
|
||||
}
|
||||
|
||||
@Test
|
||||
void showsTheFileInLogHistory(@TempDir Path temp) throws Exception {
|
||||
ChatLogStore logs = new ChatLogStore(temp, ROOM);
|
||||
logs.appendMessage(ChatLogScope.CHANNEL, false, 5, "uid", "alice", PICTURE.toMessage());
|
||||
List<String> history = logs.channelHtmlHistory();
|
||||
String html = ChatHtml.logEntry(history.get(history.size() - 1), ROOM);
|
||||
assertTrue(html.contains("<img src=\"" + PICTURE.href(ROOM) + "\">"), html);
|
||||
assertFalse(html.contains("msg_type"), html);
|
||||
}
|
||||
|
||||
@Test
|
||||
void previewsAFileByItsName() {
|
||||
assertEquals("cat.png", ChatHtml.plainMessage(PICTURE.toMessage()));
|
||||
assertEquals("hi", ChatHtml.plainMessage("[b]hi[/b]"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user