Skip to content

Commit

Permalink
actually functional networking lol
Browse files Browse the repository at this point in the history
(I tested on device this time and it works well)
  • Loading branch information
TorutheRedFox committed Oct 19, 2023
1 parent 7218b4d commit 76a21da
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 261 deletions.
46 changes: 16 additions & 30 deletions Discord Classic.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,9 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
50524F424C454D533F /* ShellScript */ = {
099DA439218A990D009DA3B5 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
buildActionMask = 12;
files = (
);
inputPaths = (
Expand All @@ -470,15 +470,11 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "export CODESIGN_ALLOCATE=/Library/JailCoder/codesign_allocate\nif [ \"${PLATFORM_NAME}\" == \"iphoneos\" ]; then\n/Library/JailCoder/Entitlements/Entitlements.py \"my.company.${PROJECT_NAME}\" \"${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent\";\ncodesign -f -s \"iPhone Developer\" --entitlements \"${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent\" \"${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/\"\nfi";
shellScript = "";
};
/* End PBXShellScriptBuildPhase section */


/* Begin PBXShellScriptBuildPhase section */
099DA439218A990D009DA3B5 /* ShellScript */ = {
50524F424C454D533F /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 12;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
Expand All @@ -487,7 +483,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "";
shellScript = "export CODESIGN_ALLOCATE=/Library/JailCoder/codesign_allocate\nif [ \"${PLATFORM_NAME}\" == \"iphoneos\" ]; then\n/Library/JailCoder/Entitlements/Entitlements.py \"my.company.${PROJECT_NAME}\" \"${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent\";\ncodesign -f -s \"iPhone Developer\" --entitlements \"${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/${PROJECT_NAME}.xcent\" \"${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/\"\nfi";
};
/* End PBXShellScriptBuildPhase section */

Expand Down Expand Up @@ -543,10 +539,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = (
armv7s,
armv7,
);
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand All @@ -569,17 +562,15 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos6.1;
TARGETED_DEVICE_FAMILY = "1,2";
VALID_ARCHS = "";
};
name = Debug;
};
09C6B955204A0F55007EAC26 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = (
armv7s,
armv7,
);
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand All @@ -596,24 +587,22 @@
SDKROOT = iphoneos6.1;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VALID_ARCHS = "";
};
name = Release;
};
09C6B957204A0F55007EAC26 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = (
armv7s,
armv7,
);
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_WARN_OBJCPP_ARC_ABI = YES;
CODE_SIGN_IDENTITY = "";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
COMPRESS_PNG_FILES = NO;
COMPRESS_PNG_FILES = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Discord Classic/Discord Classic-Prefix.pch";
INFOPLIST_FILE = "Discord Classic/Discord Classic-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos6.1;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
Expand All @@ -626,18 +615,15 @@
09C6B958204A0F55007EAC26 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = (
armv7s,
armv7,
);
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_WARN_OBJCPP_ARC_ABI = YES;
CODE_SIGN_IDENTITY = "";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
COMPRESS_PNG_FILES = NO;
COMPRESS_PNG_FILES = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Discord Classic/Discord Classic-Prefix.pch";
INFOPLIST_FILE = "Discord Classic/Discord Classic-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos6.1;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
Expand Down
113 changes: 57 additions & 56 deletions Discord Classic/DCChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ -(void)checkIfRead{


- (void)sendMessage:(NSString*)message {
//dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
dispatch_queue_t apiQueue = dispatch_queue_create("Discord::API::Send", NULL);

dispatch_async(apiQueue, ^{
NSURL* channelURL = [NSURL URLWithString: [NSString stringWithFormat:@"https://discordapp.com/api/channels/%@/messages", self.snowflake]];

NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:channelURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
Expand All @@ -49,24 +51,26 @@ - (void)sendMessage:(NSString*)message {
[urlRequest setHTTPBody:[NSData dataWithBytes:[messageString UTF8String] length:[messageString length]]];
[urlRequest addValue:DCServerCommunicator.sharedInstance.token forHTTPHeaderField:@"Authorization"];
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSError *error = nil;
NSHTTPURLResponse *responseCode = nil;
/*[UIApplication sharedApplication].networkActivityIndicatorVisible++;
[DCTools checkData:[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&responseCode error:&error] withError:error];
[UIApplication sharedApplication].networkActivityIndicatorVisible--;*/
NSLog(@"Showing network indicator (sendMessage) %d", (int)[UIApplication sharedApplication].networkActivityIndicatorVisible);

[UIApplication sharedApplication].networkActivityIndicatorVisible++;
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connError) {
NSLog(@"Hiding network indicator (sendMessage) %d", (int)[UIApplication sharedApplication].networkActivityIndicatorVisible);
[DCTools checkData:[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&responseCode error:&error] withError:error];
if ([UIApplication sharedApplication].networkActivityIndicatorVisible > 0)
[UIApplication sharedApplication].networkActivityIndicatorVisible--;
}];
//});
else if ([UIApplication sharedApplication].networkActivityIndicatorVisible < 0)
[UIApplication sharedApplication].networkActivityIndicatorVisible = 0;
});

dispatch_release(apiQueue);
}



