audience-kit 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,632 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Audience
4
+ module Kit
5
+ # The GraphQL documents behind {Operations}: the same operations the Swift, Kotlin, Node, Python and PHP SDKs
6
+ # run. Documents with module fields take the audience's {SchemaShape}: the API leaves a module's fields out of an
7
+ # audience that hasn't installed it, and asking for one fails the whole operation.
8
+ #
9
+ # Run your own operations with {Session#graphql}; the field lists here (+Queries.event_fields(shape)+,
10
+ # {ADMISSION_FIELDS}...) are there to build them from.
11
+ module Queries
12
+ # A selection of fields, for documents of your own.
13
+ PAGE_FIELDS = "id name photoUrl bannerUrl"
14
+ # A tag on a venue, person or event: its name and chip color.
15
+ TAG_FIELDS = "id name color"
16
+ # A selection of fields, for documents of your own.
17
+ MONEY_FIELDS = "cents currency"
18
+ # A selection of fields, for documents of your own.
19
+ PROFILE_FIELDS = "id name avatarUrl"
20
+ # A cover charge: the amount, fee and total, its coat check, and on a sliding scale the range to pick from.
21
+ COVER_CHARGE_FIELDS = "amount { cents currency } total { cents currency } fee { cents currency } " \
22
+ "night from payable event { id name } " \
23
+ "coatCheck { amount { cents currency } fee { cents currency } total { cents currency } " \
24
+ "night payable } " \
25
+ "scale { min { cents currency } max { cents currency } suggested { cents currency } }"
26
+ # An admission: the pass secret the door code is made from, and the buyer's profile for the door.
27
+ ADMISSION_FIELDS = "id status kind night quantity tier claimCheckNumber amount { cents currency } " \
28
+ "fee { cents currency } total { cents currency } isRefundable passSecret scanCount " \
29
+ "checkedInAt createdAt profile { id name avatarUrl } venue { id name photoUrl } " \
30
+ "event { id name }"
31
+ # An event's rsvp for the signed-in person: how many are going, and their own answer and why.
32
+ RSVP_FIELDS = "count mine mineSource"
33
+ # A selection of fields, for documents of your own.
34
+ EVENT_ATTENDEE_FIELDS = "profile { id name avatarUrl } friend source"
35
+ # A selection of fields, for documents of your own.
36
+ ADMISSION_OFFER_FIELDS = "price { cents currency } url seller { key name }"
37
+ # A selection of fields, for documents of your own.
38
+ CHAT_MESSAGE_FIELDS = "id body message name userId avatarUrl photoUrl sentAt role kind title pinned"
39
+ # A selection of fields, for documents of your own.
40
+ PURCHASE_FIELDS = "provider paymentIntentClientSecret publishableKey stripeAccountId squareApplicationId " \
41
+ "squareLocationId"
42
+
43
+ # The types whose fields the listing documents depend on.
44
+ LISTING_TYPES = %w[Locale ListingProfile Venue Location Person Event].freeze
45
+ # The types the event documents depend on.
46
+ EVENT_TYPES = (LISTING_TYPES + %w[EventSeries EventTickets EventPeopleView]).freeze
47
+ # The types +venue_cover+ and +venue_offers+ depend on.
48
+ VENUE_TICKET_TYPES = %w[Venue VenueTickets].freeze
49
+ # The types the venue page document depends on.
50
+ VENUE_DETAIL_TYPES = EVENT_TYPES
51
+ # The types the Soapbox documents depend on: the root's +soapbox+ and each listing's +articles+.
52
+ SOAPBOX_TYPES = %w[Audience Venue Event Person Locale Article Publication].freeze
53
+
54
+ # A selection of fields, for documents of your own.
55
+ PUBLICATION_SUMMARY_FIELDS = "id name photoUrl trusted acceptsOpEds"
56
+ # A selection of fields, for documents of your own.
57
+ ARTICLE_FIELDS = "id slug title summary byline kind publishedAt edited pinned teaser canonicalUrl " \
58
+ "disclosure heroPhotoUrl heroAltText publication { #{PUBLICATION_SUMMARY_FIELDS} }".freeze
59
+ # A selection of fields, for documents of your own.
60
+ ARTICLE_SUBJECT_FIELDS = "venues { id name } events { id name } people { id name } locales { id name }"
61
+ # A selection of fields, for documents of your own.
62
+ PUBLICATION_FIELDS = "id name masthead about websiteUrl photoUrl trusted acceptsOpEds opEdGuidelines " \
63
+ "locale { id name } person { id name }"
64
+ # A selection of fields, for documents of your own.
65
+ CALENDAR_FEED_FIELDS = "url webcalUrl googleUrl venues { id }"
66
+
67
+ # The +query Audience+ document.
68
+ AUDIENCE = <<~GRAPHQL
69
+ query Audience {
70
+ id name subdomain theme { preset tagline light { name value } dark { name value } }
71
+ }
72
+ GRAPHQL
73
+
74
+ # The +query Me+ document.
75
+ ME = <<~GRAPHQL
76
+ query Me {
77
+ me { self { id name firstName lastName emailAddress isAdmin presence } }
78
+ }
79
+ GRAPHQL
80
+
81
+ # The +mutation RSVPEvent+ document.
82
+ RSVP_EVENT = <<~GRAPHQL
83
+ mutation RSVPEvent($eventId: ID!, $state: RSVPState!) {
84
+ rsvpEvent(input: { eventId: $eventId, state: $state }) { event { id rsvp { count mine mineSource } } }
85
+ }
86
+ GRAPHQL
87
+
88
+ # Takes back the signed-in person's RSVP, as if they'd never answered.
89
+ CANCEL_RSVP = <<~GRAPHQL
90
+ mutation CancelRSVP($eventId: ID!) {
91
+ cancelRsvp(input: { eventId: $eventId }) { event { id rsvp { count mine mineSource } } }
92
+ }
93
+ GRAPHQL
94
+
95
+ # Who's going to an event: the counts (staff-only ones too) and the first +$first+ attendees.
96
+ EVENT_RSVP = <<~GRAPHQL
97
+ query EventRSVP($id: ID!, $first: Int) {
98
+ events {
99
+ event(id: $id) {
100
+ id
101
+ rsvp {
102
+ count mine mineSource counts { going interested tickets }
103
+ attendees(first: $first) { nodes { profile { id name avatarUrl } friend source } }
104
+ }
105
+ }
106
+ }
107
+ }
108
+ GRAPHQL
109
+
110
+ # The tribes people can identify with here, and the signed-in person's own.
111
+ TRIBES = <<~GRAPHQL
112
+ query Tribes {
113
+ id tribes: tags(tribes: true) { id name color tribe }
114
+ me { self { id tribes { isPrivate tag { id name color tribe } } } }
115
+ }
116
+ GRAPHQL
117
+
118
+ # The +mutation IdentifyWithTribe+ document.
119
+ IDENTIFY_WITH_TRIBE = <<~GRAPHQL
120
+ mutation IdentifyWithTribe($tagId: ID!, $isPrivate: Boolean) {
121
+ identifyWithTribe(input: { tagId: $tagId, isPrivate: $isPrivate }) {
122
+ tribes { isPrivate tag { id name color tribe } }
123
+ }
124
+ }
125
+ GRAPHQL
126
+
127
+ # The +mutation LeaveTribe+ document.
128
+ LEAVE_TRIBE = <<~GRAPHQL
129
+ mutation LeaveTribe($tagId: ID!) {
130
+ leaveTribe(input: { tagId: $tagId }) { tribes { isPrivate tag { id name color tribe } } }
131
+ }
132
+ GRAPHQL
133
+
134
+ # The +mutation RegisterDevice+ document.
135
+ REGISTER_DEVICE = <<~GRAPHQL
136
+ mutation RegisterDevice($notificationToken: String!, $appId: String, $sandbox: Boolean) {
137
+ registerDevice(input: { notificationToken: $notificationToken, appId: $appId, sandbox: $sandbox }) { registered }
138
+ }
139
+ GRAPHQL
140
+
141
+ # The +query Node+ document.
142
+ NODE = <<~GRAPHQL
143
+ query Node($id: ID!) {
144
+ node(id: $id) { __typename id }
145
+ }
146
+ GRAPHQL
147
+
148
+ # The +mutation SendMyChatMessage+ document.
149
+ SEND_CHAT_MESSAGE = <<~GRAPHQL.freeze
150
+ mutation SendMyChatMessage($input: ChatMessageSendOwnInput!) {
151
+ sendMyChatMessage(input: $input) { message { #{CHAT_MESSAGE_FIELDS} } }
152
+ }
153
+ GRAPHQL
154
+
155
+ # The +mutation RemoveMyChatMessage+ document.
156
+ REMOVE_CHAT_MESSAGE = <<~GRAPHQL
157
+ mutation RemoveMyChatMessage($messageId: ID!) {
158
+ removeMyChatMessage(input: { messageId: $messageId }) { deletedId }
159
+ }
160
+ GRAPHQL
161
+
162
+ # Deletes the signed-in person's account for good.
163
+ DELETE_ACCOUNT = <<~GRAPHQL
164
+ mutation DeleteAccount($confirm: Boolean!) {
165
+ deleteAccount(input: { confirm: $confirm }) { deleted }
166
+ }
167
+ GRAPHQL
168
+
169
+ # Files a report with the audience's admins, blocking the person behind it when +$block+.
170
+ REPORT_CONTENT = <<~GRAPHQL
171
+ mutation ReportContent($targetType: ReportTarget!, $targetId: ID!, $reason: String, $block: Boolean) {
172
+ reportContent(input: { targetType: $targetType, targetId: $targetId, reason: $reason, block: $block }) {
173
+ reported
174
+ blocked
175
+ }
176
+ }
177
+ GRAPHQL
178
+
179
+ # The +mutation BlockUser+ document.
180
+ BLOCK_USER = <<~GRAPHQL
181
+ mutation BlockUser($userId: ID!) {
182
+ blockUser(input: { userId: $userId }) { blockedUsers { id } }
183
+ }
184
+ GRAPHQL
185
+
186
+ # The +mutation UnblockUser+ document.
187
+ UNBLOCK_USER = <<~GRAPHQL
188
+ mutation UnblockUser($userId: ID!) {
189
+ unblockUser(input: { userId: $userId }) { blockedUsers { id } }
190
+ }
191
+ GRAPHQL
192
+
193
+ # The +query CalendarFeed+ document.
194
+ CALENDAR_FEED = <<~GRAPHQL.freeze
195
+ query CalendarFeed {
196
+ me { calendarFeed { #{CALENDAR_FEED_FIELDS} } }
197
+ }
198
+ GRAPHQL
199
+
200
+ # The +mutation CreateCalendarFeed+ document.
201
+ CREATE_CALENDAR_FEED = <<~GRAPHQL.freeze
202
+ mutation CreateCalendarFeed($reset: Boolean) {
203
+ createCalendarFeed(input: { reset: $reset }) { calendarFeed { #{CALENDAR_FEED_FIELDS} } }
204
+ }
205
+ GRAPHQL
206
+
207
+ # The +mutation SetCalendarFeedVenue+ document.
208
+ SET_CALENDAR_FEED_VENUE = <<~GRAPHQL.freeze
209
+ mutation SetCalendarFeedVenue($venueId: ID!, $following: Boolean) {
210
+ setCalendarFeedVenue(input: { venueId: $venueId, following: $following }) { calendarFeed { #{CALENDAR_FEED_FIELDS} } }
211
+ }
212
+ GRAPHQL
213
+
214
+ # The +mutation DeleteCalendarFeed+ document.
215
+ DELETE_CALENDAR_FEED = <<~GRAPHQL
216
+ mutation DeleteCalendarFeed {
217
+ deleteCalendarFeed(input: {}) { deleted }
218
+ }
219
+ GRAPHQL
220
+
221
+ # The +query Admissions+ document.
222
+ ADMISSIONS = <<~GRAPHQL.freeze
223
+ query Admissions {
224
+ me { cover { admissions { nodes { #{ADMISSION_FIELDS} } } } }
225
+ }
226
+ GRAPHQL
227
+
228
+ # The +mutation BuyCover+ document.
229
+ BUY_COVER = <<~GRAPHQL.freeze
230
+ mutation BuyCover($venueId: ID!, $amount: MoneyInput) {
231
+ buyCover(input: { venueId: $venueId, amount: $amount }) {
232
+ admission { #{ADMISSION_FIELDS} }
233
+ #{PURCHASE_FIELDS}
234
+ }
235
+ }
236
+ GRAPHQL
237
+
238
+ # The +mutation BuyCoatCheck+ document.
239
+ BUY_COAT_CHECK = <<~GRAPHQL.freeze
240
+ mutation BuyCoatCheck($venueId: ID!, $quantity: Int) {
241
+ buyCoatCheck(input: { venueId: $venueId, quantity: $quantity }) {
242
+ admission { #{ADMISSION_FIELDS} }
243
+ #{PURCHASE_FIELDS}
244
+ }
245
+ }
246
+ GRAPHQL
247
+
248
+ # The +mutation PayCover+ document.
249
+ PAY_COVER = <<~GRAPHQL.freeze
250
+ mutation PayCover($admissionId: ID!, $sourceId: String!, $verificationToken: String) {
251
+ payCover(input: { admissionId: $admissionId, sourceId: $sourceId, verificationToken: $verificationToken }) {
252
+ admission { #{ADMISSION_FIELDS} }
253
+ }
254
+ }
255
+ GRAPHQL
256
+
257
+ # The +mutation ConfirmCover+ document.
258
+ CONFIRM_COVER = <<~GRAPHQL.freeze
259
+ mutation ConfirmCover($admissionId: ID!) {
260
+ confirmCover(input: { admissionId: $admissionId }) { admission { #{ADMISSION_FIELDS} } }
261
+ }
262
+ GRAPHQL
263
+
264
+ # The +mutation RefundAdmission+ document.
265
+ REFUND_ADMISSION = <<~GRAPHQL.freeze
266
+ mutation RefundAdmission($admissionId: ID!) {
267
+ refundAdmission(input: { admissionId: $admissionId }) { admission { #{ADMISSION_FIELDS} } }
268
+ }
269
+ GRAPHQL
270
+
271
+ # The +mutation SubmitOpEd+ document.
272
+ SUBMIT_OP_ED = <<~GRAPHQL.freeze
273
+ mutation SubmitOpEd($publicationId: ID!, $title: String!, $body: String!, $summary: String, $byline: String) {
274
+ submitOpEd(input: { publicationId: $publicationId, title: $title, body: $body, summary: $summary, byline: $byline }) {
275
+ article { #{ARTICLE_FIELDS} status reviewNote submittedAt }
276
+ }
277
+ }
278
+ GRAPHQL
279
+
280
+ module_function
281
+
282
+ # Records the person's position and says where that is: +venue+ when they're in one, else +locale+ and its
283
+ # +nearbyVenues+, plus +audience+. Select the fields the app needs.
284
+ # @param fields [String] e.g. +"venue { id name } locale { id name }"+
285
+ # @return [String]
286
+ def report_location(fields)
287
+ "mutation ReportLocation($position: CoordinatesInput!) {\n reportLocation(input: { position: $position }) " \
288
+ "{ #{fields} }\n}\n"
289
+ end
290
+
291
+ # A chat room's subscription (+kind+ :venue or :locale), aliased to +room+ whichever field it is, for an app
292
+ # with its own Action Cable client. With +watch+, a staff member watches without joining.
293
+ # @return [String]
294
+ def chat_room(kind, watch: false)
295
+ field, argument = kind.to_s == "venue" ? %w[venueChat venueId] : %w[localeChat localeId]
296
+ <<~GRAPHQL
297
+ subscription ChatRoom($id: ID!) {
298
+ room: #{field}(#{argument}: $id#{", watch: true" if watch}) {
299
+ __typename
300
+ ... on ChatJoined {
301
+ outOfRange
302
+ roster { people { userId name avatarUrl friend } }
303
+ history { message { ...Line } }
304
+ pinnedMessage { message { ...Line } }
305
+ }
306
+ ... on ChatMessagePosted { message { ...Line } }
307
+ ... on ChatMessageUpdated { message { ...Line } }
308
+ ... on ChatMessageRemoved { messageId }
309
+ ... on ChatPinChanged { messageId pinned announcement { ...Line } }
310
+ ... on ChatRoomCleared { clearedAt expired }
311
+ ... on ChatPresenceChanged { userId name avatarUrl presence }
312
+ ... on ChatRangeChanged { outOfRange }
313
+ ... on ChatLeft { reason }
314
+ }
315
+ }
316
+ fragment Line on ChatMessage { #{CHAT_MESSAGE_FIELDS} }
317
+ GRAPHQL
318
+ end
319
+
320
+ # A locale, with its Google place ID (Listings and Google Places).
321
+ def locale_fields(shape)
322
+ places = shape.has?("Locale", "profile") && shape.has?("ListingProfile", "places")
323
+ "id name label cityNames bannerUrl#{" profile { places { placeId } }" if places}"
324
+ end
325
+
326
+ # A venue in a list: its Facebook Page (Listings and Facebook) and its location's Google place ID.
327
+ def venue_fields(shape)
328
+ page = shape.has?("Venue", "profile") && shape.has?("ListingProfile", "facebook")
329
+ place = shape.has?("Location", "places")
330
+ "id name hidden order photoUrl bannerUrl#{" profile { facebook { page { #{PAGE_FIELDS} } } }" if page} " \
331
+ "locale { #{locale_fields(shape)} } location { id point address#{" places { placeId }" if place} } " \
332
+ "tags { #{TAG_FIELDS} }"
333
+ end
334
+
335
+ # A person's home venue, and which of the signed-in person's friends are there now (Friends).
336
+ def person_venue_fields(shape)
337
+ friends = shape.has?("Venue", "friends")
338
+ "id name photoUrl locale { id name }#{" friends { list { nodes { id name facebookId presence } } }" if friends}"
339
+ end
340
+
341
+ # One of a person's events (+PersonEvent+).
342
+ def person_event_fields(shape)
343
+ venue = shape.has?("Event", "venue") ? " venue { name venue { #{person_venue_fields(shape)} } }" : ""
344
+ "id name startAt locale { id name }#{venue} tags { #{TAG_FIELDS} }"
345
+ end
346
+
347
+ # The +venues+ document or selection, for what +shape+ says the audience has.
348
+ # @param shape [SchemaShape]
349
+ # @return [String]
350
+ def venues(shape)
351
+ "query Venues {\n venues { list { nodes { #{venue_fields(shape)} } } }\n}\n"
352
+ end
353
+
354
+ # The +people+ document or selection, for what +shape+ says the audience has.
355
+ # @param shape [SchemaShape]
356
+ # @return [String]
357
+ def people(shape)
358
+ page = if shape.has?("Person", "profile") && shape.has?("ListingProfile", "facebook")
359
+ " profile { facebook { page { #{PAGE_FIELDS} } } }"
360
+ end
361
+ venues = " venues { home { #{person_venue_fields(shape)} } }" if shape.has?("Person", "venues")
362
+ if shape.has?("Person", "events")
363
+ events = " events { involvements { edges { relationship role via { id name } node { " \
364
+ "#{person_event_fields(shape)} } } } }"
365
+ end
366
+ "query People {\n people { list { nodes { id name avatarUrl bannerUrl order global likeRequired" \
367
+ "#{page}#{venues} locales { id name }#{events} tags { #{TAG_FIELDS} } } } }\n}\n"
368
+ end
369
+
370
+ # The +locales+ document or selection, for what +shape+ says the audience has.
371
+ # @param shape [SchemaShape]
372
+ # @return [String]
373
+ def locales(shape)
374
+ "query Locales {\n locales { #{locale_fields(shape)} }\n}\n"
375
+ end
376
+
377
+ # The +closest_locale+ document or selection, for what +shape+ says the audience has.
378
+ # @param shape [SchemaShape]
379
+ # @return [String]
380
+ def closest_locale(shape)
381
+ "query ClosestLocale($near: CoordinatesInput!) {\n closestLocale(near: $near) { #{locale_fields(shape)} }\n}\n"
382
+ end
383
+
384
+ # A venue's cover tonight, or just the venue where the audience doesn't have the Ticketing and Cover modules.
385
+ def venue_cover(shape)
386
+ if shape.has?("Venue", "tickets") && shape.has?("VenueTickets", "cover")
387
+ cover = " tickets { cover { charge { #{COVER_CHARGE_FIELDS} } viewerAdmission { #{ADMISSION_FIELDS} } " \
388
+ "viewerCoatCheck { #{ADMISSION_FIELDS} } } }"
389
+ end
390
+ "query VenueCover($id: ID!) {\n venues { venue(id: $id) { id name#{cover} } }\n}\n"
391
+ end
392
+
393
+ # Every way into a venue on a night, or just the venue where the audience doesn't have Ticketing.
394
+ def venue_offers(shape)
395
+ # A variable the document doesn't use is an error, so $night goes with the field.
396
+ if shape.has?("Venue", "tickets")
397
+ "query VenueOffers($id: ID!, $night: ISO8601Date) {\n venues { venue(id: $id) { id tickets " \
398
+ "{ offers(night: $night) { nodes { #{ADMISSION_OFFER_FIELDS} } } } } }\n}\n"
399
+ else
400
+ "query VenueOffers($id: ID!) {\n venues { venue(id: $id) { id } }\n}\n"
401
+ end
402
+ end
403
+
404
+ # The Ticketing module's part of an event, with the Cover module's charge when the audience has it.
405
+ def event_tickets_fields(shape)
406
+ cover = " cover { charge { #{COVER_CHARGE_FIELDS} } }" if shape.has?("EventTickets", "cover")
407
+ "tickets { url offers { nodes { #{ADMISSION_OFFER_FIELDS} } }#{cover} }"
408
+ end
409
+
410
+ # The Venues module's part of an event or series: a display name and the venue.
411
+ def event_venue_fields(shape)
412
+ place = " places { placeId }" if shape.has?("Location", "places")
413
+ "venue { name venue { id name photoUrl location { id point address#{place} } } }"
414
+ end
415
+
416
+ # An event: its own fields, then those of the Venues, RSVP, People and Ticketing modules the audience has.
417
+ def event_fields(shape)
418
+ fields = "id name description startAt endAt isFeatured isCanceled bannerUrl sourceUrl " \
419
+ "locale { #{locale_fields(shape)} } tags { #{TAG_FIELDS} }"
420
+ fields << " #{event_venue_fields(shape)}" if shape.has?("Event", "venue")
421
+ fields << " rsvp { mine count }" if shape.has?("Event", "rsvp")
422
+ if shape.has?("Event", "people")
423
+ jockeys = " jockeys { nodes { id name avatarUrl } } jockeyLabel" if shape.has?("EventPeopleView", "jockeys")
424
+ fields << " people { hosts { nodes { id name avatarUrl } } lineup { nodes { id name avatarUrl } }" \
425
+ "#{jockeys} }"
426
+ end
427
+ fields << " #{event_tickets_fields(shape)}" if shape.has?("Event", "tickets")
428
+ fields
429
+ end
430
+
431
+ # The +event_series_fields+ document or selection, for what +shape+ says the audience has.
432
+ # @param shape [SchemaShape]
433
+ # @return [String]
434
+ def event_series_fields(shape)
435
+ venue = " #{event_venue_fields(shape)}" if shape.has?("EventSeries", "venue")
436
+ "id name description recurrenceText startsOn startTime timeZone durationMinutes bannerUrl " \
437
+ "locale { #{locale_fields(shape)} } tags { #{TAG_FIELDS} }#{venue}"
438
+ end
439
+
440
+ # The +events+ document or selection, for what +shape+ says the audience has.
441
+ # @param shape [SchemaShape]
442
+ # @return [String]
443
+ def events(shape)
444
+ "query Events($window: EventWindow, $localeId: ID, $venueId: ID, $first: Int) {\n " \
445
+ "events { list(window: $window, localeId: $localeId, venueId: $venueId, first: $first) {\n " \
446
+ "nodes { #{event_fields(shape)} }\n } }\n}\n"
447
+ end
448
+
449
+ # The +event+ document or selection, for what +shape+ says the audience has.
450
+ # @param shape [SchemaShape]
451
+ # @return [String]
452
+ def event(shape)
453
+ "query Event($id: ID!) {\n events { event(id: $id) { #{event_fields(shape)} } }\n}\n"
454
+ end
455
+
456
+ # The +event_series+ document or selection, for what +shape+ says the audience has.
457
+ # @param shape [SchemaShape]
458
+ # @return [String]
459
+ def event_series(shape)
460
+ "query EventSeries($localeId: ID, $venueId: ID, $first: Int) {\n " \
461
+ "events { series(localeId: $localeId, venueId: $venueId, first: $first) { nodes { " \
462
+ "#{event_series_fields(shape)} } } }\n}\n"
463
+ end
464
+
465
+ # A venue's page: the venue and what the modules its audience has add to it.
466
+ def venue_detail(shape)
467
+ fields = "#{venue_fields(shape)} description"
468
+ if shape.has?("Venue", "profile")
469
+ fields << " profile { address phone website socialLinks { nodes { id handle provider url } } }"
470
+ end
471
+ chat = shape.has?("Venue", "chat")
472
+ fields << " chat { open recentMessages(limit: $recentMessages) { nodes { #{CHAT_MESSAGE_FIELDS} } } }" if chat
473
+ if shape.has?("Venue", "events")
474
+ fields << " events { upcoming(first: 20) { nodes { #{event_fields(shape)} } } }"
475
+ end
476
+ "query VenueDetail($id: ID!#{", $recentMessages: Int" if chat}) {\n " \
477
+ "venues { venue(id: $id) { #{fields} } }\n}\n"
478
+ end
479
+
480
+ # The article fields this audience's API has: the tip to read without ads came later.
481
+ def article_fields(shape)
482
+ return ARTICLE_FIELDS unless shape.has?("Article", "viewerAdFree") && shape.has?("Publication", "adFreeOffered")
483
+
484
+ "#{ARTICLE_FIELDS.sub(PUBLICATION_SUMMARY_FIELDS, "#{PUBLICATION_SUMMARY_FIELDS} adFreeOffered")} viewerAdFree"
485
+ end
486
+
487
+ # The +publication_fields+ document or selection, for what +shape+ says the audience has.
488
+ # @param shape [SchemaShape]
489
+ # @return [String]
490
+ def publication_fields(shape)
491
+ extra = " adFreeOffered adFreeTipCents adFreeDays viewerAdFree viewerAdFreeUntil"
492
+ "#{PUBLICATION_FIELDS}#{extra if shape.has?("Publication", "adFreeOffered")}"
493
+ end
494
+
495
+ # The document when the audience has no Soapbox: asks for nothing the helpers read, under the operation's name.
496
+ def no_soapbox(name)
497
+ "query #{name} { id }\n"
498
+ end
499
+
500
+ # The +articles+ document or selection, for what +shape+ says the audience has.
501
+ # @param shape [SchemaShape]
502
+ # @return [String]
503
+ def articles(shape)
504
+ return no_soapbox("Articles") unless shape.has?("Audience", "soapbox")
505
+
506
+ "query Articles($localeId: ID, $publicationId: ID, $first: Int) {\n " \
507
+ "soapbox { articles(localeId: $localeId, publicationId: $publicationId, first: $first) {\n " \
508
+ "nodes { #{article_fields(shape)} }\n } }\n}\n"
509
+ end
510
+
511
+ # A page of the audience's live articles (pinned first, then newest), for lists that load more.
512
+ def articles_page(shape)
513
+ return no_soapbox("ArticlesPage") unless shape.has?("Audience", "soapbox")
514
+
515
+ "query ArticlesPage($localeId: ID, $publicationId: ID, $first: Int, $after: String) {\n " \
516
+ "soapbox { articles(localeId: $localeId, publicationId: $publicationId, first: $first, " \
517
+ "after: $after) {\n nodes { #{article_fields(shape)} }\n pageInfo { endCursor hasNextPage }\n } }\n}\n"
518
+ end
519
+
520
+ # The audience's national news; an API from before national news gets a document that asks for none.
521
+ def national_articles(shape)
522
+ unless shape.has?("Audience", "soapbox") && shape.has?("Article", "featuredNationally")
523
+ return no_soapbox("NationalArticles")
524
+ end
525
+
526
+ "query NationalArticles($localeId: ID, $first: Int) {\n " \
527
+ "soapbox { articles(national: true, localeId: $localeId, first: $first) {\n " \
528
+ "nodes { #{article_fields(shape)} featuredNationally }\n } }\n}\n"
529
+ end
530
+
531
+ # The +article+ document or selection, for what +shape+ says the audience has.
532
+ # @param shape [SchemaShape]
533
+ # @return [String]
534
+ def article(shape)
535
+ return no_soapbox("Article") unless shape.has?("Audience", "soapbox")
536
+
537
+ "query Article($id: ID!) {\n soapbox { article(id: $id) { #{article_fields(shape)} body " \
538
+ "#{ARTICLE_SUBJECT_FIELDS} } }\n}\n"
539
+ end
540
+
541
+ # The +publications+ document or selection, for what +shape+ says the audience has.
542
+ # @param shape [SchemaShape]
543
+ # @return [String]
544
+ def publications(shape)
545
+ return no_soapbox("Publications") unless shape.has?("Audience", "soapbox")
546
+
547
+ "query Publications {\n soapbox { publications { #{publication_fields(shape)} } }\n}\n"
548
+ end
549
+
550
+ # The +publication+ document or selection, for what +shape+ says the audience has.
551
+ # @param shape [SchemaShape]
552
+ # @return [String]
553
+ def publication(shape)
554
+ return no_soapbox("Publication") unless shape.has?("Audience", "soapbox")
555
+
556
+ "query Publication($id: ID!) {\n soapbox { publication(id: $id) { #{publication_fields(shape)} } }\n}\n"
557
+ end
558
+
559
+ # The +my_op_eds+ document or selection, for what +shape+ says the audience has.
560
+ # @param shape [SchemaShape]
561
+ # @return [String]
562
+ def my_op_eds(shape)
563
+ return no_soapbox("MyOpEds") unless shape.has?("Audience", "soapbox")
564
+
565
+ "query MyOpEds {\n soapbox { myOpEds { #{ARTICLE_FIELDS} status reviewNote submittedAt } }\n}\n"
566
+ end
567
+
568
+ # Where articles about each kind of listing are read from: its type, and the selection around +articles+.
569
+ ARTICLES_ABOUT = {
570
+ "venue" => ["Venue", "venues { venue(id: $id) { ", " } }"],
571
+ "event" => ["Event", "events { event(id: $id) { ", " } }"],
572
+ "person" => ["Person", "people { person(id: $id) { ", " } }"],
573
+ "locale" => ["Locale", "locale(id: $id) { ", " }"]
574
+ }.freeze
575
+
576
+ # Live articles about a venue, event, person or locale: the listing type the document depends on, and a
577
+ # lambda that builds the document for a shape.
578
+ # @param topic [String, Symbol] venue, event, person or locale
579
+ # @return [Array(String, Proc)]
580
+ # @raise [ArgumentError] for any other topic
581
+ def articles_about(topic)
582
+ type, open, close = ARTICLES_ABOUT.fetch(topic.to_s) do
583
+ raise ArgumentError, "Articles are about a venue, event, person or locale, not #{topic}."
584
+ end
585
+ document = lambda do |shape|
586
+ return no_soapbox("ArticlesAbout") unless shape.has?(type, "articles")
587
+
588
+ "query ArticlesAbout($id: ID!, $first: Int) {\n #{open}articles { list(first: $first) { nodes { " \
589
+ "#{article_fields(shape)} } } }#{close}\n}\n"
590
+ end
591
+ [type, document]
592
+ end
593
+
594
+ # Every document the SDK can send, built for an audience with every module, by operation name. CI checks them
595
+ # against the API schema (+bin/graphql-documents+).
596
+ # @return [Hash{String => String}]
597
+ def all
598
+ everything = SchemaShape.everything
599
+ documents = {
600
+ "Audience" => AUDIENCE, "Me" => ME, "RSVPEvent" => RSVP_EVENT, "CancelRSVP" => CANCEL_RSVP,
601
+ "EventRSVP" => EVENT_RSVP, "DeleteAccount" => DELETE_ACCOUNT, "ReportContent" => REPORT_CONTENT,
602
+ "BlockUser" => BLOCK_USER, "UnblockUser" => UNBLOCK_USER, "CalendarFeed" => CALENDAR_FEED,
603
+ "CreateCalendarFeed" => CREATE_CALENDAR_FEED, "SetCalendarFeedVenue" => SET_CALENDAR_FEED_VENUE,
604
+ "DeleteCalendarFeed" => DELETE_CALENDAR_FEED,
605
+ "ReportLocation" => report_location("venue { id name } locale { id name }"),
606
+ "ChatRoom" => chat_room(:venue),
607
+ "LocaleChatRoom" => renamed(chat_room(:locale, watch: true), "LocaleChatRoom"),
608
+ "Tribes" => TRIBES, "IdentifyWithTribe" => IDENTIFY_WITH_TRIBE, "LeaveTribe" => LEAVE_TRIBE,
609
+ "RegisterDevice" => REGISTER_DEVICE, "Node" => NODE, "SendMyChatMessage" => SEND_CHAT_MESSAGE,
610
+ "RemoveMyChatMessage" => REMOVE_CHAT_MESSAGE, "Admissions" => ADMISSIONS, "BuyCover" => BUY_COVER,
611
+ "BuyCoatCheck" => BUY_COAT_CHECK, "PayCover" => PAY_COVER, "ConfirmCover" => CONFIRM_COVER,
612
+ "RefundAdmission" => REFUND_ADMISSION, "SubmitOpEd" => SUBMIT_OP_ED
613
+ }
614
+ %w[venues people locales closest_locale venue_cover venue_offers events event event_series venue_detail
615
+ articles articles_page national_articles article publications publication my_op_eds].each do |name|
616
+ document = public_send(name, everything)
617
+ documents[document[/\A\w+ (\w+)/, 1]] = document
618
+ end
619
+ ARTICLES_ABOUT.each_key do |topic|
620
+ name = "ArticlesAbout#{topic.capitalize}"
621
+ documents[name] = renamed(articles_about(topic).last.call(everything), name)
622
+ end
623
+ documents
624
+ end
625
+
626
+ # +document+ with its operation renamed, so the schema check sees each variant under its own name.
627
+ def renamed(document, name)
628
+ document.sub(/\A(query|mutation|subscription) \w+/, "\\1 #{name}")
629
+ end
630
+ end
631
+ end
632
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Audience
4
+ module Kit
5
+ # What {Operations#report_content} reports: the API's +ReportTarget+, and the words around reporting that the
6
+ # apps use, so every app says the same thing.
7
+ module ReportTarget
8
+ # A chat message (its frame ID).
9
+ MESSAGE = "MESSAGE"
10
+ # A member (their user ID, as chat gives it).
11
+ PERSON = "PERSON"
12
+ # A photo someone posted.
13
+ PHOTO = "PHOTO"
14
+ # A Ping: a wave from one member to another.
15
+ PING = "PING"
16
+ # A line in a Ping or Quickie chat; it and the lines before it are kept with the report.
17
+ PRIVATE_MESSAGE = "PRIVATE_MESSAGE"
18
+ # A post in the feed.
19
+ POST = "POST"
20
+ # A comment on a post.
21
+ POST_COMMENT = "POST_COMMENT"
22
+ # A venue listing.
23
+ VENUE = "VENUE"
24
+ # Every target.
25
+ ALL = [MESSAGE, PERSON, PHOTO, PING, PRIVATE_MESSAGE, POST, POST_COMMENT, VENUE].freeze
26
+
27
+ # The reason for an intimate image shared without the consent of the person in it. The API gives a report with
28
+ # this reason a 48-hour deadline (the US Take It Down Act), so keep the wording.
29
+ INTIMATE_IMAGE = "Intimate image shared without consent"
30
+ # The reasons to offer.
31
+ REASONS = ["Spam", "Harassment or hate", "Sexual content", INTIMATE_IMAGE, "Scam or fraud", "Other"].freeze
32
+ end
33
+ end
34
+ end