Add null check and change some formatting

This commit is contained in:
AsamK 2020-12-23 11:24:07 +01:00
parent 58db3cbd53
commit 67f62947c6
5 changed files with 28 additions and 41 deletions

View file

@ -449,7 +449,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
System.out.println("Quote: (" + quote.getId() + ")"); System.out.println("Quote: (" + quote.getId() + ")");
System.out.println(" Author: " + m.resolveSignalServiceAddress(quote.getAuthor()).getLegacyIdentifier()); System.out.println(" Author: " + m.resolveSignalServiceAddress(quote.getAuthor()).getLegacyIdentifier());
System.out.println(" Text: " + quote.getText()); System.out.println(" Text: " + quote.getText());
if (quote.getMentions().size() > 0) { if (quote.getMentions() != null && quote.getMentions().size() > 0) {
System.out.println(" Mentions: "); System.out.println(" Mentions: ");
for (SignalServiceDataMessage.Mention mention : quote.getMentions()) { for (SignalServiceDataMessage.Mention mention : quote.getMentions()) {
printMention(mention, m); printMention(mention, m);
@ -488,15 +488,8 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
} }
private void printMention(SignalServiceDataMessage.Mention mention, Manager m) { private void printMention(SignalServiceDataMessage.Mention mention, Manager m) {
System.out.println("- " System.out.println("- " + m.resolveSignalServiceAddress(new SignalServiceAddress(mention.getUuid(), null))
+ m.resolveSignalServiceAddress( .getLegacyIdentifier() + ": " + mention.getStart() + " (length: " + mention.getLength() + ")");
new SignalServiceAddress(mention.getUuid(), null)
).getLegacyIdentifier()
+ ": "
+ mention.getStart()
+ " (length: "
+ mention.getLength()
+ ")");
} }
private void printAttachment(SignalServiceAttachment attachment) { private void printAttachment(SignalServiceAttachment attachment) {

View file

@ -2,12 +2,10 @@ package org.asamk.signal.json;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage; import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
import org.whispersystems.signalservice.api.messages.SignalServiceGroup; import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
import org.whispersystems.signalservice.api.messages.SignalServiceGroupV2; import org.whispersystems.signalservice.api.messages.SignalServiceGroupV2;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -45,20 +43,22 @@ class JsonDataMessage {
this.quote = new JsonQuote(dataMessage.getQuote().get(), m); this.quote = new JsonQuote(dataMessage.getQuote().get(), m);
} }
if (dataMessage.getMentions().isPresent()) { if (dataMessage.getMentions().isPresent()) {
this.mentions = new ArrayList<>(dataMessage.getMentions().get().size()); this.mentions = dataMessage.getMentions()
for (SignalServiceDataMessage.Mention mention : dataMessage.getMentions().get()) { .get()
this.mentions.add(new JsonMention(mention, m)); .stream()
} .map(mention -> new JsonMention(mention, m))
.collect(Collectors.toList());
} else { } else {
this.mentions = new ArrayList<>(); this.mentions = List.of();
} }
if (dataMessage.getAttachments().isPresent()) { if (dataMessage.getAttachments().isPresent()) {
this.attachments = new ArrayList<>(dataMessage.getAttachments().get().size()); this.attachments = dataMessage.getAttachments()
for (SignalServiceAttachment attachment : dataMessage.getAttachments().get()) { .get()
this.attachments.add(new JsonAttachment(attachment)); .stream()
} .map(JsonAttachment::new)
.collect(Collectors.toList());
} else { } else {
this.attachments = new ArrayList<>(); this.attachments = List.of();
} }
} }

View file

@ -11,12 +11,9 @@ public class JsonMention {
int length; int length;
JsonMention(SignalServiceDataMessage.Mention mention, Manager m) { JsonMention(SignalServiceDataMessage.Mention mention, Manager m) {
this.name = m.resolveSignalServiceAddress( this.name = m.resolveSignalServiceAddress(new SignalServiceAddress(mention.getUuid(), null))
new SignalServiceAddress(mention.getUuid(), null) .getLegacyIdentifier();
).getLegacyIdentifier();
this.start = mention.getStart(); this.start = mention.getStart();
this.length = mention.getLength(); this.length = mention.getLength();
} }
} }

View file

@ -5,6 +5,7 @@ import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class JsonQuote { public class JsonQuote {
@ -20,23 +21,20 @@ public class JsonQuote {
this.author = m.resolveSignalServiceAddress(quote.getAuthor()).getLegacyIdentifier(); this.author = m.resolveSignalServiceAddress(quote.getAuthor()).getLegacyIdentifier();
this.text = quote.getText(); this.text = quote.getText();
if (quote.getMentions().size() > 0) { if (quote.getMentions() != null && quote.getMentions().size() > 0) {
this.mentions = new ArrayList<>(quote.getMentions().size()); this.mentions = quote.getMentions()
.stream()
for (SignalServiceDataMessage.Mention quotedMention: quote.getMentions()){ .map(quotedMention -> new JsonMention(quotedMention, m))
this.mentions.add(new JsonMention(quotedMention, m)); .collect(Collectors.toList());
}
} }
if (quote.getAttachments().size() > 0) { if (quote.getAttachments().size() > 0) {
this.attachments = new ArrayList<>(quote.getAttachments().size()); this.attachments = quote.getAttachments()
.stream()
for (SignalServiceDataMessage.Quote.QuotedAttachment quotedAttachment : quote.getAttachments()) { .map(JsonQuotedAttachment::new)
this.attachments.add(new JsonQuotedAttachment(quotedAttachment)); .collect(Collectors.toList());
}
} else { } else {
this.attachments = new ArrayList<>(); this.attachments = new ArrayList<>();
} }
} }
} }

View file

@ -13,8 +13,7 @@ public class JsonQuotedAttachment {
filename = quotedAttachment.getFileName(); filename = quotedAttachment.getFileName();
if (quotedAttachment.getThumbnail() != null) { if (quotedAttachment.getThumbnail() != null) {
thumbnail = new JsonAttachment(quotedAttachment.getThumbnail()); thumbnail = new JsonAttachment(quotedAttachment.getThumbnail());
} } else {
else {
thumbnail = null; thumbnail = null;
} }
} }