Skip to content

Commit 264c42e

Browse files
authored
Merge pull request #673 from matrix-org/reaction_echo
Aggregation API: Local echo for reactions
2 parents 3c1e6f9 + 88a0683 commit 264c42e

13 files changed

+805
-165
lines changed

MatrixSDK.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@
302302
32C03CBE21231C2600D92712 /* AUTHORS.rst in Resources */ = {isa = PBXBuildFile; fileRef = 32C03CBA21231C2500D92712 /* AUTHORS.rst */; };
303303
32C235721F827F3800E38FC5 /* MXRoomOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 32C235701F827F3800E38FC5 /* MXRoomOperation.h */; };
304304
32C235731F827F3800E38FC5 /* MXRoomOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 32C235711F827F3800E38FC5 /* MXRoomOperation.m */; };
305+
32C474C122AF7A2D00CFBCD2 /* MXReactionOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 32C474BF22AF7A2D00CFBCD2 /* MXReactionOperation.h */; settings = {ATTRIBUTES = (Public, ); }; };
306+
32C474C222AF7A2D00CFBCD2 /* MXReactionOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 32C474C022AF7A2D00CFBCD2 /* MXReactionOperation.m */; };
305307
32C6F93319DD814400EA4E9C /* MatrixSDK.h in Headers */ = {isa = PBXBuildFile; fileRef = 32C6F93219DD814400EA4E9C /* MatrixSDK.h */; settings = {ATTRIBUTES = (Public, ); }; };
306308
32CAB1071A91EA34008C5BB9 /* MXPushRuleRoomMemberCountConditionChecker.h in Headers */ = {isa = PBXBuildFile; fileRef = 32CAB1051A91EA34008C5BB9 /* MXPushRuleRoomMemberCountConditionChecker.h */; };
307309
32CAB1081A91EA34008C5BB9 /* MXPushRuleRoomMemberCountConditionChecker.m in Sources */ = {isa = PBXBuildFile; fileRef = 32CAB1061A91EA34008C5BB9 /* MXPushRuleRoomMemberCountConditionChecker.m */; };
@@ -765,6 +767,8 @@
765767
32C03CBA21231C2500D92712 /* AUTHORS.rst */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = AUTHORS.rst; sourceTree = "<group>"; };
766768
32C235701F827F3800E38FC5 /* MXRoomOperation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXRoomOperation.h; sourceTree = "<group>"; };
767769
32C235711F827F3800E38FC5 /* MXRoomOperation.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXRoomOperation.m; sourceTree = "<group>"; };
770+
32C474BF22AF7A2D00CFBCD2 /* MXReactionOperation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXReactionOperation.h; sourceTree = "<group>"; };
771+
32C474C022AF7A2D00CFBCD2 /* MXReactionOperation.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXReactionOperation.m; sourceTree = "<group>"; };
768772
32C6F92D19DD814400EA4E9C /* MatrixSDK.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MatrixSDK.framework; sourceTree = BUILT_PRODUCTS_DIR; };
769773
32C6F93119DD814400EA4E9C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
770774
32C6F93219DD814400EA4E9C /* MatrixSDK.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MatrixSDK.h; sourceTree = "<group>"; };
@@ -1381,6 +1385,8 @@
13811385
32133020228BF7BC0070BA9B /* MXReactionCountChange.m */,
13821386
32133023228BFA800070BA9B /* MXReactionCountChangeListener.h */,
13831387
32133024228BFA800070BA9B /* MXReactionCountChangeListener.m */,
1388+
32C474BF22AF7A2D00CFBCD2 /* MXReactionOperation.h */,
1389+
32C474C022AF7A2D00CFBCD2 /* MXReactionOperation.m */,
13841390
32B94DFF228EDEBC00716A26 /* MXReactionRelation.h */,
13851391
32B94E00228EDEBC00716A26 /* MXReactionRelation.m */,
13861392
B10AFB4522AA8A8D0092E6AF /* MXEventEditsListener.h */,
@@ -1965,6 +1971,7 @@
19651971
320BBF431D6C81550079890E /* MXEventsEnumeratorOnArray.h in Headers */,
19661972
32B76EA320FDE2BE00B095F6 /* MXRoomMembersCount.h in Headers */,
19671973
32C6F93319DD814400EA4E9C /* MatrixSDK.h in Headers */,
1974+
32C474C122AF7A2D00CFBCD2 /* MXReactionOperation.h in Headers */,
19681975
324BE46C1E422766008D99D4 /* MXMegolmSessionData.h in Headers */,
19691976
327E9AFC228AC22800A98BC1 /* MXAggregationsStore.h in Headers */,
19701977
323547DC2226FC5700F15F94 /* MXCredentials.h in Headers */,
@@ -2290,6 +2297,7 @@
22902297
32A151531DAF8A7200400192 /* MXQueuedEncryption.m in Sources */,
22912298
C6D5D60A1E4FA74000706C0F /* MXEnumConstants.swift in Sources */,
22922299
327E9ADD2284804500A98BC1 /* MXEventRelations.m in Sources */,
2300+
32C474C222AF7A2D00CFBCD2 /* MXReactionOperation.m in Sources */,
22932301
3256E3821DCB91EB003C9718 /* MXCryptoConstants.m in Sources */,
22942302
B18D18C22152B8E4003677D1 /* MXRoomMember.swift in Sources */,
22952303
32BBAE6F2178E99100D85F46 /* MXKeyBackupVersion.m in Sources */,

