Skip to content

Commit 03050cd

Browse files
fix: embed refreshing
fix: logging out unexpectedly fix: embed sizing
1 parent 89a2fce commit 03050cd

File tree

12 files changed

+97
-41
lines changed

12 files changed

+97
-41
lines changed

Accord.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
9EDD136627D5A55A000685E6 /* String++.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EDD136527D5A55A000685E6 /* String++.swift */; };
8484
9EDD136827D6F951000685E6 /* Regex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EDD136727D6F951000685E6 /* Regex.swift */; };
8585
9EDDB1B32831E4FC00510036 /* Reachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EDDB1B22831E4FC00510036 /* Reachability.swift */; };
86+
9EEC6D6B288F684B0005DF30 /* MessageUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEC6D6A288F684B0005DF30 /* MessageUpdate.swift */; };
8687
9EF5FFDA285CE0FE001B68FA /* ForumChannelList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EF5FFD9285CE0FE001B68FA /* ForumChannelList.swift */; };
8788
9EF5FFDC285E010C001B68FA /* Globals.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EF5FFDB285E010C001B68FA /* Globals.swift */; };
8889
A80FEC2327234DE7005F9950 /* ChannelViewViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A80FEC2227234DE7005F9950 /* ChannelViewViewModel.swift */; };
@@ -238,6 +239,7 @@
238239
9EDD136527D5A55A000685E6 /* String++.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String++.swift"; sourceTree = "<group>"; };
239240
9EDD136727D6F951000685E6 /* Regex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Regex.swift; sourceTree = "<group>"; };
240241
9EDDB1B22831E4FC00510036 /* Reachability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reachability.swift; sourceTree = "<group>"; };
242+
9EEC6D6A288F684B0005DF30 /* MessageUpdate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageUpdate.swift; sourceTree = "<group>"; };
241243
9EF5FFD9285CE0FE001B68FA /* ForumChannelList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForumChannelList.swift; sourceTree = "<group>"; };
242244
9EF5FFDB285E010C001B68FA /* Globals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Globals.swift; sourceTree = "<group>"; };
243245
A80FEC2227234DE7005F9950 /* ChannelViewViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelViewViewModel.swift; sourceTree = "<group>"; };
@@ -422,6 +424,7 @@
422424
A8B1F29726CB166C00E8FDC5 /* Events.swift */,
423425
A82A061D26D97D5C007B9B27 /* GatewayStructure.swift */,
424426
9EA295202753D1480014927D /* MemberList.swift */,
427+
9EEC6D6A288F684B0005DF30 /* MessageUpdate.swift */,
425428
);
426429
path = GatewayPayloads;
427430
sourceTree = "<group>";
@@ -959,6 +962,7 @@
959962
A82A061E26D97D5C007B9B27 /* GatewayStructure.swift in Sources */,
960963
9E96EB7A27DEC33D00BB3C8A /* Gateway+VoiceState.swift in Sources */,
961964
9EC5FA1B2845BACC00C2A416 /* AvatarView.swift in Sources */,
965+
9EEC6D6B288F684B0005DF30 /* MessageUpdate.swift in Sources */,
962966
9E6BCC64277E8E850085F279 /* Print++.swift in Sources */,
963967
9E2C98772780CD1A008C6AFD /* AccordAlert.swift in Sources */,
964968
A80FEC2F2724FD35005F9950 /* UserNotifications.swift in Sources */,

Accord/Backend/Gateway/Gateway.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import Combine
1010
import Foundation
1111
import Network
1212

