blob: e6ba40f59df659bd373b22776f75b6956f788287 [file] [log] [blame]
Olivier Soldanod4311552017-11-20 15:09:53 -05001/*
2 * Copyright (C) 2017 Savoir-faire Linux Inc.
3 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#import <Foundation/Foundation.h>
21#import <api/conversation.h>
22#import <api/conversationmodel.h>
23#import <api/account.h>
24#import <api/contactmodel.h>
25#import <api/contact.h>
Anthony Léonard6f819752018-01-05 09:53:40 -050026#import <map>
Olivier Soldanod4311552017-11-20 15:09:53 -050027
28static inline NSString* bestIDForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
29{
30 auto contact = model.owner.contactModel->getContact(conv.participants[0]);
31 if (!contact.registeredName.empty())
32 return @(contact.registeredName.c_str());
33 else
34 return @(contact.profileInfo.uri.c_str());
35}
36
37static inline NSString* bestNameForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
38{
39 auto contact = model.owner.contactModel->getContact(conv.participants[0]);
40 if (!contact.profileInfo.alias.empty())
41 return @(contact.profileInfo.alias.c_str());
42 else
43 return bestIDForConversation(conv, model);
44}
Anthony Léonard6f819752018-01-05 09:53:40 -050045
46/**
47 * This function return an iterator pointing to a Conversation::Info in ConversationModel given its uid. If not found
48 * the iterator is invalid thus it needs to be checked by caller.
49 * @param uid UID of conversation being searched
50 * @param model ConversationModel in which to do the lookup
51 * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise.
52 */
53static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUid(const std::string& uid, const lrc::api::ConversationModel& model) {
54 return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(),
55 [&] (const lrc::api::conversation::Info& conv) {
56 return uid == conv.uid;
57 });
58}