BallotMessageEncoder.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-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 "BallotMessageEncoder.h"
  21. #import "BallotChoice.h"
  22. #import "BallotResult.h"
  23. #import "BallotMessage.h"
  24. #import "BallotKeys.h"
  25. #import "Conversation.h"
  26. #import "Contact.h"
  27. #import "MyIdentityStore.h"
  28. #import "JsonUtil.h"
  29. #ifdef DEBUG
  30. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  31. #else
  32. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  33. #endif
  34. @implementation BallotMessageEncoder
  35. + (BoxBallotVoteMessage *)encodeVoteMessageForBallot:(Ballot *)ballot {
  36. NSData *jsonData = [self jsonVoteDataFor:ballot];
  37. BoxBallotVoteMessage *voteMessage = [[BoxBallotVoteMessage alloc] init];
  38. voteMessage.messageId = [AbstractMessage randomMessageId];
  39. voteMessage.date = [NSDate date];
  40. voteMessage.ballotCreator = ballot.creatorId;
  41. voteMessage.ballotId = ballot.id;
  42. voteMessage.jsonChoiceData = jsonData;
  43. return voteMessage;
  44. }
  45. + (BoxBallotCreateMessage *)encodeCreateMessageForBallot:(Ballot *)ballot {
  46. NSData *jsonData = [self jsonCreateDataFor:ballot];
  47. BoxBallotCreateMessage *boxMessage = [[BoxBallotCreateMessage alloc] init];
  48. boxMessage.messageId = [AbstractMessage randomMessageId];
  49. boxMessage.date = [NSDate date];
  50. boxMessage.ballotId = ballot.id;
  51. boxMessage.jsonData = jsonData;
  52. return boxMessage;
  53. }
  54. + (NSData *)jsonCreateDataFor:(Ballot *)ballot {
  55. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  56. [dictionary setObject:ballot.title forKey:JSON_KEY_TITLE];
  57. [dictionary setObject:ballot.type forKey:JSON_KEY_TYPE];
  58. [dictionary setObject:ballot.state forKey:JSON_KEY_STATE];
  59. [dictionary setObject:ballot.assessmentType forKey:JSON_KEY_ASSESSMENT_TYPE];
  60. [dictionary setObject:ballot.choicesType forKey:JSON_KEY_CHOICES_TYPE];
  61. NSArray *participantArray = nil;
  62. if ([ballot displayResult]) {
  63. NSSet *participants = [self participantsForBallot:ballot];
  64. participantArray = [participants allObjects];
  65. [dictionary setObject:participantArray forKey:JSON_KEY_PARTICIPANTS];
  66. }
  67. NSArray *choices = [self choiceDataForBallot:ballot participants:participantArray];
  68. [dictionary setObject:choices forKey:JSON_KEY_CHOICES];
  69. NSError *error;
  70. NSData *jsonData = [JsonUtil serializeJsonFrom:dictionary error:error];
  71. if (jsonData == nil) {
  72. DDLogError(@"Error encoding ballot json data %@, %@", error, [error userInfo]);
  73. }
  74. return jsonData;
  75. }
  76. + (NSArray *)choiceDataForBallot:(Ballot *)ballot participants:(NSArray *)participants {
  77. NSMutableArray *choiceData = [NSMutableArray array];
  78. for (BallotChoice *choice in ballot.choices) {
  79. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  80. [dictionary setObject:choice.id forKey:JSON_CHOICE_KEY_ID];
  81. [dictionary setObject:choice.name forKey:JSON_CHOICE_KEY_NAME];
  82. [dictionary setObject:choice.orderPosition forKey:JSON_CHOICE_KEY_ORDER_POSITION];
  83. if ([ballot displayResult]) {
  84. NSArray *result = [self resultForChoice:choice participants:participants];
  85. [dictionary setObject:result forKey:JSON_CHOICE_KEY_RESULT];
  86. }
  87. [choiceData addObject: dictionary];
  88. }
  89. return choiceData;
  90. }
  91. + (NSArray *)resultForChoice:(BallotChoice *)choice participants:(NSArray *)participants {
  92. NSMutableArray *resultArray = [NSMutableArray array];
  93. for (NSString *participantId in participants) {
  94. BallotResult *result = [choice getResultForId:participantId];
  95. if (result) {
  96. [resultArray addObject: result.value];
  97. } else {
  98. DDLogError(@"missing ballot result");
  99. [resultArray addObject: [NSNumber numberWithInt: 0]];
  100. }
  101. }
  102. return resultArray;
  103. }
  104. + (NSData *)jsonVoteDataFor:(Ballot *)ballot {
  105. NSMutableArray *choiceArray = [NSMutableArray array];
  106. for (BallotChoice *choice in ballot.choices) {
  107. NSMutableArray *resultArray = [NSMutableArray array];
  108. BallotResult *ownResult = [choice getOwnResult];
  109. if (ownResult) {
  110. [resultArray addObject: choice.id];
  111. [resultArray addObject: ownResult.value];
  112. [choiceArray addObject: resultArray];
  113. }
  114. }
  115. NSError *error;
  116. NSData *jsonData = [JsonUtil serializeJsonFrom:choiceArray error:error];
  117. if (jsonData == nil) {
  118. DDLogError(@"Error encoding ballot vote json data %@, %@", error, [error userInfo]);
  119. }
  120. return jsonData;
  121. }
  122. + (NSSet *)participantsForBallot:(Ballot *)ballot {
  123. NSMutableSet *participants = [NSMutableSet set];
  124. for (BallotChoice *choice in ballot.choices) {
  125. for (BallotResult *result in choice.result) {
  126. [participants addObject: result.participantId];
  127. }
  128. }
  129. return participants;
  130. }
  131. #pragma mark - private methods
  132. + (GroupBallotCreateMessage *)groupBallotCreateMessageFrom:(BoxBallotCreateMessage *)boxBallotMessage forConversation:(Conversation *)conversation {
  133. GroupBallotCreateMessage *msg = [[GroupBallotCreateMessage alloc] init];
  134. msg.messageId = boxBallotMessage.messageId;
  135. msg.date = boxBallotMessage.date;
  136. msg.groupId = conversation.groupId;
  137. msg.jsonData = boxBallotMessage.jsonData;
  138. msg.ballotId = boxBallotMessage.ballotId;
  139. if (conversation.contact == nil) {
  140. msg.groupCreator = [MyIdentityStore sharedMyIdentityStore].identity;
  141. } else {
  142. msg.groupCreator = conversation.contact.identity;
  143. }
  144. return msg;
  145. }
  146. + (GroupBallotVoteMessage *)groupBallotVoteMessageFrom:(BoxBallotVoteMessage *)boxBallotMessage forConversation:(Conversation *)conversation {
  147. GroupBallotVoteMessage *msg = [[GroupBallotVoteMessage alloc] init];
  148. msg.messageId = boxBallotMessage.messageId;
  149. msg.date = boxBallotMessage.date;
  150. msg.groupId = conversation.groupId;
  151. msg.ballotCreator = boxBallotMessage.ballotCreator;
  152. msg.ballotId = boxBallotMessage.ballotId;
  153. msg.jsonChoiceData = boxBallotMessage.jsonChoiceData;
  154. if (conversation.contact == nil) {
  155. msg.groupCreator = [MyIdentityStore sharedMyIdentityStore].identity;
  156. } else {
  157. msg.groupCreator = conversation.contact.identity;
  158. }
  159. return msg;
  160. }
  161. @end