Search in sources :

Example 1 with SearchItem

use of forpdateam.ru.forpda.api.search.models.SearchItem in project ForPDA by RadiationX.

the class SearchRx method transform.

public static SearchResult transform(SearchResult page, boolean withHtml) throws Exception {
    if (withHtml) {
        List<ForumUser> forumUsers = new ArrayList<>();
        for (SearchItem post : page.getItems()) {
            ForumUser forumUser = new ForumUser();
            forumUser.setId(post.getUserId());
            forumUser.setNick(post.getNick());
            forumUser.setAvatar(post.getAvatar());
        }
        ForumUsersCache.saveUsers(forumUsers);
        int memberId = ClientHelper.getUserId();
        MiniTemplator t = App.get().getTemplate(App.TEMPLATE_SEARCH);
        App.setTemplateResStrings(t);
        boolean authorized = ClientHelper.getAuthState();
        boolean prevDisabled = page.getPagination().getCurrent() <= 1;
        boolean nextDisabled = page.getPagination().getCurrent() == page.getPagination().getAll();
        t.setVariableOpt("style_type", App.get().getCssStyleType());
        t.setVariableOpt("all_pages_int", page.getPagination().getAll());
        t.setVariableOpt("posts_on_page_int", page.getPagination().getPerPage());
        t.setVariableOpt("current_page_int", page.getPagination().getCurrent());
        t.setVariableOpt("authorized_bool", Boolean.toString(authorized));
        t.setVariableOpt("member_id_int", ClientHelper.getUserId());
        t.setVariableOpt("body_type", "search");
        t.setVariableOpt("navigation_disable", ThemeRx.getDisableStr(prevDisabled && nextDisabled));
        t.setVariableOpt("first_disable", ThemeRx.getDisableStr(prevDisabled));
        t.setVariableOpt("prev_disable", ThemeRx.getDisableStr(prevDisabled));
        t.setVariableOpt("next_disable", ThemeRx.getDisableStr(nextDisabled));
        t.setVariableOpt("last_disable", ThemeRx.getDisableStr(nextDisabled));
        boolean isEnableAvatars = Preferences.Theme.isShowAvatars(null);
        t.setVariableOpt("enable_avatars_bool", Boolean.toString(isEnableAvatars));
        t.setVariableOpt("enable_avatars", isEnableAvatars ? "show_avatar" : "hide_avatar");
        t.setVariableOpt("avatar_type", Preferences.Theme.isCircleAvatars(null) ? "circle_avatar" : "square_avatar");
        Matcher letterMatcher = null;
        for (SearchItem post : page.getItems()) {
            t.setVariableOpt("topic_id", post.getTopicId());
            t.setVariableOpt("post_title", post.getTitle());
            t.setVariableOpt("user_online", post.isOnline() ? "online" : "");
            t.setVariableOpt("post_id", post.getId());
            t.setVariableOpt("user_id", post.getUserId());
            // Post header
            t.setVariableOpt("avatar", post.getAvatar());
            t.setVariableOpt("none_avatar", post.getAvatar().isEmpty() ? "none_avatar" : "");
            if (letterMatcher != null) {
                letterMatcher = letterMatcher.reset(post.getNick());
            } else {
                letterMatcher = firstLetter.matcher(post.getNick());
            }
            String letter = null;
            if (letterMatcher.find()) {
                letter = letterMatcher.group(1);
            } else {
                letter = post.getNick().substring(0, 1);
            }
            t.setVariableOpt("nick_letter", letter);
            t.setVariableOpt("nick", ApiUtils.htmlEncode(post.getNick()));
            // t.setVariableOpt("curator", false ? "curator" : "");
            t.setVariableOpt("group_color", post.getGroupColor());
            t.setVariableOpt("group", post.getGroup());
            t.setVariableOpt("reputation", post.getReputation());
            t.setVariableOpt("date", post.getDate());
            // t.setVariableOpt("number", post.getNumber());
            // Post body
            t.setVariableOpt("body", post.getBody());
            // Post footer
            /*if (post.canReport() && authorized)
                    t.addBlockOpt("report_block");
                if (page.canQuote() && authorized && post.getUserId() != memberId)
                    t.addBlockOpt("reply_block");
                if (authorized && post.getUserId() != memberId)
                    t.addBlockOpt("vote_block");
                if (post.canDelete() && authorized)
                    t.addBlockOpt("delete_block");
                if (post.canEdit() && authorized)
                    t.addBlockOpt("edit_block");*/
            t.addBlockOpt("post");
        }
        page.setHtml(t.generateOutput());
        t.reset();
    }
    return page;
}
Also used : ForumUser(forpdateam.ru.forpda.api.others.user.ForumUser) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) MiniTemplator(biz.source_code.miniTemplator.MiniTemplator) SearchItem(forpdateam.ru.forpda.api.search.models.SearchItem)