- (void)sendImage:(UIImage*)image {
//dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
dispatch_queue_t apiQueue = dispatch_queue_create("Discord::API::Send::Image", NULL);
dispatch_async(apiQueue, ^{
NSURL* channelURL = [NSURL URLWithString: [NSString stringWithFormat:@"https://discordapp.com/api/channels/%@/messages", self.snowflake]];

NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:channelURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:40];
Expand All @@ -92,72 +96,71 @@ - (void)sendImage:(UIImage*)image {

NSError *error = nil;
NSHTTPURLResponse *responseCode = nil;
int attempts = 0;
while (attempts == 0 || (attempts <= 10 && error.code == NSURLErrorTimedOut)) {
attempts++;
error = nil;
//[DCTools checkData:[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&responseCode error:&error] withError:error];
//[UIApplication sharedApplication].networkActivityIndicatorVisible--;
NSLog(@"Showing network indicator (sendImage) %d", (int)[UIApplication sharedApplication].networkActivityIndicatorVisible);
[UIApplication sharedApplication].networkActivityIndicatorVisible++;
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connError) {
NSLog(@"Hiding network indicator (sendImage) %d", (int)[UIApplication sharedApplication].networkActivityIndicatorVisible);
[UIApplication sharedApplication].networkActivityIndicatorVisible--;
}];
}
//});

[UIApplication sharedApplication].networkActivityIndicatorVisible++;
[DCTools checkData:[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&responseCode error:&error] withError:error];
if ([UIApplication sharedApplication].networkActivityIndicatorVisible > 0)
[UIApplication sharedApplication].networkActivityIndicatorVisible--;
else if ([UIApplication sharedApplication].networkActivityIndicatorVisible < 0)
[UIApplication sharedApplication].networkActivityIndicatorVisible = 0;
});
dispatch_release(apiQueue);
}

- (void)sendTypingIndicator{
dispatch_queue_t apiQueue = dispatch_queue_create("Discord::API::Event", NULL);
dispatch_async(apiQueue, ^{
NSURL* channelURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://discordapp.com/api/channels/%@/typing", self.snowflake]];

NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:channelURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:channelURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:2];

[urlRequest setHTTPMethod:@"POST"];

[urlRequest addValue:DCServerCommunicator.sharedInstance.token forHTTPHeaderField:@"Authorization"];
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connError) {
//[UIApplication sharedApplication].networkActivityIndicatorVisible--;
}];
NSError *error = nil;
NSHTTPURLResponse *responseCode = nil;

//[UIApplication sharedApplication].networkActivityIndicatorVisible++;
[DCTools checkData:[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&responseCode error:&error] withError:error];
/*if ([UIApplication sharedApplication].networkActivityIndicatorVisible > 0)
[UIApplication sharedApplication].networkActivityIndicatorVisible--;
else if ([UIApplication sharedApplication].networkActivityIndicatorVisible < 0)
[UIApplication sharedApplication].networkActivityIndicatorVisible = 0;*/
});
dispatch_release(apiQueue);
}

