A
A
Alexey Konovalov2018-02-01 13:30:23
PHP
Alexey Konovalov, 2018-02-01 13:30:23

How to make alerts on the site?

Hello! I make internal notifications on the site (your comment was answered, you were subscribed, you were voted for, etc.).
Everything is stored in the database in this form:
5a72e3a13951f430423889.jpeg
For each action, we add the corresponding entry to the database, for example, your post was liked:

INSERT INTO `events` (`user_id`, `feed_user_id`, `mode`, `mode_id`, `action`) VALUE('2', '1', 'posts', '1255', 'like');

It turns out that user 2 liked post 1255 , and the post was added by user 1 , and we show the notification to him.
You need to display it in the notification like this:
Fedya liked your post " Hurrah, Friday! " (Fedya - link to Fedya's profile, cheers Friday - link to this post).
When requesting notifications for user 1, I get them like this:
First, we pull out everything related to posts and make a request only to get actions committed with posts.
because the user who made the action should always be displayed, add a JOIN to the `users` table
because we need to display the title of the post, we need to get it from the `posts` table , we also add a JOIN
SELECT u.`id`, u.`name`, u.`photo`, p.`id` post_id, p.`title`, e.`action`
FROM `events` e 
INNER JOIN `users` u ON u.`id` = e.`user_id`
INNER JOIN `posts` p ON p.`id` = e.`mode_id`
WHERE e.`feed_user_id` = 1 AND e.`mode` = 'posts'

As a result, we get:
  • ( id, name, photo ) - information about the user who liked
  • ( post_id, title ) - Information about the post
  • ( action ) - the action that was performed (using it we determine what text should be displayed)

Then we make the same request for users. because there is no need to join the posts table
SELECT u.`id`, u.`name`, u.`photo`, e.`action`
FROM `events` e 
INNER JOIN `users` u ON u.`id` = e.`user_id`
WHERE e.`feed_user_id` = 1 AND e.`mode` = 'users'

As a result, we get:
  • ( id, name, photo ) - information about the user who subscribed to you
  • ( action ) - the action that was taken (eg subscribe)

In general, and so on, it turns out about 7 requests for all output options. After that, in php we add everything into one array and generate a string based on the received action .
The problem appears at all stages when you need to get information that a comment has been added to the post, the comment has been answered, the comment has been liked. It turns out that to obtain this data, you need to write as many queries as there are modules where there can be comments.
For example, if I did not create a post, but my comment was answered there, then for the database we get stored mode = `comments`, `mode_id` = 122254 . But it is no longer possible to get information about the post in which my comment was written, which was answered.
As a result, the problem is both in storing information in the database and in the number of requests to the database.
Maybe someone has a more or less suitable example, or tell me in words what the logic of such a system should be.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question