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

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

View file

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

View file

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

View file

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