Example 2 with SearchItem

use of forpdateam.ru.forpda.api.search.models.SearchItem in project ForPDA by RadiationX.

the class Search method getSearch.

public SearchResult getSearch(SearchSettings settings) throws Exception {
    SearchResult result = new SearchResult();
    NetworkResponse response = Api.getWebClient().get(settings.toUrl());
    Matcher matcher = null;
    SearchItem item = null;
    boolean isNews = settings.getResourceType().equals(SearchSettings.RESOURCE_NEWS.first);
    boolean resultTopics = settings.getResult().equals(SearchSettings.RESULT_TOPICS.first);
    if (isNews) {
        matcher = newsListPattern.matcher(response.getBody());
        while (matcher.find()) {
            item = new SearchItem();
            item.setId(Integer.parseInt(matcher.group(1)));
            item.setImageUrl(matcher.group(2));
            item.setDate(matcher.group(3));
            item.setUserId(Integer.parseInt(matcher.group(4)));
            item.setNick(ApiUtils.fromHtml(matcher.group(5)));
            item.setTitle(ApiUtils.fromHtml(matcher.group(6)));
            item.setBody(matcher.group(7));
            result.addItem(item);
        }
    } else {
        if (resultTopics) {
            matcher = forumTopicsPattern.matcher(response.getBody());
            while (matcher.find()) {
                item = new SearchItem();
                item.setTopicId(Integer.parseInt(matcher.group(1)));
                // item.setId(Integer.parseInt(matcher.group(1)));
                item.setTitle(ApiUtils.fromHtml(matcher.group(4)));
                item.setDesc(ApiUtils.fromHtml(matcher.group(5)));
                item.setForumId(Integer.parseInt(matcher.group(6)));
                item.setUserId(Integer.parseInt(matcher.group(10)));
                item.setNick(ApiUtils.fromHtml(matcher.group(11)));
                item.setDate(matcher.group(12));
                result.addItem(item);
            }
        } else {
            matcher = universalForumPosts.matcher(response.getBody());
            while (matcher.find()) {
                item = new SearchItem();
                item.setTopicId(Integer.parseInt(matcher.group(2)));
                item.setId(Integer.parseInt(matcher.group(3)));
                item.setTitle(ApiUtils.fromHtml(matcher.group(4)));
                item.setDate(matcher.group(5));
                // item.setNumber(Integer.parseInt(matcher.group(6)));
                item.setOnline(matcher.group(7).contains("green"));
                String avatar = matcher.group(8);
                if (!avatar.isEmpty()) {
                    avatar = "https://s.4pda.to/forum/uploads/".concat(avatar);
                }
                item.setAvatar(avatar);
                item.setNick(ApiUtils.fromHtml(matcher.group(9)));
                item.setUserId(Integer.parseInt(matcher.group(10)));
                item.setCurator(matcher.group(11) != null);
                item.setGroupColor(matcher.group(12));
                item.setGroup(matcher.group(13));
                item.setCanMinus(!matcher.group(14).isEmpty());
                item.setReputation(matcher.group(15));
                item.setCanPlus(!matcher.group(16).isEmpty());
                item.setCanReport(!matcher.group(17).isEmpty());
                item.setCanEdit(!matcher.group(18).isEmpty());
                item.setCanDelete(!matcher.group(19).isEmpty());
                item.setCanQuote(!matcher.group(20).isEmpty());
                item.setBody(matcher.group(21));
                result.addItem(item);
            }
        }
    }
    if (isNews) {
        result.setPagination(Pagination.parseNews(response.getBody()));
    } else {
        result.setPagination(Pagination.parseForum(response.getBody()));
    }
    result.setSettings(settings);
    return result;
}
Also used : Matcher(java.util.regex.Matcher) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) SearchResult(forpdateam.ru.forpda.api.search.models.SearchResult) SearchItem(forpdateam.ru.forpda.api.search.models.SearchItem)

Example 3 with SearchItem

use of forpdateam.ru.forpda.api.search.models.SearchItem in project ForPDA by RadiationX.

