Advertisement
Guest User

Untitled

a guest
Jun 13th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.70 KB | None | 0 0
  1. class SocketController < WebsocketRails::BaseController
  2.  
  3.   def addMessage
  4.     c = Chat.find(message[:chat_id])
  5.     msg = Message.new
  6.     msg.message = message[:message]
  7.     msg.chat_id = message[:chat_id]
  8.     msg.user_id = message[:user]
  9.     msg.save(validate: false)
  10.     o = c.order
  11.     new_message = {:message =>  message[:message], :date => (DateTime.now.utc - 3.hours).strftime("%d/%m/%y %H:%M") }
  12.  
  13.     if msg.user.class != Guide
  14.       data = { :alert => "Enviou uma mensagem", sound:  "default", type: "chat", order: c.order.id, chat: c.id}
  15.       pushQ = Parse::Push.new(data)
  16.       query = Parse::Query.new(Parse::Protocol::CLASS_INSTALLATION).eq('customer_id', c.order.guide.id)
  17.       pushQ.where = query.where
  18.       pushQ.save
  19.       o = c.order
  20.       o.user.notifications.where(:chat_id => c.id.to_s).destroy_all
  21.       n = Notification.new
  22.       n.user = o.guide
  23.       n.name = o.user.name
  24.       n.message = data[:alert]
  25.       n.order_id = o.id.to_s
  26.       n.chat_id = c.id.to_s
  27.       n.picture_url = o.user.picture.url(:original)
  28.       n.save
  29.       WebsocketRails["#{c.id.to_s}guide"].trigger :new_message, new_message
  30.       DucityMailer.chat_notification_notify(u.guide)
  31.     else
  32.       data = { :alert => "Enviou uma mensagem", sound:  "default", type: "chat", order: c.order.id, chat: c.id}
  33.       puts "**********1"
  34.       pushQ = Parse::Push.new(data)
  35.       query = Parse::Query.new(Parse::Protocol::CLASS_INSTALLATION).eq('customer_id', c.order.user.id)
  36.       pushQ.where = query.where
  37.       pushQ.save
  38.             puts "**********2"
  39.       o = c.order
  40.       o.user.notifications.where(:chat_id => c.id.to_s).destroy_all
  41.       n = Notification.new
  42.             puts "**********3"
  43.  
  44.       n.user = o.user
  45.       n.name = o.guide.name
  46.       n.message = data[:alert]
  47.             puts "**********4"
  48.  
  49.       n.order_id = o.id.to_s
  50.       n.chat_id = c.id.to_s
  51.             puts "**********5"
  52.  
  53.       n.picture_url = o.guide.picture.url(:original)
  54.       n.save
  55.             puts "**********6"
  56.  
  57.       WebsocketRails["#{c.id.to_s}"].trigger :new_message, new_message
  58.       puts "**********7"
  59.     end
  60.  
  61.  
  62.  
  63.  
  64.   end
  65.  
  66.    def addMessageGuide
  67.     puts "******************"
  68.     puts "******************"
  69.     puts "******************"
  70.     puts "******************"
  71.     puts "******************"
  72.     puts "******************"
  73.     puts "******************"
  74.     puts "******************"
  75.     puts "******************"
  76.     puts "******************"
  77.     puts "******************" + message.to_s
  78.     c = Chat.find(message[:chat_id])
  79.     msg = Message.new
  80.     msg.message = message[:message]
  81.     msg.chat = c
  82.     msg.user = User.find(message[:user])
  83.     msg.save(validate: false)
  84.     o = c.order
  85.  
  86.      if msg.user.class != Guide
  87.       data = { :alert => (msg.user.name + " enviou uma mensagem"), sound:  "default", type: "chat", order: c.order.id, chat: c.id}
  88.       pushQ = Parse::Push.new(data)
  89.       query = Parse::Query.new(Parse::Protocol::CLASS_INSTALLATION).eq('customer_id', c.order.guide.id)
  90.       pushQ.where = query.where
  91.       pushQ.save
  92.       o = c.order
  93.       o.user.notifications.where(:chat_id => c.id.to_s).destroy_all
  94.       n = Notification.new
  95.       n.user = o.guide
  96.       n.name = o.user.name
  97.       n.message = data[:alert]
  98.       n.order_id = o.id.to_s
  99.       n.chat_id = c.id.to_s
  100.       n.picture_url = o.user.picture.url(:original)
  101.       n.save
  102.     else
  103.       data = { :alert => (msg.user.name + " enviou uma mensagem"), sound:  "default", type: "chat", order: c.order.id, chat: c.id}
  104.       pushQ = Parse::Push.new(data)
  105.       query = Parse::Query.new(Parse::Protocol::CLASS_INSTALLATION).eq('customer_id', c.order.user.id)
  106.       pushQ.where = query.where
  107.       pushQ.save
  108.       o = c.order
  109.       o.user.notifications.where(:chat_id => c.id.to_s).destroy_all
  110.       n = Notification.new
  111.       n.user = o.user
  112.       n.name = o.guide.name
  113.       n.message = data[:alert]
  114.       n.order_id = o.id.to_s
  115.       n.chat_id = c.id.to_s
  116.       n.picture_url = o.guide.picture.url(:original)
  117.       n.save
  118.     end
  119.  
  120.   end
  121.  
  122.  
  123.   def addMessage2
  124.     puts "******@*********"
  125.     puts "***************"
  126.     puts "***************"
  127.     puts "***************"
  128.     puts "***********@****"
  129.     puts "***************" + message.to_s
  130.     puts "***************" + message[:data].to_s
  131.  
  132.     if !message[:data].nil?
  133.       m = message[:data]
  134.     else
  135.       m = message
  136.     end
  137.     # puts "***********@****"
  138.     # puts "***************" + m.to_s
  139.     puts "***************" + m["chat_id"].to_s
  140.  
  141.     m["chat_id"] = m["chat_id"].to_s.gsub(m["user"].to_s, "")
  142.     id = (m["chat_id"].to_s).gsub(m["user"].to_s, "")
  143.  
  144.     c = Chat.find(id)
  145.  
  146.     puts "***************" + c.id
  147.  
  148.     msg = Message.new
  149.     msg.message = m["message"]
  150.  
  151.     puts "***************" + msg.message
  152.  
  153.     msg.chat = c
  154.     msg.isProvider = true
  155.     msg.user = User.find(m["user"].to_s)
  156.  
  157.     msg.save
  158.  
  159.     new_message = {:message =>  msg.message, :date => (DateTime.now.utc - 2.hours).strftime("%d/%m/%y %H:%M") }
  160.  
  161.     id = (c.user_id.to_s + c.provider_id.to_s).gsub(msg.user.id.to_s, "")
  162.  
  163.  
  164.     WebsocketRails["#{c.id.to_s}#{id}"].trigger :new_message, new_message
  165.  
  166.     # Thread.new do
  167.       # data = { :alert =>  {:title => msg.user.name, body: msg.message}, sound:  "default", :chat_id => c.id.to_s}
  168.       data = {:chat_id => c.id.to_s,  :alert => (msg.user.name + " enviou uma mensagem "  ), sound:  "default"}
  169.       pushQ = Parse::Push.new(data)
  170.       query = Parse::Query.new(Parse::Protocol::CLASS_INSTALLATION).eq('customer_id ', id)
  171.       pushQ.where = query.where
  172.       pushQ.save
  173.       puts "@@@*******!!!********" + msg.user.to_s
  174.  
  175.  
  176.       puts "********"
  177.  
  178.     puts "@@@*******!!!*******----*" + msg.user.to_s
  179.  
  180.   end
  181.  
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement