aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service/search_processor.vala
blob: 3c1057ae27af96b462fce44edbcedc0b924bece8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Gee;

using Xmpp;
using Qlite;
using Dino.Entities;

namespace Dino {

public class SearchProcessor : StreamInteractionModule, Object {
    public static ModuleIdentity<SearchProcessor> IDENTITY = new ModuleIdentity<SearchProcessor>("search_processor");
    public string id { get { return IDENTITY.id; } }

    private StreamInteractor stream_interactor;
    private Database db;

    public static void start(StreamInteractor stream_interactor, Database db) {
        SearchProcessor m = new SearchProcessor(stream_interactor, db);
        stream_interactor.add_module(m);
    }

    public SearchProcessor(StreamInteractor stream_interactor, Database db) {
        this.stream_interactor = stream_interactor;
        this.db = db;
    }

    public Gee.List<Message> match_messages(string match, int offset = -1) {
        Gee.List<Message> ret = new ArrayList<Message>(Message.equals_func);
        var query = db.message
            .match(db.message.body, parse_search(match))
            .order_by(db.message.id, "DESC")
            .limit(10);
        if (offset > 0) {
            query.offset(offset);
        }
        foreach (Row row in query) {
            ret.add(new Message.from_row(db, row));
        }
        return ret;
    }

    public int count_match_messages(string match) {
        return (int)db.message.match(db.message.body, parse_search(match)).count();
    }

    private string parse_search(string search) {
        string ret = "";
        foreach(string word in search.split(" ")) {
            ret += word + "* ";
        }
        return ret;
    }
}

}