@@ -10,6 +10,22 @@ import Foundation
1010import UIKit
1111import Alamofire
1212
13+enum ResultR: Int {
14+ case internalServerError = 500
15+ case exceededContent = 470
16+ case longContent = 471
17+ case incorrectContent = 472
18+ case impossibleContent = 473
19+ case invalidTocken = 498
20+ case alreadyRegistered = 409
21+ case invalidCode = 403
22+ case badAccess = 400
23+ case tooMuchToAdd = 406
24+ case notFound = 404
25+ case success1 = 204
26+ case success = 200
27+}
28+
1329class Model{
1430
1531 static let invalidTocken = 498
@@ -17,7 +33,7 @@ class Model{
1733 static var hashTagTree: CompletionTree?
1834
1935 private static var isValidTocken: ((Int)->())? = { responce in
20−
36+ print(responce)
2137 if responce == invalidTocken {
2238
2339 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
@@ -33,6 +49,7 @@ class Model{
3349 static let decoder = JSONDecoder()
3450
3551 static let authMeURL = URL(string: "\(baseUrl)/authentication/me")!
52+ static let deactivateURL = URL(string: "\(baseUrl)/deactivateAll")!
3653 static let registerMeURL = URL(string: "\(baseUrl)/authentication/register")!
3754 static let authVerifyURL = URL(string: "\(baseUrl)/authentication/verify")!
3855 static let authenticationURL = URL(string:"\(baseUrl)/authentication/login")!
@@ -42,6 +59,7 @@ class Model{
4259 static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
4360 static let usersURL = URL(string:"\(baseUrl)/users")!
4461 static let usersAllURL = URL(string:"\(baseUrl)/users/all")!
62+ static let usersUpdateURL = URL(string:"\(baseUrl)/users/update")!
4563 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
4664 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
4765 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
@@ -251,15 +269,13 @@ class Model{
251269 }
252270
253271 if code == 204 {
254− let fields = responce.response?.allHeaderFields as? [String :String]
255272
273+ let fields = responce.response?.allHeaderFields as? [String :String]
256274 let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: responce.response!.url!)
257−
258275 HTTPCookieStorage.shared.setCookie(cookies[0])
259276
260277 completion()
261278 }
262−
263279 }
264280 }
265281
@@ -411,7 +427,7 @@ class Model{
411427 }
412428
413429
414− static func createAndPublish(body: [Attachments], tags: [String]){
430+ static func createAndPublish(body: [Attachments], tags: [String], completion: @escaping ((ResultR)->())){
415431 let jsonUpd = CreatedPost(body: body, tags: tags)
416432 var request = URLRequest(url: postsPublishURL)
417433
@@ -429,6 +445,14 @@ class Model{
429445 if let code = response.response?.statusCode, code == 498 {
430446 return
431447 }
448+
449+ if let code = response.response?.statusCode,
450+ let result = ResultR(rawValue: code)
451+ {
452+ completion(result)
453+ } else {
454+ completion(.internalServerError)
455+ }
432456 }
433457 }
434458
@@ -470,7 +494,7 @@ class Model{
470494
471495 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
472496 let json = "\(Set(ids))"
473− print(json)
497+
474498 var request = URLRequest(url: usersURL)
475499
476500 request.httpMethod = "POST"
@@ -534,7 +558,7 @@ class Model{
534558 }
535559
536560
537− static func createChannel(with channel: Channels) {
561+ static func createChannel(with channel: Channels, completion: @escaping ((ResultR)->())) {
538562
539563 var request = URLRequest(url: channelsCreateURL)
540564 request.httpMethod = "POST"
@@ -545,9 +569,13 @@ class Model{
545569 (response) in
546570
547571 isValidTocken?(response.response?.statusCode ?? 498)
548− if let code = response.response?.statusCode, code == 498 {
572+
573+ if let code = response.response?.statusCode, let result = ResultR(rawValue: code) {
574+ completion(result)
549575 return
550576 }
577+
578+ completion(ResultR.internalServerError)
551579 }
552580
553581 }
@@ -709,8 +737,6 @@ class Model{
709737 guard let json = response.data else { return }
710738
711739 guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
712−
713− print("\(anonymousChannel) \(newQueery)")
714740 completion((newQueery.users, newQueery.response))
715741 }
716742 }
@@ -927,7 +953,7 @@ class Model{
927953 }
928954 }
929955
930− static func getMessagesFor(typeOfChat: Model.Dialog, chat id: Int, exclusiveFrom: Int? = nil, limit l: Int = 10, direction: String = "backward", completion: @escaping (([LastMessage])->()))
956+ static func getMessagesFor(typeOfChat: Model.Dialog, chat id: Int, exclusiveFrom: Int? = nil, limit l: Int = 25, direction: String = "backward", completion: @escaping (([LastMessage])->()))
931957 {
932958 let req = GeneralRequest<Int>(direction: direction, limit: l, exclusiveFrom: exclusiveFrom, request: id)
933959 var request: URLRequest?
@@ -1030,7 +1056,7 @@ class Model{
10301056 }
10311057 }
10321058
1033− static func sendMessage(message: SendMessage, completion: @escaping (()->())){
1059+ static func sendMessage(message: SendMessage, completion: @escaping ((ResultR)->())){
10341060
10351061 var request = URLRequest(url: messagesSendURL)
10361062 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
@@ -1044,7 +1070,11 @@ class Model{
10441070 return
10451071 }
10461072
1047− completion()
1073+ if let result = ResultR(rawValue: response.response!.statusCode){
1074+ completion(result)
1075+ } else {
1076+ completion(.internalServerError)
1077+ }
10481078 }
10491079 }
10501080
@@ -1081,4 +1111,40 @@ class Model{
10811111 completion(faculties)
10821112 }
10831113 }
1114+
1115+ static func userUpdate(with newUser: NewRegistration, completion: @escaping ((Bool)->())) {
1116+ var request = URLRequest(url: usersUpdateURL)
1117+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
1118+ request.httpMethod = "POST"
1119+ request.httpBody = try? JSONEncoder().encode(newUser)
1120+
1121+ AF.request(request).response { (response) in
1122+ isValidTocken?(response.response?.statusCode ?? 498)
1123+
1124+ if let code = response.response?.statusCode, code == 498 {
1125+ return
1126+ }
1127+
1128+ if let code = response.response?.statusCode, code != 204{
1129+ completion (false)
1130+ } else {
1131+ completion(true)
1132+ }
1133+ }
1134+ }
1135+
1136+ static func deactivateAll(completion: @escaping (()->())){
1137+ var request = URLRequest(url: deactivateURL)
1138+ request.httpMethod = "DELETE"
1139+
1140+ AF.request(request).response { (responce) in
1141+ isValidTocken?(responce.response?.statusCode ?? 498)
1142+
1143+ if let code = responce.response?.statusCode, code == 498 {
1144+ return
1145+ }
1146+
1147+ completion()
1148+ }
1149+ }
10841150}