Skip to content

Commit

Permalink
Merge pull request #278 from TootSDK/add-post-edited
Browse files Browse the repository at this point in the history
Adds Post.editedAt, introduced in Mastodon 4.2
  • Loading branch information
kkostov committed Apr 24, 2024
2 parents 395207b + d77f858 commit fbb85de
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 8 deletions.
19 changes: 11 additions & 8 deletions Package.swift
Expand Up @@ -9,21 +9,23 @@ let package = Package(
.macOS(.v12),
.iOS(.v14),
.watchOS(.v7),
.tvOS(.v14)
.tvOS(.v14),
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(name: "MultipartKitTootSDK",
targets: ["MultipartKitTootSDK"]),
.library(name: "TootSDK",
targets: ["TootSDK"])
.library(
name: "MultipartKitTootSDK",
targets: ["MultipartKitTootSDK"]),
.library(
name: "TootSDK",
targets: ["TootSDK"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.2.0"),
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.2"),
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.4.3"),
.package(url: "https://github.com/karwa/swift-url.git", from: "0.4.1"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "4.0.0"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0"..<"4.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -33,13 +35,13 @@ let package = Package(
dependencies: [
.product(name: "NIO", package: "swift-nio"),
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "Collections", package: "swift-collections")
.product(name: "Collections", package: "swift-collections"),
]),
.target(
name: "TootSDK",
dependencies: [
"MultipartKitTootSDK",
.product(name:"SwiftSoup", package: "SwiftSoup"),
.product(name: "SwiftSoup", package: "SwiftSoup"),
.product(name: "WebURL", package: "swift-url"),
.product(name: "WebURLFoundationExtras", package: "swift-url"),
.product(name: "Crypto", package: "swift-crypto"),
Expand Down Expand Up @@ -67,6 +69,7 @@ let package = Package(
.copy("Resources/instance_iceshrimp_contact_removed.json"),
.copy("Resources/instance_sharkey_contact_removed.json"),
.copy("Resources/list.json"),
.copy("Resources/post_edited.json"),
.copy("Resources/post no emojis.json"),
.copy("Resources/post with emojis and attachments.json"),
.copy("Resources/post wordle linebreaks.json"),
Expand Down
7 changes: 7 additions & 0 deletions Sources/TootSDK/Models/Post.swift
Expand Up @@ -30,6 +30,7 @@ public class Post: Codable, Identifiable, @unchecked Sendable {
card: Card? = nil,
language: String? = nil,
text: String? = nil,
editedAt: Date? = nil,
favourited: Bool? = nil,
reposted: Bool? = nil,
muted: Bool? = nil,
Expand Down Expand Up @@ -61,6 +62,7 @@ public class Post: Codable, Identifiable, @unchecked Sendable {
self.card = card
self.language = language
self.text = text
self.editedAt = editedAt
self.favourited = favourited
self.reposted = reposted
self.muted = muted
Expand Down Expand Up @@ -119,6 +121,8 @@ public class Post: Codable, Identifiable, @unchecked Sendable {
/// Plain-text source of a post. Returned instead of content when post is deleted so the user
/// may redraft from the source text without the client having to reverse-engineer the original text from the HTML content.
public var text: String?
/// The date when the post was last edited.
public var editedAt: Date?
/// Have you favourited this post?
public var favourited: Bool?
/// Have you reposted this post?
Expand Down Expand Up @@ -168,6 +172,7 @@ public class Post: Codable, Identifiable, @unchecked Sendable {
case card
case language
case text
case editedAt
case favourited
case reposted = "reblogged"
case muted
Expand Down Expand Up @@ -203,6 +208,7 @@ extension Post: Hashable {
&& lhs.card == rhs.card
&& lhs.language == rhs.language
&& lhs.text == rhs.text
&& lhs.editedAt == rhs.editedAt
&& lhs.favourited == rhs.favourited
&& lhs.reposted == rhs.reposted
&& lhs.muted == rhs.muted
Expand Down Expand Up @@ -236,6 +242,7 @@ extension Post: Hashable {
hasher.combine(card)
hasher.combine(language)
hasher.combine(text)
hasher.combine(editedAt)
hasher.combine(favourited)
hasher.combine(reposted)
hasher.combine(muted)
Expand Down
52 changes: 52 additions & 0 deletions Tests/TootSDKTests/PostEditedTests.swift
@@ -0,0 +1,52 @@
// Created by konstantin on 23/04/2024.

import XCTest

@testable import TootSDK

final class PostTests: XCTestCase {

func testPostEdited() throws {
// arrange
let json = localContent("post_edited")
let decoder = TootDecoder()

// act
let result = try decoder.decode(Post.self, from: json)

// assert
XCTAssertNotNil(result)
XCTAssertEqual(result.id, "112300188527054704")
XCTAssertEqual(result.filtered, [])
XCTAssertEqual(result.mentions, [])
XCTAssertEqual(result.emojis, [])
XCTAssertEqual(result.mediaAttachments, [])
XCTAssertEqual(result.tags.count, 1)
XCTAssertEqual(result.favourited, false)
XCTAssertEqual(result.reposted, false)
XCTAssertNotNil(result.createdAt)

// let's test if the date was correctly parsed
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(identifier: "UTC")!
let components = DateComponents(year: 2024, month: 04, day: 19, hour: 22, minute: 15, second: 22)
let expectedDate = calendar.date(from: components)!
XCTAssertNotNil(result.editedAt)
XCTAssertEqual(result.editedAt!.timeIntervalSince1970, expectedDate.timeIntervalSince1970)

XCTAssertNil(result.inReplyToId)
XCTAssertFalse(result.sensitive)
XCTAssertNotNil(result.content)
XCTAssertNil(result.poll)
XCTAssertNotNil(result.card)
XCTAssertNotNil(result.url)
XCTAssertEqual(result.spoilerText, "")
XCTAssertEqual(result.language, "en")
XCTAssertEqual(result.visibility, .public)
XCTAssertEqual(result.pinned, false)
XCTAssertEqual(result.muted, false)
XCTAssertEqual(result.favouritesCount, 1)
XCTAssertEqual(result.repliesCount, 1)
XCTAssertEqual(result.bookmarked, true)
}
}
90 changes: 90 additions & 0 deletions Tests/TootSDKTests/Resources/post_edited.json
@@ -0,0 +1,90 @@
{
"filtered": [],
"reblog": null,
"application": {
"name": "Elk",
"website": "https://elk.zone"
},
"language": "en",
"account": {
"acct": "person",
"bot": false,
"locked": false,
"avatar_static": "https://media.example.com/accounts/avatars/109/821/003/814/913/641/original/d15a73c9d3b22e27.jpeg",
"statuses_count": 14881,
"url": "https://example.com/@person",
"following_count": 394,
"emojis": [],
"discoverable": true,
"last_status_at": "2024-04-20",
"group": false,
"uri": "https://example.com/users/person",
"id": "109821003814913641",
"roles": [],
"header_static": "https://media.example.com/accounts/headers/109/821/003/814/913/641/original/6a6d879a38c226fd.jpeg",
"followers_count": 424,
"note": "<p>This is my talk (and block) about anything account.</p>",
"avatar": "https://media.example.com/accounts/avatars/109/821/003/814/913/641/original/d15a73c9d3b22e27.jpeg",
"display_name": "Person",
"header": "https://media.example.com/accounts/headers/109/821/003/814/913/641/original/6a6d879a38c226fd.jpeg",
"created_at": "2023-02-07T00:00:00.000Z",
"noindex": false,
"fields": [
{
"name": "Example",
"value": "<a href=\"https://example.com/technicat\" target=\"_blank\" rel=\"nofollow noopener noreferrer me\" translate=\"no\"><span class=\"invisible\">https://</span><span class=\"\">example.com/person</span><span class=\"invisible\"></span></a>",
"verified_at": null
}
],
"username": "person"
},
"spoiler_text": "",
"url": "https://example.com/@person/112300188527054704",
"media_attachments": [],
"sensitive": false,
"replies_count": 1,
"tags": [
{
"name": "ub40",
"url": "https://example.com/tags/ub40"
}
],
"emojis": [],
"muted": false,
"mentions": [],
"visibility": "public",
"card": {
"description": "UB45 by UB40",
"provider_url": "",
"embed_url": "",
"image_description": "UB45 by UB40",
"blurhash": "UJEfG_n#02R+ofRjWBxu4:kDxtobWAs;s;Ri",
"published_at": null,
"provider_name": "Album Example",
"width": 1400,
"url": "https://example.com/albums/8513",
"author_url": "",
"type": "link",
"title": "UB45 by UB40",

"image": "https://media.example.com/cache/preview_cards/images/024/062/128/original/1c7d0494ae037a8f.jpg",
"language": "en",
"height": 1400,
"author_name": "",
"html": ""
},
"poll": null,
"uri": "https://example.com/users/technicat/statuses/112300188527054704",
"edited_at": "2024-04-19T22:15:22.000Z",
"id": "112300188527054704",
"reblogs_count": 0,
"favourited": false,
"reblogged": false,
"in_reply_to_account_id": null,
"created_at": "2024-04-19T22:14:48.415Z",
"in_reply_to_id": null,
"pinned": false,
"favourites_count": 1,
"bookmarked": true,
"content": "<p>listening to <a href=\"https://example.com/tags/UB40\" class=\"mention hashtag\" rel=\"tag\">#<span>UB40</span></a> </p><p><a href=\"https://example.com/albums/8513\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" translate=\"no\"><span class=\"invisible\">https://</span><span class=\"\">example.com/albums/8513</span><span class=\"invisible\"></span></a></p>"
}

0 comments on commit fbb85de

Please sign in to comment.