getSenderDevice creates a user account to be used when sending server notices. It returns an userapi.Device, which is used for building the event
( ctx context.Context, userAPI userapi.ClientUserAPI, cfg *config.ClientAPI, )
| 278 | // getSenderDevice creates a user account to be used when sending server notices. |
| 279 | // It returns an userapi.Device, which is used for building the event |
| 280 | func getSenderDevice( |
| 281 | ctx context.Context, |
| 282 | userAPI userapi.ClientUserAPI, |
| 283 | cfg *config.ClientAPI, |
| 284 | ) (*userapi.Device, error) { |
| 285 | var accRes userapi.PerformAccountCreationResponse |
| 286 | // create account if it doesn't exist |
| 287 | err := userAPI.PerformAccountCreation(ctx, &userapi.PerformAccountCreationRequest{ |
| 288 | AccountType: userapi.AccountTypeAdmin, // admin |
| 289 | Localpart: cfg.Matrix.ServerNotices.LocalPart, |
| 290 | OnConflict: userapi.ConflictUpdate, |
| 291 | }, &accRes) |
| 292 | if err != nil { |
| 293 | return nil, err |
| 294 | } |
| 295 | |
| 296 | // set the avatarurl for the user |
| 297 | res := &userapi.PerformSetAvatarURLResponse{} |
| 298 | if err = userAPI.SetAvatarURL(ctx, &userapi.PerformSetAvatarURLRequest{ |
| 299 | Localpart: cfg.Matrix.ServerNotices.LocalPart, |
| 300 | AvatarURL: cfg.Matrix.ServerNotices.AvatarURL, |
| 301 | }, res); err != nil { |
| 302 | util.GetLogger(ctx).WithError(err).Error("userAPI.SetAvatarURL failed") |
| 303 | return nil, err |
| 304 | } |
| 305 | |
| 306 | // Check if we got existing devices |
| 307 | deviceRes := &userapi.QueryDevicesResponse{} |
| 308 | err = userAPI.QueryDevices(ctx, &userapi.QueryDevicesRequest{ |
| 309 | UserID: accRes.Account.UserID, |
| 310 | }, deviceRes) |
| 311 | if err != nil { |
| 312 | return nil, err |
| 313 | } |
| 314 | |
| 315 | if len(deviceRes.Devices) > 0 { |
| 316 | return &deviceRes.Devices[0], nil |
| 317 | } |
| 318 | |
| 319 | // create an AccessToken |
| 320 | token, err := tokens.GenerateLoginToken(tokens.TokenOptions{ |
| 321 | ServerPrivateKey: cfg.Matrix.PrivateKey.Seed(), |
| 322 | ServerName: string(cfg.Matrix.ServerName), |
| 323 | UserID: accRes.Account.UserID, |
| 324 | }) |
| 325 | if err != nil { |
| 326 | return nil, err |
| 327 | } |
| 328 | |
| 329 | // create a new device, if we didn't find any |
| 330 | var devRes userapi.PerformDeviceCreationResponse |
| 331 | err = userAPI.PerformDeviceCreation(ctx, &userapi.PerformDeviceCreationRequest{ |
| 332 | Localpart: cfg.Matrix.ServerNotices.LocalPart, |
| 333 | DeviceDisplayName: &cfg.Matrix.ServerNotices.LocalPart, |
| 334 | AccessToken: token, |
| 335 | NoDeviceListUpdate: true, |
| 336 | }, &devRes) |
| 337 |
no test coverage detected