Skip to content

Commit

Permalink
Merge pull request #959 from planetary-social/task/remove-code-2
Browse files Browse the repository at this point in the history
Remove lots more unused code
  • Loading branch information
joshuatbrown authored Mar 20, 2024
2 parents a302e45 + ab95a21 commit ed1f3d3
Show file tree
Hide file tree
Showing 48 changed files with 39 additions and 352 deletions.
26 changes: 15 additions & 11 deletions Nos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@
CD4908D429B92941007443DB /* ReportABugMailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD4908D329B92941007443DB /* ReportABugMailView.swift */; };
CD76864C29B12F7E00085358 /* ExpandingTextFieldAndSubmitButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD76864B29B12F7E00085358 /* ExpandingTextFieldAndSubmitButton.swift */; };
CD76865029B6503500085358 /* NoteOptionsButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD76864F29B6503500085358 /* NoteOptionsButton.swift */; };
CD76865329B793F400085358 /* DiscoverSearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD76865229B793F400085358 /* DiscoverSearchBar.swift */; };
CDDA1F7B29A527650047ACD8 /* Starscream in Frameworks */ = {isa = PBXBuildFile; productRef = CDDA1F7A29A527650047ACD8 /* Starscream */; };
CDDA1F7D29A527650047ACD8 /* SwiftUINavigation in Frameworks */ = {isa = PBXBuildFile; productRef = CDDA1F7C29A527650047ACD8 /* SwiftUINavigation */; };
DC08FF7E2A7968FF009F87D1 /* FileStorageAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC5F20422A6ED75C00F8D73F /* FileStorageAPI.swift */; };
Expand Down Expand Up @@ -770,7 +769,6 @@
CD4908D329B92941007443DB /* ReportABugMailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportABugMailView.swift; sourceTree = "<group>"; };
CD76864B29B12F7E00085358 /* ExpandingTextFieldAndSubmitButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpandingTextFieldAndSubmitButton.swift; sourceTree = "<group>"; };
CD76864F29B6503500085358 /* NoteOptionsButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoteOptionsButton.swift; sourceTree = "<group>"; };
CD76865229B793F400085358 /* DiscoverSearchBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiscoverSearchBar.swift; sourceTree = "<group>"; };
DC2E54C72A700F1400C2CAAB /* UIDevice+Simulator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIDevice+Simulator.swift"; sourceTree = "<group>"; };
DC4AB2F52A4475B800D1478A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
DC5F203E2A6AE24200F8D73F /* ImagePickerButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagePickerButton.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -857,23 +855,31 @@
isa = PBXGroup;
children = (
C9DEC04329894BED0078B43A /* Author+CoreDataClass.swift */,
C973AB582A323167002AED16 /* Author+CoreDataProperties.swift */,
3F43C47529A9625700E896A0 /* AuthorReference+CoreDataClass.swift */,
C973AB572A323167002AED16 /* AuthorReference+CoreDataProperties.swift */,
C9DEC03F29894BED0078B43A /* Event+CoreDataClass.swift */,
C973AB562A323167002AED16 /* Event+CoreDataProperties.swift */,
3FFB1D9229A6BBCE002A755D /* EventReference+CoreDataClass.swift */,
C973AB5A2A323167002AED16 /* EventReference+CoreDataProperties.swift */,
A3B943D4299D514800A15A08 /* Follow+CoreDataClass.swift */,
C973AB552A323167002AED16 /* Follow+CoreDataProperties.swift */,
C9C547572A4F1D8C006B0741 /* NosNotification+CoreDataClass.swift */,
C9C547582A4F1D8C006B0741 /* NosNotification+CoreDataProperties.swift */,
C9DEC06C2989668E0078B43A /* Relay+CoreDataClass.swift */,
C973AB592A323167002AED16 /* Relay+CoreDataProperties.swift */,
037A2C242BA9D75F00FC554B /* Generated */,
);
path = CoreData;
sourceTree = "<group>";
};
037A2C242BA9D75F00FC554B /* Generated */ = {
isa = PBXGroup;
children = (
C973AB582A323167002AED16 /* Author+CoreDataProperties.swift */,
C973AB572A323167002AED16 /* AuthorReference+CoreDataProperties.swift */,
C973AB562A323167002AED16 /* Event+CoreDataProperties.swift */,
C973AB5A2A323167002AED16 /* EventReference+CoreDataProperties.swift */,
C973AB552A323167002AED16 /* Follow+CoreDataProperties.swift */,
C9C547582A4F1D8C006B0741 /* NosNotification+CoreDataProperties.swift */,
C973AB592A323167002AED16 /* Relay+CoreDataProperties.swift */,
);
path = Generated;
sourceTree = "<group>";
};
3AAB61B12B24CC8A00717A07 /* Extensions */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1340,7 +1346,6 @@
C9DFA968299BEC33006929C1 /* CardStyle.swift */,
C9DFA96A299BEE2C006929C1 /* CompactNoteView.swift */,
C93EC2F929C370DE0012EE2A /* DiscoverGrid.swift */,
CD76865229B793F400085358 /* DiscoverSearchBar.swift */,
C9CDBBA029A8F14C00C555C7 /* DiscoverView.swift */,
5B0D99022A94090A0039F0C5 /* DoubleTapToPopModifier.swift */,
5B8C96B529DDD3B200B73AEC /* EditableText.swift */,
Expand Down Expand Up @@ -1853,7 +1858,6 @@
C9F204632ADEDB910029A858 /* Web3Utils.swift in Sources */,
C9F75AD62A041FF7005BBE45 /* ExpirationTimePicker.swift in Sources */,
C92F015E2AC4D99400972489 /* NosForm.swift in Sources */,
CD76865329B793F400085358 /* DiscoverSearchBar.swift in Sources */,
5B8C96AC29D52AD200B73AEC /* AuthorListView.swift in Sources */,
C94A5E152A716A6D00B6EC5D /* EditableNoteText.swift in Sources */,
DC5F20412A6AE31000F8D73F /* ImagePickerUIViewController.swift in Sources */,
Expand Down
32 changes: 0 additions & 32 deletions Nos/Models/CoreData/Author+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import CoreData
import Dependencies
import Logger

