通知系统的结构


Structure of a notification system

你好,我在laravel 4制作一个电影和电视节目网页,用户可以跟随电视节目,我在用户表中存储值,例如:

user:jhon
password:secret
tvshow:1,2,7,3

用户正在关注id为1和2 7和3的电视节目,所以用户正在关注4个电视节目,如果这是最佳实践,但我喜欢它(如果有更好的实践请通知我)。

我想做一个新的通知系统,其中有不止一种类型的通知:

| @this user mentioned you in a comment       |
|_____________________________________________|
| A new episode of #your_tv_show comes today  | 
|_____________________________________________|
| @user send you a message                    | 
|_____________________________________________|
| @user its following you                     |
'_____________________________________________/

我在想:

通知表
|  id  |from_id | to_id |   type   |   read |
|  3   |    1   |   3   |  message |    0   |

但是我怎么得到通知链接呢http://www.mypage.com/username/meesage/3

例如,我有300个用户关注#tvshow,当我想给一个通知,比如它的新一集,我需要在表中创建300个新记录。

我想要的桌子就是不行,请帮忙。

我希望你能理解我的问题,不太懂英语。如果你需要更多的信息,请询问和帮助的最佳做法。

谢谢

通知是一个获得300多条消息的大表,每次节目发送更新都很好,只要你保持干净,通过删除'已见'帖子,每周一次或其他方式。

如果不将消息附加给所有关注者,就没有其他方法可以真正做到"看到"消息。

你可以做一个全局的消息附加到节目,然后只发送一次给所有的追随者,而不需要一个消息表,但这样你就不能做整个"看到"或"看不见"的位。明白我的意思吗?

但是我会为下面的演出设置一个表和模型…叫它Following或其他什么....就像. .

ID  |   user_id    |  show_id   | 
1          22            3
2          12            4  
3          22            12

然后你可以在你的User模型中设置一个关系…并且去掉逗号分隔的东西,如果网站变大,这会变得非常混乱。

public function showsFollowing(){
   return $this->hasMany('Following', 'user_id');   
 }

在你的TV Show模型中…

 public function getFollowers(){
   return $this->hasMany('Following','show_id');
 }

这将允许您获得用户正在关注的所有显示的id。或者获取用户正在关注的所有显示的id…

然后你可以随意使用这些数据。

我95%肯定有更优雅的方法,我只是不能真正坐下来规划一个完整的基础设施:)

也……对于消息,您需要创建一个Message模型,然后每次在任何控制器中执行任何操作.....比如当有人关注或添加或删除任何....在message模型.......

中创建一个新的系统消息

这里有一个例子,如果你有一个用户点击链接www.yoursite.com/show/follow/2…然后将show和user添加到Following表中,如果执行成功,则向user发送消息。

public function get_follow($id){
$follow = New Following;
$follow->user_id = Auth::user()->id;
$follow->show_id = $id; //pass the show id 
$check = $follow->save();
  if($check){
   $message = New Message;
   $message->user_id = Auth::user()->id;
   $message->body = "You have followed ".Show::find($id)->showName;
   $message->seen = 0;
   $message->save();}
Return Redirect::back();
 }

然后当你有一个消息要发送一个节目,你做一个批量发送…不知道你会在哪里做....也许你可以有一个控制器,用来上传新剧集....去看演出……每次执行时,它都会执行消息批处理....类似下面的内容......

public function post_uploadNewEpisode($id){
  //Obviously you would have input handling for file upload etc
  //But we're not covering that part here....just the message part.
  $show = Show::find($id);
  $message = "There is a new episode of ".$show->showName." available for download!";
 $followers = Followers::where('show_id','=',$id)->get();
 //Iterate over follower ids, and send a message to each...
 foreach($followers as $val){
  $message = New Message;
  $message->user_id = $val->user_id;   
  $message->body = $message;
  $message->seen = 0;
  $message->save();
}}

完全未经测试的代码,就像我之前说的,我很确定有更好的方法来做到这一点,但我只是想到了我的头顶部…

但是我确定从这里,你可以得到如何实现这一切的想法。好运!:)