From 14abe6d051ffd8bb3074a75e207c8bfbbafe629f Mon Sep 17 00:00:00 2001 From: 12944qwerty Date: Thu, 28 Sep 2023 18:52:31 -0400 Subject: [PATCH 1/3] implement message ack --- Sources/DiscordKitCore/APIUtils.swift | 2 ++ .../Objects/REST/MessageReadAck.swift | 4 ++++ Sources/DiscordKitCore/REST/APIChannel.swift | 14 +++++++++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Sources/DiscordKitCore/APIUtils.swift b/Sources/DiscordKitCore/APIUtils.swift index ead4223f6..a65792ba4 100644 --- a/Sources/DiscordKitCore/APIUtils.swift +++ b/Sources/DiscordKitCore/APIUtils.swift @@ -7,6 +7,8 @@ import Foundation +let DISCORD_EPOCH: TimeInterval = 1420070400000 + let iso8601 = { () -> ISO8601DateFormatter in let fmt = ISO8601DateFormatter() fmt.formatOptions = [.withInternetDateTime] diff --git a/Sources/DiscordKitCore/Objects/REST/MessageReadAck.swift b/Sources/DiscordKitCore/Objects/REST/MessageReadAck.swift index b1645b4c0..552dacff5 100644 --- a/Sources/DiscordKitCore/Objects/REST/MessageReadAck.swift +++ b/Sources/DiscordKitCore/Objects/REST/MessageReadAck.swift @@ -9,4 +9,8 @@ import Foundation public struct MessageReadAck: Codable { public let token: String? + public let last_viewed: Int? + + public let manual: Bool? + public let mention_count: Int? } diff --git a/Sources/DiscordKitCore/REST/APIChannel.swift b/Sources/DiscordKitCore/REST/APIChannel.swift index f4601d6b8..eaaa1dcb1 100644 --- a/Sources/DiscordKitCore/REST/APIChannel.swift +++ b/Sources/DiscordKitCore/REST/APIChannel.swift @@ -14,7 +14,7 @@ public extension DiscordREST { after: Snowflake? = nil ) async throws -> [Message] { var query = [URLQueryItem(name: "limit", value: String(limit))] - if around != nil { query.append(URLQueryItem(name: "around", value: around?.description)) } else if before != nil {query.append(URLQueryItem(name: "before", value: before?.description))} else if after != nil { query.append(URLQueryItem(name: "after", value: after?.description)) } + if around != nil { query.append(URLQueryItem(name: "around", value: around?.description)) } else if before != nil {query.append(URLQueryItem(name: "before", value: before?.description))} else if after != nil { query.append(URLQueryItem(name: "after", value: after?.description)) } return try await getReq(path: "channels/\(id)/messages", query: query) } @@ -64,9 +64,17 @@ public extension DiscordREST { /// > POST: `/channels/{channel.id}/messages/{message.id}/ack` func ackMessageRead( id: Snowflake, - msgID: Snowflake + msgID: Snowflake, + manual: Bool?, + mention_count: Int? ) async throws -> MessageReadAck { - return try await postReq(path: "channels/\(id)/messages/\(msgID)/ack", body: MessageReadAck(token: nil), attachments: []) + if manual ?? false { + return try await postReq(path: "channels/\(id)/messages/\(msgID)/ack", body: MessageReadAck(token: nil, last_viewed: nil, manual: manual, mention_count: mention_count)) + } + + let lastViewed = Int(ceil((Date().timeIntervalSince1970 - DISCORD_EPOCH) / 864e5)) + + return try await postReq(path: "channels/\(id)/messages/\(msgID)/ack", body: MessageReadAck(token: nil, last_viewed: lastViewed, manual: nil, mention_count: nil)) } /// Typing Start (Undocumented endpoint!) From 4cff754088ebabdddcf6ddab5ed773e8206f93fe Mon Sep 17 00:00:00 2001 From: 12944qwerty Date: Thu, 28 Sep 2023 19:01:14 -0400 Subject: [PATCH 2/3] default args --- Sources/DiscordKitCore/REST/APIChannel.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/DiscordKitCore/REST/APIChannel.swift b/Sources/DiscordKitCore/REST/APIChannel.swift index eaaa1dcb1..13e9739aa 100644 --- a/Sources/DiscordKitCore/REST/APIChannel.swift +++ b/Sources/DiscordKitCore/REST/APIChannel.swift @@ -65,8 +65,8 @@ public extension DiscordREST { func ackMessageRead( id: Snowflake, msgID: Snowflake, - manual: Bool?, - mention_count: Int? + manual: Bool? = false, + mention_count: Int? = 0 ) async throws -> MessageReadAck { if manual ?? false { return try await postReq(path: "channels/\(id)/messages/\(msgID)/ack", body: MessageReadAck(token: nil, last_viewed: nil, manual: manual, mention_count: mention_count)) From ddca3558feb9c184f67b9202464d615fc19a9163 Mon Sep 17 00:00:00 2001 From: 12944qwerty Date: Thu, 28 Sep 2023 19:08:27 -0400 Subject: [PATCH 3/3] oops --- Sources/DiscordKitCore/REST/APIChannel.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/DiscordKitCore/REST/APIChannel.swift b/Sources/DiscordKitCore/REST/APIChannel.swift index 13e9739aa..e9158d3e5 100644 --- a/Sources/DiscordKitCore/REST/APIChannel.swift +++ b/Sources/DiscordKitCore/REST/APIChannel.swift @@ -72,7 +72,7 @@ public extension DiscordREST { return try await postReq(path: "channels/\(id)/messages/\(msgID)/ack", body: MessageReadAck(token: nil, last_viewed: nil, manual: manual, mention_count: mention_count)) } - let lastViewed = Int(ceil((Date().timeIntervalSince1970 - DISCORD_EPOCH) / 864e5)) + let lastViewed = Int(ceil((Date().timeIntervalSince1970*1000 - DISCORD_EPOCH) / 864e5)) return try await postReq(path: "channels/\(id)/messages/\(msgID)/ack", body: MessageReadAck(token: nil, last_viewed: lastViewed, manual: nil, mention_count: nil)) }