Search in sources :

Example 1 with QmsMessage

use of forpdateam.ru.forpda.api.qms.models.QmsMessage in project ForPDA by RadiationX.

the class QmsRx method generateMess.

public static MiniTemplator generateMess(MiniTemplator t, List<QmsMessage> messages, int start, int end) {
    for (int i = start; i < end; i++) {
        QmsMessage mess = messages.get(i);
        generateMess(t, mess);
    }
    return t;
}
Also used : QmsMessage(forpdateam.ru.forpda.api.qms.models.QmsMessage)

Example 2 with QmsMessage

use of forpdateam.ru.forpda.api.qms.models.QmsMessage in project ForPDA by RadiationX.

the class Qms method sendMessage.

public ArrayList<QmsMessage> sendMessage(int userId, int themeId, String text) throws Exception {
    ArrayList<QmsMessage> messages = new ArrayList<>();
    NetworkRequest.Builder builder = new NetworkRequest.Builder().url("https://4pda.ru/forum/index.php").formHeader("act", "qms-xhr").formHeader("action", "send-message").formHeader("message", text).formHeader("mid", Integer.toString(userId)).formHeader("t", Integer.toString(themeId));
    NetworkResponse response = Api.getWebClient().request(builder.build());
    Matcher matcher = chatPattern.matcher(response.getBody());
    QmsMessage item = new QmsMessage();
    if (matcher.find()) {
        if (matcher.group(1) == null && matcher.group(7) != null) {
            item.setIsDate(true);
            item.setDate(matcher.group(7).trim());
        } else {
            item.setMyMessage(!matcher.group(1).isEmpty());
            item.setId(Integer.parseInt(matcher.group(2)));
            if (item.isMyMessage()) {
                item.setReadStatus(!matcher.group(3).equals("1"));
            } else {
                item.setReadStatus(true);
            }
            item.setTime(matcher.group(4));
            item.setAvatar(matcher.group(5));
            item.setContent(matcher.group(6).trim());
        }
        messages.add(item);
    } else {
        matcher = Pattern.compile("class=\"list-group-item[^\"]*?error\"[\\s\\S]*?<\\/a>([\\s\\S]*?)<\\/div>").matcher(response.getBody());
        if (matcher.find()) {
            throw new Exception(matcher.group(1).trim());
        }
    }
    return messages;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) NetworkRequest(forpdateam.ru.forpda.api.NetworkRequest) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) QmsMessage(forpdateam.ru.forpda.api.qms.models.QmsMessage)

Example 3 with QmsMessage

use of forpdateam.ru.forpda.api.qms.models.QmsMessage in project ForPDA by RadiationX.

the class Qms method getMessagesAfter.

public ArrayList<QmsMessage> getMessagesAfter(int userId, int themeId, int afterMessageId) throws Exception {
    ArrayList<QmsMessage> messages = new ArrayList<>();
    NetworkRequest.Builder threadMessagesBuilder = new NetworkRequest.Builder().url("https://4pda.ru/forum/index.php?act=qms-xhr&").xhrHeader().formHeader("action", "get-thread-messages").formHeader("mid", Integer.toString(userId)).formHeader("t", Integer.toString(themeId)).formHeader("after-message", Integer.toString(afterMessageId));
    NetworkResponse threadMessagesResponse = Api.getWebClient().request(threadMessagesBuilder.build());
    Matcher matcher = chatPattern.matcher(threadMessagesResponse.getBody());
    while (matcher.find()) {
        QmsMessage item = new QmsMessage();
        if (matcher.group(1) == null && matcher.group(7) != null) {
            item.setIsDate(true);
            item.setDate(matcher.group(7).trim());
        } else {
            item.setMyMessage(!matcher.group(1).isEmpty());
            item.setId(Integer.parseInt(matcher.group(2)));
            if (item.isMyMessage()) {
                item.setReadStatus(!matcher.group(3).equals("1"));
            } else {
                item.setReadStatus(true);
            }
            item.setTime(matcher.group(4));
            item.setAvatar(matcher.group(5));
            item.setContent(matcher.group(6).trim());
        }
        messages.add(item);
    }
    return messages;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) NetworkRequest(forpdateam.ru.forpda.api.NetworkRequest) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) QmsMessage(forpdateam.ru.forpda.api.qms.models.QmsMessage)

