robbiehanson / XMPPFramework

An XMPP Framework in Objective-C for Mac and iOS
Other
5.91k stars 2.09k forks source link

Bug report: message core data timestamp wrong if the server resent message timestamp is newer than the iOS current time #542

Open xhsoldier opened 9 years ago

xhsoldier commented 9 years ago

I have change the code in NSXMLElement+XEP_0203.m as followings:

- (NSDate *)delayedDeliveryDate
{
    NSDate* currentTime = [NSDate date];

    NSXMLElement *delay;

    // From XEP-0203 (Delayed Delivery)
    //
    // <delay xmlns='urn:xmpp:delay'
    //         from='juliet@capulet.com/balcony'
    //        stamp='2002-09-10T23:41:07Z'/>
    //
    // The format [of the stamp attribute] MUST adhere to the dateTime format
    // specified in XEP-0082 and MUST be expressed in UTC.

    delay = [self elementForName:@"delay" xmlns:@"urn:xmpp:delay"];
    if (delay)
    {
        NSString *stampValue = [delay attributeStringValueForName:@"stamp"];

        // There are other considerations concerning XEP-0082.
        // For example, it may optionally contain milliseconds.
        // And it may possibly express UTC as "+00:00" instead of "Z".
        //
        // Thankfully there is already an implementation that takes into account all these possibilities.

        NSDate* stamp = [XMPPDateTimeProfiles parseDateTime:stampValue];

        //xuhai, I changed this bug. If the server time is newer than the current time, we should use the current time.

        switch ([currentTime compare:stamp]){
            case NSOrderedAscending:
                NSLog(@"NSOrderedAscending");
                stamp = currentTime;
                break;

            default:
                break;
        }
        return stamp;
    }

    // From XEP-0091 (Legacy Delayed Delivery)
    //
    // <x xmlns='jabber:x:delay'
    //     from='capulet.com'
    //    stamp='20020910T23:08:25'>

    delay = [self elementForName:@"x" xmlns:@"jabber:x:delay"];
    if (delay)
    {
        NSDate *stamp;

        NSString *stampValue = [delay attributeStringValueForName:@"stamp"];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
        [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
        [dateFormatter setDateFormat:@"yyyyMMdd'T'HH:mm:ss"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

        stamp = [dateFormatter dateFromString:stampValue];

        switch ([currentTime compare:stamp]){
            case NSOrderedAscending:
                NSLog(@"NSOrderedAscending");
                stamp = currentTime;
                break;

            default:
                break;
        }

        return stamp;
    }

    return nil;
}
mukyasa commented 7 years ago

Did anyone know how to resolve this , even i am facing the same issue

nithinkmichael commented 7 years ago

Got any solution? I have this issue now.