1 /* 2 * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>. 3 * Copyright 2016-2019, Andrew Lindesay <apl@lindesay.co.nz>. 4 * All rights reserved. Distributed under the terms of the MIT License. 5 */ 6 #ifndef WEB_APP_INTERFACE_H 7 #define WEB_APP_INTERFACE_H 8 9 10 #include <Application.h> 11 #include <JsonWriter.h> 12 #include <String.h> 13 #include <package/PackageVersion.h> 14 15 #include "List.h" 16 #include "UserCredentials.h" 17 #include "UserDetail.h" 18 #include "UserUsageConditions.h" 19 20 21 class BDataIO; 22 class BMessage; 23 using BPackageKit::BPackageVersion; 24 25 typedef List<BString, false> StringList; 26 27 28 /*! These are error codes that are sent back to the client from the server */ 29 30 #define ERROR_CODE_NONE 0 31 #define ERROR_CODE_VALIDATION -32800 32 #define ERROR_CODE_OBJECTNOTFOUND -32801 33 #define ERROR_CODE_CAPTCHABADRESPONSE -32802 34 #define ERROR_CODE_AUTHORIZATIONFAILURE -32803 35 #define ERROR_CODE_BADPKGICON -32804 36 #define ERROR_CODE_LIMITEXCEEDED -32805 37 #define ERROR_CODE_AUTHORIZATIONRULECONFLICT -32806 38 39 /*! This constant can be used to indicate the lack of a rating. */ 40 41 #define RATING_NONE -1 42 43 44 class WebAppInterface { 45 public: 46 WebAppInterface(); 47 WebAppInterface(const WebAppInterface& other); 48 virtual ~WebAppInterface(); 49 50 WebAppInterface& operator=(const WebAppInterface& other); 51 52 void SetAuthorization(const UserCredentials& value); 53 const BString& Nickname() const; 54 55 status_t GetChangelog( 56 const BString& packageName, 57 BMessage& message); 58 59 status_t RetreiveUserRatingsForPackageForDisplay( 60 const BString& packageName, 61 const BString& webAppRepositoryCode, 62 int resultOffset, int maxResults, 63 BMessage& message); 64 65 status_t RetreiveUserRatingForPackageAndVersionByUser( 66 const BString& packageName, 67 const BPackageVersion& version, 68 const BString& architecture, 69 const BString& repositoryCode, 70 const BString& userNickname, 71 BMessage& message); 72 73 status_t CreateUserRating( 74 const BString& packageName, 75 const BPackageVersion& version, 76 const BString& architecture, 77 const BString& repositoryCode, 78 const BString& languageCode, 79 const BString& comment, 80 const BString& stability, 81 int rating, 82 BMessage& message); 83 84 status_t UpdateUserRating( 85 const BString& ratingID, 86 const BString& languageCode, 87 const BString& comment, 88 const BString& stability, 89 int rating, bool active, 90 BMessage& message); 91 92 status_t RetrieveUserDetailForCredentials( 93 const UserCredentials& credentials, 94 UserDetail& userDetail); 95 96 status_t RetrieveCurrentUserDetail( 97 UserDetail& userDetail); 98 99 status_t RetrieveUserUsageConditions( 100 const BString& code, 101 UserUsageConditions& conditions); 102 103 status_t RetrieveScreenshot( 104 const BString& code, 105 int32 width, int32 height, 106 BDataIO* stream); 107 108 status_t RequestCaptcha(BMessage& message); 109 110 status_t CreateUser(const BString& nickName, 111 const BString& passwordClear, 112 const BString& email, 113 const BString& captchaToken, 114 const BString& captchaResponse, 115 const BString& languageCode, 116 const BString& userUsageConditionsCode, 117 BMessage& message); 118 119 status_t AuthenticateUser(const BString& nickName, 120 const BString& passwordClear, 121 BMessage& message); 122 123 static int32 ErrorCodeFromResponse(BMessage& response); 124 125 private: 126 static status_t _UnpackUserDetails( 127 BMessage& responseEnvelopeMessage, 128 UserDetail& userDetail); 129 130 status_t _RetrieveUserUsageConditionsMeta( 131 const BString& code, BMessage& message); 132 status_t _RetrieveUserUsageConditionsCopy( 133 const BString& code, BDataIO* stream); 134 135 void _WriteStandardJsonRpcEnvelopeValues( 136 BJsonWriter& writer, 137 const char* methodName); 138 status_t _SendJsonRequest(const char* domain, 139 const BString& jsonString, uint32 flags, 140 BMessage& reply) const; 141 status_t _SendJsonRequest(const char* domain, 142 UserCredentials credentials, 143 BPositionIO* requestData, 144 size_t requestDataSize, uint32 flags, 145 BMessage& reply) const; 146 status_t _SendJsonRequest(const char* domain, 147 BPositionIO* requestData, 148 size_t requestDataSize, uint32 flags, 149 BMessage& reply) const; 150 151 status_t _SendRawGetRequest( 152 const BString urlPathComponents, 153 BDataIO* stream); 154 static void _LogPayload(BPositionIO* requestData, 155 size_t size); 156 static off_t _LengthAndSeekToZero(BPositionIO* data); 157 158 private: 159 UserCredentials fCredentials; 160 static int fRequestIndex; 161 }; 162 163 164 #endif // WEB_APP_INTERFACE_H 165