the class SearchFragment method editPost.

@Override
public void editPost(IBaseForumPost post) {
    String title;
    if (post instanceof SearchItem) {
        SearchItem item = (SearchItem) post;
        title = item.getTitle();
    } else {
        title = "пост из поиска_";
    }
    TabManager.get().add(EditPostFragment.newInstance(post.getId(), post.getTopicId(), post.getForumId(), 0, title));
}
Also used : SearchItem(forpdateam.ru.forpda.api.search.models.SearchItem)

Example 4 with SearchItem

use of forpdateam.ru.forpda.api.search.models.SearchItem in project ForPDA by RadiationX.

the class ThemeDialogsHelper method showPostMenu.

public static void showPostMenu(Context context, IPostFunctions theme, IBaseForumPost post) {
    if (postMenu == null) {
        postMenu = new DynamicDialogMenu<>();
        postMenu.addItem(App.get().getString(R.string.reply), IPostFunctions::reply);
        postMenu.addItem(App.get().getString(R.string.quote_from_clipboard), (context1, data) -> {
            String text = Utils.readFromClipboard();
            if (text != null && !text.isEmpty()) {
                theme.quotePost(text, data);
            }
        });
        postMenu.addItem(App.get().getString(R.string.report), IPostFunctions::reportPost);
        postMenu.addItem(App.get().getString(R.string.edit), IPostFunctions::editPost);
        postMenu.addItem(App.get().getString(R.string.delete), IPostFunctions::deletePost);
        postMenu.addItem(App.get().getString(R.string.copy_link), (context1, data) -> {
            String url = "https://4pda.ru/forum/index.php?s=&showtopic=" + data.getTopicId() + "&view=findpost&p=" + data.getId();
            Utils.copyToClipBoard(url);
        });
        postMenu.addItem(App.get().getString(R.string.create_note), (context1, data) -> {
            String themeTitle = null;
            if (context1 instanceof SearchFragment) {
                SearchItem searchItem = (SearchItem) data;
                themeTitle = searchItem.getTitle();
            } else if (context1 instanceof ThemeFragment) {
                ThemeFragment themeFragment = (ThemeFragment) context1;
                themeTitle = themeFragment.currentPage.getTitle();
            }
            String title = String.format(App.get().getString(R.string.post_Topic_Nick_Number), themeTitle, data.getNick(), data.getId());
            String url = "https://4pda.ru/forum/index.php?s=&showtopic=" + data.getTopicId() + "&view=findpost&p=" + data.getId();
            NotesAddPopup.showAddNoteDialog(context, title, url);
        });
        postMenu.addItem(App.get().getString(R.string.share), (context12, data) -> {
            String url = "https://4pda.ru/forum/index.php?s=&showtopic=" + data.getTopicId() + "&view=findpost&p=" + data.getId();
            Utils.shareText(url);
        });
    }
    postMenu.disallowAll();
    if (ClientHelper.getAuthState() == ClientHelper.AUTH_STATE_LOGIN) {
        if (post.canQuote()) {
            postMenu.allow(0);
            postMenu.allow(1);
        }
        if (post.canReport())
            postMenu.allow(2);
        if (post.canEdit())
            postMenu.allow(3);
        if (post.canDelete())
            postMenu.allow(4);
    }
    postMenu.allow(5);
    postMenu.allow(6);
    postMenu.allow(7);
    postMenu.show(context, theme, post);
}
Also used : IPostFunctions(forpdateam.ru.forpda.common.webview.jsinterfaces.IPostFunctions) SearchFragment(forpdateam.ru.forpda.ui.fragments.search.SearchFragment) SearchItem(forpdateam.ru.forpda.api.search.models.SearchItem)

Aggregations

SearchItem (forpdateam.ru.forpda.api.search.models.SearchItem)4 Matcher (java.util.regex.Matcher)2 MiniTemplator (biz.source_code.miniTemplator.MiniTemplator)1 NetworkResponse (forpdateam.ru.forpda.api.NetworkResponse)1 ForumUser (forpdateam.ru.forpda.api.others.user.ForumUser)1 SearchResult (forpdateam.ru.forpda.api.search.models.SearchResult)1 IPostFunctions (forpdateam.ru.forpda.common.webview.jsinterfaces.IPostFunctions)1 SearchFragment (forpdateam.ru.forpda.ui.fragments.search.SearchFragment)1 ArrayList (java.util.ArrayList)1