Exemplo n.º 1
0
        private static async void AppserviceOnOnEvent(MatrixEvent ev)
        {
            if (ev.type == MatrixEventType.RoomMember)
            {
                MatrixMRoomMember member = ev.content as MatrixMRoomMember;
                if (member?.membership == EMatrixRoomMembership.Join)
                {
                    await db.SetAddAsync($"{ev.room_id}:membership.join", ev.sender);
                }
                else if (member?.membership == EMatrixRoomMembership.Leave ||
                         member?.membership == EMatrixRoomMembership.Ban)
                {
                    await db.SetRemoveAsync($"{ev.room_id}:membership.join", ev.sender);
                }
            }
            var hasCompleteState = await db.StringGetBitAsync($"{ev.room_id}:membership.complete", 0);

            if (!hasCompleteState)
            {
                var members = appservice.GetClientAsUser().Api.GetJoinedMembers(ev.room_id).Keys;
                var b       = db.CreateBatch();
                foreach (var userId in members)
                {
                    b.SetAddAsync($"{ev.room_id}:membership.join", userId);
                }
                b.Execute();
                db.StringSetBit($"{ev.room_id}:membership.complete", 0, true);
            }
        }
Exemplo n.º 2
0
        public void SetMemberAvatarTest()
        {
            var        mock = Utils.MockApi();
            MatrixRoom room = new MatrixRoom(mock.Object, "!abc:localhost");
            var        ev   = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Join,
            };

            room.FeedEvent(Utils.MockEvent(ev, "@foobar:localhost"));
            room.SetMemberAvatar("@foobar:localhost");
        }
Exemplo n.º 3
0
        public void FeedEventRoomMemberFireEventsTest()
        {
            var        mock = Utils.MockApi();
            MatrixRoom room = new MatrixRoom((MatrixAPI)mock.Object, "!abc:localhost");

            bool[] didFire   = new bool[5];
            int    fireCount = 0;

            room.OnUserJoined  += (n, a) => didFire[0] = true;
            room.OnUserChange  += (n, a) => didFire[1] = true;
            room.OnUserLeft    += (n, a) => didFire[2] = true;
            room.OnUserInvited += (n, a) => didFire[3] = true;
            room.OnUserBanned  += (n, a) => didFire[4] = true;
            room.OnEvent       += (n, a) => fireCount++;

            var ev = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Join
            };

            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));

            Assert.That(didFire[0], Is.True, "Procesed join");
            ev = new MatrixMRoomMember()
            {
                membership  = EMatrixRoomMembership.Join,
                displayname = "Foobar!",
            };
            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));

            Assert.That(didFire[1], Is.True, "Processed change");
            ev = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Leave,
            };
            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));
            Assert.That(didFire[2], Is.True, "Processed leave");

            ev = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Invite,
            };
            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));
            Assert.That(didFire[3], Is.True, "Processed invite");

            ev = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Ban,
            };
            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));
            Assert.That(didFire[4], Is.True, "Processed ban");
            Assert.That(fireCount, Is.EqualTo(5), "OnEvent should fire each time.");
        }
Exemplo n.º 4
0
        public void FeedEventRoomMemberTest()
        {
            var        mock = Utils.MockApi();
            MatrixRoom room = new MatrixRoom((MatrixAPI)mock.Object, "!abc:localhost");
            var        ev   = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Join
            };

            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));
            Assert.That(room.Members.ContainsKey("@foobar:localhost"), Is.True, "The member is in the room.");
            Assert.That(room.Members.ContainsValue(ev), Is.True, "The member is in the room.");
        }
Exemplo n.º 5
0
        public void FeedEventRoomMemberNoFireEventsTest()
        {
            var        mock = Utils.MockApi();
            MatrixRoom room = new MatrixRoom((MatrixAPI)mock.Object, "!abc:localhost");
            var        ev   = new MatrixMRoomMember()
            {
                membership = EMatrixRoomMembership.Join
            };

            mock.Setup(f => f.RunningInitialSync).Returns(true);
            bool didFire = false;

            room.OnUserJoined += (n, a) => didFire = true;
            room.FeedEvent(Utils.MockEvent(ev, stateKey: "@foobar:localhost"));
            Assert.That(didFire, Is.False);
            Assert.That(room.Members.ContainsKey("@foobar:localhost"), Is.True, "The member is in the room.");
            Assert.That(room.Members.ContainsValue(ev), Is.True, "The member is in the room.");
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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);
            }
        }