Skip to content

Commit 118ba1d

Browse files
committed
Refactor Models
1 parent a4aebd7 commit 118ba1d

File tree

6 files changed

+144
-154
lines changed

6 files changed

+144
-154
lines changed

Sources/LoggingTelegram/Models.swift

Lines changed: 0 additions & 154 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Foundation
2+
3+
protocol TelegramGroupChat: TelegramId {
4+
var mentionedUsers: [TelegramUser] { get set }
5+
}
6+
7+
extension TelegramGroupChat {
8+
func mentioning<T: TelegramUserRepresentable>(_ users: [T]) -> Self {
9+
var newInstance = self
10+
newInstance.mentionedUsers += users.map { T.makeTelegramUser($0) }
11+
return newInstance
12+
}
13+
14+
func mentioning<T: TelegramUserRepresentable>(_ user: T) -> Self {
15+
var newInstance = self
16+
newInstance.mentionedUsers.append(T.makeTelegramUser(user))
17+
return newInstance
18+
}
19+
}
20+
21+
public struct TelegramChannel: TelegramGroupChat {
22+
public let rawId: TelegramRawId
23+
var mentionedUsers: [TelegramUser] = []
24+
25+
public init(_ rawId: TelegramRawId) {
26+
self.rawId = rawId
27+
}
28+
}
29+
30+
public struct TelegramGroup: TelegramGroupChat {
31+
public let rawId: TelegramRawId
32+
var mentionedUsers: [TelegramUser] = []
33+
34+
public init(_ rawId: TelegramRawId) {
35+
self.rawId = rawId
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Foundation
2+
3+
public enum TelegramRawId: Encodable {
4+
case id(Int)
5+
case name(String)
6+
7+
public func encode(to encoder: Encoder) throws {
8+
var container = encoder.singleValueContainer()
9+
switch self {
10+
case .id(let userId):
11+
try container.encode(userId)
12+
case .name(let userName):
13+
try container.encode("@" + userName)
14+
}
15+
}
16+
}
17+
18+
public protocol TelegramId: Encodable {
19+
var rawId: TelegramRawId { get }
20+
init(_: TelegramRawId)
21+
}
22+
23+
public extension TelegramId {
24+
static func id(_ id: Int) -> Self {
25+
return Self(.id(id))
26+
}
27+
28+
static func name(_ name: String) -> Self {
29+
return Self(.name(name))
30+
}
31+
32+
func encode(to encoder: Encoder) throws {
33+
var container = encoder.singleValueContainer()
34+
try container.encode(self.rawId)
35+
}
36+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Foundation
2+
3+
enum TelegramReturn: Decodable {
4+
init(from decoder: Decoder) throws {
5+
let container = try decoder.container(keyedBy: CodingKeys.self)
6+
let status = try container.decode(Bool.self, forKey: .ok)
7+
if status {
8+
self = .ok
9+
} else {
10+
let code = try container.decode(Int.self, forKey: .code)
11+
let message = try container.decode(String.self, forKey: .message)
12+
self = .error(code, message)
13+
}
14+
}
15+
16+
case ok
17+
case error(Int, String)
18+
19+
enum CodingKeys: String, CodingKey {
20+
case ok
21+
case code = "error_code"
22+
case message = "description"
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Foundation
2+
3+
public struct TelegramUser: TelegramId, CustomStringConvertible {
4+
public let rawId: TelegramRawId
5+
6+
public var description: String {
7+
switch rawId {
8+
case .id(let userId):
9+
return "[mentioning \(userId)](tg://user?id=\(userId))"
10+
case .name(let userName):
11+
return "@\(userName)"
12+
}
13+
}
14+
15+
public init(_ rawId: TelegramRawId) {
16+
self.rawId = rawId
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Foundation
2+
3+
protocol TelegramUserRepresentable {
4+
static func makeTelegramUser(_: Self) -> TelegramUser
5+
}
6+
7+
extension Int: TelegramUserRepresentable {
8+
static func makeTelegramUser(_ id: Int) -> TelegramUser {
9+
return TelegramUser.id(id)
10+
}
11+
}
12+
13+
extension String: TelegramUserRepresentable {
14+
static func makeTelegramUser(_ name: String) -> TelegramUser {
15+
return TelegramUser.name(name)
16+
}
17+
}
18+
19+
extension TelegramRawId: TelegramUserRepresentable {
20+
static func makeTelegramUser(_ rawId: TelegramRawId) -> TelegramUser {
21+
return TelegramUser(rawId)
22+
}
23+
}
24+
25+
extension TelegramUser: TelegramUserRepresentable {
26+
static func makeTelegramUser(_ user: TelegramUser) -> TelegramUser {
27+
return user
28+
}
29+
}

0 commit comments

Comments
 (0)