GroupMessageReceiver.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /* _____ _
  2. * |_ _| |_ _ _ ___ ___ _ __ __ _
  3. * | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. * |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. *
  6. * Threema for Android
  7. * Copyright (c) 2014-2022 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. */
  21. package ch.threema.app.messagereceiver;
  22. import android.content.Intent;
  23. import android.graphics.Bitmap;
  24. import com.neilalexander.jnacl.NaCl;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import java.security.SecureRandom;
  28. import java.sql.SQLException;
  29. import java.util.ArrayList;
  30. import java.util.Arrays;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.UUID;
  34. import androidx.annotation.NonNull;
  35. import androidx.annotation.Nullable;
  36. import ch.threema.app.ThreemaApplication;
  37. import ch.threema.app.collections.Functional;
  38. import ch.threema.app.collections.IPredicateNonNull;
  39. import ch.threema.app.services.ApiService;
  40. import ch.threema.app.services.ContactService;
  41. import ch.threema.app.services.GroupMessagingService;
  42. import ch.threema.app.services.GroupService;
  43. import ch.threema.app.services.MessageService;
  44. import ch.threema.app.utils.GroupUtil;
  45. import ch.threema.app.utils.NameUtil;
  46. import ch.threema.app.utils.TestUtil;
  47. import ch.threema.base.ThreemaException;
  48. import ch.threema.base.utils.Utils;
  49. import ch.threema.domain.models.MessageId;
  50. import ch.threema.domain.protocol.ThreemaFeature;
  51. import ch.threema.domain.protocol.blob.BlobUploader;
  52. import ch.threema.domain.protocol.csp.ProtocolDefines;
  53. import ch.threema.domain.protocol.csp.messages.AbstractGroupMessage;
  54. import ch.threema.domain.protocol.csp.messages.GroupLocationMessage;
  55. import ch.threema.domain.protocol.csp.messages.GroupTextMessage;
  56. import ch.threema.domain.protocol.csp.messages.ballot.BallotData;
  57. import ch.threema.domain.protocol.csp.messages.ballot.BallotId;
  58. import ch.threema.domain.protocol.csp.messages.ballot.BallotVote;
  59. import ch.threema.domain.protocol.csp.messages.ballot.GroupBallotCreateMessage;
  60. import ch.threema.domain.protocol.csp.messages.ballot.GroupBallotVoteMessage;
  61. import ch.threema.domain.protocol.csp.messages.file.FileData;
  62. import ch.threema.domain.protocol.csp.messages.file.GroupFileMessage;
  63. import ch.threema.storage.DatabaseServiceNew;
  64. import ch.threema.storage.models.AbstractMessageModel;
  65. import ch.threema.storage.models.ContactModel;
  66. import ch.threema.storage.models.GroupMemberModel;
  67. import ch.threema.storage.models.GroupMessageModel;
  68. import ch.threema.storage.models.GroupMessagePendingMessageIdModel;
  69. import ch.threema.storage.models.GroupModel;
  70. import ch.threema.storage.models.MessageState;
  71. import ch.threema.storage.models.MessageType;
  72. import ch.threema.storage.models.access.GroupAccessModel;
  73. import ch.threema.storage.models.ballot.BallotModel;
  74. import ch.threema.storage.models.data.LocationDataModel;
  75. import ch.threema.storage.models.data.MessageContentsType;
  76. import ch.threema.storage.models.data.media.FileDataModel;
  77. public class GroupMessageReceiver implements MessageReceiver<GroupMessageModel> {
  78. private static final Logger logger = LoggerFactory.getLogger(GroupMessageReceiver.class);
  79. private final GroupModel group;
  80. private final GroupService groupService;
  81. private Bitmap avatar = null;
  82. private final DatabaseServiceNew databaseServiceNew;
  83. private final GroupMessagingService groupMessagingService;
  84. private ContactService contactService;
  85. private ApiService apiService;
  86. public GroupMessageReceiver(GroupModel group,
  87. GroupService groupService,
  88. DatabaseServiceNew databaseServiceNew,
  89. GroupMessagingService groupMessagingService,
  90. ContactService contactService,
  91. ApiService apiService) {
  92. this.group = group;
  93. this.groupService = groupService;
  94. this.databaseServiceNew = databaseServiceNew;
  95. this.groupMessagingService = groupMessagingService;
  96. this.contactService = contactService;
  97. this.apiService = apiService;
  98. }
  99. @Override
  100. public GroupMessageModel createLocalModel(MessageType type, @MessageContentsType int messageContentsType, Date postedAt) {
  101. GroupMessageModel m = new GroupMessageModel();
  102. m.setType(type);
  103. m.setMessageContentsType(messageContentsType);
  104. m.setGroupId(this.group.getId());
  105. m.setPostedAt(postedAt);
  106. m.setCreatedAt(new Date());
  107. m.setSaved(false);
  108. m.setUid(UUID.randomUUID().toString());
  109. return m;
  110. }
  111. @Override
  112. @Deprecated
  113. public GroupMessageModel createAndSaveStatusModel(String statusBody, Date postedAt) {
  114. GroupMessageModel m = new GroupMessageModel(true);
  115. m.setType(MessageType.TEXT);
  116. m.setGroupId(this.group.getId());
  117. m.setPostedAt(postedAt);
  118. m.setCreatedAt(new Date());
  119. m.setSaved(true);
  120. m.setUid(UUID.randomUUID().toString());
  121. m.setBody(statusBody);
  122. this.saveLocalModel(m);
  123. return m;
  124. }
  125. @Override
  126. public void saveLocalModel(GroupMessageModel save) {
  127. this.databaseServiceNew.getGroupMessageModelFactory().createOrUpdate(save);
  128. }
  129. @Override
  130. public boolean createBoxedTextMessage(final String text, final GroupMessageModel messageModel) throws ThreemaException {
  131. return this.sendMessage(messageId -> {
  132. GroupTextMessage boxedTextMessage = new GroupTextMessage();
  133. boxedTextMessage.setMessageId(messageId);
  134. boxedTextMessage.setText(text);
  135. if (messageId != null) {
  136. messageModel.setApiMessageId(messageId.toString());
  137. }
  138. return boxedTextMessage;
  139. }, messageModel);
  140. }
  141. @Override
  142. public boolean createBoxedLocationMessage(GroupMessageModel messageModel) throws ThreemaException {
  143. return this.sendMessage(messageId -> {
  144. final LocationDataModel locationDataModel = messageModel.getLocationData();
  145. final GroupLocationMessage msg = new GroupLocationMessage();
  146. msg.setMessageId(messageId);
  147. msg.setLatitude(locationDataModel.getLatitude());
  148. msg.setLongitude(locationDataModel.getLongitude());
  149. msg.setAccuracy(locationDataModel.getAccuracy());
  150. msg.setPoiName(locationDataModel.getPoi());
  151. msg.setPoiAddress(locationDataModel.getAddress());
  152. if (messageId != null) {
  153. messageModel.setApiMessageId(messageId.toString());
  154. }
  155. return msg;
  156. }, messageModel);
  157. }
  158. @Override
  159. public boolean createBoxedFileMessage(final byte[] thumbnailBlobId,
  160. final byte[] fileBlobId, final EncryptResult fileResult,
  161. final GroupMessageModel messageModel) throws ThreemaException {
  162. List<ContactModel> supportedContacts = contactService.getByIdentities(this.groupService.getGroupIdentities(group));
  163. String[] identities = new String[supportedContacts.size()];
  164. for(int n = 0; n < supportedContacts.size(); n++) {
  165. identities[n] = supportedContacts.get(n).getIdentity();
  166. }
  167. final FileDataModel modelFileData = messageModel.getFileData();
  168. return this.sendMessage(messageId -> {
  169. final GroupFileMessage fileMessage = new GroupFileMessage();
  170. fileMessage.setMessageId(messageId);
  171. final FileData fileData = new FileData();
  172. fileData
  173. .setFileBlobId(fileBlobId)
  174. .setThumbnailBlobId(thumbnailBlobId)
  175. .setEncryptionKey(fileResult.getKey())
  176. .setMimeType(modelFileData.getMimeType())
  177. .setThumbnailMimeType(modelFileData.getThumbnailMimeType())
  178. .setFileSize(modelFileData.getFileSize())
  179. .setFileName(modelFileData.getFileName())
  180. .setRenderingType(modelFileData.getRenderingType())
  181. .setDescription(modelFileData.getCaption())
  182. .setCorrelationId(messageModel.getCorrelationId())
  183. .setMetaData(modelFileData.getMetaData());
  184. fileMessage.setData(fileData);
  185. if (messageId != null) {
  186. messageModel.setApiMessageId(messageId.toString());
  187. }
  188. logger.info(
  189. "Enqueue group file message ID {} to {}",
  190. fileMessage.getMessageId(),
  191. fileMessage.getToIdentity()
  192. );
  193. return fileMessage;
  194. }, messageModel, identities);
  195. }
  196. @Override
  197. public boolean createBoxedBallotMessage(final BallotData ballotData,
  198. final BallotModel ballotModel,
  199. final String[] filteredIdentities,
  200. @Nullable GroupMessageModel abstractMessageModel) throws ThreemaException {
  201. final BallotId ballotId = new BallotId(Utils.hexStringToByteArray(ballotModel.getApiBallotId()));
  202. return this.sendMessage(messageId -> {
  203. final GroupBallotCreateMessage msg = new GroupBallotCreateMessage();
  204. msg.setMessageId(messageId);
  205. msg.setBallotCreator(ballotModel.getCreatorIdentity());
  206. msg.setBallotId(ballotId);
  207. msg.setData(ballotData);
  208. if (abstractMessageModel != null && messageId != null) {
  209. abstractMessageModel.setApiMessageId(messageId.toString());
  210. }
  211. logger.info("Enqueue ballot message ID {} to {}", msg.getMessageId(), msg.getToIdentity());
  212. return msg;
  213. }, null, filteredIdentities);
  214. }
  215. @Override
  216. public boolean createBoxedBallotVoteMessage(final BallotVote[] votes, final BallotModel ballotModel) throws ThreemaException {
  217. final BallotId ballotId = new BallotId(Utils.hexStringToByteArray(ballotModel.getApiBallotId()));
  218. String[] toIdentities = this.groupService.getGroupIdentities(this.group);
  219. switch (ballotModel.getType()) {
  220. case RESULT_ON_CLOSE:
  221. String toIdentity = null;
  222. for(String i: toIdentities) {
  223. if(TestUtil.compare(i, ballotModel.getCreatorIdentity())) {
  224. toIdentity = i;
  225. }
  226. }
  227. if(toIdentity == null) {
  228. throw new ThreemaException("cannot send a ballot vote to another group!");
  229. }
  230. toIdentities = new String[] {toIdentity};
  231. //only to the creator
  232. break;
  233. }
  234. return this.sendMessage(messageId -> {
  235. final GroupBallotVoteMessage msg = new GroupBallotVoteMessage();
  236. msg.setMessageId(messageId);
  237. msg.setBallotCreator(ballotModel.getCreatorIdentity());
  238. msg.setBallotId(ballotId);
  239. for (BallotVote v : votes) {
  240. msg.getBallotVotes().add(v);
  241. }
  242. logger.info("Enqueue ballot vote message ID {} to {}", msg.getMessageId(), msg.getToIdentity());
  243. return msg;
  244. }, null, toIdentities);
  245. }
  246. @Override
  247. public List<GroupMessageModel> loadMessages(MessageService.MessageFilter filter) throws SQLException {
  248. return this.databaseServiceNew.getGroupMessageModelFactory().find(
  249. this.group.getId(),
  250. filter);
  251. }
  252. @Override
  253. public long getMessagesCount() {
  254. return this.databaseServiceNew.getGroupMessageModelFactory().countMessages(
  255. this.group.getId());
  256. }
  257. @Override
  258. public long getUnreadMessagesCount() {
  259. return this.databaseServiceNew.getGroupMessageModelFactory().countUnreadMessages(
  260. this.group.getId());
  261. }
  262. @Override
  263. public List<GroupMessageModel> getUnreadMessages() {
  264. return this.databaseServiceNew.getGroupMessageModelFactory().getUnreadMessages(
  265. this.group.getId());
  266. }
  267. public GroupModel getGroup() {
  268. return this.group;
  269. }
  270. @Override
  271. public boolean isEqual(MessageReceiver o) {
  272. return o instanceof GroupMessageReceiver && ((GroupMessageReceiver) o).getGroup().getId() == this.getGroup().getId();
  273. }
  274. @Override
  275. public String getDisplayName() {
  276. return NameUtil.getDisplayName(this.group, this.groupService);
  277. }
  278. @Override
  279. public String getShortName() {
  280. return getDisplayName();
  281. }
  282. @Override
  283. public void prepareIntent(Intent intent) {
  284. intent.putExtra(ThreemaApplication.INTENT_DATA_GROUP, this.group.getId());
  285. }
  286. @Override
  287. public Bitmap getNotificationAvatar() {
  288. //lacy
  289. if(this.avatar == null && this.groupService != null) {
  290. this.avatar = this.groupService.getAvatar(group, false);
  291. }
  292. return this.avatar;
  293. }
  294. @Override
  295. @Deprecated
  296. public int getUniqueId() {
  297. if (this.groupService != null && this.group != null) {
  298. return this.groupService.getUniqueId(this.group);
  299. }
  300. return 0;
  301. }
  302. @Override
  303. public String getUniqueIdString() {
  304. if (this.groupService != null && this.group != null) {
  305. return this.groupService.getUniqueIdString(this.group);
  306. }
  307. return "";
  308. }
  309. @Override
  310. public EncryptResult encryptFileData(final byte[] fileData) throws ThreemaException {
  311. //generate random symmetric key for file encryption
  312. SecureRandom rnd = new SecureRandom();
  313. final byte[] encryptionKey = new byte[NaCl.SYMMKEYBYTES];
  314. rnd.nextBytes(encryptionKey);
  315. NaCl.symmetricEncryptDataInplace(fileData, encryptionKey, ProtocolDefines.FILE_NONCE);
  316. BlobUploader blobUploaderThumbnail = apiService.createUploader(fileData);
  317. blobUploaderThumbnail.setVersion(ThreemaApplication.getAppVersion());
  318. return new EncryptResult() {
  319. @Override
  320. public byte[] getData() {
  321. return fileData;
  322. }
  323. @Override
  324. public byte[] getKey() {
  325. return encryptionKey;
  326. }
  327. @Override
  328. public byte[] getNonce() {
  329. return ProtocolDefines.FILE_NONCE;
  330. }
  331. @Override
  332. public int getSize() {
  333. return fileData.length;
  334. }
  335. };
  336. }
  337. @Override
  338. public EncryptResult encryptFileThumbnailData(byte[] fileThumbnailData, final byte[] encryptionKey) throws ThreemaException {
  339. final byte[] thumbnailBoxed = NaCl.symmetricEncryptData(fileThumbnailData, encryptionKey, ProtocolDefines.FILE_THUMBNAIL_NONCE);
  340. BlobUploader blobUploaderThumbnail = apiService.createUploader(thumbnailBoxed);
  341. blobUploaderThumbnail.setVersion(ThreemaApplication.getAppVersion());
  342. return new EncryptResult() {
  343. @Override
  344. public byte[] getData() {
  345. return thumbnailBoxed;
  346. }
  347. @Override
  348. public byte[] getKey() {
  349. return encryptionKey;
  350. }
  351. @Override
  352. public byte[] getNonce() {
  353. return ProtocolDefines.FILE_THUMBNAIL_NONCE;
  354. }
  355. @Override
  356. public int getSize() {
  357. return thumbnailBoxed.length;
  358. }
  359. };
  360. }
  361. @Override
  362. public boolean isMessageBelongsToMe(AbstractMessageModel message) {
  363. return message instanceof GroupMessageModel
  364. && ((GroupMessageModel)message).getGroupId() == this.group.getId();
  365. }
  366. @Override
  367. public boolean sendMediaData() {
  368. // don't really send off group media if user is the only group member left - keep it local
  369. String[] groupIdentities = this.groupService.getGroupIdentities(this.group);
  370. return groupIdentities == null || groupIdentities.length != 1 || !groupService.isGroupMember(this.group);
  371. }
  372. @Override
  373. public boolean offerRetry() {
  374. return false;
  375. }
  376. @Override
  377. public boolean validateSendingPermission(OnSendingPermissionDenied onSendingPermissionDenied) {
  378. //TODO: cache access? performance
  379. GroupAccessModel access = this.groupService.getAccess(getGroup(), true);
  380. if(access == null) {
  381. //what?
  382. return false;
  383. }
  384. if(!access.getCanSendMessageAccess().isAllowed()) {
  385. if(onSendingPermissionDenied != null) {
  386. onSendingPermissionDenied.denied(access.getCanSendMessageAccess().getNotAllowedTestResourceId());
  387. }
  388. return false;
  389. }
  390. return true;
  391. }
  392. @Override
  393. @MessageReceiverType
  394. public int getType() {
  395. return Type_GROUP;
  396. }
  397. @Override
  398. public String[] getIdentities() {
  399. return this.groupService.getGroupIdentities(this.group);
  400. }
  401. @Override
  402. public String[] getIdentities(final int requiredFeature) {
  403. List<GroupMemberModel> members = Functional.filter(this.groupService.getGroupMembers(this.group), new IPredicateNonNull<GroupMemberModel>() {
  404. @Override
  405. public boolean apply(@NonNull GroupMemberModel groupMemberModel) {
  406. ContactModel model = contactService.getByIdentity(groupMemberModel.getIdentity());
  407. return model != null && ThreemaFeature.hasFeature(model.getFeatureMask(), requiredFeature);
  408. }
  409. });
  410. String[] identities = new String[members.size()];
  411. for(int p = 0; p < members.size(); p++) {
  412. identities[p] = members.get(p).getIdentity();
  413. }
  414. return identities;
  415. }
  416. private boolean sendMessage(GroupMessagingService.CreateApiMessage createApiMessage, AbstractMessageModel messageModel) throws ThreemaException {
  417. return this.sendMessage(createApiMessage, messageModel, null);
  418. }
  419. /**
  420. * Send a message to a group.
  421. *
  422. * @param createApiMessage A callback that creates the {@link AbstractGroupMessage} that will be sent.
  423. * @param messageModel The model representing this message. It will be updated with status updates.
  424. * @param groupIdentities List of group identities that will receive this group message. If set to null, the identities belonging to the current group will be used.
  425. * @return
  426. * @throws ThreemaException
  427. */
  428. private boolean sendMessage(
  429. @NonNull GroupMessagingService.CreateApiMessage createApiMessage,
  430. final AbstractMessageModel messageModel,
  431. @Nullable String[] groupIdentities
  432. ) throws ThreemaException {
  433. if(groupIdentities == null) {
  434. groupIdentities = this.groupService.getGroupIdentities(this.group);
  435. }
  436. // do not send messages to a broadcast/gateway group that does not receive and store incoming messages
  437. if (groupIdentities.length >= 2
  438. && !GroupUtil.sendMessageToCreator(group)) {
  439. // remove creator from list of recipients
  440. ArrayList<String> fixedGroupIdentities = new ArrayList<>(Arrays.asList(groupIdentities));
  441. fixedGroupIdentities.remove(group.getCreatorIdentity());
  442. groupIdentities = fixedGroupIdentities.toArray(new String[0]);
  443. }
  444. // don't really send off messages if user is the only group member left - keep them local
  445. if (groupIdentities.length == 1 && groupService.isGroupMember(this.group)) {
  446. if(messageModel != null) {
  447. MessageId messageId = new MessageId();
  448. messageModel.setIsQueued(true);
  449. messageModel.setApiMessageId(messageId.toString());
  450. groupService.setIsArchived(group, false);
  451. messageModel.setState(MessageState.READ);
  452. messageModel.setModifiedAt(new Date());
  453. return true;
  454. }
  455. }
  456. int enqueuedMessagesCount = this.groupMessagingService.sendMessage(this.group, groupIdentities, createApiMessage, queuedGroupMessage -> {
  457. // Set as queued (first)
  458. groupService.setIsArchived(group, false);
  459. if(messageModel == null) {
  460. return;
  461. }
  462. if(!messageModel.isQueued()) {
  463. messageModel.setIsQueued(true);
  464. }
  465. //save identity message model
  466. databaseServiceNew.getGroupMessagePendingMessageIdModelFactory()
  467. .create(
  468. new GroupMessagePendingMessageIdModel(messageModel.getId(), queuedGroupMessage.getMessageId().toString()));
  469. });
  470. return enqueuedMessagesCount > 0;
  471. }
  472. @Override
  473. public String toString() {
  474. return "GroupMessageReceiver (GroupId = " + this.group.getId() + ")";
  475. }
  476. }