|
| 1 | +package connector |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "go.mau.fi/util/ptr" |
| 9 | + "maunium.net/go/mautrix/bridgev2" |
| 10 | + "maunium.net/go/mautrix/bridgev2/database" |
| 11 | + "maunium.net/go/mautrix/bridgev2/networkid" |
| 12 | + "maunium.net/go/mautrix/event" |
| 13 | + |
| 14 | + "go.mau.fi/mautrix-linkedin/pkg/linkedingo" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + _ bridgev2.GhostDMCreatingNetworkAPI = (*LinkedInClient)(nil) |
| 19 | + _ bridgev2.GroupCreatingNetworkAPI = (*LinkedInClient)(nil) |
| 20 | + _ bridgev2.IdentifierValidatingNetwork = (*LinkedInConnector)(nil) |
| 21 | +) |
| 22 | + |
| 23 | +func (l *LinkedInConnector) ValidateUserID(uid networkid.UserID) bool { |
| 24 | + id := string(uid) |
| 25 | + _, err := base64.StdEncoding.DecodeString(id) |
| 26 | + return err == nil && len(id) >= 36 |
| 27 | +} |
| 28 | + |
| 29 | +func (l *LinkedInClient) ResolveIdentifier(ctx context.Context, identifier string, createChat bool) (*bridgev2.ResolveIdentifierResponse, error) { |
| 30 | + id := networkid.UserID(identifier) |
| 31 | + if !l.main.ValidateUserID(id) { |
| 32 | + return nil, fmt.Errorf("invalid identifier: %s", identifier) |
| 33 | + } |
| 34 | + ghost, err := l.main.Bridge.GetGhostByID(ctx, id) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + var chat *bridgev2.CreateChatResponse |
| 39 | + if createChat { |
| 40 | + portal, _ := ghost.Bridge.GetDMPortal(ctx, l.userLogin.ID, id) |
| 41 | + if portal != nil { |
| 42 | + chat = &bridgev2.CreateChatResponse{ |
| 43 | + PortalKey: portal.PortalKey, |
| 44 | + } |
| 45 | + } else { |
| 46 | + chatInfo := &bridgev2.ChatInfo{ |
| 47 | + Type: ptr.Ptr(database.RoomTypeDM), |
| 48 | + Members: &bridgev2.ChatMemberList{ |
| 49 | + MemberMap: map[networkid.UserID]bridgev2.ChatMember{}, |
| 50 | + }, |
| 51 | + } |
| 52 | + participants := []networkid.UserID{ |
| 53 | + networkid.UserID(identifier), |
| 54 | + } |
| 55 | + var err error |
| 56 | + chat, err = l.createChat(ctx, chatInfo, participants) |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("failed to create dm chat: %w", err) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return &bridgev2.ResolveIdentifierResponse{ |
| 63 | + UserID: id, |
| 64 | + Ghost: ghost, |
| 65 | + Chat: chat, |
| 66 | + }, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (l *LinkedInClient) CreateChatWithGhost(ctx context.Context, ghost *bridgev2.Ghost) (*bridgev2.CreateChatResponse, error) { |
| 70 | + resp, err := l.ResolveIdentifier(ctx, string(ghost.ID), true) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } else if resp == nil { |
| 74 | + return nil, nil |
| 75 | + } |
| 76 | + return resp.Chat, nil |
| 77 | +} |
| 78 | + |
| 79 | +func (l *LinkedInClient) CreateGroup(ctx context.Context, params *bridgev2.GroupCreateParams) (*bridgev2.CreateChatResponse, error) { |
| 80 | + chatInfo := &bridgev2.ChatInfo{ |
| 81 | + Type: ptr.Ptr(database.RoomTypeDefault), |
| 82 | + Name: ptr.Ptr(params.Name.Name), |
| 83 | + Members: &bridgev2.ChatMemberList{ |
| 84 | + MemberMap: map[networkid.UserID]bridgev2.ChatMember{}, |
| 85 | + }, |
| 86 | + } |
| 87 | + chat, err := l.createChat(ctx, chatInfo, params.Participants) |
| 88 | + if err != nil { |
| 89 | + return nil, fmt.Errorf("failed to create group chat: %w", err) |
| 90 | + } |
| 91 | + return chat, nil |
| 92 | +} |
| 93 | + |
| 94 | +func (l *LinkedInClient) createChat(ctx context.Context, chatInfo *bridgev2.ChatInfo, _participants []networkid.UserID) (*bridgev2.CreateChatResponse, error) { |
| 95 | + participants := make([]linkedingo.URN, len(_participants)) |
| 96 | + for i, participant := range _participants { |
| 97 | + participants[i] = linkedingo.NewURN(participant).AsFsdProfile() |
| 98 | + sender := l.makeSender(linkedingo.MessagingParticipant{ |
| 99 | + EntityURN: participants[i], |
| 100 | + }) |
| 101 | + chatInfo.Members.MemberMap[sender.Sender] = bridgev2.ChatMember{ |
| 102 | + EventSender: sender, |
| 103 | + Membership: event.MembershipJoin, |
| 104 | + } |
| 105 | + } |
| 106 | + resp, err := l.client.NewChat(ctx, ptr.Val(chatInfo.Name), participants) |
| 107 | + |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + portalKey := networkid.PortalKey{ |
| 113 | + ID: networkid.PortalID(resp.Data.ConversationURN.String()), |
| 114 | + Receiver: l.userLogin.ID, |
| 115 | + } |
| 116 | + return &bridgev2.CreateChatResponse{ |
| 117 | + PortalKey: portalKey, |
| 118 | + PortalInfo: chatInfo, |
| 119 | + }, nil |
| 120 | +} |
0 commit comments