Skip to content

Commit

Permalink
Support more message types!
Browse files Browse the repository at this point in the history
Newly supported types:
* recipientAdd (member added to a group DM)
* recipientRemove (member removed from group DM)
* userPremiumGuildSub (server boosted)
* userPremiumGuildSubTier1, userPremiumGuildSubTier2, userPremiumGuildSubTier3 (server boosted and achieved tiers 1, 2 or 3 respectively)
  • Loading branch information
cryptoAlgorithm committed Aug 11, 2022
1 parent 36a59cd commit bbd5e1b
Showing 1 changed file with 84 additions and 14 deletions.
98 changes: 84 additions & 14 deletions Swiftcord/Views/Message/MessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,85 @@ struct NonUserBadge: View {
}
}

/// Action messages: e.g. member leave, join etc.
struct ActionMessageView: View {
let message: Message

private struct ActionMessageData {
let message: LocalizedStringKey
let icon: String
let color: Color
}

// Trust, this might seem messy but it's more scalable than putting the
// views themselves in a huge if tree
private var data: ActionMessageData {
switch message.type {
case .guildMemberJoin:
return ActionMessageData(
message: "**\(message.author.username)** joined this server.",
icon: "arrow.right",
color: .green
)
case .recipientAdd:
return ActionMessageData(
message: "**\(message.author.username)** added **\(message.mentions[0].username)** to the group.",
icon: "arrow.right",
color: .green
)
case .recipientRemove:
return ActionMessageData(
message: "**\(message.author.username)** left the group.",
icon: "arrow.left",
color: .red
)
case .userPremiumGuildSub:
return ActionMessageData(
message: "**\(message.author.username)** just boosted the server!",
icon: "rhombus.fill",
color: .purple
)
case .userPremiumGuildSubTier1:
return ActionMessageData(
message: "**\(message.author.username)** just boosted the server! This server has achieved **Level 1!**",
icon: "rhombus.fill",
color: .purple
)
case .userPremiumGuildSubTier2:
return ActionMessageData(
message: "**\(message.author.username)** just boosted the server! This server has achieved **Level 2!**",
icon: "rhombus.fill",
color: .purple
)
case .userPremiumGuildSubTier3:
return ActionMessageData(
message: "**\(message.author.username)** just boosted the server! This server has achieved **Level 3!**",
icon: "rhombus.fill",
color: .purple
)
default:
return ActionMessageData(
message: "Oops, rendering `\(String(describing: message.type))` messages aren't supported yet :(",
icon: "questionmark",
color: .gray
)
}
}

var body: some View {
Image(systemName: data.icon)
.foregroundColor(data.color)
.font(.system(size: 16))
.padding([.leading, .trailing], 12)
Group {
Text(data.message).font(.system(size: 14))
+ Text(" ").font(.system(size: 14))
+ Text(DateFormatter.messageDateFormatter.string(from: message.timestamp))
.font(.system(size: 12))
}.opacity(0.75)
}
}

struct MessageView: View {
let message: Message
let shrunk: Bool
Expand All @@ -51,8 +130,6 @@ struct MessageView: View {
@EnvironmentObject var serverCtx: ServerContext
@EnvironmentObject var restAPI: DiscordREST

public static let supportedTypes: [MessageType] = [.defaultMsg, .reply, .guildMemberJoin]

var body: some View {
VStack(alignment: .leading, spacing: 2) {
// This message is a reply!
Expand All @@ -66,15 +143,13 @@ struct MessageView: View {
alignment: message.type == .guildMemberJoin || message.type == .userPremiumGuildSub ? .center : .top,
spacing: 16
) {
// Would have loved to use switch-case but fallthrough doesn't work :(
if message.type == .reply || message.type == .defaultMsg {
if !shrunk {
UserAvatarView(user: message.author, guildID: serverCtx.guild!.id, webhookID: message.webhook_id, clickDisabled: false)
} else {
Text(message.timestamp, style: .time)
.font(.system(size: 8, weight: .semibold, design: .monospaced))
.frame(width: 40, height: 22, alignment: .center)
.animation(.linear(duration: 0.1), value: hovered)
.opacity(hovered ? 0.5 : 0)
}
VStack(alignment: .leading, spacing: lineSpacing) {
Expand Down Expand Up @@ -113,8 +188,8 @@ struct MessageView: View {
// fix this poor implementation later
}*/
let msg = message.content.containsOnlyEmojiAndSpaces
? message.content.replacingOccurrences(of: " ", with: "")
: message.content
? message.content.replacingOccurrences(of: " ", with: "")
: message.content
Group {
Text(markdown: msg)
.font(.system(
Expand Down Expand Up @@ -143,14 +218,9 @@ struct MessageView: View {
}
}
}
} else if message.type == .guildMemberJoin {
Image(systemName: "arrow.right")
.foregroundColor(.green)
.font(.system(size: 16))
.padding([.leading, .trailing], 12)
Text("Welcome, \(message.author.username), enjoy your stay!")
.font(.system(size: 14)).opacity(0.5)
}
} else {
ActionMessageView(message: message)
}
Spacer()
}
}
Expand Down

0 comments on commit bbd5e1b

Please sign in to comment.