Skip to content

Commit

Permalink
fix: unclog main thread with markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
tealbathingsuit committed May 20, 2022
1 parent a2830f1 commit 2c5ebfa
Show file tree
Hide file tree
Showing 19 changed files with 375 additions and 209 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store

Accord/Backend/Highlighting/*
**/*.xcworkspace/**/xcuserdata/
**/*.xcodeproj/**/xcuserdata/

Expand Down
16 changes: 16 additions & 0 deletions Accord.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
9EA295212753D1490014927D /* MemberList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA295202753D1480014927D /* MemberList.swift */; };
9EB3303828344929007D38EF /* PrivateChannelsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB3303728344929007D38EF /* PrivateChannelsView.swift */; };
9EB3303A28344A10007D38EF /* DMButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB3303928344A10007D38EF /* DMButton.swift */; };
9EB3303C2835B295007D38EF /* PermissionOverwrites.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB3303B2835B295007D38EF /* PermissionOverwrites.swift */; };
9EB3303F2837181E007D38EF /* Highlighting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB3303E2837181E007D38EF /* Highlighting.swift */; };
9EB3582427BFFD3E0046A136 /* Collection++.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EB3582327BFFD3E0046A136 /* Collection++.swift */; };
9ECB0F3F2766D3DA0019ADAE /* MessageCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9ECB0F3E2766D3DA0019ADAE /* MessageCellView.swift */; };
9ECE08F5277286E3001BA0F7 /* PinsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9ECE08F4277286E3001BA0F7 /* PinsView.swift */; };
Expand Down Expand Up @@ -175,6 +177,8 @@
9EA295202753D1480014927D /* MemberList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemberList.swift; sourceTree = "<group>"; };
9EB3303728344929007D38EF /* PrivateChannelsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivateChannelsView.swift; sourceTree = "<group>"; };
9EB3303928344A10007D38EF /* DMButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DMButton.swift; sourceTree = "<group>"; };
9EB3303B2835B295007D38EF /* PermissionOverwrites.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PermissionOverwrites.swift; sourceTree = "<group>"; };
9EB3303E2837181E007D38EF /* Highlighting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Highlighting.swift; sourceTree = "<group>"; };
9EB3582327BFFD3E0046A136 /* Collection++.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Collection++.swift"; sourceTree = "<group>"; };
9ECB0F3E2766D3DA0019ADAE /* MessageCellView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageCellView.swift; sourceTree = "<group>"; };
9ECE08F4277286E3001BA0F7 /* PinsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PinsView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -247,6 +251,7 @@
32AA719225EAEEF700550BA5 /* Backend */ = {
isa = PBXGroup;
children = (
9EB3303D2837180C007D38EF /* Highlighting */,
9EDDB1B42831E50500510036 /* Reachability */,
9E5145A42823513D0070828B /* Media */,
9E96EB7827DEC32B00BB3C8A /* Voice */,
Expand Down Expand Up @@ -377,6 +382,7 @@
A8B1F29526CB163B00E8FDC5 /* User.swift */,
9E6BCC55277E23E80085F279 /* Reaction.swift */,
9E82875027BD6C5400953A7B /* Permissions.swift */,
9EB3303B2835B295007D38EF /* PermissionOverwrites.swift */,
);
path = DiscordTypes;
sourceTree = "<group>";
Expand Down Expand Up @@ -483,6 +489,14 @@
path = Voice;
sourceTree = "<group>";
};
9EB3303D2837180C007D38EF /* Highlighting */ = {
isa = PBXGroup;
children = (
9EB3303E2837181E007D38EF /* Highlighting.swift */,
);
path = Highlighting;
sourceTree = "<group>";
};
9ECE42B9279E131B00883D0A /* TextField */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -706,6 +720,7 @@
9E82874D27BB515F00953A7B /* FolderListView.swift in Sources */,
A8B1F2A426CB176900E8FDC5 /* Channel.swift in Sources */,
9E82874F27BB522600953A7B /* ServerListView+Init.swift in Sources */,
9EB3303F2837181E007D38EF /* Highlighting.swift in Sources */,
A85D3FDE2677A681002327AD /* Images.swift in Sources */,
9ECB0F3F2766D3DA0019ADAE /* MessageCellView.swift in Sources */,
9E6BCC5D277E43E10085F279 /* SearchView.swift in Sources */,
Expand All @@ -717,6 +732,7 @@
A8BC5254268DF5AD00B7823E /* AttachmentView.swift in Sources */,
9ED3011127A6C76300D6235D /* Keypath++.swift in Sources */,
9E7E4D28279B63E8008FAF15 /* Models.swift in Sources */,
9EB3303C2835B295007D38EF /* PermissionOverwrites.swift in Sources */,
9ED3011327A8C40600D6235D /* RegisterImages.swift in Sources */,
A82A062426D98259007B9B27 /* ReadState.swift in Sources */,
A8B1F29826CB166C00E8FDC5 /* Events.swift in Sources */,
Expand Down
16 changes: 15 additions & 1 deletion Accord/App/AccordApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import Foundation
import SwiftUI
import UserNotifications

