BaseMessage.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "BaseMessage.h"
  21. #import "Conversation.h"
  22. @implementation BaseMessage
  23. @dynamic date;
  24. @dynamic id;
  25. @dynamic delivered;
  26. @dynamic isOwn;
  27. @dynamic read;
  28. @dynamic sent;
  29. @dynamic userack;
  30. @dynamic userackDate;
  31. @dynamic deliveryDate;
  32. @dynamic readDate;
  33. @dynamic remoteSentDate;
  34. @dynamic sendFailed;
  35. @dynamic conversation;
  36. @dynamic sender;
  37. @dynamic webRequestId;
  38. @dynamic flags;
  39. - (NSString*)logText {
  40. return nil;
  41. }
  42. - (NSString*)previewText {
  43. return @"";
  44. }
  45. - (NSString*)quotePreviewText {
  46. return self.previewText;
  47. }
  48. - (BOOL)wasDeleted {
  49. return self.managedObjectContext == nil;
  50. }
  51. // for backwards compatibility of message created before ThreemaDataV19
  52. - (NSDate *)remoteSentDate {
  53. NSDate *date = [self primitiveValueForKey:@"remoteSentDate"];
  54. if (date == nil) {
  55. date = [self primitiveValueForKey:@"date"];
  56. }
  57. return date;
  58. }
  59. - (MessageState)messageState {
  60. if (self.isOwn.boolValue) {
  61. if (self.userackDate) {
  62. if (self.userack.boolValue) {
  63. return MESSAGE_STATE_USER_ACK;
  64. } else {
  65. return MESSAGE_STATE_USER_DECLINED;
  66. }
  67. } else if (self.sendFailed.boolValue) {
  68. return MESSAGE_STATE_FAILED;
  69. } else if (self.read.boolValue) {
  70. return MESSAGE_STATE_READ;
  71. } else if (self.delivered.boolValue) {
  72. return MESSAGE_STATE_DELIVERED;
  73. } else if (self.sent.boolValue) {
  74. return MESSAGE_STATE_SENT;
  75. } else {
  76. return MESSAGE_STATE_SENDING;
  77. }
  78. } else {
  79. if (self.userackDate) {
  80. if (self.userack.boolValue) {
  81. return MESSAGE_STATE_USER_ACK;
  82. } else {
  83. return MESSAGE_STATE_USER_DECLINED;
  84. }
  85. } else {
  86. return MESSAGE_STATE_SENT;
  87. }
  88. }
  89. }
  90. - (NSDate *)dateForCurrentState {
  91. NSDate *date = nil;
  92. if (!self.isOwn.boolValue) {
  93. date = self.remoteSentDate;
  94. if (self.userackDate != nil) {
  95. date = self.userackDate;
  96. }
  97. } else {
  98. /* sent self - timestamp depends on status */
  99. if (self.conversation.groupId != nil) {
  100. /* Group messages don't really have a read date, but the UI may have set one for the new message line */
  101. date = self.date;
  102. } else {
  103. if (self.userackDate != nil) {
  104. date = self.userackDate;
  105. } else if (self.read.boolValue) {
  106. date = self.readDate;
  107. } else if (self.delivered.boolValue) {
  108. date = self.deliveryDate;
  109. } else if (self.sent.boolValue) {
  110. date = self.date;
  111. }
  112. }
  113. }
  114. return date;
  115. }
  116. - (BOOL)noDeliveryReceiptFlagSet {
  117. if (self.flags != nil) {
  118. if ([self.flags integerValue] & BaseMessageFlagsNoDeliveryReceipt) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. @end