-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Hachidori+userinfo.m
151 lines (143 loc) · 7.13 KB
/
Hachidori+userinfo.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
//
// Hachidori+userinfo.m
// Hachidori
//
// Created by 天々座理世 on 2018/05/28.
//
#import "Hachidori+userinfo.h"
#import "AniListConstants.h"
#import <AFNetworking/AFNetworking.h>
@implementation Hachidori (userinfo)
- (void)savekitsuinfo {
// Retrieves missing user information and populates it before showing the UI.
AFOAuthCredential *cred = [Hachidori getFirstAccount:0];
if (cred && cred.expired) {
[self refreshtokenWithService:0 successHandler:^(bool success) {
if (success) {
[self savekitsuinfo];
}
}];
return;
}
if (cred) {
[self.syncmanager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@", cred.accessToken] forHTTPHeaderField:@"Authorization"];
}
NSError *error;
id responseObject = [self.syncmanager syncGET:@"https://kitsu.io/api/edge/users?filter[self]=true&fields[users]=name,slug,avatar,ratingSystem" parameters:@{} headers:@{} task:NULL error:&error];
if (!error) {
if (((NSArray *)responseObject[@"data"]).count > 0) {
NSDictionary *d = [NSArray arrayWithArray:responseObject[@"data"]][0];
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
[defaults setValue:d[@"id"] forKey:@"UserID"];
if (d[@"attributes"][@"name"] != [NSNull null]) {
[defaults setValue:d[@"attributes"][@"name"] forKey:@"loggedinusername"];
}
else if (d[@"attributes"][@"slug"] != [NSNull null]) {
[defaults setValue:d[@"attributes"][@"slug"] forKey:@"loggedinusername"];
}
else {
[defaults setValue:@"Unknown User" forKey:@"loggedinusername"];
}
}
else {
// Remove Account, invalid token
[AFOAuthCredential deleteCredentialWithIdentifier:@"Hachidori"];
}
}
else {
if ([[error.userInfo valueForKey:@"NSLocalizedDescription"] isEqualToString:@"Request failed: unauthorized (401)"]) {
// Remove Account
[AFOAuthCredential deleteCredentialWithIdentifier:@"Hachidori"];
}
}
}
- (void)saveanilistuserinfo {
// Retrieves missing user information and populates it before showing the UI.
AFOAuthCredential *cred = [Hachidori getFirstAccount:1];
if (cred && cred.expired) {
return;
}
if (cred) {
[self.syncmanager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@", cred.accessToken] forHTTPHeaderField:@"Authorization"];
}
NSError *error;
id responseObject = [self.syncmanager syncPOST:@"https://graphql.anilist.co" parameters:@{@"query" : kAnilistCurrentUsernametoUserId, @"variables" : @{}} headers:@{} task:NULL error:&error];
if (!error) {
if (responseObject[@"data"][@"Viewer"] != [NSNull null]) {
NSDictionary *d = responseObject[@"data"][@"Viewer"];
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
[defaults setValue:d[@"id"] forKey:@"UserID-anilist"];
[defaults setValue:d[@"name"] forKey:@"loggedinusername-anilist"];
}
else {
// Remove Account, invalid token
[AFOAuthCredential deleteCredentialWithIdentifier:@"Hachidori - AniList"];
}
}
else {
if ([[error.userInfo valueForKey:@"NSLocalizedDescription"] isEqualToString:@"Request failed: unauthorized (401)"]) {
// Remove Account
[AFOAuthCredential deleteCredentialWithIdentifier:@"Hachidori - AniList"];
}
}
}
- (void)savemaluserinfo {
// Retrieves missing user information and populates it before showing the UI.
AFOAuthCredential *cred = [Hachidori getFirstAccount:2];
if (cred && cred.expired) {
return;
}
if (cred) {
[self.syncmanager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@", cred.accessToken] forHTTPHeaderField:@"Authorization"];
}
NSError *error;
id responseObject = [self.syncmanager syncGET:@"https://api.myanimelist.net/v2/users/@me?fields=avatar" parameters:nil headers:@{} task:NULL error:&error];
if (!error) {
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
[defaults setValue:responseObject[@"id"] forKey:@"UserID-mal"];
[defaults setValue:responseObject[@"name"] forKey:@"loggedinusername-mal"];
}
else {
if ([[error.userInfo valueForKey:@"NSLocalizedDescription"] isEqualToString:@"Request failed: unauthorized (401)"]) {
// Remove Account, invalid token
[AFOAuthCredential deleteCredentialWithIdentifier:@"Hachidori - MyAnimeList"];
}
}
}
- (void)checkaccountinformation {
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
if ([Hachidori getFirstAccount:0]) {
bool refreshKitsu = (![defaults valueForKey:@"kitsu-userinformationrefresh"] || ((NSDate *)[defaults objectForKey:@"kitsu-userinformationrefresh"]).timeIntervalSinceNow < 0);
if ((![defaults valueForKey:@"loggedinusername"] && ![defaults valueForKey:@"UserID"]) || ((NSString *)[defaults valueForKey:@"loggedinusername"]).length == 0 || refreshKitsu) {
[self savekitsuinfo];
[NSUserDefaults.standardUserDefaults setObject:[NSDate dateWithTimeIntervalSinceNow:259200] forKey:@"kitsu-userinformationrefresh"];
}
}
if ([Hachidori getFirstAccount:1]) {
bool refreshAniList = (![defaults valueForKey:@"anilist-userinformationrefresh"] || ((NSDate *)[defaults objectForKey:@"anilist-userinformationrefresh"]).timeIntervalSinceNow < 0);
if ((![defaults valueForKey:@"loggedinusername-anilist"] || ![defaults valueForKey:@"UserID-anilist"]) || ((NSString *)[defaults valueForKey:@"loggedinusername-anilist"]).length == 0 || refreshAniList) {
[self saveanilistuserinfo];
[NSUserDefaults.standardUserDefaults setObject:[NSDate dateWithTimeIntervalSinceNow:259200] forKey:@"anilist-userinformationrefresh"];
}
}
if ([Hachidori getFirstAccount:2]) {
bool refreshAniList = (![defaults valueForKey:@"mal-userinformationrefresh"] || ((NSDate *)[defaults objectForKey:@"mal-userinformationrefresh"]).timeIntervalSinceNow < 0);
if ((![defaults valueForKey:@"loggedinusername-mal"] || ![defaults valueForKey:@"UserID-anilist"]) || ((NSString *)[defaults valueForKey:@"loggedinusername-mal"]).length == 0 || refreshAniList) {
[self savemaluserinfo];
[NSUserDefaults.standardUserDefaults setObject:[NSDate dateWithTimeIntervalSinceNow:259200] forKey:@"mal-userinformationrefresh"];
}
}
}
- (bool)hasUserInfoCurrentService {
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
switch ([Hachidori currentService]) {
case serviceKitsu:
return ([defaults valueForKey:@"loggedinusername"] && [defaults valueForKey:@"UserID"]);
case serviceAniList:
return ([defaults valueForKey:@"loggedinusername-anilist"] && [defaults valueForKey:@"UserID-anilist"]);
case serviceMAL:
return ([defaults valueForKey:@"loggedinusername-mal"] && [defaults valueForKey:@"UserID-mal"]);
}
return NO;
}
@end