@@ -10,56 +10,29 @@ 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
29class Model{
13class Model{
31 static let invalidTocken = 498
15 static let invalidTocken = 498
33− static var hashTagTree: CompletionTree?
17+ static var hashTagTree: CompletionTree?
35 private static var isValidTocken: ((Int)->())? = { responce in
19 private static var isValidTocken: ((Int)->())? = { responce in
36 print(responce)
20 print(responce)
37 if responce == invalidTocken {
21 if responce == invalidTocken {
39 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
22 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
40 (UIApplication.shared.delegate as! AppDelegate).relaunch()
23 (UIApplication.shared.delegate as! AppDelegate).relaunch()
43− print("Logout Is Successful")
48 private static let baseUrl = "https://valera-denis.herokuapp.com"
27 private static let baseUrl = "https://valera-denis.herokuapp.com"
49 static let decoder = JSONDecoder()
28 static let decoder = JSONDecoder()
51− static let authMeURL = URL(string: "\(baseUrl)/authentication/me")!
52− static let deactivateURL = URL(string: "\(baseUrl)/deactivateAll")!
53− static let registerMeURL = URL(string: "\(baseUrl)/authentication/register")!
54− static let authVerifyURL = URL(string: "\(baseUrl)/authentication/verify")!
55 static let authenticationURL = URL(string:"\(baseUrl)/authentication/login")!
30 static let authenticationURL = URL(string:"\(baseUrl)/authentication/login")!
56− static let logOutURL = URL(string:"\(baseUrl)/authentication/logout")!
57 static let postsLastURL = URL(string:"\(baseUrl)/posts/last")!
31 static let postsLastURL = URL(string:"\(baseUrl)/posts/last")!
58 static let postsForUserURL = URL(string:"\(baseUrl)/posts/forUser")!
32 static let postsForUserURL = URL(string:"\(baseUrl)/posts/forUser")!
59 static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
33 static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
60 static let usersURL = URL(string:"\(baseUrl)/users")!
34 static let usersURL = URL(string:"\(baseUrl)/users")!
61 static let usersAllURL = URL(string:"\(baseUrl)/users/all")!
35 static let usersAllURL = URL(string:"\(baseUrl)/users/all")!
62− static let usersUpdateURL = URL(string:"\(baseUrl)/users/update")!
63 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
36 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
64 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
37 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
65 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
38 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
@@ -67,7 +40,7 @@ class Model{
67 static let channelsDeleteURL = URL(string: "\(baseUrl)/channels/delete")!
40 static let channelsDeleteURL = URL(string: "\(baseUrl)/channels/delete")!
68 static let channelsGetAnonURL = URL(string: "\(baseUrl)/channels/getAnonymous")!
41 static let channelsGetAnonURL = URL(string: "\(baseUrl)/channels/getAnonymous")!
69 static let complexURL = URL(string: "\(baseUrl)/complex")!
42 static let complexURL = URL(string: "\(baseUrl)/complex")!
70− static let hashTagTreeURL = URL(string: "\(baseUrl)/tags/completions")!
43+ static let hashTagTreeURL = URL(string: "\(baseUrl)/tagCompletions")!
71 static let createGroupChatURL = URL(string: "\(baseUrl)/chats/createGroupChat")!
44 static let createGroupChatURL = URL(string: "\(baseUrl)/chats/createGroupChat")!
72 static let chatsGetAllURL = URL(string: "\(baseUrl)/chats/getAll")!
45 static let chatsGetAllURL = URL(string: "\(baseUrl)/chats/getAll")!
73 static let getGroupChatURL = URL(string: "\(baseUrl)/chats/getGroupChat")!
46 static let getGroupChatURL = URL(string: "\(baseUrl)/chats/getGroupChat")!
@@ -75,8 +48,7 @@ class Model{
75 static let updateGroupChatURL = URL(string: "\(baseUrl)/chats/updateGroupChat")!
48 static let updateGroupChatURL = URL(string: "\(baseUrl)/chats/updateGroupChat")!
76 static let messagesGetGroupChatURL = URL(string: "\(baseUrl)/messages/get/groupChat")! //r
49 static let messagesGetGroupChatURL = URL(string: "\(baseUrl)/messages/get/groupChat")! //r
77 static let messagesSendURL = URL(string: "\(baseUrl)/messages/send")!
50 static let messagesSendURL = URL(string: "\(baseUrl)/messages/send")!
78− static let messagesGetUserChatURL = URL(string: "\(baseUrl)/messages/get/userChat")!
51+ static let messagesUserChatURL = URL(string: "\(baseUrl)/messages/userChat")!
79− static let facultySearchURL = URL(string: "\(baseUrl)/faculty/search")!
82 struct QueryPosts<T: Codable>: Codable {
54 struct QueryPosts<T: Codable>: Codable {
@@ -148,8 +120,6 @@ class Model{
148 var lastName: String
120 var lastName: String
149 var firstName: String
121 var firstName: String
150 var id: Int
122 var id: Int
151− var faculty: Faculty
154 func fullName() -> String {
124 func fullName() -> String {
155 return "\(firstName) \(lastName)"
125 return "\(firstName) \(lastName)"
@@ -213,108 +183,24 @@ class Model{
216− struct AuthStatus: Codable {
186+ static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
217− var userStatus: String
219− init(){
188+ let json: [String:Any] = ["authenticationId" : id]
220− userStatus = "invalid"
189+ let jsonData = try? JSONSerialization.data(withJSONObject: json)
224− static func logout(completion: @escaping (()->())){
225− var request = URLRequest(url: logOutURL)
226− request.httpMethod = "POST"
228− AF.request(request).response { (responce) in
229− isValidTocken?(responce.response?.statusCode ?? 498)
231− if let code = responce.response?.statusCode, code == 498 {
240− "email": "vchernyshev@hse.ru",
241− "middleName": "Algebrovich",
242− "lastName": "Leonidov",
243− "faculty": "cs.hse.ru/dse",
247− struct NewRegistration: Codable{
249− var firstName: String?
250− var middleName: String?
251− var lastName: String?
252− var faculty: String?
255− static func register(object: NewRegistration, completion: @escaping (()->())) {
256− var request = URLRequest(url: registerMeURL)
257− request.httpBody = try? JSONEncoder().encode(object)
191+ var request = URLRequest(url: authenticationURL)
259 request.httpMethod = "POST"
192 request.httpMethod = "POST"
260− request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
262− AF.request(request).response { (responce) in
194+ // insert json data to the request
264− guard let code = responce.response?.statusCode else { return }
265− isValidTocken?(code)
267− if let code = responce.response?.statusCode, code == 498 {
273− let fields = responce.response?.allHeaderFields as? [String :String]
274− let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: responce.response!.url!)
275− HTTPCookieStorage.shared.setCookie(cookies[0])
282− // true only if everything is good
283− static func verify(with code: Int, completion: @escaping ((Bool)->())){
284− var request = URLRequest(url: authVerifyURL)
285− request.httpMethod = "POST"
286− request.httpBody = "\(code)".data(using: .utf8)
195+ request.httpBody = jsonData
287 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
196 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
289 AF.request(request).response { (responce) in
198 AF.request(request).response { (responce) in
291− guard let statusCode = responce.response?.statusCode else { completion(false); return }
200+ guard let realResponse = responce.response, realResponse.statusCode == 200 else
292− isValidTocken?(statusCode)
201+ {
293−
202+ print("Not a 200 response")
294− if let code = responce.response?.statusCode, code == 498 {
298− if statusCode == 204 {
299− // at this point cookies are set
302 completion(false)
203 completion(false)
308− static func authenticateMe(completion: @escaping ((Bool)->())){
309− var request = URLRequest(url: authMeURL)
310− request.httpMethod = "POST"
312− AF.request(request).response { (responce) in
314− guard let statusCode = responce.response?.statusCode else { completion(false); return }
315− isValidTocken?(statusCode)
317− if let code = responce.response?.statusCode, code == 498 {
@@ -324,45 +210,14 @@ class Model{
327− DataStorage.standard.setIsLoggedIn(value: true, with: personId)
213+ let fields = realResponse.allHeaderFields as? [String :String]
332− // register, ok or invalid
333− static func authenticate(with email: String, completion: @escaping ((AuthStatus)->())) {
335− let json: [String:Any] = ["authenticationEmail" : email]
336− let jsonData = try? JSONSerialization.data(withJSONObject: json)
338− var request = URLRequest(url: authenticationURL)
339− request.httpMethod = "POST"
341− // insert json data to the request
342− request.httpBody = jsonData
343− request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
345− AF.request(request).response { (response) in
347− guard let realResponse = response.response, realResponse.statusCode == 200 else
215+ let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: realResponse.url!)
349− print("Not a 200 response")
350− completion(AuthStatus())
354− guard let json = response.data else { return }
355− guard let res = try? decoder.decode(AuthStatus.self, from: json) else { return }
357− if realResponse.statusCode == 200 && res.userStatus == "registered"
359− // at this point cookies are set
360− let fields = realResponse.allHeaderFields as? [String :String]
361− let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: realResponse.url!)
362− HTTPCookieStorage.shared.setCookie(cookies[0])
217+ HTTPCookieStorage.shared.setCookie(cookies[0])
219+ DataStorage.standard.setIsLoggedIn(value: true, with: personId)
365− completion(res)
220+ completion(true)
@@ -412,10 +267,6 @@ class Model{
413 isValidTocken?(response.response?.statusCode ?? 498)
268 isValidTocken?(response.response?.statusCode ?? 498)
415− if let code = response.response?.statusCode, code == 498 {
419 guard let json = response.data else { return }
270 guard let json = response.data else { return }
421 guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { print("no")
272 guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { print("no")
@@ -427,7 +278,7 @@ class Model{
430− static func createAndPublish(body: [Attachments], tags: [String], completion: @escaping ((ResultR)->())){
281+ static func createAndPublish(body: [Attachments], tags: [String]){
431 let jsonUpd = CreatedPost(body: body, tags: tags)
282 let jsonUpd = CreatedPost(body: body, tags: tags)
432 var request = URLRequest(url: postsPublishURL)
283 var request = URLRequest(url: postsPublishURL)
@@ -441,18 +292,6 @@ class Model{
441 (response) in
292 (response) in
443 isValidTocken?(response.response?.statusCode ?? 498)
294 isValidTocken?(response.response?.statusCode ?? 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)
@@ -478,10 +317,6 @@ class Model{
479 isValidTocken?(response.response?.statusCode ?? 498)
318 isValidTocken?(response.response?.statusCode ?? 498)
481− if let code = response.response?.statusCode, code == 498 {
485 guard let json = response.data else { return }
320 guard let json = response.data else { return }
487 guard let newPost = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
322 guard let newPost = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
@@ -494,7 +329,7 @@ class Model{
495 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
330 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
496 let json = "\(Set(ids))"
331 let json = "\(Set(ids))"
498 var request = URLRequest(url: usersURL)
333 var request = URLRequest(url: usersURL)
500 request.httpMethod = "POST"
335 request.httpMethod = "POST"
@@ -508,16 +343,11 @@ class Model{
509 isValidTocken?(response.response?.statusCode ?? 498)
344 isValidTocken?(response.response?.statusCode ?? 498)
511− if let code = response.response?.statusCode, code == 498 {
515 guard let json = response.data else { return }
346 guard let json = response.data else { return }
517 guard let users = try? decoder.decode([Users].self, from: json) else { return }
348 guard let users = try? decoder.decode([Users].self, from: json) else { return }
519 var dict: [Int:Users] = [:]
350 var dict: [Int:Users] = [:]
521 users.forEach({ (user) in
351 users.forEach({ (user) in
522 dict[user.id] = user
352 dict[user.id] = user
@@ -544,9 +374,6 @@ class Model{
545 isValidTocken?(response.response?.statusCode ?? 498)
375 isValidTocken?(response.response?.statusCode ?? 498)
547− if let code = response.response?.statusCode, code == 498 {
551 guard let json = response.data else { return }
378 guard let json = response.data else { return }
@@ -558,7 +385,7 @@ class Model{
561− static func createChannel(with channel: Channels, completion: @escaping ((ResultR)->())) {
388+ static func createChannel(with channel: Channels) {
563 var request = URLRequest(url: channelsCreateURL)
390 var request = URLRequest(url: channelsCreateURL)
564 request.httpMethod = "POST"
391 request.httpMethod = "POST"
@@ -569,13 +396,6 @@ class Model{
569 (response) in
396 (response) in
571 isValidTocken?(response.response?.statusCode ?? 498)
398 isValidTocken?(response.response?.statusCode ?? 498)
573− if let code = response.response?.statusCode, let result = ResultR(rawValue: code) {
578− completion(ResultR.internalServerError)
@@ -588,9 +408,6 @@ class Model{
588 AF.request(request).responseJSON { (response) in
408 AF.request(request).responseJSON { (response) in
590 isValidTocken?(response.response?.statusCode ?? 498)
410 isValidTocken?(response.response?.statusCode ?? 498)
591− if let code = response.response?.statusCode, code == 498 {
595 guard let json = response.data else { return }
412 guard let json = response.data else { return }
@@ -609,9 +426,6 @@ class Model{
610 AF.request(request).response { (response) in
427 AF.request(request).response { (response) in
611 isValidTocken?(response.response?.statusCode ?? 498)
428 isValidTocken?(response.response?.statusCode ?? 498)
612− if let code = response.response?.statusCode, code == 498 {
616 completion()
430 completion()
@@ -627,9 +441,6 @@ class Model{
627 (response) in
441 (response) in
629 isValidTocken?(response.response?.statusCode ?? 498)
443 isValidTocken?(response.response?.statusCode ?? 498)
630− if let code = response.response?.statusCode, code == 498 {
@@ -652,9 +463,6 @@ class Model{
654 isValidTocken?(response.response?.statusCode ?? 498)
465 isValidTocken?(response.response?.statusCode ?? 498)
655− if let code = response.response?.statusCode, code == 498 {
@@ -717,26 +525,23 @@ class Model{
720− static func getAnonymousChannel(by anonymousChannel: Model.Channels, exclusiveFrom: Int? = nil, completion: @escaping (((users:[Int: Users], posts:[Posts]))->())){
528+ static func getAnonymousChannel(by anonymousChannel: Model.AnonymousChannel, completion: @escaping (((users:[Int: Users], posts:[Posts]))->())){
722− let req = GeneralRequest<Model.Channels>(limit: 10, exclusiveFrom: exclusiveFrom, request: anonymousChannel)
724 var request = URLRequest(url: channelsGetAnonURL)
530 var request = URLRequest(url: channelsGetAnonURL)
725 request.httpMethod = "POST"
531 request.httpMethod = "POST"
726− request.httpBody = try? JSONEncoder().encode(req)
532+ request.httpBody = try? JSONEncoder().encode(anonymousChannel)
728 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
534 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
729 AF.request(request).response {
535 AF.request(request).response {
730 (response) in
536 (response) in
732 isValidTocken?(response.response?.statusCode ?? 498)
538 isValidTocken?(response.response?.statusCode ?? 498)
733− if let code = response.response?.statusCode, code == 498 {
737 guard let json = response.data else { return }
540 guard let json = response.data else { return }
739 guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
542 guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
544+ // idUser = newQueery.users
740 completion((newQueery.users, newQueery.response))
545 completion((newQueery.users, newQueery.response))
@@ -746,16 +551,13 @@ class Model{
746 AF.request(URLRequest(url: hashTagTreeURL)).responseJSON {
551 AF.request(URLRequest(url: hashTagTreeURL)).responseJSON {
747 (response) in
552 (response) in
748 isValidTocken?(response.response?.statusCode ?? 498)
553 isValidTocken?(response.response?.statusCode ?? 498)
749− if let code = response.response?.statusCode, code == 498 {
752 guard let json = response.data else { return }
554 guard let json = response.data else { return }
753 guard let tree = try? decoder.decode(CompletionTree.self, from: json) else { return }
555 guard let tree = try? decoder.decode(CompletionTree.self, from: json) else { return }
754 completion(tree)
556 completion(tree)
758− static func getChatAll(limit: Int = 10, exclusiveFrom: Int? = nil, request: [Int] = [], completion: @escaping ((([Dialog],[Int:Model.Users]))->()))
560+ static func getChatAll(limit: Int = 10, exclusiveFrom: Int? = nil, request: [Int] = [], completion: @escaping (([Dialog])->()))
760 let req = GeneralRequest<[Int]>(limit: limit, exclusiveFrom: exclusiveFrom, request: request)
562 let req = GeneralRequest<[Int]>(limit: limit, exclusiveFrom: exclusiveFrom, request: request)
761 var request = URLRequest(url: chatsGetAllURL)
563 var request = URLRequest(url: chatsGetAllURL)
@@ -766,14 +568,11 @@ class Model{
766 AF.request(request).responseJSON { response in
568 AF.request(request).responseJSON { response in
767 isValidTocken?(response.response?.statusCode ?? 498)
569 isValidTocken?(response.response?.statusCode ?? 498)
769− if let code = response.response?.statusCode, code == 498 {
773 guard let json = response.data else { return }
571 guard let json = response.data else { return }
774 let dialogs = try! decoder.decode(QueryPosts<Dialog>.self, from: json)
572 let dialogs = try! decoder.decode(QueryPosts<Dialog>.self, from: json)
574+ print(dialogs.response)
776− completion((dialogs.response,dialogs.users))
575+ completion(dialogs.response)
@@ -811,35 +610,18 @@ class Model{
812 struct GroupChat: Codable {
611 struct GroupChat: Codable {
813 var group: Group
612 var group: Group
814− var lastMessage: LastMessage?
613+ var lastMessage: LastMessage
817− init(group: Group, lastMessage: LastMessage? = nil) {
819− self.lastMessage = lastMessage
823 struct UserChat: Codable {
616 struct UserChat: Codable {
824 var user: Int
617 var user: Int
825− var lastMessage: LastMessage?
618+ var lastMessage: LastMessage
827− init(user: Int, lastMessage: LastMessage? = nil) {
829− self.lastMessage = lastMessage
833 struct Group: Codable {
621 struct Group: Codable {
834 var users: [Int: UserPermission]
622 var users: [Int: UserPermission]
835 var name: String
623 var name: String
836 var id: Int
624 var id: Int
838− init(users: [Int: UserPermission] = [:], name: String = "", id: Int) {
845 struct UserPermission: Codable {
627 struct UserPermission: Codable {
@@ -853,7 +635,7 @@ class Model{
853 var author: Int
635 var author: Int
854 var id: Int
636 var id: Int
856− enum MessageCodingKeys: CodingKey{
638+ private enum MessageCodingKeys: CodingKey{
857 case user
639 case user
858 case group
640 case group
859 case body
641 case body
@@ -941,9 +723,6 @@ class Model{
941 AF.request(request).response { (response) in
723 AF.request(request).response { (response) in
943 isValidTocken?(response.response?.statusCode ?? 498)
725 isValidTocken?(response.response?.statusCode ?? 498)
944− if let code = response.response?.statusCode, code == 498 {
948 guard let json = response.data else { return }
727 guard let json = response.data else { return }
949 let stringInt = String.init(data: json, encoding: String.Encoding.utf8)
728 let stringInt = String.init(data: json, encoding: String.Encoding.utf8)
@@ -953,198 +732,23 @@ class Model{
956− static func getMessagesFor(typeOfChat: Model.Dialog, chat id: Int, exclusiveFrom: Int? = nil, limit l: Int = 25, direction: String = "backward", completion: @escaping (([LastMessage])->()))
735+ static func getMessagesForGroupChat(chat id: Int, exclusiveFrom: Int? = nil, limit l: Int = 10, direction: String = "backward", completion: @escaping (([LastMessage])->()))
958 let req = GeneralRequest<Int>(direction: direction, limit: l, exclusiveFrom: exclusiveFrom, request: id)
737 let req = GeneralRequest<Int>(direction: direction, limit: l, exclusiveFrom: exclusiveFrom, request: id)
959− var request: URLRequest?
963− request = URLRequest(url: messagesGetGroupChatURL)
965− request = URLRequest(url: messagesGetUserChatURL)
968− request!.httpMethod = "POST"
969− request!.httpBody = try? JSONEncoder().encode(req)
970− request!.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
972− AF.request(request!).response { (response) in
974− isValidTocken?(response.response?.statusCode ?? 498)
975− if let code = response.response?.statusCode, code == 498 {
979− guard let json = response.data else { return }
980− guard let messages = try? decoder.decode([LastMessage].self, from: json) else { return }
982− completion(messages)
986− static func leaveGroupChat(id: Int, completion: @escaping (()->()))
988− var request = URLRequest(url: leaveGroupChatURL)
739+ var request = URLRequest(url: messagesGetGroupChatURL)
989 request.httpMethod = "POST"
740 request.httpMethod = "POST"
990− request.httpBody = "\(id)".data(using: .utf8)
991− request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
993− AF.request(request).response { (response) in
994− isValidTocken?(response.response?.statusCode ?? 498)
996− if let code = response.response?.statusCode, code == 498 {
1004− static func updateGroupChat(with group: Model.Group)
1006− var request = URLRequest(url: updateGroupChatURL)
1007− request.httpMethod = "POST"
1008− request.httpBody = try? JSONEncoder().encode(group)
741+ request.httpBody = try? JSONEncoder().encode(req)
1009− request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
1011− AF.request(request).response { (response) in
1012− isValidTocken?(response.response?.statusCode ?? 498)
1014− if let code = response.response?.statusCode, code == 498 {
1022− struct SendMessage: Codable {
1023− var body: Attachments
1024− var destination: MessageDestination
1026− init(from decoder: Decoder) throws
1028− let container = try decoder.container(keyedBy: LastMessage.MessageCodingKeys.self)
1029− body = try container.decode(Attachments.self, forKey: .body)
1031− if container.contains(.user){
1032− destination = MessageDestination.userChatDestination(try Int(from: container.superDecoder(forKey: .user)))
1033− } else if container.contains(.group){
1034− destination = MessageDestination.groupChatDestination(try Int(from: container.superDecoder(forKey: .group)))
1036− throw DecodingError.keyNotFound(LastMessage.MessageCodingKeys.group, DecodingError.Context(codingPath: container.codingPath, debugDescription: "hz gr1"))
1040− func encode(to encoder: Encoder) throws {
1041− var container = encoder.container(keyedBy: LastMessage.MessageCodingKeys.self)
1042− try container.encode(body, forKey: .body)
1044− switch destination {
1045− case .userChatDestination(let uId):
1046− try uId.encode(to: container.superEncoder(forKey: .user))
1047− case .groupChatDestination(let gId):
1048− try gId.encode(to: container.superEncoder(forKey: .group))
1052− init(body: Attachments, destination: MessageDestination)
1055− self.destination = destination
1059− static func sendMessage(message: SendMessage, completion: @escaping ((ResultR)->())){
1061− var request = URLRequest(url: messagesSendURL)
1062 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
742 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
1063− request.httpMethod = "POST"
1064− request.httpBody = try? JSONEncoder().encode(message)
1066 AF.request(request).response { (response) in
744 AF.request(request).response { (response) in
1067− isValidTocken?(response.response?.statusCode ?? 498)
1069− if let code = response.response?.statusCode, code == 498 {
1073− if let result = ResultR(rawValue: response.response!.statusCode){
1076− completion(.internalServerError)
1081− struct Faculty: Codable
1086− var campusName: String
1087− var campusCode: String
1090− var address: String
1094− static func searchFaculty(string: String, completion: @escaping (([Model.Faculty])->()))
1096− var request = URLRequest(url: facultySearchURL)
1097− request.httpMethod = "POST"
1098− request.httpBody = string.data(using: .utf8)
1099− request.setValue("text/plain;charset=utf-8", forHTTPHeaderField: "Content-Type")
1101− AF.request(request).response { (response) in
1102 isValidTocken?(response.response?.statusCode ?? 498)
746 isValidTocken?(response.response?.statusCode ?? 498)
1104− if let code = response.response?.statusCode, code == 498 {
1108 guard let json = response.data else { return }
748 guard let json = response.data else { return }
1109− guard let faculties = try? decoder.decode([Faculty].self, from: json) else { return }
749+ guard let messages = try? decoder.decode([LastMessage].self, from: json) else { return }
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 {
1147− completion()
751+ completion(messages)