-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
154 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Foundation | ||
|
||
protocol TelegramGroupChat: TelegramId { | ||
var mentionedUsers: [TelegramUser] { get set } | ||
} | ||
|
||
extension TelegramGroupChat { | ||
func mentioning<T: TelegramUserRepresentable>(_ users: [T]) -> Self { | ||
var newInstance = self | ||
newInstance.mentionedUsers += users.map { T.makeTelegramUser($0) } | ||
return newInstance | ||
} | ||
|
||
func mentioning<T: TelegramUserRepresentable>(_ user: T) -> Self { | ||
var newInstance = self | ||
newInstance.mentionedUsers.append(T.makeTelegramUser(user)) | ||
return newInstance | ||
} | ||
} | ||
|
||
public struct TelegramChannel: TelegramGroupChat { | ||
public let rawId: TelegramRawId | ||
var mentionedUsers: [TelegramUser] = [] | ||
|
||
public init(_ rawId: TelegramRawId) { | ||
self.rawId = rawId | ||
} | ||
} | ||
|
||
public struct TelegramGroup: TelegramGroupChat { | ||
public let rawId: TelegramRawId | ||
var mentionedUsers: [TelegramUser] = [] | ||
|
||
public init(_ rawId: TelegramRawId) { | ||
self.rawId = rawId | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Foundation | ||
|
||
public enum TelegramRawId: Encodable { | ||
case id(Int) | ||
case name(String) | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
switch self { | ||
case .id(let userId): | ||
try container.encode(userId) | ||
case .name(let userName): | ||
try container.encode("@" + userName) | ||
} | ||
} | ||
} | ||
|
||
public protocol TelegramId: Encodable { | ||
var rawId: TelegramRawId { get } | ||
init(_: TelegramRawId) | ||
} | ||
|
||
public extension TelegramId { | ||
static func id(_ id: Int) -> Self { | ||
return Self(.id(id)) | ||
} | ||
|
||
static func name(_ name: String) -> Self { | ||
return Self(.name(name)) | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
try container.encode(self.rawId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Foundation | ||
|
||
enum TelegramReturn: Decodable { | ||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let status = try container.decode(Bool.self, forKey: .ok) | ||
if status { | ||
self = .ok | ||
} else { | ||
let code = try container.decode(Int.self, forKey: .code) | ||
let message = try container.decode(String.self, forKey: .message) | ||
self = .error(code, message) | ||
} | ||
} | ||
|
||
case ok | ||
case error(Int, String) | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case ok | ||
case code = "error_code" | ||
case message = "description" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Foundation | ||
|
||
public struct TelegramUser: TelegramId, CustomStringConvertible { | ||
public let rawId: TelegramRawId | ||
|
||
public var description: String { | ||
switch rawId { | ||
case .id(let userId): | ||
return "[mentioning \(userId)](tg://user?id=\(userId))" | ||
case .name(let userName): | ||
return "@\(userName)" | ||
} | ||
} | ||
|
||
public init(_ rawId: TelegramRawId) { | ||
self.rawId = rawId | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Sources/LoggingTelegram/Models/TelegramUserRepresentable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Foundation | ||
|
||
protocol TelegramUserRepresentable { | ||
static func makeTelegramUser(_: Self) -> TelegramUser | ||
} | ||
|
||
extension Int: TelegramUserRepresentable { | ||
static func makeTelegramUser(_ id: Int) -> TelegramUser { | ||
return TelegramUser.id(id) | ||
} | ||
} | ||
|
||
extension String: TelegramUserRepresentable { | ||
static func makeTelegramUser(_ name: String) -> TelegramUser { | ||
return TelegramUser.name(name) | ||
} | ||
} | ||
|
||
extension TelegramRawId: TelegramUserRepresentable { | ||
static func makeTelegramUser(_ rawId: TelegramRawId) -> TelegramUser { | ||
return TelegramUser(rawId) | ||
} | ||
} | ||
|
||
extension TelegramUser: TelegramUserRepresentable { | ||
static func makeTelegramUser(_ user: TelegramUser) -> TelegramUser { | ||
return user | ||
} | ||
} |