• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "notification_conversational_message.h"
17 
18 #include "ans_log_wrapper.h"
19 #include "message_user.h"                         // for MessageUser
20 #include "nlohmann/json.hpp"                      // for json, basic_json<>:...
21 #include "notification_json_convert.h"            // for NotificationJsonCon...
22 #include "parcel.h"                               // for Parcel
23 #include "uri.h"                                  // for Uri
24 
25 namespace OHOS {
26 namespace Notification {
NotificationConversationalMessage(const std::string & text,int64_t timestamp,const MessageUser & sender)27 NotificationConversationalMessage::NotificationConversationalMessage(
28     const std::string &text, int64_t timestamp, const MessageUser &sender)
29     : arrivedTime_(timestamp), text_(text), sender_(sender)
30 {}
31 
GetText() const32 std::string NotificationConversationalMessage::GetText() const
33 {
34     return text_;
35 }
36 
GetArrivedTime() const37 int64_t NotificationConversationalMessage::GetArrivedTime() const
38 {
39     return arrivedTime_;
40 }
41 
GetSender() const42 MessageUser NotificationConversationalMessage::GetSender() const
43 {
44     return sender_;
45 }
46 
SetData(const std::string & mimeType,const std::shared_ptr<Uri> & uri)47 void NotificationConversationalMessage::SetData(const std::string &mimeType, const std::shared_ptr<Uri> &uri)
48 {
49     mimeType_ = mimeType;
50     uri_ = uri;
51 }
52 
GetMimeType() const53 std::string NotificationConversationalMessage::GetMimeType() const
54 {
55     return mimeType_;
56 }
57 
GetUri() const58 const std::shared_ptr<Uri> NotificationConversationalMessage::GetUri() const
59 {
60     return uri_;
61 }
62 
Dump()63 std::string NotificationConversationalMessage::Dump()
64 {
65     return "NotificationConversationalMessage{ "
66             "text = " + text_ +
67             ", arrivedTime = " + std::to_string(arrivedTime_) +
68             ", mimeType = " + mimeType_ +
69             ", uri = " + (uri_ ? uri_->ToString() : "null") +
70             ", sender = " + sender_.Dump() +
71             " }";
72 }
73 
ToJson(nlohmann::json & jsonObject) const74 bool NotificationConversationalMessage::ToJson(nlohmann::json &jsonObject) const
75 {
76     jsonObject["arrivedTime"] = arrivedTime_;
77     jsonObject["text"]        = text_;
78 
79     nlohmann::json userObj;
80     if (!NotificationJsonConverter::ConvertToJson(&sender_, userObj)) {
81         ANS_LOGE("Cannot convert sender to JSON");
82         return false;
83     }
84     jsonObject["sender"] = userObj;
85 
86     jsonObject["uri"]      = uri_ ? uri_->ToString() : "";
87     jsonObject["mimeType"] = mimeType_;
88 
89     return true;
90 }
91 
FromJson(const nlohmann::json & jsonObject)92 NotificationConversationalMessage *NotificationConversationalMessage::FromJson(const nlohmann::json &jsonObject)
93 {
94     if (jsonObject.is_null() or !jsonObject.is_object()) {
95         ANS_LOGE("Invalid JSON object");
96         return nullptr;
97     }
98 
99     auto pMessage = new (std::nothrow) NotificationConversationalMessage();
100     if (pMessage == nullptr) {
101         ANS_LOGE("Failed to create conversationalMessage instance");
102         return nullptr;
103     }
104 
105     const auto &jsonEnd = jsonObject.cend();
106     if (jsonObject.find("arrivedTime") != jsonEnd && jsonObject.at("arrivedTime").is_number_integer()) {
107         pMessage->arrivedTime_ = jsonObject.at("arrivedTime").get<int64_t>();
108     }
109 
110     if (jsonObject.find("text") != jsonEnd && jsonObject.at("text").is_string()) {
111         pMessage->text_ = jsonObject.at("text").get<std::string>();
112     }
113 
114     if (jsonObject.find("sender") != jsonEnd) {
115         auto userObj = jsonObject.at("sender");
116         auto pUser   = NotificationJsonConverter::ConvertFromJson<MessageUser>(userObj);
117         if (pUser != nullptr) {
118             pMessage->sender_ = *pUser;
119 
120             delete pUser;
121             pUser = nullptr;
122         }
123     }
124 
125     if (jsonObject.find("uri") != jsonEnd && jsonObject.at("uri").is_string()) {
126         auto uriStr = jsonObject.at("uri").get<std::string>();
127         if (!uriStr.empty()) {
128             pMessage->uri_ = std::make_shared<Uri>(uriStr);
129         }
130     }
131 
132     if (jsonObject.find("mimeType") != jsonEnd && jsonObject.at("mimeType").is_string()) {
133         pMessage->mimeType_ = jsonObject.at("mimeType").get<std::string>();
134     }
135 
136     return pMessage;
137 }
138 
Marshalling(Parcel & parcel) const139 bool NotificationConversationalMessage::Marshalling(Parcel &parcel) const
140 {
141     if (!parcel.WriteInt64(arrivedTime_)) {
142         ANS_LOGE("Failed to write arrived time");
143         return false;
144     }
145 
146     if (!parcel.WriteString(text_)) {
147         ANS_LOGE("Failed to write text");
148         return false;
149     }
150 
151     if (!parcel.WriteParcelable(&sender_)) {
152         ANS_LOGE("Failed to write sender");
153         return false;
154     }
155 
156     auto valid = uri_ ? true : false;
157     if (!parcel.WriteBool(valid)) {
158         ANS_LOGE("Failed to write the flag which indicate whether uri is null");
159         return false;
160     }
161 
162     if (valid) {
163         if (!parcel.WriteParcelable(uri_.get())) {
164             ANS_LOGE("Failed to write uri");
165             return false;
166         }
167     }
168 
169     if (!parcel.WriteString(mimeType_)) {
170         ANS_LOGE("Failed to write MIME type");
171         return false;
172     }
173 
174     return true;
175 }
176 
Unmarshalling(Parcel & parcel)177 NotificationConversationalMessage *NotificationConversationalMessage::Unmarshalling(Parcel &parcel)
178 {
179     auto pMessage = new (std::nothrow) NotificationConversationalMessage();
180     if ((pMessage != nullptr) && !pMessage->ReadFromParcel(parcel)) {
181         delete pMessage;
182         pMessage = nullptr;
183     }
184 
185     return pMessage;
186 }
187 
ReadFromParcel(Parcel & parcel)188 bool NotificationConversationalMessage::ReadFromParcel(Parcel &parcel)
189 {
190     arrivedTime_ = parcel.ReadInt64();
191 
192     if (!parcel.ReadString(text_)) {
193         ANS_LOGE("Failed to read text");
194         return false;
195     }
196 
197     auto pUser = parcel.ReadParcelable<MessageUser>();
198     if (pUser == nullptr) {
199         ANS_LOGE("Failed to read sender");
200         return false;
201     }
202     sender_ = *pUser;
203     delete pUser;
204     pUser = nullptr;
205 
206     auto valid = parcel.ReadBool();
207     if (valid) {
208         uri_ = std::shared_ptr<Uri>(parcel.ReadParcelable<Uri>());
209         if (!uri_) {
210             ANS_LOGE("Failed to read uri");
211             return false;
212         }
213     }
214 
215     if (!parcel.ReadString(mimeType_)) {
216         ANS_LOGE("Failed to read MIME type");
217         return false;
218     }
219 
220     return true;
221 }
222 }  // namespace Notification
223 }  // namespace OHOS
224