getSenderDeviceForSpecUser It returns an userapi.Device, which is used for building the event
( ctx context.Context, userId string, userAPI userapi.ClientUserAPI, cfg *config.ClientAPI, )
| 110 | // getSenderDeviceForSpecUser |
| 111 | // It returns an userapi.Device, which is used for building the event |
| 112 | func 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 | } |
no test coverage detected