예제 #1
0
        /// <summary>
        /// This method is intended for the API only.
        /// If a Room recieves a new event, process it in here.
        /// </summary>
        /// <param name="evt">New event</param>
        public void FeedEvent(MatrixEvent evt)
        {
            if (evt.content == null)
            {
                return; // We can't operate on this
            }

            Type t = evt.content.GetType();

            if (t == typeof(MatrixMRoomCreate))
            {
                var create = ((MatrixMRoomCreate)evt.content);
                Creator        = create.creator;
                ShouldFederate = create.mfederate;
            }
            else if (t == typeof(MatrixMRoomName))
            {
                Name = ((MatrixMRoomName)evt.content).name;
            }
            else if (t == typeof(MatrixMRoomTopic))
            {
                Topic = ((MatrixMRoomTopic)evt.content).topic;
            }
            else if (t == typeof(MatrixMRoomAliases))
            {
                Aliases = ((MatrixMRoomAliases)evt.content).aliases;
            }
            else if (t == typeof(MatrixMRoomCanonicalAlias))
            {
                CanonicalAlias = ((MatrixMRoomCanonicalAlias)evt.content).alias;
            }
            else if (t == typeof(MatrixMRoomJoinRules))
            {
                JoinRule = ((MatrixMRoomJoinRules)evt.content).join_rule;
            }
            else if (t == typeof(MatrixMRoomJoinRules))
            {
                PowerLevels = ((MatrixMRoomPowerLevels)evt.content);
            }
            else if (t == typeof(MatrixMRoomMember))
            {
                MatrixMRoomMember member = (MatrixMRoomMember)evt.content;
                if (!api.RunningInitialSync)
                {
                    //Handle new join,leave etc
                    MatrixRoomMemberEvent Event = null;
                    switch (member.membership)
                    {
                    case EMatrixRoomMembership.Invite:
                        Event = OnUserInvited;
                        break;

                    case EMatrixRoomMembership.Join:
                        Event = Members.ContainsKey(evt.state_key) ? OnUserChange : OnUserJoined;
                        break;

                    case EMatrixRoomMembership.Leave:
                        Event = OnUserLeft;
                        break;

                    case EMatrixRoomMembership.Ban:
                        Event = OnUserBanned;
                        break;
                    }

                    Event?.Invoke(evt.state_key, member);
                }

                Members[evt.state_key] = member;
            }
            else if (typeof(MatrixMRoomMessage).IsAssignableFrom(t))
            {
                messages.Add((MatrixMRoomMessage)evt.content);
                if (OnMessage != null)
                {
                    if (MessageMaximumAge <= 0 || evt.age <= MessageMaximumAge)
                    {
                        try
                        {
                            OnMessage.Invoke(this, evt);
                        }
                        catch (Exception e)
                        {
#if DEBUG
                            Console.WriteLine("A OnMessage handler failed");
                            Console.WriteLine(e);
#endif
                        }
                    }
                }
            }

            OnEvent?.Invoke(this, evt);
        }
예제 #2
0
        /// <summary>
        /// This method is intended for the API only.
        /// If a Room recieves a new event, process it in here.
        /// </summary>
        /// <param name="evt">New event</param>
        public void FeedEvent(MatrixEvent evt)
        {
            Type t = evt.content.GetType();

            if (t == typeof(MatrixMRoomCreate))
            {
                Creator = ((MatrixMRoomCreate)evt.content).creator;
            }
            else if (t == typeof(MatrixMRoomName))
            {
                Name = ((MatrixMRoomName)evt.content).name;
            }
            else if (t == typeof(MatrixMRoomTopic))
            {
                Topic = ((MatrixMRoomTopic)evt.content).topic;
            }
            else if (t == typeof(MatrixMRoomAliases))
            {
                Aliases = ((MatrixMRoomAliases)evt.content).aliases;
            }
            else if (t == typeof(MatrixMRoomCanonicalAlias))
            {
                CanonicalAlias = ((MatrixMRoomCanonicalAlias)evt.content).alias;
            }
            else if (t == typeof(MatrixMRoomJoinRules))
            {
                JoinRule = ((MatrixMRoomJoinRules)evt.content).join_rule;
            }
            else if (t == typeof(MatrixMRoomJoinRules))
            {
                PowerLevels = ((MatrixMRoomPowerLevels)evt.content);
            }
            else if (t == typeof(MatrixMRoomMember))
            {
                MatrixMRoomMember member = (MatrixMRoomMember)evt.content;
                if (!api.RunningInitialSync)
                {
                    //Handle new join,leave etc
                    MatrixRoomMemberEvent Event = null;
                    switch (member.membership)
                    {
                    case EMatrixRoomMembership.Invite:
                        Event = OnUserInvited;
                        break;

                    case EMatrixRoomMembership.Join:
                        Event = Members.ContainsKey(evt.state_key) ? OnUserChange : OnUserJoined;
                        break;

                    case EMatrixRoomMembership.Leave:
                        Event = OnUserLeft;
                        break;

                    case EMatrixRoomMembership.Ban:
                        Event = OnUserBanned;
                        break;
                    }
                    if (Event != null)
                    {
                        Event.Invoke(evt.state_key, member);
                    }
                }
                Members [evt.state_key] = member;
            }
            else if (t.IsSubclassOf(typeof(MatrixMRoomMessage)))
            {
                messages.Add((MatrixMRoomMessage)evt.content);
                if (OnMessage != null)
                {
                    if (MessageMaximumAge <= 0 || evt.age < MessageMaximumAge)
                    {
                        try {
                            OnMessage.Invoke(this, evt);
                        } catch (Exception e) {
                            Console.WriteLine("A OnMessage handler failed");
                            Console.WriteLine(e);
                        }
                    }
                }
            }

            if (OnEvent != null)
            {
                OnEvent.Invoke(this, evt);
            }
        }