tigase / tigase-server

(M) Highly optimized, extremely modular and very flexible XMPP/Jabber server
https://tigase.net
GNU Affero General Public License v3.0
327 stars 106 forks source link

How to notify group members after the plugin processes group messages #158

Open weiweipang opened 2 years ago

weiweipang commented 2 years ago

I have a problem with… I created a group chat with reference to the instance of tigase-tts-ng,and send messages to groups through users1,all group members can receive messages,But I don't know how to send a message to group members after processing the message in the plugin,please give me some examples,thank you

@Test(groups = {"Multi User Chat"}, description = "#8660: Delivery presence from offline user")
public void testOfflineUserSendsMessage() throws JaxmppException, InterruptedException {
    user1Jaxmpp.getEventBus()
            .addHandler(MucModule.MucMessageReceivedHandler.MucMessageReceivedEvent.class,
                    (sessionObject, message, room, nickname, timestamp) -> {
                        try {
                            mutex.notify("recv1:" + message.getBody());
                        } catch (XMLException e) {
                            Assert.fail(e.getMessage());
                        }
                    });

    final String mid = nextRnd();
    Element msg = ElementBuilder.create("message")
            .setAttribute("type", "groupchat")
            .setAttribute("to", roomJID.toString())
            .child("body")
            .setValue("test-" + mid)
            .getElement();

    user2Jaxmpp.send(Stanza.create(msg));
    mutex.waitFor(20_000, "recv1:test-" + mid);
    assertTrue(mutex.isItemNotified("recv1:test-" + mid),
            "User1 did not received message from offline user");
}

    //Here is the code snippet to simulate a group chat
        user1Jaxmpp.getEventBus()
                .addHandler(MucModule.MucMessageReceivedHandler.MucMessageReceivedEvent.class, new MucModule.MucMessageReceivedHandler() {
                    @Override
                    public void onMucMessageReceived(SessionObject sessionObject, Message message, Room room, String s, Date date) {
                        try {
                            System.out.println("user1:"+message.getBody());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

        user2Jaxmpp.getEventBus()
                .addHandler(MucModule.MucMessageReceivedHandler.MucMessageReceivedEvent.class, new MucModule.MucMessageReceivedHandler() {
                    @Override
                    public void onMucMessageReceived(SessionObject sessionObject, Message message, Room room, String s, Date date) {
                        try {
                            System.out.println("user2:"+message.getBody());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
        user3Jaxmpp.getEventBus()
                .addHandler(MucModule.MucMessageReceivedHandler.MucMessageReceivedEvent.class, new MucModule.MucMessageReceivedHandler() {
                    @Override
                    public void onMucMessageReceived(SessionObject sessionObject, Message message, Room room, String s, Date date) {
                        try {
                            System.out.println("user3:"+message.getBody());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

Details (please complete the following information):

woj-tek commented 2 years ago

Could you clarify what you are trying to achieve? What is "plugin" In your case?

You posted your question in tigase-server project yet you use jaxmpp code.

weiweipang commented 2 years ago

ok,I customized a tigase plugin to process messages sent by group chat member,But I don't know how to notify all group members after processing the message,Below is a custom plugin

@Id(Solitaire.XMLNS_SOLITAIRE_DATA)
@Handles({@Handle(path = {"iq", "query"}, xmlns = Solitaire.XMLNS_SOLITAIRE_DATA)})
@Bean(name = Solitaire.XMLNS_SOLITAIRE_DATA, parent = SessionManager.class, active = true)
public class Solitaire extends AnnotatedXMPPProcessor implements XMPPProcessorIfc {

public static final String XMLNS_SOLITAIRE_DATA = "test:group-chat";
private static final Logger log = Logger.getLogger(Solitaire.class.getName());

    @Override
    public void process(Packet packet, XMPPResourceConnection session, NonAuthUserRepository repo, Queue<Packet> results, 
        Map<String, Object> settings) throws XMPPException {
        if (session == null || !session.isAuthorized()) {
            return;
        }
        if (StanzaType.get != packet.getType()) {
            return;
        }
        try {
            Element query = packet.getElemChild("query");

            //Referring to the tigase example I know how to return the result to a single person,but I do not know how to notify 
             //group members after processing messages here

        } catch (Exception e) {
            LogUtils.logExeption(log, e);

        }
    }

}
woj-tek commented 2 years ago

Once again: Could you clarify what you are trying to achieve?

In general if you are in groupchat (MUC or MIX) then sending message to such groupchat channel broadcast the message to all members.