MatrixSDK/Aggregations/Data/MXAggregatedReactions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN
2727

2828
@property (nonatomic) NSArray<MXReactionCount*> *reactions;
2929

30+
- (nullable MXAggregatedReactions *)aggregatedReactionsWithNonZeroCount;
31+
3032
@end
3133

3234
NS_ASSUME_NONNULL_END

MatrixSDK/Aggregations/Data/MXAggregatedReactions.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,25 @@
1818

1919
@implementation MXAggregatedReactions
2020

21+
- (nullable MXAggregatedReactions *)aggregatedReactionsWithNonZeroCount
22+
{
23+
NSMutableArray *reactions = [NSMutableArray arrayWithCapacity:self.reactions.count];
24+
for (MXReactionCount *reactionCount in self.reactions)
25+
{
26+
if (reactionCount.count > 0)
27+
{
28+
[reactions addObject:reactionCount];
29+
}
30+
}
31+
32+
MXAggregatedReactions *nonZeroCountAggregatedReactions;
33+
if (reactions.count)
34+
{
35+
nonZeroCountAggregatedReactions = [MXAggregatedReactions new];
36+
nonZeroCountAggregatedReactions.reactions = reactions;
37+
}
38+
39+
return nonZeroCountAggregatedReactions;
40+
}
41+
2142
@end

MatrixSDK/Aggregations/Data/MXReactionCount.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19+
#import "MXReactionOperation.h"
20+
1921
NS_ASSUME_NONNULL_BEGIN
2022

