feat(chat): 유저 크리에이터 DM을 추가한다
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import Foundation
|
||||
import Moya
|
||||
|
||||
enum UserCreatorChatApi {
|
||||
case createRoom(request: UserCreatorCreateRoomRequest)
|
||||
case openRoom(roomId: Int, limit: Int)
|
||||
case getMessages(roomId: Int, cursor: Int?, limit: Int)
|
||||
case sendVoiceMessage(roomId: Int, parameters: [MultipartFormData])
|
||||
}
|
||||
|
||||
extension UserCreatorChatApi: TargetType {
|
||||
var baseURL: URL {
|
||||
return URL(string: BASE_URL)!
|
||||
}
|
||||
|
||||
var path: String {
|
||||
switch self {
|
||||
case .createRoom:
|
||||
return "/api/v2/user-creator-chat/rooms/create"
|
||||
case .openRoom(let roomId, _):
|
||||
return "/api/v2/user-creator-chat/rooms/\(roomId)/open"
|
||||
case .getMessages(let roomId, _, _):
|
||||
return "/api/v2/user-creator-chat/rooms/\(roomId)/messages"
|
||||
case .sendVoiceMessage(let roomId, _):
|
||||
return "/api/v2/user-creator-chat/rooms/\(roomId)/messages/voice"
|
||||
}
|
||||
}
|
||||
|
||||
var method: Moya.Method {
|
||||
switch self {
|
||||
case .createRoom, .sendVoiceMessage:
|
||||
return .post
|
||||
case .openRoom, .getMessages:
|
||||
return .get
|
||||
}
|
||||
}
|
||||
|
||||
var task: Moya.Task {
|
||||
switch self {
|
||||
case .createRoom(let request):
|
||||
return .requestJSONEncodable(request)
|
||||
case .openRoom(_, let limit):
|
||||
return .requestParameters(parameters: ["limit": limit], encoding: URLEncoding.queryString)
|
||||
case .getMessages(_, let cursor, let limit):
|
||||
var parameters: [String: Any] = ["limit": limit]
|
||||
if let cursor {
|
||||
parameters["cursor"] = cursor
|
||||
}
|
||||
return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
|
||||
case .sendVoiceMessage(_, let parameters):
|
||||
return .uploadMultipart(parameters)
|
||||
}
|
||||
}
|
||||
|
||||
var headers: [String: String]? {
|
||||
return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import Foundation
|
||||
import Combine
|
||||
import CombineMoya
|
||||
import Moya
|
||||
|
||||
final class UserCreatorChatRepository {
|
||||
private let api = MoyaProvider<UserCreatorChatApi>()
|
||||
|
||||
func createRoom(creatorId: Int) -> AnyPublisher<Response, MoyaError> {
|
||||
return api.requestPublisher(.createRoom(request: UserCreatorCreateRoomRequest(creatorId: creatorId)))
|
||||
}
|
||||
|
||||
func openRoom(roomId: Int, limit: Int) -> AnyPublisher<Response, MoyaError> {
|
||||
return api.requestPublisher(.openRoom(roomId: roomId, limit: limit))
|
||||
}
|
||||
|
||||
func getMessages(roomId: Int, cursor: Int?, limit: Int) -> AnyPublisher<Response, MoyaError> {
|
||||
return api.requestPublisher(.getMessages(roomId: roomId, cursor: cursor, limit: limit))
|
||||
}
|
||||
|
||||
func sendVoiceMessage(roomId: Int, voiceData: Data) -> AnyPublisher<Response, MoyaError> {
|
||||
let requestData = Data("{\"recipientId\": null}".utf8)
|
||||
let fileName = "\(UUID().uuidString)_\(Date().timeIntervalSince1970 * 1000).m4a"
|
||||
let multipartData = [
|
||||
MultipartFormData(provider: .data(voiceData), name: "voiceMessageFile", fileName: fileName, mimeType: "audio/m4a"),
|
||||
MultipartFormData(provider: .data(requestData), name: "request")
|
||||
]
|
||||
return api.requestPublisher(.sendVoiceMessage(roomId: roomId, parameters: multipartData))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user