2014-11-19 21:17:53 +01:00
|
|
|
//
|
|
|
|
// TSContactThread.m
|
|
|
|
// TextSecureKit
|
|
|
|
//
|
|
|
|
// Created by Frederic Jacobs on 16/11/14.
|
|
|
|
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "TSContactThread.h"
|
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
#import "Environment.h"
|
2014-11-19 21:17:53 +01:00
|
|
|
#import "TSStorageManager.h"
|
2014-11-21 14:38:37 +01:00
|
|
|
#import "ContactsManager.h"
|
2014-11-25 16:38:33 +01:00
|
|
|
#import "TSRecipient.h"
|
2014-11-19 21:17:53 +01:00
|
|
|
|
|
|
|
#define TSContactThreadPrefix @"c"
|
|
|
|
|
|
|
|
@implementation TSContactThread
|
|
|
|
|
|
|
|
- (instancetype)initWithContactId:(NSString*)contactId{
|
|
|
|
|
|
|
|
NSString *uniqueIdentifier = [[self class] threadIdFromContactId:contactId];
|
|
|
|
|
|
|
|
self = [super initWithUniqueId:uniqueIdentifier];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
+ (instancetype)threadWithContactId:(NSString*)contactId transaction:(YapDatabaseReadWriteTransaction*)transaction {
|
2014-11-19 21:17:53 +01:00
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
TSContactThread *thread = [self fetchObjectWithUniqueID:[self threadIdFromContactId:contactId] transaction:transaction];
|
2014-11-19 21:17:53 +01:00
|
|
|
|
|
|
|
if (!thread) {
|
|
|
|
thread = [[TSContactThread alloc] initWithContactId:contactId];
|
2014-11-21 14:38:37 +01:00
|
|
|
[thread saveWithTransaction:transaction];
|
2014-11-19 21:17:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
- (NSString*)contactIdentifier{
|
2014-11-19 21:17:53 +01:00
|
|
|
return [[self class]contactIdFromThreadId:self.uniqueId];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isGroupThread{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
- (NSString*)name{
|
|
|
|
NSString *contactId = [self contactIdentifier];
|
|
|
|
NSString *name = [[Environment getCurrent].contactsManager nameStringForPhoneIdentifier:contactId];
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
name = contactId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2014-11-25 19:06:09 +01:00
|
|
|
- (UIImage*)image{
|
|
|
|
UIImage *image = [[Environment getCurrent].contactsManager imageForPhoneIdentifier:self.contactIdentifier];
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2014-11-19 21:17:53 +01:00
|
|
|
+ (NSString*)threadIdFromContactId:(NSString*)contactId{
|
|
|
|
return [TSContactThreadPrefix stringByAppendingString:contactId];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString*)contactIdFromThreadId:(NSString*)threadId{
|
|
|
|
return [threadId substringWithRange:NSMakeRange(1, threadId.length-1)];
|
|
|
|
}
|
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
- (TSRecipient *)recipientWithTransaction:(YapDatabaseReadTransaction*)transaction{
|
|
|
|
return [TSRecipient recipientWithTextSecureIdentifier:self.contactIdentifier withTransaction:transaction];
|
|
|
|
}
|
|
|
|
|
2014-11-19 21:17:53 +01:00
|
|
|
@end
|