- (void)ackMessage:(NSString*)messageId{
self.lastReadMessageId = messageId;

//dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
dispatch_queue_t apiQueue = dispatch_queue_create("Discord::API::Event", NULL);
dispatch_async(apiQueue, ^{
NSURL* channelURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://discordapp.com/api/channels/%@/messages/%@/ack", self.snowflake, messageId]];

NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:channelURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:channelURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];

[urlRequest setHTTPMethod:@"POST"];

[urlRequest addValue:DCServerCommunicator.sharedInstance.token forHTTPHeaderField:@"Authorization"];
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *error = nil;
NSHTTPURLResponse *responseCode = nil;
int attempts = 0;
while (attempts == 0 || (attempts <= 10 && error.code == NSURLErrorTimedOut)) {
attempts++;
error = nil;
//[UIApplication sharedApplication].networkActivityIndicatorVisible++;
//[DCTools checkData:[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&responseCode error:&error] withError:error];

//[UIApplication sharedApplication].networkActivityIndicatorVisible--;
//[UIApplication sharedApplication].networkActivityIndicatorVisible++;
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connError) {
//[UIApplication sharedApplication].networkActivityIndicatorVisible--;
}];
}
//});

//[UIApplication sharedApplication].networkActivityIndicatorVisible++;
[DCTools checkData:[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&responseCode error:&error] withError:error];
/*if ([UIApplication sharedApplication].networkActivityIndicatorVisible > 0)
[UIApplication sharedApplication].networkActivityIndicatorVisible--;
else if ([UIApplication sharedApplication].networkActivityIndicatorVisible < 0)
[UIApplication sharedApplication].networkActivityIndicatorVisible = 0;*/
});
dispatch_release(apiQueue);
}



- (NSArray*)getMessages:(int)numberOfMessages beforeMessage:(DCMessage*)message{

NSMutableArray* messages = NSMutableArray.new;

NSMutableArray* messages = NSMutableArray.new;
//Generate URL from args
NSMutableString* getChannelAddress = [[NSString stringWithFormat: @"https://discordapp.com/api/channels/%@/messages?", self.snowflake] mutableCopy];

Expand All @@ -168,22 +171,21 @@ - (NSArray*)getMessages:(int)numberOfMessages beforeMessage:(DCMessage*)message{
if(message)
[getChannelAddress appendString:[NSString stringWithFormat:@"before=%@", message.snowflake]];

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:getChannelAddress] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:40];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:getChannelAddress] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];

[urlRequest addValue:DCServerCommunicator.sharedInstance.token forHTTPHeaderField:@"Authorization"];
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSError *error = nil;
NSHTTPURLResponse *responseCode = nil;
int attempts = 0;
while (attempts == 0 || (attempts <= 10 && error.code == NSURLErrorTimedOut)) {
attempts++;
error = nil;
NSLog(@"Showing network indicator (getMessages) %d", (int)[UIApplication sharedApplication].networkActivityIndicatorVisible);
[UIApplication sharedApplication].networkActivityIndicatorVisible++;
NSData *response = [DCTools checkData:[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&responseCode error:&error] withError:error];
NSLog(@"Showing network indicator (getMessages) %d", (int)[UIApplication sharedApplication].networkActivityIndicatorVisible);
[UIApplication sharedApplication].networkActivityIndicatorVisible--;
NSLog(@"Hiding network indicator (getMessages) %d", (int)[UIApplication sharedApplication].networkActivityIndicatorVisible);
if ([UIApplication sharedApplication].networkActivityIndicatorVisible > 0)
[UIApplication sharedApplication].networkActivityIndicatorVisible--;
else if ([UIApplication sharedApplication].networkActivityIndicatorVisible < 0)
[UIApplication sharedApplication].networkActivityIndicatorVisible = 0;
if(response){
NSArray* parsedResponse = [NSJSONSerialization JSONObjectWithData:response options:0 error:&error];

Expand Down Expand Up @@ -224,9 +226,8 @@ - (NSArray*)getMessages:(int)numberOfMessages beforeMessage:(DCMessage*)message{
return messages;

[DCTools alert:@"No messages!" withMessage:@"No further messages could be found"];
}
}
return nil;
}

Expand Down
1 change: 1 addition & 0 deletions Discord Classic/DCChatViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@property (weak, nonatomic) IBOutlet UITableView *chatTableView;
@property (weak, nonatomic) IBOutlet UITextView *inputField;
@property (weak, nonatomic) IBOutlet UILabel *inputFieldPlaceholder;
@property (weak, nonatomic) IBOutlet UIImageView *insetShadow;
@property bool viewingPresentTime;

@property DCMessage *selectedMessage;
Expand Down
Loading

0 comments on commit 76a21da

Please sign in to comment.