Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. Route::get('/', function () {
  2. return view('welcome');
  3. });
  4.  
  5. Auth::routes();
  6.  
  7. Route::get('/home', 'HomeController@index')->name('home');
  8. Route::get('/contacts', 'ContactsController@get');
  9. Route::get('/conversation/{id}', 'ContactsController@getMessagesFor');
  10. Route::post('/conversation/send', 'ContactsController@send');
  11.  
  12. Route::get('/user', function() { // for testing purposes
  13. echo Auth::id();
  14. });
  15.  
  16. namespace AppHttpControllers;
  17.  
  18. use IlluminateSupportFacadesAuth;
  19. use IlluminateHttpRequest;
  20. use AppUser;
  21. use AppMessage;
  22. use AppEventsNewMessage;
  23.  
  24. class ContactsController extends Controller {
  25.  
  26. public function __construct() {
  27. $this->middleware('auth');
  28. }
  29.  
  30. public function get() {
  31. $contacts = User::where('id', '!=', auth()->id())->get();
  32. return response()->json($contacts);
  33. }
  34.  
  35. public function getMessagesFor($id) {
  36. $messages = Message::where('from', $id)->orWhere('to', $id)->get();
  37.  
  38. return response()->json($messages);
  39. }
  40.  
  41. public function send(Request $request) {
  42. $user = Auth::user();
  43. $message = Message::create([
  44. 'from' => auth()->id(),
  45. 'to' => $request->contact_id,
  46. 'text' => $request->text
  47. ]);
  48.  
  49. broadcast(new NewMessage($user, $message))->toOthers();
  50.  
  51. return response()->json($user, $message);
  52. }
  53. }
  54.  
  55. <?php
  56.  
  57. namespace AppEvents;
  58. use AppMessage;
  59. use AppUser;
  60. use IlluminateBroadcastingChannel;
  61. use IlluminateQueueSerializesModels;
  62. use IlluminateBroadcastingPrivateChannel;
  63. use IlluminateBroadcastingPresenceChannel;
  64. use IlluminateFoundationEventsDispatchable;
  65. use IlluminateBroadcastingInteractsWithSockets;
  66. use IlluminateContractsBroadcastingShouldBroadcast;
  67.  
  68. class NewMessage implements ShouldBroadcast {
  69. use Dispatchable, InteractsWithSockets, SerializesModels;
  70.  
  71. /**
  72. * User that sent the message
  73. *
  74. * @var User
  75. */
  76.  
  77. public $user;
  78.  
  79. /**
  80. * Message details
  81. *
  82. * @var Message
  83. */
  84.  
  85. public $message;
  86.  
  87. /**
  88. * Create a new event instance.
  89. *
  90. * @return void
  91. */
  92.  
  93. public function __construct(User $user, Message $message)
  94. {
  95. $this->user = $user;
  96. $this->message = $message;
  97. }
  98.  
  99. /**
  100. * Get the channels the event should broadcast on.
  101. *
  102. * @return IlluminateBroadcastingChannel|array
  103. */
  104. public function broadcastOn()
  105. {
  106. return new PrivateChannel('messages.' . $this->message->to);
  107. }
  108.  
  109. public function broadcastWith() {
  110. return ["message" => $this->message];
  111. }
  112. }
  113.  
  114. import Conversation from './Conversation';
  115. import ContactsList from './ContactsList';
  116.  
  117. export default {
  118.  
  119. props: {
  120. user: {
  121. type: Object,
  122. required: true
  123. }
  124. },
  125.  
  126. data() {
  127. return {
  128. selectedContact: null,
  129. messages: [],
  130. contacts:[]
  131. };
  132. },
  133.  
  134. mounted() {
  135. Echo.private(`messages${this.user.id}`)
  136. .listen('NewMessage', (e) => {
  137. this.handleIncoming(e.message);
  138. });
  139.  
  140. axios.get('/contacts')
  141. .then((response) => {
  142. this.contacts = response.data;
  143. });
  144. },
  145. methods: {
  146. startConversationWith(contact) {
  147. axios.get(`conversation/${contact.id}`)
  148. .then((response) => {
  149. this.messages = response.data;
  150. this.selectedContact = contact;
  151. })
  152. },
  153.  
  154. saveNewMessage(message) {
  155. this.messages.push(message);
  156. },
  157.  
  158. handleIncoming(message) {
  159. if (this.selectedContact && message.from ==
  160. this.selectedContact.id) {
  161. this.saveNewMessage(message);
  162. return;
  163. }
  164.  
  165. //alert(message.text);
  166. }
  167. },
  168. components: {
  169. Conversation,
  170. ContactsList
  171. }
  172. }
  173.  
  174. methods: {
  175. sendMessage(text) {
  176. if (!this.contact) {
  177. return;
  178. }
  179.  
  180. axios.post('/conversation/send', {
  181. contact_id: this.contact.id,
  182. text: text
  183. }).then((response) => {
  184. this.$emit('new', response.data);
  185. })
  186. }
  187. },
  188.  
  189. export default {
  190. data() {
  191. return {
  192. message: ''
  193. };
  194. },
  195. methods: {
  196. send(e) {
  197. e.preventDefault();
  198. if (this.message == '') {
  199. return;
  200. }
  201. this.$emit('send', this.message);
  202. this.message = '';
  203. }
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement