2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2016-09-21 17:22:40 +00:00
|
|
|
class Message < ApplicationRecord
|
2015-12-11 19:23:41 +00:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :resource, polymorphic: true
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
delegate :name, to: :user, prefix: true
|
2017-01-24 00:30:13 +00:00
|
|
|
|
2017-01-03 21:12:58 +00:00
|
|
|
after_create :after_created
|
2017-11-25 19:23:47 +00:00
|
|
|
# after_create :after_created_async
|
2015-12-11 19:23:41 +00:00
|
|
|
|
|
|
|
def user_image
|
2016-07-26 00:14:23 +00:00
|
|
|
user.image.url
|
2015-12-11 19:23:41 +00:00
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
def as_json(_options = {})
|
2018-01-20 22:10:26 +00:00
|
|
|
json = super(methods: %i[user_name user_image])
|
2015-12-11 19:23:41 +00:00
|
|
|
json
|
|
|
|
end
|
2017-01-24 00:30:13 +00:00
|
|
|
|
2017-01-03 21:12:58 +00:00
|
|
|
def after_created
|
2017-01-24 00:30:13 +00:00
|
|
|
ActionCable.server.broadcast 'map_' + resource.id.to_s, type: 'messageCreated', message: as_json
|
2017-01-03 21:12:58 +00:00
|
|
|
end
|
2017-03-16 21:58:56 +00:00
|
|
|
|
2017-02-11 05:20:42 +00:00
|
|
|
def after_created_async
|
|
|
|
FollowService.follow(resource, user, 'commented')
|
|
|
|
NotificationService.notify_followers(resource, 'map_message', self)
|
|
|
|
end
|
|
|
|
handle_asynchronously :after_created_async
|
2015-12-11 19:23:41 +00:00
|
|
|
end
|