Twitch SDK (Internal)
userrepository.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2016 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 #pragma once
10 
14 #include "twitchsdk/core/timer.h"
16 
17 #include <memory>
18 #include <map>
19 
20 
21 namespace ttv
22 {
23  class TaskRunner;
24  class User;
25  class UserRepository;
26  class GetUserTask;
27 }
28 
29 
31 {
32 public:
33  using UserInfoLookupCallback = std::function<void(TTV_ErrorCode ec, const UserInfo& userInfo)>;
34 
35 public:
37 
41  std::shared_ptr<User> GetAnonymousUser() const { return mAnonymousUser; }
42 
46  std::shared_ptr<User> RegisterUser(UserId userId);
47 
51  std::shared_ptr<User> GetUser(UserId userId);
52 
56  TTV_ErrorCode GetUsers(std::vector<std::shared_ptr<User>>& users);
57 
62 
67 
71  TTV_ErrorCode FetchUserInfoByName(const std::string& username, UserInfoLookupCallback callback);
72 
76  TTV_ErrorCode FetchUserInfoByAuthToken(const std::shared_ptr<OAuthToken>& authToken, UserInfoLookupCallback callback);
77 
82 
86  TTV_ErrorCode GetUserInfoByName(const std::string& username, UserInfo& result);
87 
88  // Component overrides
89  virtual TTV_ErrorCode Initialize() override;
90  virtual void Update() override;
91  virtual TTV_ErrorCode Shutdown() override;
92  virtual std::string GetLoggerName() const override;
93 
94 private:
96  {
97  std::string username;
98  std::shared_ptr<OAuthToken> authToken;
101  std::shared_ptr<GetUserTask> task;
103  };
104 
105  // Component overrides
106  virtual bool CheckShutdown() override;
107  virtual void CompleteShutdown() override;
108 
110  void CompleteUserInfoLookup(const UserInfo& userInfo, TTV_ErrorCode ec);
111 
112  TTV_ErrorCode CacheUserInfo(const UserInfo& userInfo);
113 
114  std::map<UserId, std::shared_ptr<User>> mUsers;
115  std::vector<std::shared_ptr<User>> mCleanupUsers;
116  std::map<UserId, UserInfo> mUserInfo;
117  std::vector<UserInfoLookupEntry> mPendingUserInfoLookups;
118  std::shared_ptr<User> mAnonymousUser;
119 
121 };
uint32_t UserId
Definition: coretypes.h:22
UserInfoLookupCallback callback
Definition: userrepository.h:100
TTV_ErrorCode UnRegisterUser(UserId userId)
TTV_ErrorCode FetchUserInfoById(UserId userId, UserInfoLookupCallback callback)
virtual TTV_ErrorCode Initialize() override
std::shared_ptr< User > RegisterUser(UserId userId)
virtual TTV_ErrorCode Shutdown() override
virtual bool CheckShutdown() override
Definition: coretypes.h:32
std::shared_ptr< User > mAnonymousUser
Definition: userrepository.h:118
virtual void Update() override
TTV_ErrorCode GetUsers(std::vector< std::shared_ptr< User >> &users)
bool forceRefetch
Definition: userrepository.h:102
std::map< UserId, UserInfo > mUserInfo
A mapping of username to UserInfo.
Definition: userrepository.h:116
TTV_ErrorCode GetUserInfoByName(const std::string &username, UserInfo &result)
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
void CompleteUserInfoLookup(const UserInfo &userInfo, TTV_ErrorCode ec)
std::shared_ptr< GetUserTask > task
Definition: userrepository.h:101
UserId userId
Definition: userrepository.h:99
Definition: component.h:50
std::vector< UserInfoLookupEntry > mPendingUserInfoLookups
Definition: userrepository.h:117
bool mLookupInProgress
Definition: userrepository.h:120
uint32_t TTV_ErrorCode
Definition: errortypes.h:30
std::shared_ptr< User > GetUser(UserId userId)
std::function< void(TTV_ErrorCode ec, const UserInfo &userInfo)> UserInfoLookupCallback
Definition: userrepository.h:33
Definition: userrepository.h:30
TTV_ErrorCode GetUserInfoById(UserId userId, UserInfo &result)
std::shared_ptr< OAuthToken > authToken
Definition: userrepository.h:98
virtual void CompleteShutdown() override
virtual std::string GetLoggerName() const override
TTV_ErrorCode FetchUserInfoByAuthToken(const std::shared_ptr< OAuthToken > &authToken, UserInfoLookupCallback callback)
TTV_ErrorCode FetchUserInfoByName(const std::string &username, UserInfoLookupCallback callback)
std::shared_ptr< User > GetAnonymousUser() const
Definition: userrepository.h:41
TTV_ErrorCode ScheduleUserInfoLookup()
Definition: userrepository.h:95
std::vector< std::shared_ptr< User > > mCleanupUsers
The users that are shutting down.
Definition: userrepository.h:115
std::map< UserId, std::shared_ptr< User > > mUsers
Definition: userrepository.h:114
TTV_ErrorCode CacheUserInfo(const UserInfo &userInfo)
std::string username
Definition: userrepository.h:97