enum AuthorError: Error {
case coreData
}

@objc(Author)
@Observable public class Author: NosManagedObject {

Expand Down Expand Up @@ -305,16 +301,6 @@ enum AuthorError: Error {
return fetchRequest
}

@nonobjc func followsRequest() -> NSFetchRequest<Author> {
let fetchRequest = NSFetchRequest<Author>(entityName: "Author")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Author.hexadecimalPublicKey, ascending: false)]
fetchRequest.predicate = NSPredicate(
format: "ANY followers = %@",
self
)
return fetchRequest
}

func reportsReferencingFetchRequest() -> NSFetchRequest<Event> {
guard let hexadecimalPublicKey else {
return Event.emptyRequest()
Expand All @@ -328,25 +314,7 @@ enum AuthorError: Error {
)
return fetchRequest
}

@nonobjc func authoredReportsRequest() -> NSFetchRequest<Event> {
let fetchRequest = NSFetchRequest<Event>(entityName: "Event")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Event.createdAt, ascending: false)]
fetchRequest.predicate = NSPredicate(
format: "kind = %i AND author = %@",
EventKind.report.rawValue,
self
)
return fetchRequest
}

@nonobjc public class func emptyRequest() -> NSFetchRequest<Author> {
let fetchRequest = NSFetchRequest<Author>(entityName: "Author")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Author.hexadecimalPublicKey, ascending: true)]
fetchRequest.predicate = NSPredicate.false
return fetchRequest
}

