ApiUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.utils;
  21. import android.net.Uri;
  22. import android.text.TextUtils;
  23. import android.util.Log;
  24. import com.nextcloud.talk.BuildConfig;
  25. import com.nextcloud.talk.R;
  26. import com.nextcloud.talk.application.NextcloudTalkApplication;
  27. import com.nextcloud.talk.models.RetrofitBucket;
  28. import com.nextcloud.talk.models.database.UserEntity;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. import androidx.annotation.DimenRes;
  32. import androidx.annotation.Nullable;
  33. import okhttp3.Credentials;
  34. public class ApiUtils {
  35. private static final String TAG = "ApiUtils";
  36. private static final String ocsApiVersion = "/ocs/v2.php";
  37. private static final String spreedApiVersion = "/apps/spreed/api/v1";
  38. private static final String spreedApiBase = ocsApiVersion + "/apps/spreed/api/v";
  39. private static final String userAgent = "Mozilla/5.0 (Android) Nextcloud-Talk v";
  40. public static String getUserAgent() {
  41. return userAgent + BuildConfig.VERSION_NAME;
  42. }
  43. /**
  44. * @deprecated This is only supported on API v1-3, in API v4+ please use
  45. * {@link ApiUtils#getUrlForAttendees(int, String, String)} instead.
  46. */
  47. @Deprecated
  48. public static String getUrlForRemovingParticipantFromConversation(String baseUrl, String roomToken, boolean isGuest) {
  49. String url = getUrlForParticipants(1, baseUrl, roomToken);
  50. if (isGuest) {
  51. url += "/guests";
  52. }
  53. return url;
  54. }
  55. public static RetrofitBucket getRetrofitBucketForContactsSearch(String baseUrl, @Nullable String searchQuery) {
  56. RetrofitBucket retrofitBucket = new RetrofitBucket();
  57. retrofitBucket.setUrl(baseUrl + ocsApiVersion + "/apps/files_sharing/api/v1/sharees");
  58. Map<String, String> queryMap = new HashMap<>();
  59. if (searchQuery == null) {
  60. searchQuery = "";
  61. }
  62. queryMap.put("format", "json");
  63. queryMap.put("search", searchQuery);
  64. queryMap.put("itemType", "call");
  65. retrofitBucket.setQueryMap(queryMap);
  66. return retrofitBucket;
  67. }
  68. public static String getUrlForFilePreviewWithRemotePath(String baseUrl, String remotePath, int px) {
  69. return baseUrl + "/index.php/core/preview.png?file="
  70. + Uri.encode(remotePath, "UTF-8")
  71. + "&x=" + px + "&y=" + px + "&a=1&mode=cover&forceIcon=1";
  72. }
  73. public static String getUrlForFilePreviewWithFileId(String baseUrl, String fileId, int px) {
  74. return baseUrl + "/index.php/core/preview?fileId="
  75. + fileId + "&x=" + px + "&y=" + px + "&a=1&mode=cover&forceIcon=1";
  76. }
  77. public static String getSharingUrl(String baseUrl) {
  78. return baseUrl + ocsApiVersion + "/apps/files_sharing/api/v1/shares";
  79. }
  80. public static RetrofitBucket getRetrofitBucketForContactsSearchFor14(String baseUrl, @Nullable String searchQuery) {
  81. RetrofitBucket retrofitBucket = getRetrofitBucketForContactsSearch(baseUrl, searchQuery);
  82. retrofitBucket.setUrl(baseUrl + ocsApiVersion + "/core/autocomplete/get");
  83. retrofitBucket.getQueryMap().put("itemId", "new");
  84. return retrofitBucket;
  85. }
  86. public static String getUrlForCapabilities(String baseUrl) {
  87. return baseUrl + ocsApiVersion + "/cloud/capabilities";
  88. }
  89. public static int getConversationApiVersion(UserEntity capabilities, int[] versions) throws NoSupportedApiException {
  90. boolean hasApiV4 = false;
  91. for (int version : versions) {
  92. hasApiV4 |= version == 4;
  93. }
  94. if (!hasApiV4) {
  95. Exception e = new Exception("Api call did not try conversation-v4 api");
  96. Log.d(TAG, e.getMessage(), e);
  97. }
  98. for (int version : versions) {
  99. if (capabilities.hasSpreedFeatureCapability("conversation-v" + version)) {
  100. return version;
  101. }
  102. // Fallback for old API versions
  103. if ((version == 1 || version == 2)) {
  104. if (capabilities.hasSpreedFeatureCapability("conversation-v2")) {
  105. return version;
  106. }
  107. if (version == 1 && capabilities.hasSpreedFeatureCapability("conversation")) {
  108. return version;
  109. }
  110. }
  111. }
  112. throw new NoSupportedApiException();
  113. }
  114. public static int getSignalingApiVersion(UserEntity capabilities, int[] versions) throws NoSupportedApiException {
  115. for (int version : versions) {
  116. if (version == 2 && capabilities.hasSpreedFeatureCapability("sip-support")) {
  117. return version;
  118. }
  119. if (version == 1) {
  120. // Has no capability, we just assume it is always there for now.
  121. return version;
  122. }
  123. }
  124. throw new NoSupportedApiException();
  125. }
  126. public static int getChatApiVersion(UserEntity capabilities, int[] versions) throws NoSupportedApiException {
  127. for (int version : versions) {
  128. if (version == 1 && capabilities.hasSpreedFeatureCapability("chat-v2")) {
  129. // Do not question that chat-v2 capability shows the availability of api/v1/ endpoint *see no evil*
  130. return version;
  131. }
  132. }
  133. throw new NoSupportedApiException();
  134. }
  135. protected static String getUrlForApi(int version, String baseUrl) {
  136. return baseUrl + spreedApiBase + version;
  137. }
  138. public static String getUrlForRooms(int version, String baseUrl) {
  139. return getUrlForApi(version, baseUrl) + "/room";
  140. }
  141. public static String getUrlForRoom(int version, String baseUrl, String token) {
  142. return getUrlForRooms(version, baseUrl) + "/" + token;
  143. }
  144. public static String getUrlForAttendees(int version, String baseUrl, String token) {
  145. return getUrlForRoom(version, baseUrl, token) + "/attendees";
  146. }
  147. public static String getUrlForParticipants(int version, String baseUrl, String token) {
  148. return getUrlForRoom(version, baseUrl, token) + "/participants";
  149. }
  150. public static String getUrlForParticipantsActive(int version, String baseUrl, String token) {
  151. return getUrlForParticipants(version, baseUrl, token) + "/active";
  152. }
  153. public static String getUrlForParticipantsSelf(int version, String baseUrl, String token) {
  154. return getUrlForParticipants(version, baseUrl, token) + "/self";
  155. }
  156. public static String getUrlForRoomFavorite(int version, String baseUrl, String token) {
  157. return getUrlForRoom(version, baseUrl, token) + "/favorite";
  158. }
  159. public static String getUrlForRoomModerators(int version, String baseUrl, String token) {
  160. return getUrlForRoom(version, baseUrl, token) + "/moderators";
  161. }
  162. public static String getUrlForRoomNotificationLevel(int version, String baseUrl, String token) {
  163. return getUrlForRoom(version, baseUrl, token) + "/notify";
  164. }
  165. public static String getUrlForRoomPublic(int version, String baseUrl, String token) {
  166. return getUrlForRoom(version, baseUrl, token) + "/public";
  167. }
  168. public static String getUrlForRoomPassword(int version, String baseUrl, String token) {
  169. return getUrlForRoom(version, baseUrl, token) + "/password";
  170. }
  171. public static String getUrlForRoomReadOnlyState(int version, String baseUrl, String token) {
  172. return getUrlForRoom(version, baseUrl, token) + "/read-only";
  173. }
  174. public static String getUrlForRoomWebinaryLobby(int version, String baseUrl, String token) {
  175. return getUrlForRoom(version, baseUrl, token) + "/webinary/lobby";
  176. }
  177. public static String getUrlForCall(int version, String baseUrl, String token) {
  178. return getUrlForApi(version, baseUrl) + "/call/" + token;
  179. }
  180. public static String getUrlForChat(int version, String baseUrl, String token) {
  181. return getUrlForApi(version, baseUrl) + "/chat/" + token;
  182. }
  183. public static String getUrlForMentionSuggestions(int version, String baseUrl, String token) {
  184. return getUrlForChat(version, baseUrl, token) + "/mentions";
  185. }
  186. public static String getUrlForChatMessage(int version, String baseUrl, String token, String messageId) {
  187. return getUrlForChat(version, baseUrl, token) + "/" + messageId;
  188. }
  189. public static String getUrlForSignaling(int version, String baseUrl) {
  190. return getUrlForApi(version, baseUrl) + "/signaling";
  191. }
  192. public static String getUrlForSignalingBackend(int version, String baseUrl) {
  193. return getUrlForSignaling(version, baseUrl) + "/backend";
  194. }
  195. public static String getUrlForSignalingSettings(int version, String baseUrl) {
  196. return getUrlForSignaling(version, baseUrl) + "/settings";
  197. }
  198. public static String getUrlForSignaling(int version, String baseUrl, String token) {
  199. return getUrlForSignaling(version, baseUrl) + "/" + token;
  200. }
  201. public static RetrofitBucket getRetrofitBucketForCreateRoom(int version, String baseUrl, String roomType,
  202. @Nullable String invite,
  203. @Nullable String conversationName) {
  204. RetrofitBucket retrofitBucket = new RetrofitBucket();
  205. retrofitBucket.setUrl(getUrlForRooms(version, baseUrl));
  206. Map<String, String> queryMap = new HashMap<>();
  207. queryMap.put("roomType", roomType);
  208. if (invite != null) {
  209. queryMap.put("invite", invite);
  210. }
  211. if (conversationName != null) {
  212. queryMap.put("roomName", conversationName);
  213. }
  214. retrofitBucket.setQueryMap(queryMap);
  215. return retrofitBucket;
  216. }
  217. public static RetrofitBucket getRetrofitBucketForAddParticipant(int version, String baseUrl, String token, String user) {
  218. RetrofitBucket retrofitBucket = new RetrofitBucket();
  219. retrofitBucket.setUrl(getUrlForParticipants(version, baseUrl, token));
  220. Map<String, String> queryMap = new HashMap<>();
  221. queryMap.put("newParticipant", user);
  222. retrofitBucket.setQueryMap(queryMap);
  223. return retrofitBucket;
  224. }
  225. public static RetrofitBucket getRetrofitBucketForAddGroupParticipant(int version, String baseUrl, String token, String group) {
  226. RetrofitBucket retrofitBucket = getRetrofitBucketForAddParticipant(version, baseUrl, token, group);
  227. retrofitBucket.getQueryMap().put("source", "groups");
  228. return retrofitBucket;
  229. }
  230. public static RetrofitBucket getRetrofitBucketForAddMailParticipant(int version, String baseUrl, String token, String mail) {
  231. RetrofitBucket retrofitBucket = getRetrofitBucketForAddParticipant(version, baseUrl, token, mail);
  232. retrofitBucket.getQueryMap().put("source", "emails");
  233. return retrofitBucket;
  234. }
  235. /**
  236. * @deprecated Method is only needed before Talk 4 which is from 2018 => todrop
  237. */
  238. @Deprecated
  239. public static String getUrlForCallPing(String baseUrl, String token) {
  240. return getUrlForCall(1, baseUrl, token) + "/ping";
  241. }
  242. public static String getUrlForUserProfile(String baseUrl) {
  243. return baseUrl + ocsApiVersion + "/cloud/user";
  244. }
  245. public static String getUrlForUserData(String baseUrl, String userId) {
  246. return baseUrl + ocsApiVersion + "/cloud/users/" + userId;
  247. }
  248. public static String getUrlForUserSettings(String baseUrl) {
  249. // FIXME Introduce API version
  250. return baseUrl + ocsApiVersion + spreedApiVersion + "/settings/user";
  251. }
  252. public static String getUrlPostfixForStatus() {
  253. return "/status.php";
  254. }
  255. public static String getUrlForAvatarWithNameAndPixels(String baseUrl, String name, int avatarSize) {
  256. return baseUrl + "/index.php/avatar/" + Uri.encode(name) + "/" + avatarSize;
  257. }
  258. public static String getUrlForAvatarWithName(String baseUrl, String name, @DimenRes int avatarSize) {
  259. avatarSize = Math.round(NextcloudTalkApplication
  260. .Companion.getSharedApplication().getResources().getDimension(avatarSize));
  261. return baseUrl + "/index.php/avatar/" + Uri.encode(name) + "/" + avatarSize;
  262. }
  263. public static String getUrlForAvatarWithNameForGuests(String baseUrl, String name,
  264. @DimenRes int avatarSize) {
  265. avatarSize = Math.round(NextcloudTalkApplication
  266. .Companion.getSharedApplication().getResources().getDimension(avatarSize));
  267. return baseUrl + "/index.php/avatar/guest/" + Uri.encode(name) + "/" + avatarSize;
  268. }
  269. public static String getCredentials(String username, String token) {
  270. if (TextUtils.isEmpty(username) && TextUtils.isEmpty(token)) {
  271. return null;
  272. }
  273. return Credentials.basic(username, token);
  274. }
  275. public static String getUrlNextcloudPush(String baseUrl) {
  276. return baseUrl + ocsApiVersion + "/apps/notifications/api/v2/push";
  277. }
  278. public static String getUrlPushProxy() {
  279. return NextcloudTalkApplication.Companion.getSharedApplication().
  280. getApplicationContext().getResources().getString(R.string.nc_push_server_url) + "/devices";
  281. }
  282. public static String getUrlForNotificationWithId(String baseUrl, String notificationId) {
  283. return baseUrl + ocsApiVersion + "/apps/notifications/api/v2/notifications/" + notificationId;
  284. }
  285. public static String getUrlForSearchByNumber(String baseUrl) {
  286. return baseUrl + ocsApiVersion + "/cloud/users/search/by-phone";
  287. }
  288. public static String getUrlForFileUpload(String baseUrl, String user, String attachmentFolder, String filename) {
  289. return baseUrl + "/remote.php/dav/files/" + user + attachmentFolder + "/" + filename;
  290. }
  291. public static String getUrlForFileDownload(String baseUrl, String user, String remotePath) {
  292. return baseUrl + "/remote.php/dav/files/" + user + "/" + remotePath;
  293. }
  294. public static String getUrlForTempAvatar(String baseUrl) {
  295. return baseUrl + ocsApiVersion + "/apps/spreed/temp-user-avatar";
  296. }
  297. public static String getUrlForUserFields(String baseUrl) {
  298. return baseUrl + ocsApiVersion + "/cloud/user/fields";
  299. }
  300. }