13+
var hasReset: Bool = false
14+
1315
// The second version of Accord's gateway.
1416
// This uses Network.framework instead of URLSessionWebSocketTask
1517
final class Gateway {
@@ -29,7 +31,7 @@ final class Gateway {
2931
private(set) var memberListSubject = PassthroughSubject<MemberListUpdate, Never>()
3032

3133
var presencePipeline = [String: PassthroughSubject<PresenceUpdate, Never>]()
32-
34+
3335
private(set) var stateUpdateHandler: (NWConnection.State) -> Void = { state in
3436
switch state {
3537
case .ready:
@@ -55,7 +57,7 @@ final class Gateway {
5557
return
5658
}
5759

58-
if message.contains("not authenticated") || message.contains("authentication failed") {
60+
if (message.contains("not authenticated") || message.contains("authentication failed")) && !hasReset {
5961
logOut()
6062
}
6163
}

Accord/Backend/Gateway/Socket.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ extension Gateway {
6363
}
6464

6565
func reset() {
66-
print("resetting from function")
66+
print("resetting")
6767

68+
hasReset = true
69+
6870
if let state = wss.connection?.state, case NWConnection.State.failed = state {
6971
close(.protocolCode(.protocolError))
7072
}
@@ -80,7 +82,9 @@ extension Gateway {
8082
}
8183

8284
func reset() async -> Bool {
83-
print("resetting from function")
85+
print("resetting")
86+
87+
hasReset = true
8488

8589
if let state = wss.connection?.state, case NWConnection.State.failed = state {
8690
close(.protocolCode(.protocolError))
@@ -100,7 +104,10 @@ extension Gateway {
100104
}
101105

102106
func hardReset() {
103-
print("hard resetting from function")
107+
print("hard resetting")
108+
109+
hasReset = true
110+
104111
close(.protocolCode(.normalClosure))
105112
concurrentQueue.async {
106113
guard let new = try? Gateway(

Accord/Objects/DiscordTypes/Message.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ struct Message: Codable, Equatable, Identifiable, Hashable {
8888

8989
func hash(into hasher: inout Hasher) {
9090
hasher.combine(id)
91+
hasher.combine(content)
92+
hasher.combine(embeds?.count ?? 0)
9193
}
9294

9395
func delete() {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// MessageUpdate.swift
3+
// Accord
4+
//
5+
// Created by evelyn on 2022-07-25.
6+
//
7+
8+
import Foundation
9+
10+
struct MessageUpdate: Codable {
11+
var id: String
12+
var channelID: String
13+
var embeds: [Embed]
14+
15+
enum CodingKeys: String, CodingKey {
16+
case id, embeds
17+
case channelID = "channel_id"
18+
}
19+
}

Accord/UI/Base/Guild/NewInviteSheet.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,30 @@ struct NewInviteSheet: View {
4848
Text("Create a new invite")
4949
.font(.title)
5050
.fontWeight(.bold)
51-
if let code = code {
51+
.padding(.bottom, 5)
52+
VStack(alignment: .leading) {
53+
Text("code".uppercased())
54+
.fontWeight(.semibold)
55+
.font(.system(size: 10))
56+
.foregroundColor(.secondary)
5257
HStack {
53-
HStack(spacing: 0) {
54-
Text("Invite Code: ")
55-
.fontWeight(.medium)
58+
if let code {
5659
Text("https://discord.gg/" + code)
5760
.fontWeight(.medium)
5861
.textSelection(.enabled)
62+
.padding(4)
63+
.background(Color(NSColor.alternatingContentBackgroundColors[1]))
64+
.cornerRadius(4)
65+
} else {
66+
Text(verbatim: "https://discord.gg/a1b2c3d4e5")
67+
.fontWeight(.medium)
68+
.blur(radius: 5)
5969
}
60-
.padding(5)
6170
Button("Copy") {
6271
NSPasteboard.general.clearContents()
6372
NSPasteboard.general.setString("https://discord.gg/" + (self.code ?? ""), forType: .string)
6473
}
74+
.disabled(code == nil)
6575
}
6676
}
6777
}

Accord/UI/Chat/Accessories/EmbedView.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension CGFloat {
1919
}
2020

2121
struct EmbedView: View, Equatable {
22-
weak var embed: Embed?
22+
@Binding var embed: Embed
2323

2424
static func == (_: EmbedView, _: EmbedView) -> Bool {
2525
true
@@ -29,7 +29,7 @@ struct EmbedView: View, Equatable {
2929

3030
var body: some View {
3131
HStack(spacing: 0) {
32-
if let color = embed?.color {
32+
if let color = embed.color {
3333
if #available(macOS 13.0, *) {
3434
RoundedRectangle(cornerRadius: 1)
3535
.fill(Color(int: color).gradient)
@@ -48,7 +48,7 @@ struct EmbedView: View, Equatable {
4848
.padding(.trailing, 5)
4949
}
5050
VStack(alignment: .leading) {
51-
if let author = embed?.author {
51+
if let author = embed.author {
5252
HStack {
5353
if let iconURL = author.proxy_icon_url ?? author.icon_url {
5454
Attachment(iconURL, size: CGSize(width: 48, height: 48))
@@ -75,42 +75,42 @@ struct EmbedView: View, Equatable {
7575
}
7676
.padding(.vertical, 2)
7777
}
78-
if let title = embed?.title {
78+
if let title = embed.title {
7979
Text(title)
8080
.fontWeight(.semibold)
8181
.font(.system(size: 13.5))
8282
.padding(.vertical, 2)
8383
}
84-
if let description = embed?.description {
84+
if let description = embed.description {
8585
AsyncMarkdown(description)
8686
.lineSpacing(3)
8787
.padding(.vertical, 2)
8888
}
89-
if let image = embed?.image {
89+
if let image = embed.image {
9090
Attachment(image.url, size: CGSize(width: image.width ?? 400, height: image.width ?? 300))
9191
.equatable()
9292
.cornerRadius(5)
9393
.maxFrame(width: 380, height: 300, originalWidth: image.width ?? 0, originalHeight: image.height ?? 0)
9494
.padding(.vertical, 2)
9595
}
96-
if let video = embed?.video,
96+
if let video = embed.video,
9797
let urlString = video.proxy_url ?? video.url,
9898
let url = URL(string: urlString)
9999
{
100100
VideoPlayer(player: AVPlayer(url: url))
101101
.cornerRadius(5)
102102
.maxFrame(width: 380, height: 300, originalWidth: video.width ?? 0, originalHeight: video.height ?? 0)
103103
.padding(.vertical, 2)
104-
} else if let image = embed?.thumbnail {
104+
} else if let image = embed.thumbnail {
105105
Attachment(image.url)
106106
.equatable()
107107
.scaledToFit()
108-
.frame(width: CGFloat(optional: image.width), height: CGFloat(optional: image.height))
108+
.frame(width: min(380, CGFloat(optional: image.width) ?? 100.0))
109109
.frame(idealWidth: Double(image.width ?? 100), maxWidth: 380, maxHeight: 300)
110110
.cornerRadius(5)
111111
.padding(.vertical, 2)
112112
}
113-
if let fields = embed?.fields {
113+
if let fields = embed.fields {
114114
GridStack(fields, rowAlignment: .leading, columns: 4) { field in
115115
VStack(alignment: .leading) {
116116
Text(field.name)

Accord/UI/Chat/Accessories/MentionsView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ struct MentionsView: View {
1717
@Environment(\.presentationMode) private var presentationMode
1818

1919
var body: some View {
20-
List(mentions, id: \.id) { message in
20+
List($mentions, id: \.id) { $message in
2121
ZStack(alignment: .topTrailing) {
2222
MessageCellView(
23-
message: message,
23+
message: $message,
2424
nick: nil,
2525
replyNick: nil,
2626
pronouns: nil,

Accord/UI/Chat/Accessories/PinsView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ struct PinsView: View {
1919
@Environment(\.presentationMode) private var presentationMode
2020

2121
var body: some View {
22-
List(pins, id: \.id) { message in
22+
List($pins, id: \.id) { $message in
2323
ZStack(alignment: .topTrailing) {
2424
MessageCellView(
25-
message: message,
25+
message: $message,
2626
nick: nil,
2727
replyNick: nil,
2828
pronouns: nil,

Accord/UI/Chat/ChannelView/ChannelView.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ struct ChannelView: View, Equatable {
108108
}
109109

110110
@_transparent
111-
func cell(for message: Message) -> some View {
112-
MessageCellView(
113-
message: message,
111+
func cell(for binding: Binding<Message>) -> some View {
112+
let message = binding.wrappedValue
113+
return MessageCellView(
114+
message: binding,
114115
nick: viewModel.nicks[message.author?.id ?? ""],
115116
replyNick: viewModel.nicks[message.referencedMessage?.author?.id ?? ""],
116117
pronouns: viewModel.pronouns[message.author?.id ?? ""],
@@ -143,9 +144,9 @@ struct ChannelView: View, Equatable {
143144
}
144145

145146
var messagesView: some View {
146-
ForEach(viewModel.messages, id: \.identifier) { message in
147+
ForEach($viewModel.messages, id: \.identifier) { $message in
147148
if message.inSameDay {
148-
cell(for: message)
149+
cell(for: $message)
149150
} else {
150151
VStack {
151152
HStack {
@@ -162,7 +163,7 @@ struct ChannelView: View, Equatable {
162163
.opacity(0.4)
163164
}
164165
.padding(.top)
165-
cell(for: message)
166+
cell(for: $message)
166167
}
167168
}
168169
}

0 commit comments

Comments
 (0)