You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.9 KiB

6 years ago
  1. // Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
  2. //
  3. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  4. // copy, modify, and distribute this software in source code or binary form for use
  5. // in connection with the web services and APIs provided by Facebook.
  6. //
  7. // As with any software that integrates with the Facebook platform, your use of
  8. // this software is subject to the Facebook Developer Principles and Policies
  9. // [http://developers.facebook.com/policy/]. This copyright notice shall be
  10. // included in all copies or substantial portions of the software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. import FBSDKShareKit
  19. /**
  20. A model for a game request.
  21. */
  22. public struct GameRequest {
  23. /**
  24. Used when defining additional context about the nature of the request.
  25. */
  26. public var actionType: ActionType?
  27. /**
  28. Additional freeform data you may pass for tracking. This will be stored as part of the request objects created.
  29. Maximum length is 255 characters.
  30. */
  31. public var data: String?
  32. /**
  33. This controls the set of friends someone sees if a multi-friend selector is shown.
  34. It is `.Default` by default, meaning that all friends can be shown.
  35. If specified as `HideUsers`, only friends who don't use the app will be shown.
  36. If specified as `HideNonUsers`, only friends who do use the app will be shown.
  37. */
  38. public var recipientsFilter: RecipientsFilter
  39. /**
  40. A plain-text message to be sent as part of the request.
  41. This text will surface in the App Center view of the request, but not on the notification jewel.
  42. */
  43. public var message: String
  44. /// The title for the dialog.
  45. public var title: String
  46. /**
  47. A set of user IDs, usernames or invite tokens of people to send the request to.
  48. These may or may not be a friend of the sender. If this is specified by the app, the sender will not have a choice
  49. of recipients. If not, the sender will see a multi-friend selector.
  50. */
  51. public var recipients: Set<Recipient>?
  52. /**
  53. An array of user IDs that will be included in the dialog as the first suggested friends.
  54. */
  55. public var recipientSuggestions: Set<Recipient>?
  56. /**
  57. Create a new game request content with a title and a message.
  58. - parameter title: The title for the dialog.
  59. - parameter message: The message to be sent as part of the request.
  60. */
  61. public init(title: String, message: String) {
  62. self.title = title
  63. self.message = message
  64. self.recipientsFilter = .none
  65. }
  66. }
  67. extension GameRequest: Equatable {
  68. /**
  69. Compare two `GameRequest`s for equality.
  70. - parameter lhs: The first request to compare.
  71. - parameter rhs: The second request to compare.
  72. - returns: Whether or not the requests are equal.
  73. */
  74. public static func == (lhs: GameRequest, rhs: GameRequest) -> Bool {
  75. return lhs.sdkContentRepresentation == rhs.sdkContentRepresentation
  76. }
  77. }
  78. extension GameRequest {
  79. internal var sdkContentRepresentation: FBSDKGameRequestContent {
  80. let sdkContent = FBSDKGameRequestContent()
  81. let sdkActionRepresentation = actionType?.sdkActionRepresentation ?? (.none, nil)
  82. sdkContent.actionType = sdkActionRepresentation.0
  83. sdkContent.objectID = sdkActionRepresentation.1
  84. sdkContent.data = data
  85. sdkContent.filters = recipientsFilter.sdkFilterRepresentation
  86. sdkContent.title = title
  87. sdkContent.message = message
  88. sdkContent.recipients = recipients?.map { $0.rawValue }
  89. sdkContent.recipientSuggestions = recipientSuggestions?.map { $0.rawValue }
  90. return sdkContent
  91. }
  92. }