@@ -51,9 +51,9 @@ class Model{
5151 static let messagesUserChatURL = URL(string: "\(baseUrl)/messages/userChat")!
5252
5353
54− struct QueryPosts: Codable {
54+ struct QueryPosts<T: Codable>: Codable {
5555 var users: [Int: Users]
56− var response: [Posts]
56+ var response: [T]
5757 }
5858
5959 struct Posts: Codable {
@@ -269,7 +269,7 @@ class Model{
269269
270270 guard let json = response.data else { return }
271271
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")
273273 return }
274274
275275 idUser = newQueery.users
@@ -319,7 +319,7 @@ class Model{
319319
320320 guard let json = response.data else { return }
321321
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 }
323323
324324 completion(newPost.response)
325325 }
@@ -377,7 +377,7 @@ class Model{
377377
378378 guard let json = response.data else { return }
379379
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 }
381381
382382 idUser = newQueery.users
383383 completion((newQueery.users, newQueery.response))
@@ -539,7 +539,7 @@ class Model{
539539
540540 guard let json = response.data else { return }
541541
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 }
543543
544544 // idUser = newQueery.users
545545 completion((newQueery.users, newQueery.response))
@@ -550,11 +550,205 @@ class Model{
550550
551551 AF.request(URLRequest(url: hashTagTreeURL)).responseJSON {
552552 (response) in
553−
553+ isValidTocken?(response.response?.statusCode ?? 498)
554554 guard let json = response.data else { return }
555555 guard let tree = try? decoder.decode(CompletionTree.self, from: json) else { return }
556556 completion(tree)
557557 }
558558 }
559559
560+ static func getChatAll(limit: Int = 10, exclusiveFrom: Int? = nil, request: [Int] = [], completion: @escaping (([Dialog])->()))
561+ {
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")
567+
568+ AF.request(request).responseJSON { response in
569+ isValidTocken?(response.response?.statusCode ?? 498)
570+
571+ guard let json = response.data else { return }
572+ let dialogs = try! decoder.decode(QueryPosts<Dialog>.self, from: json)
573+
574+ print(dialogs.response)
575+ completion(dialogs.response)
576+ }
577+ }
578+
579+ enum Dialog: Codable
580+ {
581+ case groupChat(GroupChat)
582+ case userChat(UserChat)
583+
584+ private enum DialogKeys: CodingKey{
585+ case groupChat
586+ case userChat
587+ }
588+
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)))
595+ } else {
596+ throw DecodingError.keyNotFound(DialogKeys.groupChat, DecodingError.Context(codingPath: container.codingPath, debugDescription: "hz"))
597+ }
598+ }
599+
600+ func encode(to encoder: Encoder) throws {
601+ var container = encoder.container(keyedBy: DialogKeys.self)
602+ switch 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))
607+ }
608+ }
609+ }
610+
611+ struct GroupChat: Codable {
612+ var group: Group
613+ var lastMessage: LastMessage
614+ }
615+
616+ struct UserChat: Codable {
617+ var user: Int
618+ var lastMessage: LastMessage
619+ }
620+
621+ struct Group: Codable {
622+ var users: [Int: UserPermission]
623+ var name: String
624+ var id: Int
625+ }
626+
627+ struct UserPermission: Codable {
628+ var isAdmin: Bool
629+ }
630+
631+ struct LastMessage: Codable {
632+ var body: Attachments
633+ var destination: MessageDestination
634+ var time: String
635+ var author: Int
636+ var id: Int
637+
638+ private enum MessageCodingKeys: CodingKey{
639+ case user
640+ case group
641+ case body
642+ case time
643+ case author
644+ case id
645+ }
646+
647+ init(from decoder: Decoder) throws
648+ {
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)
654+
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)))
659+ } else {
660+ throw DecodingError.keyNotFound(MessageCodingKeys.group, DecodingError.Context(codingPath: container.codingPath, debugDescription: "hz gr"))
661+ }
662+ }
663+
664+ func encode(to encoder: Encoder) throws {
665+ var container = encoder.container(keyedBy: MessageCodingKeys.self)
666+
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)
671+
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))
677+ }
678+ }
679+ }
680+
681+ enum MessageDestination: Codable
682+ {
683+ func encode(to encoder: Encoder) throws
684+ {
685+ var container = encoder.container(keyedBy: MessCodingKeys.self)
686+ switch 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))
691+ }
692+ }
693+
694+ init(from decoder: Decoder) throws
695+ {
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)))
701+ } else {
702+ throw DecodingError.keyNotFound(MessCodingKeys.group, DecodingError.Context(codingPath: container.codingPath, debugDescription: "hz gr"))
703+ }
704+ }
705+
706+ enum MessCodingKeys: CodingKey{
707+ case user
708+ case group
709+ }
710+
711+ case userChatDestination(Int)
712+ case groupChatDestination(Int)
713+
714+ }
715+
716+ static func createGroupChat(from group: Group, completion: @escaping ((Int)->())) {
717+ let req = group
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")
722+
723+ AF.request(request).response { (response) in
724+
725+ isValidTocken?(response.response?.statusCode ?? 498)
726+
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!)
730+
731+ completion(intId!)
732+ }
733+ }
734+
735+ static func getMessagesForGroupChat(chat id: Int, exclusiveFrom: Int? = nil, limit l: Int = 10, direction: String = "backward", completion: @escaping (([LastMessage])->()))
736+ {
737+ let req = GeneralRequest<Int>(direction: direction, limit: l, exclusiveFrom: exclusiveFrom, request: id)
738+
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")
743+
744+ AF.request(request).response { (response) in
745+
746+ isValidTocken?(response.response?.statusCode ?? 498)
747+
748+ guard let json = response.data else { return }
749+ guard let messages = try? decoder.decode([LastMessage].self, from: json) else { return }
750+
751+ completion(messages)
752+ }
753+ }
560754}