DynamicSchema.hs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. {-# LANGUAGE Rank2Types #-}
  2. {-# LANGUAGE OverloadedStrings #-}
  3. {-|
  4. Module : Codec.ActivityStream.DynamicSchema
  5. Description : A (more dynamic) interface to the Activity Streams Base Schema
  6. Copyright : (c) Getty Ritter, 2014
  7. Maintainer : gdritter@galois.com
  8. This is an interface to the extended ActivityStreams schema which defines
  9. an extensive set of @verb@ values, additional @objectType@ values, and a
  10. set of extended properties for 'Object's.
  11. Most of the inline documentation is drawn directly from the
  12. <https://github.com/activitystreams/activity-schema/blob/master/activity-schema.md Activity Base Schema draft>
  13. specification, with minor modifications
  14. to refer to the corresponding data types in this module and to clarify
  15. certain aspects. This is not an approved draft, and as such may be
  16. subject to changes which will be reflected in this module. In contrast to
  17. "Codec.ActivityStream", the API in this module makes __no guarantees about
  18. long-term stability__.
  19. -}
  20. module Codec.ActivityStream.DynamicSchema
  21. ( module Codec.ActivityStream.Dynamic
  22. -- * Verbs
  23. , SchemaVerb(..)
  24. -- * Object Types
  25. , SchemaObjectType(..)
  26. -- ** Audio/Video
  27. , avEmbedCode
  28. , avStream
  29. -- ** Binary
  30. , bnCompression
  31. , bnData
  32. , bnFileUrl
  33. , bnLength
  34. , bnMd5
  35. , bnMimeType
  36. -- ** Event
  37. , evAttendedBy
  38. , evAttending
  39. , evEndTime
  40. , evInvited
  41. , evMaybeAttending
  42. , evNotAttendedBy
  43. , evNotAttending
  44. , evStartTime
  45. -- ** Issue
  46. , isTypes
  47. -- ** Permission
  48. , pmScope
  49. , pmActions
  50. -- ** Place
  51. , plPosition
  52. , plAddress
  53. -- *** PlacePosition
  54. , PlacePosition
  55. -- *** PlaceAddress
  56. , PlaceAddress
  57. -- ** Role/Group
  58. , rlMembers
  59. -- ** Task
  60. , tsActor
  61. , tsBy
  62. , tsObject
  63. , tsPrerequisites
  64. , tsRequired
  65. , tsSupersedes
  66. , tsVerb
  67. -- * Basic Extension Properties
  68. , acContext
  69. , getLocation
  70. , oMood
  71. , oRating
  72. , acResult
  73. , getSource
  74. , getStartTime
  75. , getEndTime
  76. , oTags
  77. -- * Mood
  78. , Mood
  79. , moodRest
  80. , moodDisplayName
  81. , moodImage
  82. ) where
  83. import qualified Data.Aeson as Aeson
  84. import Data.DateTime (DateTime)
  85. import Data.Aeson ( FromJSON(..), ToJSON(..) )
  86. import qualified Data.HashMap.Strict as HM
  87. import Data.Text (Text)
  88. import Codec.ActivityStream.LensInternal
  89. import Codec.ActivityStream.Dynamic
  90. import Codec.ActivityStream.Schema (SchemaVerb(..), SchemaObjectType(..))
  91. -- audio/video
  92. -- | A fragment of HTML markup that, when embedded within another HTML
  93. -- page, provides an interactive user-interface for viewing or listening
  94. -- to the video or audio stream.
  95. avEmbedCode :: Lens' Object (Maybe Text)
  96. avEmbedCode = makeAesonLensMb "embedCode" oRest
  97. -- | An Activity Streams Media Link to the video or audio content itself.
  98. avStream :: Lens' Object (Maybe MediaLink)
  99. avStream = makeAesonLensMb "stream" oRest
  100. -- binary
  101. -- | An optional token identifying a compression algorithm applied to
  102. -- the binary data prior to Base64-encoding. Possible algorithms
  103. -- are "deflate" and "gzip", respectively indicating the use of
  104. -- the compression mechanisms defined by RFC 1951 and RFC 1952.
  105. -- Additional compression algorithms MAY be used but are not defined
  106. -- by this specification. Note that previous versions of this
  107. -- specification allowed for multiple compression algorithms to be
  108. -- applied and listed using a comma-separated format. The use of
  109. -- multiple compressions is no longer permitted.
  110. bnCompression :: Lens' Object (Maybe Text)
  111. bnCompression = makeAesonLensMb "compression" oRest
  112. -- | The URL-Safe Base64-encoded representation of the binary data
  113. bnData :: Lens' Object (Maybe Text)
  114. bnData = makeAesonLensMb "data" oRest
  115. -- | An optional IRI for the binary data described by this object.
  116. bnFileUrl :: Lens' Object (Maybe Text)
  117. bnFileUrl = makeAesonLensMb "fileUrl" oRest
  118. -- | The total number of unencoded, uncompressed octets contained
  119. -- within the "data" field.
  120. bnLength :: Lens' Object (Maybe Text)
  121. bnLength = makeAesonLensMb "length" oRest
  122. -- | An optional MD5 checksum calculated over the unencoded,
  123. -- uncompressed octets contained within the "data" field
  124. bnMd5 :: Lens' Object (Maybe Text)
  125. bnMd5 = makeAesonLensMb "md5" oRest
  126. -- | The MIME Media Type of the binary data contained within the object.
  127. bnMimeType :: Lens' Object (Maybe Text)
  128. bnMimeType = makeAesonLensMb "mimeType" oRest
  129. -- event
  130. -- | A collection object as defined in Section 3.5 of the JSON
  131. -- Activity Streams specification that provides information about
  132. -- entities that attended the event.
  133. evAttendedBy :: Lens' Object (Maybe Collection)
  134. evAttendedBy = makeAesonLensMb "attendedBy" oRest
  135. -- | A collection object as defined in Section 3.5 of the JSON
  136. -- Activity Streams specification that provides information about
  137. -- entities that intend to attend the event.
  138. evAttending :: Lens' Object (Maybe Collection)
  139. evAttending = makeAesonLensMb "attending" oRest
  140. -- | The date and time that the event ends represented as a String
  141. -- conforming to the "date-time" production in [RFC3339].
  142. evEndTime :: Lens' Object (Maybe DateTime)
  143. evEndTime = makeAesonLensMb "endTime" oRest
  144. -- | A collection object as defined in Section 3.5 of the JSON
  145. -- Activity Streams specification that provides information about
  146. -- entities that have been invited to the event.
  147. evInvited :: Lens' Object (Maybe Collection)
  148. evInvited = makeAesonLensMb "invited" oRest
  149. -- | A collection object as defined in Section 3.5 of the JSON
  150. -- Activity Streams specification that provides information about
  151. -- entities that possibly may attend the event.
  152. evMaybeAttending :: Lens' Object (Maybe Collection)
  153. evMaybeAttending = makeAesonLensMb "maybeAttending" oRest
  154. -- | A collection object as defined in Section 3.5 of the JSON
  155. -- Activity Streams specification that provides information about
  156. -- entities that did not attend the event.
  157. evNotAttendedBy :: Lens' Object (Maybe Collection)
  158. evNotAttendedBy = makeAesonLensMb "notAttendedBy" oRest
  159. -- | A collection object as defined in Section 3.5 of the JSON
  160. -- Activity Streams specification that provides information about
  161. -- entities that do not intend to attend the event.
  162. evNotAttending :: Lens' Object (Maybe Collection)
  163. evNotAttending = makeAesonLensMb "notAttending" oRest
  164. -- | The date and time that the event begins represented as a String
  165. -- confirming to the "date-time" production in RFC 3339.
  166. evStartTime :: Lens' Object (Maybe DateTime)
  167. evStartTime = makeAesonLensMb "startTime" oRest
  168. -- issue
  169. -- | An array of one or more absolute IRI's that describe the type of
  170. -- issue represented by the object. Note that the IRI's are intended
  171. -- for use as identifiers and MAY or MAY NOT be dereferenceable.
  172. isTypes :: Lens' Object (Maybe [Text])
  173. isTypes = makeAesonLensMb "types" oRest
  174. -- permission
  175. -- | A single Activity Streams Object, of any objectType, that
  176. -- identifies the scope of the permission. For example, if the
  177. -- permission objects describes write permissions for a given file,
  178. -- the scope property would be a file object describing that file.
  179. pmScope :: Lens' Object (Maybe Object)
  180. pmScope = makeAesonLensMb "scope" oRest
  181. -- | An array of Strings that identify the specific actions associated
  182. -- with the permission. The actions are application and scope
  183. -- specific. No common, core set of actions is defined by this
  184. -- specification.
  185. pmActions :: Lens' Object (Maybe [Text])
  186. pmActions = makeAesonLensMb "actions" oRest
  187. -- place
  188. -- | The latitude, longitude and altitude of the place as a point on
  189. -- Earth. Represented as a JSON Object as described below.
  190. plPosition :: Lens' Object (Maybe PlacePosition)
  191. plPosition = makeAesonLensMb "position" oRest
  192. -- | A physical address represented as a JSON object as described below.
  193. plAddress :: Lens' Object (Maybe PlaceAddress)
  194. plAddress = makeAesonLensMb "address" oRest
  195. data PlacePosition = PPO { fromPPO :: Aeson.Object } deriving (Eq, Show)
  196. instance FromJSON PlacePosition where
  197. parseJSON (Aeson.Object o)
  198. | HM.member "altitude" o
  199. && HM.member "latitude" o
  200. && HM.member "longitude" o = return (PPO o)
  201. | otherwise = fail "..."
  202. parseJSON _ = fail "..."
  203. instance ToJSON PlacePosition where
  204. toJSON = Aeson.Object . fromPPO
  205. data PlaceAddress = PAO { fromPAO :: Aeson.Object } deriving (Eq, Show)
  206. instance FromJSON PlaceAddress where
  207. parseJSON (Aeson.Object o)
  208. | HM.member "formatted" o
  209. && HM.member "streetAddress" o
  210. && HM.member "locality" o
  211. && HM.member "region" o
  212. && HM.member "postalCode" o
  213. && HM.member "country" o = return (PAO o)
  214. | otherwise = fail "..."
  215. parseJSON _ = fail "..."
  216. instance ToJSON PlaceAddress where
  217. toJSON = Aeson.Object . fromPAO
  218. -- role/group
  219. -- | An optional Activity Streams Collection object listing the
  220. -- members of a group, or listing the entities assigned to a
  221. -- particular role.
  222. rlMembers :: Lens' Object (Maybe [Object])
  223. rlMembers = makeAesonLensMb "members" oRest
  224. -- Task
  225. -- | An Activity Streams Object that provides information about the
  226. -- actor that is expected to complete the task.
  227. tsActor :: Lens' Object (Maybe Object)
  228. tsActor = makeAesonLensMb "actor" oRest
  229. -- | A RFC 3339 date-time specifying the date and time by which the
  230. -- task is to be completed.
  231. tsBy :: Lens' Object (Maybe DateTime)
  232. tsBy = makeAesonLensMb "by" oRest
  233. -- | An Activity Streams object describing the object of the task.
  234. tsObject :: Lens' Object (Maybe Object)
  235. tsObject = makeAesonLensMb "object" oRest
  236. -- | An Array of other Task objects that are to be completed before
  237. -- this task can be completed.
  238. tsPrerequisites :: Lens' Object (Maybe [Object])
  239. tsPrerequisites = makeAesonLensMb "prerequisites" oRest
  240. -- | A boolean value indicating whether completion of this task is
  241. -- considered to be mandatory.
  242. tsRequired :: Lens' Object (Maybe Bool)
  243. tsRequired = makeAesonLensMb "required" oRest
  244. -- | An Array of other Task objects that are superseded by this task object.
  245. tsSupersedes :: Lens' Object (Maybe [Object])
  246. tsSupersedes = makeAesonLensMb "supersedes" oRest
  247. -- | A string indicating the verb for this task as defined in Section
  248. -- 3.2 of [activitystreams].
  249. tsVerb :: Lens' Object (Maybe SchemaVerb)
  250. tsVerb = makeAesonLensMb "verb" oRest
  251. -- extra properties
  252. -- | The additional @context@ property allows an 'Activity' to further
  253. -- include information about why a particular action occurred by
  254. -- providing details about the context within which a particular
  255. -- Activity was performed. The value of the @context@ property is an
  256. -- 'Object' of any @objectType@. The meaning of the @context@ property is
  257. -- only defined when used within an 'Activity' object.
  258. acContext :: Lens' Activity (Maybe Object)
  259. acContext = makeAesonLensMb "context" acRest
  260. -- | When appearing within an activity, the location data indicates
  261. -- the location where the activity occurred. When appearing within an
  262. -- object, the location data indicates the location of that object at
  263. -- the time the activity occurred.
  264. getLocation :: Lens' a Aeson.Object -> Lens' a (Maybe Object)
  265. getLocation = makeAesonLensMb "location"
  266. -- | Mood describes the mood of the user when the activity was
  267. -- performed. This is usually collected via an extra field in the user
  268. -- interface used to perform the activity. For the purpose of the
  269. -- schema, a mood is a freeform, short mood keyword or phrase along
  270. -- with an optional mood icon image.
  271. oMood :: Lens' Object (Maybe Mood)
  272. oMood = makeAesonLensMb "mood" oRest
  273. -- | A rating given as a number between 1.0 and 5.0 inclusive with one
  274. -- decimal place of precision. Represented in JSON as a property
  275. -- called @rating@ whose value is a JSON number giving the rating.
  276. oRating :: Lens' Object (Maybe Double)
  277. oRating = makeAesonLensMb "rating" oRest
  278. -- | The @result@ provides a description of the result of any particular
  279. -- activity. The value of the @result@ property is an Object of any
  280. -- objectType. The meaning of the @result@ property is only defined when
  281. -- used within an 'Activity' object.
  282. acResult :: Lens' Activity (Maybe Object)
  283. acResult = makeAesonLensMb "result" acRest
  284. -- | The @source@ property provides a reference to the original source of
  285. -- an object or activity. The value of the @source@ property is an
  286. -- Object of any objectType.
  287. --
  288. -- The @source@ property is closely related to
  289. -- the @generator@ and @provider@ properties but serves the distinct
  290. -- purpose of identifying where the activity or object was originally
  291. -- published as opposed to identifying the applications that generated
  292. -- or published it.
  293. getSource :: Lens' a Aeson.Object -> Lens' a (Maybe Object)
  294. getSource = makeAesonLensMb "source"
  295. -- | When an long running Activity occurs over a distinct period of
  296. -- time, or when an Object represents a long-running process or event,
  297. -- the @startTime@ propertiy can be used to specify the
  298. -- date and time at which the activity or object begins.
  299. -- The values for each are represented as JSON Strings
  300. -- conforming to the "date-time" production in RFC3339.
  301. getStartTime :: Lens' a Aeson.Object -> Lens' a (Maybe Text)
  302. getStartTime = makeAesonLensMb "startTime"
  303. -- | When an long running Activity occurs over a distinct period of
  304. -- time, or when an Object represents a long-running process or event,
  305. -- the @endTime@ propertiy can be used to specify the
  306. -- date and time at which the activity or object concludes.
  307. -- The values for each are represented as JSON Strings
  308. -- conforming to the "date-time" production in RFC3339.
  309. getEndTime :: Lens' a Aeson.Object -> Lens' a (Maybe Text)
  310. getEndTime = makeAesonLensMb "endTime"
  311. -- | A listing of the objects that have been associated with a
  312. -- particular object. Represented in JSON using a property named @tags@
  313. -- whose value is an Array of objects.
  314. oTags :: Lens' Object (Maybe [Object])
  315. oTags = makeAesonLensMb "tags" oRest
  316. -- mood
  317. -- | Mood describes the mood of the user when the activity was
  318. -- performed. This is usually collected via an extra field in the user
  319. -- interface used to perform the activity. For the purpose of this
  320. -- schema, a mood is a freeform, short mood keyword or phrase along
  321. -- with an optional mood icon image.
  322. data Mood = Mood { fromMood :: Aeson.Object } deriving (Eq, Show)
  323. instance FromJSON Mood where
  324. parseJSON (Aeson.Object o)
  325. | HM.member "displayName" o
  326. && HM.member "image" o = return (Mood o)
  327. | otherwise = fail "..."
  328. parseJSON _ = fail "..."
  329. instance ToJSON Mood where
  330. toJSON = Aeson.Object . fromMood
  331. -- | Access to the underlying JSON object of a 'Mood'
  332. moodRest :: Lens' Mood Aeson.Object
  333. moodRest = makeLens fromMood (\ o' m -> m { fromMood = o' })
  334. -- | The natural-language, human-readable and plain-text keyword or
  335. -- phrase describing the mood. HTML markup MUST NOT be included.
  336. moodDisplayName :: Lens' Mood Text
  337. moodDisplayName = makeAesonLens "displayName" moodRest
  338. -- | An optional image that provides a visual representation of the mood.
  339. moodImage :: Lens' Mood MediaLink
  340. moodImage = makeAesonLens "image" moodRest