| 242 | } |
| 243 | |
| 244 | std::string EventSub::AddEventSubscription(std::shared_ptr<TwitchToken> token, |
| 245 | Subscription subscription) |
| 246 | { |
| 247 | auto eventSub = token->GetEventSub(); |
| 248 | if (!eventSub) { |
| 249 | blog(LOG_WARNING, "failed to get Twitch EventSub from token!"); |
| 250 | return ""; |
| 251 | } |
| 252 | |
| 253 | std::unique_lock<std::mutex> lock(eventSub->_subscriptionMtx); |
| 254 | if (!eventSub->_connected) { |
| 255 | if (eventSub->_reconnecting) { |
| 256 | return ""; |
| 257 | } |
| 258 | |
| 259 | vblog(LOG_INFO, |
| 260 | "new Twitch EventSub connect attempt started for %s", |
| 261 | token->GetName().c_str()); |
| 262 | lock.unlock(); |
| 263 | eventSub->Connect(); |
| 264 | return ""; |
| 265 | } |
| 266 | |
| 267 | if (isAlreadySubscribed(eventSub->_activeSubscriptions, subscription)) { |
| 268 | eventSub->LogActiveSubscriptions(); |
| 269 | return eventSub->_activeSubscriptions.find(subscription)->id; |
| 270 | } |
| 271 | |
| 272 | if (!eventSub->IsValidID(eventSub->_sessionID)) { |
| 273 | vblog(LOG_INFO, |
| 274 | "session ID of Twitch event sub invalid - skip %s", |
| 275 | __func__); |
| 276 | return ""; |
| 277 | } |
| 278 | |
| 279 | // Capture the session ID and release the lock before making the HTTP |
| 280 | // request. Holding the mutex during a network call would block |
| 281 | // SubscriptionIsActive() on the condition-check thread for the entire |
| 282 | // duration of the request, causing visibly slow macro evaluations. |
| 283 | const std::string sessionID = eventSub->_sessionID; |
| 284 | lock.unlock(); |
| 285 | |
| 286 | OBSDataAutoRelease postData = copyData(subscription.data); |
| 287 | setTransportData(postData.Get(), sessionID); |
| 288 | auto result = SendPostRequest(*token, registerSubscriptionURL.data(), |
| 289 | registerSubscriptionPath.data(), {}, |
| 290 | postData.Get()); |
| 291 | |
| 292 | lock.lock(); |
| 293 | |
| 294 | if (result.status != 202) { |
| 295 | const char *error = obs_data_get_string(result.data, "error"); |
| 296 | const char *message = |
| 297 | obs_data_get_string(result.data, "message"); |
| 298 | vblog(LOG_INFO, |
| 299 | "failed to register Twitch EventSub (%d): %s - %s", |
| 300 | result.status, error ? error : "no error", |
| 301 | message ? message : "no message"); |
nothing calls this directly
no test coverage detected