Skip to content

Commit

Permalink
Merge pull request #131 from TwidereProject/feature/twitter-basic-api
Browse files Browse the repository at this point in the history
Migrate to Twitter basic api
  • Loading branch information
MainasuK committed May 5, 2023
2 parents 8c15c34 + 73c6e74 commit e1ebf42
Show file tree
Hide file tree
Showing 166 changed files with 1,481 additions and 9,173 deletions.
4 changes: 2 additions & 2 deletions NotificationService/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>CFBundleShortVersionString</key>
<string>1.4.3</string>
<string>2.0.0</string>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
Expand All @@ -19,7 +19,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleVersion</key>
<string>118</string>
<string>125</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
Expand Down
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: e2c773b0e4d6bfd3166d7794b7f8babe8f9b9b92

COCOAPODS: 1.11.3
COCOAPODS: 1.12.1
4 changes: 2 additions & 2 deletions ShareExtension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.4.3</string>
<string>2.0.0</string>
<key>CFBundleVersion</key>
<string>118</string>
<string>125</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
Expand Down
20 changes: 3 additions & 17 deletions TwidereSDK/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ let package = Package(
.library(
name: "TwidereSDK",
targets: [
"TwitterSDK",
"MastodonSDK",
"CoreDataStack",
"TwidereAsset",
Expand Down Expand Up @@ -52,6 +51,7 @@ let package = Package(
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.6.0"),
.package(url: "https://github.com/tid-kijyun/Kanna.git", from: "5.2.7"),
.package(url: "https://github.com/JohnSundell/CollectionConcurrencyKit.git", from: "0.2.0"),
.package(url: "https://github.com/TwidereProject/TwitterSDK.git", exact: "0.4.0"),
.package(name: "ArkanaKeys", path: "../dependencies/ArkanaKeys"),
.package(name: "CoverFlowStackLayout", path: "../CoverFlowStackLayout"),
],
Expand All @@ -67,20 +67,6 @@ let package = Package(
"Template/Stencil"
]
),
.target(
name: "TwitterSDK",
dependencies: [
.product(name: "SwiftyJSON", package: "SwiftyJSON"),
.product(name: "NIOHTTP1", package: "swift-nio"),
]
),
.testTarget(
name: "TwitterSDKTests",
dependencies: [
"TwitterSDK",
.product(name: "CommonOSLog", package: "CommonOSLog"),
]
),
.target(
name: "MastodonSDK",
dependencies: [
Expand All @@ -96,10 +82,10 @@ let package = Package(
.target(
name: "TwidereCommon",
dependencies: [
"TwitterSDK",
"MastodonSDK",
.product(name: "KeychainAccess", package: "KeychainAccess"),
.product(name: "ArkanaKeys", package: "ArkanaKeys"),
.product(name: "TwitterSDK", package: "TwitterSDK"),
],
exclude: [
"Template/AutoGenerateProtocolDelegate.swifttemplate",
Expand All @@ -113,7 +99,6 @@ let package = Package(
"TwidereAsset",
"TwidereCommon",
"TwidereLocalization",
"TwitterSDK",
"MastodonSDK",
"CoreDataStack",
.product(name: "CommonOSLog", package: "CommonOSLog"),
Expand All @@ -125,6 +110,7 @@ let package = Package(
.product(name: "DateToolsSwift", package: "DateTools"),
.product(name: "CryptoSwift", package: "CryptoSwift"),
.product(name: "Kanna", package: "Kanna"),
.product(name: "TwitterSDK", package: "TwitterSDK"),
.product(name: "CollectionConcurrencyKit", package: "CollectionConcurrencyKit"),
]
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21271" systemVersion="21G72" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21754" systemVersion="22E261" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="AuthenticationIndex" representedClassName="CoreDataStack.AuthenticationIndex" syncable="YES">
<attribute name="activeAt" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="createdAt" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="homeTimelineActiveAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="identifier" attributeType="UUID" usesScalarValueType="NO"/>
<attribute name="platformRaw" attributeType="String"/>
<relationship name="mastodonAuthentication" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="MastodonAuthentication" inverseName="authenticationIndex" inverseEntity="MastodonAuthentication"/>
Expand Down Expand Up @@ -49,6 +50,7 @@
<relationship name="user" maxCount="1" deletionRule="Nullify" destinationEntity="MastodonUser" inverseName="mastodonAuthentication" inverseEntity="MastodonUser"/>
</entity>
<entity name="MastodonList" representedClassName="CoreDataStack.MastodonList" syncable="YES">
<attribute name="activeAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="domain" attributeType="String"/>
<attribute name="id" attributeType="String"/>
<attribute name="repliesPolicy" optional="YES" attributeType="Binary"/>
Expand Down Expand Up @@ -208,6 +210,7 @@
<relationship name="user" maxCount="1" deletionRule="Nullify" destinationEntity="TwitterUser" inverseName="twitterAuthentication" inverseEntity="TwitterUser"/>
</entity>
<entity name="TwitterList" representedClassName="CoreDataStack.TwitterList" syncable="YES">
<attribute name="activeAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="createdAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="followerCount" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="id" attributeType="String"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ final public class AuthenticationIndex: NSManagedObject {
@NSManaged public private(set) var createdAt: Date
@NSManaged public private(set) var activeAt: Date

// Record the home timeline latest active date
@NSManaged public private(set) var homeTimelineActiveAt: Date?

// one-to-one relationship
@NSManaged public private(set) var twitterAuthentication: TwitterAuthentication?
@NSManaged public private(set) var mastodonAuthentication: MastodonAuthentication?
Expand Down Expand Up @@ -60,6 +63,12 @@ extension AuthenticationIndex {
}
}

public func update(homeTimelineActiveAt: Date) {
if self.homeTimelineActiveAt != homeTimelineActiveAt {
self.homeTimelineActiveAt = homeTimelineActiveAt
}
}

}

extension AuthenticationIndex {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ final public class MastodonList: NSManagedObject {
// sourcery: autoUpdatableObject, autoGenerateProperty
@NSManaged public private(set) var updatedAt: Date

// sourcery: autoUpdatableObject
@NSManaged public private(set) var activeAt: Date?

// many-to-one relationship
// sourcery: autoGenerateRelationship
@NSManaged public private(set) var owner: MastodonUser
Expand Down Expand Up @@ -160,6 +163,11 @@ extension MastodonList: AutoUpdatableObject {
self.updatedAt = updatedAt
}
}
public func update(activeAt: Date?) {
if self.activeAt != activeAt {
self.activeAt = activeAt
}
}
// sourcery:end

// public func update(`private`: Bool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ final public class TwitterList: NSManagedObject {
// sourcery: autoUpdatableObject, autoGenerateProperty
@NSManaged public private(set) var updatedAt: Date

// sourcery: autoUpdatableObject
@NSManaged public private(set) var activeAt: Date?

// many-to-one relationship
// sourcery: autoGenerateRelationship
@NSManaged public private(set) var owner: TwitterUser
Expand Down Expand Up @@ -167,6 +170,11 @@ extension TwitterList: AutoUpdatableObject {
self.updatedAt = updatedAt
}
}
public func update(activeAt: Date?) {
if self.activeAt != activeAt {
self.activeAt = activeAt
}
}
// sourcery:end

public func update(`private`: Bool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ extension TwitterStatus {
continue
}
}

text = text.replacingOccurrences(of: shortURL, with: expandedURL)
}
return text
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ extension AuthenticationContext {
.flatMap { UserObject.mastodon(object: $0.user) }
}
}

public func authenticationIndex(in managedObjectContext: NSManagedObjectContext) -> AuthenticationIndex? {
switch self {
case .twitter(let authenticationContext):
return authenticationContext.authenticationRecord.object(in: managedObjectContext)?.authenticationIndex
case .mastodon(let authenticationContext):
return authenticationContext.authenticationRecord.object(in: managedObjectContext)?.authenticationIndex
}
}
}

extension AuthenticationContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ extension TwitterEntity.URLEntity {
)
}

public init(entity: Twitter.Entity.V2.Entities.URL) {
public init(entity: Twitter.Entity.V2.Entities.URLNode) {
self.init(
start: entity.start,
end: entity.end,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ extension Twitter.Entity.V2.Media {
guard let kind = attachmentKind else { return nil }
guard let width = width,
let height = height
else { return nil }
else {
assertionFailure()
return nil
}
return TwitterAttachment(
kind: kind,
size: CGSize(width: width, height: height),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ extension Persistence.Twitter {
for status in context.dictionary.tweetDict.values {
guard let authorID = status.authorID,
let author = context.dictionary.userDict[authorID]
else { continue }
else {
assertionFailure()
continue
}

var repost: Persistence.TwitterStatus.PersistContextV2.Entity?
var replyTo: Persistence.TwitterStatus.PersistContextV2.Entity?
Expand All @@ -71,7 +74,10 @@ extension Persistence.Twitter {
for referencedTweet in status.referencedTweets ?? [] {
guard let type = referencedTweet.type,
let statusID = referencedTweet.id
else { continue }
else {
assertionFailure()
continue
}
guard let status = context.dictionary.tweetDict[statusID],
let authorID = status.authorID,
let author = context.dictionary.userDict[authorID]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,58 @@
// Created by MainasuK on 2023/3/28.
//

import os.log
import Foundation
import CoreDataStack
import TwitterSDK
import func QuartzCore.CACurrentMediaTime

extension APIService {
public func twitterStatusConversation(
conversationRootStatusID: Twitter.Entity.V2.Tweet.ID,
query: Twitter.API.V2.Status.Timeline.TimelineQuery,
guestAuthentication: Twitter.API.Guest.GuestAuthorization,
query: Twitter.API.V2.Status.Timeline.ConvsersationQuery,
authenticationContext: TwitterAuthenticationContext
) async throws -> Twitter.Response.Content<Twitter.API.V2.Status.Timeline.ConversationContent> {
let response = try await Twitter.API.V2.Status.Timeline.conversation(
session: URLSession(configuration: .ephemeral),
statusID: conversationRootStatusID,
query: query,
authorization: guestAuthentication
authorization: authenticationContext.authorization
)

let statusIDs = response.value.globalObjects.tweets.map { $0.idStr }

_ = try await twitterStatus(
statusIDs: statusIDs,
authenticationContext: authenticationContext
)
#if DEBUG
// log time cost
let start = CACurrentMediaTime()
defer {
// log rate limit
response.logRateLimit()

let end = CACurrentMediaTime()
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: persist cost %.2fs", ((#file as NSString).lastPathComponent), #line, #function, end - start)
}
#endif

let managedObjectContext = backgroundManagedObjectContext
try await managedObjectContext.performChanges {
let content = response.value
let dictionary = Twitter.Response.V2.DictContent(
tweets: content.includes?.tweets ?? [],
users: content.includes?.users ?? [],
media: content.includes?.media ?? [],
places: content.includes?.places ?? [],
polls: content.includes?.polls ?? []
)
let me = authenticationContext.authenticationRecord.object(in: managedObjectContext)?.user

_ = Persistence.Twitter.persist(
in: managedObjectContext,
context: Persistence.Twitter.PersistContextV2(
dictionary: dictionary,
me: me,
networkDate: response.networkDate
)
)
} // end .performChanges { … }

return response
}
Expand Down
Loading

0 comments on commit e1ebf42

Please sign in to comment.