<commit_before>%%
%%  U.C.Engine - Unified Colloboration Engine
%%  Copyright (C) 2011 af83
%%
%%  This program is free software: you can redistribute it and/or modify
%%  it under the terms of the GNU Affero General Public License as published by
%%  the Free Software Foundation, either version 3 of the License, or
%%  (at your option) any later version.
%%
%%  This program is distributed in the hope that it will be useful,
%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%%  GNU Affero General Public License for more details.
%%
%%  You should have received a copy of the GNU Affero General Public License
%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
%%
-module(uce_presence).

-author('<EMAIL>').

-export([add/2,
         all/1,
         get/2,
         delete/2,
         update/2,
         exists/2,
         check/3,
         assert/3,
         join/3,
         leave/3]).

-include("uce.hrl").

add(Domain, #uce_presence{id={none,Domain}}=Presence) ->
    add(Domain, Presence#uce_presence{id={utils:random(),Domain}});
add(Domain, #uce_presence{last_activity=0}=Presence) ->
    add(Domain, Presence#uce_presence{last_activity=utils:now()});
add(Domain, #uce_presence{timeout=0}=Presence) ->
    add(Domain, Presence#uce_presence{timeout=config:get(presence_timeout)});
add(Domain, #uce_presence{}=Presence) ->
    (db:get(?MODULE, Domain)):add(Domain, Presence).

get(Domain, Id) ->
    (db:get(?MODULE, Domain)):get(Domain, Id).

all(Domain) ->
    (db:get(?MODULE, Domain)):all(Domain).

delete(Domain, Id) ->
    case exists(Domain, Id) of
        false ->
            throw({error, not_found});
        true ->
            (db:get(?MODULE, Domain)):delete(Domain, Id)
    end.

update(Domain, #uce_presence{id={Id, Domain}}=Presence) ->
    case exists(Domain, Id) of
        false ->
            throw({error, not_found});
        true ->
            (db:get(?MODULE, Domain)):update(Domain, Presence)
    end.

exists(Domain, Id) ->
    case catch get(Domain, Id) of
        {error, not_found} ->
            false;
        {error, Reason} ->
            throw({error, Reason});
        {ok, _} ->
            true
    end.

assert(Domain, Uid, Sid) ->
    case check(Domain, {Uid, Domain}, Sid) of
        {ok, true} ->
            {ok, true};
        {ok, false} ->
            throw({error, unauthorized})
    end.

check(Domain, User, Sid) when is_list(User) ->
    {ok, RecordUser} = uce_user:get(Domain, User),
    UserId = RecordUser#uce_user.id,
    {ok, Record} = uce_presence:get(Domain, Sid),
    case Record#uce_presence.user of
        UserId ->
            uce_presence:update(Domain, Record#uce_presence{last_activity=utils:now()}),
            {ok, true};
        _OtherUserId ->
            {ok, false}
    end;
check(Domain, User, Sid) ->
    {ok, Record} = uce_presence:get(Domain, Sid),
    case Record#uce_presence.user of
        User ->
            uce_presence:update(Domain, Record#uce_presence{last_activity=utils:now()}),
            {ok, true};
        _OtherUser ->
            {ok, false}
    end.

join(Domain, Sid, Meeting) ->
    {ok, Record} = (db:get(?MODULE, Domain)):get(Domain, Sid),
    case lists:member(Meeting, Record#uce_presence.meetings) of
        true ->
            {ok, updated};
        false ->
            Meetings = Record#uce_presence.meetings ++ [Meeting],
            (db:get(?MODULE, Domain)):update(Domain, Record#uce_presence{meetings=Meetings})
    end.

leave(Domain, Sid, Meeting) ->
    {ok, Record} = (db:get(?MODULE, Domain)):get(Domain, Sid),
    Meetings = lists:delete(Meeting, Record#uce_presence.meetings),
    (db:get(?MODULE, Domain)):update(Domain, Record#uce_presence{meetings=Meetings}).<commit_msg>Cleanup uce_presence.<commit_after>%%
%%  U.C.Engine - Unified Colloboration Engine
%%  Copyright (C) 2011 af83
%%
%%  This program is free software: you can redistribute it and/or modify
%%  it under the terms of the GNU Affero General Public License as published by
%%  the Free Software Foundation, either version 3 of the License, or
%%  (at your option) any later version.
%%
%%  This program is distributed in the hope that it will be useful,
%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%%  GNU Affero General Public License for more details.
%%
%%  You should have received a copy of the GNU Affero General Public License
%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
%%
-module(uce_presence).

-author('<EMAIL>').

-export([add/2,
         all/1,
         get/2,
         delete/2,
         update/2,
         exists/2,
         check/3,
         assert/3,
         join/3,
         leave/3]).

-include("uce.hrl").

add(Domain, #uce_presence{id={none,Domain}}=Presence) ->
    add(Domain, Presence#uce_presence{id={utils:random(),Domain}});
add(Domain, #uce_presence{last_activity=0}=Presence) ->
    add(Domain, Presence#uce_presence{last_activity=utils:now()});
add(Domain, #uce_presence{timeout=0}=Presence) ->
    add(Domain, Presence#uce_presence{timeout=config:get(presence_timeout)});
add(Domain, #uce_presence{}=Presence) ->
    (db:get(?MODULE, Domain)):add(Domain, Presence).

get(Domain, Id) ->
    (db:get(?MODULE, Domain)):get(Domain, Id).

all(Domain) ->
    (db:get(?MODULE, Domain)):all(Domain).

delete(Domain, Id) ->
    case exists(Domain, Id) of
        false ->
            throw({error, not_found});
        true ->
            (db:get(?MODULE, Domain)):delete(Domain, Id)
    end.

update(Domain, #uce_presence{id={Id, Domain}}=Presence) ->
    case exists(Domain, Id) of
        false ->
            throw({error, not_found});
        true ->
            (db:get(?MODULE, Domain)):update(Domain, Presence)
    end.

exists(Domain, Id) ->
    case catch get(Domain, Id) of
        {error, not_found} ->
            false;
        {error, Reason} ->
            throw({error, Reason});
        {ok, _} ->
            true
    end.

assert(Domain, Uid, Sid) ->
    case check(Domain, Uid, Sid) of
        {ok, true} ->
            {ok, true};
        {ok, false} ->
            throw({error, unauthorized})
    end.

check(Domain, Uid, Sid) ->
    {ok, Record} = uce_presence:get(Domain, Sid),
    case Record#uce_presence.user of
        {Uid, Domain} ->
            uce_presence:update(Domain, Record#uce_presence{last_activity=utils:now()}),
            {ok, true};
        _OtherUser ->
            {ok, false}
    end.

join(Domain, Sid, Meeting) ->
    {ok, Record} = (db:get(?MODULE, Domain)):get(Domain, Sid),
    case lists:member(Meeting, Record#uce_presence.meetings) of
        true ->
            {ok, updated};
        false ->
            Meetings = Record#uce_presence.meetings ++ [Meeting],
            (db:get(?MODULE, Domain)):update(Domain, Record#uce_presence{meetings=Meetings})
    end.

leave(Domain, Sid, Meeting) ->
    {ok, Record} = (db:get(?MODULE, Domain)):get(Domain, Sid),
    Meetings = lists:delete(Meeting, Record#uce_presence.meetings),
    (db:get(?MODULE, Domain)):update(Domain, Record#uce_presence{meetings=Meetings}).
{'fraction_non_alphanumeric': 0.1492930413085667, 'fraction_numerical': 0.005267535347934572, 'mean_word_length': 2.868632707774799, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5984747', 'n_tokens_mistral': 2372, 'n_tokens_neox': 2293, 'n_words': 565}