Java Code Examples for org.whispersystems.signalservice.api.messages.SignalServiceContent#getServerTimestamp()

The following examples show how to use org.whispersystems.signalservice.api.messages.SignalServiceContent#getServerTimestamp() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PushProcessMessageJob.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private void handleEndSessionMessage(@NonNull SignalServiceContent content,
                                     @NonNull Optional<Long>       smsMessageId)
{
  SmsDatabase         smsDatabase         = DatabaseFactory.getSmsDatabase(context);
  IncomingTextMessage incomingTextMessage = new IncomingTextMessage(Recipient.externalPush(context, content.getSender()).getId(),
                                                                    content.getSenderDevice(),
                                                                    content.getTimestamp(),
                                                                    content.getServerTimestamp(),
                                                                    "", Optional.absent(), 0,
                                                                    content.isNeedsReceipt());

  Long threadId;

  if (!smsMessageId.isPresent()) {
    IncomingEndSessionMessage incomingEndSessionMessage = new IncomingEndSessionMessage(incomingTextMessage);
    Optional<InsertResult>    insertResult              = smsDatabase.insertMessageInbox(incomingEndSessionMessage);

    if (insertResult.isPresent()) threadId = insertResult.get().getThreadId();
    else                          threadId = null;
  } else {
    smsDatabase.markAsEndSession(smsMessageId.get());
    threadId = smsDatabase.getThreadIdForMessage(smsMessageId.get());
  }

  if (threadId != null) {
    SessionStore sessionStore = new TextSecureSessionStore(context);
    sessionStore.deleteAllSessions(content.getSender().getIdentifier());

    SecurityEvent.broadcastSecurityUpdateEvent(context);
    ApplicationDependencies.getMessageNotifier().updateNotification(context, threadId);
  }
}
 
Example 2
Source File: GroupV1MessageProcessor.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private static @Nullable Long storeMessage(@NonNull Context context,
                                           @NonNull SignalServiceContent content,
                                           @NonNull SignalServiceGroup group,
                                           @NonNull GroupContext storage,
                                           boolean  outgoing)
{
  if (group.getAvatar().isPresent()) {
    ApplicationDependencies.getJobManager()
                           .add(new AvatarGroupsV1DownloadJob(GroupId.v1orThrow(group.getGroupId())));
  }

  try {
    if (outgoing) {
      MmsDatabase                mmsDatabase     = DatabaseFactory.getMmsDatabase(context);
      RecipientId                recipientId     = DatabaseFactory.getRecipientDatabase(context).getOrInsertFromGroupId(GroupId.v1orThrow(group.getGroupId()));
      Recipient                  recipient       = Recipient.resolved(recipientId);
      OutgoingGroupUpdateMessage outgoingMessage = new OutgoingGroupUpdateMessage(recipient, storage, null, content.getTimestamp(), 0, false, null, Collections.emptyList(), Collections.emptyList());
      long                       threadId        = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);
      long                       messageId       = mmsDatabase.insertMessageOutbox(outgoingMessage, threadId, false, null);

      mmsDatabase.markAsSent(messageId, true);

      return threadId;
    } else {
      SmsDatabase          smsDatabase  = DatabaseFactory.getSmsDatabase(context);
      String               body         = Base64.encodeBytes(storage.toByteArray());
      IncomingTextMessage  incoming     = new IncomingTextMessage(Recipient.externalPush(context, content.getSender()).getId(), content.getSenderDevice(), content.getTimestamp(), content.getServerTimestamp(), body, Optional.of(GroupId.v1orThrow(group.getGroupId())), 0, content.isNeedsReceipt());
      IncomingGroupUpdateMessage groupMessage = new IncomingGroupUpdateMessage(incoming, storage, body);

      Optional<InsertResult> insertResult = smsDatabase.insertMessageInbox(groupMessage);

      if (insertResult.isPresent()) {
        ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
        return insertResult.get().getThreadId();
      } else {
        return null;
      }
    }
  } catch (MmsException e) {
    Log.w(TAG, e);
  }

  return null;
}
 
Example 3
Source File: PushProcessMessageJob.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private void handleExpirationUpdate(@NonNull SignalServiceContent content,
                                    @NonNull SignalServiceDataMessage message,
                                    @NonNull Optional<Long> smsMessageId,
                                    @NonNull Optional<GroupId> groupId)
    throws StorageFailedException, BadGroupIdException
{
  if (groupId.isPresent() && groupId.get().isV2()) {
    Log.w(TAG, "Expiration update received for GV2. Ignoring.");
    return;
  }

  int                                 expiresInSeconds = message.getExpiresInSeconds();
  Optional<SignalServiceGroupContext> groupContext     = message.getGroupContext();
  Recipient                           recipient        = getMessageDestination(content, groupContext);

  if (recipient.getExpireMessages() == expiresInSeconds) {
    Log.i(TAG, "No change in message expiry for group. Ignoring.");
    return;
  }

  try {
    MmsDatabase          database     = DatabaseFactory.getMmsDatabase(context);
    Recipient            sender       = Recipient.externalPush(context, content.getSender());
    IncomingMediaMessage mediaMessage = new IncomingMediaMessage(sender.getId(),
                                                                 content.getTimestamp(),
                                                                 content.getServerTimestamp(),
                                                                 -1,
                                                                 expiresInSeconds * 1000L,
                                                                  true,
                                                                 false,
                                                                 content.isNeedsReceipt(),
                                                                 Optional.absent(),
                                                                 groupContext,
                                                                 Optional.absent(),
                                                                 Optional.absent(),
                                                                 Optional.absent(),
                                                                 Optional.absent(),
                                                                 Optional.absent());

    database.insertSecureDecryptedMessageInbox(mediaMessage, -1);

    DatabaseFactory.getRecipientDatabase(context).setExpireMessages(recipient.getId(), expiresInSeconds);

    if (smsMessageId.isPresent()) {
      DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get());
    }
  } catch (MmsException e) {
    throw new StorageFailedException(e, content.getSender().getIdentifier(), content.getSenderDevice());
  }
}
 
