private void IfClosed(Action callback) { if (_client == null) { FizzUtils.DoCallback(callback); } else { FizzLogger.W("Client should have been closed."); } }
private void IfOpened(Action callback) { if (_restClient != null) { FizzUtils.DoCallback(callback); } else { FizzLogger.W("Moderation client should be opened before usage."); } }
public void Unsubscribe(string userId, Action <FizzException> callback) { IfOpened(() => { string path = string.Format(FizzConfig.API_PATH_USER_SUBCRIBERS, userId); _client.Delete(FizzConfig.API_BASE_URL, path, string.Empty, (response, ex) => { FizzUtils.DoCallback(ex, callback); }); }); }
public FizzJsonUserGroup(JSONNode json) { GroupId = json[KEY_GROUP_ID]; State = FizzUtils.ParseState(json[KEY_STATE]); Role = FizzUtils.ParseRole(json[KEY_ROLE]); if (json[KEY_LAST_READ_MESSAGE_ID] != null) { LastReadMessageId = (long)json[KEY_LAST_READ_MESSAGE_ID].AsDouble; } Created = (long)json[KEY_CREATED].AsDouble; }
private void IfClosed(Action callback) { if (_restClient == null) { FizzUtils.DoCallback(callback); } else { FizzLogger.W("Moderation client should be closed before opening."); } }
public void Unsubscribe(Action <FizzException> callback) { IfOpened(() => { string path = FizzConfig.API_PATH_USER_NOTIFICATIONS; _client.Delete(FizzConfig.API_BASE_URL, path, string.Empty, (response, ex) => { FizzUtils.DoCallback(ex, callback); }); }); }
private void IfOpened(Action callback) { if (_restClient != null) { FizzUtils.DoCallback(callback); } else { FizzLogger.W("Client should have been opened."); } }
private void IfClosed(Action callback) { if (string.IsNullOrEmpty(_userId)) { FizzUtils.DoCallback(callback); } else { FizzLogger.W("Chat client should be closed before opening."); } }
public void RemoveGroup(string userId, string groupId, Action <FizzException> callback) { IfOpened(() => { string path = string.Format(FizzConfig.API_PATH_USER_GROUP, userId, groupId); _client.Delete(FizzConfig.API_BASE_URL, path, string.Empty, (response, ex) => { FizzUtils.DoCallback(ex, callback); }); }); }
private FizzGroupMemberEventData ParseMemberEventData(FizzTopicMessage message) { JSONClass payload = JSONNode.Parse(message.Data).AsObject; FizzGroupMemberEventData data = new FizzGroupMemberEventData(); data.MemberId = payload["id"]; data.GroupId = message.From; data.State = FizzUtils.ParseState(payload["state"]); data.Role = FizzUtils.ParseRole(payload["role"]); return(data); }
public void PublishMessage( string channelId, string nick, string body, Dictionary <string, string> data, bool translate, bool filter, bool persist, Action <FizzException> callback) { IfOpened(() => { if (string.IsNullOrEmpty(channelId)) { FizzUtils.DoCallback(ERROR_INVALID_CHANNEL, callback); return; } try { string path = string.Format(FizzConfig.API_PATH_MESSAGES, channelId); JSONClass json = new JSONClass(); json[FizzJsonChannelMessage.KEY_NICK] = nick; json[FizzJsonChannelMessage.KEY_BODY] = body; json[FizzJsonChannelMessage.KEY_PERSIST].AsBool = persist; json[FizzJsonChannelMessage.KEY_FILTER].AsBool = filter; json[FizzJsonChannelMessage.KEY_TRANSLATE].AsBool = translate; string dataStr = string.Empty; if (data != null) { JSONClass dataJson = new JSONClass(); foreach (KeyValuePair <string, string> pair in data) { dataJson.Add(pair.Key, new JSONData(pair.Value)); } dataStr = dataJson.ToString(); } json[FizzJsonChannelMessage.KEY_DATA] = dataStr; _restClient.Post(FizzConfig.API_BASE_URL, path, json.ToString(), (response, ex) => { FizzUtils.DoCallback(ex, callback); }); } catch (FizzException ex) { FizzUtils.DoCallback(ex, callback); } }); }
public void Delay(int delayMS, Action action) { if (action == null || delayMS < 0) { FizzLogger.W("Invalid timer scheduled"); return; } lock (synclock) { timers.Add(FizzUtils.Now() + delayMS, action); } }
public void RemoveGroup(string groupId, Action <FizzException> cb) { if (Client.State == FizzClientState.Closed) { FizzLogger.W("FizzClient should be opened before removing group."); return; } Client.Chat.Users.RemoveGroup(UserId, groupId, ex => { FizzUtils.DoCallback(ex, cb); }); }
public void QueryLatest(string channel, int count, long beforeId, Action <IList <FizzChannelMessage>, FizzException> callback) { IfOpened(() => { if (string.IsNullOrEmpty(channel)) { FizzUtils.DoCallback <IList <FizzChannelMessage> > (null, ERROR_INVALID_CHANNEL, callback); return; } if (count < 0) { FizzUtils.DoCallback <IList <FizzChannelMessage> > (null, ERROR_INVALID_MESSAGE_QUERY_COUNT, callback); return; } if (count == 0) { FizzUtils.DoCallback <IList <FizzChannelMessage> > (new List <FizzChannelMessage> (), null, callback); return; } string path = string.Format(FizzConfig.API_PATH_MESSAGES, channel) + "?count=" + count; if (beforeId > 0) { path += "&before_id=" + beforeId; } _restClient.Get(FizzConfig.API_BASE_URL, path, (response, ex) => { if (ex != null) { FizzUtils.DoCallback <IList <FizzChannelMessage> > (null, ex, callback); } else { try { JSONArray messagesArr = JSONNode.Parse(response).AsArray; IList <FizzChannelMessage> messages = new List <FizzChannelMessage> (); foreach (JSONNode message in messagesArr.Childs) { messages.Add(new FizzJsonChannelMessage(message)); } FizzUtils.DoCallback <IList <FizzChannelMessage> > (messages, null, callback); } catch { FizzUtils.DoCallback <IList <FizzChannelMessage> > (null, ERROR_INVALID_RESPONSE_FORMAT, callback); } } }); }); }
private FizzEvent BuildEvent(string userId, string sessionId) { return(new FizzEvent( userId, FizzEventType.session_started, 1, sessionId, FizzUtils.Now(), "ios", "b1", "c1", "c2", "c3", null )); }
public void Subscribe(Action <FizzException> cb) { FizzService.Instance.Client.Chat.Subscribe(Id, ex => { if (ex == null) { if (FizzService.Instance.OnChannelSubscribed != null) { FizzService.Instance.OnChannelSubscribed.Invoke(Id); } } FizzUtils.DoCallback(ex, cb); }); }
public void Close(Action <FizzException> callback) { try { if (State == FizzClientState.Closed) { return; } Close(() => { FizzUtils.DoCallback(null, callback); }); } catch (FizzException ex) { FizzUtils.DoCallback(ex, callback); } }
public void Flush() { if (_flushInProgress) { FizzUtils.DoCallback(_onLogEmpty); return; } _flushInProgress = true; _eventLog.Read(128, items => { if (items.Count <= 0) { _flushInProgress = false; FizzUtils.DoCallback(_onLogEmpty); return; } PostEvents(items, (response, ex) => { bool rollLog = true; if (ex != null) { if (ex.Code == FizzError.ERROR_REQUEST_FAILED) { FizzLogger.W("Failed to submit events to service"); rollLog = false; } else if (ex.Code == FizzError.ERROR_INVALID_REQUEST) { FizzLogger.E("Submission of some events failed: " + ex.Message); } } if (rollLog) { _eventLog.RollTo(items[items.Count - 1]); } FizzUtils.DoCallback(_onLogEmpty); _flushInProgress = false; }); }); }
public void FetchGroup(string groupId, Action <IFizzGroup, FizzException> callback) { IfOpened(() => { string path = string.Format(FizzConfig.API_PATH_GROUP, groupId); _restClient.Get(FizzConfig.API_BASE_URL, path, (response, ex) => { if (ex != null) { FizzUtils.DoCallback <IFizzGroup> (null, ex, callback); } else { FizzUtils.DoCallback <IFizzGroup>(new FizzJsonGroup(response), null, callback); } }); }); }
public void TimerTest() { var dispatcher = new FizzActionDispatcher(); bool fired1 = false; bool fired2 = false; bool fired3 = false; long scheduledAt1 = FizzUtils.Now(); dispatcher.Delay(10, () => { long now = FizzUtils.Now(); Assert.IsTrue(now >= scheduledAt1 + 10); Assert.IsFalse(fired2); Assert.IsFalse(fired3); fired1 = true; }); long scheduledAt2 = FizzUtils.Now(); dispatcher.Delay(1000, () => { long now = FizzUtils.Now(); Assert.IsTrue(now >= scheduledAt2 + 1000); Assert.IsTrue(fired1); Assert.IsTrue(fired3); fired2 = true; }); long scheduledAt3 = FizzUtils.Now(); dispatcher.Delay(100, () => { long now = FizzUtils.Now(); Assert.IsTrue(now >= scheduledAt3 + 100); Assert.IsTrue(fired1); Assert.IsFalse(fired2); fired3 = true; }); while (!fired1 || !fired2 || !fired3) { dispatcher.Process(); } }
public void UpdateGroup(string groupId, Action <FizzException> cb) { if (Client.State == FizzClientState.Closed) { FizzLogger.W("FizzClient should be opened before joining group."); return; } Client.Chat.Users.UpdateGroup(UserId, groupId, null, null, ex => { if (ex == null) { GroupInvites.Remove(groupId); groupLookup[groupId].Channel.SubscribeAndQueryLatest(); } FizzUtils.DoCallback(ex, cb); }); }
public void Unsubscribe(string channel, Action <FizzException> callback) { IfOpened(() => { if (channel == null || string.IsNullOrEmpty(channel)) { FizzUtils.DoCallback(ERROR_INVALID_CHANNEL, callback); return; } string path = string.Format(FizzConfig.API_PATH_SUBSCRIBERS, channel); _restClient.Delete(FizzConfig.API_BASE_URL, path, "", (resp, ex) => { FizzUtils.DoCallback(ex, callback); }); }); }
public void Subscribe(Action <FizzException> cb) { if (Client.State == FizzClientState.Closed) { FizzLogger.W("FizzClient should be opened before subscribing user."); return; } Client.Chat.Users.Subscribe(Id, ex => { if (ex == null) { IsSubscribed = true; } FizzUtils.DoCallback(ex, cb); }); }
public void Close(Action cb) { _closeCallback = cb; if (_connection == null) { FizzUtils.DoCallback(_closeCallback); _closeCallback = null; return; } lock (synclock) { _sessionRepo.OnSessionUpdate -= OnSessionUpdate; _sessionRepo = null; var conn = _connection; _connection = null; conn.DisconnectAsync(); } }
private void ClosePreviousSession() { string lastSessionId = UnityEngine.PlayerPrefs.GetString(KEY_SESSION, string.Empty); if (!string.IsNullOrEmpty(lastSessionId)) { long sessionStartTs = FizzUtils.Now() + _timeOffset; long sessionUpdateTs = FizzUtils.Now() + _timeOffset; long.TryParse(UnityEngine.PlayerPrefs.GetString(KEY_SESSION_START_TS), out sessionStartTs); long.TryParse(UnityEngine.PlayerPrefs.GetString(KEY_SESSION_UPDATE_TS), out sessionUpdateTs); SessionEnded(lastSessionId, sessionStartTs, sessionUpdateTs); UnityEngine.PlayerPrefs.DeleteKey(KEY_SESSION); UnityEngine.PlayerPrefs.DeleteKey(KEY_SESSION_START_TS); UnityEngine.PlayerPrefs.DeleteKey(KEY_SESSION_UPDATE_TS); UnityEngine.PlayerPrefs.Save(); } }
private void GetUser(GetUserRequest request) { Client.Chat.Users.GetUser(request.UserId, (userMeta, ex) => { FizzUserModel user = null; if (ex == null) { user = new FizzUserModel(userMeta, Client); Users[user.Id] = user; } FizzUtils.DoCallback(user, ex, request.Callback); requestQueue.Dequeue(); if (requestQueue.Count > 0) { GetUser(requestQueue.Peek()); } }); }
public void Open(string userId, IFizzLanguageCode locale, FizzServices services, Action <FizzException> callback) { try { if (State == FizzClientState.Opened) { FizzUtils.DoCallback(null, callback); return; } FizzSessionRepository sessionRepo = new FizzSessionRepository(userId, locale.Code, _sessionClient); _authClient.Open(sessionRepo, ex => { if (ex == null) { if (services.HasFlag(FizzServices.Chat)) { _chat.Open(userId, _authClient, sessionRepo); } if (services.HasFlag(FizzServices.Analytics)) { _ingestionClient.Open(userId, sessionRepo.Session._serverTS, _authClient); } if (services.HasFlag(FizzServices.Moderation)) { _moderationClient.Open(_authClient); } State = FizzClientState.Opened; FizzUtils.DoCallback(null, callback); } else { FizzUtils.DoCallback(ex, callback); } }); } catch (FizzException ex) { FizzUtils.DoCallback(ex, callback); } }
public void IntegrityTest() { JSONClass json = new JSONClass(); long id = FizzUtils.Now(); long created = FizzUtils.Now(); json[FizzTopicMessage.KEY_ID].AsDouble = (double)id; json[FizzTopicMessage.KEY_TYPE] = "test"; json[FizzTopicMessage.KEY_FROM] = "from"; json[FizzTopicMessage.KEY_DATA] = "data"; json[FizzTopicMessage.KEY_CREATED].AsDouble = (double)created; var m = new FizzTopicMessage(json.ToString()); Assert.AreEqual(m.Id, id); Assert.AreEqual(m.Type, "test"); Assert.AreEqual(m.From, "from"); Assert.AreEqual(m.Data, "data"); Assert.AreEqual(m.Created, created); }
public virtual void PublishMessage(string nick, string body, string locale, Dictionary <string, string> data, bool translate, Action <FizzException> callback) { FizzService.Instance.Client.Chat.PublishMessage( Id, nick, body, locale, data, translate, Meta.FilterContent, Meta.PersistMessages, ex => { FizzUtils.DoCallback(ex, callback); }); }
public void InvalidInputTest() { var ex = Assert.Throws <FizzException>(() => new FizzIngestionClient(null, new FizzMockActionDispatcher())); Assert.AreEqual(ex.Message, "invalid_event_log"); ex = Assert.Throws <FizzException>(() => new FizzIngestionClient(new FizzInMemoryEventLog(), null)); Assert.AreEqual(ex.Message, "invalid_dispatcher"); var ingestion = new FizzIngestionClient( new FizzInMemoryEventLog(), new FizzMockActionDispatcher() ); ex = Assert.Throws <FizzException>(() => ingestion.Open(null, FizzUtils.Now(), new FizzMockAuthRestClient())); Assert.AreEqual(ex.Message, "invalid_user_id"); ex = Assert.Throws <FizzException>(() => ingestion.Open("userA", FizzUtils.Now(), null)); Assert.AreEqual(ex.Message, "invalid_client"); }