Skip to content

Commit

Permalink
Add some commands
Browse files Browse the repository at this point in the history
  • Loading branch information
KC-2001MS committed Dec 9, 2024
1 parent 557c33d commit 0d4f3d1
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 88 deletions.
54 changes: 0 additions & 54 deletions .swiftpm/xcode/xcshareddata/xcschemes/atpctrlTests.xcscheme

This file was deleted.

4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/MasterJ93/ATProtoKit.git",
"state" : {
"revision" : "c0622712ade49a04b24d2384083b3d99444b540b",
"version" : "0.20.3"
"revision" : "990851d7fd48b90bade9631cbb1c58071d3591be",
"version" : "0.21.0"
}
},
{
Expand Down
13 changes: 10 additions & 3 deletions Sources/Subcommands/BlockedAccounts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ struct BlockedAccounts: AsyncParsableCommand {
)

mutating func run() async throws {
let atProto = try await restoreLogin()
let atProto: ATProtoKit

do {
atProto = try await restoreLogin()
} catch {
LoginErrorView().render()
return
}
//Get a list of blocked accounts
//FIXME: It seems to be a bug in the ATProtoKit framework. It could be fixed with an update.
let result = try await atProto.getListBlocks()
let users = result.blocks
let result = try? await atProto.getListBlocks()
let users = result?.blocks ?? []
//Display process
Group {
Text("Blocked Accounts")
Expand Down
34 changes: 25 additions & 9 deletions Sources/Subcommands/Feeds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ struct Feeds: AsyncParsableCommand {
)

mutating func run() async throws {
let atProto = try await restoreLogin()
let atProto: ATProtoKit

do {
atProto = try await restoreLogin()
} catch {
LoginErrorView().render()
return
}
//Get a list of suggested feeds
let getSuggestedFeedsItem = try await atProto.getSuggestedFeeds()
let suggestions = getSuggestedFeedsItem.feeds
let feeds: [AppBskyLexicon.Feed.GeneratorViewDefinition]

let getSuggestedFeedsItem = try? await atProto.getSuggestedFeeds()
feeds = getSuggestedFeedsItem?.feeds ?? []


//Display process
Group {
Expand All @@ -42,14 +52,20 @@ struct Feeds: AsyncParsableCommand {
.forgroundColor(.eight_bit(244))
.newLine()

for suggestion in suggestions {
Text(suggestion.displayName.isEmpty ? "No display Name" : suggestion.displayName)
if feeds.isEmpty {
Text("No Feeds")
.forgroundColor(.red)
.newLine()
} else {
for suggestion in feeds {
Text(suggestion.displayName.isEmpty ? "No display Name" : suggestion.displayName)
.newLine()

if let displayName = suggestion.creator.displayName {
Text("Created by \(displayName)")
.forgroundColor(.eight_bit(244))
.newLine()
if let displayName = suggestion.creator.displayName {
Text("Created by \(displayName)")
.forgroundColor(.eight_bit(244))
.newLine()
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion Sources/Subcommands/Followers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ struct Followers: AsyncParsableCommand {
)

mutating func run() async throws {
let atProto = try await restoreLogin()
let atProto: ATProtoKit

do {
atProto = try await restoreLogin()
} catch {
LoginErrorView().render()
return
}
//Get a list of blocked accounts
//FIXME: It seems to be a bug in the ATProtoKit framework. It could be fixed with an update.
let result = try await atProto.getFollowers(by: text.isEmpty ? atProto.session?.handle ?? text : text)
Expand Down
9 changes: 8 additions & 1 deletion Sources/Subcommands/Following.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ struct Following: AsyncParsableCommand {
)

mutating func run() async throws {
let atProto = try await restoreLogin()
let atProto: ATProtoKit

do {
atProto = try await restoreLogin()
} catch {
LoginErrorView().render()
return
}
//Get a list of blocked accounts
//FIXME: It seems to be a bug in the ATProtoKit framework. It could be fixed with an update.
let result = try await atProto.getFollows(from: text.isEmpty ? atProto.session?.handle ?? text : text)
Expand Down
10 changes: 9 additions & 1 deletion Sources/Subcommands/Lists.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ struct Lists: AsyncParsableCommand {
)

mutating func run() async throws {
let atProto = try await restoreLogin()
let atProto: ATProtoKit

do {
atProto = try await restoreLogin()
} catch {
LoginErrorView().render()
return
}

let profile = try await atProto.getProfile(text.isEmpty ? atProto.session?.handle ?? text : text)
//FIXME: I do not know how to specify the DID for the argument of the getLists function. It does not work.
let result = try await atProto.getLists(from: profile.actorDID)
Expand Down
13 changes: 10 additions & 3 deletions Sources/Subcommands/MutesAccounts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ struct MutesAccounts: AsyncParsableCommand {
)

mutating func run() async throws {
let atProto = try await restoreLogin()
let atProto: ATProtoKit

do {
atProto = try await restoreLogin()
} catch {
LoginErrorView().render()
return
}
//Get a list of mutes accounts
let result = try await atProto.getMutes()
let users = result.mutes
let result = try? await atProto.getMutes()
let users = result?.mutes ?? []
//Display process
Group {
Text("Mutes Accounts")
Expand Down
13 changes: 10 additions & 3 deletions Sources/Subcommands/Notifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ struct Notifications: AsyncParsableCommand {
)

mutating func run() async throws {
let atProto = try await restoreLogin()
let atProto: ATProtoKit

do {
atProto = try await restoreLogin()
} catch {
LoginErrorView().render()
return
}
//Get a list of notifications
let result = try await atProto.listNotifications(priority: nil)
let notifications = result.notifications
let result = try? await atProto.listNotifications(priority: nil)
let notifications = result?.notifications ?? []

Group {
Text("Notifications")
Expand Down
9 changes: 8 additions & 1 deletion Sources/Subcommands/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ struct Post: AsyncParsableCommand {
)

mutating func run() async throws {
let atProto = try await restoreLogin()
let atProto: ATProtoKit

do {
atProto = try await restoreLogin()
} catch {
LoginErrorView().render()
return
}

Group {
Text("Please enter the content of your submission.")
Expand Down
9 changes: 8 additions & 1 deletion Sources/Subcommands/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ struct User: AsyncParsableCommand {
)

mutating func run() async throws {
let atProto = try await restoreLogin()
let atProto: ATProtoKit

do {
atProto = try await restoreLogin()
} catch {
LoginErrorView().render()
return
}
//Retrieve user profiles
let result: AppBskyLexicon.Actor.ProfileViewDetailedDefinition? = try? await atProto.getProfile(text.isEmpty ? atProto.session?.handle ?? text : text)

Expand Down
17 changes: 12 additions & 5 deletions Sources/Subcommands/Users.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@ struct Users: AsyncParsableCommand {
)

mutating func run() async throws {
let atProto = try await restoreLogin()
let atProto: ATProtoKit

do {
atProto = try await restoreLogin()
} catch {
LoginErrorView().render()
return
}
//Get account list
let users: Array<AppBskyLexicon.Actor.ProfileViewDefinition>
if text.isEmpty {
let result = try await atProto.getSuggestedFollowsByActor(atProto.session?.sessionDID ?? "")
users = result.suggestions
let result = try? await atProto.getSuggestedFollowsByActor(atProto.session?.sessionDID ?? "")
users = result?.suggestions ?? []
} else {
let result = try await atProto.searchUsers(matching: text)
users = result.actors
let result = try? await atProto.searchUsers(matching: text)
users = result?.actors ?? []
}
//Display process
Group {
Expand Down
16 changes: 16 additions & 0 deletions Sources/View/LoginErrorView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// LoginErrorView.swift
// atpctrl
//
// Created by 茅根啓介 on 2024/12/08.
//

import SwiftLI

struct LoginErrorView: View {
var body: [View] {
Text("Login with stored password failed.")
.forgroundColor(.red)
.newLine()
}
}
8 changes: 4 additions & 4 deletions Sources/loginLogic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

import ATProtoKit

func restoreLogin() async throws -> (ATProtoKit) {
func restoreLogin() async throws -> ATProtoKit {
let atProto: ATProtoKit

if let account = try? FileHelper.loadLoginData() {
let config = ATProtocolConfiguration(handle: account.handle, appPassword: account.password)
do {
let result = try await config.authenticate()
atProto = ATProtoKit(session: result)
try await config.authenticate()
} catch {
throw(RuntimeError("\(error)"))
}
atProto = ATProtoKit(sessionConfiguration: config)
} else {
print("There is no login record. Please login with the login command.")
throw(RuntimeError("There is no login record. Please login with the login command."))
}
return (atProto)
return atProto
}

0 comments on commit 0d4f3d1

Please sign in to comment.