session-ios/Signal/src/call/RecentCallManager.m

91 lines
3.5 KiB
Mathematica
Raw Normal View History

2014-05-06 19:41:08 +02:00
#import "RecentCallManager.h"
#import "ContactsManager.h"
#import "FunctionalUtil.h"
#import "ObservableValue.h"
#import "PreferencesUtil.h"
2014-12-29 22:40:33 +01:00
#import "NSDate+millisecondTimeStamp.h"
#import "TSCall.h"
#import "TSStorageManager.h"
#import "TSContactThread.h"
2014-05-06 19:41:08 +02:00
2014-12-29 22:40:33 +01:00
@interface RecentCallManager ()
@property YapDatabaseConnection *dbConnection;
2014-05-06 19:41:08 +02:00
@end
@implementation RecentCallManager
2014-12-29 22:40:33 +01:00
- (instancetype)init{
self = [super init];
if (self) {
_dbConnection = [TSStorageManager sharedManager].newDatabaseConnection;
2014-05-06 19:41:08 +02:00
}
2014-12-29 22:40:33 +01:00
2014-05-06 19:41:08 +02:00
return self;
}
2014-12-29 22:40:33 +01:00
- (void)watchForCallsThrough:(PhoneManager*)phoneManager
untilCancelled:(TOCCancelToken*)untilCancelledToken {
2014-05-06 19:41:08 +02:00
require(phoneManager != nil);
[phoneManager.currentCallObservable watchLatestValue:^(CallState* latestCall) {
2014-11-27 03:10:00 +01:00
if (latestCall != nil) {
2014-05-06 19:41:08 +02:00
[self addCall:latestCall];
}
2014-12-29 22:40:33 +01:00
} onThread:NSThread.currentThread untilCancelled:untilCancelledToken];
2014-05-06 19:41:08 +02:00
}
2014-12-29 22:40:33 +01:00
- (void)addCall:(CallState*)call {
2014-05-06 19:41:08 +02:00
require(call != nil);
[call.futureTermination finallyDo:^(TOCFuture* interactionCompletion) {
2014-05-06 19:41:08 +02:00
bool isOutgoingCall = call.initiatedLocally;
bool isMissedCall = interactionCompletion.hasFailed;
2014-05-06 19:41:08 +02:00
Contact* contact = [self tryGetContactForCall:call];
RPRecentCallType callType = isOutgoingCall ? RPRecentCallTypeOutgoing
: isMissedCall ? RPRecentCallTypeMissed
: RPRecentCallTypeIncoming;
[self addRecentCall:[RecentCall recentCallWithContactID:contact.recordID
andNumber:call.remoteNumber
andCallType:callType]];
}];
}
2014-12-29 22:40:33 +01:00
- (Contact*)tryGetContactForCall:(CallState*)call {
2014-05-06 19:41:08 +02:00
if (call.potentiallySpecifiedContact != nil) return call.potentiallySpecifiedContact;
return [self tryGetContactForNumber:call.remoteNumber];
}
2014-12-29 22:40:33 +01:00
- (Contact*)tryGetContactForNumber:(PhoneNumber*)number {
return [Environment.getCurrent.contactsManager latestContactForPhoneNumber:number];
2014-05-06 19:41:08 +02:00
}
- (void)addMissedCallDueToBusy:(ResponderSessionDescriptor*)incomingCallDescriptor {
require(incomingCallDescriptor != nil);
Contact* contact = [self tryGetContactForNumber:incomingCallDescriptor.initiatorNumber];
[self addRecentCall:[RecentCall recentCallWithContactID:contact.recordID
andNumber:incomingCallDescriptor.initiatorNumber
andCallType:RPRecentCallTypeMissed]];
}
2014-12-29 22:40:33 +01:00
- (void)addRecentCall:(RecentCall*)recentCall {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:recentCall.phoneNumber.toE164 transaction:transaction];
uint64_t callDateSeconds = (uint64_t)[recentCall.date timeIntervalSince1970];
TSCall *call = [[TSCall alloc] initWithTimestamp:callDateSeconds*1000 withCallNumber:recentCall.phoneNumber.toE164 callType:recentCall.callType inThread:thread];
if(recentCall.isArchived) { //for migration only from Signal versions with RedPhone only
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(callDateSeconds+60)]; // archive has to happen in the future of the original call
[thread archiveThreadWithTransaction:transaction referenceDate:date];
}
2014-12-29 22:40:33 +01:00
[call saveWithTransaction:transaction];
2014-05-06 19:41:08 +02:00
}];
}
2014-05-06 19:41:08 +02:00
@end