public override CoolQRouteMessage OnMessageReceived(CoolQScopeEventArgs scope) { var routeMsg = scope.RouteMessage; _routeMsg = routeMsg; _identity = (CoolQIdentity)_routeMsg.Identity; _plugins = PluginManager.Current.Plugins.OfType <MessagePlugin>() .Where(k => (k.MiddlewareConfig as BackendConfig)?.CanDisabled == true) .Where(k => k.TargetAuthority != Authority.Root); if (!DisabledList.ContainsKey(_identity)) { DisabledList.Add(_identity, new List <Guid>()); } _disabled = DisabledList[_identity]; if (_routeMsg.CurrentAuthority == Authority.Public && _routeMsg.MessageType == MessageType.Group) { return(_routeMsg.ToSource(DefaultReply.AdminOnly)); } if (List) { return(ShowPluginList()); } if (DisabledPlugin != null) { return(EnablePlugin()); } if (EnabledPlugin != null) { return(DisablePlugin()); } return(_routeMsg.ToSource(DefaultReply.ParamError)); }
public void Add(CoolQIdentity key, T value) { if (ContainsKey(key)) { throw new ArgumentOutOfRangeException($"Already contains identity: \"{key}\""); } List.Add(new ValuePair <T>(key, value)); }
public bool TryGetValue(CoolQIdentity key, out T value) { if (ContainsKey(key)) { value = this[key]; return(true); } value = default; return(false); }
private bool IsMeAlive(CoolQIdentity id) { if (!ExpireTimeCollection.ContainsKey(id)) { return(true); } var time = ExpireTimeCollection[id]; if (!time.HasValue) { return(true); } return(false); }
private void DispatchMessage(CoolQMessageApi coolQMessageApi) { CoolQIdentity cqIdentity; switch (coolQMessageApi) { case CoolQPrivateMessageApi privateMsg: cqIdentity = new CoolQIdentity(privateMsg.UserId, MessageType.Private); RunNext <CoolQPrivateMessageApi>(cqIdentity); break; case CoolQDiscussMessageApi discussMsg: cqIdentity = new CoolQIdentity(discussMsg.DiscussId, MessageType.Discuss); RunNext <CoolQDiscussMessageApi>(cqIdentity); break; case CoolQGroupMessageApi groupMsg: cqIdentity = new CoolQIdentity(groupMsg.GroupId, MessageType.Group); RunNext <CoolQGroupMessageApi>(cqIdentity); break; default: throw new ArgumentException(); } SessionList[cqIdentity].LockMsg = false; void RunNext <T>(CoolQIdentity id) where T : CoolQMessageApi { while (SessionList[id].MsgQueue.TryDequeue(out object current)) { var currentMsg = (T)current; try { CoolQRouteMessage coolQRouteMessage = CoolQRouteMessage.Parse(currentMsg); HandleMessage(new CoolQScopeEventArgs { DisabledApplications = new List <ApplicationPlugin>(), RouteMessage = coolQRouteMessage }); } catch (Exception ex) { Logger.Exception(ex); } } } }
public bool Remove(CoolQIdentity key) { if (!ContainsKey(key)) { return(false); } try { List.Remove(List.Single(k => k.Identity == key)); } catch (Exception e) { return(false); } return(true); }
public override bool Message_Received(object sender, MessageEventArgs args) { bool handled = false; var originObj = (CoolQMessageApi)args.ParsedObject; CoolQIdentity identity; switch (originObj) { case CoolQPrivateMessageApi privateMsg: identity = new CoolQIdentity(privateMsg.UserId, MessageType.Private); break; case CoolQDiscussMessageApi discussMsg: identity = new CoolQIdentity(discussMsg.DiscussId, MessageType.Discuss); break; case CoolQGroupMessageApi groupMsg: identity = new CoolQIdentity(groupMsg.GroupId, MessageType.Group); break; default: throw new ArgumentOutOfRangeException(); } if (!SessionList.ContainsKey(identity)) { SessionList.Add(identity, new Session(identity)); } if (SessionList[identity].MsgQueue.Count < SessionList[identity].MsgLimit) // 允许缓存n条,再多的丢弃 { SessionList[identity].MsgQueue.Enqueue(originObj); } else if (!SessionList[identity].LockMsg) { SessionList[identity].LockMsg = true; SendMessageAsync(CoolQRouteMessage.Parse(originObj).ToSource(originObj.Message)); } if (!SessionList[identity].TryRun(() => DispatchMessage(originObj))) { Logger.Info("当前已有" + SessionList[identity].MsgQueue.Count + "条消息在" + identity + "排队"); } return(handled); }
public Session(CoolQIdentity identity) { Identity = identity; switch (identity.Type) { case MessageType.Private: MsgLimit = 4; break; case MessageType.Discuss: MsgLimit = 10; break; case MessageType.Group: MsgLimit = 10; break; default: throw new ArgumentOutOfRangeException(); } }
public override bool Event_Received(object sender, EventEventArgs args) { var rootId = new CoolQIdentity(2241521134, MessageType.Private); bool handled = false; var obj = args.ParsedObject; switch (obj) { case string str: SendMessageAsync(new CoolQRouteMessage(str, rootId)); break; case FriendRequest friendRequest: { var msg = string.Format("{0} ({1})邀请加我为好友", CoolQHttpApiClient.GetStrangerInfo(friendRequest.UserId.ToString()).Data?.Nickname, friendRequest.UserId); SendMessageAsync(new CoolQRouteMessage(msg, rootId)); break; } case GroupInvite groupInvite: if (groupInvite.SubType == "invite") { var msg = string.Format("{0} ({1})邀请我加入群{2}", CoolQHttpApiClient.GetStrangerInfo(groupInvite.UserId.ToString()).Data?.Nickname, groupInvite.UserId, groupInvite.GroupId); SendMessageAsync(new CoolQRouteMessage(msg, rootId)); } break; case GroupAdminChange groupAdminChange: break; } return(handled); }
public T this[CoolQIdentity key] { get => List.Single(k => k.Identity == key).Value;
public bool ContainsKey(CoolQIdentity key) { return(List.Any(k => k.Identity == key)); }
public ValuePair(CoolQIdentity identity, T value) : this() { Identity = identity; this.Value = value; }
public RebootInfo(DateTime rebootTime, CoolQIdentity operatorId) { RebootTime = rebootTime; OperatorId = operatorId; }
private static IEnumerable <long> GetSubscribed(CoolQIdentity identity) { return(_userDic.Where(k => k.Value.Contains(identity)).Select(k => k.Key)); }