AbstractGroupMessage.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2013-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 "AbstractGroupMessage.h"
  21. #import "BundleUtil.h"
  22. #import "GroupTextMessage.h"
  23. #import "GroupImageMessage.h"
  24. #import "GroupVideoMessage.h"
  25. #import "GroupLocationMessage.h"
  26. #import "GroupAudioMessage.h"
  27. #import "GroupBallotCreateMessage.h"
  28. #import "GroupFileMessage.h"
  29. #import "BallotMessageDecoder.h"
  30. #import "FileMessageDecoder.h"
  31. #import "Ballot.h"
  32. #import "QuoteParser.h"
  33. #import "TextStyleUtils.h"
  34. #import "Utils.h"
  35. @implementation AbstractGroupMessage
  36. @synthesize groupCreator;
  37. @synthesize groupId;
  38. - (BOOL)isGroup {
  39. return YES;
  40. }
  41. - (NSString *)description {
  42. NSString *result = [super description];
  43. return [result stringByAppendingFormat:@" groupCreator: %@ - groupId: %@ ", groupCreator, groupId];
  44. }
  45. - (NSString *)pushNotificationBody {
  46. NSString *body = [NSString new];
  47. if ([self isKindOfClass:[GroupTextMessage class]]) {
  48. NSString *quotedIdentity = nil;
  49. NSString *remainingBody = nil;
  50. NSString *quotedText = [QuoteParser parseQuoteFromMessage:((GroupTextMessage *)self).text quotedIdentity:&quotedIdentity remainingBody:&remainingBody];
  51. if (quotedText) {
  52. body = remainingBody;
  53. } else {
  54. body = ((GroupTextMessage *)self).text;
  55. }
  56. body = [TextStyleUtils makeMentionsStringForText:body];
  57. }
  58. else if ([self isKindOfClass:[GroupImageMessage class]]) {
  59. body = [BundleUtil localizedStringForKey:@"new_image_message"];
  60. }
  61. else if ([self isKindOfClass:[GroupVideoMessage class]]) {
  62. body = [BundleUtil localizedStringForKey:@"new_video_message"];
  63. }
  64. else if ([self isKindOfClass:[GroupLocationMessage class]]) {
  65. NSString *locationName = [(GroupLocationMessage *)self poiName];
  66. if (locationName)
  67. body = [NSString stringWithFormat:@"%@: %@", [BundleUtil localizedStringForKey:@"new_location_message"], locationName];
  68. else
  69. body = [BundleUtil localizedStringForKey:@"new_location_message"];
  70. }
  71. else if ([self isKindOfClass:[GroupAudioMessage class]]) {
  72. body = [NSString stringWithFormat:@"%@ (%@)", [BundleUtil localizedStringForKey:@"new_audio_message"], [Utils timeStringForSeconds:((GroupAudioMessage *)self).duration]];
  73. }
  74. else if ([self isKindOfClass:[GroupBallotCreateMessage class]]) {
  75. BallotMessageDecoder *decoder = [BallotMessageDecoder messageDecoder];
  76. BOOL closed = [decoder decodeNotificationCreateBallotStateFromBox:(BoxBallotCreateMessage *)self].integerValue == kBallotStateClosed;
  77. NSString *ballotTitle = [decoder decodeCreateBallotTitleFromBox:(BoxBallotCreateMessage *)self];
  78. if (closed) {
  79. body = [BundleUtil localizedStringForKey:@"new_ballot_closed_message"];
  80. } else {
  81. body = [NSString stringWithFormat:[BundleUtil localizedStringForKey:@"new_ballot_create_message"], ballotTitle];
  82. }
  83. }
  84. else if ([self isKindOfClass:[GroupFileMessage class]]) {
  85. NSString *caption = [FileMessageDecoder decodeGroupFileCaptionFromBox:(GroupFileMessage *)self];
  86. if (caption != nil) {
  87. body = caption;
  88. } else {
  89. NSString *fileName = [FileMessageDecoder decodeGroupFilenameFromBox:(GroupFileMessage *)self];
  90. body = [NSString stringWithFormat:[BundleUtil localizedStringForKey:@"new_file_message"], fileName];
  91. }
  92. }
  93. return body;
  94. }
  95. - (BOOL)allowToSendProfilePicture {
  96. return NO;
  97. }
  98. #pragma mark - NSCoding
  99. - (id)initWithCoder:(NSCoder *)decoder {
  100. if (self = [super init]) {
  101. self.groupCreator = [decoder decodeObjectForKey:@"groupCreator"];
  102. self.groupId = [decoder decodeObjectForKey:@"groupId"];
  103. }
  104. return self;
  105. }
  106. - (void)encodeWithCoder:(NSCoder *)encoder {
  107. [encoder encodeObject:self.groupCreator forKey:@"groupCreator"];
  108. [encoder encodeObject:self.groupId forKey:@"groupId"];
  109. }
  110. @end