var allowReconnection: Bool = false
var reachability: Reachability? = {
var reachability = try? Reachability()
reachability?.whenReachable = { status in
concurrentQueue.async {
if wss?.connection?.state != .preparing {
if wss?.connection?.state != .preparing && allowReconnection {
wss?.reset()
}
}
Expand All @@ -23,6 +24,9 @@ var reachability: Reachability? = {
print($0, "unreachable")
}
try? reachability?.startNotifier()
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
allowReconnection = true
}
return reachability
}()

Expand Down Expand Up @@ -179,6 +183,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
self, selector: #selector(loadWindowRect(_:)),
name: NSWindow.didBecomeKeyNotification, object: nil
)
NotificationCenter.default.addObserver(
self, selector: #selector(onWake(_:)),
name: NSWorkspace.didWakeNotification, object: nil
)
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "_MRPlayerPlaybackQueueContentItemsChangedNotification"), object: nil, queue: nil) { _ in
print("Song Changed")
DispatchQueue.global().asyncAfter(deadline: .now() + 2) {
Expand Down Expand Up @@ -219,6 +227,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
}
}

@objc func onWake(_:AnyObject?) {
concurrentQueue.async {
wss?.reset()
}
}

@objc func closePopover(_ sender: AnyObject?) {
popover.performClose(sender)
Expand Down
6 changes: 5 additions & 1 deletion Accord/Backend/Gateway/Gateway.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,12 @@ final class Gateway {
url: URL = Gateway.gatewayURL,
session_id: String? = nil,
seq: Int? = nil,
compress: Bool = true
compress: Bool = true,
decompressor: ZStream? = nil
) throws {
if let decompressor = decompressor {
self.decompressor = decompressor
}
if compress {
socketEndpoint = NWEndpoint.url(URL(string: "wss://gateway.discord.gg?v=9&encoding=json&compress=zlib-stream")!)
print("Connecting with stream compression enabled")
Expand Down
3 changes: 2 additions & 1 deletion Accord/Backend/Gateway/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ extension Gateway {
url: Gateway.gatewayURL,
session_id: wss.sessionID,
seq: wss.seq,
compress: UserDefaults.standard.value(forKey: "CompressGateway") as? Bool ?? true
compress: UserDefaults.standard.value(forKey: "CompressGateway") as? Bool ?? true,
decompressor: wss.decompressor
) else { return }
wss = new
}
Expand Down
11 changes: 8 additions & 3 deletions Accord/Backend/Markdown/AsyncMarkdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ final class AsyncMarkdownModel: ObservableObject {
@Published var hasEmojiOnly: Bool = false
@Published var loaded: Bool = false

private var cancellable: AnyCancellable?

func make(text: String) {
DispatchQueue.global().async { [weak self] in
guard let self = self else { return }
let emojis = text.hasEmojisOnly
Markdown.markAll(text: text, Storage.usernames, font: emojis)
self.cancellable = Markdown.markAll(text: text, Storage.usernames, font: emojis)
.replaceError(with: Text(text))
.receive(on: RunLoop.main)
.assign(to: &self.$markdown)
.sink { text in
DispatchQueue.main.async {
self.markdown = text
}
}
DispatchQueue.main.async {
self.loaded = true
self.hasEmojiOnly = text.hasEmojisOnly
Expand Down
3 changes: 1 addition & 2 deletions Accord/Backend/Markdown/Markdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public final class Markdown {
if word.contains("+") || word.contains("<") || word.contains(">") { // the markdown parser removes these??
return promise(.success(Text(word) + Text(" ")))
}

return promise(.success(appleMarkdown(word)))
}
.debugWarnNoMainThread()
.eraseToAnyPublisher()
}

Expand Down Expand Up @@ -162,7 +162,6 @@ public final class Markdown {
.collect()
.map { $0.reduce(Text(""), +) }
.eraseToAnyPublisher()
.debugWarnNoMainThread()
}
}

Expand Down
55 changes: 0 additions & 55 deletions Accord/Objects/DiscordTypes/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,58 +91,3 @@ enum ChannelType: Int, Codable {
case unknown4 = 14
case unknown5 = 15
}

extension Array where Self.Element == Channel.PermissionOverwrites {
func hasPermission(guildID: String, perms: Permissions) -> Bool {
var allowed = true
for overwrite in self {
if (overwrite.id == user_id ||
ServerListView.mergedMembers[guildID]?.roles.contains(overwrite.id) ?? false) &&
overwrite.allow.contains(perms) {
return true
}
if (overwrite.id == user_id ||
// for the role permissions
ServerListView.mergedMembers[guildID]?.roles.contains(overwrite.id) ?? false ||
// for the everyone permissions
overwrite.id == guildID) &&
overwrite.deny.contains(perms) {
allowed = false
}
}
return allowed
}

func allAllowed(guildID: String) -> Permissions {
var permsArray = ServerListView.mergedMembers[guildID]?.cachedPermissions ??
Permissions (
ServerListView.folders.lazy
.map { $0.guilds }
.joined()
.filter { $0.id == guildID }
.first?.roles?.lazy
.filter { ServerListView.mergedMembers[guildID]?.roles.contains($0.id) == true }
.compactMap { $0.permissions }
.compactMap { Int64($0) }
.map { Permissions($0) } ?? [Permissions]()
)

defer { ServerListView.mergedMembers[guildID]?.cachedPermissions = permsArray }

if permsArray.contains(.administrator) {
permsArray = Permissions(rawValue: 2199023255551)
return permsArray
}

let everyonePerms = self.filter { $0.id == guildID }
permsArray.remove(Permissions(everyonePerms.map(\.deny)))
permsArray.insert(Permissions(everyonePerms.map(\.allow)))
let rolePerms = self.filter { ServerListView.mergedMembers[guildID]?.roles.contains($0.id) ?? false }
permsArray.remove(Permissions(rolePerms.map(\.deny)))
permsArray.insert(Permissions(rolePerms.map(\.allow)))
let memberPerms = self.filter { $0.id == AccordCoreVars.user?.id }
permsArray.remove(Permissions(memberPerms.map(\.deny)))
permsArray.insert(Permissions(memberPerms.map(\.allow)))
return permsArray
}
}
2 changes: 1 addition & 1 deletion Accord/Objects/DiscordTypes/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

final class Message: Decodable, Equatable, Identifiable, Hashable {
static func == (lhs: Message, rhs: Message) -> Bool {
lhs.id == rhs.id && lhs.content == rhs.content
lhs.id == rhs.id && lhs.content == rhs.content && lhs.embeds == rhs.embeds
}

var author: User?
Expand Down
66 changes: 66 additions & 0 deletions Accord/Objects/DiscordTypes/PermissionOverwrites.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// Channel+Permissions.swift
// Accord
//
// Created by evelyn on 2022-05-18.
//

import Foundation
import CloudKit

extension Array where Self.Element == Channel.PermissionOverwrites {
func hasPermission(guildID: String, perms: Permissions) -> Bool {
var allowed = true
for overwrite in self {
if (overwrite.id == user_id ||
ServerListView.mergedMembers[guildID]?.roles.contains(overwrite.id) ?? false) &&
overwrite.allow.contains(perms) {
return true
}
if (overwrite.id == user_id ||
// for the role permissions
ServerListView.mergedMembers[guildID]?.roles.contains(overwrite.id) ?? false ||
// for the everyone permissions
overwrite.id == guildID) &&
overwrite.deny.contains(perms) {
allowed = false
}
}
return allowed
}

func allAllowed(guildID: String) -> Permissions {
var permsArray = ServerListView.mergedMembers[guildID]?.cachedPermissions ?? Permissions (
ServerListView.folders.lazy
.map { $0.guilds }
.joined()
.filter { $0.id == guildID }
.first?.roles?.lazy
.filter { ServerListView.mergedMembers[guildID]?.roles.contains($0.id) == true }
.compactMap { $0.permissions }
.compactMap { Int64($0) }
.map { Permissions($0) } ?? [Permissions]()
)

ServerListView.mergedMembers[guildID]?.cachedPermissions = permsArray

if permsArray.contains(.administrator) {
permsArray = Permissions(rawValue: 2199023255551)
return permsArray
}

let everyonePerms = self.filter { $0.id == guildID }
permsArray.insert(.init([
.sendMessages, .readMessages
]))
permsArray.remove(Permissions(everyonePerms.map(\.deny)))
permsArray.insert(Permissions(everyonePerms.map(\.allow)))
let rolePerms = self.filter { ServerListView.mergedMembers[guildID]?.roles.contains($0.id) ?? false }
permsArray.remove(Permissions(rolePerms.map(\.deny)))
permsArray.insert(Permissions(rolePerms.map(\.allow)))
let memberPerms = self.filter { $0.id == AccordCoreVars.user?.id }
permsArray.remove(Permissions(memberPerms.map(\.deny)))
permsArray.insert(Permissions(memberPerms.map(\.allow)))
return permsArray
}
}
4 changes: 4 additions & 0 deletions Accord/Objects/DiscordTypes/Permissions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ struct Permissions: Decodable, OptionSet {

public let rawValue: Int64

var moderator: Bool {
self.contains(.moderateMembers) || self.contains(.kickMembers) || self.contains(.banMembers)
}

static var createInstantInvite = Permissions(rawValue: 1 << 0)
static var kickMembers = Permissions(rawValue: 1 << 1)
static var banMembers = Permissions(rawValue: 1 << 2)
Expand Down
3 changes: 2 additions & 1 deletion Accord/Objects/DiscordTypes/Reaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import Foundation

final class Reaction: Codable {
final class Reaction: Identifiable, Codable {
var count: Int
var me: Bool
var emoji: ReactionEmote
var id: String { identifier }
var identifier: String { self.emoji.id ?? emoji.name ?? "some emoji" }
}

Expand Down
2 changes: 1 addition & 1 deletion Accord/UI/Chat/Accessories/MentionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct MentionsView: View {
replyNick: nil,
pronouns: nil,
avatar: nil,
guildID: nil,
guildID: "",
permissions: .init(),
role: Binding.constant(nil),
replyRole: Binding.constant(nil),
Expand Down
2 changes: 1 addition & 1 deletion Accord/UI/Chat/Accessories/PinsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct PinsView: View {
replyNick: nil,
pronouns: nil,
avatar: nil,
guildID: nil,
guildID: "",
permissions: .init(),
role: Binding.constant(nil),
replyRole: Binding.constant(nil),
Expand Down
Loading

0 comments on commit 2c5ebfa

Please sign in to comment.