Example 4
Source File: PushProcessMessageJob.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private void handleMediaMessage(@NonNull SignalServiceContent content,
                                @NonNull SignalServiceDataMessage message,
                                @NonNull Optional<Long> smsMessageId)
    throws StorageFailedException, BadGroupIdException
{
  notifyTypingStoppedFromIncomingMessage(getMessageDestination(content, message), content.getSender(), content.getSenderDevice());

  Optional<InsertResult> insertResult;

  MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
  database.beginTransaction();

  try {
    Optional<QuoteModel>        quote          = getValidatedQuote(message.getQuote());
    Optional<List<Contact>>     sharedContacts = getContacts(message.getSharedContacts());
    Optional<List<LinkPreview>> linkPreviews   = getLinkPreviews(message.getPreviews(), message.getBody().or(""));
    Optional<Attachment>        sticker        = getStickerAttachment(message.getSticker());
    IncomingMediaMessage        mediaMessage   = new IncomingMediaMessage(Recipient.externalPush(context, content.getSender()).getId(),
                                                                          message.getTimestamp(),
                                                                          content.getServerTimestamp(),
                                                                          -1,
                                                                          message.getExpiresInSeconds() * 1000L,
                                                                          false,
                                                                          message.isViewOnce(),
                                                                          content.isNeedsReceipt(),
                                                                          message.getBody(),
                                                                          message.getGroupContext(),
                                                                          message.getAttachments(),
                                                                          quote,
                                                                          sharedContacts,
                                                                          linkPreviews,
                                                                          sticker);

    insertResult = database.insertSecureDecryptedMessageInbox(mediaMessage, -1);

    if (insertResult.isPresent()) {
      List<DatabaseAttachment> allAttachments     = DatabaseFactory.getAttachmentDatabase(context).getAttachmentsForMessage(insertResult.get().getMessageId());
      List<DatabaseAttachment> stickerAttachments = Stream.of(allAttachments).filter(Attachment::isSticker).toList();
      List<DatabaseAttachment> attachments        = Stream.of(allAttachments).filterNot(Attachment::isSticker).toList();

      forceStickerDownloadIfNecessary(stickerAttachments);

      for (DatabaseAttachment attachment : attachments) {
        ApplicationDependencies.getJobManager().add(new AttachmentDownloadJob(insertResult.get().getMessageId(), attachment.getAttachmentId(), false));
      }

      if (smsMessageId.isPresent()) {
        DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get());
      }

      database.setTransactionSuccessful();
    }
  } catch (MmsException e) {
    throw new StorageFailedException(e, content.getSender().getIdentifier(), content.getSenderDevice());
  } finally {
    database.endTransaction();
  }

  if (insertResult.isPresent()) {
    ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());

    if (message.isViewOnce()) {
      ApplicationContext.getInstance(context).getViewOnceMessageManager().scheduleIfNecessary();
    }
  }
}
 
Example 5
Source File: PushProcessMessageJob.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private void handleTextMessage(@NonNull SignalServiceContent content,
                               @NonNull SignalServiceDataMessage message,
                               @NonNull Optional<Long> smsMessageId,
                               @NonNull Optional<GroupId> groupId)
    throws StorageFailedException, BadGroupIdException
{
  SmsDatabase database  = DatabaseFactory.getSmsDatabase(context);
  String      body      = message.getBody().isPresent() ? message.getBody().get() : "";
  Recipient   recipient = getMessageDestination(content, message);

  if (message.getExpiresInSeconds() != recipient.getExpireMessages()) {
    handleExpirationUpdate(content, message, Optional.absent(), groupId);
  }

  Long threadId;

  if (smsMessageId.isPresent() && !message.getGroupContext().isPresent()) {
    threadId = database.updateBundleMessageBody(smsMessageId.get(), body).second;
  } else {
    notifyTypingStoppedFromIncomingMessage(recipient, content.getSender(), content.getSenderDevice());

    IncomingTextMessage textMessage = new IncomingTextMessage(Recipient.externalPush(context, content.getSender()).getId(),
                                                              content.getSenderDevice(),
                                                              message.getTimestamp(),
                                                              content.getServerTimestamp(),
                                                              body,
                                                              groupId,
                                                              message.getExpiresInSeconds() * 1000L,
                                                              content.isNeedsReceipt());

    textMessage = new IncomingEncryptedMessage(textMessage, body);
    Optional<InsertResult> insertResult = database.insertMessageInbox(textMessage);

    if (insertResult.isPresent()) threadId = insertResult.get().getThreadId();
    else                          threadId = null;

    if (smsMessageId.isPresent()) database.deleteMessage(smsMessageId.get());
  }

  if (threadId != null) {
    ApplicationDependencies.getMessageNotifier().updateNotification(context, threadId);
  }
}