This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
/
SKClient+Stories.m
222 lines (193 loc) · 9.12 KB
/
SKClient+Stories.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// SKClient+Stories.m
// SnapchatKit
//
// Created by Tanner Bennett on 6/13/15.
// Copyright (c) 2015 Tanner Bennett. All rights reserved.
//
#import "SKClient+Stories.h"
#import "SKStoryCollection.h"
#import "SKStory.h"
#import "SKUser.h"
#import "SKUserStory.h"
#import "SKStoryUpdater.h"
#import "SKStoryOptions.h"
#import "SKSharedStoryDescription.h"
#import "SKRequest.h"
#import "NSString+SnapchatKit.h"
#import "NSArray+SnapchatKit.h"
@implementation SKClient (Stories)
- (void)postStory:(SKBlob *)blob for:(NSTimeInterval)duration completion:(ErrorBlock)completion {
SKStoryOptions *options = [SKStoryOptions storyWithText:nil timer:duration];
[self postStory:blob options:options completion:completion];
}
- (void)postStory:(SKBlob *)blob options:(SKStoryOptions *)options completion:(ErrorBlock)completion {
NSParameterAssert(blob); NSParameterAssert(options);
[self uploadStory:blob completion:^(NSString *mediaID, NSError *error) {
if (!error) {
NSMutableDictionary *query = @{@"camera_front_facing": @(options.cameraFrontFacing),
@"client_id": mediaID,
@"filter_id": @"",
@"media_id": mediaID,
@"orientation": @"0",
@"story_timestamp": [NSString timestamp],
@"time": @((NSUInteger)options.timer),
@"type": blob.isImage ? @(SKMediaKindImage) : @(SKMediaKindVideo),
@"username": self.username,
// @"my_story": @"true",
@"zipped": blob.zipData ? @1 : @0}.mutableCopy;
// Optional parts
if (options.text) {
query[@"caption_text_display"] = options.text;
}
if (blob.videoThumbnail) {
query[@"thumbnail_data"] = blob.videoThumbnail;
}
[self postTo:SKEPStories.post query:query callback:^(NSDictionary *json, NSError *sendError) {
SKRunBlockP(completion, sendError);
}];
} else {
SKRunBlockP(completion, error);
}
}];
}
- (void)uploadStory:(SKBlob *)blob completion:(ResponseBlock)completion {
NSString *uuid = SKMediaIdentifier(self.username);
NSDictionary *query = @{@"media_id": uuid,
@"type": blob.isImage ? @(SKMediaKindImage) : @(SKMediaKindVideo),
@"data": blob.zipData ? blob.zipData : blob.data,
@"zipped": blob.zipData ? @1 : @0,
@"features_map": @"{}",
@"username": self.username};
[self postTo:SKEPStories.upload query:query callback:^(id object, NSError *error) {
SKRunBlockP(completion, error ? nil : uuid, error);
}];
}
- (void)loadStoryBlob:(SKStory *)story completion:(ResponseBlock)completion {
[self loadStoryOrThumb:story thumbnail:NO completion:completion];
}
- (void)loadStoryThumbnailBlob:(SKStory *)story completion:(ResponseBlock)completion {
[self loadStoryOrThumb:story thumbnail:YES completion:completion];
}
- (void)loadStoryOrThumb:(SKStory *)story thumbnail:(BOOL)thumbnail completion:(ResponseBlock)completion {
NSParameterAssert(story); NSParameterAssert(completion);
if (story.needsAuth) {
NSString *endpoint = thumbnail ? SKEPStories.authThumb : SKEPStories.authBlob;
NSDictionary *query = @{@"story_id": story.mediaIdentifier, @"username": self.username};
[self postTo:endpoint query:query response:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSInteger code = [(NSHTTPURLResponse *)response statusCode];
if (code == 200) {
[SKBlob blobWithStoryData:data forStory:story isThumb:thumbnail completion:^(SKBlob *storyBlob, NSError *blobError) {
if (!blobError) {
completion(storyBlob, nil);
} else {
completion(nil, blobError);
}
}];
} else {
[self handleError:error data:data response:response completion:completion];
}
} else {
[self handleError:error data:data response:response completion:completion];
}
}];
} else {
NSString *url;
if (thumbnail) {
url = [story.thumbURL.absoluteString stringByReplacingOccurrencesOfString:SKConsts.baseURL withString:@""];
} else {
url = [story.mediaURL.absoluteString stringByReplacingOccurrencesOfString:SKConsts.baseURL withString:@""];
}
[self get:url callback:^(NSData *data, NSError *error) {
if (!error) {
[SKBlob blobWithStoryData:data forStory:story isThumb:thumbnail completion:^(SKBlob *thumbBlob, NSError *blobError) {
if (!blobError) {
completion(thumbBlob, nil);
} else {
completion(nil, blobError);
}
}];
} else {
completion(nil, error);
}
}];
}
}
- (void)loadStories:(NSArray *)stories completion:(CollectionResponseBlock)completion {
NSMutableArray *loaded = [NSMutableArray array];
NSMutableArray *failed = [NSMutableArray array];
NSMutableArray *errors = [NSMutableArray array];
for (SKStory *story in stories)
[story load:^(NSError *error) {
if (!error) {
[loaded addObject:story];
} else {
[errors addObject:error];
[failed addObject:story];
}
if (loaded.count + failed.count == stories.count)
completion(loaded, failed, errors);
}];
}
- (void)deleteStory:(SKUserStory *)story completion:(ErrorBlock)completion {
NSParameterAssert(story);
NSDictionary *query = @{@"story_id": story.identifier,
@"username": self.username};
[self postTo:SKEPStories.remove query:query callback:^(id object, NSError *error) {
if (!error)
[self.currentSession.userStories removeObject:story];
completion(error);
}];
}
- (void)markStoriesViewed:(NSArray *)stories completion:(ErrorBlock)completion {
NSParameterAssert(stories);
NSMutableArray *friendStories = [NSMutableArray array];
for (SKStoryUpdater *update in stories)
[friendStories addObject:@{@"id": update.storyID,
@"screenshot_count": @(update.screenshotCount),
@"timestamp": update.timestamp}];
NSDictionary *query = @{@"username": self.username,
@"friend_stories": friendStories.JSONString};
[self postTo:SKEPUpdate.stories query:query callback:^(NSDictionary *json, NSError *error) {
completion(error);
}];
}
- (void)markStoryViewed:(SKStory *)story screenshotCount:(NSUInteger)sscount completion:(ErrorBlock)completion {
NSParameterAssert(story);
[self markStoriesViewed:@[[SKStoryUpdater viewedStory:story at:[NSDate date] screenshots:sscount]] completion:completion];
}
- (void)hideSharedStory:(SKStoryCollection *)story completion:(ErrorBlock)completion {
NSParameterAssert(story);
NSDictionary *query = @{@"friend": story.username,
@"hide": @"true",
@"username": self.username};
[self postTo:SKEPFriends.hide query:query callback:^(NSDictionary *json, NSError *error) {
completion(error);
}];
}
- (void)provideSharedDescription:(SKStory *)sharedStory completion:(ErrorBlock)completion {
NSParameterAssert(sharedStory);
if (!sharedStory.shared) return;
NSDictionary *query = @{@"shared_id": sharedStory.identifier,
@"username": self.username};
[self postTo:kepSharedDescription query:query callback:^(id object, NSError *error) {
completion(error);
}];
}
- (void)getSharedDescriptionForStory:(SKUser *)sharedStory completion:(ResponseBlock)completion {
NSParameterAssert(sharedStory.sharedStoryIdentifier); NSParameterAssert(completion);
[self get:[NSString stringWithFormat:@"shared/description?ln=en&shared_id=%@", sharedStory.sharedStoryIdentifier] callback:^(NSData *data, NSError *error) {
if (!error) {
NSError *jsonError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!jsonError)
completion([[SKSharedStoryDescription alloc] initWithDictionary:json], nil);
else
completion(nil, jsonError);
} else {
completion(nil, error);
}
}];
}
@end