2123
/**
@@ -26,12 +28,21 @@ NS_ASSUME_NONNULL_BEGIN
2628
@property (nonatomic) NSString *reaction;
2729
@property (nonatomic) NSUInteger count;
2830

29-
// The id of the event if our user has made this reaction
31+
32+
// The id of the reaction event if our user has made this reaction
3033
@property (nonatomic, nullable) NSString *myUserReactionEventId;
3134

3235
// YES if our user has made this reaction
3336
@property (nonatomic, readonly) BOOL myUserHasReacted;
3437

38+
39+
// List of pending operation requests on this reaction
40+
// Note: self.count includes counts of them
41+
@property (nonatomic) NSArray<MXReactionOperation*> *localEchoesOperations;
42+
43+
// YES if there are pending operations
44+
@property (nonatomic, readonly) BOOL containsLocalEcho;
45+
3546
@end
3647

3748
NS_ASSUME_NONNULL_END

MatrixSDK/Aggregations/Data/MXReactionCount.m

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,33 @@ @implementation MXReactionCount
2020

2121
- (BOOL)myUserHasReacted
2222
{
23-
return (_myUserReactionEventId != nil);
23+
// Take local echoes into consideration first
24+
if (self.localEchoesOperations.count)
25+
{
26+
return self.localEchoesOperations.lastObject.isAddOperation;
27+
}
28+
else
29+
{
30+
return (_myUserReactionEventId != nil);
31+
}
32+
}
33+
34+
- (BOOL)containsLocalEcho
35+
{
36+
return (self.localEchoesOperations.count > 0);
2437
}
2538

2639
- (NSString *)description
2740
{
41+
NSString *echoes = self.localEchoesOperations.count ? [NSString stringWithFormat:@" - echoes: %@", @(self.localEchoesOperations.count)] : @"";
42+
2843
if (self.myUserHasReacted)
2944
{
30-
return [NSString stringWithFormat:@"(%@: %@)", self.reaction, @(self.count)];
45+
return [NSString stringWithFormat:@"(%@: %@%@)", self.reaction, @(self.count), echoes];
3146
}
3247
else
3348
{
34-
return [NSString stringWithFormat:@"%@: %@", self.reaction, @(self.count)];
49+
return [NSString stringWithFormat:@"%@: %@%@", self.reaction, @(self.count), echoes];
3550
}
3651
}
3752

MatrixSDK/Aggregations/Data/MXReactionCountChange.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ NS_ASSUME_NONNULL_BEGIN
2626
@property (nonatomic, nullable) NSArray<MXReactionCount*> *modified;
2727
@property (nonatomic, nullable) NSArray<NSString* /*reaction string*/> *deleted;
2828

29+
@property (nonatomic) BOOL changeDueToLocalEcho;
30+
2931
@end
3032

3133
NS_ASSUME_NONNULL_END
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2019 The Matrix.org Foundation C.I.C
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
/**
22+
A `MXReactionOperation` represents a pending operation on a reaction on a event.
23+
The `MXReactionOperation` exists while the operation has not been acknowledged
24+
by the homeserver, ie while we have not received the reaction event back from
25+
the event stream.
26+
*/
27+
@interface MXReactionOperation : NSObject
28+
29+
@property (nonatomic) NSString *eventId;
30+
@property (nonatomic) NSString *reaction;
31+
@property (nonatomic) BOOL isAddOperation;
32+
33+
@property (nonatomic) void (^block)(BOOL requestAlreadyPending);
34+
35+
@end
36+
37+
NS_ASSUME_NONNULL_END
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2019 The Matrix.org Foundation C.I.C
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#import "MXReactionOperation.h"
18+
19+
@implementation MXReactionOperation
20+
21+
- (NSString *)description
22+
{
23+
NSString *sign = self.isAddOperation ? @"+" : @"-";
24+
return [NSString stringWithFormat:@"%@: %@", self.reaction, sign];
25+
}
26+
27+
@end

MatrixSDK/Aggregations/MXAggregatedReactionsUpdater.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ NS_ASSUME_NONNULL_BEGIN
2727

2828
@interface MXAggregatedReactionsUpdater : NSObject
2929

30-
- (instancetype)initWithMyUser:(NSString*)userId
31-
aggregationStore:(id<MXAggregationsStore>)store
32-
matrixStore:(id<MXStore>)matrixStore;
30+
- (instancetype)initWithMatrixSession:(MXSession *)mxSession aggregationStore:(id<MXAggregationsStore>)store;
31+
32+
#pragma mark - Requests
33+
- (void)addReaction:(NSString*)reaction
34+
forEvent:(NSString*)eventId
35+
inRoom:(NSString*)roomId
36+
success:(void (^)(void))success
37+
failure:(void (^)(NSError *error))failure;
38+
- (void)removeReaction:(NSString*)reaction
39+
forEvent:(NSString*)eventId
40+
inRoom:(NSString*)roomId
41+
success:(void (^)(void))success
42+
failure:(void (^)(NSError *error))failure;
3343

3444
#pragma mark - Data access
3545
- (nullable MXAggregatedReactions *)aggregatedReactionsOnEvent:(NSString*)eventId inRoom:(NSString*)roomId;

0 commit comments

Comments
 (0)