<commit_before>/*
 * Copyright (c) 2017 yvolk (<NAME>), http://yurivolkov.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.andstatus.app.net.social;

import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import org.andstatus.app.data.DownloadStatus;
import org.andstatus.app.data.MyContentType;
import org.andstatus.app.net.http.ConnectionException;
import org.andstatus.app.net.http.HttpConnection;
import org.andstatus.app.util.MyLog;
import org.andstatus.app.util.SharedPreferencesUtil;
import org.andstatus.app.util.TriState;
import org.andstatus.app.util.UriUtils;
import org.andstatus.app.util.UrlUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URL;
import java.util.List;

public class ConnectionMastodon extends ConnectionTwitter1p0 {
    private static final String ATTACHMENTS_FIELD_NAME = "media_attachments";

    @Override
    protected String getApiPath1(ApiRoutineEnum routine) {
        String url;
        // See https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md
        switch (routine) {
            case REGISTER_CLIENT:
                url = "apps";
                break;
            case STATUSES_HOME_TIMELINE:
                url = "timelines/home";
                break;
            case PUBLIC_TIMELINE:
                url = "timelines/public";
                break;
            case ACCOUNT_VERIFY_CREDENTIALS:
                url = "accounts/verify_credentials";
                break;
            case POST_MESSAGE:
                url = "statuses";
                break;
            case POST_WITH_MEDIA:
                url = "media";
                break;
            case GET_MESSAGE:
                url = "statuses/%messageId%";
                break;
            case CREATE_FAVORITE:
                url = "statuses/%messageId%/favourite";
                break;
            case DESTROY_FAVORITE:
                url = "statuses/%messageId%/unfavourite";
                break;
            case FOLLOW_USER:
                url = "accounts/%userId%/follow";
                break;
            case STOP_FOLLOWING_USER:
                url = "accounts/%userId%/unfollow";
                break;
            case GET_FOLLOWERS:
                url = "accounts/%userId%/followers";
                break;
            case GET_FRIENDS:
                url = "accounts/%userId%/following";
                break;
            case GET_USER:
                url = "accounts/%userId%";
                break;
            case POST_REBLOG:
                url = "statuses/%messageId%/reblog";
                break;
            case DESTROY_REBLOG:
                url = "statuses/%messageId%/unreblog";
                break;
            default:
                url = "";
                break;
        }

        return prependWithBasicPath(url);
    }

    @Override
    public List<MbTimelineItem> getTimeline(ApiRoutineEnum apiRoutine, TimelinePosition youngestPosition,
                                            TimelinePosition oldestPosition, int limit, String userId)
            throws ConnectionException {
        String url = this.getApiPath(apiRoutine);
        Uri.Builder builder = Uri.parse(url).buildUpon();
        appendPositionParameters(builder, youngestPosition, oldestPosition);
        builder.appendQueryParameter("local", "false");
        builder.appendQueryParameter("limit", String.valueOf(fixedDownloadLimitForApiRoutine(limit, apiRoutine)));
        JSONArray jArr = http.getRequestAsArray(builder.build().toString());
        return jArrToTimeline(jArr, apiRoutine, url);
    }


    @Override
    public MbMessage updateStatus(String message, String statusId, String inReplyToId, Uri mediaUri) throws ConnectionException {
        JSONObject formParams = new JSONObject();
        try {
            formParams.put("status", message);<commit_msg>#457 Fixed: Public timeline in Mastodon showed local posts only<commit_after>/*
 * Copyright (c) 2017 yvolk (<NAME>), http://yurivolkov.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.andstatus.app.net.social;

import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import org.andstatus.app.data.DownloadStatus;
import org.andstatus.app.data.MyContentType;
import org.andstatus.app.net.http.ConnectionException;
import org.andstatus.app.net.http.HttpConnection;
import org.andstatus.app.util.MyLog;
import org.andstatus.app.util.SharedPreferencesUtil;
import org.andstatus.app.util.TriState;
import org.andstatus.app.util.UriUtils;
import org.andstatus.app.util.UrlUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URL;
import java.util.List;

public class ConnectionMastodon extends ConnectionTwitter1p0 {
    private static final String ATTACHMENTS_FIELD_NAME = "media_attachments";

    @Override
    protected String getApiPath1(ApiRoutineEnum routine) {
        String url;
        // See https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md
        switch (routine) {
            case REGISTER_CLIENT:
                url = "apps";
                break;
            case STATUSES_HOME_TIMELINE:
                url = "timelines/home";
                break;
            case PUBLIC_TIMELINE:
                url = "timelines/public";
                break;
            case ACCOUNT_VERIFY_CREDENTIALS:
                url = "accounts/verify_credentials";
                break;
            case POST_MESSAGE:
                url = "statuses";
                break;
            case POST_WITH_MEDIA:
                url = "media";
                break;
            case GET_MESSAGE:
                url = "statuses/%messageId%";
                break;
            case CREATE_FAVORITE:
                url = "statuses/%messageId%/favourite";
                break;
            case DESTROY_FAVORITE:
                url = "statuses/%messageId%/unfavourite";
                break;
            case FOLLOW_USER:
                url = "accounts/%userId%/follow";
                break;
            case STOP_FOLLOWING_USER:
                url = "accounts/%userId%/unfollow";
                break;
            case GET_FOLLOWERS:
                url = "accounts/%userId%/followers";
                break;
            case GET_FRIENDS:
                url = "accounts/%userId%/following";
                break;
            case GET_USER:
                url = "accounts/%userId%";
                break;
            case POST_REBLOG:
                url = "statuses/%messageId%/reblog";
                break;
            case DESTROY_REBLOG:
                url = "statuses/%messageId%/unreblog";
                break;
            default:
                url = "";
                break;
        }

        return prependWithBasicPath(url);
    }

    @Override
    public List<MbTimelineItem> getTimeline(ApiRoutineEnum apiRoutine, TimelinePosition youngestPosition,
                                            TimelinePosition oldestPosition, int limit, String userId)
            throws ConnectionException {
        String url = this.getApiPath(apiRoutine);
        Uri.Builder builder = Uri.parse(url).buildUpon();
        appendPositionParameters(builder, youngestPosition, oldestPosition);
        builder.appendQueryParameter("limit", String.valueOf(fixedDownloadLimitForApiRoutine(limit, apiRoutine)));
        JSONArray jArr = http.getRequestAsArray(builder.build().toString());
        return jArrToTimeline(jArr, apiRoutine, url);
    }


    @Override
    public MbMessage updateStatus(String message, String statusId, String inReplyToId, Uri mediaUri) throws ConnectionException {
        JSONObject formParams = new JSONObject();
        try {
            formParams.put("status", message);
{'fraction_non_alphanumeric': 0.08742742295667709, 'fraction_numerical': 0.0027914247431889234, 'mean_word_length': 2.100380754586362, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4741528', 'n_tokens_mistral': 2474, 'n_tokens_neox': 2348, 'n_words': 574}