/// <summary> /// 通过订阅者位置移除订阅者 /// </summary> /// <param name="position">订阅者位置</param> /// <returns>结果</returns> public async Task<bool> RemoveSubsriberAt(int position) { if (position < 0 || position > instance.SubscriberList.Count) { return false; } Subscriber subscriber = instance.SubscriberList[position]; Topic removedTopic = GetTopicByName(subscriber.TopicName); //订阅者列表更新 instance.SubscriberList.Remove(subscriber); //数据库更新 SubscriberController subscriberController = new SubscriberController(db.Subscribers); subscriberController.SetSubscriber(subscriber); subscriberController.DeleteSubscriber(); //话题列表更新 instance.TopicList.Remove(removedTopic); //数据库更新 TopicController topicController = await GetTopicController(removedTopic); topicController.DeleteTopic(); //向服务器请求删除订阅者 client.RemoveSubscriber(subscriber); //移除成功返回真 return true; }
/// <summary> /// 增加订阅者(群组话题使用) /// </summary> /// <param name="subsricber">订阅者对象</param> /// <returns>结果</returns> public async Task <bool> AddSubscriber(Subscriber subscriber) { //判断订阅者是否位于订阅者列表中 if (instance.SubsriberList.Contains(subscriber)) { //重复添加返回假 return(false); } subscriber.TopicName = instance.Name; instance.SubsriberList.Add(subscriber); //调用订阅者控制器更新订阅者 SubscriberController subscriberController = new SubscriberController(db.Subscribers); subscriberController.SetSubscriber(subscriber); subscriberController.UpsertSubscriber(); //更新话题时间戳 instance.LastUsed = ChatMessageBuilder.GetTimeStamp(); var dbContext = db.Topics.GetRepository(); await dbContext.UpsertTopic(instance); //添加成功返回真 return(true); }
/// <summary> /// 移除订阅者 /// </summary> /// <param name="subscriber">订阅者对象</param> /// <returns>结果</returns> public async Task<bool> RemoveSubscriber(Subscriber subscriber) { //判断订阅者是否位于订阅者列表中 if (!instance.SubscriberList.Contains(subscriber)) { return false; } Topic removedTopic = GetTopicByName(subscriber.TopicName); //订阅者列表更新 instance.SubscriberList.Remove(subscriber); //数据库更新 SubscriberController subscriberController = new SubscriberController(db.Subscribers); subscriberController.SetSubscriber(subscriber); subscriberController.DeleteSubscriber(); //话题列表更新 instance.TopicList.Remove(removedTopic); //数据库更新 TopicController topicController = await GetTopicController(removedTopic); topicController.DeleteTopic(); //向服务器请求删除订阅者 client.RemoveSubscriber(subscriber); return true; }
/// <summary> /// 通过订阅者获取订阅者控制器 /// </summary> /// <param name="subscriber">订阅者</param> /// <returns>订阅者控制器</returns> public SubscriberController GetSubscriberController(Subscriber subscriber) { if (subscriber == null) { return null; } SubscriberController subscriberController = new SubscriberController(db.Subscribers); subscriberController.SetSubscriber(subscriber); return subscriberController; }
/// <summary> /// 订阅者状态改变 /// </summary> /// <param name="sender">发送者</param> /// <param name="args">订阅者状态改变参数</param> private void SubscriberStateChanged(object sender, SubscriberStateChangedEventArgs args) { //获取拥有完整信息的订阅者 Subscriber subscriber = GetSubscriberByUserId(args.Subscriber.UserId); if (subscriber != null) { SubscriberController subscriberController = GetSubscriberController(subscriber); //调用更改订阅者状态方法 subscriberController.ChangeSubscriberState(args.IsOnline); } }
/// <summary> /// 设置用户数据库 /// </summary> /// <param name="database">数据库</param> /// <returns></returns> public async Task SetDatabase(IAccountRepository database) { db = database; //获取控制器 SubscriberController subscriberController = new SubscriberController(database.Subscribers); TopicController topicController = new TopicController(database); //填充数据 instance.SubscriberList = await subscriberController.GetSubscribers(); instance.TopicList = await topicController.GetTopics(); }
/// <summary> /// 新增订阅者 /// </summary> /// <param name="subscriber">订阅者对象</param> /// <param name="isTemporary">是否临时保存</param> /// <returns>结果</returns> public async Task<bool> AddSubscriber(Subscriber subscriber, bool isTemporary = false) { //判断是否为有效订阅者 if (instance.SubscriberList.Contains(subscriber) || subscriber.TopicName == "fnd" || subscriber.TopicName == "me") { //无效订阅者返回假 return false; } //判断是否为临时查询 if (isTemporary == true) { if (searchSubscriberResult.Contains(subscriber)) { return false; } searchSubscriberResult.Add(subscriber); return true; } //获取已存在话题或创建话题 Topic newTopic = GetTopicByName(subscriber.TopicName); if (newTopic == null) { newTopic = new Topic(subscriber.TopicName); } if (newTopic == null) { newTopic = new Topic(subscriber.TopicName); } //话题列表更新 await AddTopic(newTopic); //数据库更新 TopicController topicController = await GetTopicController(newTopic); topicController.UpsertTopic(); //订阅者列表更新 instance.SubscriberList.Add(subscriber); //数据库更新 SubscriberController subscriberController = new SubscriberController(db.Subscribers); subscriberController.SetSubscriber(subscriber); subscriberController.UpsertSubscriber(); //服务器更新 client.SubscribeTopic(newTopic); return true; }
/// <summary> /// 设置话题对象 /// </summary> /// <param name="topic">话题</param> /// <returns></returns> public async Task SetTopic(Topic topic) { instance = topic; //初始化消息列表 MessageController messageController = new MessageController(db.Messages); List <ChatMessage> messages = await messageController.GetMessages(instance, instance.Limit); messages.ForEach(async m => await AddMessage(m)); //初始化话题订阅者列表 SubscriberController subscriberController = new SubscriberController(db.Subscribers); instance.SubsriberList = await subscriberController.GetSubscribers(instance); }
/// <summary> /// 移除订阅者(群组话题使用) /// </summary> /// <param name="subscriber">订阅者对象</param> /// <returns>结果</returns> public async Task <bool> RemoveSubscriber(Subscriber subscriber) { //判断订阅者是否位于订阅者列表中 if (!instance.SubsriberList.Contains(subscriber)) { //订阅者不存在返回假 return(false); } instance.SubsriberList.Remove(subscriber); //调用订阅者控制器删除订阅者 SubscriberController subscriberController = new SubscriberController(db.Subscribers); subscriberController.SetSubscriber(subscriber); subscriberController.DeleteSubscriber(); //更新话题时间戳 instance.LastUsed = ChatMessageBuilder.GetTimeStamp(); var dbContext = db.Topics.GetRepository(); await dbContext.UpsertTopic(instance); //删除成功返回真 return(true); }
/// <summary> /// 获取订阅者控制器 /// </summary> /// <returns>订阅者控制器</returns> public SubscriberController GetSubscriberController() { SubscriberController subscriberController = new SubscriberController(db.Subscribers); return subscriberController; }