@@ -51,9 +51,9 @@ class Model{
51 static let messagesUserChatURL = URL(string: "\(baseUrl)/messages/userChat")!
51 static let messagesUserChatURL = URL(string: "\(baseUrl)/messages/userChat")!
54− struct QueryPosts: Codable {
54+ struct QueryPosts<T: Codable>: Codable {
55 var users: [Int: Users]
55 var users: [Int: Users]
56− var response: [Posts]
56+ var response: [T]
59 struct Posts: Codable {
59 struct Posts: Codable {
@@ -269,7 +269,7 @@ class Model{
270 guard let json = response.data else { return }
270 guard let json = response.data else { return }
272− guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { print("no")
272+ guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { print("no")
275 idUser = newQueery.users
275 idUser = newQueery.users
@@ -319,7 +319,7 @@ class Model{
320 guard let json = response.data else { return }
320 guard let json = response.data else { return }
322− guard let newPost = try? decoder.decode(QueryPosts.self, from: json) else { return }
322+ guard let newPost = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
324 completion(newPost.response)
324 completion(newPost.response)
@@ -377,7 +377,7 @@ class Model{
378 guard let json = response.data else { return }
378 guard let json = response.data else { return }
380− guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { return }
380+ guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
382 idUser = newQueery.users
382 idUser = newQueery.users
383 completion((newQueery.users, newQueery.response))
383 completion((newQueery.users, newQueery.response))
@@ -539,7 +539,7 @@ class Model{
540 guard let json = response.data else { return }
540 guard let json = response.data else { return }
542− guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { return }
542+ guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
544 // idUser = newQueery.users
544 // idUser = newQueery.users
545 completion((newQueery.users, newQueery.response))
545 completion((newQueery.users, newQueery.response))
@@ -550,11 +550,205 @@ class Model{
551 AF.request(URLRequest(url: hashTagTreeURL)).responseJSON {
551 AF.request(URLRequest(url: hashTagTreeURL)).responseJSON {
552 (response) in
552 (response) in
553−
553+ isValidTocken?(response.response?.statusCode ?? 498)
554 guard let json = response.data else { return }
554 guard let json = response.data else { return }
555 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 }
556 completion(tree)
556 completion(tree)
560+ static func getChatAll(limit: Int = 10, exclusiveFrom: Int? = nil, request: [Int] = [], completion: @escaping (([Dialog])->()))
562+ let req = GeneralRequest<[Int]>(limit: limit, exclusiveFrom: exclusiveFrom, request: request)
563+ var request = URLRequest(url: chatsGetAllURL)
564+ request.httpMethod = "POST"
565+ request.httpBody = try? JSONEncoder().encode(req)
566+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
568+ AF.request(request).responseJSON { response in
569+ isValidTocken?(response.response?.statusCode ?? 498)
571+ guard let json = response.data else { return }
572+ let dialogs = try! decoder.decode(QueryPosts<Dialog>.self, from: json)
574+ print(dialogs.response)
575+ completion(dialogs.response)
579+ enum Dialog: Codable
581+ case groupChat(GroupChat)
582+ case userChat(UserChat)
584+ private enum DialogKeys: CodingKey{
589+ init(from decoder: Decoder) throws {
590+ let container = try decoder.container(keyedBy: DialogKeys.self)
591+ if container.contains(.groupChat){
592+ self = .groupChat(try GroupChat(from: container.superDecoder(forKey: .groupChat)))
593+ } else if container.contains(.userChat){
594+ self = .userChat(try UserChat(from: container.superDecoder(forKey: .userChat)))
596+ throw DecodingError.keyNotFound(DialogKeys.groupChat, DecodingError.Context(codingPath: container.codingPath, debugDescription: "hz"))
600+ func encode(to encoder: Encoder) throws {
601+ var container = encoder.container(keyedBy: DialogKeys.self)
603+ case .groupChat(let chat):
604+ try chat.encode(to: container.superEncoder(forKey: .groupChat))
605+ case .userChat(let chat):
606+ try chat.encode(to: container.superEncoder(forKey: .userChat))
611+ struct GroupChat: Codable {
613+ var lastMessage: LastMessage
616+ struct UserChat: Codable {
618+ var lastMessage: LastMessage
621+ struct Group: Codable {
622+ var users: [Int: UserPermission]
627+ struct UserPermission: Codable {
631+ struct LastMessage: Codable {
632+ var body: Attachments
633+ var destination: MessageDestination
638+ private enum MessageCodingKeys: CodingKey{
647+ init(from decoder: Decoder) throws
649+ let container = try decoder.container(keyedBy: MessageCodingKeys.self)
650+ time = try container.decode(String.self, forKey: .time)
651+ body = try container.decode(Attachments.self, forKey: .body)
652+ author = try container.decode(Int.self, forKey: .author)
653+ id = try container.decode(Int.self, forKey: .id)
655+ if container.contains(.user){
656+ destination = MessageDestination.userChatDestination(try Int(from: container.superDecoder(forKey: .user)))
657+ } else if container.contains(.group){
658+ destination = MessageDestination.groupChatDestination(try Int(from: container.superDecoder(forKey: .group)))
660+ throw DecodingError.keyNotFound(MessageCodingKeys.group, DecodingError.Context(codingPath: container.codingPath, debugDescription: "hz gr"))
664+ func encode(to encoder: Encoder) throws {
665+ var container = encoder.container(keyedBy: MessageCodingKeys.self)
667+ try container.encode(time, forKey: .time)
668+ try container.encode(id, forKey: .id)
669+ try container.encode(author, forKey: .author)
670+ try container.encode(body, forKey: .body)
672+ switch destination {
673+ case .userChatDestination(let uId):
674+ try uId.encode(to: container.superEncoder(forKey: .user))
675+ case .groupChatDestination(let gId):
676+ try gId.encode(to: container.superEncoder(forKey: .group))
681+ enum MessageDestination: Codable
683+ func encode(to encoder: Encoder) throws
685+ var container = encoder.container(keyedBy: MessCodingKeys.self)
687+ case .userChatDestination(let uId):
688+ try uId.encode(to: container.superEncoder(forKey: .user))
689+ case .groupChatDestination(let gId):
690+ try gId.encode(to: container.superEncoder(forKey: .group))
694+ init(from decoder: Decoder) throws
696+ let container = try decoder.container(keyedBy: MessCodingKeys.self)
697+ if container.contains(.user){
698+ self = .userChatDestination(try Int(from: container.superDecoder(forKey: .user)))
699+ } else if container.contains(.group){
700+ self = .groupChatDestination(try Int(from: container.superDecoder(forKey: .group)))
702+ throw DecodingError.keyNotFound(MessCodingKeys.group, DecodingError.Context(codingPath: container.codingPath, debugDescription: "hz gr"))
706+ enum MessCodingKeys: CodingKey{
711+ case userChatDestination(Int)
712+ case groupChatDestination(Int)
716+ static func createGroupChat(from group: Group, completion: @escaping ((Int)->())) {
718+ var request = URLRequest(url: createGroupChatURL)
719+ request.httpMethod = "POST"
720+ request.httpBody = try? JSONEncoder().encode(req)
721+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
723+ AF.request(request).response { (response) in
725+ isValidTocken?(response.response?.statusCode ?? 498)
727+ guard let json = response.data else { return }
728+ let stringInt = String.init(data: json, encoding: String.Encoding.utf8)
729+ let intId = Int.init(stringInt!)
735+ static func getMessagesForGroupChat(chat id: Int, exclusiveFrom: Int? = nil, limit l: Int = 10, direction: String = "backward", completion: @escaping (([LastMessage])->()))
737+ let req = GeneralRequest<Int>(direction: direction, limit: l, exclusiveFrom: exclusiveFrom, request: id)
739+ var request = URLRequest(url: messagesGetGroupChatURL)
740+ request.httpMethod = "POST"
741+ request.httpBody = try? JSONEncoder().encode(req)
742+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
744+ AF.request(request).response { (response) in
746+ isValidTocken?(response.response?.statusCode ?? 498)
748+ guard let json = response.data else { return }
749+ guard let messages = try? decoder.decode([LastMessage].self, from: json) else { return }
751+ completion(messages)