抖音项目关系模块 | 青训营笔记

225 阅读4分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 13 天

需求分析

社交接口,实现用户之间的关注关系维护,登录用户能够关注或取关其他用户,同时自己能够看到自己关注过的所有用户列表,以及所有关注自己的用户列表,以及能够查看互相关注的好友列表。

/douyin/relation/action/ - 关系操作

  • 登录用户对其他用户进行关注或取消关注。
  • 接口类型:POST

/douyin/relatioin/follow/list/ - 用户关注列表​

  • 登录用户关注的所有用户列表。
  • 接口类型:GET

/douyin/relation/follower/list/ - 用户粉丝列表​

  • 所有关注登录用户的粉丝列表。​
  • 接口类型​:GET

/douyin/relation/friend/list/ - 用户好友列表​

  • 互相关注的用户列表。​
  • 接口类型:GET

接口定义

IDL 文件如下:

syntax = "proto3";  
package message;  
option go_package = "message";  
  
message BaseResp {  
  int32 status_code = 1;//状态码  
  string status_message = 2;//状态描述  
  int64 service_time = 3;//服务时间  
}  
  
message User {  
  int64 id = 1;//用户id  
  string name = 2;//用户名称  
  int64 follow_count = 3;//关注总数  
  int64 follower_count = 4;//粉丝总数  
  bool is_follow = 5;//true-已关注,false-未关注  
  string avatar = 6; // 用户头像  
  string background_image = 7; // 用户个人页顶部大图  
  string signature = 8; // 个人简介  
  int64 total_favorited = 9; // 获赞数量  
  int64 work_count = 10; // 作品数量  
  int64 favorite_count = 11; // 点赞数量  
}  
  
message Message {  
  int64 id = 1; // 消息id  
  int64 to_user_id = 2; // 该消息接收者的id  
  int64 from_user_id =3; // 该消息发送者的id  
  string content = 4; // 消息内容  
  int64 create_time = 5; // 消息创建时间  
}  
  
message message_chat_request {  
  string token = 1; // 用户鉴权token  
  int64 to_user_id = 2; // 对方用户id  
  int64 pre_msg_time = 3; // 上次最新消息的时间  
}  
  
message message_chat_response {  
  BaseResp base_resp = 1;  
  repeated Message message_list = 2; // 消息列  
}  
  
message message_action_request {  
  string token = 1; // 用户鉴权 token  int64 to_user_id = 2; // 对方用户 id  int32 action_type = 3; // 1- 发送消息  
  string content = 4; // 消息内容  
}  
  
message message_action_response {  
  BaseResp base_resp = 1;  
}  
  
service MessageService {  
  rpc MessageAction (message_action_request) returns (message_action_response) {}  
  rpc MessageChat (message_chat_request) returns (message_chat_response) {}  
}

数据表定义

数据表设计的基本思路是:定义一个关系表(Relation),含有两个字段 userId 和 toUserId,表示 userId 关注了 toUserId。查询的时候根据用户 ID 即可查到关注列表、粉丝列表以及好友列表。

// RelationRaw Gorm Data Structures
type RelationRaw struct {  
   gorm.Model  
   UserId   int64 `gorm:"column:user_id;not null;index:idx_userid"`  
   ToUserId int64 `gorm:"column:to_user_id;not null;index:idx_touserid"`  
}

微服务流程

关系操作

  • 首先根据发送请求的信息进行 JWT 认证,以及检查请求的 toUserId,看关注操作的对象是否存在,同时避免自己关注自己;
  • 接着,根据请求的 ActionType 字段,判断是关注还是取消关注其他用户,在 dal 层调用数据库具体的操作;
  • 如果 ActionType 是其他字段,说明请求的类型错误,直接返回。

用户关注列表

  • 首先根据发送请求的信息进行 JWT 认证,没有认证的表示没有登陆,也可以继续查看用户关注列表;
  • 同时对请求的 userId 进行校验,判断是否存在该用户;
  • 接着,根据 userId 在数据库中查询 userId 关注的列表,返回一个 ID 数组;
  • 在用户表中查询,根据 ID 得到用户信息;
  • 最后根据关注列表的用户信息打包返回响应的数据。

用户粉丝列表

和关注列表思路基本一致:

  • 首先根据发送请求的信息进行 JWT 认证,没有认证的表示没有登陆,也可以继续查看用户关注列表;
  • 同时对请求的 userId 进行校验,判断是否存在该用户;
  • 接着,根据 userId 在数据库中查询 userId 的粉丝列表(即数据库中的 toUserId = 请求的 userID),返回一个 ID 数组;
  • 在用户表中查询,根据 ID 得到用户信息;
  • 最后根据粉丝列表的用户信息打包返回响应的数据。

用户好友列表

和关注列表思路基本一致:

  • 首先根据发送请求的信息进行 JWT 认证,没有认证的表示没有登陆,也可以继续查看用户关注列表;
  • 同时对请求的 userId 进行校验,判断是否存在该用户;
  • 接着,根据 userId 在数据库中查询 userId 的好友列表(即两者互相关注),返回一个 ID 数组;
  • 在用户表中查询,根据 ID 得到用户信息;
  • 在消息表中查询,根据 userId 和 toUserId 查询最新的聊天消息;
  • 最后根据好友列表的用户信息打包返回响应的数据。