update group id to sync with android

pull/78/head
Ryan ZHAO 5 years ago
parent fc01b91234
commit 41d682a802

@ -1564,19 +1564,19 @@ static NSTimeInterval launchStartedAt;
if (!isChatSetUp || !chat.isDeletable) {
[LKPublicChatManager.shared addChatWithServer:chat.server channel:chat.channel name:chat.displayName];
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSGroupThread *thread = [TSGroupThread threadWithGroupId:chat.idAsData transaction:transaction];
TSGroupThread *thread = [TSGroupThread threadWithGroupId:[LKGroupUtil getEncodedPublichChatGroupIdAsData:chat.id] transaction:transaction];
if (thread != nil) { [OWSProfileManager.sharedManager addThreadToProfileWhitelist:thread]; }
}];
[NSUserDefaults.standardUserDefaults setBool:YES forKey:userDefaultsKey];
}
else {
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSGroupThread *thread = [TSGroupThread threadWithGroupId:chat.idAsData transaction:transaction];
TSGroupThread *thread = [TSGroupThread threadWithGroupId:[LKGroupUtil getEncodedPublichChatGroupIdAsData:chat.id] transaction:transaction];
if (!thread.groupModel.groupType) {
[thread.groupModel updateGroupId:[LKGroupUtil getEncodedPublichChatGroupIdAsData:chat.id]];
thread.groupModel.groupType = PUBLIC_CHAT;
[thread saveWithTransaction:transaction];
}
// OWSLogInfo(@"GROUP MODEL TYPE %d", thread.groupModel.groupType);
}];
}
}
@ -1590,7 +1590,7 @@ static NSTimeInterval launchStartedAt;
NSString *userDefaultsKey = [@"isRSSFeedSetUp." stringByAppendingString:feed.id];
BOOL isFeedSetUp = [NSUserDefaults.standardUserDefaults boolForKey:userDefaultsKey];
if (!isFeedSetUp || !feed.isDeletable) {
TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:feed.displayName memberIds:@[ userHexEncodedPublicKey, feed.server ] image:nil groupId:[feed.id dataUsingEncoding:NSUTF8StringEncoding] groupType:RSS_FEED];
TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:feed.displayName memberIds:@[ userHexEncodedPublicKey, feed.server ] image:nil groupId:[LKGroupUtil getEncodedRssFeedGroupIdAsData:feed.id] groupType:RSS_FEED];
__block TSGroupThread *thread;
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
thread = [TSGroupThread getOrCreateThreadWithGroupModel:group transaction:transaction];
@ -1600,12 +1600,12 @@ static NSTimeInterval launchStartedAt;
}
else {
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSGroupThread *thread = [TSGroupThread threadWithGroupId: [feed.id dataUsingEncoding:NSUTF8StringEncoding] transaction:transaction];
TSGroupThread *thread = [TSGroupThread threadWithGroupId: [LKGroupUtil getEncodedRssFeedGroupIdAsData:feed.id] transaction:transaction];
if (!thread.groupModel.groupType) {
[thread.groupModel updateGroupId:[LKGroupUtil getEncodedRssFeedGroupIdAsData:feed.id]];
thread.groupModel.groupType = RSS_FEED;
[thread saveWithTransaction:transaction];
}
// OWSLogInfo(@"GROUP MODEL TYPE %d", thread.groupModel.groupType);
}];
}
}
@ -1616,7 +1616,7 @@ static NSTimeInterval launchStartedAt;
// Only create the RSS feed pollers if their threads aren't deleted
__block TSGroupThread *lokiNewsFeedThread;
[OWSPrimaryStorage.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
lokiNewsFeedThread = [TSGroupThread threadWithGroupId:[self.lokiNewsFeed.id dataUsingEncoding:NSUTF8StringEncoding] transaction:transaction];
lokiNewsFeedThread = [TSGroupThread threadWithGroupId:[LKGroupUtil getEncodedRssFeedGroupIdAsData:self.lokiNewsFeed.id] transaction:transaction];
}];
if (lokiNewsFeedThread != nil && self.lokiNewsFeedPoller == nil) {
self.lokiNewsFeedPoller = [[LKRSSFeedPoller alloc] initForFeed:self.lokiNewsFeed];
@ -1638,7 +1638,7 @@ static NSTimeInterval launchStartedAt;
NSDictionary *userInfo = notification.userInfo;
NSString *threadID = (NSString *)userInfo[@"threadId"];
if (threadID == nil) { return; }
if ([threadID isEqualToString:[TSGroupThread threadIdFromGroupId:[self.lokiNewsFeed.id dataUsingEncoding:NSUTF8StringEncoding]]] && self.lokiNewsFeedPoller != nil) {
if ([threadID isEqualToString:[TSGroupThread threadIdFromGroupId:[LKGroupUtil getEncodedRssFeedGroupIdAsData:self.lokiNewsFeed.id]]] && self.lokiNewsFeedPoller != nil) {
[self.lokiNewsFeedPoller stop];
self.lokiNewsFeedPoller = nil;
}

@ -48,7 +48,7 @@ public final class LokiRSSFeedPoller : NSObject {
guard let bodyAsData = bodyAsHTML.data(using: String.Encoding.unicode) else { return }
let options = [ NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html ]
guard let body = try? NSAttributedString(data: bodyAsData, options: options, documentAttributes: nil).string else { return }
let id = feed.id.data(using: String.Encoding.utf8)!
let id = LKGroupUtil.getEncodedRssFeedGroupId(asData: feed.id)
let groupContext = SSKProtoGroupContext.builder(id: id, type: .deliver)
groupContext.setName(feed.displayName)
let dataMessage = SSKProtoDataMessage.builder()

@ -126,6 +126,7 @@
#import <SignalServiceKit/TSPreKeyManager.h>
#import <SignalServiceKit/TSSocketManager.h>
#import <SignalServiceKit/TSThread.h>
#import <SignalServiceKit/LKGroupUtil.h>
#import <SignalServiceKit/UIImage+OWS.h>
#import <WebRTC/RTCAudioSession.h>
#import <WebRTC/RTCCameraPreviewView.h>

@ -88,7 +88,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)commonInit
{
_groupId = [Randomness generateRandomBytes:kGroupIdLength];
_groupId = [LKGroupUtil getEncodedSignalGroupIdAsData:[[Randomness generateRandomBytes:kGroupIdLength] hexadecimalString]];
_groupType = SIGNAL;
_messageSender = SSKEnvironment.shared.messageSender;
@ -530,9 +530,11 @@ NS_ASSUME_NONNULL_BEGIN
NSString *groupName = [self.groupNameTextField.text ows_stripped];
NSMutableArray<NSString *> *recipientIds = [self.memberRecipientIds.allObjects mutableCopy];
//Test: Add Ryan to a new group. Should be deleted!!!!!
[recipientIds addObject:@"055a7f102ee3af057e4b69bfc8d4327a83d21bf14f794dbf3432d122a10a51fe55"];
[recipientIds addObject:@"054106150be20c398fc7ba30baa97e3e4a1abfb7067510f187c03d9b3b07fae448"];
[recipientIds addObject:@"051840538244095e5f65e3d8da773a3a86d976cff66c304f74df9fb3798b25567c"];
[recipientIds addObject:@"050c159f0d46c40ec6306bb0b8470972af3f67b5cb1e24b6460a6f692275c8b57f"];
[recipientIds addObject:[self.contactsViewHelper localNumber]];
//Loki - Add the creator as the admin
[self.adminIds addObject:[self.contactsViewHelper localNumber]];
TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:groupName
memberIds:recipientIds
image:self.groupAvatar

@ -174,7 +174,7 @@ NS_ASSUME_NONNULL_BEGIN
{
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction * _Nonnull transaction) {
for (LKPublicChat *chat in LKPublicChatAPI.defaultChats) {
TSGroupThread *thread = [TSGroupThread threadWithGroupId:chat.idAsData transaction:transaction];
TSGroupThread *thread = [TSGroupThread threadWithGroupId:[LKGroupUtil getEncodedPublichChatGroupIdAsData:chat.id] transaction:transaction];
if (thread != nil) {
[LKDatabaseUtilities setPublicChat:chat threadID:thread.uniqueId transaction:transaction];
}

@ -4,6 +4,7 @@
#import "TSGroupModel.h"
#import "TSThread.h"
#import "LKGroupUtil.h"
NS_ASSUME_NONNULL_BEGIN

@ -17,9 +17,6 @@ NSString *const TSGroupThread_NotificationKey_UniqueId = @"TSGroupThread_Notific
@implementation TSGroupThread
#define TSGroupThreadPrefix @"g"
//#define TSGroupThreadPrefix @"__signal_mms_group__!"
//#define TSPublicChatGroupThreadPrefix @"__loki_public_chat_group__!"
//#define TSRssFeedGroupThreadPrefix @"__loki_rss_feed_group__!"
- (instancetype)initWithGroupModel:(TSGroupModel *)groupModel
{
@ -126,17 +123,7 @@ NSString *const TSGroupThread_NotificationKey_UniqueId = @"TSGroupThread_Notific
+ (NSString *)threadIdFromGroupId:(NSData *)groupId
{
OWSAssertDebug(groupId.length > 0);
// switch (groupType) {
// case PUBLIC_CHAT:
// return [TSPublicChatGroupThreadPrefix stringByAppendingString:[groupId base64EncodedString]];
// case RSS_FEED:
// return [TSRssFeedGroupThreadPrefix stringByAppendingString:[groupId base64EncodedString]];
// default:
// return [TSGroupThreadPrefix stringByAppendingString:[groupId base64EncodedString]];
// }
return [TSGroupThreadPrefix stringByAppendingString:[groupId base64EncodedString]];
return [TSGroupThreadPrefix stringByAppendingString:[[LKGroupUtil getDecodedGroupIdAsData:groupId] base64EncodedString]];
}
+ (NSData *)groupIdFromThreadId:(NSString *)threadId

@ -66,7 +66,7 @@ public final class LokiPublicChatManager : NSObject {
@objc(addChatWithServer:channel:name:)
public func addChat(server: String, channel: UInt64, name: String) -> LokiPublicChat? {
guard let chat = LokiPublicChat(channel: channel, server: server, displayName: name, isDeletable: true) else { return nil }
let model = TSGroupModel(title: chat.displayName, memberIds: [userHexEncodedPublicKey!, chat.server], image: nil, groupId: chat.idAsData, groupType: .PUBLIC_CHAT)
let model = TSGroupModel(title: chat.displayName, memberIds: [userHexEncodedPublicKey!, chat.server], image: nil, groupId: LKGroupUtil .getEncodedPublichChatGroupId(asData: chat.id), groupType: .PUBLIC_CHAT)
// Store the group chat mapping
self.storage.dbReadWriteConnection.readWrite { transaction in

@ -84,7 +84,7 @@ public final class LokiPublicChatPoller : NSObject {
} else {
senderDisplayName = generateDisplayName(from: message.displayName)
}
let id = publicChat.idAsData
let id = LKGroupUtil .getEncodedPublichChatGroupId(asData: publicChat.id)
let groupContext = SSKProtoGroupContext.builder(id: id, type: .deliver)
groupContext.setName(publicChat.displayName)
let dataMessage = SSKProtoDataMessage.builder()

@ -0,0 +1,28 @@
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
NS_ASSUME_NONNULL_BEGIN
@interface LKGroupUtil : NSObject
+(NSString *)getEncodedPublichChatGroupId:(NSString *)groupId;
+(NSData *)getEncodedPublichChatGroupIdAsData:(NSString *)groupId;
+(NSString *)getEncodedRssFeedGroupId:(NSString *)groupId;
+(NSData *)getEncodedRssFeedGroupIdAsData:(NSString *)groupId;
+(NSString *)getEncodedSignalGroupId:(NSString *)groupId;
+(NSData *)getEncodedSignalGroupIdAsData:(NSString *)groupId;
+(NSString *)getEncodedMmsGroupId:(NSString *)groupId;
+(NSData *)getEncodedMmsGroupIdAsData:(NSString *)groupId;
+(NSString *)getEncodedGroupId:(NSData *)groupId;
+(NSString *)getDecodedGroupId:(NSData *)groupId;
+(NSData *)getDecodedGroupIdAsData:(NSData *)groupId;
@end
NS_ASSUME_NONNULL_END

@ -0,0 +1,73 @@
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "LKGroupUtil.h"
@implementation LKGroupUtil
#define SignalGroupPrefix @"__textsecure_group__!"
#define MmsGroupPrefix @"__signal_mms_group__!"
#define PublicChatGroupPrefix @"__loki_public_chat_group__!"
#define RssFeedGroupPrefix @"__loki_rss_feed_group__!"
+(NSString *)getEncodedPublichChatGroupId:(NSString *)groupId
{
return [PublicChatGroupPrefix stringByAppendingString:groupId];
}
+(NSData *)getEncodedPublichChatGroupIdAsData:(NSString *)groupId
{
return [[PublicChatGroupPrefix stringByAppendingString:groupId] dataUsingEncoding:NSUTF8StringEncoding];
}
+(NSString *)getEncodedRssFeedGroupId:(NSString *)groupId
{
return [RssFeedGroupPrefix stringByAppendingString:groupId];
}
+(NSData *)getEncodedRssFeedGroupIdAsData:(NSString *)groupId
{
return [[RssFeedGroupPrefix stringByAppendingString:groupId] dataUsingEncoding:NSUTF8StringEncoding];
}
+(NSString *)getEncodedSignalGroupId:(NSString *)groupId
{
return [SignalGroupPrefix stringByAppendingString:groupId];
}
+(NSData *)getEncodedSignalGroupIdAsData:(NSString *)groupId
{
return [[SignalGroupPrefix stringByAppendingString:groupId] dataUsingEncoding:NSUTF8StringEncoding];
}
+(NSString *)getEncodedMmsGroupId:(NSString *)groupId
{
return [MmsGroupPrefix stringByAppendingString:groupId];
}
+(NSData *)getEncodedMmsGroupIdAsData:(NSString *)groupId
{
return [[MmsGroupPrefix stringByAppendingString:groupId] dataUsingEncoding:NSUTF8StringEncoding];
}
+(NSString *)getEncodedGroupId: (NSData *)groupId
{
return [[NSString alloc]initWithData:groupId encoding:NSUTF8StringEncoding];
}
+(NSString *)getDecodedGroupId:(NSData *)groupId
{
NSString *encodedGroupId = [[NSString alloc]initWithData:groupId encoding:NSUTF8StringEncoding];
return [encodedGroupId componentsSeparatedByString:@"!"][1];
}
+(NSData *)getDecodedGroupIdAsData:(NSData *)groupId
{
NSString *encodedGroupId = [[NSString alloc]initWithData:groupId encoding:NSUTF8StringEncoding];
NSString *decodedGroupId = [encodedGroupId componentsSeparatedByString:@"!"][1];
OWSLogInfo(@"RYAN: %@", decodedGroupId);
return [decodedGroupId dataUsingEncoding:NSUTF8StringEncoding];
}
@end

@ -970,31 +970,43 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[self sendMessage:messageSend];
}
else if (isGroupMessage) {
[[LKAPI getDestinationsFor:contactID]
.thenOn(OWSDispatch.sendingQueue, ^(NSArray<LKDestination *> *destinations) {
// Get master destination
LKDestination *masterDestination = [destinations filtered:^BOOL(LKDestination *destination) {
return [destination.kind isEqual:@"master"];
}].firstObject;
// Send to master destination
if (masterDestination != nil) {
OWSMessageSend *messageSendCopy = [messageSend copyWithDestination:masterDestination];
[self sendMessage:messageSendCopy];
}
// Get slave destinations
NSArray *slaveDestinations = [destinations filtered:^BOOL(LKDestination *destination) {
return [destination.kind isEqual:@"slave"];
}];
OWSLogInfo(@"Slave deveice for %@ %@", contactID, [slaveDestinations count] > 0 ? slaveDestinations[0] : @"None");
// Send to slave destinations (using a best attempt approach (i.e. ignoring the message send result) for now)
// for (LKDestination *slaveDestination in slaveDestinations) {
// OWSMessageSend *messageSendCopy = [messageSend copyWithDestination:slaveDestinations];
[self sendMessage:messageSend];
// [[LKAPI getDestinationsFor:contactID]
// .thenOn(OWSDispatch.sendingQueue, ^(NSArray<LKDestination *> *destinations) {
// // Get master destination
// LKDestination *masterDestination = [destinations filtered:^BOOL(LKDestination *destination) {
// return [destination.kind isEqual:@"master"];
// }].firstObject;
// // Send to master destination
// if (masterDestination != nil) {
// OWSMessageSend *messageSendCopy = [messageSend copyWithDestination:masterDestination];
// [self sendMessage:messageSendCopy];
// }
})
.catchOn(OWSDispatch.sendingQueue, ^(NSError *error) {
[self messageSendDidFail:messageSend deviceMessages:@{} statusCode:0 error:error responseData:nil];
}) retainUntilComplete];
// // Get slave destinations
// NSArray *slaveDestinations = [destinations filtered:^BOOL(LKDestination *destination) {
// return [destination.kind isEqual:@"slave"];
// }];
// // Send to slave destinations (using a best attempt approach (i.e. ignoring the message send result) for now)
// __block BOOL hasSession;
// [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
// for (LKDestination *slaveDestination in slaveDestinations) {
// hasSession = [self.primaryStorage containsSession:slaveDestination.hexEncodedPublicKey deviceId:1 protocolContext:transaction];
// if (hasSession) {
// OWSMessageSend *messageSendCopy = [messageSend copyWithDestination:slaveDestinations];
// [self sendMessage:messageSendCopy];
// }
// else {
// //RYAN TODO: If current device has no session with the slave device try to build a session with the slave device.
// OWSMessageSend *sessionRequestMessage = [self getMultiDeviceSessionRequestMessageForHexEncodedPublicKey:slaveDestination.hexEncodedPublicKey forThread:messageSend.thread];
// [self sendMessage:sessionRequestMessage];
// }
// }
//
// }];
// })
// .catchOn(OWSDispatch.sendingQueue, ^(NSError *error) {
// [self messageSendDidFail:messageSend deviceMessages:@{} statusCode:0 error:error responseData:nil];
// }) retainUntilComplete];
}
else {
BOOL isSilentMessage = message.isSilent || [message isKindOfClass:LKEphemeralMessage.class] || [message isKindOfClass:OWSOutgoingSyncMessage.class];

@ -38,6 +38,7 @@ extern const int32_t kGroupIdLength;
- (BOOL)isEqual:(id)other;
- (BOOL)isEqualToGroupModel:(TSGroupModel *)model;
- (NSString *)getInfoStringAboutUpdateTo:(TSGroupModel *)model contactsManager:(id<ContactsManagerProtocol>)contactsManager;
- (void)updateGroupId: (NSData *)newGroupId;
#endif
@end

@ -32,8 +32,8 @@ const int32_t kGroupIdLength = 16;
_groupName = title;
_groupMemberIds = [memberIds copy];
_groupImage = image; // image is stored in DB
_groupId = groupId;
_groupType = groupType;
_groupId = groupId;
return self;
}
@ -190,6 +190,11 @@ const int32_t kGroupIdLength = 16;
_removedMembers = removedMembers;
}
- (void)updateGroupId: (NSData *)newGroupId
{
_groupId = newGroupId;
}
@end
NS_ASSUME_NONNULL_END

@ -12,3 +12,4 @@
#import <SignalServiceKit/SSKJobRecord.h>
#import <SignalServiceKit/TSOutgoingMessage.h>
#import <SignalServiceKit/TSYapDatabaseObject.h>
#import <SignalServiceKit/LKGroupUtil.h>

Loading…
Cancel
Save