package api import ( "context" "gim/internal/logic/app" "gim/pkg/grpclib" "gim/pkg/pb" "google.golang.org/protobuf/proto" ) type LogicExtServer struct{} // RegisterDevice 注册设备 func (*LogicExtServer) RegisterDevice(ctx context.Context, in *pb.RegisterDeviceReq) (*pb.RegisterDeviceResp, error) { deviceId, err := app.DeviceApp.Register(ctx, in) return &pb.RegisterDeviceResp{DeviceId: deviceId}, err } // SendMessage 发送消息 func (*LogicExtServer) SendMessage(ctx context.Context, in *pb.SendMessageReq) (*pb.SendMessageResp, error) { userId, deviceId, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } if in.MessageContentBack != "" { buf, err := proto.Marshal(&pb.Text{ Text: in.MessageContentBack, }) if err != nil { return nil, err } in.MessageContent = buf } sender := pb.Sender{ SenderType: pb.SenderType_ST_USER, SenderId: userId, DeviceId: deviceId, } seq, err := app.MessageApp.SendMessage(ctx, &sender, in) if err != nil { return nil, err } return &pb.SendMessageResp{Seq: seq}, nil } // PushRoom 推送房间 func (s *LogicExtServer) PushRoom(ctx context.Context, req *pb.PushRoomReq) (*pb.Empty, error) { userId, deviceId, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } return &pb.Empty{}, app.RoomApp.Push(ctx, &pb.Sender{ SenderType: pb.SenderType_ST_USER, SenderId: userId, DeviceId: deviceId, }, req) } func (s *LogicExtServer) AddFriend(ctx context.Context, in *pb.AddFriendReq) (*pb.Empty, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } err = app.FriendApp.AddFriend(ctx, userId, in.FriendId, in.Remarks, in.Description) if err != nil { return nil, err } return &pb.Empty{}, nil } func (s *LogicExtServer) AgreeAddFriend(ctx context.Context, in *pb.AgreeAddFriendReq) (*pb.Empty, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } err = app.FriendApp.AgreeAddFriend(ctx, userId, in.UserId, in.Remarks) if err != nil { return nil, err } return &pb.Empty{}, nil } func (s *LogicExtServer) SetFriend(ctx context.Context, req *pb.SetFriendReq) (*pb.SetFriendResp, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } app.FriendApp.SetFriend(ctx, userId, req) if err != nil { return nil, err } return &pb.SetFriendResp{}, nil } func (s *LogicExtServer) GetFriends(ctx context.Context, in *pb.Empty) (*pb.GetFriendsResp, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } friends, err := app.FriendApp.List(ctx, userId) return &pb.GetFriendsResp{Friends: friends}, err } // CreateGroup 创建群组 func (*LogicExtServer) CreateGroup(ctx context.Context, in *pb.CreateGroupReq) (*pb.CreateGroupResp, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } groupId, err := app.GroupApp.CreateGroup(ctx, userId, in) return &pb.CreateGroupResp{GroupId: groupId}, err } // UpdateGroup 更新群组 func (*LogicExtServer) UpdateGroup(ctx context.Context, in *pb.UpdateGroupReq) (*pb.Empty, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } return &pb.Empty{}, app.GroupApp.Update(ctx, userId, in) } // GetGroup 获取群组信息 func (*LogicExtServer) GetGroup(ctx context.Context, in *pb.GetGroupReq) (*pb.GetGroupResp, error) { group, err := app.GroupApp.GetGroup(ctx, in.GroupId) return &pb.GetGroupResp{Group: group}, err } // GetGroups 获取用户加入的所有群组 func (*LogicExtServer) GetGroups(ctx context.Context, in *pb.Empty) (*pb.GetGroupsResp, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } groups, err := app.GroupApp.GetUserGroups(ctx, userId) return &pb.GetGroupsResp{Groups: groups}, err } func (s *LogicExtServer) AddGroupMembers(ctx context.Context, in *pb.AddGroupMembersReq) (*pb.AddGroupMembersResp, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } userIds, err := app.GroupApp.AddMembers(ctx, userId, in.GroupId, in.UserIds) return &pb.AddGroupMembersResp{UserIds: userIds}, err } // UpdateGroupMember 更新群组成员信息 func (*LogicExtServer) UpdateGroupMember(ctx context.Context, in *pb.UpdateGroupMemberReq) (*pb.Empty, error) { return &pb.Empty{}, app.GroupApp.UpdateMember(ctx, in) } // DeleteGroupMember 添加群组成员 func (*LogicExtServer) DeleteGroupMember(ctx context.Context, in *pb.DeleteGroupMemberReq) (*pb.Empty, error) { userId, _, err := grpclib.GetCtxData(ctx) if err != nil { return nil, err } err = app.GroupApp.DeleteMember(ctx, in.GroupId, in.UserId, userId) return &pb.Empty{}, err } // GetGroupMembers 获取群组成员信息 func (s *LogicExtServer) GetGroupMembers(ctx context.Context, in *pb.GetGroupMembersReq) (*pb.GetGroupMembersResp, error) { members, err := app.GroupApp.GetMembers(ctx, in.GroupId) return &pb.GetGroupMembersResp{Members: members}, err }