<commit_before>//
//  ShareHelper.swift
//  edX
//
//  Created by <NAME> on 11/25/15.
//  Copyright © 2015 edX. All rights reserved.
//

import Foundation

func shareTextAndALink(text: String, url: NSURL, analyticsCallback:((String) -> Void)?) -> UIActivityViewController {
    let items = [text, url] as [Any]
    return controllerWithItems(items: items as [AnyObject], analyticsCallback: analyticsCallback)
}

func shareHashtaggedTextAndALink(textBuilder: @escaping (_ hashtagOrPlatform: String) -> String, url: NSURL, utmParams:CourseShareUtmParameters, analyticsCallback:((String) -> Void)?) -> UIActivityViewController {
    let items = [PlatformHashTag(textBuilder: textBuilder), CourseShareURL(url: url, utmParams: utmParams)]
    return controllerWithItems(items: items, analyticsCallback: analyticsCallback)
}

private func controllerWithItems(items: [AnyObject], analyticsCallback:((String) -> Void)?) -> UIActivityViewController{
    let controller = UIActivityViewController(activityItems: items, applicationActivities: nil)
    controller.excludedActivityTypes = [UIActivityType.assignToContact, UIActivityType.print, UIActivityType.saveToCameraRoll]
    controller.completionWithItemsHandler = {activityType, completed, _, error in
        if let type = activityType, completed {
            let analyticsType: String
            switch type {
            case UIActivityType.postToTwitter:
                analyticsType = "twitter"
            case UIActivityType.postToFacebook:
                analyticsType = "facebook"
            default:
                analyticsType = "other"
            }
            analyticsCallback?(analyticsType)
        }
    }
    return controller

}

private class PlatformHashTag: NSObject, UIActivityItemSource {
    var config: OEXConfig { return OEXConfig.shared() }
    var platformName : String { return config.platformName() }

    let textBuilder: (_ hashtagOrPlatform: String) -> String

    init(textBuilder: @escaping (_ hashtagOrPlatform: String) -> String) {
        self.textBuilder = textBuilder
        super.init()
    }
    
    fileprivate func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return textBuilder(platformName)
    }

    //If this is going to Twitter and the hashtag has been defined in the configuration, use it otherwise use the platform name
    fileprivate func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
        var item = platformName
        if let hashTag = config.twitterConfiguration?.hashTag, activityType == UIActivityType.postToTwitter {
            item = hashTag
        }

        return textBuilder(item)
    }
}

// This class creates new course share url by adding utm parameters with it depending on activity type 
private class CourseShareURL: NSObject, UIActivityItemSource {
    
    let courseShareURL: NSURL
    let courseShareUtmParams: CourseShareUtmParameters
    
    init(url: NSURL, utmParams:CourseShareUtmParameters) {
        courseShareURL = url
        self.courseShareUtmParams = utmParams
    }
    
    fileprivate func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return courseShareURL
    }
    
    fileprivate func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
        
        var courseShareURLStringWithUtmParams: String?
        if activityType == UIActivityType.postToFacebook, let utmParams = courseShareUtmParams.facebook {
            courseShareURLStringWithUtmParams = String(format:"%@?%@",courseShareURL, utmParams)
        }
        else if activityType == UIActivityType.postToTwitter, let utmParams = courseShareUtmParams.twitter{
            courseShareURLStringWithUtmParams = String(format:"%@?%@",courseShareURL, utmParams)
        }
        
        return NSURL(string: courseShareURLStringWithUtmParams ?? "")
    }
}
<commit_msg>Course share URL is empty for all apps other than facebook and twitter (#1090)<commit_after>//
//  ShareHelper.swift
//  edX
//
//  Created by <NAME> on 11/25/15.
//  Copyright © 2015 edX. All rights reserved.
//

import Foundation

func shareTextAndALink(text: String, url: NSURL, analyticsCallback:((String) -> Void)?) -> UIActivityViewController {
    let items = [text, url] as [Any]
    return controllerWithItems(items: items as [AnyObject], analyticsCallback: analyticsCallback)
}

func shareHashtaggedTextAndALink(textBuilder: @escaping (_ hashtagOrPlatform: String) -> String, url: NSURL, utmParams:CourseShareUtmParameters, analyticsCallback:((String) -> Void)?) -> UIActivityViewController {
    let items = [PlatformHashTag(textBuilder: textBuilder), CourseShareURL(url: url, utmParams: utmParams)]
    return controllerWithItems(items: items, analyticsCallback: analyticsCallback)
}

private func controllerWithItems(items: [AnyObject], analyticsCallback:((String) -> Void)?) -> UIActivityViewController{
    let controller = UIActivityViewController(activityItems: items, applicationActivities: nil)
    controller.excludedActivityTypes = [UIActivityType.assignToContact, UIActivityType.print, UIActivityType.saveToCameraRoll]
    controller.completionWithItemsHandler = {activityType, completed, _, error in
        if let type = activityType, completed {
            let analyticsType: String
            switch type {
            case UIActivityType.postToTwitter:
                analyticsType = "twitter"
            case UIActivityType.postToFacebook:
                analyticsType = "facebook"
            default:
                analyticsType = "other"
            }
            analyticsCallback?(analyticsType)
        }
    }
    return controller

}

private class PlatformHashTag: NSObject, UIActivityItemSource {
    var config: OEXConfig { return OEXConfig.shared() }
    var platformName : String { return config.platformName() }

    let textBuilder: (_ hashtagOrPlatform: String) -> String

    init(textBuilder: @escaping (_ hashtagOrPlatform: String) -> String) {
        self.textBuilder = textBuilder
        super.init()
    }
    
    fileprivate func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return textBuilder(platformName)
    }

    //If this is going to Twitter and the hashtag has been defined in the configuration, use it otherwise use the platform name
    fileprivate func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
        var item = platformName
        if let hashTag = config.twitterConfiguration?.hashTag, activityType == UIActivityType.postToTwitter {
            item = hashTag
        }

        return textBuilder(item)
    }
}

// This class creates new course share url by adding utm parameters with it depending on activity type 
private class CourseShareURL: NSObject, UIActivityItemSource {
    
    let courseShareURL: NSURL
    let courseShareUtmParams: CourseShareUtmParameters
    
    init(url: NSURL, utmParams:CourseShareUtmParameters) {
        courseShareURL = url
        self.courseShareUtmParams = utmParams
    }
    
    fileprivate func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return courseShareURL
    }
    
    fileprivate func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
        
        var shareURL: String = courseShareURL.absoluteString ?? ""
        if activityType == UIActivityType.postToFacebook, let utmParams = courseShareUtmParams.facebook {
            shareURL = String(format:"%@?%@",courseShareURL, utmParams)
        }
        else if activityType == UIActivityType.postToTwitter, let utmParams = courseShareUtmParams.twitter{
            shareURL = String(format:"%@?%@",courseShareURL, utmParams)
        }
        
        return NSURL(string: shareURL)
    }
}

{'fraction_non_alphanumeric': 0.07990196078431372, 'fraction_numerical': 0.0029411764705882353, 'mean_word_length': 3.566871852266368, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3676601', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2124, 'n_words': 546}