@@ -10,6 +10,22 @@ import Foundation
10import UIKit
10import UIKit
11import Alamofire
11import Alamofire
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
23+ case tooMuchToAdd = 406
13class Model{
29class Model{
15 static let invalidTocken = 498
31 static let invalidTocken = 498
@@ -17,7 +33,7 @@ class Model{
17 static var hashTagTree: CompletionTree?
33 static var hashTagTree: CompletionTree?
19 private static var isValidTocken: ((Int)->())? = { responce in
35 private static var isValidTocken: ((Int)->())? = { responce in
21 if responce == invalidTocken {
37 if responce == invalidTocken {
23 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
39 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
@@ -33,6 +49,7 @@ class Model{
33 static let decoder = JSONDecoder()
49 static let decoder = JSONDecoder()
35 static let authMeURL = URL(string: "\(baseUrl)/authentication/me")!
51 static let authMeURL = URL(string: "\(baseUrl)/authentication/me")!
52+ static let deactivateURL = URL(string: "\(baseUrl)/deactivateAll")!
36 static let registerMeURL = URL(string: "\(baseUrl)/authentication/register")!
53 static let registerMeURL = URL(string: "\(baseUrl)/authentication/register")!
37 static let authVerifyURL = URL(string: "\(baseUrl)/authentication/verify")!
54 static let authVerifyURL = URL(string: "\(baseUrl)/authentication/verify")!
38 static let authenticationURL = URL(string:"\(baseUrl)/authentication/login")!
55 static let authenticationURL = URL(string:"\(baseUrl)/authentication/login")!
@@ -42,6 +59,7 @@ class Model{
42 static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
59 static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
43 static let usersURL = URL(string:"\(baseUrl)/users")!
60 static let usersURL = URL(string:"\(baseUrl)/users")!
44 static let usersAllURL = URL(string:"\(baseUrl)/users/all")!
61 static let usersAllURL = URL(string:"\(baseUrl)/users/all")!
62+ static let usersUpdateURL = URL(string:"\(baseUrl)/users/update")!
45 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
63 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
46 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
64 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
47 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
65 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
@@ -251,15 +269,13 @@ class Model{
253 if code == 204 {
271 if code == 204 {
254− let fields = responce.response?.allHeaderFields as? [String :String]
273+ let fields = responce.response?.allHeaderFields as? [String :String]
256 let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: responce.response!.url!)
274 let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: responce.response!.url!)
258 HTTPCookieStorage.shared.setCookie(cookies[0])
275 HTTPCookieStorage.shared.setCookie(cookies[0])
260 completion()
277 completion()
@@ -411,7 +427,7 @@ class Model{
414− static func createAndPublish(body: [Attachments], tags: [String]){
430+ static func createAndPublish(body: [Attachments], tags: [String], completion: @escaping ((ResultR)->())){
415 let jsonUpd = CreatedPost(body: body, tags: tags)
431 let jsonUpd = CreatedPost(body: body, tags: tags)
416 var request = URLRequest(url: postsPublishURL)
432 var request = URLRequest(url: postsPublishURL)
@@ -429,6 +445,14 @@ class Model{
429 if let code = response.response?.statusCode, code == 498 {
445 if let code = response.response?.statusCode, code == 498 {
449+ if let code = response.response?.statusCode,
450+ let result = ResultR(rawValue: code)
454+ completion(.internalServerError)
@@ -470,7 +494,7 @@ class Model{
471 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
495 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
472 let json = "\(Set(ids))"
496 let json = "\(Set(ids))"
474 var request = URLRequest(url: usersURL)
498 var request = URLRequest(url: usersURL)
476 request.httpMethod = "POST"
500 request.httpMethod = "POST"
@@ -534,7 +558,7 @@ class Model{
537− static func createChannel(with channel: Channels) {
561+ static func createChannel(with channel: Channels, completion: @escaping ((ResultR)->())) {
539 var request = URLRequest(url: channelsCreateURL)
563 var request = URLRequest(url: channelsCreateURL)
540 request.httpMethod = "POST"
564 request.httpMethod = "POST"
@@ -545,9 +569,13 @@ class Model{
545 (response) in
569 (response) in
547 isValidTocken?(response.response?.statusCode ?? 498)
571 isValidTocken?(response.response?.statusCode ?? 498)
548− if let code = response.response?.statusCode, code == 498 {
573+ if let code = response.response?.statusCode, let result = ResultR(rawValue: code) {
578+ completion(ResultR.internalServerError)
@@ -709,8 +737,6 @@ class Model{
709 guard let json = response.data else { return }
737 guard let json = response.data else { return }
711 guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
739 guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
713− print("\(anonymousChannel) \(newQueery)")
714 completion((newQueery.users, newQueery.response))
740 completion((newQueery.users, newQueery.response))
@@ -927,7 +953,7 @@ class Model{
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])->()))
932 let req = GeneralRequest<Int>(direction: direction, limit: l, exclusiveFrom: exclusiveFrom, request: id)
958 let req = GeneralRequest<Int>(direction: direction, limit: l, exclusiveFrom: exclusiveFrom, request: id)
933 var request: URLRequest?
959 var request: URLRequest?
@@ -1030,7 +1056,7 @@ class Model{
1033− static func sendMessage(message: SendMessage, completion: @escaping (()->())){
1059+ static func sendMessage(message: SendMessage, completion: @escaping ((ResultR)->())){
1035 var request = URLRequest(url: messagesSendURL)
1061 var request = URLRequest(url: messagesSendURL)
1036 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
1062 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
@@ -1044,7 +1070,11 @@ class Model{
1073+ if let result = ResultR(rawValue: response.response!.statusCode){
1047− completion()
1076+ completion(.internalServerError)
@@ -1081,4 +1111,40 @@ class Model{
1081 completion(faculties)
1111 completion(faculties)
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)
1121+ AF.request(request).response { (response) in
1122+ isValidTocken?(response.response?.statusCode ?? 498)
1124+ if let code = response.response?.statusCode, code == 498 {
1128+ if let code = response.response?.statusCode, code != 204{
1136+ static func deactivateAll(completion: @escaping (()->())){
1137+ var request = URLRequest(url: deactivateURL)
1138+ request.httpMethod = "DELETE"
1140+ AF.request(request).response { (responce) in
1141+ isValidTocken?(responce.response?.statusCode ?? 498)
1143+ if let code = responce.response?.statusCode, code == 498 {