MCPcopy Create free account
hub / github.com/daodst/chat / getSenderDeviceForSpecUser

Function getSenderDeviceForSpecUser

clientapi/routing/group_master_notice.go:112–160  ·  view source on GitHub ↗

getSenderDeviceForSpecUser It returns an userapi.Device, which is used for building the event

(
	ctx context.Context,
	userId string,
	userAPI userapi.ClientUserAPI,
	cfg *config.ClientAPI,
)

Source from the content-addressed store, hash-verified

110// getSenderDeviceForSpecUser
111// It returns an userapi.Device, which is used for building the event
112func getSenderDeviceForSpecUser(
113 ctx context.Context,
114 userId string,
115 userAPI userapi.ClientUserAPI,
116 cfg *config.ClientAPI,
117) (*userapi.Device, error) {
118 // Check if we got existing devices
119 deviceRes := &userapi.QueryDevicesResponse{}
120 err := userAPI.QueryDevices(ctx, &userapi.QueryDevicesRequest{
121 UserID: userId,
122 }, deviceRes)
123 if err != nil {
124 return nil, err
125 }
126
127 if len(deviceRes.Devices) > 0 {
128 return &deviceRes.Devices[0], nil
129 }
130 local, domain, err := gomatrixserverlib.SplitID('@', userId)
131 if err != nil {
132 return nil, err
133 }
134 if domain != cfg.Matrix.ServerName {
135 return nil, errors.New("wrong servername")
136 }
137 // create an AccessToken
138 token, err := tokens.GenerateLoginToken(tokens.TokenOptions{
139 ServerPrivateKey: cfg.Matrix.PrivateKey.Seed(),
140 ServerName: string(cfg.Matrix.ServerName),
141 UserID: userId,
142 })
143 if err != nil {
144 return nil, err
145 }
146
147 // create a new device, if we didn't find any
148 var devRes userapi.PerformDeviceCreationResponse
149 err = userAPI.PerformDeviceCreation(ctx, &userapi.PerformDeviceCreationRequest{
150 Localpart: local,
151 DeviceDisplayName: &local,
152 AccessToken: token,
153 NoDeviceListUpdate: true,
154 }, &devRes)
155
156 if err != nil {
157 return nil, err
158 }
159 return devRes.Device, nil
160}

Callers 1

SendGroupMasterNoticeFunction · 0.85

Calls 2

QueryDevicesMethod · 0.65
PerformDeviceCreationMethod · 0.65

Tested by

no test coverage detected