class func all(context: NSManagedObjectContext) -> [Author] {
let allRequest = Author.allAuthorsRequest()

Expand Down
105 changes: 3 additions & 102 deletions Nos/Models/CoreData/Event+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import Logger
import Dependencies

enum EventError: Error {
case jsonEncoding
case utf8Encoding
case unrecognizedKind
case missingAuthor
case invalidETag([String])
case invalidSignature(Event)
case expiredEvent
case coreData


var description: String? {
switch self {
case .unrecognizedKind:
Expand Down Expand Up @@ -58,8 +56,7 @@ public enum EventKind: Int64, CaseIterable, Hashable {
public class Event: NosManagedObject {

@Dependency(\.currentUser) @ObservationIgnored private var currentUser
@Dependency(\.persistenceController) @ObservationIgnored private var persistenceController


static var replyNoteReferences = "kind = 1 AND ANY eventReferences.referencedEvent.identifier == %@ " +
"AND author.muted = false"
public static var discoverKinds = [EventKind.text, EventKind.longFormContent]
Expand Down Expand Up @@ -388,21 +385,7 @@ public class Event: NosManagedObject {
fetchRequest.predicate = homeFeedPredicate(for: user, before: before)
return fetchRequest
}

@nonobjc public class func noteIsLikedByUser(for userPubKey: String, noteId: String) -> NSFetchRequest<Event> {
let fetchRequest = NSFetchRequest<Event>(entityName: "Event")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Event.createdAt, ascending: false)]
let noteIsLikedByUserPredicate = NSPredicate(
// swiftlint:disable line_length
format: "kind = \(String(EventKind.like.rawValue)) AND author.hexadecimalPublicKey = %@ AND SUBQUERY(eventReferences, $reference, $reference.eventId = %@).@count > 0 AND deletedOn.@count = 0",
// swiftlint:enable line_length
userPubKey,
noteId
)
fetchRequest.predicate = noteIsLikedByUserPredicate
return fetchRequest
}


@nonobjc public class func likes(noteID: String) -> NSFetchRequest<Event> {
let fetchRequest = NSFetchRequest<Event>(entityName: "Event")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Event.createdAt, ascending: false)]
Expand All @@ -429,15 +412,6 @@ public class Event: NosManagedObject {
return fetchRequest
}

@nonobjc public class func allFollowedPostsRequest(from publicKeys: [String]) -> NSFetchRequest<Event> {
let fetchRequest = NSFetchRequest<Event>(entityName: "Event")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Event.createdAt, ascending: false)]
let kind = EventKind.text.rawValue
let predicate = NSPredicate(format: "kind = %i AND author.hexadecimalPublicKey IN %@", kind, publicKeys)
fetchRequest.predicate = predicate
return fetchRequest
}

@nonobjc public class func emptyRequest() -> NSFetchRequest<Event> {
let fetchRequest = NSFetchRequest<Event>(entityName: "Event")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Event.createdAt, ascending: true)]
Expand All @@ -460,29 +434,6 @@ public class Event: NosManagedObject {
return deleteRequest
}

@nonobjc public class func deletePostsRequest(for identifiers: [String]) -> NSBatchDeleteRequest {
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Event")
fetchRequest.predicate = NSPredicate(format: "identifier IN %@", identifiers)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
return deleteRequest
}

@nonobjc public class func contactListRequest(_ author: Author) -> NSFetchRequest<Event> {
let fetchRequest = NSFetchRequest<Event>(entityName: "Event")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Event.createdAt, ascending: false)]
let kind = EventKind.contactList.rawValue
let key = author.hexadecimalPublicKey ?? "notakey"
fetchRequest.predicate = NSPredicate(format: "kind = %i AND author.hexadecimalPublicKey = %@", kind, key)
return fetchRequest
}

class func oldest() -> NSFetchRequest<Event> {
let request = Event.allEventsRequest()
request.sortDescriptors = [NSSortDescriptor(keyPath: \Event.receivedAt, ascending: true)]
request.fetchLimit = 1
return request
}

class func all(context: NSManagedObjectContext) -> [Event] {
let allRequest = Event.allPostsRequest()

Expand Down Expand Up @@ -569,21 +520,6 @@ public class Event: NosManagedObject {
}
}

func deleteEvents(identifiers: [String], context: NSManagedObjectContext) async {
print("Deleting: \(identifiers)")
let deleteRequest = Event.deletePostsRequest(for: identifiers)

do {
try context.execute(deleteRequest)
} catch let error as NSError {
print("Failed to delete posts in \(identifiers). Error: \(error.description)")
}

await context.perform {
try? context.save()
}
}

/// Populates an event stub (with only its ID set) using the data in the given JSON.
func hydrate(from jsonEvent: JSONEvent, relay: Relay?, in context: NSManagedObjectContext) throws {
guard isStub else {
Expand Down Expand Up @@ -1210,41 +1146,6 @@ public class Event: NosManagedObject {
return "https://njump.me"
}
}

/// Returns a list of the authors this event was reported by if any of them are followed by the given user.
/// This isn't very performant so use sparingly.
@MainActor func reportingAuthors(followedBy currentUser: CurrentUser) async -> [Author] {
let allReferencingEvents = referencingEvents
.compactMap { $0.referencingEvent }

var reportingAuthors = [Author]()
for event in allReferencingEvents {
let isReportEvent = event.kind == EventKind.report.rawValue
let isFollowed = await currentUser.socialGraph.follows(event.author?.hexadecimalPublicKey ?? "")
if isReportEvent && isFollowed, let author = event.author {
reportingAuthors.append(author)
}
}
return reportingAuthors
}

/// Returns a list of reports for this event from authors followed by the given user.
/// This isn't very performant so use sparingly.
@MainActor
func reports(followedBy currentUser: CurrentUser) async -> [Event] {
let allReferencingEvents = referencingEvents
.compactMap { $0.referencingEvent }

var followedReportEvents = [Event]()
for event in allReferencingEvents {
let isReportEvent = event.kind == EventKind.report.rawValue
let isFollowed = await currentUser.socialGraph.follows(event.author?.hexadecimalPublicKey ?? "")
if isReportEvent && isFollowed {
followedReportEvents.append(event)
}
}
return followedReportEvents
}
}
// swiftlint:enable type_body_length
// swiftlint:enable file_length
9 changes: 1 addition & 8 deletions Nos/Models/CoreData/Follow+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,7 @@ public class Follow: NosManagedObject {
fetchRequest.predicate = NSPredicate(format: "destination IN %@", authors)
return fetchRequest
}

