Skip to content

Commit

Permalink
Merge pull request #2000 from nextcloud/fix/noid/highlight-group-and-…
Browse files Browse the repository at this point in the history
…team-mentions

fix(mentions): Highlight group and team mentions
  • Loading branch information
SystemKeeper authored Feb 25, 2025
2 parents b2a79ae + 4f62050 commit be661ef
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 35 deletions.
3 changes: 2 additions & 1 deletion NextcloudTalk/NCAPIController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2461,7 +2461,8 @@ - (NSURLSessionDataTask *)getUserProfileEditableFieldsForAccount:(TalkAccount *)

- (NSURLSessionDataTask *)setUserProfileField:(NSString *)field withValue:(NSString*)value forAccount:(TalkAccount *)account withCompletionBlock:(SetUserProfileFieldCompletionBlock)block
{
NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/cloud/users/%@", account.server, account.userId];
NSString *encodedUserId = [account.userId stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSString *URLString = [NSString stringWithFormat:@"%@/ocs/v2.php/cloud/users/%@", account.server, encodedUserId];
NSDictionary *parameters = @{@"format" : @"json",
@"key" : field,
@"value" : value};
Expand Down
40 changes: 40 additions & 0 deletions NextcloudTalk/NCAPIControllerExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -703,4 +703,44 @@ import Foundation
}
}
}

// MARK: - Groups & Teams

func getUserGroups(forAccount account: TalkAccount, completionBlock: @escaping (_ groupIds: [String]?, _ error: Error?) -> Void) {
guard let encodedUserId = account.userId.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed),
let apiSessionManager = self.apiSessionManagers.object(forKey: account.accountId) as? NCAPISessionManager
else {
completionBlock(nil, NSError(domain: "", code: 0, userInfo: nil))
return
}

let urlString = "\(account.server)/ocs/v2.php/cloud/users/\(encodedUserId)/groups"

apiSessionManager.getOcs(urlString, account: account) { ocsResponse, ocsError in
if ocsError?.error == nil, let groupdIds = ocsResponse?.dataDict?["groups"] as? [String] {
completionBlock(groupdIds, nil)
} else {
completionBlock(nil, ocsError?.error)
}
}
}

func getUserTeams(forAccount account: TalkAccount, completionBlock: @escaping (_ teamIds: [String]?, _ error: Error?) -> Void) {
guard let apiSessionManager = self.apiSessionManagers.object(forKey: account.accountId) as? NCAPISessionManager
else {
completionBlock(nil, NSError(domain: "", code: 0, userInfo: nil))
return
}

let urlString = "\(account.server)/ocs/v2.php/apps/circles/probecircles"

apiSessionManager.getOcs(urlString, account: account) { ocsResponse, ocsError in
if ocsError?.error == nil, let teamsDicts = ocsResponse?.dataArrayDict {
let teamIds = teamsDicts.compactMap { $0["id"] as? String }
completionBlock(teamIds, nil)
} else {
completionBlock(nil, ocsError?.error)
}
}
}
}
3 changes: 3 additions & 0 deletions NextcloudTalk/NCConnectionController.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ - (void)checkAppState
}];
}];
} else {
// Fetch additional data asynchronously.
// We set the app as ready, so we don’t need to wait for this to complete.
[[NCSettingsController sharedInstance] getUserGroupsAndTeamsForAccountId:activeAccount.accountId];
[self setAppState:kAppStateReady];
}

Expand Down
2 changes: 1 addition & 1 deletion NextcloudTalk/NCDatabaseManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

NSString *const kTalkDatabaseFolder = @"Library/Application Support/Talk";
NSString *const kTalkDatabaseFileName = @"talk.realm";
uint64_t const kTalkDatabaseSchemaVersion = 75;
uint64_t const kTalkDatabaseSchemaVersion = 76;

