-
Notifications
You must be signed in to change notification settings - Fork 0
/
Messages.swift
429 lines (423 loc) · 30.2 KB
/
Messages.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//
// Messages.swift
// wasteof.money
//
// Created by Oren Lindsey on 11/3/23.
//
import SwiftUI
import RichText
struct MessagesObject: Hashable, Codable {
var unread: Optional<[Message]>
var read: Optional<[Message]>
var last: Bool
enum CodingKeys: String, CodingKey {
case unread
case read
case last
}
/*init() {
self.last = true
self.unread = []
}
static func == (lhs: MessagesObject, rhs: MessagesObject) -> Bool {
return lhs.unread == rhs.unread && lhs.last == rhs.last
}
func hash(into hasher: inout Hasher) {
hasher.combine(unread)
hasher.combine(last)
}*/
}
class MessagesStateObject: ObservableObject {
@Published var last: Bool
@Published var unread: [Message]
init() {
self.last = true
self.unread = []
}
}
struct Message: Hashable, Codable, Identifiable {
let id: UUID = UUID()
var _id: String
var data: Optional<MessageData>
var read: Bool
var time: Int
var to: MessageReceiver
var type: String
var top: Optional<String>
enum CodingKeys: String, CodingKey {
case _id
case data
case read
case time
case to
case type
case top
}
}
struct MessageData: Hashable, Codable {
var actor: Optional<MessageActor>
var comment: Optional<MessagesCommentType>
var post: Optional<PostType>
var wall: Optional<WallUser>
var content: Optional<String>
enum CodingKeys: String, CodingKey {
case actor
case comment
case post
case wall
case content
}
}
struct MessagesCommentType: Hashable, Codable {
var _id: String
var post: Optional<String>
var poster: CommentPoster
var wall: Optional<WallUser>
var parent: Optional<String>
var content: String
var time: Int
var hasReplies: Bool
}
struct MessageActor: Hashable, Codable {
let name: String
let id: String
/*enum CodingKeys: String, CodingKey {
case name
case id
}*/
}
struct MessageReceiver: Hashable, Codable {
let name: String
let id: String
/*enum CodingKeys: String, CodingKey {
case name
case id
}*/
}
struct Messages: View {
@EnvironmentObject var session: Session
@StateObject var messagesState: MessagesStateObject
@State var page = 1
@State var read = false
var body: some View {
NavigationStack {
VStack {
List {
if messagesState.unread.count > 0 {
ForEach(messagesState.unread.indices, id: \.self) { i in
let profilecolor: Color = {
switch messagesState.unread[i].data?.comment?.poster.color {
case "red":
return Color.red
case "orange":
return Color.orange
case "yellow":
return Color.yellow
case "green":
return Color.green
case "teal":
return Color.teal
case "blue":
return Color.blue
case "indigo":
return Color.indigo
case "violet":
return Color.purple
case "fuchsia":
return Color.pink
case "pink":
return Color.pink
case "gray":
return Color.gray
case "rainbow":
return Color.gray
default:
return Color.green
}}()
VStack {
if messagesState.unread[i].type == "comment_reply" {
if messagesState.unread[i].data?.comment != nil && messagesState.unread[i].data?.post != nil {
HStack {
Text("@\( messagesState.unread[i].data?.actor?.name ?? "") replied to your comment").multilineTextAlignment(.leading)
Spacer()
}
NavigationLink {
Post(commentsState: CommentsObject(), _id: messagesState.unread[i].data?.post?._id ?? "", content: messagesState.unread[i].data?.post?.content ?? "", time: messagesState.unread[i].data?.post?.time ?? 0, comments: messagesState.unread[i].data?.post?.comments ?? 0, loves: messagesState.unread[i].data?.post?.loves ?? 0, reposts: messagesState.unread[i].data?.post?.reposts ?? 0, poster: messagesState.unread[i].data?.post?.poster ?? Poster(name: "", id: "", color: ""), revisions: messagesState.unread[i].data?.post?.revisions ?? [], edited: messagesState.unread[i].data?.post?.edited ?? 0, repost: messagesState.unread[i].data?.post?.repost ?? nil, pinType: true).environmentObject(session)
} label: {
CommentPreview(postId: messagesState.unread[i].data?.post?._id ?? "", _id: messagesState.unread[i].data?.comment?._id ?? "", poster: messagesState.unread[i].data?.comment?.poster ?? CommentPoster(name: "", id: "", color: nil), /*parent: messagesState.unread[i].data?.comment?.parent ?? "",*/ parentPoster: nil, content: messagesState.unread[i].data?.comment?.content ?? "", time: messagesState.unread[i].data?.comment?.time ?? 0, hasReplies: messagesState.unread[i].data?.comment?.hasReplies ?? false, profileColor: profilecolor, recursion: 3).environmentObject(session)
}
} else {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") replied to your comment, but the comment was deleted").multilineTextAlignment(.leading)
Spacer()
}
}
} else if messagesState.unread[i].type == "follow" {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") is now following you")
Spacer()
}
} else if messagesState.unread[i].type == "admin_notification" {
let time: Date = Date(timeIntervalSince1970: TimeInterval(messagesState.unread[i].time / 1000))
HStack {
Text("Admin message: \"\(messagesState.unread[i].data?.content ?? "")\" at \(time.formatted(date: .numeric, time: .shortened))").bold().font(.title3)
Spacer()
}
} else if messagesState.unread[i].type == "comment" {
if messagesState.unread[i].data?.comment != nil && messagesState.unread[i].data?.post != nil {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") commented on your post")
Spacer()
}
NavigationLink {
Post(commentsState: CommentsObject(), _id: messagesState.unread[i].data?.post?._id ?? "", content: messagesState.unread[i].data?.post?.content ?? "", time: messagesState.unread[i].data?.post?.time ?? 0, comments: messagesState.unread[i].data?.post?.comments ?? 0, loves: messagesState.unread[i].data?.post?.loves ?? 0, reposts: messagesState.unread[i].data?.post?.reposts ?? 0, poster: messagesState.unread[i].data?.post?.poster ?? Poster(name: "", id: "", color: ""), revisions: messagesState.unread[i].data?.post?.revisions ?? [], edited: messagesState.unread[i].data?.post?.edited ?? 0, repost: messagesState.unread[i].data?.post?.repost ?? nil, pinType: true).environmentObject(session)
} label: {
CommentPreview(postId: messagesState.unread[i].data?.post?._id ?? "", _id: messagesState.unread[i].data?.comment?._id ?? "", poster: messagesState.unread[i].data?.comment?.poster ?? CommentPoster(name: "", id: "", color: nil), /*parent: messagesState.unread[i].data?.comment?.parent ?? "",*/ parentPoster: nil, content: messagesState.unread[i].data?.comment?.content ?? "", time: messagesState.unread[i].data?.comment?.time ?? 0, hasReplies: messagesState.unread[i].data?.comment?.hasReplies ?? false, profileColor: profilecolor, recursion: 3).environmentObject(session)
}
} else {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") commented on your post, but either the comment or the post was deleted :(")
Spacer()
}
}
} else if messagesState.unread[i].type == "post_mention" {
if messagesState.unread[i].data?.post != nil {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") mentioned you in their post")
Spacer()
}
NavigationLink {
Post(commentsState: CommentsObject(), _id: messagesState.unread[i].data?.post?._id ?? "", content: messagesState.unread[i].data?.post?.content ?? "", time: messagesState.unread[i].data?.post?.time ?? 0, comments: messagesState.unread[i].data?.post?.comments ?? 0, loves: messagesState.unread[i].data?.post?.loves ?? 0, reposts: messagesState.unread[i].data?.post?.reposts ?? 0, poster: messagesState.unread[i].data?.post?.poster ?? Poster(name: "", id: "", color: ""), revisions: messagesState.unread[i].data?.post?.revisions ?? [], edited: messagesState.unread[i].data?.post?.edited ?? 0, repost: messagesState.unread[i].data?.post?.repost ?? nil, currentpage: 1, pinType: true).environmentObject(session)
} label: {
PostPreview(_id: messagesState.unread[i].data?.post?._id ?? "", content: messagesState.unread[i].data?.post?.content ?? "", time: messagesState.unread[i].data?.post?.time ?? 0, comments: messagesState.unread[i].data?.post?.comments ?? 0, loves: messagesState.unread[i].data?.post?.loves ?? 0, reposts: messagesState.unread[i].data?.post?.reposts ?? 0, poster: messagesState.unread[i].data?.post?.poster ?? Poster(name: "", id: "", color: ""), revisions: messagesState.unread[i].data?.post?.revisions ?? [], edited: messagesState.unread[i].data?.post?.edited ?? 0, repost: messagesState.unread[i].data?.post?.repost ?? nil, navigation: false,pinType: true, recursion: 0).environmentObject(session)
}
} else {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") mentioned you in a deleted post")
Spacer()
}
}
} else if messagesState.unread[i].type == "comment_mention" {
if messagesState.unread[i].data?.comment != nil && messagesState.unread[i].data?.post != nil {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") mentioned you in their comment")
Spacer()
}
NavigationLink {
Post(commentsState: CommentsObject(), _id: messagesState.unread[i].data?.post?._id ?? "", content: messagesState.unread[i].data?.post?.content ?? "", time: messagesState.unread[i].data?.post?.time ?? 0, comments: messagesState.unread[i].data?.post?.comments ?? 0, loves: messagesState.unread[i].data?.post?.loves ?? 0, reposts: messagesState.unread[i].data?.post?.reposts ?? 0, poster: messagesState.unread[i].data?.post?.poster ?? Poster(name: "", id: "", color: ""), revisions: messagesState.unread[i].data?.post?.revisions ?? [], edited: messagesState.unread[i].data?.post?.edited ?? 0, repost: messagesState.unread[i].data?.post?.repost ?? nil, pinType: true).environmentObject(session)
} label: {
CommentPreview(postId: messagesState.unread[i].data?.post?._id ?? "", _id: messagesState.unread[i].data?.comment?._id ?? "", poster: messagesState.unread[i].data?.comment?.poster ?? CommentPoster(name: "", id: "", color: nil), /*parent: messagesState.unread[i].data?.comment?.parent ?? "",*/ parentPoster: nil, content: messagesState.unread[i].data?.comment?.content ?? "", time: messagesState.unread[i].data?.comment?.time ?? 0, hasReplies: messagesState.unread[i].data?.comment?.hasReplies ?? false, profileColor: profilecolor, recursion: 3).environmentObject(session)
}
} else {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") mentioned you in their comment, but the comment was deleted :(")
Spacer()
}
}
} else if messagesState.unread[i].type == "repost" {
if messagesState.unread[i].data?.post != nil {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") reposted your post")
Spacer()
}
NavigationLink {
Post(commentsState: CommentsObject(), _id: messagesState.unread[i].data?.post?._id ?? "", content: messagesState.unread[i].data?.post?.content ?? "", time: messagesState.unread[i].data?.post?.time ?? 0, comments: messagesState.unread[i].data?.post?.comments ?? 0, loves: messagesState.unread[i].data?.post?.loves ?? 0, reposts: messagesState.unread[i].data?.post?.reposts ?? 0, poster: messagesState.unread[i].data?.post?.poster ?? Poster(name: "", id: "", color: ""), revisions: messagesState.unread[i].data?.post?.revisions ?? [], edited: messagesState.unread[i].data?.post?.edited ?? 0, repost: messagesState.unread[i].data?.post?.repost ?? nil, currentpage: 1, pinType: true).environmentObject(session)
} label: {
PostPreview(_id: messagesState.unread[i].data?.post?._id ?? "", content: messagesState.unread[i].data?.post?.content ?? "", time: messagesState.unread[i].data?.post?.time ?? 0, comments: messagesState.unread[i].data?.post?.comments ?? 0, loves: messagesState.unread[i].data?.post?.loves ?? 0, reposts: messagesState.unread[i].data?.post?.reposts ?? 0, poster: messagesState.unread[i].data?.post?.poster ?? Poster(name: "", id: "", color: ""), revisions: messagesState.unread[i].data?.post?.revisions ?? [], edited: messagesState.unread[i].data?.post?.edited ?? 0, repost: messagesState.unread[i].data?.post?.repost ?? nil, navigation: false, pinType: true, recursion: 0).environmentObject(session)
}
} else {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") reposted your post, but the post was deleted")
Spacer()
}
}
} else if messagesState.unread[i].type == "wall_comment" {
if messagesState.unread[i].data?.comment != nil {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") commented on your wall")
Spacer()
}
NavigationLink {
Wall(username: messagesState.unread[i].data?.wall?.name ?? "").environmentObject(session)
} label: {
WallCommentPreview(id: messagesState.unread[i].data?.comment?._id ?? "", parent: messagesState.unread[i].data?.comment?.parent ?? "", content: messagesState.unread[i].data?.comment?.content ?? "", wall: messagesState.unread[i].data?.wall ?? WallUser(name: "", id: ""), poster: messagesState.unread[i].data?.comment?.poster ?? CommentPoster(name: "", id: "", color: nil), time: messagesState.unread[i].data?.comment?.time ?? 0, hasReplies: messagesState.unread[i].data?.comment?.hasReplies ?? false, recursion: 1).environmentObject(session)
}
} else {
HStack {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") commented on your wall, but the comment was deleted :(")
Spacer()
}
}
} else if messagesState.unread[i].type == "wall_comment_mention" {
if messagesState.unread[i].data?.comment != nil {
HStack {
if messagesState.unread[i].data?.wall?.name == session.name {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") mentioned you on your wall")
} else {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") mentioned you on @\(messagesState.unread[i].data?.wall?.name ?? "")'s wall")
}
Spacer()
}
NavigationLink {
Wall(username: messagesState.unread[i].data?.wall?.name ?? "").environmentObject(session)
} label: {
WallCommentPreview(id: messagesState.unread[i].data?.comment?._id ?? "", parent: messagesState.unread[i].data?.comment?.parent ?? "", content: messagesState.unread[i].data?.comment?.content ?? "", wall: messagesState.unread[i].data?.wall ?? WallUser(name: "", id: ""), poster: messagesState.unread[i].data?.comment?.poster ?? CommentPoster(name: "", id: "", color: nil), time: messagesState.unread[i].data?.comment?.time ?? 0, hasReplies: messagesState.unread[i].data?.comment?.hasReplies ?? false, recursion: 1).environmentObject(session)
}
} else {
HStack {
if messagesState.unread[i].data?.wall?.name == session.name {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") mentioned you on your wall, but the comment was deleted :(")
} else {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") mentioned you on @\(messagesState.unread[i].data?.wall?.name ?? "")'s wall, but the comment was deleted :(")
}
Spacer()
}
}
} else if messagesState.unread[i].type == "wall_comment_reply" {
if messagesState.unread[i].data?.comment != nil {
HStack {
if messagesState.unread[i].data?.wall?.name == session.name {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") replied to your comment on your wall")
} else {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") replied to your comment on @\(messagesState.unread[i].data?.wall?.name ?? "")'s wall")
}
Spacer()
}
NavigationLink {
Wall(username: messagesState.unread[i].data?.wall?.name ?? "").environmentObject(session)
} label: {
WallCommentPreview(id: messagesState.unread[i].data?.comment?._id ?? "", parent: messagesState.unread[i].data?.comment?.parent ?? "", content: messagesState.unread[i].data?.comment?.content ?? "", wall: messagesState.unread[i].data?.wall ?? WallUser(name: "", id: ""), poster: messagesState.unread[i].data?.comment?.poster ?? CommentPoster(name: "", id: "", color: nil), time: messagesState.unread[i].data?.comment?.time ?? 0, hasReplies: messagesState.unread[i].data?.comment?.hasReplies ?? false, recursion: 1).environmentObject(session)
}
} else {
HStack {
if messagesState.unread[i].data?.wall?.name == session.name {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") replied to your comment on your wall, but the comment was deleted :(")
} else {
Text("@\(messagesState.unread[i].data?.actor?.name ?? "") replied to your comment on @\(messagesState.unread[i].data?.wall?.name ?? "")'s wall, but the comment was deleted :(")
}
Spacer()
}
}
} else {
HStack {
Text("Unhandled message type: \(messagesState.unread[i].type)")
Spacer()
}
}
}.listRowSeparator(.hidden).padding().background(.regularMaterial,in: RoundedRectangle(cornerRadius: 8, style: .continuous))
}.onDelete(perform: delete)
} else {
VStack {
Spacer()
HStack {
Spacer()
Text("No messages :)")
Spacer()
}
Spacer()
}
}
if messagesState.last != true {
Button {
page += 1
fetchMessages(token: session.token, page: page, read: read) { messages in
let messagesArray = messages.unread ?? []
if messagesArray.count > 0 {
messagesState.unread = messagesArray
}
messagesState.last = messages.last
}
} label: {
Text("Show more")
}.buttonStyle(.bordered).tint(.accentColor)
}
}.listStyle(.plain)
}.toolbar {
Button {
read.toggle()
fetchMessages(token: session.token, page: page, read: read) { messages in
var messagesArray: [Message]
if read {
messagesArray = messages.read ?? []
} else {
messagesArray = messages.unread ?? []
}
messagesState.unread = messagesArray
messagesState.last = messages.last
}
} label: {
Label(read ? "Read" : "Unread", systemImage: read ? "eye.fill" : "eye")
}
EditButton()
}.navigationTitle(read ? "Read Messages" : "Unread Messages")
}.refreshable {
page = 1
fetchMessages(token: session.token, page: page, read: read) { messages in
var messagesArray: [Message]
if read {
messagesArray = messages.read ?? []
} else {
messagesArray = messages.unread ?? []
}
messagesState.unread = messagesArray
messagesState.last = messages.last
}
}
}
func delete(at offsets: IndexSet) {
for n in offsets {
let messageid = messagesState.unread[n]._id
let messageState = messagesState.unread[n].read
messagesState.unread.remove(at: n)
markMessage(id: messageid, token: session.token, read: !messageState)
/*if messagesState.unread.count < 14 && oldCount > 14 {
fetchMessages(token: session.token, page: 1, add: false)
}*/
}
}
func markMessage(id: String, token: String, read: Bool) {
struct messagesList: Hashable, Codable {
var messages: [String]
}
let messageToMark = messagesList(messages: [id])
guard let url = URL(string: "https://api.wasteof.money/messages/mark/\(read == true ? "read" : "unread")") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
//let postString = "username=\(username)&password=\(password)"
/*struct LoginData: Hashable, Codable {
let username: String
let password: String
}*/
struct MarkResponse: Hashable, Codable {
var ok: String
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(token, forHTTPHeaderField: "Authorization")
let body = messageToMark
let finalBody = try? JSONEncoder().encode(body)
request.httpBody = finalBody
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Error took place \(error)")
//result = LoginResponse(ok: "error.localizedDescription", new: New(isLoving: false, loves: 0))
} else {
if let data = data, let dataString = String(data: data, encoding: .utf8) {
let jsonData = dataString.data(using: .utf8)!
var response: MarkResponse = MarkResponse(ok: "no")
//print(response)
do {
response = try JSONDecoder().decode(MarkResponse.self, from: jsonData)
print(response.ok)
} catch DecodingError.keyNotFound(_, _) {
print("something went wrong, key not found")
print("error: \(String(decoding: jsonData, as: UTF8.self))")
} catch {
print("something went wrong")
print(String(decoding: jsonData, as: UTF8.self))
}
}
}
}
task.resume()
}
}