|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package app
-
- import (
- "context"
- "gim/internal/logic/domain/group/model"
- "gim/internal/logic/domain/group/repo"
- "gim/pkg/pb"
- )
-
- type groupApp struct{}
-
- var GroupApp = new(groupApp)
-
- // CreateGroup 创建群组
- func (*groupApp) CreateGroup(ctx context.Context, userId int64, in *pb.CreateGroupReq) (int64, error) {
- group := model.CreateGroup(userId, in)
- err := repo.GroupRepo.Save(group)
- if err != nil {
- return 0, err
- }
- return group.Id, nil
- }
-
- // GetGroup 获取群组信息
- func (*groupApp) GetGroup(ctx context.Context, groupId int64) (*pb.Group, error) {
- group, err := repo.GroupRepo.Get(groupId)
- if err != nil {
- return nil, err
- }
-
- return group.ToProto(), nil
- }
-
- // GetUserGroups 获取用户加入的群组列表
- func (*groupApp) GetUserGroups(ctx context.Context, userId int64) ([]*pb.Group, error) {
- groups, err := repo.GroupUserRepo.ListByUserId(userId)
- if err != nil {
- return nil, err
- }
-
- pbGroups := make([]*pb.Group, len(groups))
- for i := range groups {
- pbGroups[i] = groups[i].ToProto()
- }
- return pbGroups, nil
- }
-
- // Update 更新群组
- func (*groupApp) Update(ctx context.Context, userId int64, update *pb.UpdateGroupReq) error {
- group, err := repo.GroupRepo.Get(update.GroupId)
- if err != nil {
- return err
- }
-
- err = group.Update(ctx, update)
- if err != nil {
- return err
- }
-
- err = repo.GroupRepo.Save(group)
- if err != nil {
- return err
- }
-
- err = group.PushUpdate(ctx, userId)
- if err != nil {
- return err
- }
- return nil
- }
-
- // AddMembers 添加群组成员
- func (*groupApp) AddMembers(ctx context.Context, userId, groupId int64, userIds []int64) ([]int64, error) {
- group, err := repo.GroupRepo.Get(groupId)
- if err != nil {
- return nil, err
- }
- existIds, addedIds, err := group.AddMembers(ctx, userIds)
- if err != nil {
- return nil, err
- }
- err = repo.GroupRepo.Save(group)
- if err != nil {
- return nil, err
- }
-
- err = group.PushAddMember(ctx, userId, addedIds)
- if err != nil {
- return nil, err
- }
- return existIds, nil
- }
-
- // UpdateMember 更新群组用户
- func (*groupApp) UpdateMember(ctx context.Context, in *pb.UpdateGroupMemberReq) error {
- group, err := repo.GroupRepo.Get(in.GroupId)
- if err != nil {
- return err
- }
- err = group.UpdateMember(ctx, in)
- if err != nil {
- return err
- }
- err = repo.GroupRepo.Save(group)
- if err != nil {
- return err
- }
- return nil
- }
-
- // DeleteMember 删除群组成员
- func (*groupApp) DeleteMember(ctx context.Context, groupId int64, userId int64, optId int64) error {
- group, err := repo.GroupRepo.Get(groupId)
- if err != nil {
- return err
- }
- err = group.DeleteMember(ctx, userId)
- if err != nil {
- return err
- }
- err = repo.GroupRepo.Save(group)
- if err != nil {
- return err
- }
-
- err = group.PushDeleteMember(ctx, optId, userId)
- if err != nil {
- return err
- }
- return nil
- }
-
- // GetMembers 获取群组成员
- func (*groupApp) GetMembers(ctx context.Context, groupId int64) ([]*pb.GroupMember, error) {
- group, err := repo.GroupRepo.Get(groupId)
- if err != nil {
- return nil, err
- }
- return group.GetMembers(ctx)
- }
-
- // SendMessage 获取群组成员
- func (*groupApp) SendMessage(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
- group, err := repo.GroupRepo.Get(req.ReceiverId)
- if err != nil {
- return 0, err
- }
-
- return group.SendMessage(ctx, sender, req)
- }
|