NSString * const kCapabilitySystemMessages = @"system-messages";
NSString * const kCapabilityNotificationLevels = @"notification-levels";
Expand Down
11 changes: 8 additions & 3 deletions NextcloudTalk/NCMessageParameter.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ - (instancetype)initWithDictionary:(NSDictionary *)parameterDict

- (BOOL)shouldBeHighlighted
{
// Own mentions
// Call mentions
TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
return ([_type isEqualToString:@"user"] && [activeAccount.userId isEqualToString:_parameterId]) || [_type isEqualToString:@"call"];
BOOL ownMention = [_type isEqualToString:@"user"] && [activeAccount.userId isEqualToString:_parameterId];
BOOL callMention = [_type isEqualToString:@"call"];
NSArray *userGroups = [activeAccount.groupIds valueForKey:@"self"];
BOOL groupMention = [_type isEqualToString:@"user-group"] && [userGroups containsObject:_mention.id];
NSArray *userTeams = [activeAccount.teamIds valueForKey:@"self"];
BOOL teamMention = [_type isEqualToString:@"circle"] && [userTeams containsObject:_mention.id];

return ownMention || callMention || groupMention || teamMention;
}

- (BOOL)isMention
Expand Down
1 change: 1 addition & 0 deletions NextcloudTalk/NCSettingsController.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ typedef NS_ENUM(NSInteger, NCPreferredFileSorting) {
- (void)addNewAccountForUser:(NSString *)user withToken:(NSString *)token inServer:(NSString *)server;
- (void)setActiveAccountWithAccountId:(NSString *)accountId;
- (void)getUserProfileForAccountId:(NSString *)accountId withCompletionBlock:(UpdatedProfileCompletionBlock _Nonnull)block;
- (void)getUserGroupsAndTeamsForAccountId:(NSString *)accountId;
- (void)logoutAccountWithAccountId:(NSString *)accountId withCompletionBlock:(LogoutCompletionBlock)block;
- (void)getCapabilitiesForAccountId:(NSString *)accountId withCompletionBlock:(GetCapabilitiesCompletionBlock)block;
- (void)updateSignalingConfigurationForAccountId:(NSString * _Nonnull)accountId withCompletionBlock:(UpdateSignalingConfigCompletionBlock _Nonnull)block;
Expand Down
103 changes: 73 additions & 30 deletions NextcloudTalk/NCSettingsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -374,44 +374,87 @@ - (void)getUserProfileForAccountId:(NSString *)accountId withCompletionBlock:(Up
email = @"";
}
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm transactionWithBlock:^{
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
TalkAccount *managedActiveAccount = [TalkAccount objectsWithPredicate:query].firstObject;

NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
TalkAccount *managedActiveAccount = [TalkAccount objectsWithPredicate:query].firstObject;
if (!managedActiveAccount) {
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
block(error);

if (!managedActiveAccount) {
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
block(error);
return;
}

return;
}
managedActiveAccount.userId = [userProfile objectForKey:kUserProfileUserId];
// "display-name" is returned by /cloud/user endpoint
// change to kUserProfileDisplayName ("displayName") when using /cloud/users/{userId} endpoint
managedActiveAccount.userDisplayName = [userProfile objectForKey:@"display-name"];
managedActiveAccount.userDisplayNameScope = [userProfile objectForKey:kUserProfileDisplayNameScope];
managedActiveAccount.phone = [userProfile objectForKey:kUserProfilePhone];
managedActiveAccount.phoneScope = [userProfile objectForKey:kUserProfilePhoneScope];
managedActiveAccount.email = email;
managedActiveAccount.emailScope = [userProfile objectForKey:kUserProfileEmailScope];
managedActiveAccount.address = [userProfile objectForKey:kUserProfileAddress];
managedActiveAccount.addressScope = [userProfile objectForKey:kUserProfileAddressScope];
managedActiveAccount.website = [userProfile objectForKey:kUserProfileWebsite];
managedActiveAccount.websiteScope = [userProfile objectForKey:kUserProfileWebsiteScope];
managedActiveAccount.twitter = [userProfile objectForKey:kUserProfileTwitter];
managedActiveAccount.twitterScope = [userProfile objectForKey:kUserProfileTwitterScope];
managedActiveAccount.avatarScope = [userProfile objectForKey:kUserProfileAvatarScope];

TalkAccount *unmanagedUpdatedAccount = [[TalkAccount alloc] initWithValue:managedActiveAccount];
[[NCAPIController sharedInstance] saveProfileImageForAccount:unmanagedUpdatedAccount];

managedActiveAccount.userId = [userProfile objectForKey:kUserProfileUserId];
// "display-name" is returned by /cloud/user endpoint
// change to kUserProfileDisplayName ("displayName") when using /cloud/users/{userId} endpoint
managedActiveAccount.userDisplayName = [userProfile objectForKey:@"display-name"];
managedActiveAccount.userDisplayNameScope = [userProfile objectForKey:kUserProfileDisplayNameScope];
managedActiveAccount.phone = [userProfile objectForKey:kUserProfilePhone];
managedActiveAccount.phoneScope = [userProfile objectForKey:kUserProfilePhoneScope];
managedActiveAccount.email = email;
managedActiveAccount.emailScope = [userProfile objectForKey:kUserProfileEmailScope];
managedActiveAccount.address = [userProfile objectForKey:kUserProfileAddress];
managedActiveAccount.addressScope = [userProfile objectForKey:kUserProfileAddressScope];
managedActiveAccount.website = [userProfile objectForKey:kUserProfileWebsite];
managedActiveAccount.websiteScope = [userProfile objectForKey:kUserProfileWebsiteScope];
managedActiveAccount.twitter = [userProfile objectForKey:kUserProfileTwitter];
managedActiveAccount.twitterScope = [userProfile objectForKey:kUserProfileTwitterScope];
managedActiveAccount.avatarScope = [userProfile objectForKey:kUserProfileAvatarScope];
block(nil);
}];
} else {
NSLog(@"Error while getting the user profile");
block(error);
}
}];
}

