Skip to content

Commit

Permalink
org.gradle.daemon=false
Browse files Browse the repository at this point in the history
feat: make user editable on BugsnagEvent
  • Loading branch information
fractalwrench committed Apr 21, 2020
1 parent 72bf407 commit d86b926
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Bugsnag Notifiers on other platforms.

## Enhancements

* Make user editable on `BugsnagEvent`
[#557](https://github.com/bugsnag/bugsnag-cocoa/pull/557)

* Add `sendThreads` property to `BugsnagConfiguration`
[#549](https://github.com/bugsnag/bugsnag-cocoa/pull/549)

Expand Down
22 changes: 22 additions & 0 deletions Source/BugsnagEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@class BugsnagMetadata;
@class BugsnagThread;
@class BugsnagError;
@class BugsnagUser;

typedef NS_ENUM(NSUInteger, BSGSeverity) {
BSGSeverityError,
Expand Down Expand Up @@ -130,5 +131,26 @@ initWithErrorName:(NSString *_Nonnull)name
*/
@property(nullable) id originalError;


// =============================================================================
// MARK: - User
// =============================================================================

/**
* The current user
*/
@property(readonly, nonatomic, nonnull) BugsnagUser *user;

/**
* Set user metadata
*
* @param userId ID of the user
* @param name Name of the user
* @param email Email address of the user
*/
- (void)setUser:(NSString *_Nullable)userId
withEmail:(NSString *_Nullable)email
andName:(NSString *_Nullable)name;

@end

57 changes: 47 additions & 10 deletions Source/BugsnagEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#import "BugsnagThread.h"
#import "RegisterErrorData.h"
#import "BugsnagSessionInternal.h"
#import "BugsnagUser.h"

static NSString *const DEFAULT_EXCEPTION_TYPE = @"cocoa";

Expand Down Expand Up @@ -277,6 +278,7 @@ - (BOOL)shouldBeSent;
@property(readwrite, copy, nullable) NSString *releaseStage;

@property NSArray *redactedKeys;

@end

@implementation BugsnagEvent
Expand Down Expand Up @@ -383,6 +385,7 @@ - (instancetype)initWithKSReport:(NSDictionary *)report {
}
_severity = _handledState.currentSeverity;
}
_user = [self parseUser:[_metadata toDictionary]];
}
return self;
}
Expand Down Expand Up @@ -525,6 +528,48 @@ - (void)setSeverity:(BSGSeverity)severity {
}
}

// =============================================================================
// MARK: - User
// =============================================================================

/**
* The current user
*/
@synthesize user = _user;
- (BugsnagUser *_Nonnull)user {
return _user;
}

/**
* Set user metadata
*
* @param userId ID of the user
* @param name Name of the user
* @param email Email address of the user
*/
- (void)setUser:(NSString *_Nullable)userId
withEmail:(NSString *_Nullable)email
andName:(NSString *_Nullable)name {
_user = [[BugsnagUser alloc] initWithUserId:userId name:name emailAddress:email];
}

/**
* Read the user from a persisted KSCrash report
* @param metadata the KSCrash report metadata
* @return the user, or nil if not available
*/
- (BugsnagUser *)parseUser:(NSDictionary *)metadata {
NSMutableDictionary *user = [metadata[BSGKeyUser] mutableCopy];
if (user == nil) {
user = [NSMutableDictionary dictionary];
}

if (!user[BSGKeyId] && self.device.id) { // if device id is null, don't set user id to default
BSGDictSetSafeObject(user, self.deviceAppHash, BSGKeyId);
}
return [[BugsnagUser alloc] initWithDictionary:user];
}

// MARK: - Callback overrides

@synthesize overrides = _overrides;
Expand Down Expand Up @@ -601,16 +646,8 @@ - (NSDictionary *)toJson {
// Build metadata
BSGDictSetSafeObject(metadata, [self error], BSGKeyError);

// Make user mutable and set the id if the user hasn't already
NSMutableDictionary *user = [metadata[BSGKeyUser] mutableCopy];
if (user == nil) {
user = [NSMutableDictionary dictionary];
}
BSGDictInsertIfNotNil(event, user, BSGKeyUser);

if (!user[BSGKeyId] && self.device.id) { // if device id is null, don't set user id to default
BSGDictSetSafeObject(user, [self deviceAppHash], BSGKeyId);
}
// add user
BSGDictInsertIfNotNil(event, [self.user toJson], BSGKeyUser);

if (self.session) {
BSGDictSetSafeObject(event, [self generateSessionDict], BSGKeySession);
Expand Down
16 changes: 16 additions & 0 deletions Tests/BugsnagUserTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <XCTest/XCTest.h>

#import "BugsnagUser.h"
#import "BugsnagEvent.h"

@interface BugsnagUserTest : XCTestCase
@end
Expand Down Expand Up @@ -45,4 +46,19 @@ - (void)testPayloadSerialisation {
XCTAssertEqualObjects(@"Tom Bombadil", rootNode[@"name"]);
}

- (void)testUserEvent {
// Setup
BugsnagEvent *event = [[BugsnagEvent alloc] initWithKSReport:@{
@"user.metaData": @{
@"user": @{
@"id": @"123",
@"name": @"Jane Smith",
@"email": @"jane@example.com",
}
}}];
XCTAssertEqualObjects(@"123", event.user.userId);
XCTAssertEqualObjects(@"Jane Smith", event.user.name);
XCTAssertEqualObjects(@"jane@example.com", event.user.emailAddress);
}

@end
2 changes: 2 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ This is now BugsnagEvent.
```diff
+ event.unhandled
+ event.originalError
+ event.user
+ event.setUser
```

`event.device` is now a structured class with properties for each value, rather than an `NSDictionary`.
Expand Down

0 comments on commit d86b926

Please sign in to comment.