示例#1
0
        public virtual Tuple <List <UserId>, List <ITextChatMessage> > JoinRoom(UserId userId, RoomId roomId)
        {
            var user = ChatModel.GetUser(userId);

            // If the room is private and the user has nothing to do here, EXPLODE!
            if (roomId.IsPrivate() && !UserIdsInPrivateRoom(roomId).Contains(userId))
            {
                throw new LogReadyException(LogTag.PrivateRoomIntrusionAttempt, new { userId, roomId });
            }

            if (!ChatModel.IsInRoom(roomId, user.Id))
            {
                ChatModel.AddUserToRoom(userId, roomId);
                OnUserJoinedRoom?.Invoke(roomId, user.Id);
                if (roomId.IsPublic())
                {
                    OnCountOfUsersUpdated?.Invoke(roomId, ChatModel.UsersCountOf(roomId));
                }
            }

            var withVisibilities = new List <MessageVisibility> {
                MessageVisibility.Everyone, MessageVisibility.Sender, MessageVisibility.Ephemeral, MessageVisibility.News
            };

            var customMessageHistory = ChatModel.LatestMessagesIn(roomId, HistoryLength * 2, withVisibilities)
                                       .Where(a => (a.Visibility != MessageVisibility.Sender && a.Visibility != MessageVisibility.Ephemeral) || a.UserId == userId)
                                       .Reverse().Take(HistoryLength).Reverse()
                                       .ToList();

            // If in a private room and the partner doesn't want private chat, Signal it with a service message
            if (roomId.IsGroup())
            {
                goto Skip;
            }
            var partnerId = PartnerInPrivateRoom(roomId, userId);

            if (!ChatModel.IsInChat(partnerId))
            {
                goto Skip;
            }
            var partner = ChatModel.GetUser(partnerId);

            if (partner.IsNoPrivateChat)
            {
                customMessageHistory.Add(new TextChatMessage {
                    RoomId = roomId,
                    Text   = JsonConvert.SerializeObject(new { noPrivateChat = ChatModel.GetPublicRoomsFor(partner.Id) }, Formatting.None, new JsonSerializerSettings {
                        ContractResolver = new CamelCasePropertyNamesContractResolver()
                    }),
                    Visibility = MessageVisibility.System
                });
            }
Skip:

            return(new Tuple <List <UserId>, List <ITextChatMessage> >(
                       ChatModel.UsersInRoom(roomId, user.Id),
                       customMessageHistory
                       ));
        }
示例#2
0
        public virtual void LeaveRoom(UserId userId, RoomId roomId)
        {
            ChatModel.RemoveUserFromRoom(userId, roomId);
            if (roomId.IsPublic())
            {
                OnCountOfUsersUpdated?.Invoke(roomId, ChatModel.UsersCountOf(roomId));
            }

            OnUserLeftRoom?.Invoke(roomId, userId);
        }