Example 4 with QmsMessage

use of forpdateam.ru.forpda.api.qms.models.QmsMessage in project ForPDA by RadiationX.

the class Qms method parseChat.

private QmsChatModel parseChat(String response) {
    QmsChatModel chat = new QmsChatModel();
    Matcher matcher = chatPattern.matcher(response);
    QmsMessage item;
    while (matcher.find()) {
        item = new QmsMessage();
        if (matcher.group(1) == null && matcher.group(7) != null) {
            item.setIsDate(true);
            item.setDate(matcher.group(7).trim());
        } else {
            item.setMyMessage(!matcher.group(1).isEmpty());
            item.setId(Integer.parseInt(matcher.group(2)));
            if (item.isMyMessage()) {
                item.setReadStatus(!matcher.group(3).equals("1"));
            } else {
                item.setReadStatus(true);
            }
            item.setTime(matcher.group(4));
            item.setAvatar(matcher.group(5));
            item.setContent(matcher.group(6).trim());
        }
        chat.addMessage(item);
    }
    matcher = chatInfoPattern.matcher(response);
    if (matcher.find()) {
        chat.setNick(ApiUtils.fromHtml(matcher.group(1).trim()));
        chat.setTitle(ApiUtils.fromHtml(matcher.group(2).trim()));
        chat.setUserId(Integer.parseInt(matcher.group(3)));
        chat.setThemeId(Integer.parseInt(matcher.group(4)));
        chat.setAvatarUrl(matcher.group(5));
    }
    return chat;
}
Also used : Matcher(java.util.regex.Matcher) QmsMessage(forpdateam.ru.forpda.api.qms.models.QmsMessage) QmsChatModel(forpdateam.ru.forpda.api.qms.models.QmsChatModel)

Example 5 with QmsMessage

use of forpdateam.ru.forpda.api.qms.models.QmsMessage in project ForPDA by RadiationX.

the class QmsChatFragment method onNewMessages.

private void onNewMessages(ArrayList<QmsMessage> qmsMessage) {
    Log.d(LOG_TAG, "Returned messages " + qmsMessage.size());
    if (!qmsMessage.isEmpty()) {
        MiniTemplator t = App.get().getTemplate(App.TEMPLATE_QMS_CHAT_MESS);
        App.setTemplateResStrings(t);
        for (int i = 0; i < qmsMessage.size(); i++) {
            QmsMessage message = qmsMessage.get(i);
            for (QmsMessage viewmessage : currentChat.getMessages()) {
                if (viewmessage.getId() == message.getId()) {
                    return;
                }
            }
            currentChat.addMessage(message);
            QmsRx.generateMess(t, message);
        }
        String messagesSrc = t.generateOutput();
        t.reset();
        messagesSrc = QmsRx.transformMessageSrc(messagesSrc);
        webView.evalJs("showNewMess('".concat(messagesSrc).concat("', true)"));
    }
}
Also used : QmsMessage(forpdateam.ru.forpda.api.qms.models.QmsMessage) MiniTemplator(biz.source_code.miniTemplator.MiniTemplator) SuppressLint(android.annotation.SuppressLint)

Aggregations

QmsMessage (forpdateam.ru.forpda.api.qms.models.QmsMessage)5 Matcher (java.util.regex.Matcher)3 NetworkRequest (forpdateam.ru.forpda.api.NetworkRequest)2 NetworkResponse (forpdateam.ru.forpda.api.NetworkResponse)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 MiniTemplator (biz.source_code.miniTemplator.MiniTemplator)1 QmsChatModel (forpdateam.ru.forpda.api.qms.models.QmsChatModel)1