use of org.thoughtcrime.securesms.database.model.Mention in project Signal-Android by signalapp.
the class MentionDatabase method readMentions.
@NonNull
private Map<Long, List<Mention>> readMentions(@Nullable Cursor cursor) {
Map<Long, List<Mention>> mentions = new HashMap<>();
while (cursor != null && cursor.moveToNext()) {
long messageId = CursorUtil.requireLong(cursor, MESSAGE_ID);
List<Mention> messageMentions = mentions.get(messageId);
if (messageMentions == null) {
messageMentions = new LinkedList<>();
mentions.put(messageId, messageMentions);
}
messageMentions.add(new Mention(RecipientId.from(CursorUtil.requireLong(cursor, RECIPIENT_ID)), CursorUtil.requireInt(cursor, RANGE_START), CursorUtil.requireInt(cursor, RANGE_LENGTH)));
}
return mentions;
}
use of org.thoughtcrime.securesms.database.model.Mention in project Signal-Android by signalapp.
the class MentionDatabase method getMentionsForMessage.
@NonNull
public List<Mention> getMentionsForMessage(long messageId) {
SQLiteDatabase db = databaseHelper.getSignalReadableDatabase();
List<Mention> mentions = new LinkedList<>();
try (Cursor cursor = db.query(TABLE_NAME, null, MESSAGE_ID + " = ?", SqlUtil.buildArgs(messageId), null, null, null)) {
while (cursor != null && cursor.moveToNext()) {
mentions.add(new Mention(RecipientId.from(CursorUtil.requireLong(cursor, RECIPIENT_ID)), CursorUtil.requireInt(cursor, RANGE_START), CursorUtil.requireInt(cursor, RANGE_LENGTH)));
}
}
return mentions;
}
use of org.thoughtcrime.securesms.database.model.Mention in project Signal-Android by signalapp.
the class MessageContentProcessor method getValidatedQuote.
private Optional<QuoteModel> getValidatedQuote(Optional<SignalServiceDataMessage.Quote> quote) {
if (!quote.isPresent())
return Optional.absent();
if (quote.get().getId() <= 0) {
warn("Received quote without an ID! Ignoring...");
return Optional.absent();
}
if (quote.get().getAuthor() == null) {
warn("Received quote without an author! Ignoring...");
return Optional.absent();
}
RecipientId author = Recipient.externalPush(quote.get().getAuthor()).getId();
MessageRecord message = SignalDatabase.mmsSms().getMessageFor(quote.get().getId(), author);
if (message != null && !message.isRemoteDelete()) {
log("Found matching message record...");
List<Attachment> attachments = new LinkedList<>();
List<Mention> mentions = new LinkedList<>();
if (message.isMms()) {
MmsMessageRecord mmsMessage = (MmsMessageRecord) message;
mentions.addAll(SignalDatabase.mentions().getMentionsForMessage(mmsMessage.getId()));
if (mmsMessage.isViewOnce()) {
attachments.add(new TombstoneAttachment(MediaUtil.VIEW_ONCE, true));
} else {
attachments = mmsMessage.getSlideDeck().asAttachments();
if (attachments.isEmpty()) {
attachments.addAll(Stream.of(mmsMessage.getLinkPreviews()).filter(lp -> lp.getThumbnail().isPresent()).map(lp -> lp.getThumbnail().get()).toList());
}
}
}
return Optional.of(new QuoteModel(quote.get().getId(), author, message.getBody(), false, attachments, mentions));
} else if (message != null) {
warn("Found the target for the quote, but it's flagged as remotely deleted.");
}
warn("Didn't find matching message record...");
return Optional.of(new QuoteModel(quote.get().getId(), author, quote.get().getText(), true, PointerAttachment.forPointers(quote.get().getAttachments()), getMentions(quote.get().getMentions())));
}
use of org.thoughtcrime.securesms.database.model.Mention in project Signal-Android by signalapp.
the class SearchRepository method updateSnippetWithDisplayNames.
@NonNull
private String updateSnippetWithDisplayNames(@NonNull String body, @NonNull String bodySnippet, @NonNull List<Mention> mentions) {
String cleanSnippet = bodySnippet;
int startOffset = 0;
if (cleanSnippet.startsWith(SNIPPET_WRAP)) {
cleanSnippet = cleanSnippet.substring(SNIPPET_WRAP.length());
startOffset = SNIPPET_WRAP.length();
}
if (cleanSnippet.endsWith(SNIPPET_WRAP)) {
cleanSnippet = cleanSnippet.substring(0, cleanSnippet.length() - SNIPPET_WRAP.length());
}
int startIndex = body.indexOf(cleanSnippet);
if (startIndex != -1) {
List<Mention> adjustMentions = new ArrayList<>(mentions.size());
for (Mention mention : mentions) {
int adjustedStart = mention.getStart() - startIndex + startOffset;
if (adjustedStart >= 0 && adjustedStart + mention.getLength() <= cleanSnippet.length()) {
adjustMentions.add(new Mention(mention.getRecipientId(), adjustedStart, mention.getLength()));
}
}
// noinspection ConstantConditions
return MentionUtil.updateBodyAndMentionsWithDisplayNames(context, bodySnippet, adjustMentions).getBody().toString();
}
return bodySnippet;
}
use of org.thoughtcrime.securesms.database.model.Mention in project Signal-Android by signalapp.
the class SearchRepository method queryMentions.
@NonNull
private List<MessageResult> queryMentions(@NonNull List<String> cleanQueries) {
Set<RecipientId> recipientIds = new HashSet<>();
for (String cleanQuery : cleanQueries) {
for (Recipient recipient : recipientDatabase.queryRecipientsForMentions(cleanQuery)) {
recipientIds.add(recipient.getId());
}
}
Map<Long, List<Mention>> mentionQueryResults = mentionDatabase.getMentionsContainingRecipients(recipientIds, 500);
if (mentionQueryResults.isEmpty()) {
return Collections.emptyList();
}
List<MessageResult> results = new ArrayList<>();
try (MessageDatabase.Reader reader = mmsDatabase.getMessages(mentionQueryResults.keySet())) {
MessageRecord record;
while ((record = reader.getNext()) != null) {
List<Mention> mentions = mentionQueryResults.get(record.getId());
if (Util.hasItems(mentions)) {
MentionUtil.UpdatedBodyAndMentions updated = MentionUtil.updateBodyAndMentionsWithDisplayNames(context, record.getBody(), mentions);
String updatedBody = updated.getBody() != null ? updated.getBody().toString() : record.getBody();
String updatedSnippet = makeSnippet(cleanQueries, updatedBody);
// noinspection ConstantConditions
results.add(new MessageResult(threadDatabase.getRecipientForThreadId(record.getThreadId()), record.getRecipient(), updatedBody, updatedSnippet, record.getThreadId(), record.getId(), record.getDateReceived(), true));
}
}
}
return results;
}
Aggregations