Skip to content

Commit

Permalink
fix: forum changes
Browse files Browse the repository at this point in the history
fix: remove mentionsender
  • Loading branch information
tealbathingsuit committed Sep 15, 2022
1 parent cb92fd8 commit ac4037a
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 130 deletions.
12 changes: 4 additions & 8 deletions Accord.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
9EEC6D6D2890429E0005DF30 /* MessageUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEC6D6C2890429E0005DF30 /* MessageUpdate.swift */; };
9EF5FFDA285CE0FE001B68FA /* ForumChannelList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EF5FFD9285CE0FE001B68FA /* ForumChannelList.swift */; };
9EF5FFDC285E010C001B68FA /* Globals.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EF5FFDB285E010C001B68FA /* Globals.swift */; };
9EFACB9828D3DB82005AA690 /* AppModel++.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EFACB9728D3DB82005AA690 /* AppModel++.swift */; };
A80FEC2327234DE7005F9950 /* ChannelViewViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A80FEC2227234DE7005F9950 /* ChannelViewViewModel.swift */; };
A80FEC2B2724FD35005F9950 /* ImageProcessing.swift in Sources */ = {isa = PBXBuildFile; fileRef = A80FEC262724FD35005F9950 /* ImageProcessing.swift */; };
A80FEC2C2724FD35005F9950 /* Operators++.swift in Sources */ = {isa = PBXBuildFile; fileRef = A80FEC272724FD35005F9950 /* Operators++.swift */; };
Expand All @@ -115,8 +116,6 @@
A869085D269DDCE500F13CAD /* PopoverProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A869085C269DDCE400F13CAD /* PopoverProfileView.swift */; };
A869085F269F2A0A00F13CAD /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A869085E269F2A0A00F13CAD /* SettingsView.swift */; };
A88A3D15265B22C4003AE08F /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A88A3D14265B22C4003AE08F /* LoginView.swift */; };
A88CD70F26E065F3007D979B /* MentionProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = A88CD70E26E065F3007D979B /* MentionProtocol.swift */; };
A88CD71126E066A0007D979B /* ServerListView+Mentions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A88CD71026E066A0007D979B /* ServerListView+Mentions.swift */; };
A8922501266ED88B00FEA21C /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8922500266ED88B00FEA21C /* Extensions.swift */; };
A898453E26978EBA00B1C9DB /* EmotesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A898453D26978EBA00B1C9DB /* EmotesView.swift */; };
A89845422698C3FD00B1C9DB /* GifManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A89845402698C3FD00B1C9DB /* GifManager.swift */; };
Expand Down Expand Up @@ -254,6 +253,7 @@
9EEC6D6C2890429E0005DF30 /* MessageUpdate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageUpdate.swift; sourceTree = "<group>"; };
9EF5FFD9285CE0FE001B68FA /* ForumChannelList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForumChannelList.swift; sourceTree = "<group>"; };
9EF5FFDB285E010C001B68FA /* Globals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Globals.swift; sourceTree = "<group>"; };
9EFACB9728D3DB82005AA690 /* AppModel++.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppModel++.swift"; sourceTree = "<group>"; };
A80FEC2227234DE7005F9950 /* ChannelViewViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelViewViewModel.swift; sourceTree = "<group>"; };
A80FEC262724FD35005F9950 /* ImageProcessing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageProcessing.swift; sourceTree = "<group>"; };
A80FEC272724FD35005F9950 /* Operators++.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Operators++.swift"; sourceTree = "<group>"; };
Expand All @@ -277,8 +277,6 @@
A869085C269DDCE400F13CAD /* PopoverProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = PopoverProfileView.swift; path = Accord/UI/Profile/PopoverProfileView.swift; sourceTree = SOURCE_ROOT; };
A869085E269F2A0A00F13CAD /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
A88A3D14265B22C4003AE08F /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
A88CD70E26E065F3007D979B /* MentionProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = MentionProtocol.swift; path = Accord/Backend/Networking/MentionProtocol.swift; sourceTree = SOURCE_ROOT; };
A88CD71026E066A0007D979B /* ServerListView+Mentions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ServerListView+Mentions.swift"; sourceTree = "<group>"; };
A8922500266ED88B00FEA21C /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Extensions.swift; path = Accord/Misc/PlusPlus/Extensions.swift; sourceTree = SOURCE_ROOT; };
A898453D26978EBA00B1C9DB /* EmotesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmotesView.swift; sourceTree = "<group>"; };
A89845402698C3FD00B1C9DB /* GifManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GifManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -544,6 +542,7 @@
9E6BCC61277E8E570085F279 /* LoadingScreenView.swift */,
9E2C98762780CD1A008C6AFD /* AccordAlert.swift */,
9EF5FFDB285E010C001B68FA /* Globals.swift */,
9EFACB9728D3DB82005AA690 /* AppModel++.swift */,
);
path = App;
sourceTree = "<group>";
Expand Down Expand Up @@ -638,7 +637,6 @@
isa = PBXGroup;
children = (
A8CD4CA4267D27F300E2C4F1 /* ServerListView.swift */,
A88CD71026E066A0007D979B /* ServerListView+Mentions.swift */,
A832557726EEEC1D00F18A55 /* ServerListView+Misc.swift */,
9E82874E27BB522600953A7B /* ServerListView+Init.swift */,
9EA3272928810F5100F55DF0 /* ServerListViewModel.swift */,
Expand Down Expand Up @@ -775,7 +773,6 @@
isa = PBXGroup;
children = (
9E7E4D31279BA105008FAF15 /* Core.swift */,
A88CD70E26E065F3007D979B /* MentionProtocol.swift */,
32AA719025EAEEDA00550BA5 /* Request.swift */,
A8C73D1126F5812D00CF0350 /* Songlink.swift */,
);
Expand Down Expand Up @@ -947,6 +944,7 @@
9EB3303828344929007D38EF /* PrivateChannelsView.swift in Sources */,
9EA3272A28810F5100F55DF0 /* ServerListViewModel.swift in Sources */,
9EC5FA192845B9EA00C2A416 /* InteractionView.swift in Sources */,
9EFACB9828D3DB82005AA690 /* AppModel++.swift in Sources */,
9E7834D22781F2D500ABB9F8 /* Combine++.swift in Sources */,
A8C73D1226F5812D00CF0350 /* Songlink.swift in Sources */,
9E7BC56D27472317008E3FAB /* Markdown.swift in Sources */,
Expand Down Expand Up @@ -996,7 +994,6 @@
9EB3304128372079007D38EF /* NewInviteSheet.swift in Sources */,
9E6B710A2784EBEC00A26CB6 /* GatewayEvent.swift in Sources */,
3207A24425867586006C2665 /* Constants.swift in Sources */,
A88CD71126E066A0007D979B /* ServerListView+Mentions.swift in Sources */,
A82A061E26D97D5C007B9B27 /* GatewayStructure.swift in Sources */,
9E96EB7A27DEC33D00BB3C8A /* Gateway+VoiceState.swift in Sources */,
9EC5FA1B2845BACC00C2A416 /* AvatarView.swift in Sources */,
Expand All @@ -1013,7 +1010,6 @@
9EDD136627D5A55A000685E6 /* String++.swift in Sources */,
9E13BEE428078F5D001DF4F2 /* ZStream.swift in Sources */,
A80FEC2327234DE7005F9950 /* ChannelViewViewModel.swift in Sources */,
A88CD70F26E065F3007D979B /* MentionProtocol.swift in Sources */,
A864BE612686700300B244BE /* MainView.swift in Sources */,
9EC5FA252846B7ED00C2A416 /* MessageCellMenu.swift in Sources */,
A82A062026D98193007B9B27 /* Settings.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Accord/App/AccordApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class NSWorkspace2: NSWorkspace {
guard comp.count == 3 else {
return
}
MentionSender.shared.select(channel: Channel(
Storage.globals?.select(channel: Channel(
id: comp[1],
type: .normal,
guild_id: comp[0],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
//
// ServerListView+Mentions.swift
// ServerListView+Mentions
// self++.swift
// Accord
//
// Created by evelyn on 2021-09-01.
// Created by evelyn on 2022-09-15.
//

import Foundation
import UserNotifications

extension ServerListView: MentionSenderDelegate {
extension AppGlobals {
func addMention(guild: String, channel: String) {
if guild == "@me" {
guard channel != String(selection ?? 0) else { print("currently reading already"); return }
guard let index = appModel.privateChannels.generateKeyMap()[channel] else { return }
guard channel != String(serverListViewSelection ?? 0) else { print("currently reading already"); return }
guard let index = self.privateChannels.generateKeyMap()[channel] else { return }
DispatchQueue.main.async {
appModel.privateChannels[index].read_state?.mention_count? += 1
self.privateChannels[index].read_state?.mention_count? += 1
}
}
guard channel != String(selection ?? 0) else { print("currently reading already"); return }
let index = appModel.folders.map { ServerListView.fastIndexGuild(guild, array: $0.guilds) }
guard channel != String(serverListViewSelection ?? 0) else { print("currently reading already"); return }
let index = self.folders.map { ServerListView.fastIndexGuild(guild, array: $0.guilds) }
for (i, v) in index.enumerated() {
guard let v = v else { continue }
var folderList = appModel.folders[i].guilds[v].channels
folderList.append(contentsOf: appModel.privateChannels)
if let index = fastIndexChannels(channel, array: folderList) {
DispatchQueue.main.async {
appModel.folders[i].guilds[v].channels[index].read_state?.mention_count? += 1
}
}
var folderList = self.folders[i].guilds[v].channels
folderList.append(contentsOf: self.privateChannels)
// if let index = fastIndexChannels(channel, array: folderList) {
// DispatchQueue.main.async {
// self.folders[i].guilds[v].channels[index].read_state?.mention_count? += 1
// }
// }
}
DispatchQueue.main.async {
self.appModel.objectWillChange.send()
self.self.objectWillChange.send()
}
}

func deselect() {
selection = nil
serverListViewSelection = nil
}

func removeMentions(server: String) {
let index = appModel.folders.map { $0.guilds[indexOf: server] }
let index = self.folders.map { $0.guilds[indexOf: server] }
for (index1, index2) in index.enumerated().filter({ $0.element != nil }) {
DispatchQueue.main.async {
appModel.folders[index1].guilds[index2!].channels.forEach { $0.read_state?.mention_count = 0 }
self.folders[index1].guilds[index2!].channels.forEach { $0.read_state?.mention_count = 0 }
}
}
}
Expand All @@ -55,11 +54,11 @@ extension ServerListView: MentionSenderDelegate {
let messageID = message.id
let isDM = message.guildID == nil
let guildID = message.guildID ?? "@me"
newMessageProcessThread.async {
DispatchQueue.global().async {
let ids = message.mentions.map(\.id)
if ids.contains(user_id) || (appModel.privateChannels.map(\.id).contains(channelID) && message.author?.id != user_id) {
let matchingGuild = Array(appModel.folders.map(\.guilds).joined())[keyed: message.guildID ?? ""]
let matchingChannel = matchingGuild?.channels[keyed: message.channelID] ?? appModel.privateChannels[keyed: message.channelID]
if ids.contains(user_id) || (self.privateChannels.map(\.id).contains(channelID) && message.author?.id != user_id) {
let matchingGuild = Array(self.folders.map(\.guilds).joined())[keyed: message.guildID ?? ""]
let matchingChannel = matchingGuild?.channels[keyed: message.channelID] ?? self.privateChannels[keyed: message.channelID]
showNotification(
title: message.author?.username ?? "Unknown User",
subtitle: matchingGuild == nil ? matchingChannel?.computedName ?? "Direct Messages" : "#\(matchingChannel?.computedName ?? "")\(matchingGuild?.name ?? "")",
Expand All @@ -70,18 +69,18 @@ extension ServerListView: MentionSenderDelegate {
self.addMention(guild: guildID, channel: channelID)
}
if isDM {
guard let index = appModel.privateChannels.generateKeyMap()[channelID] else { return }
guard let index = self.privateChannels.generateKeyMap()[channelID] else { return }
DispatchQueue.main.async {
self.appModel.privateChannels[index].last_message_id = messageID
self.self.privateChannels[index].last_message_id = messageID
}
} else {
guard channelID != String(self.selection ?? 0) else { print("currently reading already"); return }
appModel.folders.enumerated().forEach { index1, folder in
guard channelID != String(self.serverListViewSelection ?? 0) else { print("currently reading already"); return }
self.folders.enumerated().forEach { index1, folder in
folder.guilds.enumerated().forEach { index2, guild in
guild.channels.enumerated().forEach { index3, channel in
if channel.id == channelID {
DispatchQueue.main.async {
appModel.folders[index1].guilds[index2].channels[index3].last_message_id = messageID
self.folders[index1].guilds[index2].channels[index3].last_message_id = messageID
}
}
}
Expand All @@ -91,5 +90,3 @@ extension ServerListView: MentionSenderDelegate {
}
}
}

let newMessageProcessThread = DispatchQueue(label: "NewMessageProcessor")
3 changes: 3 additions & 0 deletions Accord/App/Globals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ final class AppGlobals: ObservableObject {
@MainActor @Published
public var privateChannels = [Channel]()

@MainActor @Published
var serverListViewSelection: Int? = nil

static var newItemPublisher = PassthroughSubject<(Channel?, GuildFolder?), Never>()

func permissionsAllowed(_ perms: [Channel.PermissionOverwrites], guildID: String) -> Permissions {
Expand Down
2 changes: 1 addition & 1 deletion Accord/Backend/Gateway/Gateway+MessageHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extension Gateway {
}
guard message.author?.id != user_id else { return }
guard let channelID = event.channelID else { print("wat"); break }
MentionSender.shared.newMessage(in: channelID, message: message)
Storage.globals?.newMessage(in: channelID, message: message)
case .messageUpdate:
if let channelID = event.channelID {
editSubject.send((event.data, channelID))
Expand Down
13 changes: 8 additions & 5 deletions Accord/Backend/Markdown/AsyncMarkdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class AsyncMarkdownModel: ObservableObject {
static let queue = DispatchQueue(label: "textQueue", attributes: .concurrent)

@_optimize(speed)
func make(text: String, usernames: [String:String]) {
func make(text: String, usernames: [String:String], allowLinkShortening: Bool) {
Self.queue.async { [weak self] in
let emojis = text.hasEmojisOnly
guard (text.contains("*") ||
Expand All @@ -46,7 +46,7 @@ final class AsyncMarkdownModel: ObservableObject {
}
return
}
self?.cancellable = Markdown.markAll(text: text, usernames, font: emojis)
self?.cancellable = Markdown.markAll(text: text, usernames, font: emojis, allowLinkShortening: allowLinkShortening)
.replaceError(with: Text(text))
.sink { [weak self] text in
DispatchQueue.main.async {
Expand All @@ -71,11 +71,14 @@ struct AsyncMarkdown: View, Equatable {

@EnvironmentObject
var appModel: AppGlobals

var linkShortening: Bool

@MainActor
init(_ text: String, binded: Binding<String>? = nil) {
init(_ text: String, binded: Binding<String>? = nil, linkShortening: Bool = false) {
_model = StateObject(wrappedValue: AsyncMarkdownModel(text: text))
_text = binded ?? .constant(text)
self.linkShortening = linkShortening
}

@ViewBuilder
Expand All @@ -86,10 +89,10 @@ struct AsyncMarkdown: View, Equatable {
.font(self.model.hasEmojiOnly ? .system(size: 48, design: .rounded) : .chatTextFont)
.fixedSize(horizontal: false, vertical: true)
.onChange(of: self.text, perform: { [weak model] text in
model?.make(text: text, usernames: Storage.usernames)
model?.make(text: text, usernames: Storage.usernames, allowLinkShortening: linkShortening)
})
.onAppear { [weak model] in
model?.make(text: text, usernames: Storage.usernames)
model?.make(text: text, usernames: Storage.usernames, allowLinkShortening: linkShortening)
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions Accord/Backend/Markdown/Markdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ public final class Markdown {
- Returns AnyPublisher with array of SwiftUI Text views
**/
@_optimize(speed)
public class func markLine(_ line: String, _ members: [String: String] = [:], font: Bool, highlight: Bool) -> TextArrayPublisher {
let line = line.replacingOccurrences(of: "](", with: "]\(blankCharacter)(") // disable link shortening forcefully
public class func markLine(_ line: String, _ members: [String: String] = [:], font: Bool, highlight: Bool, allowLinkShortening: Bool) -> TextArrayPublisher {
var line = line
if !allowLinkShortening {
print("disabling link shortening")
line = line.replacingOccurrences(of: "](", with: "]\(blankCharacter)(") // disable link shortening forcefully
}
let words = line.matchRange(precomputed: RegexExpressions.line).map { line[$0].trimmingCharacters(in: .whitespaces) }
let pubs: [AnyPublisher<Text, Error>] = words.map { markWord($0, members, font: font, highlight: highlight, quote: line.first == $0.first) }
return Publishers.MergeMany(pubs)
Expand All @@ -204,13 +208,13 @@ public final class Markdown {
}

/**
markLine: Simple Publisher that combines an array of word and line publishers for a text section
markAll: Simple Publisher that combines an array of word and line publishers for a text section
- Parameter text: The text being processed
- Parameter members: Dictionary of channel members from which we get the mentions
- Returns AnyPublisher with SwiftUI Text view
**/
@_optimize(speed)
public class func markAll(text: String, _ members: [String: String] = [:], font: Bool = false) -> TextPublisher {
public class func markAll(text: String, _ members: [String: String] = [:], font: Bool = false, allowLinkShortening: Bool = false) -> TextPublisher {
let newlines = text.split(whereSeparator: \.isNewline)

let codeBlockMarkerRawOffsets = newlines
Expand All @@ -229,7 +233,7 @@ public final class Markdown {
}
.compactMap(\.self)

let pubs = newlines.map { markLine(String($0), members, font: font, highlight: (text.count > 100) && highlighting) }
let pubs = newlines.map { markLine(String($0), members, font: font, highlight: (text.count > 100) && highlighting, allowLinkShortening: allowLinkShortening) }
var strippedPublishers = pubs
.map { [$0] }
.joined()
Expand Down
Loading

0 comments on commit ac4037a

Please sign in to comment.