[realm commitWriteTransaction];
- (void)getUserGroupsAndTeamsForAccountId:(NSString *)accountId
{
TalkAccount *account = [[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId];

if (!account) {
return;
}

[[NCAPIController sharedInstance] getUserGroupsForAccount:account completionBlock:^(NSArray * _Nullable groupIds, NSError * _Nullable error) {
if (!error) {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
TalkAccount *managedActiveAccount = [TalkAccount objectsWithPredicate:query].firstObject;

TalkAccount *unmanagedUpdatedAccount = [[TalkAccount alloc] initWithValue:managedActiveAccount];
[[NCAPIController sharedInstance] saveProfileImageForAccount:unmanagedUpdatedAccount];
if (!managedActiveAccount) {
return;
}

block(nil);
managedActiveAccount.groupIds = groupIds;
}];
} else {
NSLog(@"Error while getting the user profile");
block(error);
NSLog(@"Error while getting user's groups");
}
}];

[[NCAPIController sharedInstance] getUserTeamsForAccount:account completionBlock:^(NSArray * _Nullable teamIds, NSError * _Nullable error) {
if (!error) {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
TalkAccount *managedActiveAccount = [TalkAccount objectsWithPredicate:query].firstObject;

if (!managedActiveAccount) {
return;
}

managedActiveAccount.teamIds = teamIds;
}];
} else {
NSLog(@"Error while getting user' teams");
}
}];
}
Expand Down
2 changes: 2 additions & 0 deletions NextcloudTalk/TalkAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ NS_ASSUME_NONNULL_BEGIN
@property NSString *lastNotificationETag;
@property NSInteger pendingFederationInvitations;
@property NSString *frequentlyUsedEmojisJSONString;
@property RLMArray<RLMString> *groupIds;
@property RLMArray<RLMString> *teamIds;

@end

Expand Down

0 comments on commit be661ef

Please sign in to comment.