音讯体系设想与完成「下篇」

原文链接:BlueSun | 音讯体系设想与完成「上篇」

模子设想

Notify

id            : {type: 'integer', primaryKey: true},        // 主键
content     : {type: 'text'},    // 音讯的内容
type        : {type: 'integer', required: true, enum: [1, 2, 3]},  // 音讯的范例,1: 通告 Announce,2: 提示 Remind,3:信息 Message
target      : {type: 'integer'},    // 目的的ID
targetType  : {type: 'string'},    // 目的的范例
action      : {type: 'string'},    // 提示信息的行动范例
sender      : {type: 'integer'},    // 发送者的ID
createdAt    : {type: 'datetime', required: true}

Save Remind
音讯表,我们须要targettargetType字段,来纪录该条提示所关联的对象。而action字段,则纪录该条提示所关联的行动。
比方音讯:「小明喜好了文章」
则:

target = 123,  // 文章ID
targetType = 'post',  // 指明target所属范例是文章
sender = 123456  // 小明ID

Save Announce and Message
固然,Notify还支撑存储通告和信息。它们会用到content字段,而不会用到targettargetTypeaction字段。

UserNotify

id            : {type: 'integer', primaryKey: true},        // 主键
isRead      : {type: 'boolean', required: true},   
user        : {type: 'integer', required: true},  // 用户音讯所属者
notify      : {type: 'integer', required: true}   // 关联的Notify
createdAt    : {type: 'datetime', required: true}

我们用UserNotify来存储用户的音讯行列,它关联一则提示(Notify)的具体内容。
UserNotify的建立,重要经由过程两个门路:

  1. 遍历定阅(Subscription)表拉取通告(Announce)和提示(Remind)的时候建立

  2. 新建信息(Message)以后,马上建立。

Subscription

target      : {type: 'integer', required: true},    // 目的的ID
targetType  : {type: 'string', required: true},    // 目的的范例
action      : {type: 'string'},   // 定阅行动,如: comment/like/post/update etc.
user        : {type: 'integer'},
createdAt    : {type: 'datetime', required: true}

定阅,是从Notify表拉取音讯到UserNotify的前提,用户起首定阅了某一个目的的某一个行动,在此以后发生这个目的的这个行动的音讯,才会被关照到该用户。
如:「小明关注了产物A的批评」,数据表现为:

target: 123,  // 产物A的ID
targetType: 'product',
action: 'comment',
user: 123  // 小明的ID

如许,产物A下发生的每一条批评,都邑发生关照给小清楚明了。

SubscriptionConfig

action: {type: 'json', required: true},   // 用户的设置
user: {type: 'integer'}

差别用户能够会有不一样的定阅习气,在这个表中,用户能够一致针对某种行动举行是不是定阅的设置。而默许是运用体系供应的默许设置:

defaultSubscriptionConfig: {
  'comment'   : true,    // 批评
  'like'      : true,    // 喜好
}

在这套模子中,targetTypeaction是能够依据需求来扩大的,比方我们还能够增加多几个行动的提示:hate被踩、update被更新….诸如此类。

设置文件 NotifyConfig

// 提示关联的目的范例
targetType: {
  PRODUCT : 'product',    // 产物
  POST    : 'post'    // 文章
},

// 提示关联的行动
action: {
  COMMENT   : 'comment',  // 批评
  LIKE      : 'like',     // 喜好
},

// 定阅缘由对应定阅事宜
reasonAction: {
  'create_product'  : ['comment', 'like']
  'like_product'    : ['comment'],
  'like_post'       : ['comment'],
},

// 默许定阅设置
defaultSubscriptionConfig: {
  'comment'   : true,    // 批评
  'like'      : true,    // 喜好
}

效劳层 NotifyService

NotifyService具有以下要领:

  • createAnnounce(content, sender)

  • createRemind(target, targetType, action, sender, content)

  • createMessage(content, sender, receiver)

  • pullAnnounce(user)

  • pullRemind(user)

  • subscribe(user, target, targetType, reason)

  • cancelSubscription(user, target ,targetType)

  • getSubscriptionConfig(userID)

  • updateSubscriptionConfig(userID)

  • getUserNotify(userID)

  • read(user, notifyIDs)

各要领的处置惩罚逻辑以下:

createAnnounce(content, sender)

  1. 往Notify表中插进去一条通告纪录

createRemind(target, targetType, action, sender, content)

  1. 往Notify表中插进去一条提示纪录

createMessage(content, sender, receiver)

  1. 往Notify表中插进去一条信息纪录

  2. 往UserNotify表中插进去一条纪录,并关联新建的Notify

pullAnnounce(user)

  1. 从UserNotify中猎取近来的一条通告信息的建立时候: lastTime

  2. lastTime作为过滤前提,查询Notify的通告信息

  3. 新建UserNotify并关联查询出来的通告信息

pullRemind(user)

  1. 查询用户的定阅表,获得用户的一系列定阅纪录

  2. 经由过程每一条的定阅纪录的targettargetTypeactioncreatedAt去查询Notify表,猎取定阅的Notify纪录。(注重定阅时候必需早于提示建立时候)

  3. 查询用户的设置文件SubscriptionConfig,假如没有则运用默许的设置DefaultSubscriptionConfig

  4. 运用定阅设置,过滤查询出来的Notify

  5. 运用过滤好的Notify作为关联新建UserNotify

subscribe(user, target, targetType, reason)

  1. 经由过程reason,查询NotifyConfig,猎取对应的行动组:actions

  2. 遍历行动组,每个行动新建一则Subscription纪录

cancelSubscription(user, target ,targetType)

  1. 删除usertargettargetType对应的一则或多则纪录

getSubscriptionConfig(userID)

  1. 查询SubscriptionConfig表,猎取用户的定阅设置

updateSubscriptionConfig(userID)

  1. 更新用户的SubscriptionConfig纪录

getUserNotify(userID)

  1. 猎取用户的音讯列表

read(user, notifyIDs)

  1. 更新指定的notify,把isRead属性设置为true

时序图

提示的定阅、建立、拉取

《音讯体系设想与完成「下篇」》

我们能够在产物建立以后,挪用NotifyService.subscribe要领,
然后在产物被批评以后挪用NotifyService.createRemind要领,
再就是用户登录体系或许其他的某一个时候挪用NotifyService.pullRemind要领,
末了在用户查询音讯行列的时候挪用NotifyService.getUserNotify要领。

通告的建立、拉取

《音讯体系设想与完成「下篇」》

在管理员发送了一则通告的时候,挪用NotifyService.createAnnounce要领,
然后在用户登录体系或许其他的某一个时候挪用NotifyService.pullAnnounce要领,
末了在用户查询音讯行列的时候挪用NotifyService.getUserNotify要领。

信息的建立

《音讯体系设想与完成「下篇」》
信息的建立,只须要直接挪用NotifyService.createMessage要领就能够了,
鄙人一次用户查询音讯行列的时候,就会查询这条信息。

假如本文对您有效
请不要悭吝你们的Follow与Start
这会大大支撑我们继承创作

「Github」
MZMonster :@MZMonster
JC_Huang :@JerryC8080

    原文作者:JerryC
    原文地址: https://segmentfault.com/a/1190000004619723
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