Advertisement
Guest User

Untitled

a guest
May 25th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (nullable __kindof VFDIncomingDialogPresentableModel *)dialogPresentableModelFromDialog:(nullable VFDDialog *)aDialog {
  2.     __kindof VFDIncomingDialogPresentableModel * returned = nil;
  3.     guardvar(VFDDialog, dialog, aDialog, else { return nil; })
  4.     guardvar(VFDMessage, message, dialog.lastMessage, else { return nil; })
  5.     guardvar(NSNumber, lastMessageIdentifier, message.identifier, else { return nil; })
  6.     VFDDialogType type = dialog.type;
  7.     guardvar(NSNumber, dialogIdentifier, dialog.identifier, else { return nil; })
  8.     guardvar(NSNumber, userIdentifier, dialog.userIdentifier, else { return nil; })
  9.     VFDUser * _Nullable user = nil;
  10.     VFDGroup * _Nullable group = nil;
  11.     if (userIdentifier.integerValue > 0) {
  12.         user = [VFDUser objectForPrimaryKey:userIdentifier];
  13.         if (user == nil) { return nil; }
  14.     } else {
  15.         group = [VFDGroup objectForPrimaryKey:userIdentifier];
  16.         if (group == nil) {
  17.             group = [VFDGroup objectForPrimaryKey:@(-userIdentifier.integerValue)];
  18.             if (group == nil) { return nil; }
  19.         }
  20.     }
  21.     /********************************* title *********************************/
  22.     NSString *title = nil;
  23.     switch (type) {
  24.         case VFDDialogTypeSingleWithUser: {
  25.             guard(user != nil) else { return nil; }
  26.             title = user.fullName;
  27.             break;
  28.         }
  29.         case VFDDialogTypeSingleWithGroup: {
  30.             guard(group != nil) else { return nil; }
  31.             title = group.name;
  32.             break;
  33.         }
  34.         case VFDDialogTypeConference: {
  35.             title = dialog.title;
  36.             break;
  37.         }
  38.         default: return nil;
  39.     }
  40.     guard([title isKindOfClass:NSString.class]) else { return nil; }
  41.     /********************************* date *********************************/
  42.     guardvar(NSNumber, date, message.date, else { return nil; })
  43.    
  44.     /********************************* unreadCount *********************************/
  45.     NSNumber *unreadCount = nil;
  46.     if (isKindOfNumber(dialog.unreadCount)) {
  47.         unreadCount = dialog.unreadCount;
  48.     }
  49.     /********************************* text/isHighlightText *********************************/
  50.     NSString *text = nil;
  51.     BOOL isHighlightText = NO;
  52.     if (type == VFDDialogTypeConference) {
  53.         if ([message.action isKindOfClass:VFDMessageAction.class]) {
  54.             VFDMessageAction * _Nonnull messageAction = message.action;
  55.             switch (messageAction.action) {
  56.                 default: return nil;
  57.                 case VFDMessageActionTypePhotoUpdate:
  58.                 case VFDMessageActionTypePhotoRemove:
  59.                     guard(user != nil) else { return nil; }
  60.                     text = [NSString stringWithFormat:@"%@ %@", user.fullName, VFDMessageActionTypeToHumanReadableFormat(messageAction.action, VFDSexTypeIsMale(user.sex), NO)];
  61.                     break;
  62.                 case VFDMessageActionTypeCreate:
  63.                 case VFDMessageActionTypeTitleUpdate:
  64.                     guard([messageAction.text isKindOfClass:NSString.class]) else { return nil; }
  65.                     guard(user != nil) else { return nil; }
  66.                     text = [NSString stringWithFormat:@"%@ %@ '%@'", user.fullName, VFDMessageActionTypeToHumanReadableFormat(messageAction.action, VFDSexTypeIsMale(user.sex), NO), messageAction.text];
  67.                     break;
  68.                 case VFDMessageActionTypeInviteUser:
  69.                 case VFDMessageActionTypeKickUser:
  70.                 case VFDMessageActionTypeChatPinMessage:
  71.                 case VFDMessageActionTypeChatUnpinMessage:
  72.                     guard([messageAction.mid isKindOfClass:NSNumber.class]) else { return nil; }
  73.                     VFDUser *usedUser = [VFDUser objectForPrimaryKey:messageAction.mid];
  74.                     guard(usedUser != nil) else { return nil; }
  75.                     guard(user != nil) else { return nil; }
  76.                     if ([user.id isEqualToNumber:usedUser.id]) {
  77.                         text = [NSString stringWithFormat:@"%@ %@", user.fullName, VFDMessageActionTypeToHumanReadableFormat(messageAction.action,
  78.                                                                                                                              VFDSexTypeIsMale(user.sex),
  79.                                                                                                                              YES)];
  80.                     } else {
  81.                         text = [NSString stringWithFormat:@"%@ %@ %@", user.fullName, VFDMessageActionTypeToHumanReadableFormat(messageAction.action,
  82.                                                                                                                                 VFDSexTypeIsMale(user.sex),
  83.                                                                                                                                 NO),
  84.                                 usedUser.fullName];
  85.                     }
  86.                     break;
  87.             }
  88.             isHighlightText = YES;
  89.         }
  90.     }
  91.     if (message.forwardedMessages.count > 0) {
  92.         text = @"Пересланное сообщение";
  93.         isHighlightText = YES;
  94.     }
  95.     if (message.geoAttachment != nil) {
  96.         text = @"Местоположение";
  97.         isHighlightText = YES;
  98.     }
  99.     if (!isHighlightText) {
  100.         if (message.attachments.count > 0) {
  101.             guardvar(VFDAttachment, attachment, message.attachments.firstObject, else { return nil; })
  102.             switch (attachment.type) {
  103.                 case VFDAttachmentTypePhoto:
  104.                     text = @"Фотография";
  105.                     break;
  106.                 case VFDAttachmentTypeVideo:
  107.                     text = @"Видеозапись";
  108.                     break;
  109.                 case VFDAttachmentTypeAudio:
  110.                     text = @"Аудиозапись";
  111.                     break;
  112.                 case VFDAttachmentTypeDoc:
  113.                     text = @"Документ";
  114.                     if (isKindOf(attachment.doc, VFDDocAttachment)) {
  115.                         if (isKindOf(attachment.doc.voiceMessage, VFDAudioDocAttachment)) {
  116.                             text = @"Аудиосообщение";
  117.                         }
  118.                     }
  119.                     break;
  120.                 case VFDAttachmentTypeWall:
  121.                     text = @"Запись со стены";
  122.                     break;
  123.                 case VFDAttachmentTypeSticker:
  124.                     text = @"Стикер";
  125.                     break;
  126.                 case VFDAttachmentTypeLink:
  127.                     text = @"Ссылка";
  128.                     break;
  129.                 case VFDAttachmentTypeMarket:
  130.                     text = @"Товар";
  131.                     break;
  132.                 case VFDAttachmentTypeGift:
  133.                     text = @"Подарок";
  134.                     break;
  135.                 default:
  136.                     text = @"Вложение";
  137.                     break;
  138.             }
  139.             isHighlightText = YES;
  140.         }
  141.     }
  142.     if (isKindOfString(message.text) && message.text.length > 0) {
  143.         text = message.text;
  144.         isHighlightText = NO;
  145.     }
  146.     guard([text isKindOfClass:NSString.class]) else { return nil; }
  147.     /********************************* isOut *********************************/
  148.     guardvar(NSNumber, isOut, message.isOut, else { return nil; })
  149.     /********************************* isRead *********************************/
  150.     guardvar(NSNumber, isRead, message.readState, else { return nil; })
  151.     /********************************* onlineStatus *********************************/
  152.     VFDDialogPresentableModelOnlineStatus onlineStatus = VFDDialogPresentableModelOnlineStatusMake(NO, NO, 0);
  153.     if (type == VFDDialogTypeSingleWithUser) {
  154.         if (user != nil) {
  155.             if ([user.online isKindOfClass:NSNumber.class]) {
  156.                 if ([user.last_seen isKindOfClass:VFDLastSeen.class]) {
  157.                     NSTimeInterval time = 0;
  158.                     if ([user.last_seen.time isKindOfClass:NSNumber.class]) {
  159.                         time = user.last_seen.time.doubleValue;
  160.                     }
  161.                     if ([user.last_seen.platform isKindOfClass:NSNumber.class]) {
  162.                         BOOL fromMobile = onlineFromMobileFromVFDLastSeenPlatformType(user.last_seen.platform.unsignedIntegerValue);
  163.                         onlineStatus = VFDDialogPresentableModelOnlineStatusMake(user.online.boolValue, fromMobile , time);
  164.                     }
  165.                 }
  166.             }
  167.         }
  168.     }
  169.     /********************************* avatarURL *********************************/
  170.     NSURL * _Nullable avatarURL = nil;
  171.     NSString * _Nullable avatarURLString = nil;
  172.     switch (type) {
  173.         case VFDDialogTypeSingleWithUser:
  174.             if (user != nil) {
  175.                 avatarURLString = user.photo_100 ? user.photo_100 : user.photo_50;
  176.             }
  177.             break;
  178.         case VFDDialogTypeSingleWithGroup:
  179.             if (group != nil) {
  180.                 avatarURLString = group.photo_100 ? group.photo_100 : group.photo_50;
  181.             }
  182.             break;
  183.         case VFDDialogTypeConference:
  184.             avatarURLString = dialog.photo100 ? dialog.photo100 : dialog.photo50;
  185.             break;
  186.         default: return nil;
  187.     }
  188.     if ([avatarURLString isKindOfClass:NSString.class]) {
  189.         avatarURL = [NSURL URLWithString:avatarURLString];
  190.     }
  191.     /********************************* avatarURL *********************************/
  192.     NSNumber * _Nonnull membersCount;
  193.     if (type == VFDDialogTypeConference) {
  194.         safeBind(NSNumber, membersCount, dialog.membersCount) else { return nil; }
  195.     }
  196.     /********************************* answerAvatarURL *********************************/
  197.     NSURL * _Nullable answerAvatarURL = nil;
  198.     NSString * _Nullable answerAvatarURLString = nil;
  199.     switch (type) {
  200.         case VFDDialogTypeSingleWithUser:
  201.             if (isOut) {
  202.                 if ([VKSERVICE.currentUser isKindOfClass:VFDCurrentUser.class]) {
  203.                     if ([VKSERVICE.currentUser.user isKindOfClass:VFDUser.class]) {
  204.                         answerAvatarURLString = VKSERVICE.currentUser.user.photo_50;
  205.                     }
  206.                 }
  207.             } else {
  208.                 if (user != nil) {
  209.                     answerAvatarURLString = user.photo_50;
  210.                 }
  211.             }
  212.             break;
  213.         case VFDDialogTypeSingleWithGroup:
  214.             if (isOut) {
  215.                 if ([VKSERVICE.currentUser isKindOfClass:VFDCurrentUser.class]) {
  216.                     if ([VKSERVICE.currentUser.user isKindOfClass:VFDUser.class]) {
  217.                         answerAvatarURLString = VKSERVICE.currentUser.user.photo_50;
  218.                     }
  219.                 }
  220.             } else {
  221.                 if (group != nil) {
  222.                     answerAvatarURLString = group.photo_50;
  223.                 }
  224.             }
  225.             break;
  226.         case VFDDialogTypeConference:
  227.             if (user != nil) {
  228.                 answerAvatarURLString = user.photo_50;
  229.             }
  230.             break;
  231.         default: return nil;
  232.     }
  233.     if ([answerAvatarURLString isKindOfClass:NSString.class]) {
  234.         answerAvatarURL = [NSURL URLWithString:answerAvatarURLString];
  235.     }
  236.     /********************************* creatings new values *********************************/
  237.     switch (type) {
  238.         case VFDDialogTypeSingleWithUser:
  239.         case VFDDialogTypeSingleWithGroup:
  240.             returned = (isOut.boolValue) ? [VFDOutgoingDialogPresentableModel new] : [VFDIncomingDialogPresentableModel new];
  241.             break;
  242.         case VFDDialogTypeConference:
  243.             returned  = [VFDConferenceDialogPresentableModel new];
  244.             break;
  245.         default: return nil;
  246.     }
  247.     /********************************* binding *********************************/
  248.     if ([returned respondsToSelector:@selector(setAnswerAvatarURL:)]) {
  249.         [(VFDOutgoingDialogPresentableModel *)returned setAnswerAvatarURL:answerAvatarURL];
  250.     }
  251.     if ([returned respondsToSelector:@selector(setMembersCount:)]) {
  252.         ((VFDConferenceDialogPresentableModel *)returned).membersCount = membersCount;
  253.     }
  254.     returned.lastMessageIdentifier = lastMessageIdentifier;
  255.     returned.type = type;
  256.     returned.dialogIdentifier = dialogIdentifier;
  257.     returned.title = title;
  258.     returned.date = date;
  259.     returned.text = text;
  260.     returned.isHighlightText = isHighlightText;
  261.     returned.isRead = isRead.boolValue;
  262.     returned.unreadCount = unreadCount;
  263.     returned.onlineStatus = onlineStatus;
  264.     returned.avatarURL = avatarURL;
  265.     return returned;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement