Skip to content

Commit

Permalink
Merge pull request #150 from Tunous/feat/tags
Browse files Browse the repository at this point in the history
API to get, follow and unfollow tags
  • Loading branch information
kkostov committed Apr 22, 2023
2 parents 7157345 + bac1162 commit 57440d8
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 1 deletion.
28 changes: 28 additions & 0 deletions Examples/swiftyadmin/Sources/swiftyadmin/Account/FollowTag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// FollowTag.swift
// Created by Łukasz Rutkowski on 21/04/2023.
//

import ArgumentParser
import Foundation
import TootSDK

struct FollowTag: AsyncParsableCommand {

@Option(name: .short, help: "URL to the instance to connect to")
var url: String

@Option(name: .short, help: "Access token for an account with sufficient permissions.")
var token: String

@Option(name: .shortAndLong, help: "id of the tag")
var id: String

mutating func run() async throws {
print("Following tag with local id: \(id)")
let client = TootClient(instanceURL: URL(string: url)!, accessToken: token)

let tag = try await client.followTag(id)
print(tag)
}
}
28 changes: 28 additions & 0 deletions Examples/swiftyadmin/Sources/swiftyadmin/Account/GetTag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// GetTag.swift
// Created by Łukasz Rutkowski on 21/04/2023.
//

import ArgumentParser
import Foundation
import TootSDK

struct GetTag: AsyncParsableCommand {

@Option(name: .short, help: "URL to the instance to connect to")
var url: String

@Option(name: .short, help: "Access token for an account with sufficient permissions.")
var token: String

@Option(name: .shortAndLong, help: "id of the tag")
var id: String

mutating func run() async throws {
print("Getting tag with local id: \(id)")
let client = TootClient(instanceURL: URL(string: url)!, accessToken: token)

let tag = try await client.getTag(id)
print(tag)
}
}
28 changes: 28 additions & 0 deletions Examples/swiftyadmin/Sources/swiftyadmin/Account/UnfollowTag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// UnfollowTag.swift
// Created by Łukasz Rutkowski on 21/04/2023.
//

import ArgumentParser
import Foundation
import TootSDK

struct UnfollowTag: AsyncParsableCommand {

@Option(name: .short, help: "URL to the instance to connect to")
var url: String

@Option(name: .short, help: "Access token for an account with sufficient permissions.")
var token: String

@Option(name: .shortAndLong, help: "id of the tag")
var id: String

mutating func run() async throws {
print("Unfollowing tag with local id: \(id)")
let client = TootClient(instanceURL: URL(string: url)!, accessToken: token)

let tag = try await client.unfollowTag(id)
print(tag)
}
}
3 changes: 3 additions & 0 deletions Examples/swiftyadmin/Sources/swiftyadmin/swiftyadmin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ struct SwiftyAdmin: AsyncParsableCommand {
GetTrendingTags.self,
GetTrendingPosts.self,
GetTrendingLinks.self,
GetTag.self,
FollowTag.self,
UnfollowTag.self,
])
}
5 changes: 4 additions & 1 deletion Sources/TootSDK/Models/Tag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import Foundation

/// Represents a hashtag used within the content of a post.
public struct Tag: Codable, Hashable, Sendable {
public init(name: String, url: String, history: [History]? = nil) {
public init(name: String, url: String, history: [History]? = nil, following: Bool? = nil) {
self.name = name
self.url = url
self.history = history
self.following = following
}

/// The value of the hashtag after the # sign.
Expand All @@ -17,4 +18,6 @@ public struct Tag: Codable, Hashable, Sendable {
public let url: String
/// Usage statistics for given days.
public let history: [History]?
/// Are you following this tag?
public let following: Bool?
}
4 changes: 4 additions & 0 deletions Sources/TootSDK/Models/TootSDKFlavour.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import Foundation
public enum TootSDKFlavour: String, Codable, Sendable, CaseIterable {
/// Mastodon server. API Documentation can be found at https://docs.joinmastodon.org/api/
case mastodon

/// Pleroma server. API Documentation can be found at https://docs-develop.pleroma.social/backend/development/API/differences_in_mastoapi_responses/
case pleroma

/// Pixelfed server. API Documentation can be found at https://docs.pixelfed.org/technical-documentation/api/
case pixelfed

/// Friendica server. API Documentation can be found at https://github.com/friendica/friendica/blob/stable/doc/API-Mastodon.md
case friendica

/// Akkoma server. API Documentation can be found at https://docs.akkoma.dev/stable/development/API/differences_in_mastoapi_responses/
case akkoma
}
55 changes: 55 additions & 0 deletions Sources/TootSDK/TootClient/TootClient+Tags.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// TootClient+Tags.swift
// Created by Łukasz Rutkowski on 21/04/2023.
//

import Foundation

public extension TootClient {

/// Get a tag.
/// - Parameter id: Name of the tag.
func getTag(_ id: String) async throws -> Tag {
let req = HTTPRequestBuilder {
$0.url = getURL(["api", "v1", "tags", id])
$0.method = .get
}

return try await fetch(Tag.self, req)
}

/// Follow a tag.
/// - Parameter id: Name of the tag.
@discardableResult
func followTag(_ id: String) async throws -> Tag {
try requireFlavour(flavoursSupportingFollowingTags)
let req = HTTPRequestBuilder {
$0.url = getURL(["api", "v1", "tags", id, "follow"])
$0.method = .post
}

return try await fetch(Tag.self, req)
}

/// Unfollow a tag.
/// - Parameter id: Name of the tag.
@discardableResult
func unfollowTag(_ id: String) async throws -> Tag {
try requireFlavour(flavoursSupportingFollowingTags)
let req = HTTPRequestBuilder {
$0.url = getURL(["api", "v1", "tags", id, "unfollow"])
$0.method = .post
}

return try await fetch(Tag.self, req)
}

/// Tells whether current flavour supports following or unfollowing tags.
var canFollowTags: Bool {
flavoursSupportingFollowingTags.contains(flavour)
}

private var flavoursSupportingFollowingTags: Set<TootSDKFlavour> {
[.mastodon, .friendica]
}
}

0 comments on commit 57440d8

Please sign in to comment.