|
- package api
-
- import (
- "context"
- "egg-im/internal/business/comm/utils"
- "egg-im/internal/logic/app"
- "egg-im/internal/logic/domain/group/repo"
- "egg-im/pkg/grpclib"
- "egg-im/pkg/pb"
- "google.golang.org/protobuf/proto"
- )
-
- type LogicExtServer struct{}
-
- func (s *LogicExtServer) SetGroupAddFriend(ctx context.Context, req *pb.SetGroupAddFriendReq) (*pb.Empty, error) {
- userId, _, err := grpclib.GetCtxData(ctx)
- if err != nil {
- return nil, err
- }
- err = app.GroupApp.SetGroupAddFriend(ctx, userId, req)
- if err != nil {
- return nil, err
- }
- return &pb.Empty{}, nil
- }
-
- func (s *LogicExtServer) SetGroupRemoveBannedMembers(ctx context.Context, in *pb.SetGroupMemberRemoveBannedReq) (*pb.Empty, error) {
- userId, _, err := grpclib.GetCtxData(ctx)
- if err != nil {
- return nil, err
- }
-
- err = app.GroupApp.SetGroupMemberRemoveBanned(ctx, userId, in.GroupId, in.RemoveUserIds)
- if err != nil {
- return nil, err
- }
- return &pb.Empty{}, nil
- }
-
- // SetGroupBannedMembers 设置禁言
- func (s *LogicExtServer) SetGroupBannedMembers(ctx context.Context, in *pb.SetGroupMemberBannedReq) (*pb.SetGroupMemberBannedResp, error) {
- userId, _, err := grpclib.GetCtxData(ctx)
- if err != nil {
- return nil, err
- }
-
- var isAllMemberBanned = false
- if in.IsAllMemberBanned == pb.AllMemberBannedType_YES_All_Member_Banned {
- isAllMemberBanned = true
- }
- members, err := app.GroupApp.SetGroupMemberBanned(ctx, userId, in.GroupId, in.UserIds, isAllMemberBanned)
- return &pb.SetGroupMemberBannedResp{Members: members, IsAllMemberBanned: in.IsAllMemberBanned}, err
- }
-
- // GetGroupBannedMembers 获取群组禁言列表
- func (s *LogicExtServer) GetGroupBannedMembers(ctx context.Context, in *pb.GetGroupBannedMembersReq) (*pb.GetGroupBannedMembersResp, error) {
- members, err := app.GroupApp.GetBannedMembers(ctx, in.GroupId)
- return &pb.GetGroupBannedMembersResp{Members: members}, err
- }
-
- func (s *LogicExtServer) SendRedPacket(ctx context.Context, in *pb.SendRedPacketReq) (*pb.SendRedPacketResp, 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.SendRedPackage(ctx, &sender, in)
- if err != nil {
- return nil, err
- }
- return &pb.SendRedPacketResp{Seq: seq}, nil
- }
-
- func (s *LogicExtServer) RecallMessage(ctx context.Context, in *pb.RecallMessageReq) (*pb.RecallMessageResp, error) {
- userId, deviceId, err := grpclib.GetCtxData(ctx)
- if err != nil {
- return nil, err
- }
- if in.MessageContentBack != "" {
- buf, err := proto.Marshal(&pb.RECALL{RecallSeq: utils.StrToInt64(in.MessageContentBack)})
- if err != nil {
- return nil, err
- }
- in.MessageContent = buf
- }
- sender := pb.Sender{
- SenderType: pb.SenderType_ST_USER,
- SenderId: userId,
- DeviceId: deviceId,
- }
- utils.FilePutContents("RecallMessage", utils.SerializeStr(map[string]interface{}{
- "args": in,
- }))
- seq, err := app.MessageApp.RecallMessage(ctx, &sender, in)
- if err != nil {
- return nil, err
- }
- return &pb.RecallMessageResp{Seq: seq}, nil
- }
-
- // 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) DeleteFriend(ctx context.Context, in *pb.DeleteFriendReq) (*pb.Empty, error) {
- userId, _, err := grpclib.GetCtxData(ctx)
- if err != nil {
- return nil, err
- }
- err = app.FriendApp.DeleteFriend(ctx, userId, in.UserId)
- 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, memberType, userStatusType, err := app.GroupApp.GetGroup(ctx, in.GroupId)
- if err != nil {
- return &pb.GetGroupResp{Group: group, MemberType: memberType, GroupUserStatusType: userStatusType}, err
- }
- groupNotice, err := repo.GroupRepo.GetGroupNotice(in.GroupId)
- return &pb.GetGroupResp{Group: group, MemberType: memberType, GroupUserStatusType: userStatusType, GroupNotice: &pb.GroupNotice{
- Content: groupNotice.Content,
- PublishType: pb.GroupNoticePublishType(groupNotice.PublishType),
- PublishTime: groupNotice.UpdateTime.Format("2006-01-02 15:04:05"),
- }}, 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) {
- userId, _, err := grpclib.GetCtxData(ctx)
- if err != nil {
- return nil, err
- }
- return &pb.Empty{}, app.GroupApp.UpdateMember(ctx, in, userId)
- }
-
- // 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, in.Limit)
- utils.FilePutContents("GetGroupMembers", utils.SerializeStr(map[string]interface{}{
- "req": map[string]interface{}{
- "group_id": in.GroupId,
- "limit": in.Limit,
- },
- "resp": members,
- }))
- return &pb.GetGroupMembersResp{Members: members}, err
- }
|