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 #include "ans_log_wrapper.h"
18
19 namespace OHOS {
20 namespace Notification {
NotificationConversationalMessage(const std::string & text,int64_t timestamp,const MessageUser & sender)21 NotificationConversationalMessage::NotificationConversationalMessage(
22 const std::string &text, int64_t timestamp, const MessageUser &sender)
23 : arrivedTime_(timestamp), text_(text), sender_(sender)
24 {}
25
GetText() const26 std::string NotificationConversationalMessage::GetText() const
27 {
28 return text_;
29 }
30
GetArrivedTime() const31 int64_t NotificationConversationalMessage::GetArrivedTime() const
32 {
33 return arrivedTime_;
34 }
35
GetSender() const36 MessageUser NotificationConversationalMessage::GetSender() const
37 {
38 return sender_;
39 }
40
SetData(const std::string & mimeType,const std::shared_ptr<Uri> & uri)41 void NotificationConversationalMessage::SetData(const std::string &mimeType, const std::shared_ptr<Uri> &uri)
42 {
43 mimeType_ = mimeType;
44 uri_ = uri;
45 }
46
GetMimeType() const47 std::string NotificationConversationalMessage::GetMimeType() const
48 {
49 return mimeType_;
50 }
51
GetUri() const52 const std::shared_ptr<Uri> NotificationConversationalMessage::GetUri() const
53 {
54 return uri_;
55 }
56
Dump()57 std::string NotificationConversationalMessage::Dump()
58 {
59 return "NotificationConversationalMessage{ "
60 "text = " + text_ +
61 ", arrivedTime = " + std::to_string(arrivedTime_) +
62 ", mimeType = " + mimeType_ +
63 ", uri = " + (uri_ ? uri_->ToString() : "null") +
64 ", sender = " + sender_.Dump() +
65 " }";
66 }
67
ToJson(nlohmann::json & jsonObject) const68 bool NotificationConversationalMessage::ToJson(nlohmann::json &jsonObject) const
69 {
70 jsonObject["arrivedTime"] = arrivedTime_;
71 jsonObject["text"] = text_;
72
73 nlohmann::json userObj;
74 if (!NotificationJsonConverter::ConvertToJosn(&sender_, userObj)) {
75 ANS_LOGE("Cannot convert sender to JSON");
76 return false;
77 }
78 jsonObject["sender"] = userObj;
79
80 jsonObject["uri"] = uri_ ? uri_->ToString() : "";
81 jsonObject["mimeType"] = mimeType_;
82
83 return true;
84 }
85
FromJson(const nlohmann::json & jsonObject)86 NotificationConversationalMessage *NotificationConversationalMessage::FromJson(const nlohmann::json &jsonObject)
87 {
88 if (jsonObject.is_null() or !jsonObject.is_object()) {
89 ANS_LOGE("Invalid JSON object");
90 return nullptr;
91 }
92
93 auto pMessage = new (std::nothrow) NotificationConversationalMessage();
94 if (pMessage == nullptr) {
95 ANS_LOGE("Failed to create conversationalMessage instance");
96 return nullptr;
97 }
98
99 const auto &jsonEnd = jsonObject.cend();
100 if (jsonObject.find("arrivedTime") != jsonEnd) {
101 pMessage->arrivedTime_ = jsonObject.at("arrivedTime").get<int64_t>();
102 }
103
104 if (jsonObject.find("text") != jsonEnd) {
105 pMessage->text_ = jsonObject.at("text").get<std::string>();
106 }
107
108 if (jsonObject.find("sender") != jsonEnd) {
109 auto userObj = jsonObject.at("sender");
110 auto pUser = NotificationJsonConverter::ConvertFromJosn<MessageUser>(userObj);
111 if (pUser != nullptr) {
112 pMessage->sender_ = *pUser;
113
114 delete pUser;
115 pUser = nullptr;
116 }
117 }
118
119 if (jsonObject.find("uri") != jsonEnd) {
120 auto uriStr = jsonObject.at("uri").get<std::string>();
121 if (!uriStr.empty()) {
122 pMessage->uri_ = std::make_shared<Uri>(uriStr);
123 }
124 }
125
126 if (jsonObject.find("mimeType") != jsonEnd) {
127 pMessage->mimeType_ = jsonObject.at("mimeType").get<std::string>();
128 }
129
130 return pMessage;
131 }
132
Marshalling(Parcel & parcel) const133 bool NotificationConversationalMessage::Marshalling(Parcel &parcel) const
134 {
135 if (!parcel.WriteInt64(arrivedTime_)) {
136 ANS_LOGE("Failed to write arrived time");
137 return false;
138 }
139
140 if (!parcel.WriteString(text_)) {
141 ANS_LOGE("Failed to write text");
142 return false;
143 }
144
145 if (!parcel.WriteParcelable(&sender_)) {
146 ANS_LOGE("Failed to write sender");
147 return false;
148 }
149
150 auto valid = uri_ ? true : false;
151 if (!parcel.WriteBool(valid)) {
152 ANS_LOGE("Failed to write the flag which indicate whether uri is null");
153 return false;
154 }
155
156 if (valid) {
157 if (!parcel.WriteParcelable(uri_.get())) {
158 ANS_LOGE("Failed to write uri");
159 return false;
160 }
161 }
162
163 if (!parcel.WriteString(mimeType_)) {
164 ANS_LOGE("Failed to write MIME type");
165 return false;
166 }
167
168 return true;
169 }
170
Unmarshalling(Parcel & parcel)171 NotificationConversationalMessage *NotificationConversationalMessage::Unmarshalling(Parcel &parcel)
172 {
173 auto pMessage = new (std::nothrow) NotificationConversationalMessage();
174 if ((pMessage != nullptr) && !pMessage->ReadFromParcel(parcel)) {
175 delete pMessage;
176 pMessage = nullptr;
177 }
178
179 return pMessage;
180 }
181
ReadFromParcel(Parcel & parcel)182 bool NotificationConversationalMessage::ReadFromParcel(Parcel &parcel)
183 {
184 arrivedTime_ = parcel.ReadInt64();
185
186 if (!parcel.ReadString(text_)) {
187 ANS_LOGE("Failed to read text");
188 return false;
189 }
190
191 auto pUser = parcel.ReadParcelable<MessageUser>();
192 if (pUser == nullptr) {
193 ANS_LOGE("Failed to read sender");
194 return false;
195 }
196 sender_ = *pUser;
197
198 auto valid = parcel.ReadBool();
199 if (valid) {
200 uri_ = std::shared_ptr<Uri>(parcel.ReadParcelable<Uri>());
201 if (!uri_) {
202 ANS_LOGE("Failed to read uri");
203 return false;
204 }
205 }
206
207 if (!parcel.ReadString(mimeType_)) {
208 ANS_LOGE("Failed to read MIME type");
209 return false;
210 }
211
212 return true;
213 }
214 } // namespace Notification
215 } // namespace OHOS
216