Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #103 from matrix-org/release_v_2.14.4
Browse files Browse the repository at this point in the history
release_v_2.14.4
  • Loading branch information
Velin92 authored Oct 20, 2023
2 parents a82eece + e2b75bd commit e60d0ff
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Sources/WysiwygComposer/Components/ComposerModelWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ final class ComposerModelWrapper: ComposerModelWrapperProtocol {
func getLinkAction() -> LinkAction {
model.getLinkAction()
}

func getMentionsState() -> MentionsState {
model.getMentionsState()
}

// MARK: Extensions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ public extension WysiwygComposerViewModel {
func getLinkAction() -> LinkAction {
model.getLinkAction()
}

/// Get the current mentions present in the composer
func getMentionsState() -> MentionsState {
model.getMentionsState()
}

func enter() {
applyUpdate(createEnterUpdate(), skipTextViewUpdate: false)
Expand Down
123 changes: 123 additions & 0 deletions Sources/WysiwygComposer/WysiwygComposer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,27 @@ private struct FfiConverterUInt32: FfiConverterPrimitive {
}
}

private struct FfiConverterBool: FfiConverter {
typealias FfiType = Int8
typealias SwiftType = Bool

public static func lift(_ value: Int8) throws -> Bool {
return value != 0
}

public static func lower(_ value: Bool) -> Int8 {
return value ? 1 : 0
}

public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Bool {
return try lift(readInt(&buf))
}

public static func write(_ value: Bool, into buf: inout [UInt8]) {
writeInt(&buf, lower(value))
}
}

private struct FfiConverterString: FfiConverter {
typealias SwiftType = String
typealias FfiType = RustBuffer
Expand Down Expand Up @@ -375,6 +396,7 @@ public protocol ComposerModelProtocol {
func getContentAsPlainText() -> String
func getCurrentDomState() -> ComposerState
func getLinkAction() -> LinkAction
func getMentionsState() -> MentionsState
func indent() throws -> ComposerUpdate
func inlineCode() throws -> ComposerUpdate
func insertAtRoomMention() throws -> ComposerUpdate
Expand Down Expand Up @@ -561,6 +583,15 @@ public class ComposerModel: ComposerModelProtocol {
)
}

public func getMentionsState() -> MentionsState {
return try! FfiConverterTypeMentionsState.lift(
try!
rustCall {
uniffi_uniffi_wysiwyg_composer_fn_method_composermodel_get_mentions_state(self.pointer, $0)
}
)
}

public func indent() throws -> ComposerUpdate {
return try FfiConverterTypeComposerUpdate.lift(
try
Expand Down Expand Up @@ -1061,6 +1092,73 @@ public func FfiConverterTypeComposerState_lower(_ value: ComposerState) -> RustB
return FfiConverterTypeComposerState.lower(value)
}

public struct MentionsState {
public var userIds: [String]
public var roomIds: [String]
public var roomAliases: [String]
public var hasAtRoomMention: Bool

// Default memberwise initializers are never public by default, so we
// declare one manually.
public init(userIds: [String], roomIds: [String], roomAliases: [String], hasAtRoomMention: Bool) {
self.userIds = userIds
self.roomIds = roomIds
self.roomAliases = roomAliases
self.hasAtRoomMention = hasAtRoomMention
}
}

extension MentionsState: Equatable, Hashable {
public static func == (lhs: MentionsState, rhs: MentionsState) -> Bool {
if lhs.userIds != rhs.userIds {
return false
}
if lhs.roomIds != rhs.roomIds {
return false
}
if lhs.roomAliases != rhs.roomAliases {
return false
}
if lhs.hasAtRoomMention != rhs.hasAtRoomMention {
return false
}
return true
}

public func hash(into hasher: inout Hasher) {
hasher.combine(userIds)
hasher.combine(roomIds)
hasher.combine(roomAliases)
hasher.combine(hasAtRoomMention)
}
}

public struct FfiConverterTypeMentionsState: FfiConverterRustBuffer {
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> MentionsState {
return try MentionsState(
userIds: FfiConverterSequenceString.read(from: &buf),
roomIds: FfiConverterSequenceString.read(from: &buf),
roomAliases: FfiConverterSequenceString.read(from: &buf),
hasAtRoomMention: FfiConverterBool.read(from: &buf)
)
}

public static func write(_ value: MentionsState, into buf: inout [UInt8]) {
FfiConverterSequenceString.write(value.userIds, into: &buf)
FfiConverterSequenceString.write(value.roomIds, into: &buf)
FfiConverterSequenceString.write(value.roomAliases, into: &buf)
FfiConverterBool.write(value.hasAtRoomMention, into: &buf)
}
}

public func FfiConverterTypeMentionsState_lift(_ buf: RustBuffer) throws -> MentionsState {
return try FfiConverterTypeMentionsState.lift(buf)
}

public func FfiConverterTypeMentionsState_lower(_ value: MentionsState) -> RustBuffer {
return FfiConverterTypeMentionsState.lower(value)
}

public struct SuggestionPattern {
public var key: PatternKey
public var text: String
Expand Down Expand Up @@ -1655,6 +1753,28 @@ private struct FfiConverterSequenceUInt16: FfiConverterRustBuffer {
}
}

private struct FfiConverterSequenceString: FfiConverterRustBuffer {
typealias SwiftType = [String]

public static func write(_ value: [String], into buf: inout [UInt8]) {
let len = Int32(value.count)
writeInt(&buf, len)
for item in value {
FfiConverterString.write(item, into: &buf)
}
}

public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [String] {
let len: Int32 = try readInt(&buf)
var seq = [String]()
seq.reserveCapacity(Int(len))
for _ in 0 ..< len {
try seq.append(FfiConverterString.read(from: &buf))
}
return seq
}
}

private struct FfiConverterSequenceTypeAttribute: FfiConverterRustBuffer {
typealias SwiftType = [Attribute]

Expand Down Expand Up @@ -1775,6 +1895,9 @@ private var initializationResult: InitializationResult {
if uniffi_uniffi_wysiwyg_composer_checksum_method_composermodel_get_link_action() != 2600 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_uniffi_wysiwyg_composer_checksum_method_composermodel_get_mentions_state() != 20232 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_uniffi_wysiwyg_composer_checksum_method_composermodel_indent() != 48116 {
return InitializationResult.apiChecksumMismatch
}
Expand Down
20 changes: 11 additions & 9 deletions Tests/WysiwygComposerSnapshotTests/SnapshotTests+Blocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class BlocksSnapshotTests: SnapshotTests {
}

func testQuoteContent() throws {
viewModel.setHtmlContent("<blockquote><p>Some quote with</p><p></p><p></p><p></p><p>line breaks inside</p></blockquote>")
viewModel.setHtmlContent("<blockquote>Some quote with<br/><br/><br/><br/>line breaks inside</blockquote>")
assertSnapshot(
matching: hostingController,
as: .image(on: .iPhone13),
Expand All @@ -47,15 +47,17 @@ final class BlocksSnapshotTests: SnapshotTests {
func testMultipleBlocksContent() throws {
viewModel.setHtmlContent(
"""
<blockquote><p>Some</p>\
<p>multi-line</p>\
<p>quote</p></blockquote>\
<p></p>\
<p>Some text</p>\
<p></p>\
<blockquote>Some<br/>\
multi-line<br/>\
quote</blockquote>\
<br/>\
<br/>\
Some text<br/>\
<br/>\
<pre>A\n\tcode\nblock</pre>\
<p></p>\
<p>Some <code>inline</code> code</p>
<br/>\
<br/>\
Some <code>inline</code> code
"""
)
assertSnapshot(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//
// Copyright 2023 The Matrix.org Foundation C.I.C
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

@testable import WysiwygComposer
import XCTest

extension WysiwygComposerViewModelTests {
func testSetAtRooMentionsState() {
viewModel.setAtRoomMention()
XCTAssertEqual(viewModel.getMentionsState(), MentionsState(userIds: [], roomIds: [], roomAliases: [], hasAtRoomMention: true))
}

func testAtRooMentionsStateBySettingContent() {
viewModel.setHtmlContent("@room")
XCTAssertEqual(viewModel.getMentionsState(), MentionsState(userIds: [], roomIds: [], roomAliases: [], hasAtRoomMention: true))
}

func testMentionsStatBySettingUserMention() {
viewModel.setMention(url: "https://matrix.to/#/@alice:matrix.org", name: "Alice", mentionType: .user)
XCTAssertEqual(viewModel.getMentionsState(),
MentionsState(userIds: ["@alice:matrix.org"], roomIds: [], roomAliases: [], hasAtRoomMention: false))
}

func testMentionsStateBySettingUserMentionFromContent() {
let result = MentionsState(userIds: ["@alice:matrix.org"], roomIds: [], roomAliases: [], hasAtRoomMention: false)
viewModel.setHtmlContent("<a href=\"https://matrix.to/#/@alice:matrix.org\">Alice</a>")
XCTAssertEqual(viewModel.getMentionsState(), result)

viewModel.setMarkdownContent("[Alice](https://matrix.to/#/@alice:matrix.org)")
XCTAssertEqual(viewModel.getMentionsState(), result)
}

func testMentionsStatBySettingRoomAliasMention() {
viewModel.setMention(url: "https://matrix.to/#/#room:matrix.org", name: "Room", mentionType: .room)
XCTAssertEqual(viewModel.getMentionsState(),
MentionsState(userIds: [], roomIds: [], roomAliases: ["#room:matrix.org"], hasAtRoomMention: false))
}

func testMentionsStateBySettingRoomAliasMentionFromContent() {
let result = MentionsState(userIds: [], roomIds: [], roomAliases: ["#room:matrix.org"], hasAtRoomMention: false)
viewModel.setHtmlContent("<a href=\"https://matrix.to/#/#room:matrix.org\">Room</a>")
XCTAssertEqual(viewModel.getMentionsState(), result)

viewModel.setMarkdownContent("[Room](https://matrix.to/#/#room:matrix.org)")
XCTAssertEqual(viewModel.getMentionsState(), result)
}

func testMentionsStatBySettingRoomIDMention() {
viewModel.setMention(url: "https://matrix.to/#/!room:matrix.org", name: "Room", mentionType: .room)
XCTAssertEqual(viewModel.getMentionsState(), MentionsState(userIds: [], roomIds: ["!room:matrix.org"], roomAliases: [], hasAtRoomMention: false))
}

func testMentionsStateBySettingRoomIDMentionFromContent() {
let result = MentionsState(userIds: [], roomIds: ["!room:matrix.org"], roomAliases: [], hasAtRoomMention: false)
viewModel.setHtmlContent("<a href=\"https://matrix.to/#/!room:matrix.org\">Room</a>")
XCTAssertEqual(viewModel.getMentionsState(), result)

viewModel.setMarkdownContent("[Room](https://matrix.to/#/!room:matrix.org)")
XCTAssertEqual(viewModel.getMentionsState(), result)
}

func testMultipleMentionsBySettingThemIndividually() {
viewModel.setMention(url: "https://matrix.to/#/@alice:matrix.org", name: "Alice", mentionType: .user)
viewModel.setMention(url: "https://matrix.to/#/@bob:matrix.org", name: "Bob", mentionType: .user)
viewModel.setAtRoomMention()

let mentionsState = viewModel.getMentionsState()
XCTAssertEqual(mentionsState.userIds.count, 2)
XCTAssertEqual(Set(mentionsState.userIds), ["@alice:matrix.org", "@bob:matrix.org"])
XCTAssertTrue(mentionsState.hasAtRoomMention)
XCTAssertTrue(mentionsState.roomIds.isEmpty)
XCTAssertTrue(mentionsState.roomAliases.isEmpty)
}

func testMultipleDuplicateMentionsBySettingThemIndividually() {
viewModel.setMention(url: "https://matrix.to/#/@alice:matrix.org", name: "Alice", mentionType: .user)
viewModel.setMention(url: "https://matrix.to/#/@alice:matrix.org", name: "Alice", mentionType: .user)

XCTAssertEqual(viewModel.getMentionsState(),
MentionsState(userIds: ["@alice:matrix.org"], roomIds: [], roomAliases: [], hasAtRoomMention: false))
}

func testMultipleMentionsBySettingThemWithHtmlContent() {
viewModel.setHtmlContent(
"""
<p><a href=\"https://matrix.to/#/@alice:matrix.org\">Alice</a>, \
<a href=\"https://matrix.to/#/!room:matrix.org\">Room</a>, \
<a href=\"https://matrix.to/#/@bob:matrix.org\">Bob</a>, \
<a href=\"https://matrix.to/#/#room:matrix.org\">Room</a>, \
@room</p>
"""
)
let mentionState = viewModel.getMentionsState()
XCTAssertEqual(Set(mentionState.userIds), ["@alice:matrix.org", "@bob:matrix.org"])
XCTAssertEqual(mentionState.roomAliases, ["#room:matrix.org"])
XCTAssertEqual(mentionState.roomIds, ["!room:matrix.org"])
XCTAssertTrue(mentionState.hasAtRoomMention)
}

func testMultipleMentionsBySettingThemWithMarkdownContent() {
viewModel.setMarkdownContent(
"""
[Room](https://matrix.to/#/!room:matrix.org), \
[Room](https://matrix.to/#/#room:matrix.org), \
[Alice](https://matrix.to/#/@alice:matrix.org), \
[Bob](https://matrix.to/#/@bob:matrix.org), \
@room
"""
)
let mentionState = viewModel.getMentionsState()
XCTAssertEqual(Set(mentionState.userIds), ["@alice:matrix.org", "@bob:matrix.org"])
XCTAssertEqual(mentionState.roomAliases, ["#room:matrix.org"])
XCTAssertEqual(mentionState.roomIds, ["!room:matrix.org"])
XCTAssertTrue(mentionState.hasAtRoomMention)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ extension WysiwygComposerViewModelTests {
)
}


func testAtMentionWithNoSuggestionAtLeading() {
_ = viewModel.replaceText(range: .zero, replacementText: "Text")
viewModel.select(range: .init(location: 0, length: 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ RustBuffer uniffi_uniffi_wysiwyg_composer_fn_method_composermodel_get_current_do
);
RustBuffer uniffi_uniffi_wysiwyg_composer_fn_method_composermodel_get_link_action(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_uniffi_wysiwyg_composer_fn_method_composermodel_get_mentions_state(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
);
void*_Nonnull uniffi_uniffi_wysiwyg_composer_fn_method_composermodel_indent(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
);
void*_Nonnull uniffi_uniffi_wysiwyg_composer_fn_method_composermodel_inline_code(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
Expand Down Expand Up @@ -223,6 +225,9 @@ uint16_t uniffi_uniffi_wysiwyg_composer_checksum_method_composermodel_get_curren
);
uint16_t uniffi_uniffi_wysiwyg_composer_checksum_method_composermodel_get_link_action(void

);
uint16_t uniffi_uniffi_wysiwyg_composer_checksum_method_composermodel_get_mentions_state(void

);
uint16_t uniffi_uniffi_wysiwyg_composer_checksum_method_composermodel_indent(void

Expand Down
Loading

0 comments on commit e60d0ff

Please sign in to comment.