cirandas.net

ref: master

plugins/open_graph/lib/open_graph_plugin/stories.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
class OpenGraphPlugin::Stories

  class_attribute :publishers
  self.publishers = []

  def self.register_publisher publisher
    self.publishers << publisher
  end

  def self.publish record, stories
    actor = User.current.person rescue nil
    return unless actor

    self.publishers.each do |publisher|
      publisher = publisher.delay unless Rails.env.development? or Rails.env.test?
      publisher.publish_stories record, actor, stories
    end
  end

  Definitions = {
    # needed a patch on UploadedFile: def notifiable?; true; end
    add_a_document: {
      action_tracker_verb: :create_article,
      track_config: 'OpenGraphPlugin::ActivityTrackConfig',
      action: :add,
      object_type: :uploaded_file,
      models: :UploadedFile,
      on: :create,
      criteria: proc do |article, actor|
        article.is_a? UploadedFile and not article.image?
      end,
      publish_if: proc do |uploaded_file, actor|
        uploaded_file.published?
      end,
      object_data_url: proc do |uploaded_file, actor|
        uploaded_file.url.merge view: true
      end,
    },
    add_an_image: {
      # :upload_image verb can't be used as it uses the parent Gallery as target
      # hooked via open_graph_attach_stories
      action_tracker_verb: nil,
      track_config: 'OpenGraphPlugin::ActivityTrackConfig',
      action: :add,
      object_type: :gallery_image,
      models: :UploadedFile,
      on: :create,
      criteria: proc do |article, actor|
        article.is_a? UploadedFile and article.image?
      end,
      publish_if: proc do |uploaded_file, actor|
        uploaded_file.published? and uploaded_file.parent.is_a? Gallery
      end,
      object_data_url: proc do |uploaded_file, actor|
        uploaded_file.url.merge view: true
      end,
    },
    create_an_article: {
      action_tracker_verb: :create_article,
      track_config: 'OpenGraphPlugin::ActivityTrackConfig',
      action: :create,
      object_type: :blog_post,
      models: :Article,
      on: :create,
      criteria: proc do |article, actor|
        article.parent.is_a? Blog
      end,
      publish_if: proc do |article, actor|
        article.published?
      end,
    },
    create_an_event: {
      action_tracker_verb: :create_article,
      track_config: 'OpenGraphPlugin::ActivityTrackConfig',
      action: :create,
      object_type: :event,
      models: :Event,
      on: :create,
      criteria: proc do |article, actor|
        article.is_a? Event
      end,
      publish_if: proc do |event, actor|
        event.published?
      end,
    },
    start_a_discussion: {
      action_tracker_verb: :create_article,
      track_config: 'OpenGraphPlugin::ActivityTrackConfig',
      action: :start,
      object_type: :forum,
      models: :Article,
      on: :create,
      criteria: proc do |article, actor|
        article.parent.is_a? Forum
      end,
      publish_if: proc do |article, actor|
        article.published?
      end,
    },

    # these a published as passive to give focus to the enterprise
=begin
    add_a_sse_product: {
      action_tracker_verb: :create_product,
      track_config: 'OpenGraphPlugin::ActivityTrackConfig',
      action: :announce_new,
      models: :Product,
      on: :create,
      object_type: :product,
      publish_if: proc do |product, actor|
        product.profile.public?
      end,
    },
    update_a_sse_product: {
      action_tracker_verb: :update_product,
      track_config: 'OpenGraphPlugin::ActivityTrackConfig',
      action: :announce_update,
      object_type: :product,
      models: :Product,
      on: :update,
      publish_if: proc do |product, actor|
        product.profile.public?
      end,
    },
=end

    favorite_a_sse_initiative: {
      action_tracker_verb: :favorite_enterprise,
      track_config: 'OpenGraphPlugin::ActivityTrackConfig',
      action: :favorite,
      object_type: :favorite_enterprise,
      models: :FavoriteEnterprisePerson,
      on: :create,
      object_actor: proc do |favorite_enterprise_person|
        favorite_enterprise_person.person
      end,
      object_profile: proc do |favorite_enterprise_person|
        favorite_enterprise_person.enterprise
      end,
      object_data_url: proc do |favorite_enterprise_person, actor|
        self.og_profile_url favorite_enterprise_person.enterprise
      end,
    },

=begin
    comment_a_discussion: {
      action_tracker_verb: nil,
      action: :comment,
      object_type: :forum,
      models: :Comment,
      on: :create,
      criteria: proc do |comment, actor|
        source, parent = comment.source, comment.source.parent
        source.is_a? Article and parent.is_a? Forum
      end,
      publish_if: proc do |comment, actor|
        comment.source.parent.published?
      end,
    },
    comment_an_article: {
      action_tracker_verb: nil,
      action: :comment,
      object_type: :blog_post,
      models: :Comment,
      on: :create,
      criteria: proc do |comment, actor|
        source, parent = comment.source, comment.source.parent
        source.is_a? Article and parent.is_a? Blog
      end,
      publish_if: proc do |comment, actor|
        comment.source.parent.published?
      end,
    },
=end

    make_friendship_with: {
      action_tracker_verb: :new_friendship,
      track_config: 'OpenGraphPlugin::ActivityTrackConfig',
      action: :make_friendship,
      object_type: :friend,
      models: :Friendship,
      on: :create,
      custom_actor: proc do |friendship|
        friendship.person
      end,
      object_actor: proc do |friendship|
        friendship.person
      end,
      object_profile: proc do |friendship|
        friendship.friend
      end,
      object_data_url: proc do |friendship, actor|
        self.og_profile_url friendship.friend
      end,
    },

    # PASSIVE STORIES
    announce_news_from_a_sse_initiative: {
      action_tracker_verb: :create_article,
      track_config: 'OpenGraphPlugin::EnterpriseTrackConfig',
      action: :announce_news,
      object_type: :enterprise,
      passive: true,
      models: :Article,
      on: :create,
      criteria: proc do |article, actor|
        article.profile.enterprise?
      end,
      publish_if: proc do |article, actor|
        article.published?
      end,
    },
    announce_a_new_sse_product: {
      action_tracker_verb: :create_product,
      track_config: 'OpenGraphPlugin::EnterpriseTrackConfig',
      action: :announce_new,
      object_type: :product,
      passive: true,
      models: :Product,
      on: :create,
      criteria: proc do |product, actor|
        product.profile.enterprise?
      end,
    },
    announce_an_update_of_sse_product: {
      action_tracker_verb: :update_product,
      track_config: 'OpenGraphPlugin::EnterpriseTrackConfig',
      action: :announce_update,
      object_type: :product,
      passive: true,
      models: :Product,
      on: :update,
      criteria: proc do |product, actor|
        # only post from enterprises and products with images
        product.profile.enterprise? and product.image.present?
      end,
    },

    announce_news_from_a_community: {
      action_tracker_verb: :create_article,
      track_config: 'OpenGraphPlugin::CommunityTrackConfig',
      action: :announce_news,
      object_type: :community,
      passive: true,
      models: :Article,
      on: :create,
      criteria: proc do |article, actor|
        article.profile.community?
      end,
      publish_if: proc do |article, actor|
        article.published?
      end,
    },

  }

  ValidObjectList = Definitions.map{ |story, data| data[:object_type] }.uniq
  ValidActionList = Definitions.map{ |story, data| data[:action] }.uniq

  # TODO make this verification work
  #raise "Each active story must use a unique object_type for configuration to work" if ValidObjectList.size < Definitions.size

  DefaultActions = ValidActionList.inject({}){ |h, a| h[a] = a; h }
  DefaultObjects = ValidObjectList.inject({}){ |h, o| h[o] = o; h }

  TrackerStories = {}; Definitions.each do |story, data|
    Array(data[:action_tracker_verb]).each do |verb|
      next unless verb
      TrackerStories[verb] ||= []
      TrackerStories[verb] << story
    end
  end

  TrackConfigStories = {}; Definitions.each do |story, data|
    Array(data[:track_config]).each do |track_config|
      next unless track_config
      TrackConfigStories[track_config] ||= []
      TrackConfigStories[track_config] << [story, data]
    end
  end

  ModelStories = {}; Definitions.each do |story, data|
    Array(data[:models]).each do |model|
      ModelStories[model] ||= {}
      Array(data[:on]).each do |on|
        ModelStories[model][on] ||= []
        ModelStories[model][on] << story
      end
    end
  end

end