@nonobjc public class func emptyRequest() -> NSFetchRequest<Follow> {
let fetchRequest = NSFetchRequest<Follow>(entityName: "Follow")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Follow.petName, ascending: true)]
fetchRequest.fetchLimit = 0
return fetchRequest
}


/// Retreives all the Follows whose source Author has been deleted.
static func orphanedRequest() -> NSFetchRequest<Follow> {
let fetchRequest = NSFetchRequest<Follow>(entityName: "Follow")
Expand Down
57 changes: 1 addition & 56 deletions Nos/Models/CoreData/Relay+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ public class Relay: NosManagedObject {
]
}

@nonobjc public class func allRelaysRequest() -> NSFetchRequest<Relay> {
let fetchRequest = NSFetchRequest<Relay>(entityName: "Relay")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Relay.address, ascending: true)]
return fetchRequest
}

@nonobjc public class func relay(by address: String) -> NSFetchRequest<Relay> {
let fetchRequest = NSFetchRequest<Relay>(entityName: "Relay")
fetchRequest.predicate = NSPredicate(format: "address = %@", address)
Expand All @@ -65,13 +59,6 @@ public class Relay: NosManagedObject {
return fetchRequest
}

@nonobjc public class func emptyRequest() -> NSFetchRequest<Relay> {
let fetchRequest = NSFetchRequest<Relay>(entityName: "Relay")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Relay.createdAt, ascending: true)]
fetchRequest.predicate = NSPredicate.false
return fetchRequest
}

/// Retreives all the Relays that are no longer referenced by anyone in the db.
static func orphanedRequest() -> NSFetchRequest<Relay> {
let fetchRequest = NSFetchRequest<Relay>(entityName: "Relay")
Expand Down Expand Up @@ -119,23 +106,7 @@ public class Relay: NosManagedObject {
version = jsonMetadata.version
metadataFetchedAt = Date.now
}

var jsonRepresentation: String? {
address
}

class func all(context: NSManagedObjectContext) -> [Relay] {
let allRequest = Relay.allRelaysRequest()

do {
let results = try context.fetch(allRequest)
return results
} catch let error as NSError {
print("Failed to fetch relays. Error: \(error.description)")
return []
}
}


convenience init(context: NSManagedObjectContext, address: String, author: Author? = nil) throws {
guard let addressURL = URL(string: address),
addressURL.scheme == "wss" else {
Expand Down Expand Up @@ -165,30 +136,4 @@ public class Relay: NosManagedObject {
var hasMetadata: Bool {
metadataFetchedAt != nil
}

var metadata: String {
var attributes = [String]()
if let name {
attributes.append("Name: \(name)")
}
if let relayDescription {
attributes.append("Description: \(relayDescription)")
}
if let supportedNIPs {
attributes.append("Supported NIPs: \(supportedNIPs.map { String($0) }.joined(separator: ", "))")
}
if let pubkey {
attributes.append("PubKey: \(pubkey.prefix(7))")
}
if let contact {
attributes.append("Contact: \(contact.prefix(7))")
}
if let software {
attributes.append("Software: \(software)")
}
if let version {
attributes.append("Version: \(version)")
}
return attributes.joined(separator: "\n")
}
}
8 changes: 3 additions & 5 deletions Nos/Views/AboutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ struct AboutView: View {
}
}

struct AboutView_Previews: PreviewProvider {
static var previews: some View {
NavigationStack {
AboutView()
}
#Preview {
NavigationStack {
AboutView()
}
}
Loading

0 comments on commit ed1f3d3

Please sign in to comment.