Skip to content
This repository was archived by the owner on Jun 7, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Rocket.Chat/Controllers/Chat/MessagesComposerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class MessagesComposerViewModel {
var replyMessageIdentifier = ""
var messageToEdit: Message?

let hintPrefixes: [Character] = ["/", "#", "@", ":"]
let hintPrefixes: [Character] = ["/", "#", "@", ":", "+"]

var hints: [Hint] = []
var hintPrefixedWord: String = ""
Expand All @@ -56,6 +56,7 @@ final class MessagesComposerViewModel {
}

let word = String(word.dropFirst())
let subprefix = word.first

if prefix == "@" {
hints = User.search(
Expand Down Expand Up @@ -97,7 +98,7 @@ final class MessagesComposerViewModel {
commands.forEach {
hints.append(.command($0))
}
} else if prefix == ":" {
} else if prefix == ":" || (prefix == "+" && subprefix == ":") {
let emojis = EmojiSearcher.standard.search(shortname: word.lowercased(), custom: CustomEmoji.emojis())

emojis.forEach {
Expand Down
6 changes: 6 additions & 0 deletions Rocket.Chat/Controllers/Chat/MessagesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,12 @@ extension MessagesViewModel {
}

client.runCommand(command: command, params: params, roomId: subscription.rid)
} else if let lastMessage = subscription.roomLastMessage, let emoji = text.reaction() {
guard let client = API.current()?.client(MessagesClient.self) else {
return Alert.defaultError.present()
}

client.reactMessage(lastMessage, emoji: emoji)
} else {
guard let client = API.current()?.client(MessagesClient.self) else {
return Alert.defaultError.present()
Expand Down
9 changes: 9 additions & 0 deletions Rocket.Chat/Extensions/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ extension String {
return (command: command, params: params)
}

func reaction() -> String? {
guard self.first == "+" && self.count > 1 else { return nil }

let emoji = String(self.dropFirst())
guard emoji.first == ":" && emoji.last == ":" else { return nil }

return emoji
}

var boolValue: Bool {
return NSString(string: self).boolValue
}
Expand Down
4 changes: 4 additions & 0 deletions Rocket.Chat/External/RCEmojiKit/EmojiSearcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ open class EmojiSearcher {

public func search(shortname: String, custom: [Emoji] = []) -> [EmojiSearchResult] {
return (emojis + custom).compactMap { emoji -> EmojiSearchResult? in
if shortname.count == 0 {
return (emoji: emoji, suggestion: emoji.shortname)
}

if let suggestion = emoji.shortname.contains(shortname) ? emoji.shortname : emoji.alternates.filter({ $0.contains(shortname) }).first {
return (emoji: emoji, suggestion: suggestion.contains(":") ? suggestion : ":\(suggestion):")
}
Expand Down
19 changes: 19 additions & 0 deletions Rocket.ChatTests/Extensions/StringExtensionSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,23 @@ class StringExtensionSpec: XCTestCase {

XCTAssertNil(string2.commandAndParams())
}

func testReaction() {
let string = "+:upside_down:"

guard let emoji = string.reaction() else {
return XCTFail("string is valid reaction")
}

XCTAssertEqual(emoji, ":upside_down:")

let string2 = ":upside_down:"
XCTAssertNil(string2.reaction())

let string3 = "+upside_down:"
XCTAssertNil(string3.reaction())

let string4 = "+:upside_down"
XCTAssertNil(string4.reaction())
}
}
7 changes: 7 additions & 0 deletions Rocket.ChatTests/External/RCEmojiKit/EmojiSearcherSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,12 @@ class EmojiSearcherSpec: XCTestCase {
XCTAssert(search3.count == 1)
XCTAssert(search3[0].emoji.shortname == ":thumbsup:")
XCTAssert(search3[0].suggestion == ":+1:")

let search4 = searcher.search(shortname: "")
XCTAssert(search4.count == 4)
XCTAssert(search4[0].emoji.shortname == ":smiley:")
XCTAssert(search4[1].emoji.shortname == ":thumbsup:")
XCTAssert(search4[2].emoji.shortname == ":sunglasses:")
XCTAssert(search4[3].emoji.shortname == ":radioactive:")
}
}